bootstrap-datepicker.js 53 KB

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