bootstrap-datetimepicker.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /* =========================================================
  2. * bootstrap-datetimepicker.js
  3. * =========================================================
  4. * Copyright 2012 Stefan Petre
  5. * Improvements by Andrew Rowls
  6. * Improvements by Sébastien Malot
  7. * Improvements by Yun Lai
  8. * Project URL : http://www.malot.fr/bootstrap-datetimepicker
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ========================================================= */
  22. /*
  23. * Improvement by CuGBabyBeaR @ 2013-09-12
  24. *
  25. * Make it work in bootstrap v3
  26. */
  27. !function ($) {
  28. function UTCDate() {
  29. return new Date(Date.UTC.apply(Date, arguments));
  30. }
  31. function UTCToday() {
  32. var today = new Date();
  33. return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), today.getUTCHours(), today.getUTCMinutes(), today.getUTCSeconds(), 0);
  34. }
  35. // Picker object
  36. var Datetimepicker = function (element, options) {
  37. var that = this;
  38. this.element = $(element);
  39. this.language = options.language || this.element.data('date-language') || "en";
  40. this.language = this.language in dates ? this.language : "en";
  41. this.isRTL = dates[this.language].rtl || false;
  42. this.formatType = options.formatType || this.element.data('format-type') || 'standard';
  43. this.format = DPGlobal.parseFormat(options.format || this.element.data('date-format') || dates[this.language].format || DPGlobal.getDefaultFormat(this.formatType, 'input'), this.formatType);
  44. this.isInline = false;
  45. this.isVisible = false;
  46. this.isInput = this.element.is('input');
  47. this.bootcssVer = this.isInput ? (this.element.is('.form-control') ? 3 : 2) : ( this.bootcssVer = this.element.is('.input-group') ? 3 : 2 );
  48. this.component = this.element.is('.date') ? ( this.bootcssVer == 3 ? this.element.find('.input-group-addon .glyphicon-th, .input-group-addon .glyphicon-time, .input-group-addon .glyphicon-calendar').parent() : this.element.find('.add-on .icon-th, .add-on .icon-time, .add-on .icon-calendar').parent()) : false;
  49. this.componentReset = this.element.is('.date') ? ( this.bootcssVer == 3 ? this.element.find('.input-group-addon .glyphicon-remove').parent() : this.element.find('.add-on .icon-remove').parent()) : false;
  50. this.hasInput = this.component && this.element.find('input').length;
  51. if (this.component && this.component.length === 0) {
  52. this.component = false;
  53. }
  54. this.linkField = options.linkField || this.element.data('link-field') || false;
  55. this.linkFormat = DPGlobal.parseFormat(options.linkFormat || this.element.data('link-format') || DPGlobal.getDefaultFormat(this.formatType, 'link'), this.formatType);
  56. this.minuteStep = options.minuteStep || this.element.data('minute-step') || 5;
  57. this.pickerPosition = options.pickerPosition || this.element.data('picker-position') || 'bottom-right';
  58. this.showMeridian = options.showMeridian || this.element.data('show-meridian') || false;
  59. this.initialDate = options.initialDate || new Date();
  60. this._attachEvents();
  61. this.formatViewType = "datetime";
  62. if ('formatViewType' in options) {
  63. this.formatViewType = options.formatViewType;
  64. } else if ('formatViewType' in this.element.data()) {
  65. this.formatViewType = this.element.data('formatViewType');
  66. }
  67. this.minView = 0;
  68. if ('minView' in options) {
  69. this.minView = options.minView;
  70. } else if ('minView' in this.element.data()) {
  71. this.minView = this.element.data('min-view');
  72. }
  73. this.minView = DPGlobal.convertViewMode(this.minView);
  74. this.maxView = DPGlobal.modes.length - 1;
  75. if ('maxView' in options) {
  76. this.maxView = options.maxView;
  77. } else if ('maxView' in this.element.data()) {
  78. this.maxView = this.element.data('max-view');
  79. }
  80. this.maxView = DPGlobal.convertViewMode(this.maxView);
  81. this.wheelViewModeNavigation = false;
  82. if ('wheelViewModeNavigation' in options) {
  83. this.wheelViewModeNavigation = options.wheelViewModeNavigation;
  84. } else if ('wheelViewModeNavigation' in this.element.data()) {
  85. this.wheelViewModeNavigation = this.element.data('view-mode-wheel-navigation');
  86. }
  87. this.wheelViewModeNavigationInverseDirection = false;
  88. if ('wheelViewModeNavigationInverseDirection' in options) {
  89. this.wheelViewModeNavigationInverseDirection = options.wheelViewModeNavigationInverseDirection;
  90. } else if ('wheelViewModeNavigationInverseDirection' in this.element.data()) {
  91. this.wheelViewModeNavigationInverseDirection = this.element.data('view-mode-wheel-navigation-inverse-dir');
  92. }
  93. this.wheelViewModeNavigationDelay = 100;
  94. if ('wheelViewModeNavigationDelay' in options) {
  95. this.wheelViewModeNavigationDelay = options.wheelViewModeNavigationDelay;
  96. } else if ('wheelViewModeNavigationDelay' in this.element.data()) {
  97. this.wheelViewModeNavigationDelay = this.element.data('view-mode-wheel-navigation-delay');
  98. }
  99. this.startViewMode = 2;
  100. if ('startView' in options) {
  101. this.startViewMode = options.startView;
  102. } else if ('startView' in this.element.data()) {
  103. this.startViewMode = this.element.data('start-view');
  104. }
  105. this.startViewMode = DPGlobal.convertViewMode(this.startViewMode);
  106. this.viewMode = this.startViewMode;
  107. this.viewSelect = this.minView;
  108. if ('viewSelect' in options) {
  109. this.viewSelect = options.viewSelect;
  110. } else if ('viewSelect' in this.element.data()) {
  111. this.viewSelect = this.element.data('view-select');
  112. }
  113. this.viewSelect = DPGlobal.convertViewMode(this.viewSelect);
  114. this.forceParse = true;
  115. if ('forceParse' in options) {
  116. this.forceParse = options.forceParse;
  117. } else if ('dateForceParse' in this.element.data()) {
  118. this.forceParse = this.element.data('date-force-parse');
  119. }
  120. this.picker = $((this.bootcssVer == 3) ? DPGlobal.templateV3 : DPGlobal.template)
  121. .appendTo(this.isInline ? this.element : 'body')
  122. .on({
  123. click: $.proxy(this.click, this),
  124. mousedown: $.proxy(this.mousedown, this)
  125. });
  126. if (this.wheelViewModeNavigation) {
  127. if ($.fn.mousewheel) {
  128. this.picker.on({mousewheel: $.proxy(this.mousewheel, this)});
  129. } else {
  130. console.log("Mouse Wheel event is not supported. Please include the jQuery Mouse Wheel plugin before enabling this option");
  131. }
  132. }
  133. if (this.isInline) {
  134. this.picker.addClass('datetimepicker-inline');
  135. } else {
  136. this.picker.addClass('datetimepicker-dropdown-' + this.pickerPosition + ' dropdown-menu');
  137. }
  138. if (this.isRTL) {
  139. this.picker.addClass('datetimepicker-rtl');
  140. if (this.bootcssVer == 3) {
  141. this.picker.find('.prev span, .next span')
  142. .toggleClass('glyphicon-arrow-left glyphicon-arrow-right');
  143. } else {
  144. this.picker.find('.prev i, .next i')
  145. .toggleClass('icon-arrow-left icon-arrow-right');
  146. }
  147. ;
  148. }
  149. $(document).on('mousedown', function (e) {
  150. // Clicked outside the datetimepicker, hide it
  151. if ($(e.target).closest('.datetimepicker').length === 0) {
  152. that.hide();
  153. }
  154. });
  155. this.autoclose = false;
  156. if ('autoclose' in options) {
  157. this.autoclose = options.autoclose;
  158. } else if ('dateAutoclose' in this.element.data()) {
  159. this.autoclose = this.element.data('date-autoclose');
  160. }
  161. this.keyboardNavigation = true;
  162. if ('keyboardNavigation' in options) {
  163. this.keyboardNavigation = options.keyboardNavigation;
  164. } else if ('dateKeyboardNavigation' in this.element.data()) {
  165. this.keyboardNavigation = this.element.data('date-keyboard-navigation');
  166. }
  167. this.todayBtn = (options.todayBtn || this.element.data('date-today-btn') || false);
  168. this.todayHighlight = (options.todayHighlight || this.element.data('date-today-highlight') || false);
  169. this.weekStart = ((options.weekStart || this.element.data('date-weekstart') || dates[this.language].weekStart || 0) % 7);
  170. this.weekEnd = ((this.weekStart + 6) % 7);
  171. this.startDate = -Infinity;
  172. this.endDate = Infinity;
  173. this.daysOfWeekDisabled = [];
  174. this.setStartDate(options.startDate || this.element.data('date-startdate'));
  175. this.setEndDate(options.endDate || this.element.data('date-enddate'));
  176. this.setDaysOfWeekDisabled(options.daysOfWeekDisabled || this.element.data('date-days-of-week-disabled'));
  177. this.fillDow();
  178. this.fillMonths();
  179. this.update();
  180. this.showMode();
  181. if (this.isInline) {
  182. this.show();
  183. }
  184. };
  185. Datetimepicker.prototype = {
  186. constructor: Datetimepicker,
  187. _events: [],
  188. _attachEvents: function () {
  189. this._detachEvents();
  190. if (this.isInput) { // single input
  191. this._events = [
  192. [this.element, {
  193. focus: $.proxy(this.show, this),
  194. keyup: $.proxy(this.update, this),
  195. keydown: $.proxy(this.keydown, this)
  196. }]
  197. ];
  198. }
  199. else if (this.component && this.hasInput) { // component: input + button
  200. this._events = [
  201. // For components that are not readonly, allow keyboard nav
  202. [this.element.find('input'), {
  203. focus: $.proxy(this.show, this),
  204. keyup: $.proxy(this.update, this),
  205. keydown: $.proxy(this.keydown, this)
  206. }],
  207. [this.component, {
  208. click: $.proxy(this.show, this)
  209. }]
  210. ];
  211. if (this.componentReset) {
  212. this._events.push([
  213. this.componentReset,
  214. {click: $.proxy(this.reset, this)}
  215. ]);
  216. }
  217. }
  218. else if (this.element.is('div')) { // inline datetimepicker
  219. this.isInline = true;
  220. }
  221. else {
  222. this._events = [
  223. [this.element, {
  224. click: $.proxy(this.show, this)
  225. }]
  226. ];
  227. }
  228. for (var i = 0, el, ev; i < this._events.length; i++) {
  229. el = this._events[i][0];
  230. ev = this._events[i][1];
  231. el.on(ev);
  232. }
  233. },
  234. _detachEvents: function () {
  235. for (var i = 0, el, ev; i < this._events.length; i++) {
  236. el = this._events[i][0];
  237. ev = this._events[i][1];
  238. el.off(ev);
  239. }
  240. this._events = [];
  241. },
  242. show: function (e) {
  243. this.picker.show();
  244. this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  245. if (this.forceParse) {
  246. this.update();
  247. }
  248. this.place();
  249. $(window).on('resize', $.proxy(this.place, this));
  250. if (e) {
  251. e.stopPropagation();
  252. e.preventDefault();
  253. }
  254. this.isVisible = true;
  255. this.element.trigger({
  256. type: 'show',
  257. date: this.date
  258. });
  259. },
  260. hide: function (e) {
  261. if (!this.isVisible) return;
  262. if (this.isInline) return;
  263. this.picker.hide();
  264. $(window).off('resize', this.place);
  265. this.viewMode = this.startViewMode;
  266. this.showMode();
  267. if (!this.isInput) {
  268. $(document).off('mousedown', this.hide);
  269. }
  270. if (
  271. this.forceParse &&
  272. (
  273. this.isInput && this.element.val() ||
  274. this.hasInput && this.element.find('input').val()
  275. )
  276. )
  277. this.setValue();
  278. this.isVisible = false;
  279. this.element.trigger({
  280. type: 'hide',
  281. date: this.date
  282. });
  283. },
  284. remove: function () {
  285. this._detachEvents();
  286. this.picker.remove();
  287. delete this.picker;
  288. delete this.element.data().datetimepicker;
  289. },
  290. getDate: function () {
  291. var d = this.getUTCDate();
  292. return new Date(d.getTime() + (d.getTimezoneOffset() * 60000));
  293. },
  294. getUTCDate: function () {
  295. return this.date;
  296. },
  297. setDate: function (d) {
  298. this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset() * 60000)));
  299. },
  300. setUTCDate: function (d) {
  301. if (d >= this.startDate && d <= this.endDate) {
  302. this.date = d;
  303. this.setValue();
  304. this.viewDate = this.date;
  305. this.fill();
  306. } else {
  307. this.element.trigger({
  308. type: 'outOfRange',
  309. date: d,
  310. startDate: this.startDate,
  311. endDate: this.endDate
  312. });
  313. }
  314. },
  315. setFormat: function (format) {
  316. this.format = DPGlobal.parseFormat(format, this.formatType);
  317. var element;
  318. if (this.isInput) {
  319. element = this.element;
  320. } else if (this.component) {
  321. element = this.element.find('input');
  322. }
  323. if (element && element.val()) {
  324. this.setValue();
  325. }
  326. },
  327. setValue: function () {
  328. var formatted = this.getFormattedDate();
  329. if (!this.isInput) {
  330. if (this.component) {
  331. this.element.find('input').val(formatted);
  332. }
  333. this.element.data('date', formatted);
  334. } else {
  335. this.element.val(formatted);
  336. }
  337. if (this.linkField) {
  338. $('#' + this.linkField).val(this.getFormattedDate(this.linkFormat));
  339. }
  340. },
  341. getFormattedDate: function (format) {
  342. if (format == undefined) format = this.format;
  343. return DPGlobal.formatDate(this.date, format, this.language, this.formatType);
  344. },
  345. setStartDate: function (startDate) {
  346. this.startDate = startDate || -Infinity;
  347. if (this.startDate !== -Infinity) {
  348. this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language, this.formatType);
  349. }
  350. this.update();
  351. this.updateNavArrows();
  352. },
  353. setEndDate: function (endDate) {
  354. this.endDate = endDate || Infinity;
  355. if (this.endDate !== Infinity) {
  356. this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language, this.formatType);
  357. }
  358. this.update();
  359. this.updateNavArrows();
  360. },
  361. setDaysOfWeekDisabled: function (daysOfWeekDisabled) {
  362. this.daysOfWeekDisabled = daysOfWeekDisabled || [];
  363. if (!$.isArray(this.daysOfWeekDisabled)) {
  364. this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
  365. }
  366. this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function (d) {
  367. return parseInt(d, 10);
  368. });
  369. this.update();
  370. this.updateNavArrows();
  371. },
  372. place: function () {
  373. if (this.isInline) return;
  374. var index_highest = 0;
  375. $('div').each(function () {
  376. var index_current = parseInt($(this).css("zIndex"), 10);
  377. if (index_current > index_highest) {
  378. index_highest = index_current;
  379. }
  380. });
  381. var zIndex = index_highest + 10;
  382. var offset, top, left;
  383. if (this.component) {
  384. offset = this.component.offset();
  385. left = offset.left;
  386. if (this.pickerPosition == 'bottom-left' || this.pickerPosition == 'top-left') {
  387. left += this.component.outerWidth() - this.picker.outerWidth();
  388. }
  389. } else {
  390. offset = this.element.offset();
  391. left = offset.left;
  392. }
  393. if (this.pickerPosition == 'top-left' || this.pickerPosition == 'top-right') {
  394. top = offset.top - this.picker.outerHeight();
  395. } else {
  396. top = offset.top + this.height;
  397. }
  398. this.picker.css({
  399. top: top,
  400. left: left,
  401. zIndex: zIndex
  402. });
  403. },
  404. update: function () {
  405. var date, fromArgs = false;
  406. if (arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
  407. date = arguments[0];
  408. fromArgs = true;
  409. } else {
  410. date = this.element.data('date') || (this.isInput ? this.element.val() : this.element.find('input').val()) || this.initialDate;
  411. if (typeof date == 'string' || date instanceof String) {
  412. date = date.replace(/^\s+|\s+$/g,'');
  413. }
  414. }
  415. if (!date) {
  416. date = new Date();
  417. fromArgs = false;
  418. }
  419. this.date = DPGlobal.parseDate(date, this.format, this.language, this.formatType);
  420. if (fromArgs) this.setValue();
  421. if (this.date < this.startDate) {
  422. this.viewDate = new Date(this.startDate);
  423. } else if (this.date > this.endDate) {
  424. this.viewDate = new Date(this.endDate);
  425. } else {
  426. this.viewDate = new Date(this.date);
  427. }
  428. this.fill();
  429. },
  430. fillDow: function () {
  431. var dowCnt = this.weekStart,
  432. html = '<tr>';
  433. while (dowCnt < this.weekStart + 7) {
  434. html += '<th class="dow">' + dates[this.language].daysMin[(dowCnt++) % 7] + '</th>';
  435. }
  436. html += '</tr>';
  437. this.picker.find('.datetimepicker-days thead').append(html);
  438. },
  439. fillMonths: function () {
  440. var html = '',
  441. i = 0;
  442. while (i < 12) {
  443. html += '<span class="month">' + dates[this.language].monthsShort[i++] + '</span>';
  444. }
  445. this.picker.find('.datetimepicker-months td').html(html);
  446. },
  447. fill: function () {
  448. if (this.date == null || this.viewDate == null) {
  449. return;
  450. }
  451. var d = new Date(this.viewDate),
  452. year = d.getUTCFullYear(),
  453. month = d.getUTCMonth(),
  454. dayMonth = d.getUTCDate(),
  455. hours = d.getUTCHours(),
  456. minutes = d.getUTCMinutes(),
  457. startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
  458. startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
  459. endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
  460. endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
  461. currentDate = (new UTCDate(this.date.getUTCFullYear(), this.date.getUTCMonth(), this.date.getUTCDate())).valueOf(),
  462. today = new Date();
  463. this.picker.find('.datetimepicker-days thead th:eq(1)')
  464. .text(dates[this.language].months[month] + ' ' + year);
  465. if (this.formatViewType == "time") {
  466. var hourConverted = hours % 12 ? hours % 12 : 12;
  467. var hoursDisplay = (hourConverted < 10 ? '0' : '') + hourConverted;
  468. var minutesDisplay = (minutes < 10 ? '0' : '') + minutes;
  469. var meridianDisplay = dates[this.language].meridiem[hours < 12 ? 0 : 1];
  470. this.picker.find('.datetimepicker-hours thead th:eq(1)')
  471. .text(hoursDisplay + ':' + minutesDisplay + ' ' + meridianDisplay.toUpperCase());
  472. this.picker.find('.datetimepicker-minutes thead th:eq(1)')
  473. .text(hoursDisplay + ':' + minutesDisplay + ' ' + meridianDisplay.toUpperCase());
  474. } else {
  475. this.picker.find('.datetimepicker-hours thead th:eq(1)')
  476. .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);
  477. this.picker.find('.datetimepicker-minutes thead th:eq(1)')
  478. .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);
  479. }
  480. this.picker.find('tfoot th.today')
  481. .text(dates[this.language].today)
  482. .toggle(this.todayBtn !== false);
  483. this.updateNavArrows();
  484. this.fillMonths();
  485. /*var prevMonth = UTCDate(year, month, 0,0,0,0,0);
  486. prevMonth.setUTCDate(prevMonth.getDate() - (prevMonth.getUTCDay() - this.weekStart + 7)%7);*/
  487. var prevMonth = UTCDate(year, month - 1, 28, 0, 0, 0, 0),
  488. day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  489. prevMonth.setUTCDate(day);
  490. prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7) % 7);
  491. var nextMonth = new Date(prevMonth);
  492. nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  493. nextMonth = nextMonth.valueOf();
  494. var html = [];
  495. var clsName;
  496. while (prevMonth.valueOf() < nextMonth) {
  497. if (prevMonth.getUTCDay() == this.weekStart) {
  498. html.push('<tr>');
  499. }
  500. clsName = '';
  501. if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
  502. clsName += ' old';
  503. } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
  504. clsName += ' new';
  505. }
  506. // Compare internal UTC date with local today, not UTC today
  507. if (this.todayHighlight &&
  508. prevMonth.getUTCFullYear() == today.getFullYear() &&
  509. prevMonth.getUTCMonth() == today.getMonth() &&
  510. prevMonth.getUTCDate() == today.getDate()) {
  511. clsName += ' today';
  512. }
  513. if (prevMonth.valueOf() == currentDate) {
  514. clsName += ' active';
  515. }
  516. if ((prevMonth.valueOf() + 86400000) <= this.startDate || prevMonth.valueOf() > this.endDate ||
  517. $.inArray(prevMonth.getUTCDay(), this.daysOfWeekDisabled) !== -1) {
  518. clsName += ' disabled';
  519. }
  520. html.push('<td class="day' + clsName + '">' + prevMonth.getUTCDate() + '</td>');
  521. if (prevMonth.getUTCDay() == this.weekEnd) {
  522. html.push('</tr>');
  523. }
  524. prevMonth.setUTCDate(prevMonth.getUTCDate() + 1);
  525. }
  526. this.picker.find('.datetimepicker-days tbody').empty().append(html.join(''));
  527. html = [];
  528. var txt = '', meridian = '', meridianOld = '';
  529. for (var i = 0; i < 24; i++) {
  530. var actual = UTCDate(year, month, dayMonth, i);
  531. clsName = '';
  532. // We want the previous hour for the startDate
  533. if ((actual.valueOf() + 3600000) <= this.startDate || actual.valueOf() > this.endDate) {
  534. clsName += ' disabled';
  535. } else if (hours == i) {
  536. clsName += ' active';
  537. }
  538. if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  539. meridian = (i < 12 ? dates[this.language].meridiem[0] : dates[this.language].meridiem[1]);
  540. if (meridian != meridianOld) {
  541. if (meridianOld != '') {
  542. html.push('</fieldset>');
  543. }
  544. html.push('<fieldset class="hour"><legend>' + meridian.toUpperCase() + '</legend>');
  545. }
  546. meridianOld = meridian;
  547. txt = (i % 12 ? i % 12 : 12);
  548. html.push('<span class="hour' + clsName + ' hour_' + (i < 12 ? 'am' : 'pm') + '">' + txt + '</span>');
  549. if (i == 23) {
  550. html.push('</fieldset>');
  551. }
  552. } else {
  553. txt = i + ':00';
  554. html.push('<span class="hour' + clsName + '">' + txt + '</span>');
  555. }
  556. }
  557. this.picker.find('.datetimepicker-hours td').html(html.join(''));
  558. html = [];
  559. txt = '', meridian = '', meridianOld = '';
  560. for (var i = 0; i < 60; i += this.minuteStep) {
  561. var actual = UTCDate(year, month, dayMonth, hours, i, 0);
  562. clsName = '';
  563. if (actual.valueOf() < this.startDate || actual.valueOf() > this.endDate) {
  564. clsName += ' disabled';
  565. } else if (Math.floor(minutes / this.minuteStep) == Math.floor(i / this.minuteStep)) {
  566. clsName += ' active';
  567. }
  568. if (this.showMeridian && dates[this.language].meridiem.length == 2) {
  569. meridian = (hours < 12 ? dates[this.language].meridiem[0] : dates[this.language].meridiem[1]);
  570. if (meridian != meridianOld) {
  571. if (meridianOld != '') {
  572. html.push('</fieldset>');
  573. }
  574. html.push('<fieldset class="minute"><legend>' + meridian.toUpperCase() + '</legend>');
  575. }
  576. meridianOld = meridian;
  577. txt = (hours % 12 ? hours % 12 : 12);
  578. //html.push('<span class="minute'+clsName+' minute_'+(hours<12?'am':'pm')+'">'+txt+'</span>');
  579. html.push('<span class="minute' + clsName + '">' + txt + ':' + (i < 10 ? '0' + i : i) + '</span>');
  580. if (i == 59) {
  581. html.push('</fieldset>');
  582. }
  583. } else {
  584. txt = i + ':00';
  585. //html.push('<span class="hour'+clsName+'">'+txt+'</span>');
  586. html.push('<span class="minute' + clsName + '">' + hours + ':' + (i < 10 ? '0' + i : i) + '</span>');
  587. }
  588. }
  589. this.picker.find('.datetimepicker-minutes td').html(html.join(''));
  590. var currentYear = this.date.getUTCFullYear();
  591. var months = this.picker.find('.datetimepicker-months')
  592. .find('th:eq(1)')
  593. .text(year)
  594. .end()
  595. .find('span').removeClass('active');
  596. if (currentYear == year) {
  597. months.eq(this.date.getUTCMonth()).addClass('active');
  598. }
  599. if (year < startYear || year > endYear) {
  600. months.addClass('disabled');
  601. }
  602. if (year == startYear) {
  603. months.slice(0, startMonth).addClass('disabled');
  604. }
  605. if (year == endYear) {
  606. months.slice(endMonth + 1).addClass('disabled');
  607. }
  608. html = '';
  609. year = parseInt(year / 10, 10) * 10;
  610. var yearCont = this.picker.find('.datetimepicker-years')
  611. .find('th:eq(1)')
  612. .text(year + '-' + (year + 9))
  613. .end()
  614. .find('td');
  615. year -= 1;
  616. for (var i = -1; i < 11; i++) {
  617. html += '<span class="year' + (i == -1 || i == 10 ? ' old' : '') + (currentYear == year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : '') + '">' + year + '</span>';
  618. year += 1;
  619. }
  620. yearCont.html(html);
  621. this.place();
  622. },
  623. updateNavArrows: function () {
  624. var d = new Date(this.viewDate),
  625. year = d.getUTCFullYear(),
  626. month = d.getUTCMonth(),
  627. day = d.getUTCDate(),
  628. hour = d.getUTCHours();
  629. switch (this.viewMode) {
  630. case 0:
  631. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  632. && month <= this.startDate.getUTCMonth()
  633. && day <= this.startDate.getUTCDate()
  634. && hour <= this.startDate.getUTCHours()) {
  635. this.picker.find('.prev').css({visibility: 'hidden'});
  636. } else {
  637. this.picker.find('.prev').css({visibility: 'visible'});
  638. }
  639. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  640. && month >= this.endDate.getUTCMonth()
  641. && day >= this.endDate.getUTCDate()
  642. && hour >= this.endDate.getUTCHours()) {
  643. this.picker.find('.next').css({visibility: 'hidden'});
  644. } else {
  645. this.picker.find('.next').css({visibility: 'visible'});
  646. }
  647. break;
  648. case 1:
  649. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  650. && month <= this.startDate.getUTCMonth()
  651. && day <= this.startDate.getUTCDate()) {
  652. this.picker.find('.prev').css({visibility: 'hidden'});
  653. } else {
  654. this.picker.find('.prev').css({visibility: 'visible'});
  655. }
  656. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  657. && month >= this.endDate.getUTCMonth()
  658. && day >= this.endDate.getUTCDate()) {
  659. this.picker.find('.next').css({visibility: 'hidden'});
  660. } else {
  661. this.picker.find('.next').css({visibility: 'visible'});
  662. }
  663. break;
  664. case 2:
  665. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()
  666. && month <= this.startDate.getUTCMonth()) {
  667. this.picker.find('.prev').css({visibility: 'hidden'});
  668. } else {
  669. this.picker.find('.prev').css({visibility: 'visible'});
  670. }
  671. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()
  672. && month >= this.endDate.getUTCMonth()) {
  673. this.picker.find('.next').css({visibility: 'hidden'});
  674. } else {
  675. this.picker.find('.next').css({visibility: 'visible'});
  676. }
  677. break;
  678. case 3:
  679. case 4:
  680. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
  681. this.picker.find('.prev').css({visibility: 'hidden'});
  682. } else {
  683. this.picker.find('.prev').css({visibility: 'visible'});
  684. }
  685. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
  686. this.picker.find('.next').css({visibility: 'hidden'});
  687. } else {
  688. this.picker.find('.next').css({visibility: 'visible'});
  689. }
  690. break;
  691. }
  692. },
  693. mousewheel: function (e) {
  694. e.preventDefault();
  695. e.stopPropagation();
  696. if (this.wheelPause) {
  697. return;
  698. }
  699. this.wheelPause = true;
  700. var originalEvent = e.originalEvent;
  701. var delta = originalEvent.wheelDelta;
  702. var mode = delta > 0 ? 1 : (delta === 0) ? 0 : -1;
  703. if (this.wheelViewModeNavigationInverseDirection) {
  704. mode = -mode;
  705. }
  706. this.showMode(mode);
  707. setTimeout($.proxy(function () {
  708. this.wheelPause = false
  709. }, this), this.wheelViewModeNavigationDelay);
  710. },
  711. click: function (e) {
  712. e.stopPropagation();
  713. e.preventDefault();
  714. var target = $(e.target).closest('span, td, th, legend');
  715. if (target.length == 1) {
  716. if (target.is('.disabled')) {
  717. this.element.trigger({
  718. type: 'outOfRange',
  719. date: this.viewDate,
  720. startDate: this.startDate,
  721. endDate: this.endDate
  722. });
  723. return;
  724. }
  725. switch (target[0].nodeName.toLowerCase()) {
  726. case 'th':
  727. switch (target[0].className) {
  728. case 'switch':
  729. this.showMode(1);
  730. break;
  731. case 'prev':
  732. case 'next':
  733. var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  734. switch (this.viewMode) {
  735. case 0:
  736. this.viewDate = this.moveHour(this.viewDate, dir);
  737. break;
  738. case 1:
  739. this.viewDate = this.moveDate(this.viewDate, dir);
  740. break;
  741. case 2:
  742. this.viewDate = this.moveMonth(this.viewDate, dir);
  743. break;
  744. case 3:
  745. case 4:
  746. this.viewDate = this.moveYear(this.viewDate, dir);
  747. break;
  748. }
  749. this.fill();
  750. break;
  751. case 'today':
  752. var date = new Date();
  753. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0);
  754. // Respect startDate and endDate.
  755. if (date < this.startDate) date = this.startDate;
  756. else if (date > this.endDate) date = this.endDate;
  757. this.viewMode = this.startViewMode;
  758. this.showMode(0);
  759. this._setDate(date);
  760. this.fill();
  761. if (this.autoclose) {
  762. this.hide();
  763. }
  764. break;
  765. }
  766. break;
  767. case 'span':
  768. if (!target.is('.disabled')) {
  769. var year = this.viewDate.getUTCFullYear(),
  770. month = this.viewDate.getUTCMonth(),
  771. day = this.viewDate.getUTCDate(),
  772. hours = this.viewDate.getUTCHours(),
  773. minutes = this.viewDate.getUTCMinutes(),
  774. seconds = this.viewDate.getUTCSeconds();
  775. if (target.is('.month')) {
  776. this.viewDate.setUTCDate(1);
  777. month = target.parent().find('span').index(target);
  778. day = this.viewDate.getUTCDate();
  779. this.viewDate.setUTCMonth(month);
  780. this.element.trigger({
  781. type: 'changeMonth',
  782. date: this.viewDate
  783. });
  784. if (this.viewSelect >= 3) {
  785. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  786. }
  787. } else if (target.is('.year')) {
  788. this.viewDate.setUTCDate(1);
  789. year = parseInt(target.text(), 10) || 0;
  790. this.viewDate.setUTCFullYear(year);
  791. this.element.trigger({
  792. type: 'changeYear',
  793. date: this.viewDate
  794. });
  795. if (this.viewSelect >= 4) {
  796. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  797. }
  798. } else if (target.is('.hour')) {
  799. hours = parseInt(target.text(), 10) || 0;
  800. if (target.hasClass('hour_am') || target.hasClass('hour_pm')) {
  801. if (hours == 12 && target.hasClass('hour_am')) {
  802. hours = 0;
  803. } else if (hours != 12 && target.hasClass('hour_pm')) {
  804. hours += 12;
  805. }
  806. }
  807. this.viewDate.setUTCHours(hours);
  808. this.element.trigger({
  809. type: 'changeHour',
  810. date: this.viewDate
  811. });
  812. if (this.viewSelect >= 1) {
  813. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  814. }
  815. } else if (target.is('.minute')) {
  816. minutes = parseInt(target.text().substr(target.text().indexOf(':') + 1), 10) || 0;
  817. this.viewDate.setUTCMinutes(minutes);
  818. this.element.trigger({
  819. type: 'changeMinute',
  820. date: this.viewDate
  821. });
  822. if (this.viewSelect >= 0) {
  823. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  824. }
  825. }
  826. if (this.viewMode != 0) {
  827. var oldViewMode = this.viewMode;
  828. this.showMode(-1);
  829. this.fill();
  830. if (oldViewMode == this.viewMode && this.autoclose) {
  831. this.hide();
  832. }
  833. } else {
  834. this.fill();
  835. if (this.autoclose) {
  836. this.hide();
  837. }
  838. }
  839. }
  840. break;
  841. case 'td':
  842. if (target.is('.day') && !target.is('.disabled')) {
  843. var day = parseInt(target.text(), 10) || 1;
  844. var year = this.viewDate.getUTCFullYear(),
  845. month = this.viewDate.getUTCMonth(),
  846. hours = this.viewDate.getUTCHours(),
  847. minutes = this.viewDate.getUTCMinutes(),
  848. seconds = this.viewDate.getUTCSeconds();
  849. if (target.is('.old')) {
  850. if (month === 0) {
  851. month = 11;
  852. year -= 1;
  853. } else {
  854. month -= 1;
  855. }
  856. } else if (target.is('.new')) {
  857. if (month == 11) {
  858. month = 0;
  859. year += 1;
  860. } else {
  861. month += 1;
  862. }
  863. }
  864. this.viewDate.setUTCFullYear(year);
  865. this.viewDate.setUTCMonth(month, day);
  866. this.element.trigger({
  867. type: 'changeDay',
  868. date: this.viewDate
  869. });
  870. if (this.viewSelect >= 2) {
  871. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  872. }
  873. }
  874. var oldViewMode = this.viewMode;
  875. this.showMode(-1);
  876. this.fill();
  877. if (oldViewMode == this.viewMode && this.autoclose) {
  878. this.hide();
  879. }
  880. break;
  881. }
  882. }
  883. },
  884. _setDate: function (date, which) {
  885. if (!which || which == 'date')
  886. this.date = date;
  887. if (!which || which == 'view')
  888. this.viewDate = date;
  889. this.fill();
  890. this.setValue();
  891. var element;
  892. if (this.isInput) {
  893. element = this.element;
  894. } else if (this.component) {
  895. element = this.element.find('input');
  896. }
  897. if (element) {
  898. element.change();
  899. if (this.autoclose && (!which || which == 'date')) {
  900. //this.hide();
  901. }
  902. }
  903. this.element.trigger({
  904. type: 'changeDate',
  905. date: this.date
  906. });
  907. },
  908. moveMinute: function (date, dir) {
  909. if (!dir) return date;
  910. var new_date = new Date(date.valueOf());
  911. //dir = dir > 0 ? 1 : -1;
  912. new_date.setUTCMinutes(new_date.getUTCMinutes() + (dir * this.minuteStep));
  913. return new_date;
  914. },
  915. moveHour: function (date, dir) {
  916. if (!dir) return date;
  917. var new_date = new Date(date.valueOf());
  918. //dir = dir > 0 ? 1 : -1;
  919. new_date.setUTCHours(new_date.getUTCHours() + dir);
  920. return new_date;
  921. },
  922. moveDate: function (date, dir) {
  923. if (!dir) return date;
  924. var new_date = new Date(date.valueOf());
  925. //dir = dir > 0 ? 1 : -1;
  926. new_date.setUTCDate(new_date.getUTCDate() + dir);
  927. return new_date;
  928. },
  929. moveMonth: function (date, dir) {
  930. if (!dir) return date;
  931. var new_date = new Date(date.valueOf()),
  932. day = new_date.getUTCDate(),
  933. month = new_date.getUTCMonth(),
  934. mag = Math.abs(dir),
  935. new_month, test;
  936. dir = dir > 0 ? 1 : -1;
  937. if (mag == 1) {
  938. test = dir == -1
  939. // If going back one month, make sure month is not current month
  940. // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  941. ? function () {
  942. return new_date.getUTCMonth() == month;
  943. }
  944. // If going forward one month, make sure month is as expected
  945. // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  946. : function () {
  947. return new_date.getUTCMonth() != new_month;
  948. };
  949. new_month = month + dir;
  950. new_date.setUTCMonth(new_month);
  951. // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  952. if (new_month < 0 || new_month > 11)
  953. new_month = (new_month + 12) % 12;
  954. } else {
  955. // For magnitudes >1, move one month at a time...
  956. for (var i = 0; i < mag; i++)
  957. // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  958. new_date = this.moveMonth(new_date, dir);
  959. // ...then reset the day, keeping it in the new month
  960. new_month = new_date.getUTCMonth();
  961. new_date.setUTCDate(day);
  962. test = function () {
  963. return new_month != new_date.getUTCMonth();
  964. };
  965. }
  966. // Common date-resetting loop -- if date is beyond end of month, make it
  967. // end of month
  968. while (test()) {
  969. new_date.setUTCDate(--day);
  970. new_date.setUTCMonth(new_month);
  971. }
  972. return new_date;
  973. },
  974. moveYear: function (date, dir) {
  975. return this.moveMonth(date, dir * 12);
  976. },
  977. dateWithinRange: function (date) {
  978. return date >= this.startDate && date <= this.endDate;
  979. },
  980. keydown: function (e) {
  981. if (this.picker.is(':not(:visible)')) {
  982. if (e.keyCode == 27) // allow escape to hide and re-show picker
  983. this.show();
  984. return;
  985. }
  986. var dateChanged = false,
  987. dir, day, month,
  988. newDate, newViewDate;
  989. switch (e.keyCode) {
  990. case 27: // escape
  991. this.hide();
  992. e.preventDefault();
  993. break;
  994. case 37: // left
  995. case 39: // right
  996. if (!this.keyboardNavigation) break;
  997. dir = e.keyCode == 37 ? -1 : 1;
  998. viewMode = this.viewMode;
  999. if (e.ctrlKey) {
  1000. viewMode += 2;
  1001. } else if (e.shiftKey) {
  1002. viewMode += 1;
  1003. }
  1004. if (viewMode == 4) {
  1005. newDate = this.moveYear(this.date, dir);
  1006. newViewDate = this.moveYear(this.viewDate, dir);
  1007. } else if (viewMode == 3) {
  1008. newDate = this.moveMonth(this.date, dir);
  1009. newViewDate = this.moveMonth(this.viewDate, dir);
  1010. } else if (viewMode == 2) {
  1011. newDate = this.moveDate(this.date, dir);
  1012. newViewDate = this.moveDate(this.viewDate, dir);
  1013. } else if (viewMode == 1) {
  1014. newDate = this.moveHour(this.date, dir);
  1015. newViewDate = this.moveHour(this.viewDate, dir);
  1016. } else if (viewMode == 0) {
  1017. newDate = this.moveMinute(this.date, dir);
  1018. newViewDate = this.moveMinute(this.viewDate, dir);
  1019. }
  1020. if (this.dateWithinRange(newDate)) {
  1021. this.date = newDate;
  1022. this.viewDate = newViewDate;
  1023. this.setValue();
  1024. this.update();
  1025. e.preventDefault();
  1026. dateChanged = true;
  1027. }
  1028. break;
  1029. case 38: // up
  1030. case 40: // down
  1031. if (!this.keyboardNavigation) break;
  1032. dir = e.keyCode == 38 ? -1 : 1;
  1033. viewMode = this.viewMode;
  1034. if (e.ctrlKey) {
  1035. viewMode += 2;
  1036. } else if (e.shiftKey) {
  1037. viewMode += 1;
  1038. }
  1039. if (viewMode == 4) {
  1040. newDate = this.moveYear(this.date, dir);
  1041. newViewDate = this.moveYear(this.viewDate, dir);
  1042. } else if (viewMode == 3) {
  1043. newDate = this.moveMonth(this.date, dir);
  1044. newViewDate = this.moveMonth(this.viewDate, dir);
  1045. } else if (viewMode == 2) {
  1046. newDate = this.moveDate(this.date, dir * 7);
  1047. newViewDate = this.moveDate(this.viewDate, dir * 7);
  1048. } else if (viewMode == 1) {
  1049. if (this.showMeridian) {
  1050. newDate = this.moveHour(this.date, dir * 6);
  1051. newViewDate = this.moveHour(this.viewDate, dir * 6);
  1052. } else {
  1053. newDate = this.moveHour(this.date, dir * 4);
  1054. newViewDate = this.moveHour(this.viewDate, dir * 4);
  1055. }
  1056. } else if (viewMode == 0) {
  1057. newDate = this.moveMinute(this.date, dir * 4);
  1058. newViewDate = this.moveMinute(this.viewDate, dir * 4);
  1059. }
  1060. if (this.dateWithinRange(newDate)) {
  1061. this.date = newDate;
  1062. this.viewDate = newViewDate;
  1063. this.setValue();
  1064. this.update();
  1065. e.preventDefault();
  1066. dateChanged = true;
  1067. }
  1068. break;
  1069. case 13: // enter
  1070. if (this.viewMode != 0) {
  1071. var oldViewMode = this.viewMode;
  1072. this.showMode(-1);
  1073. this.fill();
  1074. if (oldViewMode == this.viewMode && this.autoclose) {
  1075. this.hide();
  1076. }
  1077. } else {
  1078. this.fill();
  1079. if (this.autoclose) {
  1080. this.hide();
  1081. }
  1082. }
  1083. e.preventDefault();
  1084. break;
  1085. case 9: // tab
  1086. this.hide();
  1087. break;
  1088. }
  1089. if (dateChanged) {
  1090. var element;
  1091. if (this.isInput) {
  1092. element = this.element;
  1093. } else if (this.component) {
  1094. element = this.element.find('input');
  1095. }
  1096. if (element) {
  1097. element.change();
  1098. }
  1099. this.element.trigger({
  1100. type: 'changeDate',
  1101. date: this.date
  1102. });
  1103. }
  1104. },
  1105. showMode: function (dir) {
  1106. if (dir) {
  1107. var newViewMode = Math.max(0, Math.min(DPGlobal.modes.length - 1, this.viewMode + dir));
  1108. if (newViewMode >= this.minView && newViewMode <= this.maxView) {
  1109. this.element.trigger({
  1110. type: 'changeMode',
  1111. date: this.viewDate,
  1112. oldViewMode: this.viewMode,
  1113. newViewMode: newViewMode
  1114. });
  1115. this.viewMode = newViewMode;
  1116. }
  1117. }
  1118. /*
  1119. vitalets: fixing bug of very special conditions:
  1120. jquery 1.7.1 + webkit + show inline datetimepicker in bootstrap popover.
  1121. Method show() does not set display css correctly and datetimepicker is not shown.
  1122. Changed to .css('display', 'block') solve the problem.
  1123. See https://github.com/vitalets/x-editable/issues/37
  1124. In jquery 1.7.2+ everything works fine.
  1125. */
  1126. //this.picker.find('>div').hide().filter('.datetimepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  1127. this.picker.find('>div').hide().filter('.datetimepicker-' + DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
  1128. this.updateNavArrows();
  1129. },
  1130. reset: function (e) {
  1131. this._setDate(null, 'date');
  1132. }
  1133. };
  1134. $.fn.datetimepicker = function (option) {
  1135. var args = Array.apply(null, arguments);
  1136. args.shift();
  1137. var internal_return;
  1138. this.each(function () {
  1139. var $this = $(this),
  1140. data = $this.data('datetimepicker'),
  1141. options = typeof option == 'object' && option;
  1142. if (!data) {
  1143. $this.data('datetimepicker', (data = new Datetimepicker(this, $.extend({}, $.fn.datetimepicker.defaults, options))));
  1144. }
  1145. if (typeof option == 'string' && typeof data[option] == 'function') {
  1146. internal_return = data[option].apply(data, args);
  1147. if (internal_return !== undefined) {
  1148. return false;
  1149. }
  1150. }
  1151. });
  1152. if (internal_return !== undefined)
  1153. return internal_return;
  1154. else
  1155. return this;
  1156. };
  1157. $.fn.datetimepicker.defaults = {
  1158. };
  1159. $.fn.datetimepicker.Constructor = Datetimepicker;
  1160. var dates = $.fn.datetimepicker.dates = {
  1161. en: {
  1162. days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
  1163. daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
  1164. daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
  1165. months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  1166. monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  1167. meridiem: ["am", "pm"],
  1168. suffix: ["st", "nd", "rd", "th"],
  1169. today: "Today"
  1170. }
  1171. };
  1172. var DPGlobal = {
  1173. modes: [
  1174. {
  1175. clsName: 'minutes',
  1176. navFnc: 'Hours',
  1177. navStep: 1
  1178. },
  1179. {
  1180. clsName: 'hours',
  1181. navFnc: 'Date',
  1182. navStep: 1
  1183. },
  1184. {
  1185. clsName: 'days',
  1186. navFnc: 'Month',
  1187. navStep: 1
  1188. },
  1189. {
  1190. clsName: 'months',
  1191. navFnc: 'FullYear',
  1192. navStep: 1
  1193. },
  1194. {
  1195. clsName: 'years',
  1196. navFnc: 'FullYear',
  1197. navStep: 10
  1198. }
  1199. ],
  1200. isLeapYear: function (year) {
  1201. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0))
  1202. },
  1203. getDaysInMonth: function (year, month) {
  1204. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
  1205. },
  1206. getDefaultFormat: function (type, field) {
  1207. if (type == "standard") {
  1208. if (field == 'input')
  1209. return 'yyyy-mm-dd hh:ii';
  1210. else
  1211. return 'yyyy-mm-dd hh:ii:ss';
  1212. } else if (type == "php") {
  1213. if (field == 'input')
  1214. return 'Y-m-d H:i';
  1215. else
  1216. return 'Y-m-d H:i:s';
  1217. } else {
  1218. throw new Error("Invalid format type.");
  1219. }
  1220. },
  1221. validParts: function (type) {
  1222. if (type == "standard") {
  1223. return /hh?|HH?|p|P|ii?|ss?|dd?|DD?|mm?|MM?|yy(?:yy)?/g;
  1224. } else if (type == "php") {
  1225. return /[dDjlNwzFmMnStyYaABgGhHis]/g;
  1226. } else {
  1227. throw new Error("Invalid format type.");
  1228. }
  1229. },
  1230. nonpunctuation: /[^ -\/:-@\[-`{-~\t\n\rTZ]+/g,
  1231. parseFormat: function (format, type) {
  1232. // IE treats \0 as a string end in inputs (truncating the value),
  1233. // so it's a bad format delimiter, anyway
  1234. var separators = format.replace(this.validParts(type), '\0').split('\0'),
  1235. parts = format.match(this.validParts(type));
  1236. if (!separators || !separators.length || !parts || parts.length == 0) {
  1237. throw new Error("Invalid date format.");
  1238. }
  1239. return {separators: separators, parts: parts};
  1240. },
  1241. parseDate: function (date, format, language, type) {
  1242. if (date instanceof Date) {
  1243. var dateUTC = new Date(date.valueOf() - date.getTimezoneOffset() * 60000);
  1244. dateUTC.setMilliseconds(0);
  1245. return dateUTC;
  1246. }
  1247. if (/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(date)) {
  1248. format = this.parseFormat('yyyy-mm-dd', type);
  1249. }
  1250. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}$/.test(date)) {
  1251. format = this.parseFormat('yyyy-mm-dd hh:ii', type);
  1252. }
  1253. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}\:\d{1,2}[Z]{0,1}$/.test(date)) {
  1254. format = this.parseFormat('yyyy-mm-dd hh:ii:ss', type);
  1255. }
  1256. if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
  1257. var part_re = /([-+]\d+)([dmwy])/,
  1258. parts = date.match(/([-+]\d+)([dmwy])/g),
  1259. part, dir;
  1260. date = new Date();
  1261. for (var i = 0; i < parts.length; i++) {
  1262. part = part_re.exec(parts[i]);
  1263. dir = parseInt(part[1]);
  1264. switch (part[2]) {
  1265. case 'd':
  1266. date.setUTCDate(date.getUTCDate() + dir);
  1267. break;
  1268. case 'm':
  1269. date = Datetimepicker.prototype.moveMonth.call(Datetimepicker.prototype, date, dir);
  1270. break;
  1271. case 'w':
  1272. date.setUTCDate(date.getUTCDate() + dir * 7);
  1273. break;
  1274. case 'y':
  1275. date = Datetimepicker.prototype.moveYear.call(Datetimepicker.prototype, date, dir);
  1276. break;
  1277. }
  1278. }
  1279. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), 0);
  1280. }
  1281. var parts = date && date.match(this.nonpunctuation) || [],
  1282. date = new Date(0, 0, 0, 0, 0, 0, 0),
  1283. parsed = {},
  1284. setters_order = ['hh', 'h', 'ii', 'i', 'ss', 's', 'yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'D', 'DD', 'd', 'dd', 'H', 'HH', 'p', 'P'],
  1285. setters_map = {
  1286. hh: function (d, v) {
  1287. return d.setUTCHours(v);
  1288. },
  1289. h: function (d, v) {
  1290. return d.setUTCHours(v);
  1291. },
  1292. HH: function (d, v) {
  1293. return d.setUTCHours(v == 12 ? 0 : v);
  1294. },
  1295. H: function (d, v) {
  1296. return d.setUTCHours(v == 12 ? 0 : v);
  1297. },
  1298. ii: function (d, v) {
  1299. return d.setUTCMinutes(v);
  1300. },
  1301. i: function (d, v) {
  1302. return d.setUTCMinutes(v);
  1303. },
  1304. ss: function (d, v) {
  1305. return d.setUTCSeconds(v);
  1306. },
  1307. s: function (d, v) {
  1308. return d.setUTCSeconds(v);
  1309. },
  1310. yyyy: function (d, v) {
  1311. return d.setUTCFullYear(v);
  1312. },
  1313. yy: function (d, v) {
  1314. return d.setUTCFullYear(2000 + v);
  1315. },
  1316. m: function (d, v) {
  1317. v -= 1;
  1318. while (v < 0) v += 12;
  1319. v %= 12;
  1320. d.setUTCMonth(v);
  1321. while (d.getUTCMonth() != v)
  1322. d.setUTCDate(d.getUTCDate() - 1);
  1323. return d;
  1324. },
  1325. d: function (d, v) {
  1326. return d.setUTCDate(v);
  1327. },
  1328. p: function (d, v) {
  1329. return d.setUTCHours(v == 1 ? d.getUTCHours() + 12 : d.getUTCHours());
  1330. }
  1331. },
  1332. val, filtered, part;
  1333. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1334. setters_map['dd'] = setters_map['d'];
  1335. setters_map['P'] = setters_map['p'];
  1336. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  1337. if (parts.length == format.parts.length) {
  1338. for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
  1339. val = parseInt(parts[i], 10);
  1340. part = format.parts[i];
  1341. if (isNaN(val)) {
  1342. switch (part) {
  1343. case 'MM':
  1344. filtered = $(dates[language].months).filter(function () {
  1345. var m = this.slice(0, parts[i].length),
  1346. p = parts[i].slice(0, m.length);
  1347. return m == p;
  1348. });
  1349. val = $.inArray(filtered[0], dates[language].months) + 1;
  1350. break;
  1351. case 'M':
  1352. filtered = $(dates[language].monthsShort).filter(function () {
  1353. var m = this.slice(0, parts[i].length),
  1354. p = parts[i].slice(0, m.length);
  1355. return m == p;
  1356. });
  1357. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1358. break;
  1359. case 'p':
  1360. case 'P':
  1361. val = $.inArray(parts[i].toLowerCase(), dates[language].meridiem);
  1362. break;
  1363. }
  1364. }
  1365. parsed[part] = val;
  1366. }
  1367. for (var i = 0, s; i < setters_order.length; i++) {
  1368. s = setters_order[i];
  1369. if (s in parsed && !isNaN(parsed[s]))
  1370. setters_map[s](date, parsed[s])
  1371. }
  1372. }
  1373. return date;
  1374. },
  1375. formatDate: function (date, format, language, type) {
  1376. if (date == null) {
  1377. return '';
  1378. }
  1379. var val;
  1380. if (type == 'standard') {
  1381. val = {
  1382. // year
  1383. yy: date.getUTCFullYear().toString().substring(2),
  1384. yyyy: date.getUTCFullYear(),
  1385. // month
  1386. m: date.getUTCMonth() + 1,
  1387. M: dates[language].monthsShort[date.getUTCMonth()],
  1388. MM: dates[language].months[date.getUTCMonth()],
  1389. // day
  1390. d: date.getUTCDate(),
  1391. D: dates[language].daysShort[date.getUTCDay()],
  1392. DD: dates[language].days[date.getUTCDay()],
  1393. p: (dates[language].meridiem.length == 2 ? dates[language].meridiem[date.getUTCHours() < 12 ? 0 : 1] : ''),
  1394. // hour
  1395. h: date.getUTCHours(),
  1396. // minute
  1397. i: date.getUTCMinutes(),
  1398. // second
  1399. s: date.getUTCSeconds()
  1400. };
  1401. if (dates[language].meridiem.length == 2) {
  1402. val.H = (val.h % 12 == 0 ? 12 : val.h % 12);
  1403. }
  1404. else {
  1405. val.H = val.h;
  1406. }
  1407. val.HH = (val.H < 10 ? '0' : '') + val.H;
  1408. val.P = val.p.toUpperCase();
  1409. val.hh = (val.h < 10 ? '0' : '') + val.h;
  1410. val.ii = (val.i < 10 ? '0' : '') + val.i;
  1411. val.ss = (val.s < 10 ? '0' : '') + val.s;
  1412. val.dd = (val.d < 10 ? '0' : '') + val.d;
  1413. val.mm = (val.m < 10 ? '0' : '') + val.m;
  1414. } else if (type == 'php') {
  1415. // php format
  1416. val = {
  1417. // year
  1418. y: date.getUTCFullYear().toString().substring(2),
  1419. Y: date.getUTCFullYear(),
  1420. // month
  1421. F: dates[language].months[date.getUTCMonth()],
  1422. M: dates[language].monthsShort[date.getUTCMonth()],
  1423. n: date.getUTCMonth() + 1,
  1424. t: DPGlobal.getDaysInMonth(date.getUTCFullYear(), date.getUTCMonth()),
  1425. // day
  1426. j: date.getUTCDate(),
  1427. l: dates[language].days[date.getUTCDay()],
  1428. D: dates[language].daysShort[date.getUTCDay()],
  1429. w: date.getUTCDay(), // 0 -> 6
  1430. N: (date.getUTCDay() == 0 ? 7 : date.getUTCDay()), // 1 -> 7
  1431. S: (date.getUTCDate() % 10 <= dates[language].suffix.length ? dates[language].suffix[date.getUTCDate() % 10 - 1] : ''),
  1432. // hour
  1433. a: (dates[language].meridiem.length == 2 ? dates[language].meridiem[date.getUTCHours() < 12 ? 0 : 1] : ''),
  1434. g: (date.getUTCHours() % 12 == 0 ? 12 : date.getUTCHours() % 12),
  1435. G: date.getUTCHours(),
  1436. // minute
  1437. i: date.getUTCMinutes(),
  1438. // second
  1439. s: date.getUTCSeconds()
  1440. };
  1441. val.m = (val.n < 10 ? '0' : '') + val.n;
  1442. val.d = (val.j < 10 ? '0' : '') + val.j;
  1443. val.A = val.a.toString().toUpperCase();
  1444. val.h = (val.g < 10 ? '0' : '') + val.g;
  1445. val.H = (val.G < 10 ? '0' : '') + val.G;
  1446. val.i = (val.i < 10 ? '0' : '') + val.i;
  1447. val.s = (val.s < 10 ? '0' : '') + val.s;
  1448. } else {
  1449. throw new Error("Invalid format type.");
  1450. }
  1451. var date = [],
  1452. seps = $.extend([], format.separators);
  1453. for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
  1454. if (seps.length) {
  1455. date.push(seps.shift());
  1456. }
  1457. date.push(val[format.parts[i]]);
  1458. }
  1459. if (seps.length) {
  1460. date.push(seps.shift());
  1461. }
  1462. return date.join('');
  1463. },
  1464. convertViewMode: function (viewMode) {
  1465. switch (viewMode) {
  1466. case 4:
  1467. case 'decade':
  1468. viewMode = 4;
  1469. break;
  1470. case 3:
  1471. case 'year':
  1472. viewMode = 3;
  1473. break;
  1474. case 2:
  1475. case 'month':
  1476. viewMode = 2;
  1477. break;
  1478. case 1:
  1479. case 'day':
  1480. viewMode = 1;
  1481. break;
  1482. case 0:
  1483. case 'hour':
  1484. viewMode = 0;
  1485. break;
  1486. }
  1487. return viewMode;
  1488. },
  1489. headTemplate: '<thead>' +
  1490. '<tr>' +
  1491. '<th class="prev"><i class="icon-arrow-left"/></th>' +
  1492. '<th colspan="5" class="switch"></th>' +
  1493. '<th class="next"><i class="icon-arrow-right"/></th>' +
  1494. '</tr>' +
  1495. '</thead>',
  1496. headTemplateV3: '<thead>' +
  1497. '<tr>' +
  1498. '<th class="prev"><i class="glyphicon glyphicon-arrow-left"></i> </th>' +
  1499. '<th colspan="5" class="switch"></th>' +
  1500. '<th class="next"><i class="glyphicon glyphicon-arrow-right"></i> </th>' +
  1501. '</tr>' +
  1502. '</thead>',
  1503. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1504. footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr></tfoot>'
  1505. };
  1506. DPGlobal.template = '<div class="datetimepicker">' +
  1507. '<div class="datetimepicker-minutes">' +
  1508. '<table class=" table-condensed">' +
  1509. DPGlobal.headTemplate +
  1510. DPGlobal.contTemplate +
  1511. DPGlobal.footTemplate +
  1512. '</table>' +
  1513. '</div>' +
  1514. '<div class="datetimepicker-hours">' +
  1515. '<table class=" table-condensed">' +
  1516. DPGlobal.headTemplate +
  1517. DPGlobal.contTemplate +
  1518. DPGlobal.footTemplate +
  1519. '</table>' +
  1520. '</div>' +
  1521. '<div class="datetimepicker-days">' +
  1522. '<table class=" table-condensed">' +
  1523. DPGlobal.headTemplate +
  1524. '<tbody></tbody>' +
  1525. DPGlobal.footTemplate +
  1526. '</table>' +
  1527. '</div>' +
  1528. '<div class="datetimepicker-months">' +
  1529. '<table class="table-condensed">' +
  1530. DPGlobal.headTemplate +
  1531. DPGlobal.contTemplate +
  1532. DPGlobal.footTemplate +
  1533. '</table>' +
  1534. '</div>' +
  1535. '<div class="datetimepicker-years">' +
  1536. '<table class="table-condensed">' +
  1537. DPGlobal.headTemplate +
  1538. DPGlobal.contTemplate +
  1539. DPGlobal.footTemplate +
  1540. '</table>' +
  1541. '</div>' +
  1542. '</div>';
  1543. DPGlobal.templateV3 = '<div class="datetimepicker">' +
  1544. '<div class="datetimepicker-minutes">' +
  1545. '<table class=" table-condensed">' +
  1546. DPGlobal.headTemplateV3 +
  1547. DPGlobal.contTemplate +
  1548. DPGlobal.footTemplate +
  1549. '</table>' +
  1550. '</div>' +
  1551. '<div class="datetimepicker-hours">' +
  1552. '<table class=" table-condensed">' +
  1553. DPGlobal.headTemplateV3 +
  1554. DPGlobal.contTemplate +
  1555. DPGlobal.footTemplate +
  1556. '</table>' +
  1557. '</div>' +
  1558. '<div class="datetimepicker-days">' +
  1559. '<table class=" table-condensed">' +
  1560. DPGlobal.headTemplateV3 +
  1561. '<tbody></tbody>' +
  1562. DPGlobal.footTemplate +
  1563. '</table>' +
  1564. '</div>' +
  1565. '<div class="datetimepicker-months">' +
  1566. '<table class="table-condensed">' +
  1567. DPGlobal.headTemplateV3 +
  1568. DPGlobal.contTemplate +
  1569. DPGlobal.footTemplate +
  1570. '</table>' +
  1571. '</div>' +
  1572. '<div class="datetimepicker-years">' +
  1573. '<table class="table-condensed">' +
  1574. DPGlobal.headTemplateV3 +
  1575. DPGlobal.contTemplate +
  1576. DPGlobal.footTemplate +
  1577. '</table>' +
  1578. '</div>' +
  1579. '</div>';
  1580. $.fn.datetimepicker.DPGlobal = DPGlobal;
  1581. /* DATETIMEPICKER NO CONFLICT
  1582. * =================== */
  1583. $.fn.datetimepicker.noConflict = function () {
  1584. $.fn.datetimepicker = old;
  1585. return this;
  1586. };
  1587. /* DATETIMEPICKER DATA-API
  1588. * ================== */
  1589. $(document).on(
  1590. 'focus.datetimepicker.data-api click.datetimepicker.data-api',
  1591. '[data-provide="datetimepicker"]',
  1592. function (e) {
  1593. var $this = $(this);
  1594. if ($this.data('datetimepicker')) return;
  1595. e.preventDefault();
  1596. // component click requires us to explicitly show it
  1597. $this.datetimepicker('show');
  1598. }
  1599. );
  1600. $(function () {
  1601. $('[data-provide="datetimepicker-inline"]').datetimepicker();
  1602. });
  1603. }(window.jQuery);