bootstrap-datepicker.js 49 KB

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