bootstrap-datepicker.js 52 KB

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