datepicker.js 25 KB

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