bootstrap-datepicker.js 63 KB

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