bootstrap-datepicker.js 46 KB

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