jquery.datetimepicker.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. /**
  2. * @preserve jQuery DateTimePicker plugin v2.4.5
  3. * @homepage http://xdsoft.net/jqplugins/datetimepicker/
  4. * (c) 2014, Chupurnov Valeriy.
  5. */
  6. /*global document,window,jQuery,setTimeout,clearTimeout,HighlightedDate,getCurrentValue*/
  7. ;(function (factory) {
  8. if ( typeof define === 'function' && define.amd ) {
  9. // AMD. Register as an anonymous module.
  10. define(['jquery'], factory);
  11. } else if (typeof exports === 'object') {
  12. // Node/CommonJS style for Browserify
  13. module.exports = factory;
  14. } else {
  15. // Browser globals
  16. factory(jQuery);
  17. }
  18. }(function ($) {
  19. 'use strict';
  20. var default_options = {
  21. i18n: {
  22. en: { // English
  23. months: [
  24. "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  25. ],
  26. dayOfWeekShort: [
  27. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  28. ],
  29. dayOfWeek: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  30. },
  31. 'zh-TW': { //Traditional Chinese (繁體中文)
  32. months: [
  33. "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"
  34. ],
  35. dayOfWeekShort: [
  36. "日", "一", "二", "三", "四", "五", "六"
  37. ],
  38. dayOfWeek: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
  39. },
  40. zh: { //Simplified Chinese (简体中文)
  41. months: [
  42. "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"
  43. ],
  44. dayOfWeekShort: [
  45. "日", "一", "二", "三", "四", "五", "六"
  46. ],
  47. dayOfWeek: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
  48. }
  49. },
  50. value: '',
  51. rtl: false,
  52. format: 'Y/m/d H:i',
  53. formatTime: 'H:i',
  54. formatDate: 'Y/m/d',
  55. startDate: false, // new Date(), '1986/12/08', '-1970/01/05','-1970/01/05',
  56. step: 60,
  57. monthChangeSpinner: true,
  58. closeOnDateSelect: false,
  59. closeOnTimeSelect: true,
  60. closeOnWithoutClick: true,
  61. closeOnInputClick: true,
  62. timepicker: true,
  63. datepicker: true,
  64. weeks: false,
  65. defaultTime: false, // use formatTime format (ex. '10:00' for formatTime: 'H:i')
  66. defaultDate: false, // use formatDate format (ex new Date() or '1986/12/08' or '-1970/01/05' or '-1970/01/05')
  67. minDate: false,
  68. maxDate: false,
  69. minTime: false,
  70. maxTime: false,
  71. disabledMinTime: false,
  72. disabledMaxTime: false,
  73. allowTimes: [],
  74. opened: false,
  75. initTime: true,
  76. inline: false,
  77. theme: '',
  78. onSelectDate: function () {},
  79. onSelectTime: function () {},
  80. onChangeMonth: function () {},
  81. onChangeYear: function () {},
  82. onChangeDateTime: function () {},
  83. onShow: function () {},
  84. onClose: function () {},
  85. onGenerate: function () {},
  86. withoutCopyright: true,
  87. inverseButton: false,
  88. hours12: false,
  89. next: 'xdsoft_next',
  90. prev : 'xdsoft_prev',
  91. dayOfWeekStart: 0,
  92. parentID: 'body',
  93. timeHeightInTimePicker: 25,
  94. timepickerScrollbar: true,
  95. todayButton: true,
  96. prevButton: true,
  97. nextButton: true,
  98. defaultSelect: true,
  99. scrollMonth: true,
  100. scrollTime: true,
  101. scrollInput: true,
  102. lazyInit: false,
  103. mask: false,
  104. validateOnBlur: true,
  105. allowBlank: true,
  106. yearStart: 1950,
  107. yearEnd: 2050,
  108. monthStart: 0,
  109. monthEnd: 11,
  110. style: '',
  111. id: '',
  112. fixed: false,
  113. roundTime: 'round', // ceil, floor
  114. className: '',
  115. weekends: [],
  116. highlightedDates: [],
  117. highlightedPeriods: [],
  118. disabledDates : [],
  119. disabledWeekDays: [],
  120. yearOffset: 0,
  121. beforeShowDay: null,
  122. enterLikeTab: true,
  123. showApplyButton: false
  124. };
  125. var globalLocaleDefault = 'zh',
  126. globalLocale = 'zh';
  127. // for locale settings
  128. $.datetimepicker = {
  129. setLocale: function(locale){
  130. globalLocale = default_options.i18n[locale]?locale:globalLocaleDefault;
  131. // Override Parse and Format Library entities
  132. Date.monthNames = default_options.i18n[globalLocale].months;
  133. Date.dayNames = default_options.i18n[globalLocale].dayOfWeek;
  134. }
  135. };
  136. // fix for ie8
  137. if (!window.getComputedStyle) {
  138. window.getComputedStyle = function (el, pseudo) {
  139. this.el = el;
  140. this.getPropertyValue = function (prop) {
  141. var re = /(\-([a-z]){1})/g;
  142. if (prop === 'float') {
  143. prop = 'styleFloat';
  144. }
  145. if (re.test(prop)) {
  146. prop = prop.replace(re, function (a, b, c) {
  147. return c.toUpperCase();
  148. });
  149. }
  150. return el.currentStyle[prop] || null;
  151. };
  152. return this;
  153. };
  154. }
  155. if (!Array.prototype.indexOf) {
  156. Array.prototype.indexOf = function (obj, start) {
  157. var i, j;
  158. for (i = (start || 0), j = this.length; i < j; i += 1) {
  159. if (this[i] === obj) { return i; }
  160. }
  161. return -1;
  162. };
  163. }
  164. Date.prototype.countDaysInMonth = function () {
  165. return new Date(this.getFullYear(), this.getMonth() + 1, 0).getDate();
  166. };
  167. $.fn.xdsoftScroller = function (percent) {
  168. return this.each(function () {
  169. var timeboxparent = $(this),
  170. pointerEventToXY = function (e) {
  171. var out = {x: 0, y: 0},
  172. touch;
  173. if (e.type === 'touchstart' || e.type === 'touchmove' || e.type === 'touchend' || e.type === 'touchcancel') {
  174. touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
  175. out.x = touch.clientX;
  176. out.y = touch.clientY;
  177. } else if (e.type === 'mousedown' || e.type === 'mouseup' || e.type === 'mousemove' || e.type === 'mouseover' || e.type === 'mouseout' || e.type === 'mouseenter' || e.type === 'mouseleave') {
  178. out.x = e.clientX;
  179. out.y = e.clientY;
  180. }
  181. return out;
  182. },
  183. move = 0,
  184. timebox,
  185. parentHeight,
  186. height,
  187. scrollbar,
  188. scroller,
  189. maximumOffset = 100,
  190. start = false,
  191. startY = 0,
  192. startTop = 0,
  193. h1 = 0,
  194. touchStart = false,
  195. startTopScroll = 0,
  196. calcOffset = function () {};
  197. if (percent === 'hide') {
  198. timeboxparent.find('.xdsoft_scrollbar').hide();
  199. return;
  200. }
  201. if (!$(this).hasClass('xdsoft_scroller_box')) {
  202. timebox = timeboxparent.children().eq(0);
  203. parentHeight = timeboxparent[0].clientHeight;
  204. height = timebox[0].offsetHeight;
  205. scrollbar = $('<div class="xdsoft_scrollbar"></div>');
  206. scroller = $('<div class="xdsoft_scroller"></div>');
  207. scrollbar.append(scroller);
  208. timeboxparent.addClass('xdsoft_scroller_box').append(scrollbar);
  209. calcOffset = function calcOffset(event) {
  210. var offset = pointerEventToXY(event).y - startY + startTopScroll;
  211. if (offset < 0) {
  212. offset = 0;
  213. }
  214. if (offset + scroller[0].offsetHeight > h1) {
  215. offset = h1 - scroller[0].offsetHeight;
  216. }
  217. timeboxparent.trigger('scroll_element.xdsoft_scroller', [maximumOffset ? offset / maximumOffset : 0]);
  218. };
  219. scroller
  220. .on('touchstart.xdsoft_scroller mousedown.xdsoft_scroller', function (event) {
  221. if (!parentHeight) {
  222. timeboxparent.trigger('resize_scroll.xdsoft_scroller', [percent]);
  223. }
  224. startY = pointerEventToXY(event).y;
  225. startTopScroll = parseInt(scroller.css('margin-top'), 10);
  226. h1 = scrollbar[0].offsetHeight;
  227. if (event.type === 'mousedown') {
  228. if (document) {
  229. $(document.body).addClass('xdsoft_noselect');
  230. }
  231. $([document.body, window]).on('mouseup.xdsoft_scroller', function arguments_callee() {
  232. $([document.body, window]).off('mouseup.xdsoft_scroller', arguments_callee)
  233. .off('mousemove.xdsoft_scroller', calcOffset)
  234. .removeClass('xdsoft_noselect');
  235. });
  236. $(document.body).on('mousemove.xdsoft_scroller', calcOffset);
  237. } else {
  238. touchStart = true;
  239. event.stopPropagation();
  240. event.preventDefault();
  241. }
  242. })
  243. .on('touchmove', function (event) {
  244. if (touchStart) {
  245. event.preventDefault();
  246. calcOffset(event);
  247. }
  248. })
  249. .on('touchend touchcancel', function (event) {
  250. touchStart = false;
  251. startTopScroll = 0;
  252. });
  253. timeboxparent
  254. .on('scroll_element.xdsoft_scroller', function (event, percentage) {
  255. if (!parentHeight) {
  256. timeboxparent.trigger('resize_scroll.xdsoft_scroller', [percentage, true]);
  257. }
  258. percentage = percentage > 1 ? 1 : (percentage < 0 || isNaN(percentage)) ? 0 : percentage;
  259. scroller.css('margin-top', maximumOffset * percentage);
  260. setTimeout(function () {
  261. timebox.css('marginTop', -parseInt((timebox[0].offsetHeight - parentHeight) * percentage, 10));
  262. }, 10);
  263. })
  264. .on('resize_scroll.xdsoft_scroller', function (event, percentage, noTriggerScroll) {
  265. var percent, sh;
  266. parentHeight = timeboxparent[0].clientHeight;
  267. height = timebox[0].offsetHeight;
  268. percent = parentHeight / height;
  269. sh = percent * scrollbar[0].offsetHeight;
  270. if (percent > 1) {
  271. scroller.hide();
  272. } else {
  273. scroller.show();
  274. scroller.css('height', parseInt(sh > 10 ? sh : 10, 10));
  275. maximumOffset = scrollbar[0].offsetHeight - scroller[0].offsetHeight;
  276. if (noTriggerScroll !== true) {
  277. timeboxparent.trigger('scroll_element.xdsoft_scroller', [percentage || Math.abs(parseInt(timebox.css('marginTop'), 10)) / (height - parentHeight)]);
  278. }
  279. }
  280. });
  281. timeboxparent.on('mousewheel', function (event) {
  282. var top = Math.abs(parseInt(timebox.css('marginTop'), 10));
  283. top = top - (event.deltaY * 20);
  284. if (top < 0) {
  285. top = 0;
  286. }
  287. timeboxparent.trigger('scroll_element.xdsoft_scroller', [top / (height - parentHeight)]);
  288. event.stopPropagation();
  289. return false;
  290. });
  291. timeboxparent.on('touchstart', function (event) {
  292. start = pointerEventToXY(event);
  293. startTop = Math.abs(parseInt(timebox.css('marginTop'), 10));
  294. });
  295. timeboxparent.on('touchmove', function (event) {
  296. if (start) {
  297. event.preventDefault();
  298. var coord = pointerEventToXY(event);
  299. timeboxparent.trigger('scroll_element.xdsoft_scroller', [(startTop - (coord.y - start.y)) / (height - parentHeight)]);
  300. }
  301. });
  302. timeboxparent.on('touchend touchcancel', function (event) {
  303. start = false;
  304. startTop = 0;
  305. });
  306. }
  307. timeboxparent.trigger('resize_scroll.xdsoft_scroller', [percent]);
  308. });
  309. };
  310. $.fn.datetimepicker = function (opt) {
  311. var KEY0 = 48,
  312. KEY9 = 57,
  313. _KEY0 = 96,
  314. _KEY9 = 105,
  315. CTRLKEY = 17,
  316. DEL = 46,
  317. ENTER = 13,
  318. ESC = 27,
  319. BACKSPACE = 8,
  320. ARROWLEFT = 37,
  321. ARROWUP = 38,
  322. ARROWRIGHT = 39,
  323. ARROWDOWN = 40,
  324. TAB = 9,
  325. F5 = 116,
  326. AKEY = 65,
  327. CKEY = 67,
  328. VKEY = 86,
  329. ZKEY = 90,
  330. YKEY = 89,
  331. ctrlDown = false,
  332. options = ($.isPlainObject(opt) || !opt) ? $.extend(true, {}, default_options, opt) : $.extend(true, {}, default_options),
  333. lazyInitTimer = 0,
  334. createDateTimePicker,
  335. destroyDateTimePicker,
  336. lazyInit = function (input) {
  337. input
  338. .on('open.xdsoft focusin.xdsoft mousedown.xdsoft', function initOnActionCallback(event) {
  339. if (input.is(':disabled') || input.data('xdsoft_datetimepicker')) {
  340. return;
  341. }
  342. clearTimeout(lazyInitTimer);
  343. lazyInitTimer = setTimeout(function () {
  344. if (!input.data('xdsoft_datetimepicker')) {
  345. createDateTimePicker(input);
  346. }
  347. input
  348. .off('open.xdsoft focusin.xdsoft mousedown.xdsoft', initOnActionCallback)
  349. .trigger('open.xdsoft');
  350. }, 100);
  351. });
  352. };
  353. createDateTimePicker = function (input) {
  354. var datetimepicker = $('<div class="xdsoft_datetimepicker xdsoft_noselect"></div>'),
  355. xdsoft_copyright = $('<div class="xdsoft_copyright"><a target="_blank" href="http://xdsoft.net/jqplugins/datetimepicker/">xdsoft.net</a></div>'),
  356. datepicker = $('<div class="xdsoft_datepicker active"></div>'),
  357. mounth_picker = $('<div class="xdsoft_mounthpicker"><button type="button" class="xdsoft_prev"></button><button type="button" class="xdsoft_today_button"></button>' +
  358. '<div class="xdsoft_label xdsoft_month"><span></span><i></i></div>' +
  359. '<div class="xdsoft_label xdsoft_year"><span></span><i></i></div>' +
  360. '<button type="button" class="xdsoft_next"></button></div>'),
  361. calendar = $('<div class="xdsoft_calendar"></div>'),
  362. timepicker = $('<div class="xdsoft_timepicker active"><button type="button" class="xdsoft_prev"></button><div class="xdsoft_time_box"></div><button type="button" class="xdsoft_next"></button></div>'),
  363. timeboxparent = timepicker.find('.xdsoft_time_box').eq(0),
  364. timebox = $('<div class="xdsoft_time_variant"></div>'),
  365. applyButton = $('<button type="button" class="xdsoft_save_selected blue-gradient-button">Save Selected</button>'),
  366. /*scrollbar = $('<div class="xdsoft_scrollbar"></div>'),
  367. scroller = $('<div class="xdsoft_scroller"></div>'),*/
  368. monthselect = $('<div class="xdsoft_select xdsoft_monthselect"><div></div></div>'),
  369. yearselect = $('<div class="xdsoft_select xdsoft_yearselect"><div></div></div>'),
  370. triggerAfterOpen = false,
  371. XDSoft_datetime,
  372. //scroll_element,
  373. xchangeTimer,
  374. timerclick,
  375. current_time_index,
  376. setPos,
  377. timer = 0,
  378. timer1 = 0,
  379. _xdsoft_datetime;
  380. if (options.id) {
  381. datetimepicker.attr('id', options.id);
  382. }
  383. if (options.style) {
  384. datetimepicker.attr('style', options.style);
  385. }
  386. if (options.weeks) {
  387. datetimepicker.addClass('xdsoft_showweeks');
  388. }
  389. if (options.rtl) {
  390. datetimepicker.addClass('xdsoft_rtl');
  391. }
  392. datetimepicker.addClass('xdsoft_' + options.theme);
  393. datetimepicker.addClass(options.className);
  394. mounth_picker
  395. .find('.xdsoft_month span')
  396. .after(monthselect);
  397. mounth_picker
  398. .find('.xdsoft_year span')
  399. .after(yearselect);
  400. mounth_picker
  401. .find('.xdsoft_month,.xdsoft_year')
  402. .on('mousedown.xdsoft', function (event) {
  403. var select = $(this).find('.xdsoft_select').eq(0),
  404. val = 0,
  405. top = 0,
  406. visible = select.is(':visible'),
  407. items,
  408. i;
  409. mounth_picker
  410. .find('.xdsoft_select')
  411. .hide();
  412. if (_xdsoft_datetime.currentTime) {
  413. val = _xdsoft_datetime.currentTime[$(this).hasClass('xdsoft_month') ? 'getMonth' : 'getFullYear']();
  414. }
  415. select[visible ? 'hide' : 'show']();
  416. for (items = select.find('div.xdsoft_option'), i = 0; i < items.length; i += 1) {
  417. if (items.eq(i).data('value') === val) {
  418. break;
  419. } else {
  420. top += items[0].offsetHeight;
  421. }
  422. }
  423. select.xdsoftScroller(top / (select.children()[0].offsetHeight - (select[0].clientHeight)));
  424. event.stopPropagation();
  425. return false;
  426. });
  427. mounth_picker
  428. .find('.xdsoft_select')
  429. .xdsoftScroller()
  430. .on('mousedown.xdsoft', function (event) {
  431. event.stopPropagation();
  432. event.preventDefault();
  433. })
  434. .on('mousedown.xdsoft', '.xdsoft_option', function (event) {
  435. if (_xdsoft_datetime.currentTime === undefined || _xdsoft_datetime.currentTime === null) {
  436. _xdsoft_datetime.currentTime = _xdsoft_datetime.now();
  437. }
  438. var year = _xdsoft_datetime.currentTime.getFullYear();
  439. if (_xdsoft_datetime && _xdsoft_datetime.currentTime) {
  440. _xdsoft_datetime.currentTime[$(this).parent().parent().hasClass('xdsoft_monthselect') ? 'setMonth' : 'setFullYear']($(this).data('value'));
  441. }
  442. $(this).parent().parent().hide();
  443. datetimepicker.trigger('xchange.xdsoft');
  444. if (options.onChangeMonth && $.isFunction(options.onChangeMonth)) {
  445. options.onChangeMonth.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  446. }
  447. if (year !== _xdsoft_datetime.currentTime.getFullYear() && $.isFunction(options.onChangeYear)) {
  448. options.onChangeYear.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  449. }
  450. });
  451. datetimepicker.setOptions = function (_options) {
  452. var highlightedDates = {},
  453. getCaretPos = function (input) {
  454. try {
  455. if (document.selection && document.selection.createRange) {
  456. var range = document.selection.createRange();
  457. return range.getBookmark().charCodeAt(2) - 2;
  458. }
  459. if (input.setSelectionRange) {
  460. return input.selectionStart;
  461. }
  462. } catch (e) {
  463. return 0;
  464. }
  465. },
  466. setCaretPos = function (node, pos) {
  467. node = (typeof node === "string" || node instanceof String) ? document.getElementById(node) : node;
  468. if (!node) {
  469. return false;
  470. }
  471. if (node.createTextRange) {
  472. var textRange = node.createTextRange();
  473. textRange.collapse(true);
  474. textRange.moveEnd('character', pos);
  475. textRange.moveStart('character', pos);
  476. textRange.select();
  477. return true;
  478. }
  479. if (node.setSelectionRange) {
  480. node.setSelectionRange(pos, pos);
  481. return true;
  482. }
  483. return false;
  484. },
  485. isValidValue = function (mask, value) {
  486. var reg = mask
  487. .replace(/([\[\]\/\{\}\(\)\-\.\+]{1})/g, '\\$1')
  488. .replace(/_/g, '{digit+}')
  489. .replace(/([0-9]{1})/g, '{digit$1}')
  490. .replace(/\{digit([0-9]{1})\}/g, '[0-$1_]{1}')
  491. .replace(/\{digit[\+]\}/g, '[0-9_]{1}');
  492. return (new RegExp(reg)).test(value);
  493. };
  494. options = $.extend(true, {}, options, _options);
  495. if (_options.allowTimes && $.isArray(_options.allowTimes) && _options.allowTimes.length) {
  496. options.allowTimes = $.extend(true, [], _options.allowTimes);
  497. }
  498. if (_options.weekends && $.isArray(_options.weekends) && _options.weekends.length) {
  499. options.weekends = $.extend(true, [], _options.weekends);
  500. }
  501. if (_options.highlightedDates && $.isArray(_options.highlightedDates) && _options.highlightedDates.length) {
  502. $.each(_options.highlightedDates, function (index, value) {
  503. var splitData = $.map(value.split(','), $.trim),
  504. exDesc,
  505. hDate = new HighlightedDate(Date.parseDate(splitData[0], options.formatDate), splitData[1], splitData[2]), // date, desc, style
  506. keyDate = hDate.date.dateFormat(options.formatDate);
  507. if (highlightedDates[keyDate] !== undefined) {
  508. exDesc = highlightedDates[keyDate].desc;
  509. if (exDesc && exDesc.length && hDate.desc && hDate.desc.length) {
  510. highlightedDates[keyDate].desc = exDesc + "\n" + hDate.desc;
  511. }
  512. } else {
  513. highlightedDates[keyDate] = hDate;
  514. }
  515. });
  516. options.highlightedDates = $.extend(true, [], highlightedDates);
  517. }
  518. if (_options.highlightedPeriods && $.isArray(_options.highlightedPeriods) && _options.highlightedPeriods.length) {
  519. highlightedDates = $.extend(true, [], options.highlightedDates);
  520. $.each(_options.highlightedPeriods, function (index, value) {
  521. var dateTest, // start date
  522. dateEnd,
  523. desc,
  524. hDate,
  525. keyDate,
  526. exDesc,
  527. style;
  528. if ($.isArray(value)) {
  529. dateTest = value[0];
  530. dateEnd = value[1];
  531. desc = value[2];
  532. style = value[3];
  533. }
  534. else {
  535. var splitData = $.map(value.split(','), $.trim);
  536. dateTest = Date.parseDate(splitData[0], options.formatDate);
  537. dateEnd = Date.parseDate(splitData[1], options.formatDate);
  538. desc = splitData[2];
  539. style = splitData[3];
  540. }
  541. while (dateTest <= dateEnd) {
  542. hDate = new HighlightedDate(dateTest, desc, style);
  543. keyDate = dateTest.dateFormat(options.formatDate);
  544. dateTest.setDate(dateTest.getDate() + 1);
  545. if (highlightedDates[keyDate] !== undefined) {
  546. exDesc = highlightedDates[keyDate].desc;
  547. if (exDesc && exDesc.length && hDate.desc && hDate.desc.length) {
  548. highlightedDates[keyDate].desc = exDesc + "\n" + hDate.desc;
  549. }
  550. } else {
  551. highlightedDates[keyDate] = hDate;
  552. }
  553. }
  554. });
  555. options.highlightedDates = $.extend(true, [], highlightedDates);
  556. }
  557. if (_options.disabledDates && $.isArray(_options.disabledDates) && _options.disabledDates.length) {
  558. options.disabledDates = $.extend(true, [], _options.disabledDates);
  559. }
  560. if (_options.disabledWeekDays && $.isArray(_options.disabledWeekDays) && _options.disabledWeekDays.length) {
  561. options.disabledWeekDays = $.extend(true, [], _options.disabledWeekDays);
  562. }
  563. if ((options.open || options.opened) && (!options.inline)) {
  564. input.trigger('open.xdsoft');
  565. }
  566. if (options.inline) {
  567. triggerAfterOpen = true;
  568. datetimepicker.addClass('xdsoft_inline');
  569. input.after(datetimepicker).hide();
  570. }
  571. if (options.inverseButton) {
  572. options.next = 'xdsoft_prev';
  573. options.prev = 'xdsoft_next';
  574. }
  575. if (options.datepicker) {
  576. datepicker.addClass('active');
  577. } else {
  578. datepicker.removeClass('active');
  579. }
  580. if (options.timepicker) {
  581. timepicker.addClass('active');
  582. } else {
  583. timepicker.removeClass('active');
  584. }
  585. if (options.value) {
  586. _xdsoft_datetime.setCurrentTime(options.value);
  587. if (input && input.val) {
  588. input.val(_xdsoft_datetime.str);
  589. }
  590. }
  591. if (isNaN(options.dayOfWeekStart)) {
  592. options.dayOfWeekStart = 0;
  593. } else {
  594. options.dayOfWeekStart = parseInt(options.dayOfWeekStart, 10) % 7;
  595. }
  596. if (!options.timepickerScrollbar) {
  597. timeboxparent.xdsoftScroller('hide');
  598. }
  599. if (options.minDate && /^[\+\-](.*)$/.test(options.minDate)) {
  600. options.minDate = _xdsoft_datetime.strToDateTime(options.minDate).dateFormat(options.formatDate);
  601. }
  602. if (options.maxDate && /^[\+\-](.*)$/.test(options.maxDate)) {
  603. options.maxDate = _xdsoft_datetime.strToDateTime(options.maxDate).dateFormat(options.formatDate);
  604. }
  605. applyButton.toggle(options.showApplyButton);
  606. mounth_picker
  607. .find('.xdsoft_today_button')
  608. .css('visibility', !options.todayButton ? 'hidden' : 'visible');
  609. mounth_picker
  610. .find('.' + options.prev)
  611. .css('visibility', !options.prevButton ? 'hidden' : 'visible');
  612. mounth_picker
  613. .find('.' + options.next)
  614. .css('visibility', !options.nextButton ? 'hidden' : 'visible');
  615. if (options.mask) {
  616. input.off('keydown.xdsoft');
  617. if (options.mask === true) {
  618. options.mask = options.format
  619. .replace(/Y/g, '9999')
  620. .replace(/F/g, '9999')
  621. .replace(/m/g, '19')
  622. .replace(/d/g, '39')
  623. .replace(/H/g, '29')
  624. .replace(/i/g, '59')
  625. .replace(/s/g, '59');
  626. }
  627. if ($.type(options.mask) === 'string') {
  628. if (!isValidValue(options.mask, input.val())) {
  629. input.val(options.mask.replace(/[0-9]/g, '_'));
  630. }
  631. input.on('keydown.xdsoft', function (event) {
  632. var val = this.value,
  633. key = event.which,
  634. pos,
  635. digit;
  636. if (((key >= KEY0 && key <= KEY9) || (key >= _KEY0 && key <= _KEY9)) || (key === BACKSPACE || key === DEL)) {
  637. pos = getCaretPos(this);
  638. digit = (key !== BACKSPACE && key !== DEL) ? String.fromCharCode((_KEY0 <= key && key <= _KEY9) ? key - KEY0 : key) : '_';
  639. if ((key === BACKSPACE || key === DEL) && pos) {
  640. pos -= 1;
  641. digit = '_';
  642. }
  643. while (/[^0-9_]/.test(options.mask.substr(pos, 1)) && pos < options.mask.length && pos > 0) {
  644. pos += (key === BACKSPACE || key === DEL) ? -1 : 1;
  645. }
  646. val = val.substr(0, pos) + digit + val.substr(pos + 1);
  647. if ($.trim(val) === '') {
  648. val = options.mask.replace(/[0-9]/g, '_');
  649. } else {
  650. if (pos === options.mask.length) {
  651. event.preventDefault();
  652. return false;
  653. }
  654. }
  655. pos += (key === BACKSPACE || key === DEL) ? 0 : 1;
  656. while (/[^0-9_]/.test(options.mask.substr(pos, 1)) && pos < options.mask.length && pos > 0) {
  657. pos += (key === BACKSPACE || key === DEL) ? -1 : 1;
  658. }
  659. if (isValidValue(options.mask, val)) {
  660. this.value = val;
  661. setCaretPos(this, pos);
  662. } else if ($.trim(val) === '') {
  663. this.value = options.mask.replace(/[0-9]/g, '_');
  664. } else {
  665. input.trigger('error_input.xdsoft');
  666. }
  667. } else {
  668. if (([AKEY, CKEY, VKEY, ZKEY, YKEY].indexOf(key) !== -1 && ctrlDown) || [ESC, ARROWUP, ARROWDOWN, ARROWLEFT, ARROWRIGHT, F5, CTRLKEY, TAB, ENTER].indexOf(key) !== -1) {
  669. return true;
  670. }
  671. }
  672. event.preventDefault();
  673. return false;
  674. });
  675. }
  676. }
  677. if (options.validateOnBlur) {
  678. input
  679. .off('blur.xdsoft')
  680. .on('blur.xdsoft', function () {
  681. if (options.allowBlank && !$.trim($(this).val()).length) {
  682. $(this).val(null);
  683. datetimepicker.data('xdsoft_datetime').empty();
  684. } else if (!Date.parseDate($(this).val(), options.format)) {
  685. var splittedHours = +([$(this).val()[0], $(this).val()[1]].join('')),
  686. splittedMinutes = +([$(this).val()[2], $(this).val()[3]].join(''));
  687. // parse the numbers as 0312 => 03:12
  688. if (!options.datepicker && options.timepicker && splittedHours >= 0 && splittedHours < 24 && splittedMinutes >= 0 && splittedMinutes < 60) {
  689. $(this).val([splittedHours, splittedMinutes].map(function (item) {
  690. return item > 9 ? item : '0' + item;
  691. }).join(':'));
  692. } else {
  693. $(this).val((_xdsoft_datetime.now()).dateFormat(options.format));
  694. }
  695. datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val());
  696. } else {
  697. datetimepicker.data('xdsoft_datetime').setCurrentTime($(this).val());
  698. }
  699. datetimepicker.trigger('changedatetime.xdsoft');
  700. });
  701. }
  702. options.dayOfWeekStartPrev = (options.dayOfWeekStart === 0) ? 6 : options.dayOfWeekStart - 1;
  703. datetimepicker
  704. .trigger('xchange.xdsoft')
  705. .trigger('afterOpen.xdsoft');
  706. };
  707. datetimepicker
  708. .data('options', options)
  709. .on('mousedown.xdsoft', function (event) {
  710. event.stopPropagation();
  711. event.preventDefault();
  712. yearselect.hide();
  713. monthselect.hide();
  714. return false;
  715. });
  716. //scroll_element = timepicker.find('.xdsoft_time_box');
  717. timeboxparent.append(timebox);
  718. timeboxparent.xdsoftScroller();
  719. datetimepicker.on('afterOpen.xdsoft', function () {
  720. timeboxparent.xdsoftScroller();
  721. });
  722. datetimepicker
  723. .append(datepicker)
  724. .append(timepicker);
  725. if (options.withoutCopyright !== true) {
  726. datetimepicker
  727. .append(xdsoft_copyright);
  728. }
  729. datepicker
  730. .append(mounth_picker)
  731. .append(calendar)
  732. .append(applyButton);
  733. $(options.parentID)
  734. .append(datetimepicker);
  735. XDSoft_datetime = function () {
  736. var _this = this;
  737. _this.now = function (norecursion) {
  738. var d = new Date(),
  739. date,
  740. time;
  741. if (!norecursion && options.defaultDate) {
  742. date = _this.strToDateTime(options.defaultDate);
  743. d.setFullYear(date.getFullYear());
  744. d.setMonth(date.getMonth());
  745. d.setDate(date.getDate());
  746. }
  747. if (options.yearOffset) {
  748. d.setFullYear(d.getFullYear() + options.yearOffset);
  749. }
  750. if (!norecursion && options.defaultTime) {
  751. time = _this.strtotime(options.defaultTime);
  752. d.setHours(time.getHours());
  753. d.setMinutes(time.getMinutes());
  754. }
  755. return d;
  756. };
  757. _this.isValidDate = function (d) {
  758. if (Object.prototype.toString.call(d) !== "[object Date]") {
  759. return false;
  760. }
  761. return !isNaN(d.getTime());
  762. };
  763. _this.setCurrentTime = function (dTime) {
  764. _this.currentTime = (typeof dTime === 'string') ? _this.strToDateTime(dTime) : _this.isValidDate(dTime) ? dTime : _this.now();
  765. datetimepicker.trigger('xchange.xdsoft');
  766. };
  767. _this.empty = function () {
  768. _this.currentTime = null;
  769. };
  770. _this.getCurrentTime = function (dTime) {
  771. return _this.currentTime;
  772. };
  773. _this.nextMonth = function () {
  774. if (_this.currentTime === undefined || _this.currentTime === null) {
  775. _this.currentTime = _this.now();
  776. }
  777. var month = _this.currentTime.getMonth() + 1,
  778. year;
  779. if (month === 12) {
  780. _this.currentTime.setFullYear(_this.currentTime.getFullYear() + 1);
  781. month = 0;
  782. }
  783. year = _this.currentTime.getFullYear();
  784. _this.currentTime.setDate(
  785. Math.min(
  786. new Date(_this.currentTime.getFullYear(), month + 1, 0).getDate(),
  787. _this.currentTime.getDate()
  788. )
  789. );
  790. _this.currentTime.setMonth(month);
  791. if (options.onChangeMonth && $.isFunction(options.onChangeMonth)) {
  792. options.onChangeMonth.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  793. }
  794. if (year !== _this.currentTime.getFullYear() && $.isFunction(options.onChangeYear)) {
  795. options.onChangeYear.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  796. }
  797. datetimepicker.trigger('xchange.xdsoft');
  798. return month;
  799. };
  800. _this.prevMonth = function () {
  801. if (_this.currentTime === undefined || _this.currentTime === null) {
  802. _this.currentTime = _this.now();
  803. }
  804. var month = _this.currentTime.getMonth() - 1;
  805. if (month === -1) {
  806. _this.currentTime.setFullYear(_this.currentTime.getFullYear() - 1);
  807. month = 11;
  808. }
  809. _this.currentTime.setDate(
  810. Math.min(
  811. new Date(_this.currentTime.getFullYear(), month + 1, 0).getDate(),
  812. _this.currentTime.getDate()
  813. )
  814. );
  815. _this.currentTime.setMonth(month);
  816. if (options.onChangeMonth && $.isFunction(options.onChangeMonth)) {
  817. options.onChangeMonth.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  818. }
  819. datetimepicker.trigger('xchange.xdsoft');
  820. return month;
  821. };
  822. _this.getWeekOfYear = function (datetime) {
  823. var onejan = new Date(datetime.getFullYear(), 0, 1);
  824. return Math.ceil((((datetime - onejan) / 86400000) + onejan.getDay() + 1) / 7);
  825. };
  826. _this.strToDateTime = function (sDateTime) {
  827. var tmpDate = [], timeOffset, currentTime;
  828. if (sDateTime && sDateTime instanceof Date && _this.isValidDate(sDateTime)) {
  829. return sDateTime;
  830. }
  831. tmpDate = /^(\+|\-)(.*)$/.exec(sDateTime);
  832. if (tmpDate) {
  833. tmpDate[2] = Date.parseDate(tmpDate[2], options.formatDate);
  834. }
  835. if (tmpDate && tmpDate[2]) {
  836. timeOffset = tmpDate[2].getTime() - (tmpDate[2].getTimezoneOffset()) * 60000;
  837. currentTime = new Date((_this.now(true)).getTime() + parseInt(tmpDate[1] + '1', 10) * timeOffset);
  838. } else {
  839. currentTime = sDateTime ? Date.parseDate(sDateTime, options.format) : _this.now();
  840. }
  841. if (!_this.isValidDate(currentTime)) {
  842. currentTime = _this.now();
  843. }
  844. return currentTime;
  845. };
  846. _this.strToDate = function (sDate) {
  847. if (sDate && sDate instanceof Date && _this.isValidDate(sDate)) {
  848. return sDate;
  849. }
  850. var currentTime = sDate ? Date.parseDate(sDate, options.formatDate) : _this.now(true);
  851. if (!_this.isValidDate(currentTime)) {
  852. currentTime = _this.now(true);
  853. }
  854. return currentTime;
  855. };
  856. _this.strtotime = function (sTime) {
  857. if (sTime && sTime instanceof Date && _this.isValidDate(sTime)) {
  858. return sTime;
  859. }
  860. var currentTime = sTime ? Date.parseDate(sTime, options.formatTime) : _this.now(true);
  861. if (!_this.isValidDate(currentTime)) {
  862. currentTime = _this.now(true);
  863. }
  864. return currentTime;
  865. };
  866. _this.str = function () {
  867. return _this.currentTime.dateFormat(options.format);
  868. };
  869. _this.currentTime = this.now();
  870. };
  871. _xdsoft_datetime = new XDSoft_datetime();
  872. applyButton.on('click', function (e) {//pathbrite
  873. e.preventDefault();
  874. datetimepicker.data('changed', true);
  875. _xdsoft_datetime.setCurrentTime(getCurrentValue());
  876. input.val(_xdsoft_datetime.str());
  877. datetimepicker.trigger('close.xdsoft');
  878. });
  879. mounth_picker
  880. .find('.xdsoft_today_button')
  881. .on('mousedown.xdsoft', function () {
  882. datetimepicker.data('changed', true);
  883. _xdsoft_datetime.setCurrentTime(0);
  884. datetimepicker.trigger('afterOpen.xdsoft');
  885. }).on('dblclick.xdsoft', function () {
  886. var currentDate = _xdsoft_datetime.getCurrentTime(), minDate, maxDate;
  887. currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
  888. minDate = _xdsoft_datetime.strToDate(options.minDate);
  889. minDate = new Date(minDate.getFullYear(), minDate.getMonth(), minDate.getDate());
  890. if (currentDate < minDate) {
  891. return;
  892. }
  893. maxDate = _xdsoft_datetime.strToDate(options.maxDate);
  894. maxDate = new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate());
  895. if (currentDate > maxDate) {
  896. return;
  897. }
  898. input.val(_xdsoft_datetime.str());
  899. input.trigger('change');
  900. datetimepicker.trigger('close.xdsoft');
  901. });
  902. mounth_picker
  903. .find('.xdsoft_prev,.xdsoft_next')
  904. .on('mousedown.xdsoft', function () {
  905. var $this = $(this),
  906. timer = 0,
  907. stop = false;
  908. (function arguments_callee1(v) {
  909. if ($this.hasClass(options.next)) {
  910. _xdsoft_datetime.nextMonth();
  911. } else if ($this.hasClass(options.prev)) {
  912. _xdsoft_datetime.prevMonth();
  913. }
  914. if (options.monthChangeSpinner) {
  915. if (!stop) {
  916. timer = setTimeout(arguments_callee1, v || 100);
  917. }
  918. }
  919. }(500));
  920. $([document.body, window]).on('mouseup.xdsoft', function arguments_callee2() {
  921. clearTimeout(timer);
  922. stop = true;
  923. $([document.body, window]).off('mouseup.xdsoft', arguments_callee2);
  924. });
  925. });
  926. timepicker
  927. .find('.xdsoft_prev,.xdsoft_next')
  928. .on('mousedown.xdsoft', function () {
  929. var $this = $(this),
  930. timer = 0,
  931. stop = false,
  932. period = 110;
  933. (function arguments_callee4(v) {
  934. var pheight = timeboxparent[0].clientHeight,
  935. height = timebox[0].offsetHeight,
  936. top = Math.abs(parseInt(timebox.css('marginTop'), 10));
  937. if ($this.hasClass(options.next) && (height - pheight) - options.timeHeightInTimePicker >= top) {
  938. timebox.css('marginTop', '-' + (top + options.timeHeightInTimePicker) + 'px');
  939. } else if ($this.hasClass(options.prev) && top - options.timeHeightInTimePicker >= 0) {
  940. timebox.css('marginTop', '-' + (top - options.timeHeightInTimePicker) + 'px');
  941. }
  942. timeboxparent.trigger('scroll_element.xdsoft_scroller', [Math.abs(parseInt(timebox.css('marginTop'), 10) / (height - pheight))]);
  943. period = (period > 10) ? 10 : period - 10;
  944. if (!stop) {
  945. timer = setTimeout(arguments_callee4, v || period);
  946. }
  947. }(500));
  948. $([document.body, window]).on('mouseup.xdsoft', function arguments_callee5() {
  949. clearTimeout(timer);
  950. stop = true;
  951. $([document.body, window])
  952. .off('mouseup.xdsoft', arguments_callee5);
  953. });
  954. });
  955. xchangeTimer = 0;
  956. // base handler - generating a calendar and timepicker
  957. datetimepicker
  958. .on('xchange.xdsoft', function (event) {
  959. clearTimeout(xchangeTimer);
  960. xchangeTimer = setTimeout(function () {
  961. if (_xdsoft_datetime.currentTime === undefined || _xdsoft_datetime.currentTime === null) {
  962. _xdsoft_datetime.currentTime = _xdsoft_datetime.now();
  963. }
  964. var table = '',
  965. start = new Date(_xdsoft_datetime.currentTime.getFullYear(), _xdsoft_datetime.currentTime.getMonth(), 1, 12, 0, 0),
  966. i = 0,
  967. j,
  968. today = _xdsoft_datetime.now(),
  969. maxDate = false,
  970. minDate = false,
  971. hDate,
  972. day,
  973. d,
  974. y,
  975. m,
  976. w,
  977. classes = [],
  978. customDateSettings,
  979. newRow = true,
  980. time = '',
  981. h = '',
  982. line_time,
  983. description;
  984. while (start.getDay() !== options.dayOfWeekStart) {
  985. start.setDate(start.getDate() - 1);
  986. }
  987. table += '<table><thead><tr>';
  988. if (options.weeks) {
  989. table += '<th></th>';
  990. }
  991. for (j = 0; j < 7; j += 1) {
  992. table += '<th>' + options.i18n[globalLocale].dayOfWeekShort[(j + options.dayOfWeekStart) % 7] + '</th>';
  993. }
  994. table += '</tr></thead>';
  995. table += '<tbody>';
  996. if (options.maxDate !== false) {
  997. maxDate = _xdsoft_datetime.strToDate(options.maxDate);
  998. maxDate = new Date(maxDate.getFullYear(), maxDate.getMonth(), maxDate.getDate(), 23, 59, 59, 999);
  999. }
  1000. if (options.minDate !== false) {
  1001. minDate = _xdsoft_datetime.strToDate(options.minDate);
  1002. minDate = new Date(minDate.getFullYear(), minDate.getMonth(), minDate.getDate());
  1003. }
  1004. while (i < _xdsoft_datetime.currentTime.countDaysInMonth() || start.getDay() !== options.dayOfWeekStart || _xdsoft_datetime.currentTime.getMonth() === start.getMonth()) {
  1005. classes = [];
  1006. i += 1;
  1007. day = start.getDay();
  1008. d = start.getDate();
  1009. y = start.getFullYear();
  1010. m = start.getMonth();
  1011. w = _xdsoft_datetime.getWeekOfYear(start);
  1012. description = '';
  1013. classes.push('xdsoft_date');
  1014. if (options.beforeShowDay && $.isFunction(options.beforeShowDay.call)) {
  1015. customDateSettings = options.beforeShowDay.call(datetimepicker, start);
  1016. } else {
  1017. customDateSettings = null;
  1018. }
  1019. if ((maxDate !== false && start > maxDate) || (minDate !== false && start < minDate) || (customDateSettings && customDateSettings[0] === false)) {
  1020. classes.push('xdsoft_disabled');
  1021. } else if (options.disabledDates.indexOf(start.dateFormat(options.formatDate)) !== -1) {
  1022. classes.push('xdsoft_disabled');
  1023. } else if (options.disabledWeekDays.indexOf(day) !== -1) {
  1024. classes.push('xdsoft_disabled');
  1025. }
  1026. if (customDateSettings && customDateSettings[1] !== "") {
  1027. classes.push(customDateSettings[1]);
  1028. }
  1029. if (_xdsoft_datetime.currentTime.getMonth() !== m) {
  1030. classes.push('xdsoft_other_month');
  1031. }
  1032. if ((options.defaultSelect || datetimepicker.data('changed')) && _xdsoft_datetime.currentTime.dateFormat(options.formatDate) === start.dateFormat(options.formatDate)) {
  1033. classes.push('xdsoft_current');
  1034. }
  1035. if (today.dateFormat(options.formatDate) === start.dateFormat(options.formatDate)) {
  1036. classes.push('xdsoft_today');
  1037. }
  1038. if (start.getDay() === 0 || start.getDay() === 6 || options.weekends.indexOf(start.dateFormat(options.formatDate)) !== -1) {
  1039. classes.push('xdsoft_weekend');
  1040. }
  1041. if (options.highlightedDates[start.dateFormat(options.formatDate)] !== undefined) {
  1042. hDate = options.highlightedDates[start.dateFormat(options.formatDate)];
  1043. classes.push(hDate.style === undefined ? 'xdsoft_highlighted_default' : hDate.style);
  1044. description = hDate.desc === undefined ? '' : hDate.desc;
  1045. }
  1046. if (options.beforeShowDay && $.isFunction(options.beforeShowDay)) {
  1047. classes.push(options.beforeShowDay(start));
  1048. }
  1049. if (newRow) {
  1050. table += '<tr>';
  1051. newRow = false;
  1052. if (options.weeks) {
  1053. table += '<th>' + w + '</th>';
  1054. }
  1055. }
  1056. table += '<td data-date="' + d + '" data-month="' + m + '" data-year="' + y + '"' + ' class="xdsoft_date xdsoft_day_of_week' + start.getDay() + ' ' + classes.join(' ') + '" title="' + description + '">' +
  1057. '<div>' + d + '</div>' +
  1058. '</td>';
  1059. if (start.getDay() === options.dayOfWeekStartPrev) {
  1060. table += '</tr>';
  1061. newRow = true;
  1062. }
  1063. start.setDate(d + 1);
  1064. }
  1065. table += '</tbody></table>';
  1066. calendar.html(table);
  1067. mounth_picker.find('.xdsoft_label span').eq(0).text(options.i18n[globalLocale].months[_xdsoft_datetime.currentTime.getMonth()]);
  1068. mounth_picker.find('.xdsoft_label span').eq(1).text(_xdsoft_datetime.currentTime.getFullYear());
  1069. // generate timebox
  1070. time = '';
  1071. h = '';
  1072. m = '';
  1073. line_time = function line_time(h, m) {
  1074. var now = _xdsoft_datetime.now(), optionDateTime, current_time,
  1075. isALlowTimesInit = options.allowTimes && $.isArray(options.allowTimes) && options.allowTimes.length;
  1076. now.setHours(h);
  1077. h = parseInt(now.getHours(), 10);
  1078. now.setMinutes(m);
  1079. m = parseInt(now.getMinutes(), 10);
  1080. optionDateTime = new Date(_xdsoft_datetime.currentTime);
  1081. optionDateTime.setHours(h);
  1082. optionDateTime.setMinutes(m);
  1083. classes = [];
  1084. if ((options.minDateTime !== false && options.minDateTime > optionDateTime) || (options.maxTime !== false && _xdsoft_datetime.strtotime(options.maxTime).getTime() < now.getTime()) || (options.minTime !== false && _xdsoft_datetime.strtotime(options.minTime).getTime() > now.getTime())) {
  1085. classes.push('xdsoft_disabled');
  1086. }
  1087. if ((options.minDateTime !== false && options.minDateTime > optionDateTime) || ((options.disabledMinTime !== false && now.getTime() > _xdsoft_datetime.strtotime(options.disabledMinTime).getTime()) && (options.disabledMaxTime !== false && now.getTime() < _xdsoft_datetime.strtotime(options.disabledMaxTime).getTime()))) {
  1088. classes.push('xdsoft_disabled');
  1089. }
  1090. current_time = new Date(_xdsoft_datetime.currentTime);
  1091. current_time.setHours(parseInt(_xdsoft_datetime.currentTime.getHours(), 10));
  1092. if (!isALlowTimesInit) {
  1093. current_time.setMinutes(Math[options.roundTime](_xdsoft_datetime.currentTime.getMinutes() / options.step) * options.step);
  1094. }
  1095. if ((options.initTime || options.defaultSelect || datetimepicker.data('changed')) && current_time.getHours() === parseInt(h, 10) && ((!isALlowTimesInit && options.step > 59) || current_time.getMinutes() === parseInt(m, 10))) {
  1096. if (options.defaultSelect || datetimepicker.data('changed')) {
  1097. classes.push('xdsoft_current');
  1098. } else if (options.initTime) {
  1099. classes.push('xdsoft_init_time');
  1100. }
  1101. }
  1102. if (parseInt(today.getHours(), 10) === parseInt(h, 10) && parseInt(today.getMinutes(), 10) === parseInt(m, 10)) {
  1103. classes.push('xdsoft_today');
  1104. }
  1105. time += '<div class="xdsoft_time ' + classes.join(' ') + '" data-hour="' + h + '" data-minute="' + m + '">' + now.dateFormat(options.formatTime) + '</div>';
  1106. };
  1107. if (!options.allowTimes || !$.isArray(options.allowTimes) || !options.allowTimes.length) {
  1108. for (i = 0, j = 0; i < (options.hours12 ? 12 : 24); i += 1) {
  1109. for (j = 0; j < 60; j += options.step) {
  1110. h = (i < 10 ? '0' : '') + i;
  1111. m = (j < 10 ? '0' : '') + j;
  1112. line_time(h, m);
  1113. }
  1114. }
  1115. } else {
  1116. for (i = 0; i < options.allowTimes.length; i += 1) {
  1117. h = _xdsoft_datetime.strtotime(options.allowTimes[i]).getHours();
  1118. m = _xdsoft_datetime.strtotime(options.allowTimes[i]).getMinutes();
  1119. line_time(h, m);
  1120. }
  1121. }
  1122. timebox.html(time);
  1123. opt = '';
  1124. i = 0;
  1125. for (i = parseInt(options.yearStart, 10) + options.yearOffset; i <= parseInt(options.yearEnd, 10) + options.yearOffset; i += 1) {
  1126. opt += '<div class="xdsoft_option ' + (_xdsoft_datetime.currentTime.getFullYear() === i ? 'xdsoft_current' : '') + '" data-value="' + i + '">' + i + '</div>';
  1127. }
  1128. yearselect.children().eq(0)
  1129. .html(opt);
  1130. for (i = parseInt(options.monthStart, 10), opt = ''; i <= parseInt(options.monthEnd, 10); i += 1) {
  1131. opt += '<div class="xdsoft_option ' + (_xdsoft_datetime.currentTime.getMonth() === i ? 'xdsoft_current' : '') + '" data-value="' + i + '">' + options.i18n[globalLocale].months[i] + '</div>';
  1132. }
  1133. monthselect.children().eq(0).html(opt);
  1134. $(datetimepicker)
  1135. .trigger('generate.xdsoft');
  1136. }, 10);
  1137. event.stopPropagation();
  1138. })
  1139. .on('afterOpen.xdsoft', function () {
  1140. if (options.timepicker) {
  1141. var classType, pheight, height, top;
  1142. if (timebox.find('.xdsoft_current').length) {
  1143. classType = '.xdsoft_current';
  1144. } else if (timebox.find('.xdsoft_init_time').length) {
  1145. classType = '.xdsoft_init_time';
  1146. }
  1147. if (classType) {
  1148. pheight = timeboxparent[0].clientHeight;
  1149. height = timebox[0].offsetHeight;
  1150. top = timebox.find(classType).index() * options.timeHeightInTimePicker + 1;
  1151. if ((height - pheight) < top) {
  1152. top = height - pheight;
  1153. }
  1154. timeboxparent.trigger('scroll_element.xdsoft_scroller', [parseInt(top, 10) / (height - pheight)]);
  1155. } else {
  1156. timeboxparent.trigger('scroll_element.xdsoft_scroller', [0]);
  1157. }
  1158. }
  1159. });
  1160. timerclick = 0;
  1161. calendar
  1162. .on('click.xdsoft', 'td', function (xdevent) {
  1163. xdevent.stopPropagation(); // Prevents closing of Pop-ups, Modals and Flyouts in Bootstrap
  1164. timerclick += 1;
  1165. var $this = $(this),
  1166. currentTime = _xdsoft_datetime.currentTime;
  1167. if (currentTime === undefined || currentTime === null) {
  1168. _xdsoft_datetime.currentTime = _xdsoft_datetime.now();
  1169. currentTime = _xdsoft_datetime.currentTime;
  1170. }
  1171. if ($this.hasClass('xdsoft_disabled')) {
  1172. return false;
  1173. }
  1174. currentTime.setDate(1);
  1175. currentTime.setFullYear($this.data('year'));
  1176. currentTime.setMonth($this.data('month'));
  1177. currentTime.setDate($this.data('date'));
  1178. datetimepicker.trigger('select.xdsoft', [currentTime]);
  1179. input.val(_xdsoft_datetime.str());
  1180. if (options.onSelectDate && $.isFunction(options.onSelectDate)) {
  1181. options.onSelectDate.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'), xdevent);
  1182. }
  1183. datetimepicker.data('changed', true);
  1184. datetimepicker.trigger('xchange.xdsoft');
  1185. datetimepicker.trigger('changedatetime.xdsoft');
  1186. if ((timerclick > 1 || (options.closeOnDateSelect === true || (options.closeOnDateSelect === false && !options.timepicker))) && !options.inline) {
  1187. datetimepicker.trigger('close.xdsoft');
  1188. }
  1189. setTimeout(function () {
  1190. timerclick = 0;
  1191. }, 200);
  1192. });
  1193. timebox
  1194. .on('click.xdsoft', 'div', function (xdevent) {
  1195. xdevent.stopPropagation();
  1196. var $this = $(this),
  1197. currentTime = _xdsoft_datetime.currentTime;
  1198. if (currentTime === undefined || currentTime === null) {
  1199. _xdsoft_datetime.currentTime = _xdsoft_datetime.now();
  1200. currentTime = _xdsoft_datetime.currentTime;
  1201. }
  1202. if ($this.hasClass('xdsoft_disabled')) {
  1203. return false;
  1204. }
  1205. currentTime.setHours($this.data('hour'));
  1206. currentTime.setMinutes($this.data('minute'));
  1207. datetimepicker.trigger('select.xdsoft', [currentTime]);
  1208. datetimepicker.data('input').val(_xdsoft_datetime.str());
  1209. if (options.onSelectTime && $.isFunction(options.onSelectTime)) {
  1210. options.onSelectTime.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'), xdevent);
  1211. }
  1212. datetimepicker.data('changed', true);
  1213. datetimepicker.trigger('xchange.xdsoft');
  1214. datetimepicker.trigger('changedatetime.xdsoft');
  1215. if (options.inline !== true && options.closeOnTimeSelect === true) {
  1216. datetimepicker.trigger('close.xdsoft');
  1217. }
  1218. });
  1219. datepicker
  1220. .on('mousewheel.xdsoft', function (event) {
  1221. if (!options.scrollMonth) {
  1222. return true;
  1223. }
  1224. if (event.deltaY < 0) {
  1225. _xdsoft_datetime.nextMonth();
  1226. } else {
  1227. _xdsoft_datetime.prevMonth();
  1228. }
  1229. return false;
  1230. });
  1231. input
  1232. .on('mousewheel.xdsoft', function (event) {
  1233. if (!options.scrollInput) {
  1234. return true;
  1235. }
  1236. if (!options.datepicker && options.timepicker) {
  1237. current_time_index = timebox.find('.xdsoft_current').length ? timebox.find('.xdsoft_current').eq(0).index() : 0;
  1238. if (current_time_index + event.deltaY >= 0 && current_time_index + event.deltaY < timebox.children().length) {
  1239. current_time_index += event.deltaY;
  1240. }
  1241. if (timebox.children().eq(current_time_index).length) {
  1242. timebox.children().eq(current_time_index).trigger('mousedown');
  1243. }
  1244. return false;
  1245. }
  1246. if (options.datepicker && !options.timepicker) {
  1247. datepicker.trigger(event, [event.deltaY, event.deltaX, event.deltaY]);
  1248. if (input.val) {
  1249. input.val(_xdsoft_datetime.str());
  1250. }
  1251. datetimepicker.trigger('changedatetime.xdsoft');
  1252. return false;
  1253. }
  1254. });
  1255. datetimepicker
  1256. .on('changedatetime.xdsoft', function (event) {
  1257. if (options.onChangeDateTime && $.isFunction(options.onChangeDateTime)) {
  1258. var $input = datetimepicker.data('input');
  1259. options.onChangeDateTime.call(datetimepicker, _xdsoft_datetime.currentTime, $input, event);
  1260. delete options.value;
  1261. $input.trigger('change');
  1262. }
  1263. })
  1264. .on('generate.xdsoft', function () {
  1265. if (options.onGenerate && $.isFunction(options.onGenerate)) {
  1266. options.onGenerate.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'));
  1267. }
  1268. if (triggerAfterOpen) {
  1269. datetimepicker.trigger('afterOpen.xdsoft');
  1270. triggerAfterOpen = false;
  1271. }
  1272. })
  1273. .on('click.xdsoft', function (xdevent) {
  1274. xdevent.stopPropagation();
  1275. });
  1276. current_time_index = 0;
  1277. setPos = function () {
  1278. var offset = datetimepicker.data('input').offset(), top = offset.top + datetimepicker.data('input')[0].offsetHeight - 1, left = offset.left, position = "absolute", node;
  1279. if (datetimepicker.data('input').parent().css('direction') == 'rtl')
  1280. left -= (datetimepicker.outerWidth() - datetimepicker.data('input').outerWidth());
  1281. if (options.fixed) {
  1282. top -= $(window).scrollTop();
  1283. left -= $(window).scrollLeft();
  1284. position = "fixed";
  1285. } else {
  1286. if (top + datetimepicker[0].offsetHeight > $(window).height() + $(window).scrollTop()) {
  1287. top = offset.top - datetimepicker[0].offsetHeight + 1;
  1288. }
  1289. if (top < 0) {
  1290. top = 0;
  1291. }
  1292. if (left + datetimepicker[0].offsetWidth > $(window).width()) {
  1293. left = $(window).width() - datetimepicker[0].offsetWidth;
  1294. }
  1295. }
  1296. node = datetimepicker[0];
  1297. do {
  1298. node = node.parentNode;
  1299. if (window.getComputedStyle(node).getPropertyValue('position') === 'relative' && $(window).width() >= node.offsetWidth) {
  1300. left = left - (($(window).width() - node.offsetWidth) / 2);
  1301. break;
  1302. }
  1303. } while (node.nodeName !== 'HTML');
  1304. datetimepicker.css({
  1305. left: left,
  1306. top: top,
  1307. position: position
  1308. });
  1309. };
  1310. datetimepicker
  1311. .on('open.xdsoft', function (event) {
  1312. var onShow = true;
  1313. if (options.onShow && $.isFunction(options.onShow)) {
  1314. onShow = options.onShow.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'), event);
  1315. }
  1316. if (onShow !== false) {
  1317. $('body').focus();
  1318. datetimepicker.show();
  1319. setPos();
  1320. $(window)
  1321. .off('resize.xdsoft', setPos)
  1322. .on('resize.xdsoft', setPos);
  1323. if (options.closeOnWithoutClick) {
  1324. $([document.body, window]).on('mousedown.xdsoft', function arguments_callee6() {
  1325. datetimepicker.trigger('close.xdsoft');
  1326. $([document.body, window]).off('mousedown.xdsoft', arguments_callee6);
  1327. });
  1328. }
  1329. }
  1330. })
  1331. .on('close.xdsoft', function (event) {
  1332. var onClose = true;
  1333. mounth_picker
  1334. .find('.xdsoft_month,.xdsoft_year')
  1335. .find('.xdsoft_select')
  1336. .hide();
  1337. if (options.onClose && $.isFunction(options.onClose)) {
  1338. onClose = options.onClose.call(datetimepicker, _xdsoft_datetime.currentTime, datetimepicker.data('input'), event);
  1339. }
  1340. if (onClose !== false && !options.opened && !options.inline) {
  1341. datetimepicker.hide();
  1342. }
  1343. event.stopPropagation();
  1344. })
  1345. .on('toggle.xdsoft', function (event) {
  1346. if (datetimepicker.is(':visible')) {
  1347. datetimepicker.trigger('close.xdsoft');
  1348. } else {
  1349. datetimepicker.trigger('open.xdsoft');
  1350. }
  1351. })
  1352. .data('input', input);
  1353. timer = 0;
  1354. timer1 = 0;
  1355. datetimepicker.data('xdsoft_datetime', _xdsoft_datetime);
  1356. datetimepicker.setOptions(options);
  1357. function getCurrentValue() {
  1358. var ct = false, time;
  1359. if (options.startDate) {
  1360. ct = _xdsoft_datetime.strToDate(options.startDate);
  1361. } else {
  1362. ct = options.value || ((input && input.val && input.val()) ? input.val() : '');
  1363. if (ct) {
  1364. ct = _xdsoft_datetime.strToDateTime(ct);
  1365. } else if (options.defaultDate) {
  1366. ct = _xdsoft_datetime.strToDateTime(options.defaultDate);
  1367. if (options.defaultTime) {
  1368. time = _xdsoft_datetime.strtotime(options.defaultTime);
  1369. ct.setHours(time.getHours());
  1370. ct.setMinutes(time.getMinutes());
  1371. }
  1372. }
  1373. }
  1374. if (ct && _xdsoft_datetime.isValidDate(ct)) {
  1375. datetimepicker.data('changed', true);
  1376. } else {
  1377. ct = '';
  1378. }
  1379. return ct || 0;
  1380. }
  1381. _xdsoft_datetime.setCurrentTime(getCurrentValue());
  1382. input
  1383. .data('xdsoft_datetimepicker', datetimepicker)
  1384. .on('open.xdsoft focusin.xdsoft mousedown.xdsoft', function (event) {
  1385. if (input.is(':disabled') || (input.data('xdsoft_datetimepicker').is(':visible') && options.closeOnInputClick)) {
  1386. return;
  1387. }
  1388. clearTimeout(timer);
  1389. timer = setTimeout(function () {
  1390. if (input.is(':disabled')) {
  1391. return;
  1392. }
  1393. triggerAfterOpen = true;
  1394. _xdsoft_datetime.setCurrentTime(getCurrentValue());
  1395. datetimepicker.trigger('open.xdsoft');
  1396. }, 100);
  1397. })
  1398. .on('keydown.xdsoft', function (event) {
  1399. var val = this.value, elementSelector,
  1400. key = event.which;
  1401. if ([ENTER].indexOf(key) !== -1 && options.enterLikeTab) {
  1402. elementSelector = $("input:visible,textarea:visible");
  1403. datetimepicker.trigger('close.xdsoft');
  1404. elementSelector.eq(elementSelector.index(this) + 1).focus();
  1405. return false;
  1406. }
  1407. if ([TAB].indexOf(key) !== -1) {
  1408. datetimepicker.trigger('close.xdsoft');
  1409. return true;
  1410. }
  1411. });
  1412. };
  1413. destroyDateTimePicker = function (input) {
  1414. var datetimepicker = input.data('xdsoft_datetimepicker');
  1415. if (datetimepicker) {
  1416. datetimepicker.data('xdsoft_datetime', null);
  1417. datetimepicker.remove();
  1418. input
  1419. .data('xdsoft_datetimepicker', null)
  1420. .off('.xdsoft');
  1421. $(window).off('resize.xdsoft');
  1422. $([window, document.body]).off('mousedown.xdsoft');
  1423. if (input.unmousewheel) {
  1424. input.unmousewheel();
  1425. }
  1426. }
  1427. };
  1428. $(document)
  1429. .off('keydown.xdsoftctrl keyup.xdsoftctrl')
  1430. .on('keydown.xdsoftctrl', function (e) {
  1431. if (e.keyCode === CTRLKEY) {
  1432. ctrlDown = true;
  1433. }
  1434. })
  1435. .on('keyup.xdsoftctrl', function (e) {
  1436. if (e.keyCode === CTRLKEY) {
  1437. ctrlDown = false;
  1438. }
  1439. });
  1440. return this.each(function () {
  1441. var datetimepicker = $(this).data('xdsoft_datetimepicker'), $input;
  1442. if (datetimepicker) {
  1443. if ($.type(opt) === 'string') {
  1444. switch (opt) {
  1445. case 'show':
  1446. $(this).select().focus();
  1447. datetimepicker.trigger('open.xdsoft');
  1448. break;
  1449. case 'hide':
  1450. datetimepicker.trigger('close.xdsoft');
  1451. break;
  1452. case 'toggle':
  1453. datetimepicker.trigger('toggle.xdsoft');
  1454. break;
  1455. case 'destroy':
  1456. destroyDateTimePicker($(this));
  1457. break;
  1458. case 'reset':
  1459. this.value = this.defaultValue;
  1460. if (!this.value || !datetimepicker.data('xdsoft_datetime').isValidDate(Date.parseDate(this.value, options.format))) {
  1461. datetimepicker.data('changed', false);
  1462. }
  1463. datetimepicker.data('xdsoft_datetime').setCurrentTime(this.value);
  1464. break;
  1465. case 'validate':
  1466. $input = datetimepicker.data('input');
  1467. $input.trigger('blur.xdsoft');
  1468. break;
  1469. }
  1470. } else {
  1471. datetimepicker
  1472. .setOptions(opt);
  1473. }
  1474. return 0;
  1475. }
  1476. if ($.type(opt) !== 'string') {
  1477. if (!options.lazyInit || options.open || options.inline) {
  1478. createDateTimePicker($(this));
  1479. } else {
  1480. lazyInit($(this));
  1481. }
  1482. }
  1483. });
  1484. };
  1485. $.fn.datetimepicker.defaults = default_options;
  1486. function HighlightedDate(date, desc, style) {
  1487. "use strict";
  1488. this.date = date;
  1489. this.desc = desc;
  1490. this.style = style;
  1491. }
  1492. }));