bootstrap-datepicker.js 58 KB

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