bootstrap-datepicker.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. var $window = $(window);
  22. function UTCDate(){
  23. return new Date(Date.UTC.apply(Date, arguments));
  24. }
  25. function UTCToday(){
  26. var today = new Date();
  27. return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
  28. }
  29. // Picker object
  30. var Datepicker = function(element, options) {
  31. var that = this;
  32. this._process_options(options);
  33. this.element = $(element);
  34. this.isInline = false;
  35. this.isInput = this.element.is('input');
  36. this.component = this.element.is('.date') ? this.element.find('.add-on, .btn') : false;
  37. this.hasInput = this.component && this.element.find('input').length;
  38. if(this.component && this.component.length === 0)
  39. this.component = false;
  40. this.picker = $(DPGlobal.template);
  41. this._buildEvents();
  42. this._attachEvents();
  43. if(this.isInline) {
  44. this.picker.addClass('datepicker-inline').appendTo(this.element);
  45. } else {
  46. this.picker.addClass('datepicker-dropdown dropdown-menu');
  47. }
  48. if (this.o.rtl){
  49. this.picker.addClass('datepicker-rtl');
  50. this.picker.find('.prev i, .next i')
  51. .toggleClass('icon-arrow-left icon-arrow-right');
  52. }
  53. this.viewMode = this.o.startView;
  54. if (this.o.calendarWeeks)
  55. this.picker.find('tfoot th.today')
  56. .attr('colspan', function(i, val){
  57. return parseInt(val) + 1;
  58. });
  59. this._allow_update = false;
  60. this.setStartDate(this._o.startDate);
  61. this.setEndDate(this._o.endDate);
  62. this.setDaysOfWeekDisabled(this.o.daysOfWeekDisabled);
  63. this.fillDow();
  64. this.fillMonths();
  65. this._allow_update = true;
  66. this.update();
  67. this.showMode();
  68. if(this.isInline) {
  69. this.show();
  70. }
  71. };
  72. Datepicker.prototype = {
  73. constructor: Datepicker,
  74. _process_options: function(opts){
  75. // Store raw options for reference
  76. this._o = $.extend({}, this._o, opts);
  77. // Processed options
  78. var o = this.o = $.extend({}, this._o);
  79. // Check if "de-DE" style date is available, if not language should
  80. // fallback to 2 letter code eg "de"
  81. var lang = o.language;
  82. if (!dates[lang]) {
  83. lang = lang.split('-')[0];
  84. if (!dates[lang])
  85. lang = defaults.language;
  86. }
  87. o.language = lang;
  88. switch(o.startView){
  89. case 2:
  90. case 'decade':
  91. o.startView = 2;
  92. break;
  93. case 1:
  94. case 'year':
  95. o.startView = 1;
  96. break;
  97. default:
  98. o.startView = 0;
  99. }
  100. switch (o.minViewMode) {
  101. case 1:
  102. case 'months':
  103. o.minViewMode = 1;
  104. break;
  105. case 2:
  106. case 'years':
  107. o.minViewMode = 2;
  108. break;
  109. default:
  110. o.minViewMode = 0;
  111. }
  112. o.startView = Math.max(o.startView, o.minViewMode);
  113. o.weekStart %= 7;
  114. o.weekEnd = ((o.weekStart + 6) % 7);
  115. var format = DPGlobal.parseFormat(o.format);
  116. if (o.startDate !== -Infinity) {
  117. if (!!o.startDate) {
  118. if (o.startDate instanceof Date)
  119. o.startDate = this._local_to_utc(this._zero_time(o.startDate));
  120. else
  121. o.startDate = DPGlobal.parseDate(o.startDate, format, o.language);
  122. } else {
  123. o.startDate = -Infinity;
  124. }
  125. }
  126. if (o.endDate !== Infinity) {
  127. if (!!o.endDate) {
  128. if (o.endDate instanceof Date)
  129. o.endDate = this._local_to_utc(this._zero_time(o.endDate));
  130. else
  131. o.endDate = DPGlobal.parseDate(o.endDate, format, o.language);
  132. } else {
  133. o.endDate = Infinity;
  134. }
  135. }
  136. o.daysOfWeekDisabled = o.daysOfWeekDisabled||[];
  137. if (!$.isArray(o.daysOfWeekDisabled))
  138. o.daysOfWeekDisabled = o.daysOfWeekDisabled.split(/[,\s]*/);
  139. o.daysOfWeekDisabled = $.map(o.daysOfWeekDisabled, function (d) {
  140. return parseInt(d, 10);
  141. });
  142. var plc = String(o.orientation).toLowerCase().split(/\s+/g),
  143. _plc = o.orientation.toLowerCase();
  144. plc = $.grep(plc, function(word){
  145. return (/^auto|left|right|top|bottom$/).test(word);
  146. });
  147. o.orientation = {x: 'auto', y: 'auto'};
  148. if (!_plc || _plc === 'auto')
  149. ; // no action
  150. else if (plc.length === 1){
  151. switch(plc[0]){
  152. case 'top':
  153. case 'bottom':
  154. o.orientation.y = plc[0];
  155. break;
  156. case 'left':
  157. case 'right':
  158. o.orientation.x = plc[0];
  159. break;
  160. }
  161. }
  162. else {
  163. _plc = $.grep(plc, function(word){
  164. return (/^left|right$/).test(word);
  165. });
  166. o.orientation.x = _plc[0] || 'auto';
  167. _plc = $.grep(plc, function(word){
  168. return (/^top|bottom$/).test(word);
  169. });
  170. o.orientation.y = _plc[0] || 'auto';
  171. }
  172. },
  173. _events: [],
  174. _secondaryEvents: [],
  175. _applyEvents: function(evs){
  176. for (var i=0, el, ev; i<evs.length; i++){
  177. el = evs[i][0];
  178. ev = evs[i][1];
  179. el.on(ev);
  180. }
  181. },
  182. _unapplyEvents: function(evs){
  183. for (var i=0, el, ev; i<evs.length; i++){
  184. el = evs[i][0];
  185. ev = evs[i][1];
  186. el.off(ev);
  187. }
  188. },
  189. _buildEvents: function(){
  190. if (this.isInput) { // single input
  191. this._events = [
  192. [this.element, {
  193. focus: $.proxy(this.show, this),
  194. keyup: $.proxy(this.update, this),
  195. keydown: $.proxy(this.keydown, this)
  196. }]
  197. ];
  198. }
  199. else if (this.component && this.hasInput){ // component: input + button
  200. this._events = [
  201. // For components that are not readonly, allow keyboard nav
  202. [this.element.find('input'), {
  203. focus: $.proxy(this.show, this),
  204. keyup: $.proxy(this.update, this),
  205. keydown: $.proxy(this.keydown, this)
  206. }],
  207. [this.component, {
  208. click: $.proxy(this.show, this)
  209. }]
  210. ];
  211. }
  212. else if (this.element.is('div')) { // inline datepicker
  213. this.isInline = true;
  214. }
  215. else {
  216. this._events = [
  217. [this.element, {
  218. click: $.proxy(this.show, this)
  219. }]
  220. ];
  221. }
  222. this._secondaryEvents = [
  223. [this.picker, {
  224. click: $.proxy(this.click, this)
  225. }],
  226. [$(window), {
  227. resize: $.proxy(this.place, this)
  228. }],
  229. [$(document), {
  230. 'mousedown touchstart': $.proxy(function (e) {
  231. // Clicked outside the datepicker, hide it
  232. if (!(
  233. this.element.is(e.target) ||
  234. this.element.find(e.target).length ||
  235. this.picker.is(e.target) ||
  236. this.picker.find(e.target).length
  237. )) {
  238. this.hide();
  239. }
  240. }, this)
  241. }]
  242. ];
  243. },
  244. _attachEvents: function(){
  245. this._detachEvents();
  246. this._applyEvents(this._events);
  247. },
  248. _detachEvents: function(){
  249. this._unapplyEvents(this._events);
  250. },
  251. _attachSecondaryEvents: function(){
  252. this._detachSecondaryEvents();
  253. this._applyEvents(this._secondaryEvents);
  254. },
  255. _detachSecondaryEvents: function(){
  256. this._unapplyEvents(this._secondaryEvents);
  257. },
  258. _trigger: function(event, altdate){
  259. var date = altdate || this.date,
  260. local_date = this._utc_to_local(date);
  261. this.element.trigger({
  262. type: event,
  263. date: local_date,
  264. format: $.proxy(function(altformat){
  265. var format = altformat || this.o.format;
  266. return DPGlobal.formatDate(date, format, this.o.language);
  267. }, this)
  268. });
  269. },
  270. show: function(e) {
  271. if (!this.isInline)
  272. this.picker.appendTo('body');
  273. this.picker.show();
  274. this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  275. this.place();
  276. this._attachSecondaryEvents();
  277. if (e) {
  278. e.preventDefault();
  279. }
  280. this._trigger('show');
  281. },
  282. hide: function(e){
  283. if(this.isInline) return;
  284. if (!this.picker.is(':visible')) return;
  285. this.picker.hide().detach();
  286. this._detachSecondaryEvents();
  287. this.viewMode = this.o.startView;
  288. this.showMode();
  289. if (
  290. this.o.forceParse &&
  291. (
  292. this.isInput && this.element.val() ||
  293. this.hasInput && this.element.find('input').val()
  294. )
  295. )
  296. this.setValue();
  297. this._trigger('hide');
  298. },
  299. remove: function() {
  300. this.hide();
  301. this._detachEvents();
  302. this._detachSecondaryEvents();
  303. this.picker.remove();
  304. delete this.element.data().datepicker;
  305. if (!this.isInput) {
  306. delete this.element.data().date;
  307. }
  308. },
  309. _utc_to_local: function(utc){
  310. return new Date(utc.getTime() + (utc.getTimezoneOffset()*60000));
  311. },
  312. _local_to_utc: function(local){
  313. return new Date(local.getTime() - (local.getTimezoneOffset()*60000));
  314. },
  315. _zero_time: function(local){
  316. return new Date(local.getFullYear(), local.getMonth(), local.getDate());
  317. },
  318. _zero_utc_time: function(utc){
  319. return new Date(Date.UTC(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate()));
  320. },
  321. getDate: function() {
  322. return this._utc_to_local(this.getUTCDate());
  323. },
  324. getUTCDate: function() {
  325. return this.date;
  326. },
  327. setDate: function(d) {
  328. this.setUTCDate(this._local_to_utc(d));
  329. },
  330. setUTCDate: function(d) {
  331. this.date = d;
  332. this.setValue();
  333. },
  334. setValue: function() {
  335. var formatted = this.getFormattedDate();
  336. if (!this.isInput) {
  337. if (this.component){
  338. this.element.find('input').val(formatted).change();
  339. }
  340. } else {
  341. this.element.val(formatted).change();
  342. }
  343. },
  344. getFormattedDate: function(format) {
  345. if (format === undefined)
  346. format = this.o.format;
  347. return DPGlobal.formatDate(this.date, format, this.o.language);
  348. },
  349. setStartDate: function(startDate){
  350. this._process_options({startDate: startDate});
  351. this.update();
  352. this.updateNavArrows();
  353. },
  354. setEndDate: function(endDate){
  355. this._process_options({endDate: endDate});
  356. this.update();
  357. this.updateNavArrows();
  358. },
  359. setDaysOfWeekDisabled: function(daysOfWeekDisabled){
  360. this._process_options({daysOfWeekDisabled: daysOfWeekDisabled});
  361. this.update();
  362. this.updateNavArrows();
  363. },
  364. place: function(){
  365. if(this.isInline) return;
  366. var calendarWidth = this.picker.outerWidth(),
  367. calendarHeight = this.picker.outerHeight(),
  368. visualPadding = 10,
  369. windowWidth = $window.width(),
  370. windowHeight = $window.height(),
  371. scrollTop = $window.scrollTop();
  372. var zIndex = parseInt(this.element.parents().filter(function() {
  373. return $(this).css('z-index') != 'auto';
  374. }).first().css('z-index'))+10;
  375. var offset = this.component ? this.component.parent().offset() : this.element.offset();
  376. var height = this.component ? this.component.outerHeight(true) : this.element.outerHeight(false);
  377. var width = this.component ? this.component.outerWidth(true) : this.element.outerWidth(false);
  378. var left = offset.left,
  379. top = offset.top;
  380. this.picker.removeClass(
  381. 'datepicker-orient-top datepicker-orient-bottom '+
  382. 'datepicker-orient-right datepicker-orient-left'
  383. );
  384. if (this.o.orientation.x !== 'auto') {
  385. this.picker.addClass('datepicker-orient-' + this.o.orientation.x);
  386. if (this.o.orientation.x === 'right')
  387. left -= calendarWidth - width;
  388. }
  389. // auto x orientation is best-placement: if it crosses a window
  390. // edge, fudge it sideways
  391. else {
  392. // Default to left
  393. this.picker.addClass('datepicker-orient-left');
  394. if (offset.left < 0)
  395. left -= offset.left - visualPadding;
  396. else if (offset.left + calendarWidth > windowWidth)
  397. left = windowWidth - calendarWidth - visualPadding;
  398. }
  399. // auto y orientation is best-situation: top or bottom, no fudging,
  400. // decision based on which shows more of the calendar
  401. var yorient = this.o.orientation.y,
  402. top_overflow, bottom_overflow;
  403. if (yorient === 'auto') {
  404. top_overflow = -scrollTop + offset.top - calendarHeight;
  405. bottom_overflow = scrollTop + windowHeight - (offset.top + height + calendarHeight);
  406. if (Math.max(top_overflow, bottom_overflow) === bottom_overflow)
  407. yorient = 'top';
  408. else
  409. yorient = 'bottom';
  410. }
  411. this.picker.addClass('datepicker-orient-' + yorient);
  412. if (yorient === 'top')
  413. top += height;
  414. else
  415. top -= calendarHeight + parseInt(this.picker.css('padding-top'));
  416. this.picker.css({
  417. top: top,
  418. left: left,
  419. zIndex: zIndex
  420. });
  421. },
  422. _allow_update: true,
  423. update: function(){
  424. if (!this._allow_update) return;
  425. var oldDate = new Date(this.date),
  426. date, fromArgs = false;
  427. if(arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
  428. date = arguments[0];
  429. if (date instanceof Date)
  430. date = this._local_to_utc(date);
  431. fromArgs = true;
  432. } else {
  433. date = this.isInput ? this.element.val() : this.element.data('date') || this.element.find('input').val();
  434. delete this.element.data().date;
  435. }
  436. this.date = DPGlobal.parseDate(date, this.o.format, this.o.language);
  437. if (fromArgs) {
  438. // setting date by clicking
  439. this.setValue();
  440. } else if (date) {
  441. // setting date by typing
  442. if (oldDate.getTime() !== this.date.getTime())
  443. this._trigger('changeDate');
  444. } else {
  445. // clearing date
  446. this._trigger('clearDate');
  447. }
  448. if (this.date < this.o.startDate) {
  449. this.viewDate = new Date(this.o.startDate);
  450. this.date = new Date(this.o.startDate);
  451. } else if (this.date > this.o.endDate) {
  452. this.viewDate = new Date(this.o.endDate);
  453. this.date = new Date(this.o.endDate);
  454. } else {
  455. this.viewDate = new Date(this.date);
  456. this.date = new Date(this.date);
  457. }
  458. this.fill();
  459. },
  460. fillDow: function(){
  461. var dowCnt = this.o.weekStart,
  462. html = '<tr>';
  463. if(this.o.calendarWeeks){
  464. var cell = '<th class="cw">&nbsp;</th>';
  465. html += cell;
  466. this.picker.find('.datepicker-days thead tr:first-child').prepend(cell);
  467. }
  468. while (dowCnt < this.o.weekStart + 7) {
  469. html += '<th class="dow">'+dates[this.o.language].daysMin[(dowCnt++)%7]+'</th>';
  470. }
  471. html += '</tr>';
  472. this.picker.find('.datepicker-days thead').append(html);
  473. },
  474. fillMonths: function(){
  475. var html = '',
  476. i = 0;
  477. while (i < 12) {
  478. html += '<span class="month">'+dates[this.o.language].monthsShort[i++]+'</span>';
  479. }
  480. this.picker.find('.datepicker-months td').html(html);
  481. },
  482. setRange: function(range){
  483. if (!range || !range.length)
  484. delete this.range;
  485. else
  486. this.range = $.map(range, function(d){ return d.valueOf(); });
  487. this.fill();
  488. },
  489. getClassNames: function(date){
  490. var cls = [],
  491. year = this.viewDate.getUTCFullYear(),
  492. month = this.viewDate.getUTCMonth(),
  493. currentDate = this.date.valueOf(),
  494. today = new Date();
  495. if (date.getUTCFullYear() < year || (date.getUTCFullYear() == year && date.getUTCMonth() < month)) {
  496. cls.push('old');
  497. } else if (date.getUTCFullYear() > year || (date.getUTCFullYear() == year && date.getUTCMonth() > month)) {
  498. cls.push('new');
  499. }
  500. // Compare internal UTC date with local today, not UTC today
  501. if (this.o.todayHighlight &&
  502. date.getUTCFullYear() == today.getFullYear() &&
  503. date.getUTCMonth() == today.getMonth() &&
  504. date.getUTCDate() == today.getDate()) {
  505. cls.push('today');
  506. }
  507. if (date.valueOf() == currentDate) {
  508. cls.push('active');
  509. }
  510. if (date.valueOf() < this.o.startDate || date.valueOf() > this.o.endDate ||
  511. $.inArray(date.getUTCDay(), this.o.daysOfWeekDisabled) !== -1) {
  512. cls.push('disabled');
  513. }
  514. if (this.range){
  515. if (date > this.range[0] && date < this.range[this.range.length-1]){
  516. cls.push('range');
  517. }
  518. if ($.inArray(date.valueOf(), this.range) != -1){
  519. cls.push('selected');
  520. }
  521. }
  522. return cls;
  523. },
  524. fill: function() {
  525. var d = new Date(this.viewDate),
  526. year = d.getUTCFullYear(),
  527. month = d.getUTCMonth(),
  528. startYear = this.o.startDate !== -Infinity ? this.o.startDate.getUTCFullYear() : -Infinity,
  529. startMonth = this.o.startDate !== -Infinity ? this.o.startDate.getUTCMonth() : -Infinity,
  530. endYear = this.o.endDate !== Infinity ? this.o.endDate.getUTCFullYear() : Infinity,
  531. endMonth = this.o.endDate !== Infinity ? this.o.endDate.getUTCMonth() : Infinity,
  532. currentDate = this.date && this.date.valueOf(),
  533. tooltip;
  534. this.picker.find('.datepicker-days thead th.datepicker-switch')
  535. .text(dates[this.o.language].months[month]+' '+year);
  536. this.picker.find('tfoot th.today')
  537. .text(dates[this.o.language].today)
  538. .toggle(this.o.todayBtn !== false);
  539. this.picker.find('tfoot th.clear')
  540. .text(dates[this.o.language].clear)
  541. .toggle(this.o.clearBtn !== false);
  542. this.updateNavArrows();
  543. this.fillMonths();
  544. var prevMonth = UTCDate(year, month-1, 28,0,0,0,0),
  545. day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  546. prevMonth.setUTCDate(day);
  547. prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.o.weekStart + 7)%7);
  548. var nextMonth = new Date(prevMonth);
  549. nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  550. nextMonth = nextMonth.valueOf();
  551. var html = [];
  552. var clsName;
  553. while(prevMonth.valueOf() < nextMonth) {
  554. if (prevMonth.getUTCDay() == this.o.weekStart) {
  555. html.push('<tr>');
  556. if(this.o.calendarWeeks){
  557. // ISO 8601: First week contains first thursday.
  558. // ISO also states week starts on Monday, but we can be more abstract here.
  559. var
  560. // Start of current week: based on weekstart/current date
  561. ws = new Date(+prevMonth + (this.o.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
  562. // Thursday of this week
  563. th = new Date(+ws + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
  564. // First Thursday of year, year from thursday
  565. yth = new Date(+(yth = UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
  566. // Calendar week: ms between thursdays, div ms per day, div 7 days
  567. calWeek = (th - yth) / 864e5 / 7 + 1;
  568. html.push('<td class="cw">'+ calWeek +'</td>');
  569. }
  570. }
  571. clsName = this.getClassNames(prevMonth);
  572. clsName.push('day');
  573. if (this.o.beforeShowDay !== $.noop){
  574. var before = this.o.beforeShowDay(this._utc_to_local(prevMonth));
  575. if (before === undefined)
  576. before = {};
  577. else if (typeof(before) === 'boolean')
  578. before = {enabled: before};
  579. else if (typeof(before) === 'string')
  580. before = {classes: before};
  581. if (before.enabled === false)
  582. clsName.push('disabled');
  583. if (before.classes)
  584. clsName = clsName.concat(before.classes.split(/\s+/));
  585. if (before.tooltip)
  586. tooltip = before.tooltip;
  587. }
  588. clsName = $.unique(clsName);
  589. html.push('<td class="'+clsName.join(' ')+'"' + (tooltip ? ' title="'+tooltip+'"' : '') + '>'+prevMonth.getUTCDate() + '</td>');
  590. if (prevMonth.getUTCDay() == this.o.weekEnd) {
  591. html.push('</tr>');
  592. }
  593. prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
  594. }
  595. this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
  596. var currentYear = this.date && this.date.getUTCFullYear();
  597. var months = this.picker.find('.datepicker-months')
  598. .find('th:eq(1)')
  599. .text(year)
  600. .end()
  601. .find('span').removeClass('active');
  602. if (currentYear && currentYear == year) {
  603. months.eq(this.date.getUTCMonth()).addClass('active');
  604. }
  605. if (year < startYear || year > endYear) {
  606. months.addClass('disabled');
  607. }
  608. if (year == startYear) {
  609. months.slice(0, startMonth).addClass('disabled');
  610. }
  611. if (year == endYear) {
  612. months.slice(endMonth+1).addClass('disabled');
  613. }
  614. html = '';
  615. year = parseInt(year/10, 10) * 10;
  616. var yearCont = this.picker.find('.datepicker-years')
  617. .find('th:eq(1)')
  618. .text(year + '-' + (year + 9))
  619. .end()
  620. .find('td');
  621. year -= 1;
  622. for (var i = -1; i < 11; i++) {
  623. html += '<span class="year'+(i == -1 ? ' old' : i == 10 ? ' new' : '')+(currentYear == year ? ' active' : '')+(year < startYear || year > endYear ? ' disabled' : '')+'">'+year+'</span>';
  624. year += 1;
  625. }
  626. yearCont.html(html);
  627. },
  628. updateNavArrows: function() {
  629. if (!this._allow_update) return;
  630. var d = new Date(this.viewDate),
  631. year = d.getUTCFullYear(),
  632. month = d.getUTCMonth();
  633. switch (this.viewMode) {
  634. case 0:
  635. if (this.o.startDate !== -Infinity && year <= this.o.startDate.getUTCFullYear() && month <= this.o.startDate.getUTCMonth()) {
  636. this.picker.find('.prev').css({visibility: 'hidden'});
  637. } else {
  638. this.picker.find('.prev').css({visibility: 'visible'});
  639. }
  640. if (this.o.endDate !== Infinity && year >= this.o.endDate.getUTCFullYear() && month >= this.o.endDate.getUTCMonth()) {
  641. this.picker.find('.next').css({visibility: 'hidden'});
  642. } else {
  643. this.picker.find('.next').css({visibility: 'visible'});
  644. }
  645. break;
  646. case 1:
  647. case 2:
  648. if (this.o.startDate !== -Infinity && year <= this.o.startDate.getUTCFullYear()) {
  649. this.picker.find('.prev').css({visibility: 'hidden'});
  650. } else {
  651. this.picker.find('.prev').css({visibility: 'visible'});
  652. }
  653. if (this.o.endDate !== Infinity && year >= this.o.endDate.getUTCFullYear()) {
  654. this.picker.find('.next').css({visibility: 'hidden'});
  655. } else {
  656. this.picker.find('.next').css({visibility: 'visible'});
  657. }
  658. break;
  659. }
  660. },
  661. click: function(e) {
  662. e.preventDefault();
  663. var target = $(e.target).closest('span, td, th');
  664. if (target.length == 1) {
  665. switch(target[0].nodeName.toLowerCase()) {
  666. case 'th':
  667. switch(target[0].className) {
  668. case 'datepicker-switch':
  669. this.showMode(1);
  670. break;
  671. case 'prev':
  672. case 'next':
  673. var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  674. switch(this.viewMode){
  675. case 0:
  676. this.viewDate = this.moveMonth(this.viewDate, dir);
  677. this._trigger('changeMonth', this.viewDate);
  678. break;
  679. case 1:
  680. case 2:
  681. this.viewDate = this.moveYear(this.viewDate, dir);
  682. if (this.viewMode === 1)
  683. this._trigger('changeYear', this.viewDate);
  684. break;
  685. }
  686. this.fill();
  687. break;
  688. case 'today':
  689. var date = new Date();
  690. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  691. this.showMode(-2);
  692. var which = this.o.todayBtn == 'linked' ? null : 'view';
  693. this._setDate(date, which);
  694. break;
  695. case 'clear':
  696. var element;
  697. if (this.isInput)
  698. element = this.element;
  699. else if (this.component)
  700. element = this.element.find('input');
  701. if (element)
  702. element.val("").change();
  703. this._trigger('changeDate');
  704. this.update();
  705. if (this.o.autoclose)
  706. this.hide();
  707. break;
  708. }
  709. break;
  710. case 'span':
  711. if (!target.is('.disabled')) {
  712. this.viewDate.setUTCDate(1);
  713. if (target.is('.month')) {
  714. var day = 1;
  715. var month = target.parent().find('span').index(target);
  716. var year = this.viewDate.getUTCFullYear();
  717. this.viewDate.setUTCMonth(month);
  718. this._trigger('changeMonth', this.viewDate);
  719. if (this.o.minViewMode === 1) {
  720. this._setDate(UTCDate(year, month, day,0,0,0,0));
  721. }
  722. } else {
  723. var year = parseInt(target.text(), 10)||0;
  724. var day = 1;
  725. var month = 0;
  726. this.viewDate.setUTCFullYear(year);
  727. this._trigger('changeYear', this.viewDate);
  728. if (this.o.minViewMode === 2) {
  729. this._setDate(UTCDate(year, month, day,0,0,0,0));
  730. }
  731. }
  732. this.showMode(-1);
  733. this.fill();
  734. }
  735. break;
  736. case 'td':
  737. if (target.is('.day') && !target.is('.disabled')){
  738. var day = parseInt(target.text(), 10)||1;
  739. var year = this.viewDate.getUTCFullYear(),
  740. month = this.viewDate.getUTCMonth();
  741. if (target.is('.old')) {
  742. if (month === 0) {
  743. month = 11;
  744. year -= 1;
  745. } else {
  746. month -= 1;
  747. }
  748. } else if (target.is('.new')) {
  749. if (month == 11) {
  750. month = 0;
  751. year += 1;
  752. } else {
  753. month += 1;
  754. }
  755. }
  756. this._setDate(UTCDate(year, month, day,0,0,0,0));
  757. }
  758. break;
  759. }
  760. }
  761. },
  762. _setDate: function(date, which){
  763. if (!which || which == 'date')
  764. this.date = new Date(date);
  765. if (!which || which == 'view')
  766. this.viewDate = new Date(date);
  767. this.fill();
  768. this.setValue();
  769. this._trigger('changeDate');
  770. var element;
  771. if (this.isInput) {
  772. element = this.element;
  773. } else if (this.component){
  774. element = this.element.find('input');
  775. }
  776. if (element) {
  777. element.change();
  778. }
  779. if (this.o.autoclose && (!which || which == 'date')) {
  780. this.hide();
  781. }
  782. },
  783. moveMonth: function(date, dir){
  784. if (!dir) return date;
  785. var new_date = new Date(date.valueOf()),
  786. day = new_date.getUTCDate(),
  787. month = new_date.getUTCMonth(),
  788. mag = Math.abs(dir),
  789. new_month, test;
  790. dir = dir > 0 ? 1 : -1;
  791. if (mag == 1){
  792. test = dir == -1
  793. // If going back one month, make sure month is not current month
  794. // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  795. ? function(){ return new_date.getUTCMonth() == month; }
  796. // If going forward one month, make sure month is as expected
  797. // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  798. : function(){ return new_date.getUTCMonth() != new_month; };
  799. new_month = month + dir;
  800. new_date.setUTCMonth(new_month);
  801. // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  802. if (new_month < 0 || new_month > 11)
  803. new_month = (new_month + 12) % 12;
  804. } else {
  805. // For magnitudes >1, move one month at a time...
  806. for (var i=0; i<mag; i++)
  807. // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  808. new_date = this.moveMonth(new_date, dir);
  809. // ...then reset the day, keeping it in the new month
  810. new_month = new_date.getUTCMonth();
  811. new_date.setUTCDate(day);
  812. test = function(){ return new_month != new_date.getUTCMonth(); };
  813. }
  814. // Common date-resetting loop -- if date is beyond end of month, make it
  815. // end of month
  816. while (test()){
  817. new_date.setUTCDate(--day);
  818. new_date.setUTCMonth(new_month);
  819. }
  820. return new_date;
  821. },
  822. moveYear: function(date, dir){
  823. return this.moveMonth(date, dir*12);
  824. },
  825. dateWithinRange: function(date){
  826. return date >= this.o.startDate && date <= this.o.endDate;
  827. },
  828. keydown: function(e){
  829. if (this.picker.is(':not(:visible)')){
  830. if (e.keyCode == 27) // allow escape to hide and re-show picker
  831. this.show();
  832. return;
  833. }
  834. var dateChanged = false,
  835. dir, day, month,
  836. newDate, newViewDate;
  837. switch(e.keyCode){
  838. case 27: // escape
  839. this.hide();
  840. e.preventDefault();
  841. break;
  842. case 37: // left
  843. case 39: // right
  844. if (!this.o.keyboardNavigation) break;
  845. dir = e.keyCode == 37 ? -1 : 1;
  846. if (e.ctrlKey){
  847. newDate = this.moveYear(this.date, dir);
  848. newViewDate = this.moveYear(this.viewDate, dir);
  849. this._trigger('changeYear', this.viewDate);
  850. } else if (e.shiftKey){
  851. newDate = this.moveMonth(this.date, dir);
  852. newViewDate = this.moveMonth(this.viewDate, dir);
  853. this._trigger('changeMonth', this.viewDate);
  854. } else {
  855. newDate = new Date(this.date);
  856. newDate.setUTCDate(this.date.getUTCDate() + dir);
  857. newViewDate = new Date(this.viewDate);
  858. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
  859. }
  860. if (this.dateWithinRange(newDate)){
  861. this.date = newDate;
  862. this.viewDate = newViewDate;
  863. this.setValue();
  864. this.update();
  865. e.preventDefault();
  866. dateChanged = true;
  867. }
  868. break;
  869. case 38: // up
  870. case 40: // down
  871. if (!this.o.keyboardNavigation) break;
  872. dir = e.keyCode == 38 ? -1 : 1;
  873. if (e.ctrlKey){
  874. newDate = this.moveYear(this.date, dir);
  875. newViewDate = this.moveYear(this.viewDate, dir);
  876. this._trigger('changeYear', this.viewDate);
  877. } else if (e.shiftKey){
  878. newDate = this.moveMonth(this.date, dir);
  879. newViewDate = this.moveMonth(this.viewDate, dir);
  880. this._trigger('changeMonth', this.viewDate);
  881. } else {
  882. newDate = new Date(this.date);
  883. newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
  884. newViewDate = new Date(this.viewDate);
  885. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
  886. }
  887. if (this.dateWithinRange(newDate)){
  888. this.date = newDate;
  889. this.viewDate = newViewDate;
  890. this.setValue();
  891. this.update();
  892. e.preventDefault();
  893. dateChanged = true;
  894. }
  895. break;
  896. case 13: // enter
  897. this.hide();
  898. e.preventDefault();
  899. break;
  900. case 9: // tab
  901. this.hide();
  902. break;
  903. }
  904. if (dateChanged){
  905. this._trigger('changeDate');
  906. var element;
  907. if (this.isInput) {
  908. element = this.element;
  909. } else if (this.component){
  910. element = this.element.find('input');
  911. }
  912. if (element) {
  913. element.change();
  914. }
  915. }
  916. },
  917. showMode: function(dir) {
  918. if (dir) {
  919. this.viewMode = Math.max(this.o.minViewMode, Math.min(2, this.viewMode + dir));
  920. }
  921. /*
  922. vitalets: fixing bug of very special conditions:
  923. jquery 1.7.1 + webkit + show inline datepicker in bootstrap popover.
  924. Method show() does not set display css correctly and datepicker is not shown.
  925. Changed to .css('display', 'block') solve the problem.
  926. See https://github.com/vitalets/x-editable/issues/37
  927. In jquery 1.7.2+ everything works fine.
  928. */
  929. //this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  930. this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
  931. this.updateNavArrows();
  932. }
  933. };
  934. var DateRangePicker = function(element, options){
  935. this.element = $(element);
  936. this.inputs = $.map(options.inputs, function(i){ return i.jquery ? i[0] : i; });
  937. delete options.inputs;
  938. $(this.inputs)
  939. .datepicker(options)
  940. .bind('changeDate', $.proxy(this.dateUpdated, this));
  941. this.pickers = $.map(this.inputs, function(i){ return $(i).data('datepicker'); });
  942. this.updateDates();
  943. };
  944. DateRangePicker.prototype = {
  945. updateDates: function(){
  946. this.dates = $.map(this.pickers, function(i){ return i.date; });
  947. this.updateRanges();
  948. },
  949. updateRanges: function(){
  950. var range = $.map(this.dates, function(d){ return d.valueOf(); });
  951. $.each(this.pickers, function(i, p){
  952. p.setRange(range);
  953. });
  954. },
  955. dateUpdated: function(e){
  956. var dp = $(e.target).data('datepicker'),
  957. new_date = dp.getUTCDate(),
  958. i = $.inArray(e.target, this.inputs),
  959. l = this.inputs.length;
  960. if (i == -1) return;
  961. if (new_date < this.dates[i]){
  962. // Date being moved earlier/left
  963. while (i>=0 && new_date < this.dates[i]){
  964. this.pickers[i--].setUTCDate(new_date);
  965. }
  966. }
  967. else if (new_date > this.dates[i]){
  968. // Date being moved later/right
  969. while (i<l && new_date > this.dates[i]){
  970. this.pickers[i++].setUTCDate(new_date);
  971. }
  972. }
  973. this.updateDates();
  974. },
  975. remove: function(){
  976. $.map(this.pickers, function(p){ p.remove(); });
  977. delete this.element.data().datepicker;
  978. }
  979. };
  980. function opts_from_el(el, prefix){
  981. // Derive options from element data-attrs
  982. var data = $(el).data(),
  983. out = {}, inkey,
  984. replace = new RegExp('^' + prefix.toLowerCase() + '([A-Z])'),
  985. prefix = new RegExp('^' + prefix.toLowerCase());
  986. for (var key in data)
  987. if (prefix.test(key)){
  988. inkey = key.replace(replace, function(_,a){ return a.toLowerCase(); });
  989. out[inkey] = data[key];
  990. }
  991. return out;
  992. }
  993. function opts_from_locale(lang){
  994. // Derive options from locale plugins
  995. var out = {};
  996. // Check if "de-DE" style date is available, if not language should
  997. // fallback to 2 letter code eg "de"
  998. if (!dates[lang]) {
  999. lang = lang.split('-')[0]
  1000. if (!dates[lang])
  1001. return;
  1002. }
  1003. var d = dates[lang];
  1004. $.each(locale_opts, function(i,k){
  1005. if (k in d)
  1006. out[k] = d[k];
  1007. });
  1008. return out;
  1009. }
  1010. var old = $.fn.datepicker;
  1011. $.fn.datepicker = function ( option ) {
  1012. var args = Array.apply(null, arguments);
  1013. args.shift();
  1014. var internal_return,
  1015. this_return;
  1016. this.each(function () {
  1017. var $this = $(this),
  1018. data = $this.data('datepicker'),
  1019. options = typeof option == 'object' && option;
  1020. if (!data) {
  1021. var elopts = opts_from_el(this, 'date'),
  1022. // Preliminary otions
  1023. xopts = $.extend({}, defaults, elopts, options),
  1024. locopts = opts_from_locale(xopts.language),
  1025. // Options priority: js args, data-attrs, locales, defaults
  1026. opts = $.extend({}, defaults, locopts, elopts, options);
  1027. if ($this.is('.input-daterange') || opts.inputs){
  1028. var ropts = {
  1029. inputs: opts.inputs || $this.find('input').toArray()
  1030. };
  1031. $this.data('datepicker', (data = new DateRangePicker(this, $.extend(opts, ropts))));
  1032. }
  1033. else{
  1034. $this.data('datepicker', (data = new Datepicker(this, opts)));
  1035. }
  1036. }
  1037. if (typeof option == 'string' && typeof data[option] == 'function') {
  1038. internal_return = data[option].apply(data, args);
  1039. if (internal_return !== undefined)
  1040. return false;
  1041. }
  1042. });
  1043. if (internal_return !== undefined)
  1044. return internal_return;
  1045. else
  1046. return this;
  1047. };
  1048. var defaults = $.fn.datepicker.defaults = {
  1049. autoclose: false,
  1050. beforeShowDay: $.noop,
  1051. calendarWeeks: false,
  1052. clearBtn: false,
  1053. daysOfWeekDisabled: [],
  1054. endDate: Infinity,
  1055. forceParse: true,
  1056. format: 'mm/dd/yyyy',
  1057. keyboardNavigation: true,
  1058. language: 'en',
  1059. minViewMode: 0,
  1060. orientation: "auto",
  1061. rtl: false,
  1062. startDate: -Infinity,
  1063. startView: 0,
  1064. todayBtn: false,
  1065. todayHighlight: false,
  1066. weekStart: 0
  1067. };
  1068. var locale_opts = $.fn.datepicker.locale_opts = [
  1069. 'format',
  1070. 'rtl',
  1071. 'weekStart'
  1072. ];
  1073. $.fn.datepicker.Constructor = Datepicker;
  1074. var dates = $.fn.datepicker.dates = {
  1075. en: {
  1076. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  1077. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  1078. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  1079. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  1080. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  1081. today: "Today",
  1082. clear: "Clear"
  1083. }
  1084. };
  1085. var DPGlobal = {
  1086. modes: [
  1087. {
  1088. clsName: 'days',
  1089. navFnc: 'Month',
  1090. navStep: 1
  1091. },
  1092. {
  1093. clsName: 'months',
  1094. navFnc: 'FullYear',
  1095. navStep: 1
  1096. },
  1097. {
  1098. clsName: 'years',
  1099. navFnc: 'FullYear',
  1100. navStep: 10
  1101. }],
  1102. isLeapYear: function (year) {
  1103. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
  1104. },
  1105. getDaysInMonth: function (year, month) {
  1106. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
  1107. },
  1108. validParts: /dd?|DD?|mm?|MM?|yy(?:yy)?/g,
  1109. nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
  1110. parseFormat: function(format){
  1111. // IE treats \0 as a string end in inputs (truncating the value),
  1112. // so it's a bad format delimiter, anyway
  1113. var separators = format.replace(this.validParts, '\0').split('\0'),
  1114. parts = format.match(this.validParts);
  1115. if (!separators || !separators.length || !parts || parts.length === 0){
  1116. throw new Error("Invalid date format.");
  1117. }
  1118. return {separators: separators, parts: parts};
  1119. },
  1120. parseDate: function(date, format, language) {
  1121. if (date instanceof Date) return date;
  1122. if (typeof format === 'string')
  1123. format = DPGlobal.parseFormat(format);
  1124. if (/^[\-+]\d+[dmwy]([\s,]+[\-+]\d+[dmwy])*$/.test(date)) {
  1125. var part_re = /([\-+]\d+)([dmwy])/,
  1126. parts = date.match(/([\-+]\d+)([dmwy])/g),
  1127. part, dir;
  1128. date = new Date();
  1129. for (var i=0; i<parts.length; i++) {
  1130. part = part_re.exec(parts[i]);
  1131. dir = parseInt(part[1]);
  1132. switch(part[2]){
  1133. case 'd':
  1134. date.setUTCDate(date.getUTCDate() + dir);
  1135. break;
  1136. case 'm':
  1137. date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
  1138. break;
  1139. case 'w':
  1140. date.setUTCDate(date.getUTCDate() + dir * 7);
  1141. break;
  1142. case 'y':
  1143. date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
  1144. break;
  1145. }
  1146. }
  1147. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
  1148. }
  1149. var parts = date && date.match(this.nonpunctuation) || [],
  1150. date = new Date(),
  1151. parsed = {},
  1152. setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
  1153. setters_map = {
  1154. yyyy: function(d,v){ return d.setUTCFullYear(v); },
  1155. yy: function(d,v){ return d.setUTCFullYear(2000+v); },
  1156. m: function(d,v){
  1157. if (isNaN(d))
  1158. return d;
  1159. v -= 1;
  1160. while (v<0) v += 12;
  1161. v %= 12;
  1162. d.setUTCMonth(v);
  1163. while (d.getUTCMonth() != v)
  1164. d.setUTCDate(d.getUTCDate()-1);
  1165. return d;
  1166. },
  1167. d: function(d,v){ return d.setUTCDate(v); }
  1168. },
  1169. val, filtered, part;
  1170. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1171. setters_map['dd'] = setters_map['d'];
  1172. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  1173. var fparts = format.parts.slice();
  1174. // Remove noop parts
  1175. if (parts.length != fparts.length) {
  1176. fparts = $(fparts).filter(function(i,p){
  1177. return $.inArray(p, setters_order) !== -1;
  1178. }).toArray();
  1179. }
  1180. // Process remainder
  1181. if (parts.length == fparts.length) {
  1182. for (var i=0, cnt = fparts.length; i < cnt; i++) {
  1183. val = parseInt(parts[i], 10);
  1184. part = fparts[i];
  1185. if (isNaN(val)) {
  1186. switch(part) {
  1187. case 'MM':
  1188. filtered = $(dates[language].months).filter(function(){
  1189. var m = this.slice(0, parts[i].length),
  1190. p = parts[i].slice(0, m.length);
  1191. return m == p;
  1192. });
  1193. val = $.inArray(filtered[0], dates[language].months) + 1;
  1194. break;
  1195. case 'M':
  1196. filtered = $(dates[language].monthsShort).filter(function(){
  1197. var m = this.slice(0, parts[i].length),
  1198. p = parts[i].slice(0, m.length);
  1199. return m == p;
  1200. });
  1201. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1202. break;
  1203. }
  1204. }
  1205. parsed[part] = val;
  1206. }
  1207. for (var i=0, _date, s; i<setters_order.length; i++){
  1208. s = setters_order[i];
  1209. if (s in parsed && !isNaN(parsed[s])){
  1210. _date = new Date(date);
  1211. setters_map[s](_date, parsed[s]);
  1212. if (!isNaN(_date))
  1213. date = _date;
  1214. }
  1215. }
  1216. }
  1217. return date;
  1218. },
  1219. formatDate: function(date, format, language){
  1220. if (typeof format === 'string')
  1221. format = DPGlobal.parseFormat(format);
  1222. var val = {
  1223. d: date.getUTCDate(),
  1224. D: dates[language].daysShort[date.getUTCDay()],
  1225. DD: dates[language].days[date.getUTCDay()],
  1226. m: date.getUTCMonth() + 1,
  1227. M: dates[language].monthsShort[date.getUTCMonth()],
  1228. MM: dates[language].months[date.getUTCMonth()],
  1229. yy: date.getUTCFullYear().toString().substring(2),
  1230. yyyy: date.getUTCFullYear()
  1231. };
  1232. val.dd = (val.d < 10 ? '0' : '') + val.d;
  1233. val.mm = (val.m < 10 ? '0' : '') + val.m;
  1234. var date = [],
  1235. seps = $.extend([], format.separators);
  1236. for (var i=0, cnt = format.parts.length; i <= cnt; i++) {
  1237. if (seps.length)
  1238. date.push(seps.shift());
  1239. date.push(val[format.parts[i]]);
  1240. }
  1241. return date.join('');
  1242. },
  1243. headTemplate: '<thead>'+
  1244. '<tr>'+
  1245. '<th class="prev">&laquo;</th>'+
  1246. '<th colspan="5" class="datepicker-switch"></th>'+
  1247. '<th class="next">&raquo;</th>'+
  1248. '</tr>'+
  1249. '</thead>',
  1250. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1251. footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr><tr><th colspan="7" class="clear"></th></tr></tfoot>'
  1252. };
  1253. DPGlobal.template = '<div class="datepicker">'+
  1254. '<div class="datepicker-days">'+
  1255. '<table class=" table-condensed">'+
  1256. DPGlobal.headTemplate+
  1257. '<tbody></tbody>'+
  1258. DPGlobal.footTemplate+
  1259. '</table>'+
  1260. '</div>'+
  1261. '<div class="datepicker-months">'+
  1262. '<table class="table-condensed">'+
  1263. DPGlobal.headTemplate+
  1264. DPGlobal.contTemplate+
  1265. DPGlobal.footTemplate+
  1266. '</table>'+
  1267. '</div>'+
  1268. '<div class="datepicker-years">'+
  1269. '<table class="table-condensed">'+
  1270. DPGlobal.headTemplate+
  1271. DPGlobal.contTemplate+
  1272. DPGlobal.footTemplate+
  1273. '</table>'+
  1274. '</div>'+
  1275. '</div>';
  1276. $.fn.datepicker.DPGlobal = DPGlobal;
  1277. /* DATEPICKER NO CONFLICT
  1278. * =================== */
  1279. $.fn.datepicker.noConflict = function(){
  1280. $.fn.datepicker = old;
  1281. return this;
  1282. };
  1283. /* DATEPICKER DATA-API
  1284. * ================== */
  1285. $(document).on(
  1286. 'focus.datepicker.data-api click.datepicker.data-api',
  1287. '[data-provide="datepicker"]',
  1288. function(e){
  1289. var $this = $(this);
  1290. if ($this.data('datepicker')) return;
  1291. e.preventDefault();
  1292. // component click requires us to explicitly show it
  1293. $this.datepicker('show');
  1294. }
  1295. );
  1296. $(function(){
  1297. $('[data-provide="datepicker-inline"]').datepicker();
  1298. });
  1299. }( window.jQuery ));