Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bootstrap-datetimepicker.js 54KB

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