foundation-datepicker.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /* =========================================================
  2. * foundation-datepicker.js
  3. * Copyright 2015 Peter Beno, najlepsiwebdesigner@gmail.com, @benopeter
  4. * project website http://foundation-datepicker.peterbeno.com
  5. * ========================================================= */
  6. ! function($) {
  7. function UTCDate() {
  8. return new Date(Date.UTC.apply(Date, arguments));
  9. }
  10. function UTCToday() {
  11. var today = new Date();
  12. return UTCDate(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate());
  13. }
  14. var Datepicker = function(element, options) {
  15. var that = this;
  16. this.element = $(element);
  17. this.autoShow = options.autoShow || true;
  18. this.appendTo = options.appendTo || 'body';
  19. this.closeButton = options.closeButton;
  20. this.language = options.language || this.element.data('date-language') || "en";
  21. this.language = this.language in dates ? this.language : this.language.split('-')[0]; //Check if "de-DE" style date is available, if not language should fallback to 2 letter code eg "de"
  22. this.language = this.language in dates ? this.language : "en";
  23. this.isRTL = dates[this.language].rtl || false;
  24. this.format = DPGlobal.parseFormat(options.format || this.element.data('date-format') || dates[this.language].format || 'yyyy-mm-dd');
  25. this.isInline = false;
  26. this.isInput = this.element.is('input');
  27. this.component = this.element.is('.date') ? this.element.find('.prefix, .postfix') : false;
  28. this.hasInput = this.component && this.element.find('input').length;
  29. this.disableDblClickSelection = options.disableDblClickSelection;
  30. this.onRender = options.onRender || function() {};
  31. if (this.component && this.component.length === 0) {
  32. this.component = false;
  33. }
  34. this.linkField = options.linkField || this.element.data('link-field') || false;
  35. this.linkFormat = DPGlobal.parseFormat(options.linkFormat || this.element.data('link-format') || 'yyyy-mm-dd hh:ii:ss');
  36. this.minuteStep = options.minuteStep || this.element.data('minute-step') || 5;
  37. this.pickerPosition = options.pickerPosition || this.element.data('picker-position') || 'bottom-right';
  38. this.initialDate = options.initialDate || null;
  39. this._attachEvents();
  40. this.minView = 0;
  41. if ('minView' in options) {
  42. this.minView = options.minView;
  43. } else if ('minView' in this.element.data()) {
  44. this.minView = this.element.data('min-view');
  45. }
  46. this.minView = DPGlobal.convertViewMode(this.minView);
  47. this.maxView = DPGlobal.modes.length - 1;
  48. if ('maxView' in options) {
  49. this.maxView = options.maxView;
  50. } else if ('maxView' in this.element.data()) {
  51. this.maxView = this.element.data('max-view');
  52. }
  53. this.maxView = DPGlobal.convertViewMode(this.maxView);
  54. this.startViewMode = 'month';
  55. if ('startView' in options) {
  56. this.startViewMode = options.startView;
  57. } else if ('startView' in this.element.data()) {
  58. this.startViewMode = this.element.data('start-view');
  59. }
  60. this.startViewMode = DPGlobal.convertViewMode(this.startViewMode);
  61. this.viewMode = this.startViewMode;
  62. if (!('minView' in options) && !('maxView' in options) && !(this.element.data('min-view') && !(this.element.data('max-view')))) {
  63. this.pickTime = false;
  64. if ('pickTime' in options) {
  65. this.pickTime = options.pickTime;
  66. }
  67. if (this.pickTime == true) {
  68. this.minView = 0;
  69. this.maxView = 4;
  70. } else {
  71. this.minView = 2;
  72. this.maxView = 4;
  73. }
  74. }
  75. this.forceParse = true;
  76. if ('forceParse' in options) {
  77. this.forceParse = options.forceParse;
  78. } else if ('dateForceParse' in this.element.data()) {
  79. this.forceParse = this.element.data('date-force-parse');
  80. }
  81. this.picker = $(DPGlobal.template)
  82. .appendTo(this.isInline ? this.element : this.appendTo)
  83. .on({
  84. click: $.proxy(this.click, this),
  85. mousedown: $.proxy(this.mousedown, this)
  86. });
  87. if (this.closeButton) {
  88. this.picker.find('a.datepicker-close').show();
  89. } else {
  90. this.picker.find('a.datepicker-close').hide();
  91. }
  92. if (this.isInline) {
  93. this.picker.addClass('datepicker-inline');
  94. } else {
  95. this.picker.addClass('datepicker-dropdown dropdown-menu');
  96. }
  97. if (this.isRTL) {
  98. this.picker.addClass('datepicker-rtl');
  99. this.picker.find('.prev i, .next i')
  100. .toggleClass('fa-chevron-left fa-chevron-right');
  101. }
  102. $(document).on('mousedown', function(e) {
  103. // Clicked outside the datepicker, hide it
  104. if ($(e.target).closest('.datepicker.datepicker-inline, .datepicker.datepicker-dropdown').length === 0) {
  105. that.hide();
  106. }
  107. });
  108. this.autoclose = true;
  109. if ('autoclose' in options) {
  110. this.autoclose = options.autoclose;
  111. } else if ('dateAutoclose' in this.element.data()) {
  112. this.autoclose = this.element.data('date-autoclose');
  113. }
  114. this.keyboardNavigation = true;
  115. if ('keyboardNavigation' in options) {
  116. this.keyboardNavigation = options.keyboardNavigation;
  117. } else if ('dateKeyboardNavigation' in this.element.data()) {
  118. this.keyboardNavigation = this.element.data('date-keyboard-navigation');
  119. }
  120. this.todayBtn = (options.todayBtn || this.element.data('date-today-btn') || false);
  121. this.todayHighlight = (options.todayHighlight || this.element.data('date-today-highlight') || false);
  122. this.calendarWeeks = false;
  123. if ('calendarWeeks' in options) {
  124. this.calendarWeeks = options.calendarWeeks;
  125. } else if ('dateCalendarWeeks' in this.element.data()) {
  126. this.calendarWeeks = this.element.data('date-calendar-weeks');
  127. }
  128. if (this.calendarWeeks)
  129. this.picker.find('tfoot th.today')
  130. .attr('colspan', function(i, val) {
  131. return parseInt(val) + 1;
  132. });
  133. this.weekStart = ((options.weekStart || this.element.data('date-weekstart') || dates[this.language].weekStart || 0) % 7);
  134. this.weekEnd = ((this.weekStart + 6) % 7);
  135. this.startDate = -Infinity;
  136. this.endDate = Infinity;
  137. this.daysOfWeekDisabled = [];
  138. this.setStartDate(options.startDate || this.element.data('date-startdate'));
  139. this.setEndDate(options.endDate || this.element.data('date-enddate'));
  140. this.setDaysOfWeekDisabled(options.daysOfWeekDisabled || this.element.data('date-days-of-week-disabled'));
  141. this.fillDow();
  142. this.fillMonths();
  143. this.update();
  144. this.showMode();
  145. if (this.isInline) {
  146. this.show();
  147. }
  148. };
  149. Datepicker.prototype = {
  150. constructor: Datepicker,
  151. _events: [],
  152. _attachEvents: function() {
  153. this._detachEvents();
  154. if (this.isInput) { // single input
  155. this._events = [
  156. [this.element, {
  157. focus: (this.autoShow) ? $.proxy(this.show, this) : function() {},
  158. keyup: $.proxy(this.update, this),
  159. keydown: $.proxy(this.keydown, this)
  160. }]
  161. ];
  162. } else if (this.component && this.hasInput) { // component: input + button
  163. this._events = [
  164. // For components that are not readonly, allow keyboard nav
  165. [this.element.find('input'), {
  166. focus: (this.autoShow) ? $.proxy(this.show, this) : function() {},
  167. keyup: $.proxy(this.update, this),
  168. keydown: $.proxy(this.keydown, this)
  169. }],
  170. [this.component, {
  171. click: $.proxy(this.show, this)
  172. }]
  173. ];
  174. } else if (this.element.is('div')) { // inline datepicker
  175. this.isInline = true;
  176. } else {
  177. this._events = [
  178. [this.element, {
  179. click: $.proxy(this.show, this)
  180. }]
  181. ];
  182. }
  183. if (this.disableDblClickSelection) {
  184. this._events[this._events.length] = [
  185. this.element, {
  186. dblclick: function(e) {
  187. e.preventDefault();
  188. e.stopPropagation();
  189. $(this).blur()
  190. }
  191. }
  192. ];
  193. }
  194. for (var i = 0, el, ev; i < this._events.length; i++) {
  195. el = this._events[i][0];
  196. ev = this._events[i][1];
  197. el.on(ev);
  198. }
  199. },
  200. _detachEvents: function() {
  201. for (var i = 0, el, ev; i < this._events.length; i++) {
  202. el = this._events[i][0];
  203. ev = this._events[i][1];
  204. el.off(ev);
  205. }
  206. this._events = [];
  207. },
  208. show: function(e) {
  209. this.picker.show();
  210. this.height = this.component ? this.component.outerHeight() : this.element.outerHeight();
  211. this.update();
  212. this.place();
  213. $(window).on('resize', $.proxy(this.place, this));
  214. if (e) {
  215. e.stopPropagation();
  216. e.preventDefault();
  217. }
  218. this.element.trigger({
  219. type: 'show',
  220. date: this.date
  221. });
  222. },
  223. hide: function(e) {
  224. if (this.isInline) return;
  225. if (!this.picker.is(':visible')) return;
  226. this.picker.hide();
  227. $(window).off('resize', this.place);
  228. this.viewMode = this.startViewMode;
  229. this.showMode();
  230. if (!this.isInput) {
  231. $(document).off('mousedown', this.hide);
  232. }
  233. if (
  234. this.forceParse &&
  235. (
  236. this.isInput && this.element.val() ||
  237. this.hasInput && this.element.find('input').val()
  238. )
  239. )
  240. this.setValue();
  241. this.element.trigger({
  242. type: 'hide',
  243. date: this.date
  244. });
  245. },
  246. remove: function() {
  247. this._detachEvents();
  248. this.picker.remove();
  249. delete this.element.data().datepicker;
  250. },
  251. getDate: function() {
  252. var d = this.getUTCDate();
  253. return new Date(d.getTime() + (d.getTimezoneOffset() * 60000));
  254. },
  255. getUTCDate: function() {
  256. return this.date;
  257. },
  258. setDate: function(d) {
  259. this.setUTCDate(new Date(d.getTime() - (d.getTimezoneOffset() * 60000)));
  260. },
  261. setUTCDate: function(d) {
  262. this.date = d;
  263. this.setValue();
  264. },
  265. setValue: function() {
  266. var formatted = this.getFormattedDate();
  267. if (!this.isInput) {
  268. if (this.component) {
  269. this.element.find('input').val(formatted);
  270. }
  271. this.element.data('date', formatted);
  272. } else {
  273. this.element.val(formatted);
  274. }
  275. },
  276. getFormattedDate: function(format) {
  277. if (format === undefined)
  278. format = this.format;
  279. return DPGlobal.formatDate(this.date, format, this.language);
  280. },
  281. setStartDate: function(startDate) {
  282. this.startDate = startDate || -Infinity;
  283. if (this.startDate !== -Infinity) {
  284. this.startDate = DPGlobal.parseDate(this.startDate, this.format, this.language);
  285. }
  286. this.update();
  287. this.updateNavArrows();
  288. },
  289. setEndDate: function(endDate) {
  290. this.endDate = endDate || Infinity;
  291. if (this.endDate !== Infinity) {
  292. this.endDate = DPGlobal.parseDate(this.endDate, this.format, this.language);
  293. }
  294. this.update();
  295. this.updateNavArrows();
  296. },
  297. setDaysOfWeekDisabled: function(daysOfWeekDisabled) {
  298. this.daysOfWeekDisabled = daysOfWeekDisabled || [];
  299. if (!$.isArray(this.daysOfWeekDisabled)) {
  300. this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
  301. }
  302. this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function(d) {
  303. return parseInt(d, 10);
  304. });
  305. this.update();
  306. this.updateNavArrows();
  307. },
  308. place: function() {
  309. if (this.isInline) return;
  310. var zIndex = parseInt(this.element.parents().filter(function() {
  311. return $(this).css('z-index') != 'auto';
  312. }).first().css('z-index')) + 10;
  313. var textbox = this.component ? this.component : this.element;
  314. var offset = textbox.offset();
  315. var height = textbox.outerHeight() + parseInt(textbox.css('margin-top'));
  316. var width = textbox.outerWidth() + parseInt(textbox.css('margin-left'));
  317. var fullOffsetTop = offset.top + height;
  318. var offsetLeft = offset.left;
  319. // if the datepicker is going to be below the window, show it on top of the input
  320. if ((fullOffsetTop + this.picker.outerHeight()) >= $(window).scrollTop() + $(window).height()) {
  321. fullOffsetTop = offset.top - this.picker.outerHeight();
  322. }
  323. // if the datepicker is going to go past the right side of the window, we want
  324. // to set the right position so the datepicker lines up with the textbox
  325. if (offset.left + this.picker.width() >= $(window).width()) {
  326. offsetLeft = (offset.left + width) - this.picker.width();
  327. }
  328. this.picker.css({
  329. top: fullOffsetTop,
  330. left: offsetLeft,
  331. zIndex: zIndex
  332. });
  333. },
  334. update: function() {
  335. var date, fromArgs = false;
  336. var currentVal = this.isInput ? this.element.val() : this.element.data('date') || this.element.find('input').val();
  337. if (arguments && arguments.length && (typeof arguments[0] === 'string' || arguments[0] instanceof Date)) {
  338. date = arguments[0];
  339. fromArgs = true;
  340. } else if (!currentVal && this.initialDate != null) { // If value is not set, set it to the initialDate
  341. date = this.initialDate
  342. } else {
  343. date = this.isInput ? this.element.val() : this.element.data('date') || this.element.find('input').val();
  344. }
  345. this.date = DPGlobal.parseDate(date, this.format, this.language);
  346. if (fromArgs || this.initialDate != null) this.setValue();
  347. if (this.date < this.startDate) {
  348. this.viewDate = new Date(this.startDate.valueOf());
  349. } else if (this.date > this.endDate) {
  350. this.viewDate = new Date(this.endDate.valueOf());
  351. } else {
  352. this.viewDate = new Date(this.date.valueOf());
  353. }
  354. this.fill();
  355. },
  356. fillDow: function() {
  357. var dowCnt = this.weekStart,
  358. html = '<tr>';
  359. if (this.calendarWeeks) {
  360. var cell = '<th class="cw">&nbsp;</th>';
  361. html += cell;
  362. this.picker.find('.datepicker-days thead tr:first-child').prepend(cell);
  363. }
  364. while (dowCnt < this.weekStart + 7) {
  365. html += '<th class="dow">' + dates[this.language].daysMin[(dowCnt++) % 7] + '</th>';
  366. }
  367. html += '</tr>';
  368. this.picker.find('.datepicker-days thead').append(html);
  369. },
  370. fillMonths: function() {
  371. var html = '',
  372. i = 0;
  373. while (i < 12) {
  374. html += '<span class="month">' + dates[this.language].monthsShort[i++] + '</span>';
  375. }
  376. this.picker.find('.datepicker-months td').html(html);
  377. },
  378. fill: function() {
  379. if (this.date == null || this.viewDate == null) {
  380. return;
  381. }
  382. var d = new Date(this.viewDate.valueOf()),
  383. year = d.getUTCFullYear(),
  384. month = d.getUTCMonth(),
  385. dayMonth = d.getUTCDate(),
  386. hours = d.getUTCHours(),
  387. minutes = d.getUTCMinutes(),
  388. startYear = this.startDate !== -Infinity ? this.startDate.getUTCFullYear() : -Infinity,
  389. startMonth = this.startDate !== -Infinity ? this.startDate.getUTCMonth() : -Infinity,
  390. endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
  391. endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
  392. currentDate = this.date && this.date.valueOf(),
  393. today = new Date(),
  394. titleFormat = dates[this.language].titleFormat || dates['en'].titleFormat;
  395. // this.picker.find('.datepicker-days thead th.date-switch')
  396. // .text(DPGlobal.formatDate(new UTCDate(year, month), titleFormat, this.language));
  397. this.picker.find('.datepicker-days thead th:eq(1)')
  398. .text(dates[this.language].months[month] + ' ' + year);
  399. this.picker.find('.datepicker-hours thead th:eq(1)')
  400. .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);
  401. this.picker.find('.datepicker-minutes thead th:eq(1)')
  402. .text(dayMonth + ' ' + dates[this.language].months[month] + ' ' + year);
  403. this.picker.find('tfoot th.today')
  404. .text(dates[this.language].today)
  405. .toggle(this.todayBtn !== false);
  406. this.updateNavArrows();
  407. this.fillMonths();
  408. var prevMonth = UTCDate(year, month - 1, 28, 0, 0, 0, 0),
  409. day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
  410. prevMonth.setUTCDate(day);
  411. prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.weekStart + 7) % 7);
  412. var nextMonth = new Date(prevMonth.valueOf());
  413. nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
  414. nextMonth = nextMonth.valueOf();
  415. var html = [];
  416. var clsName;
  417. while (prevMonth.valueOf() < nextMonth) {
  418. if (prevMonth.getUTCDay() == this.weekStart) {
  419. html.push('<tr>');
  420. if (this.calendarWeeks) {
  421. // adapted from https://github.com/timrwood/moment/blob/master/moment.js#L128
  422. var a = new Date(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth(), prevMonth.getUTCDate() - prevMonth.getDay() + 10 - (this.weekStart && this.weekStart % 7 < 5 && 7)),
  423. b = new Date(a.getFullYear(), 0, 4),
  424. calWeek = ~~((a - b) / 864e5 / 7 + 1.5);
  425. html.push('<td class="cw">' + calWeek + '</td>');
  426. }
  427. }
  428. clsName = ' ' + this.onRender(prevMonth) + ' ';
  429. if (prevMonth.getUTCFullYear() < year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() < month)) {
  430. clsName += ' old';
  431. } else if (prevMonth.getUTCFullYear() > year || (prevMonth.getUTCFullYear() == year && prevMonth.getUTCMonth() > month)) {
  432. clsName += ' new';
  433. }
  434. // Compare internal UTC date with local today, not UTC today
  435. if (this.todayHighlight &&
  436. prevMonth.getUTCFullYear() == today.getFullYear() &&
  437. prevMonth.getUTCMonth() == today.getMonth() &&
  438. prevMonth.getUTCDate() == today.getDate()) {
  439. clsName += ' today';
  440. }
  441. if (currentDate && prevMonth.valueOf() == currentDate) {
  442. clsName += ' active';
  443. }
  444. if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
  445. $.inArray(prevMonth.getUTCDay(), this.daysOfWeekDisabled) !== -1) {
  446. clsName += ' disabled';
  447. }
  448. html.push('<td class="day' + clsName + '">' + prevMonth.getUTCDate() + '</td>');
  449. if (prevMonth.getUTCDay() == this.weekEnd) {
  450. html.push('</tr>');
  451. }
  452. prevMonth.setUTCDate(prevMonth.getUTCDate() + 1);
  453. }
  454. this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
  455. html = [];
  456. for (var i = 0; i < 24; i++) {
  457. var actual = UTCDate(year, month, dayMonth, i);
  458. clsName = '';
  459. // We want the previous hour for the startDate
  460. if ((actual.valueOf() + 3600000) < this.startDate || actual.valueOf() > this.endDate) {
  461. clsName += ' disabled';
  462. } else if (hours == i) {
  463. clsName += ' active';
  464. }
  465. html.push('<span class="hour' + clsName + '">' + i + ':00</span>');
  466. }
  467. this.picker.find('.datepicker-hours td').html(html.join(''));
  468. html = [];
  469. for (var i = 0; i < 60; i += this.minuteStep) {
  470. var actual = UTCDate(year, month, dayMonth, hours, i);
  471. clsName = '';
  472. if (actual.valueOf() < this.startDate || actual.valueOf() > this.endDate) {
  473. clsName += ' disabled';
  474. } else if (Math.floor(minutes / this.minuteStep) == Math.floor(i / this.minuteStep)) {
  475. clsName += ' active';
  476. }
  477. html.push('<span class="minute' + clsName + '">' + hours + ':' + (i < 10 ? '0' + i : i) + '</span>');
  478. }
  479. this.picker.find('.datepicker-minutes td').html(html.join(''));
  480. var currentYear = this.date && this.date.getUTCFullYear();
  481. var months = this.picker.find('.datepicker-months')
  482. .find('th:eq(1)')
  483. .text(year)
  484. .end()
  485. .find('span').removeClass('active');
  486. if (currentYear && currentYear == year) {
  487. months.eq(this.date.getUTCMonth()).addClass('active');
  488. }
  489. if (year < startYear || year > endYear) {
  490. months.addClass('disabled');
  491. }
  492. if (year == startYear) {
  493. months.slice(0, startMonth).addClass('disabled');
  494. }
  495. if (year == endYear) {
  496. months.slice(endMonth + 1).addClass('disabled');
  497. }
  498. html = '';
  499. year = parseInt(year / 10, 10) * 10;
  500. var yearCont = this.picker.find('.datepicker-years')
  501. .find('th:eq(1)')
  502. .text(year + '-' + (year + 9))
  503. .end()
  504. .find('td');
  505. year -= 1;
  506. for (var i = -1; i < 11; i++) {
  507. html += '<span class="year' + (i == -1 || i == 10 ? ' old' : '') + (currentYear == year ? ' active' : '') + (year < startYear || year > endYear ? ' disabled' : '') + '">' + year + '</span>';
  508. year += 1;
  509. }
  510. yearCont.html(html);
  511. },
  512. updateNavArrows: function() {
  513. var d = new Date(this.viewDate),
  514. year = d.getUTCFullYear(),
  515. month = d.getUTCMonth(),
  516. day = d.getUTCDate(),
  517. hour = d.getUTCHours();
  518. switch (this.viewMode) {
  519. case 0:
  520. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth() && day <= this.startDate.getUTCDate() && hour <= this.startDate.getUTCHours()) {
  521. this.picker.find('.prev').css({
  522. visibility: 'hidden'
  523. });
  524. } else {
  525. this.picker.find('.prev').css({
  526. visibility: 'visible'
  527. });
  528. }
  529. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth() && day >= this.endDate.getUTCDate() && hour >= this.endDate.getUTCHours()) {
  530. this.picker.find('.next').css({
  531. visibility: 'hidden'
  532. });
  533. } else {
  534. this.picker.find('.next').css({
  535. visibility: 'visible'
  536. });
  537. }
  538. break;
  539. case 1:
  540. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth() && day <= this.startDate.getUTCDate()) {
  541. this.picker.find('.prev').css({
  542. visibility: 'hidden'
  543. });
  544. } else {
  545. this.picker.find('.prev').css({
  546. visibility: 'visible'
  547. });
  548. }
  549. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth() && day >= this.endDate.getUTCDate()) {
  550. this.picker.find('.next').css({
  551. visibility: 'hidden'
  552. });
  553. } else {
  554. this.picker.find('.next').css({
  555. visibility: 'visible'
  556. });
  557. }
  558. break;
  559. case 2:
  560. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear() && month <= this.startDate.getUTCMonth()) {
  561. this.picker.find('.prev').css({
  562. visibility: 'hidden'
  563. });
  564. } else {
  565. this.picker.find('.prev').css({
  566. visibility: 'visible'
  567. });
  568. }
  569. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear() && month >= this.endDate.getUTCMonth()) {
  570. this.picker.find('.next').css({
  571. visibility: 'hidden'
  572. });
  573. } else {
  574. this.picker.find('.next').css({
  575. visibility: 'visible'
  576. });
  577. }
  578. break;
  579. case 3:
  580. case 4:
  581. if (this.startDate !== -Infinity && year <= this.startDate.getUTCFullYear()) {
  582. this.picker.find('.prev').css({
  583. visibility: 'hidden'
  584. });
  585. } else {
  586. this.picker.find('.prev').css({
  587. visibility: 'visible'
  588. });
  589. }
  590. if (this.endDate !== Infinity && year >= this.endDate.getUTCFullYear()) {
  591. this.picker.find('.next').css({
  592. visibility: 'hidden'
  593. });
  594. } else {
  595. this.picker.find('.next').css({
  596. visibility: 'visible'
  597. });
  598. }
  599. break;
  600. }
  601. },
  602. click: function(e) {
  603. e.stopPropagation();
  604. e.preventDefault();
  605. if ($(e.target).hasClass('datepicker-close') || $(e.target).parent().hasClass('datepicker-close')) {
  606. this.hide();
  607. }
  608. var target = $(e.target).closest('span, td, th');
  609. if (target.length == 1) {
  610. if (target.is('.disabled')) {
  611. this.element.trigger({
  612. type: 'outOfRange',
  613. date: this.viewDate,
  614. startDate: this.startDate,
  615. endDate: this.endDate
  616. });
  617. return;
  618. }
  619. switch (target[0].nodeName.toLowerCase()) {
  620. case 'th':
  621. switch (target[0].className) {
  622. case 'date-switch':
  623. this.showMode(1);
  624. break;
  625. case 'prev':
  626. case 'next':
  627. var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className == 'prev' ? -1 : 1);
  628. switch (this.viewMode) {
  629. case 0:
  630. this.viewDate = this.moveHour(this.viewDate, dir);
  631. break;
  632. case 1:
  633. this.viewDate = this.moveDate(this.viewDate, dir);
  634. break;
  635. case 2:
  636. this.viewDate = this.moveMonth(this.viewDate, dir);
  637. break;
  638. case 3:
  639. case 4:
  640. this.viewDate = this.moveYear(this.viewDate, dir);
  641. break;
  642. }
  643. this.fill();
  644. break;
  645. case 'today':
  646. var date = new Date();
  647. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
  648. this.viewMode = this.startViewMode;
  649. this.showMode(0);
  650. this._setDate(date);
  651. break;
  652. }
  653. break;
  654. case 'span':
  655. if (!target.is('.disabled')) {
  656. if (target.is('.month')) {
  657. if (this.minView === 3) {
  658. var month = target.parent().find('span').index(target) || 0;
  659. var year = this.viewDate.getUTCFullYear(),
  660. day = 1,
  661. hours = this.viewDate.getUTCHours(),
  662. minutes = this.viewDate.getUTCMinutes(),
  663. seconds = this.viewDate.getUTCSeconds();
  664. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  665. } else {
  666. this.viewDate.setUTCDate(1);
  667. var month = target.parent().find('span').index(target);
  668. this.viewDate.setUTCMonth(month);
  669. this.element.trigger({
  670. type: 'changeMonth',
  671. date: this.viewDate
  672. });
  673. }
  674. } else if (target.is('.year')) {
  675. if (this.minView === 4) {
  676. var year = parseInt(target.text(), 10) || 0;
  677. var month = 0,
  678. day = 1,
  679. hours = this.viewDate.getUTCHours(),
  680. minutes = this.viewDate.getUTCMinutes(),
  681. seconds = this.viewDate.getUTCSeconds();
  682. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  683. } else {
  684. this.viewDate.setUTCDate(1);
  685. var year = parseInt(target.text(), 10) || 0;
  686. this.viewDate.setUTCFullYear(year);
  687. this.element.trigger({
  688. type: 'changeYear',
  689. date: this.viewDate
  690. });
  691. }
  692. } else if (target.is('.hour')) {
  693. var hours = parseInt(target.text(), 10) || 0;
  694. var year = this.viewDate.getUTCFullYear(),
  695. month = this.viewDate.getUTCMonth(),
  696. day = this.viewDate.getUTCDate(),
  697. minutes = this.viewDate.getUTCMinutes(),
  698. seconds = this.viewDate.getUTCSeconds();
  699. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  700. } else if (target.is('.minute')) {
  701. var minutes = parseInt(target.text().substr(target.text().indexOf(':') + 1), 10) || 0;
  702. var year = this.viewDate.getUTCFullYear(),
  703. month = this.viewDate.getUTCMonth(),
  704. day = this.viewDate.getUTCDate(),
  705. hours = this.viewDate.getUTCHours(),
  706. seconds = this.viewDate.getUTCSeconds();
  707. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  708. }
  709. if (this.viewMode != 0) {
  710. var oldViewMode = this.viewMode;
  711. this.showMode(-1);
  712. this.fill();
  713. if (oldViewMode == this.viewMode && this.autoclose) {
  714. this.hide();
  715. }
  716. } else {
  717. this.fill();
  718. if (this.autoclose) {
  719. this.hide();
  720. }
  721. }
  722. }
  723. break;
  724. case 'td':
  725. if (target.is('.day') && !target.is('.disabled')) {
  726. var day = parseInt(target.text(), 10) || 1;
  727. var year = this.viewDate.getUTCFullYear(),
  728. month = this.viewDate.getUTCMonth(),
  729. hours = this.viewDate.getUTCHours(),
  730. minutes = this.viewDate.getUTCMinutes(),
  731. seconds = this.viewDate.getUTCSeconds();
  732. if (target.is('.old')) {
  733. if (month === 0) {
  734. month = 11;
  735. year -= 1;
  736. } else {
  737. month -= 1;
  738. }
  739. } else if (target.is('.new')) {
  740. if (month == 11) {
  741. month = 0;
  742. year += 1;
  743. } else {
  744. month += 1;
  745. }
  746. }
  747. this._setDate(UTCDate(year, month, day, hours, minutes, seconds, 0));
  748. }
  749. var oldViewMode = this.viewMode;
  750. this.showMode(-1);
  751. this.fill();
  752. if (oldViewMode == this.viewMode && this.autoclose) {
  753. this.hide();
  754. }
  755. break;
  756. }
  757. }
  758. },
  759. _setDate: function(date, which) {
  760. if (!which || which == 'date')
  761. this.date = date;
  762. if (!which || which == 'view')
  763. this.viewDate = date;
  764. this.fill();
  765. this.setValue();
  766. this.element.trigger({
  767. type: 'changeDate',
  768. date: this.date
  769. });
  770. var element;
  771. if (this.isInput) {
  772. element = this.element;
  773. } else if (this.component) {
  774. element = this.element.find('input');
  775. }
  776. if (element) {
  777. element.change();
  778. if (this.autoclose && (!which || which == 'date')) {
  779. // this.hide();
  780. }
  781. }
  782. },
  783. moveHour: function(date, dir) {
  784. if (!dir) return date;
  785. var new_date = new Date(date.valueOf());
  786. dir = dir > 0 ? 1 : -1;
  787. new_date.setUTCHours(new_date.getUTCHours() + dir);
  788. return new_date;
  789. },
  790. moveDate: function(date, dir) {
  791. if (!dir) return date;
  792. var new_date = new Date(date.valueOf());
  793. dir = dir > 0 ? 1 : -1;
  794. new_date.setUTCDate(new_date.getUTCDate() + dir);
  795. return new_date;
  796. },
  797. moveMonth: function(date, dir) {
  798. if (!dir) return date;
  799. var new_date = new Date(date.valueOf()),
  800. day = new_date.getUTCDate(),
  801. month = new_date.getUTCMonth(),
  802. mag = Math.abs(dir),
  803. new_month, test;
  804. dir = dir > 0 ? 1 : -1;
  805. if (mag == 1) {
  806. test = dir == -1
  807. // If going back one month, make sure month is not current month
  808. // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
  809. ?
  810. function() {
  811. return new_date.getUTCMonth() == month;
  812. }
  813. // If going forward one month, make sure month is as expected
  814. // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
  815. :
  816. function() {
  817. return new_date.getUTCMonth() != new_month;
  818. };
  819. new_month = month + dir;
  820. new_date.setUTCMonth(new_month);
  821. // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
  822. if (new_month < 0 || new_month > 11)
  823. new_month = (new_month + 12) % 12;
  824. } else {
  825. // For magnitudes >1, move one month at a time...
  826. for (var i = 0; i < mag; i++)
  827. // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
  828. new_date = this.moveMonth(new_date, dir);
  829. // ...then reset the day, keeping it in the new month
  830. new_month = new_date.getUTCMonth();
  831. new_date.setUTCDate(day);
  832. test = function() {
  833. return new_month != new_date.getUTCMonth();
  834. };
  835. }
  836. // Common date-resetting loop -- if date is beyond end of month, make it
  837. // end of month
  838. while (test()) {
  839. new_date.setUTCDate(--day);
  840. new_date.setUTCMonth(new_month);
  841. }
  842. return new_date;
  843. },
  844. moveYear: function(date, dir) {
  845. return this.moveMonth(date, dir * 12);
  846. },
  847. dateWithinRange: function(date) {
  848. return date >= this.startDate && date <= this.endDate;
  849. },
  850. keydown: function(e) {
  851. if (this.picker.is(':not(:visible)')) {
  852. if (e.keyCode == 27) // allow escape to hide and re-show picker
  853. this.show();
  854. return;
  855. }
  856. var dateChanged = false,
  857. dir, day, month,
  858. newDate, newViewDate;
  859. switch (e.keyCode) {
  860. case 27: // escape
  861. this.hide();
  862. e.preventDefault();
  863. break;
  864. case 37: // left
  865. case 39: // right
  866. if (!this.keyboardNavigation) break;
  867. dir = e.keyCode == 37 ? -1 : 1;
  868. if (e.ctrlKey) {
  869. newDate = this.moveYear(this.date, dir);
  870. newViewDate = this.moveYear(this.viewDate, dir);
  871. } else if (e.shiftKey) {
  872. newDate = this.moveMonth(this.date, dir);
  873. newViewDate = this.moveMonth(this.viewDate, dir);
  874. } else {
  875. newDate = new Date(this.date.valueOf());
  876. newDate.setUTCDate(this.date.getUTCDate() + dir);
  877. newViewDate = new Date(this.viewDate.valueOf());
  878. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir);
  879. }
  880. if (this.dateWithinRange(newDate)) {
  881. this.date = newDate;
  882. this.viewDate = newViewDate;
  883. this.setValue();
  884. this.update();
  885. e.preventDefault();
  886. dateChanged = true;
  887. }
  888. break;
  889. case 38: // up
  890. case 40: // down
  891. if (!this.keyboardNavigation) break;
  892. dir = e.keyCode == 38 ? -1 : 1;
  893. if (e.ctrlKey) {
  894. newDate = this.moveYear(this.date, dir);
  895. newViewDate = this.moveYear(this.viewDate, dir);
  896. } else if (e.shiftKey) {
  897. newDate = this.moveMonth(this.date, dir);
  898. newViewDate = this.moveMonth(this.viewDate, dir);
  899. } else {
  900. newDate = new Date(this.date.valueOf());
  901. newDate.setUTCDate(this.date.getUTCDate() + dir * 7);
  902. newViewDate = new Date(this.viewDate.valueOf());
  903. newViewDate.setUTCDate(this.viewDate.getUTCDate() + dir * 7);
  904. }
  905. if (this.dateWithinRange(newDate)) {
  906. this.date = newDate;
  907. this.viewDate = newViewDate;
  908. this.setValue();
  909. this.update();
  910. e.preventDefault();
  911. dateChanged = true;
  912. }
  913. break;
  914. case 13: // enter
  915. this.hide();
  916. e.preventDefault();
  917. break;
  918. case 9: // tab
  919. this.hide();
  920. break;
  921. }
  922. if (dateChanged) {
  923. this.element.trigger({
  924. type: 'changeDate',
  925. date: this.date
  926. });
  927. var element;
  928. if (this.isInput) {
  929. element = this.element;
  930. } else if (this.component) {
  931. element = this.element.find('input');
  932. }
  933. if (element) {
  934. element.change();
  935. }
  936. }
  937. },
  938. showMode: function(dir) {
  939. if (dir) {
  940. var newViewMode = Math.max(0, Math.min(DPGlobal.modes.length - 1, this.viewMode + dir));
  941. if (newViewMode >= this.minView && newViewMode <= this.maxView) {
  942. this.viewMode = newViewMode;
  943. }
  944. }
  945. /*
  946. vitalets: fixing bug of very special conditions:
  947. jquery 1.7.1 + webkit + show inline datepicker in bootstrap popover.
  948. Method show() does not set display css correctly and datepicker is not shown.
  949. Changed to .css('display', 'block') solve the problem.
  950. See https://github.com/vitalets/x-editable/issues/37
  951. In jquery 1.7.2+ everything works fine.
  952. */
  953. //this.picker.find('>div').hide().filter('.datepicker-'+DPGlobal.modes[this.viewMode].clsName).show();
  954. this.picker.find('>div').hide().filter('.datepicker-' + DPGlobal.modes[this.viewMode].clsName).css('display', 'block');
  955. this.updateNavArrows();
  956. },
  957. reset: function(e) {
  958. this._setDate(null, 'date');
  959. }
  960. };
  961. $.fn.fdatepicker = function(option) {
  962. var args = Array.apply(null, arguments);
  963. args.shift();
  964. return this.each(function() {
  965. var $this = $(this),
  966. data = $this.data('datepicker'),
  967. options = typeof option == 'object' && option;
  968. if (!data) {
  969. $this.data('datepicker', (data = new Datepicker(this, $.extend({}, $.fn.fdatepicker.defaults, options))));
  970. }
  971. if (typeof option == 'string' && typeof data[option] == 'function') {
  972. data[option].apply(data, args);
  973. }
  974. });
  975. };
  976. $.fn.fdatepicker.defaults = {
  977. onRender: function(date) {
  978. return '';
  979. }
  980. };
  981. $.fn.fdatepicker.Constructor = Datepicker;
  982. var dates = $.fn.fdatepicker.dates = {
  983. 'en': {
  984. days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
  985. daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
  986. daysMin: ["日", "一", "二", "三", "四", "五", "六"],
  987. months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
  988. monthsShort: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
  989. today: '今天',
  990. titleFormat: "MM yyyy"
  991. }
  992. };
  993. var DPGlobal = {
  994. modes: [{
  995. clsName: 'minutes',
  996. navFnc: 'Hours',
  997. navStep: 1
  998. }, {
  999. clsName: 'hours',
  1000. navFnc: 'Date',
  1001. navStep: 1
  1002. }, {
  1003. clsName: 'days',
  1004. navFnc: 'Month',
  1005. navStep: 1
  1006. }, {
  1007. clsName: 'months',
  1008. navFnc: 'FullYear',
  1009. navStep: 1
  1010. }, {
  1011. clsName: 'years',
  1012. navFnc: 'FullYear',
  1013. navStep: 10
  1014. }],
  1015. isLeapYear: function(year) {
  1016. return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
  1017. },
  1018. getDaysInMonth: function(year, month) {
  1019. return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
  1020. },
  1021. validParts: /hh?|ii?|ss?|dd?|mm?|MM?|yy(?:yy)?/g,
  1022. nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
  1023. parseFormat: function(format) {
  1024. // IE treats \0 as a string end in inputs (truncating the value),
  1025. // so it's a bad format delimiter, anyway
  1026. var separators = format.replace(this.validParts, '\0').split('\0'),
  1027. parts = format.match(this.validParts);
  1028. if (!separators || !separators.length || !parts || parts.length === 0) {
  1029. throw new Error("Invalid date format.");
  1030. }
  1031. return {
  1032. separators: separators,
  1033. parts: parts
  1034. };
  1035. },
  1036. parseDate: function(date, format, language) {
  1037. if (date instanceof Date) return new Date(date.valueOf() - date.getTimezoneOffset() * 60000);
  1038. if (/^\d{4}\-\d{1,2}\-\d{1,2}$/.test(date)) {
  1039. format = this.parseFormat('yyyy-mm-dd');
  1040. }
  1041. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}$/.test(date)) {
  1042. format = this.parseFormat('yyyy-mm-dd hh:ii');
  1043. }
  1044. if (/^\d{4}\-\d{1,2}\-\d{1,2}[T ]\d{1,2}\:\d{1,2}\:\d{1,2}[Z]{0,1}$/.test(date)) {
  1045. format = this.parseFormat('yyyy-mm-dd hh:ii:ss');
  1046. }
  1047. if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
  1048. var part_re = /([-+]\d+)([dmwy])/,
  1049. parts = date.match(/([-+]\d+)([dmwy])/g),
  1050. part, dir;
  1051. date = new Date();
  1052. for (var i = 0; i < parts.length; i++) {
  1053. part = part_re.exec(parts[i]);
  1054. dir = parseInt(part[1]);
  1055. switch (part[2]) {
  1056. case 'd':
  1057. date.setUTCDate(date.getUTCDate() + dir);
  1058. break;
  1059. case 'm':
  1060. date = Datetimepicker.prototype.moveMonth.call(Datetimepicker.prototype, date, dir);
  1061. break;
  1062. case 'w':
  1063. date.setUTCDate(date.getUTCDate() + dir * 7);
  1064. break;
  1065. case 'y':
  1066. date = Datetimepicker.prototype.moveYear.call(Datetimepicker.prototype, date, dir);
  1067. break;
  1068. }
  1069. }
  1070. return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
  1071. }
  1072. var parts = date && date.match(this.nonpunctuation) || [],
  1073. date = new Date(),
  1074. parsed = {},
  1075. setters_order = ['hh', 'h', 'ii', 'i', 'ss', 's', 'yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
  1076. setters_map = {
  1077. hh: function(d, v) {
  1078. return d.setUTCHours(v);
  1079. },
  1080. h: function(d, v) {
  1081. return d.setUTCHours(v);
  1082. },
  1083. ii: function(d, v) {
  1084. return d.setUTCMinutes(v);
  1085. },
  1086. i: function(d, v) {
  1087. return d.setUTCMinutes(v);
  1088. },
  1089. ss: function(d, v) {
  1090. return d.setUTCSeconds(v);
  1091. },
  1092. s: function(d, v) {
  1093. return d.setUTCSeconds(v);
  1094. },
  1095. yyyy: function(d, v) {
  1096. return d.setUTCFullYear(v);
  1097. },
  1098. yy: function(d, v) {
  1099. return d.setUTCFullYear(2000 + v);
  1100. },
  1101. m: function(d, v) {
  1102. v -= 1;
  1103. while (v < 0) v += 12;
  1104. v %= 12;
  1105. d.setUTCMonth(v);
  1106. while (d.getUTCMonth() != v)
  1107. d.setUTCDate(d.getUTCDate() - 1);
  1108. return d;
  1109. },
  1110. d: function(d, v) {
  1111. return d.setUTCDate(v);
  1112. }
  1113. },
  1114. val, filtered, part;
  1115. setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
  1116. setters_map['dd'] = setters_map['d'];
  1117. date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0); //date.getHours(), date.getMinutes(), date.getSeconds());
  1118. if (parts.length == format.parts.length) {
  1119. for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
  1120. val = parseInt(parts[i], 10);
  1121. part = format.parts[i];
  1122. if (isNaN(val)) {
  1123. switch (part) {
  1124. case 'MM':
  1125. filtered = $(dates[language].months).filter(function() {
  1126. var m = this.slice(0, parts[i].length),
  1127. p = parts[i].slice(0, m.length);
  1128. return m == p;
  1129. });
  1130. val = $.inArray(filtered[0], dates[language].months) + 1;
  1131. break;
  1132. case 'M':
  1133. filtered = $(dates[language].monthsShort).filter(function() {
  1134. var m = this.slice(0, parts[i].length),
  1135. p = parts[i].slice(0, m.length);
  1136. return m == p;
  1137. });
  1138. val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
  1139. break;
  1140. }
  1141. }
  1142. parsed[part] = val;
  1143. }
  1144. for (var i = 0, s; i < setters_order.length; i++) {
  1145. s = setters_order[i];
  1146. if (s in parsed && !isNaN(parsed[s]))
  1147. setters_map[s](date, parsed[s])
  1148. }
  1149. }
  1150. return date;
  1151. },
  1152. formatDate: function(date, format, language) {
  1153. if (date == null) {
  1154. return '';
  1155. }
  1156. var val = {
  1157. h: date.getUTCHours(),
  1158. i: date.getUTCMinutes(),
  1159. s: date.getUTCSeconds(),
  1160. d: date.getUTCDate(),
  1161. m: date.getUTCMonth() + 1,
  1162. M: dates[language].monthsShort[date.getUTCMonth()],
  1163. MM: dates[language].months[date.getUTCMonth()],
  1164. yy: date.getUTCFullYear().toString().substring(2),
  1165. yyyy: date.getUTCFullYear()
  1166. };
  1167. val.hh = (val.h < 10 ? '0' : '') + val.h;
  1168. val.ii = (val.i < 10 ? '0' : '') + val.i;
  1169. val.ss = (val.s < 10 ? '0' : '') + val.s;
  1170. val.dd = (val.d < 10 ? '0' : '') + val.d;
  1171. val.mm = (val.m < 10 ? '0' : '') + val.m;
  1172. var date = [],
  1173. seps = $.extend([], format.separators);
  1174. for (var i = 0, cnt = format.parts.length; i < cnt; i++) {
  1175. if (seps.length)
  1176. date.push(seps.shift())
  1177. date.push(val[format.parts[i]]);
  1178. }
  1179. return date.join('');
  1180. },
  1181. convertViewMode: function(viewMode) {
  1182. switch (viewMode) {
  1183. case 4:
  1184. case 'decade':
  1185. viewMode = 4;
  1186. break;
  1187. case 3:
  1188. case 'year':
  1189. viewMode = 3;
  1190. break;
  1191. case 2:
  1192. case 'month':
  1193. viewMode = 2;
  1194. break;
  1195. case 1:
  1196. case 'day':
  1197. viewMode = 1;
  1198. break;
  1199. case 0:
  1200. case 'hour':
  1201. viewMode = 0;
  1202. break;
  1203. }
  1204. return viewMode;
  1205. },
  1206. headTemplate: '<thead>' +
  1207. '<tr>' +
  1208. '<th class="prev"><i class="fa fa-chevron-left fi-arrow-left"/></th>' +
  1209. '<th colspan="5" class="date-switch"></th>' +
  1210. '<th class="next"><i class="fa fa-chevron-right fi-arrow-right"/></th>' +
  1211. '</tr>' +
  1212. '</thead>',
  1213. contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
  1214. footTemplate: '<tfoot><tr><th colspan="7" class="today"></th></tr></tfoot>'
  1215. };
  1216. DPGlobal.template = '<div class="datepicker">' +
  1217. '<div class="datepicker-minutes">' +
  1218. '<table class=" table-condensed">' +
  1219. DPGlobal.headTemplate +
  1220. DPGlobal.contTemplate +
  1221. DPGlobal.footTemplate +
  1222. '</table>' +
  1223. '</div>' +
  1224. '<div class="datepicker-hours">' +
  1225. '<table class=" table-condensed">' +
  1226. DPGlobal.headTemplate +
  1227. DPGlobal.contTemplate +
  1228. DPGlobal.footTemplate +
  1229. '</table>' +
  1230. '</div>' +
  1231. '<div class="datepicker-days">' +
  1232. '<table class=" table-condensed">' +
  1233. DPGlobal.headTemplate +
  1234. '<tbody></tbody>' +
  1235. DPGlobal.footTemplate +
  1236. '</table>' +
  1237. '</div>' +
  1238. '<div class="datepicker-months">' +
  1239. '<table class="table-condensed">' +
  1240. DPGlobal.headTemplate +
  1241. DPGlobal.contTemplate +
  1242. DPGlobal.footTemplate +
  1243. '</table>' +
  1244. '</div>' +
  1245. '<div class="datepicker-years">' +
  1246. '<table class="table-condensed">' +
  1247. DPGlobal.headTemplate +
  1248. DPGlobal.contTemplate +
  1249. DPGlobal.footTemplate +
  1250. '</table>' +
  1251. '</div>' +
  1252. '<a class="button datepicker-close tiny alert right" style="width:auto;"><i class="fa fa-remove fa-times fi-x"></i></a>' +
  1253. '</div>';
  1254. $.fn.fdatepicker.DPGlobal = DPGlobal;
  1255. }(window.jQuery);