bootstrap-datepicker.js 57 KB

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