bootstrap-datepicker.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /* =========================================================
  2. * bootstrap-datepicker.js
  3. * http://www.eyecon.ro/bootstrap-datepicker
  4. * =========================================================
  5. * Copyright 2012 Stefan Petre
  6. * Improvements by Andrew Rowls
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ========================================================= */
  20. !function( $ ) {
  21. function UTCDate(){
  22. return new Date(Date.UTC.apply(Date, arguments));
  23. }
  24. // Picker object
  25. var Datepicker = function(element, options) {
  26. var that = this;
  27. this.element = $(element);
  28. this.language = options.language||this.element.data('date-language')||"en";
  29. this.language = this.language in dates ? this.language : "en";
  30. this.format = DPGlobal.parseFormat(options.format||this.element.data('date-format')||'mm/dd/yyyy');
  31. this.picker = $(DPGlobal.template)
  32. .appendTo('body')
  33. .on({
  34. click: $.proxy(this.click, this)
  35. });
  36. this.isInput = this.element.is('input');
  37. this.component = this.element.is('.date') ? this.element.find('.add-on') : false;
  38. this.hasInput = this.component && this.element.find('input').length;
  39. if(this.component && this.component.length === 0)
  40. this.component = false;
  41. if (this.isInput) {
  42. this.element.on({
  43. focus: $.proxy(this.show, this),
  44. keyup: $.proxy(this.update, this),
  45. keydown: $.proxy(this.keydown, this)
  46. });
  47. } else {
  48. if (this.component && this.hasInput){
  49. // For components that are not readonly, allow keyboard nav
  50. this.element.find('input').on({
  51. focus: $.proxy(this.show, this),
  52. keyup: $.proxy(this.update, this),
  53. keydown: $.proxy(this.keydown, this)
  54. });
  55. this.component.on('click', $.proxy(this.show, this));
  56. } else {
  57. this.element.on('click', $.proxy(this.show, this));
  58. }
  59. }
  60. $(document).on('mousedown', function (e) {
  61. // Clicked outside the datepicker, hide it
  62. if ($(e.target).closest('.datepicker').length == 0) {
  63. that.hide();
  64. }
  65. });
  66. this.autoclose = false;
  67. if ('autoclose' in options) {
  68. this.autoclose = options.autoclose;
  69. } else if ('dateAutoclose' in this.element.data()) {
  70. this.autoclose = this.element.data('date-autoclose');
  71. }
  72. this.keyboardNavigation = true;
  73. if ('keyboardNavigation' in options) {
  74. this.keyboardNavigation = options.keyboardNavigation;
  75. } else if ('dateKeyboardNavigation' in this.element.data()) {
  76. this.keyboardNavigation = this.element.data('date-keyboard-navigation');
  77. }
  78. switch(options.startView || this.element.data('date-start-view')){
  79. case 2:
  80. case 'decade':
  81. this.viewMode = this.startViewMode = 2;
  82. break;
  83. case 1:
  84. case 'year':
  85. this.viewMode = this.startViewMode = 1;
  86. break;
  87. case 0:
  88. case 'month':
  89. default:
  90. this.viewMode = this.startViewMode = 0;
  91. break;
  92. }
  93. this.weekStart = ((options.weekStart||this.element.data('date-weekstart')||dates[this.language].weekStart||0) % 7);
  94. this.weekEnd = ((this.weekStart + 6) % 7);
  95. this.startDate = -Infinity;
  96. this.endDate = Infinity;
  97. this.setStartDate(options.startDate||this.element.data('date-startdate'));
  98. this.setEndDate(options.endDate||this.element.data('date-enddate'));
  99. this.fillDow();
  100. this.fillMonths();
  101. this.update();
  102. this.showMode();
  103. };
  104. Datepicker.prototype = {
  105. constructor: Datepicker,
  106. show: function(e) {
  107. this.picker.show();
  108. this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  109. this.update();
  110. this.place();
  111. $(window).on('resize', $.proxy(this.place, this));
  112. if (e ) {
  113. e.stopPropagation();
  114. e.preventDefault();
  115. }
  116. this.element.trigger({
  117. type: 'show',
  118. date: this.date
  119. });
  120. },
  121. hide: function(e){
  122. this.picker.hide();
  123. $(window).off('resize', this.place);
  124. this.viewMode = this.startViewMode;
  125. this.showMode();
  126. if (!this.isInput) {
  127. $(document).off('mousedown', this.hide);
  128. }
  129. if (e && e.currentTarget.value)
  130. this.setValue();
  131. this.element.trigger({
  132. type: 'hide',
  133. date: this.date
  134. });
  135. },
  136. setValue: function() {
  137. var formatted = DPGlobal.formatDate(this.date, this.format, this.language);
  138. if (!this.isInput) {
  139. if (this.component){
  140. this.element.find('input').prop('value', formatted);
  141. }
  142. this.element.data('date', formatted);
  143. } else {
  144. this.element.prop('value', formatted);
  145. }
  146. },
  147. setStartDate: function(startDate){
  148. this.startDate = startDate||-Infinity;
  149. if (this.startDate !== -Infinity) {
  150. this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language);
  151. }
  152. this.update();
  153. this.updateNavArrows();
  154. },
  155. setEndDate: function(endDate){
  156. this.endDate = endDate||Infinity;
  157. if (this.endDate !== Infinity) {
  158. this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language);
  159. }
  160. this.update();
  161. this.updateNavArrows();
  162. },
  163. place: function(){
  164. var zIndex = parseInt(this.element.parents().filter(function() {
  165. return $(this).css('z-index') != 'auto';
  166. }).first().css('z-index'))+10;
  167. var offset = this.component ? this.component.offset() : this.element.offset();
  168. this.picker.css({
  169. top: offset.top + this.height,
  170. left: offset.left,
  171. zIndex: zIndex
  172. });
  173. },
  174. update: function(){
  175. this.date = DPGlobal.parseDate(
  176. this.isInput ? this.element.prop('value') : this.element.data('date') || this.element.find('input').prop('value'),
  177. this.format, this.language
  178. );
  179. if (this.date < this.startDate) {
  180. this.viewDate = new Date(this.startDate);
  181. } else if (this.date > this.endDate) {
  182. this.viewDate = new Date(this.endDate);
  183. } else {
  184. this.viewDate = new Date(this.date);
  185. }
  186. this.fill();
  187. },
  188. fillDow: function(){
  189. var dowCnt = this.weekStart;
  190. var html = '<tr>';
  191. while (dowCnt < this.weekStart + 7) {
  192. html += '<th class="dow">'+dates[this.language].daysMin[(dowCnt++)%7]+'</th>';
  193. }
  194. html += '</tr>';
  195. this.picker.find('.datepicker-days thead').append(html);
  196. },
  197. fillMonths: function(){
  198. var html = '';
  199. var i = 0
  200. while (i < 12) {
  201. html += '<span class="month">'+dates[this.language].monthsShort[i++]+'</span>';
  202. }
  203. this.picker.find('.datepicker-months td').html(html);
  204. },
  205. fill: function() {
  206. var d = new Date(this.viewDate),
  207. year = d.getUTCFullYear(),
  208. month = d.getUTCMonth(),
  209. startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
  210. startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
  211. endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
  212. endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
  213. currentDate = this.date.valueOf();
  214. this.picker.find('.datepicker-days th:eq(1)')
  215. .text(dates[this.language].months[month]+' '+year);
  216. this.updateNavArrows();
  217. this.fillMonths();
  218. var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
  219. day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  220. prevMonth.setUTCDate(day);
  221. prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7)%7);
  222. var nextMonth = new Date(prevMonth);
  223. nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  224. nextMonth = nextMonth.valueOf();
  225. var html = [];
  226. var clsName;
  227. while(prevMonth.valueOf() < nextMonth) {
  228. if (prevMonth.getUTCDay() == this.weekStart) {
  229. html.push('<tr>');
  230. }
  231. clsName = '';
  232. if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
  233. clsName += ' old';
  234. } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
  235. clsName += ' new';
  236. }
  237. if (prevMonth.valueOf() == currentDate) {
  238. clsName += ' active';
  239. }
  240. if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate) {
  241. clsName += ' disabled';
  242. }
  243. html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');
  244. if (prevMonth.getUTCDay() == this.weekEnd) {
  245. html.push('</tr>');
  246. }
  247. prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
  248. }
  249. this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
  250. var currentYear = this.date.getUTCFullYear();
  251. var months = this.picker.find('.datepicker-months')
  252. .find('th:eq(1)')
  253. .text(year)
  254. .end()
  255. .find('span').removeClass('active');
  256. if (currentYear == year) {
  257. months.eq(this.date.getUTCMonth()).addClass('active');
  258. }
  259. if (year < startYear || year > endYear) {
  260. months.addClass('disabled');
  261. }
  262. if (year == startYear) {
  263. months.slice(0, startMonth).addClass('disabled');
  264. }
  265. if (year == endYear) {
  266. months.slice(endMonth+1).addClass('disabled');
  267. }
  268. html = '';
  269. year = parseInt(year/10, 10) * 10;
  270. var yearCont = this.picker.find('.datepicker-years')
  271. .find('th:eq(1)')
  272. .text(year + '-' + (year + 9))
  273. .end()
  274. .find('td');
  275. year -= 1;
  276. for (var i = -1; i < 11; i++) {
  277. html += '<span class="year'+(i == -1 || i == 10 ? ' old' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
  278. year += 1;
  279. }
  280. yearCont.html(html);
  281. },
  282. updateNavArrows: function() {
  283. var d = new Date(this.viewDate),
  284. year = d.getUTCFullYear(),
  285. month = d.getUTCMonth();
  286. switch (this.viewMode) {
  287. case 0:
  288. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
  289. this.picker.find('.prev').css({visibility: 'hidden'});
  290. } else {
  291. this.picker.find('.prev').css({visibility: 'visible'});
  292. }
  293. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
  294. this.picker.find('.next').css({visibility: 'hidden'});
  295. } else {
  296. this.picker.find('.next').css({visibility: 'visible'});
  297. }
  298. break;
  299. case 1:
  300. case 2:
  301. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
  302. this.picker.find('.prev').css({visibility: 'hidden'});
  303. } else {
  304. this.picker.find('.prev').css({visibility: 'visible'});
  305. }
  306. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
  307. this.picker.find('.next').css({visibility: 'hidden'});
  308. } else {
  309. this.picker.find('.next').css({visibility: 'visible'});
  310. }
  311. break;
  312. }
  313. },
  314. click: function(e) {
  315. e.stopPropagation();
  316. e.preventDefault();
  317. var target = $(e.target).closest('span, td, th');
  318. if (target.length == 1) {
  319. switch(target[0].nodeName.toLowerCase()) {
  320. case 'th':
  321. switch(target[0].className) {
  322. case 'switch':
  323. this.showMode(1);
  324. break;
  325. case 'prev':
  326. case 'next':
  327. var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  328. switch(this.viewMode){
  329. case 0:
  330. this.viewDate = this.moveMonth(this.viewDate, dir);
  331. break;
  332. case 1:
  333. case 2:
  334. this.viewDate = this.moveYear(this.viewDate, dir);
  335. break;
  336. }
  337. this.fill();
  338. break;
  339. }
  340. break;
  341. case 'span':
  342. if (!target.is('.disabled')) {
  343. this.viewDate.setUTCDate(1);
  344. if (target.is('.month')) {
  345. var month = target.parent().find('span').index(target);
  346. this.viewDate.setUTCMonth(month);
  347. this.element.trigger({
  348. type: 'changeMonth',
  349. date: this.viewDate
  350. });
  351. } else {
  352. var year = parseInt(target.text(), 10)||0;
  353. this.viewDate.setUTCFullYear(year);
  354. this.element.trigger({
  355. type: 'changeYear',
  356. date: this.viewDate
  357. });
  358. }
  359. this.showMode(-1);
  360. this.fill();
  361. }
  362. break;
  363. case 'td':
  364. if (target.is('.day') && !target.is('.disabled')){
  365. var day = parseInt(target.text(), 10)||1;
  366. var year = this.viewDate.getUTCFullYear(),
  367. month = this.viewDate.getUTCMonth();
  368. if (target.is('.old')) {
  369. if (month == 0) {
  370. month = 11;
  371. year -= 1;
  372. } else {
  373. month -= 1;
  374. }
  375. } else if (target.is('.new')) {
  376. if (month == 11) {
  377. month = 0;
  378. year += 1;
  379. } else {
  380. month += 1;
  381. }
  382. }
  383. this.date = UTCDate(year, month, day,0,0,0,0);
  384. this.viewDate = UTCDate(year, month, day,0,0,0,0);
  385. this.fill();
  386. this.setValue();
  387. this.element.trigger({
  388. type: 'changeDate',
  389. date: this.date
  390. });
  391. var element;
  392. if (this.isInput) {
  393. element = this.element;
  394. } else if (this.component){
  395. element = this.element.find('input');
  396. }
  397. if (element) {
  398. element.change();
  399. if (this.autoclose) {
  400. this.hide();
  401. }
  402. }
  403. }
  404. break;
  405. }
  406. }
  407. },
  408. moveMonth: function(date, dir){
  409. if (!dir) return date;
  410. var new_date = new Date(date.valueOf()),
  411. day = new_date.getUTCDate(),
  412. month = new_date.getUTCMonth(),
  413. mag = Math.abs(dir),
  414. new_month, test;
  415. dir = dir > 0 ? 1 : -1;
  416. if (mag == 1){
  417. test = dir == -1
  418. // If going back one month, make sure month is not current month
  419. // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  420. ? function(){ return new_date.getUTCMonth() == month; }
  421. // If going forward one month, make sure month is as expected
  422. // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  423. : function(){ return new_date.getUTCMonth() != new_month; };
  424. new_month = month + dir;
  425. new_date.setUTCMonth(new_month);
  426. // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  427. if (new_month < 0 || new_month > 11)
  428. new_month = (new_month + 12) % 12;
  429. } else {
  430. // For magnitudes >1, move one month at a time...
  431. for (var i=0; i<mag; i++)
  432. // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  433. new_date = this.moveMonth(new_date, dir);
  434. // ...then reset the day, keeping it in the new month
  435. new_month = new_date.getUTCMonth();
  436. new_date.setUTCDate(day);
  437. test = function(){ return new_month != new_date.getUTCMonth(); };
  438. }
  439. // Common date-resetting loop -- if date is beyond end of month, make it
  440. // end of month
  441. while (test()){
  442. new_date.setUTCDate(--day);
  443. new_date.setUTCMonth(new_month);
  444. }
  445. return new_date;
  446. },
  447. moveYear: function(date, dir){
  448. return this.moveMonth(date, dir*12);
  449. },
  450. dateWithinRange: function(date){
  451. return date >= this.startDate && date <= this.endDate;
  452. },
  453. keydown: function(e){
  454. if (this.picker.is(':not(:visible)')){
  455. if (e.keyCode == 27) // allow escape to hide and re-show picker
  456. this.show();
  457. return;
  458. }
  459. var dateChanged = false,
  460. dir, day, month,
  461. newDate, newViewDate;
  462. switch(e.keyCode){
  463. case 27: // escape
  464. this.hide();
  465. e.preventDefault();
  466. break;
  467. case 37: // left
  468. case 39: // right
  469. if (!this.keyboardNavigation) break;
  470. dir = e.keyCode == 37 ? -1 : 1;
  471. if (e.ctrlKey){
  472. newDate = this.moveYear(this.date, dir);
  473. newViewDate = this.moveYear(this.viewDate, dir);
  474. } else if (e.shiftKey){
  475. newDate = this.moveMonth(this.date, dir);
  476. newViewDate = this.moveMonth(this.viewDate, dir);
  477. } else {
  478. newDate = new Date(this.date);
  479. newDate.setUTCDate(this.date.getUTCDate() + dir);
  480. newViewDate = new Date(this.viewDate);
  481. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
  482. }
  483. if (this.dateWithinRange(newDate)){
  484. this.date = newDate;
  485. this.viewDate = newViewDate;
  486. this.setValue();
  487. this.update();
  488. e.preventDefault();
  489. dateChanged = true;
  490. }
  491. break;
  492. case 38: // up
  493. case 40: // down
  494. if (!this.keyboardNavigation) break;
  495. dir = e.keyCode == 38 ? -1 : 1;
  496. if (e.ctrlKey){
  497. newDate = this.moveYear(this.date, dir);
  498. newViewDate = this.moveYear(this.viewDate, dir);
  499. } else if (e.shiftKey){
  500. newDate = this.moveMonth(this.date, dir);
  501. newViewDate = this.moveMonth(this.viewDate, dir);
  502. } else {
  503. newDate = new Date(this.date);
  504. newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
  505. newViewDate = new Date(this.viewDate);
  506. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
  507. }
  508. if (this.dateWithinRange(newDate)){
  509. this.date = newDate;
  510. this.viewDate = newViewDate;
  511. this.setValue();
  512. this.update();
  513. e.preventDefault();
  514. dateChanged = true;
  515. }
  516. break;
  517. case 13: // enter
  518. this.hide();
  519. e.preventDefault();
  520. break;
  521. case 9: // tab
  522. this.hide();
  523. break;
  524. }
  525. if (dateChanged){
  526. this.element.trigger({
  527. type: 'changeDate',
  528. date: this.date
  529. });
  530. var element;
  531. if (this.isInput) {
  532. element = this.element;
  533. } else if (this.component){
  534. element = this.element.find('input');
  535. }
  536. if (element) {
  537. element.change();
  538. }
  539. }
  540. },
  541. showMode: function(dir) {
  542. if (dir) {
  543. this.viewMode = Math.max(0, Math.min(2, this.viewMode + dir));
  544. }
  545. this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  546. this.updateNavArrows();
  547. }
  548. };
  549. $.fn.datepicker = function ( option ) {
  550. var args = Array.apply(null, arguments);
  551. args.shift();
  552. return this.each(function () {
  553. var $this = $(this),
  554. data = $this.data('datepicker'),
  555. options = typeof option == 'object' && option;
  556. if (!data) {
  557. $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.datepicker.defaults,options))));
  558. }
  559. if (typeof option == 'string' && typeof data[option] == 'function') {
  560. data[option].apply(data, args);
  561. }
  562. });
  563. };
  564. $.fn.datepicker.defaults = {
  565. };
  566. $.fn.datepicker.Constructor = Datepicker;
  567. var dates = $.fn.datepicker.dates = {
  568. en: {
  569. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  570. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  571. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  572. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  573. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
  574. }
  575. }
  576. var DPGlobal = {
  577. modes: [
  578. {
  579. clsName: 'days',
  580. navFnc: 'Month',
  581. navStep: 1
  582. },
  583. {
  584. clsName: 'months',
  585. navFnc: 'FullYear',
  586. navStep: 1
  587. },
  588. {
  589. clsName: 'years',
  590. navFnc: 'FullYear',
  591. navStep: 10
  592. }],
  593. isLeapYear: function (year) {
  594. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
  595. },
  596. getDaysInMonth: function (year, month) {
  597. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
  598. },
  599. validParts: /dd?|mm?|MM?|yy(?:yy)?/g,
  600. nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\r]+/g,
  601. parseFormat: function(format){
  602. // IE treats \0 as a string end in inputs (truncating the value),
  603. // so it's a bad format delimiter, anyway
  604. var separators = format.replace(this.validParts, '\0').split('\0'),
  605. parts = format.match(this.validParts);
  606. if (!separators || !separators.length || !parts || parts.length == 0){
  607. throw new Error("Invalid date format.");
  608. }
  609. return {separators: separators, parts: parts};
  610. },
  611. parseDate: function(date, format, language) {
  612. if (date instanceof Date) return date;
  613. if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
  614. var part_re = /([-+]\d+)([dmwy])/,
  615. parts = date.match(/([-+]\d+)([dmwy])/g),
  616. part, dir;
  617. date = new Date();
  618. for (var i=0; i<parts.length; i++) {
  619. part = part_re.exec(parts[i]);
  620. dir = parseInt(part[1]);
  621. switch(part[2]){
  622. case 'd':
  623. date.setUTCDate(date.getUTCDate() + dir);
  624. break;
  625. case 'm':
  626. date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
  627. break;
  628. case 'w':
  629. date.setUTCDate(date.getUTCDate() + dir * 7);
  630. break;
  631. case 'y':
  632. date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
  633. break;
  634. }
  635. }
  636. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
  637. }
  638. var parts = date && date.match(this.nonpunctuation) || [],
  639. date = new Date(),
  640. parsed = {},
  641. setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
  642. setters_map = {
  643. yyyy: function(d,v){ return d.setUTCFullYear(v); },
  644. yy: function(d,v){ return d.setUTCFullYear(2000+v); },
  645. m: function(d,v){
  646. v -= 1;
  647. while (v<0) v += 12;
  648. v %= 12;
  649. d.setUTCMonth(v);
  650. while (d.getUTCMonth() != v)
  651. d.setUTCDate(d.getUTCDate()-1);
  652. return d;
  653. },
  654. d: function(d,v){ return d.setUTCDate(v); }
  655. },
  656. val, filtered, part;
  657. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  658. setters_map['dd'] = setters_map['d'];
  659. date = UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
  660. if (parts.length == format.parts.length) {
  661. for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  662. val = parseInt(parts[i], 10);
  663. part = format.parts[i];
  664. if (isNaN(val)) {
  665. switch(part) {
  666. case 'MM':
  667. filtered = $(dates[language].months).filter(function(){
  668. var m = this.slice(0, parts[i].length),
  669. p = parts[i].slice(0, m.length);
  670. return m == p;
  671. });
  672. val = $.inArray(filtered[0], dates[language].months) + 1;
  673. break;
  674. case 'M':
  675. filtered = $(dates[language].monthsShort).filter(function(){
  676. var m = this.slice(0, parts[i].length),
  677. p = parts[i].slice(0, m.length);
  678. return m == p;
  679. });
  680. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  681. break;
  682. }
  683. }
  684. parsed[part] = val;
  685. }
  686. for (var i=0, s; i<setters_order.length; i++){
  687. s = setters_order[i];
  688. if (s in parsed)
  689. setters_map[s](date, parsed[s])
  690. }
  691. }
  692. return date;
  693. },
  694. formatDate: function(date, format, language){
  695. var val = {
  696. d: date.getUTCDate(),
  697. m: date.getUTCMonth() + 1,
  698. M: dates[language].monthsShort[date.getUTCMonth()],
  699. MM: dates[language].months[date.getUTCMonth()],
  700. yy: date.getUTCFullYear().toString().substring(2),
  701. yyyy: date.getUTCFullYear()
  702. };
  703. val.dd = (val.d < 10 ? '0' : '') + val.d;
  704. val.mm = (val.m < 10 ? '0' : '') + val.m;
  705. var date = [],
  706. seps = $.extend([], format.separators);
  707. for (var i=0, cnt = format.parts.length; i < cnt; i++) {
  708. if (seps.length)
  709. date.push(seps.shift())
  710. date.push(val[format.parts[i]]);
  711. }
  712. return date.join('');
  713. },
  714. headTemplate: '<thead>'+
  715. '<tr>'+
  716. '<th class="prev"><i class="icon-arrow-left"/></th>'+
  717. '<th colspan="5" class="switch"></th>'+
  718. '<th class="next"><i class="icon-arrow-right"/></th>'+
  719. '</tr>'+
  720. '</thead>',
  721. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>'
  722. };
  723. DPGlobal.template = '<div class="datepicker dropdown-menu">'+
  724. '<div class="datepicker-days">'+
  725. '<table class=" table-condensed">'+
  726. DPGlobal.headTemplate+
  727. '<tbody></tbody>'+
  728. '</table>'+
  729. '</div>'+
  730. '<div class="datepicker-months">'+
  731. '<table class="table-condensed">'+
  732. DPGlobal.headTemplate+
  733. DPGlobal.contTemplate+
  734. '</table>'+
  735. '</div>'+
  736. '<div class="datepicker-years">'+
  737. '<table class="table-condensed">'+
  738. DPGlobal.headTemplate+
  739. DPGlobal.contTemplate+
  740. '</table>'+
  741. '</div>'+
  742. '</div>';
  743. }( window.jQuery );