bootstrap-datepicker.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  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: 'es',
  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. es: {
  1420. days:["Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo"],
  1421. daysShort:["Dom","Lun","Mar","Mié","Jue","Vie","Sáb","Dom"],
  1422. daysMin:["Do","Lu","Ma","Mi","Ju","Vi","Sa","Do"],
  1423. months:["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"],
  1424. monthsShort:["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"],
  1425. today:"Hoy",
  1426. clear:"Borrar",
  1427. weekStart:1,
  1428. format:"dd/mm/yyyy"
  1429. }
  1430. };
  1431. var DPGlobal = {
  1432. modes: [
  1433. {
  1434. clsName: 'days',
  1435. navFnc: 'Month',
  1436. navStep: 1
  1437. },
  1438. {
  1439. clsName: 'months',
  1440. navFnc: 'FullYear',
  1441. navStep: 1
  1442. },
  1443. {
  1444. clsName: 'years',
  1445. navFnc: 'FullYear',
  1446. navStep: 10
  1447. }],
  1448. isLeapYear: function(year){
  1449. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
  1450. },
  1451. getDaysInMonth: function(year, month){
  1452. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
  1453. },
  1454. validParts: /dd?|DD?|mm?|MM?|yy(?:yy)?/g,
  1455. nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
  1456. parseFormat: function(format){
  1457. // IE treats \0 as a string end in inputs (truncating the value),
  1458. // so it's a bad format delimiter, anyway
  1459. var separators = format.replace(this.validParts, '\0').split('\0'),
  1460. parts = format.match(this.validParts);
  1461. if (!separators || !separators.length || !parts || parts.length === 0){
  1462. throw new Error("Invalid date format.");
  1463. }
  1464. return {separators: separators, parts: parts};
  1465. },
  1466. parseDate: function(date, format, language){
  1467. if (!date)
  1468. return undefined;
  1469. if (date instanceof Date)
  1470. return date;
  1471. if (typeof format === 'string')
  1472. format = DPGlobal.parseFormat(format);
  1473. var part_re = /([\-+]\d+)([dmwy])/,
  1474. parts = date.match(/([\-+]\d+)([dmwy])/g),
  1475. part, dir, i;
  1476. if (/^[\-+]\d+[dmwy]([\s,]+[\-+]\d+[dmwy])*$/.test(date)){
  1477. date = new Date();
  1478. for (i=0; i < parts.length; i++){
  1479. part = part_re.exec(parts[i]);
  1480. dir = parseInt(part[1]);
  1481. switch (part[2]){
  1482. case 'd':
  1483. date.setUTCDate(date.getUTCDate() + dir);
  1484. break;
  1485. case 'm':
  1486. date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
  1487. break;
  1488. case 'w':
  1489. date.setUTCDate(date.getUTCDate() + dir * 7);
  1490. break;
  1491. case 'y':
  1492. date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
  1493. break;
  1494. }
  1495. }
  1496. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
  1497. }
  1498. parts = date && date.match(this.nonpunctuation) || [];
  1499. date = new Date();
  1500. var parsed = {},
  1501. setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
  1502. setters_map = {
  1503. yyyy: function(d,v){
  1504. return d.setUTCFullYear(v);
  1505. },
  1506. yy: function(d,v){
  1507. return d.setUTCFullYear(2000+v);
  1508. },
  1509. m: function(d,v){
  1510. if (isNaN(d))
  1511. return d;
  1512. v -= 1;
  1513. while (v < 0) v += 12;
  1514. v %= 12;
  1515. d.setUTCMonth(v);
  1516. while (d.getUTCMonth() !== v)
  1517. d.setUTCDate(d.getUTCDate()-1);
  1518. return d;
  1519. },
  1520. d: function(d,v){
  1521. return d.setUTCDate(v);
  1522. }
  1523. },
  1524. val, filtered;
  1525. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1526. setters_map['dd'] = setters_map['d'];
  1527. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  1528. var fparts = format.parts.slice();
  1529. // Remove noop parts
  1530. if (parts.length !== fparts.length){
  1531. fparts = $(fparts).filter(function(i,p){
  1532. return $.inArray(p, setters_order) !== -1;
  1533. }).toArray();
  1534. }
  1535. // Process remainder
  1536. function match_part(){
  1537. var m = this.slice(0, parts[i].length),
  1538. p = parts[i].slice(0, m.length);
  1539. return m.toLowerCase() === p.toLowerCase();
  1540. }
  1541. if (parts.length === fparts.length){
  1542. var cnt;
  1543. for (i=0, cnt = fparts.length; i < cnt; i++){
  1544. val = parseInt(parts[i], 10);
  1545. part = fparts[i];
  1546. if (isNaN(val)){
  1547. switch (part){
  1548. case 'MM':
  1549. filtered = $(dates[language].months).filter(match_part);
  1550. val = $.inArray(filtered[0], dates[language].months) + 1;
  1551. break;
  1552. case 'M':
  1553. filtered = $(dates[language].monthsShort).filter(match_part);
  1554. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1555. break;
  1556. }
  1557. }
  1558. parsed[part] = val;
  1559. }
  1560. var _date, s;
  1561. for (i=0; i < setters_order.length; i++){
  1562. s = setters_order[i];
  1563. if (s in parsed && !isNaN(parsed[s])){
  1564. _date = new Date(date);
  1565. setters_map[s](_date, parsed[s]);
  1566. if (!isNaN(_date))
  1567. date = _date;
  1568. }
  1569. }
  1570. }
  1571. return date;
  1572. },
  1573. formatDate: function(date, format, language){
  1574. if (!date)
  1575. return '';
  1576. if (typeof format === 'string')
  1577. format = DPGlobal.parseFormat(format);
  1578. var val = {
  1579. d: date.getUTCDate(),
  1580. D: dates[language].daysShort[date.getUTCDay()],
  1581. DD: dates[language].days[date.getUTCDay()],
  1582. m: date.getUTCMonth() + 1,
  1583. M: dates[language].monthsShort[date.getUTCMonth()],
  1584. MM: dates[language].months[date.getUTCMonth()],
  1585. yy: date.getUTCFullYear().toString().substring(2),
  1586. yyyy: date.getUTCFullYear()
  1587. };
  1588. val.dd = (val.d < 10 ? '0' : '') + val.d;
  1589. val.mm = (val.m < 10 ? '0' : '') + val.m;
  1590. date = [];
  1591. var seps = $.extend([], format.separators);
  1592. for (var i=0, cnt = format.parts.length; i <= cnt; i++){
  1593. if (seps.length)
  1594. date.push(seps.shift());
  1595. date.push(val[format.parts[i]]);
  1596. }
  1597. return date.join('');
  1598. },
  1599. headTemplate: '<thead>'+
  1600. '<tr>'+
  1601. '<th class="prev"><i class="fa fa-angle-left"></th>'+
  1602. '<th colspan="5" class="datepicker-switch"></th>'+
  1603. '<th class="next"><i class="fa fa-angle-right"></th>'+
  1604. '</tr>'+
  1605. '</thead>',
  1606. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1607. footTemplate: '<tfoot>'+
  1608. '<tr>'+
  1609. '<th colspan="7" class="today"></th>'+
  1610. '</tr>'+
  1611. '<tr>'+
  1612. '<th colspan="7" class="clear"></th>'+
  1613. '</tr>'+
  1614. '</tfoot>'
  1615. };
  1616. DPGlobal.template = '<div class="datepicker">'+
  1617. '<div class="datepicker-days">'+
  1618. '<table class=" table-condensed">'+
  1619. DPGlobal.headTemplate+
  1620. '<tbody></tbody>'+
  1621. DPGlobal.footTemplate+
  1622. '</table>'+
  1623. '</div>'+
  1624. '<div class="datepicker-months">'+
  1625. '<table class="table-condensed">'+
  1626. DPGlobal.headTemplate+
  1627. DPGlobal.contTemplate+
  1628. DPGlobal.footTemplate+
  1629. '</table>'+
  1630. '</div>'+
  1631. '<div class="datepicker-years">'+
  1632. '<table class="table-condensed">'+
  1633. DPGlobal.headTemplate+
  1634. DPGlobal.contTemplate+
  1635. DPGlobal.footTemplate+
  1636. '</table>'+
  1637. '</div>'+
  1638. '</div>';
  1639. $.fn.datepicker.DPGlobal = DPGlobal;
  1640. /* DATEPICKER NO CONFLICT
  1641. * =================== */
  1642. $.fn.datepicker.noConflict = function(){
  1643. $.fn.datepicker = old;
  1644. return this;
  1645. };
  1646. /* DATEPICKER VERSION
  1647. * =================== */
  1648. $.fn.datepicker.version = "1.4.0";
  1649. /* DATEPICKER DATA-API
  1650. * ================== */
  1651. $(document).on(
  1652. 'focus.datepicker.data-api click.datepicker.data-api',
  1653. '[data-provide="datepicker"]',
  1654. function(e){
  1655. var $this = $(this);
  1656. if ($this.data('datepicker'))
  1657. return;
  1658. e.preventDefault();
  1659. // component click requires us to explicitly show it
  1660. datepickerPlugin.call($this, 'show');
  1661. }
  1662. );
  1663. $(function(){
  1664. datepickerPlugin.call($('[data-provide="datepicker-inline"]'));
  1665. });
  1666. }(window.jQuery));