bootstrap-datetimepicker.js 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  1. /*! version : 4.17.47
  2. =========================================================
  3. bootstrap-datetimejs
  4. https://github.com/Eonasdan/bootstrap-datetimepicker
  5. Copyright (c) 2015 Jonathan Peterson
  6. =========================================================
  7. */
  8. /*
  9. The MIT License (MIT)
  10. Copyright (c) 2015 Jonathan Peterson
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. */
  27. /* global define:false */
  28. /* global jQuery:false */
  29. /* global moment:false */
  30. (function(factory) {
  31. 'use strict';
  32. if (typeof define === 'function' && define.amd) {
  33. // AMD is used - Register as an anonymous module.
  34. define(['jquery', 'moment'], factory);
  35. } else if (typeof exports === 'object') {
  36. module.exports = factory(require('jquery'), require('moment'));
  37. } else {
  38. // Neither AMD nor CommonJS used. Use global variables.
  39. if (typeof jQuery === 'undefined') {
  40. throw new Error('bootstrap-datetimepicker requires jQuery to be loaded first');
  41. }
  42. if (typeof moment === 'undefined') {
  43. throw new Error('bootstrap-datetimepicker requires Moment.js to be loaded first');
  44. }
  45. factory(jQuery, moment);
  46. }
  47. }(function($, moment) {
  48. 'use strict';
  49. if (!moment) {
  50. throw new Error('bootstrap-datetimepicker requires Moment.js to be loaded first');
  51. }
  52. var dateTimePicker = function(element, options) {
  53. var picker = {};
  54. var date;
  55. var viewDate;
  56. var unset = true;
  57. var input;
  58. var component = false;
  59. var widget = false;
  60. var use24Hours;
  61. var minViewModeNumber = 0;
  62. var actualFormat;
  63. var parseFormats;
  64. var currentViewMode;
  65. var datePickerModes = [
  66. {
  67. clsName: 'days',
  68. navFnc: 'M',
  69. navStep: 1
  70. },
  71. {
  72. clsName: 'months',
  73. navFnc: 'y',
  74. navStep: 1
  75. },
  76. {
  77. clsName: 'years',
  78. navFnc: 'y',
  79. navStep: 10
  80. },
  81. {
  82. clsName: 'decades',
  83. navFnc: 'y',
  84. navStep: 100
  85. }
  86. ];
  87. var viewModes = ['days', 'months', 'years', 'decades'];
  88. var verticalModes = ['top', 'bottom', 'auto'];
  89. var horizontalModes = ['left', 'right', 'auto'];
  90. var toolbarPlacements = ['default', 'top', 'bottom'];
  91. var keyMap = {
  92. 'up': 38,
  93. 38: 'up',
  94. 'down': 40,
  95. 40: 'down',
  96. 'left': 37,
  97. 37: 'left',
  98. 'right': 39,
  99. 39: 'right',
  100. 'tab': 9,
  101. 9: 'tab',
  102. 'escape': 27,
  103. 27: 'escape',
  104. 'enter': 13,
  105. 13: 'enter',
  106. 'pageUp': 33,
  107. 33: 'pageUp',
  108. 'pageDown': 34,
  109. 34: 'pageDown',
  110. 'shift': 16,
  111. 16: 'shift',
  112. 'control': 17,
  113. 17: 'control',
  114. 'space': 32,
  115. 32: 'space',
  116. 't': 84,
  117. 84: 't',
  118. 'delete': 46,
  119. 46: 'delete'
  120. };
  121. var keyState = {};
  122. /** ******************************************************************************
  123. *
  124. * Private functions
  125. *
  126. ********************************************************************************/
  127. var hasTimeZone = function() {
  128. return moment.tz !== undefined && options.timeZone !== undefined && options.timeZone !== null && options.timeZone !== '';
  129. };
  130. var getMoment = function(d) {
  131. var returnMoment;
  132. if (d === undefined || d === null) {
  133. returnMoment = moment(); // TODO should this use format? and locale?
  134. } else if (moment.isDate(d) || moment.isMoment(d)) {
  135. // If the date that is passed in is already a Date() or moment() object,
  136. // pass it directly to moment.
  137. returnMoment = moment(d);
  138. } else if (hasTimeZone()) { // There is a string to parse and a default time zone
  139. // parse with the tz function which takes a default time zone if it is not in the format string
  140. returnMoment = moment.tz(d, parseFormats, options.useStrict, options.timeZone);
  141. } else {
  142. returnMoment = moment(d, parseFormats, options.useStrict);
  143. }
  144. if (hasTimeZone()) {
  145. returnMoment.tz(options.timeZone);
  146. }
  147. return returnMoment;
  148. };
  149. var isEnabled = function(granularity) {
  150. if (typeof granularity !== 'string' || granularity.length > 1) {
  151. throw new TypeError('isEnabled expects a single character string parameter');
  152. }
  153. switch (granularity) {
  154. case 'y':
  155. return actualFormat.indexOf('Y') !== -1;
  156. case 'M':
  157. return actualFormat.indexOf('M') !== -1;
  158. case 'd':
  159. return actualFormat.toLowerCase().indexOf('d') !== -1;
  160. case 'h':
  161. case 'H':
  162. return actualFormat.toLowerCase().indexOf('h') !== -1;
  163. case 'm':
  164. return actualFormat.indexOf('m') !== -1;
  165. case 's':
  166. return actualFormat.indexOf('s') !== -1;
  167. default:
  168. return false;
  169. }
  170. };
  171. var hasTime = function() {
  172. return (isEnabled('h') || isEnabled('m') || isEnabled('s'));
  173. };
  174. var hasDate = function() {
  175. return (isEnabled('y') || isEnabled('M') || isEnabled('d'));
  176. };
  177. var getDatePickerTemplate = function() {
  178. var headTemplate = $('<thead>')
  179. .append($('<tr>')
  180. .append($('<th>').addClass('prev').attr('data-action', 'previous')
  181. .append($('<span>').addClass(options.icons.previous))
  182. )
  183. .append($('<th>').addClass('picker-switch').attr('data-action', 'pickerSwitch').attr('colspan', (options.calendarWeeks ? '6' : '5')))
  184. .append($('<th>').addClass('next').attr('data-action', 'next')
  185. .append($('<span>').addClass(options.icons.next))
  186. )
  187. );
  188. var contTemplate = $('<tbody>')
  189. .append($('<tr>')
  190. .append($('<td>').attr('colspan', (options.calendarWeeks ? '8' : '7')))
  191. );
  192. return [
  193. $('<div>').addClass('datepicker-days')
  194. .append($('<table>').addClass('table-condensed')
  195. .append(headTemplate)
  196. .append($('<tbody>'))
  197. ),
  198. $('<div>').addClass('datepicker-months')
  199. .append($('<table>').addClass('table-condensed')
  200. .append(headTemplate.clone())
  201. .append(contTemplate.clone())
  202. ),
  203. $('<div>').addClass('datepicker-years')
  204. .append($('<table>').addClass('table-condensed')
  205. .append(headTemplate.clone())
  206. .append(contTemplate.clone())
  207. ),
  208. $('<div>').addClass('datepicker-decades')
  209. .append($('<table>').addClass('table-condensed')
  210. .append(headTemplate.clone())
  211. .append(contTemplate.clone())
  212. )
  213. ];
  214. };
  215. var getTimePickerMainTemplate = function() {
  216. var topRow = $('<tr>');
  217. var middleRow = $('<tr>');
  218. var bottomRow = $('<tr>');
  219. if (isEnabled('h')) {
  220. topRow.append($('<td>')
  221. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.incrementHour }).addClass('btn').attr('data-action', 'incrementHours').append($('<span>').addClass(options.icons.up))));
  222. middleRow.append($('<td>')
  223. .append($('<span>').addClass('timepicker-hour').attr({ 'data-time-component': 'hours', 'title': options.tooltips.pickHour }).attr('data-action', 'showHours')));
  224. bottomRow.append($('<td>')
  225. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.decrementHour }).addClass('btn').attr('data-action', 'decrementHours').append($('<span>').addClass(options.icons.down))));
  226. }
  227. if (isEnabled('m')) {
  228. if (isEnabled('h')) {
  229. topRow.append($('<td>').addClass('separator'));
  230. middleRow.append($('<td>').addClass('separator').html(':'));
  231. bottomRow.append($('<td>').addClass('separator'));
  232. }
  233. topRow.append($('<td>')
  234. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.incrementMinute }).addClass('btn').attr('data-action', 'incrementMinutes')
  235. .append($('<span>').addClass(options.icons.up))));
  236. middleRow.append($('<td>')
  237. .append($('<span>').addClass('timepicker-minute').attr({ 'data-time-component': 'minutes', 'title': options.tooltips.pickMinute }).attr('data-action', 'showMinutes')));
  238. bottomRow.append($('<td>')
  239. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.decrementMinute }).addClass('btn').attr('data-action', 'decrementMinutes')
  240. .append($('<span>').addClass(options.icons.down))));
  241. }
  242. if (isEnabled('s')) {
  243. if (isEnabled('m')) {
  244. topRow.append($('<td>').addClass('separator'));
  245. middleRow.append($('<td>').addClass('separator').html(':'));
  246. bottomRow.append($('<td>').addClass('separator'));
  247. }
  248. topRow.append($('<td>')
  249. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.incrementSecond }).addClass('btn').attr('data-action', 'incrementSeconds')
  250. .append($('<span>').addClass(options.icons.up))));
  251. middleRow.append($('<td>')
  252. .append($('<span>').addClass('timepicker-second').attr({ 'data-time-component': 'seconds', 'title': options.tooltips.pickSecond }).attr('data-action', 'showSeconds')));
  253. bottomRow.append($('<td>')
  254. .append($('<a>').attr({ href: '#', tabindex: '-1', 'title': options.tooltips.decrementSecond }).addClass('btn').attr('data-action', 'decrementSeconds')
  255. .append($('<span>').addClass(options.icons.down))));
  256. }
  257. if (!use24Hours) {
  258. topRow.append($('<td>').addClass('separator'));
  259. middleRow.append($('<td>')
  260. .append($('<button>').addClass('btn btn-primary').attr({ 'data-action': 'togglePeriod', tabindex: '-1', 'title': options.tooltips.togglePeriod })));
  261. bottomRow.append($('<td>').addClass('separator'));
  262. }
  263. return $('<div>').addClass('timepicker-picker')
  264. .append($('<table>').addClass('table-condensed')
  265. .append([topRow, middleRow, bottomRow]));
  266. };
  267. var getTimePickerTemplate = function() {
  268. var hoursView = $('<div>').addClass('timepicker-hours')
  269. .append($('<table>').addClass('table-condensed'));
  270. var minutesView = $('<div>').addClass('timepicker-minutes')
  271. .append($('<table>').addClass('table-condensed'));
  272. var secondsView = $('<div>').addClass('timepicker-seconds')
  273. .append($('<table>').addClass('table-condensed'));
  274. var ret = [getTimePickerMainTemplate()];
  275. if (isEnabled('h')) {
  276. ret.push(hoursView);
  277. }
  278. if (isEnabled('m')) {
  279. ret.push(minutesView);
  280. }
  281. if (isEnabled('s')) {
  282. ret.push(secondsView);
  283. }
  284. return ret;
  285. };
  286. var getToolbar = function() {
  287. var row = [];
  288. if (options.showTodayButton) {
  289. row.push($('<td>').append($('<a>').attr({ 'data-action': 'today', 'title': options.tooltips.today }).append($('<span>').addClass(options.icons.today))));
  290. }
  291. if (!options.sideBySide && hasDate() && hasTime()) {
  292. row.push($('<td>').append($('<a>').attr({ 'data-action': 'togglePicker', 'title': options.tooltips.selectTime }).append($('<span>').addClass(options.icons.time))));
  293. }
  294. if (options.showClear) {
  295. row.push($('<td>').append($('<a>').attr({ 'data-action': 'clear', 'title': options.tooltips.clear }).append($('<span>').addClass(options.icons.clear))));
  296. }
  297. if (options.showClose) {
  298. row.push($('<td>').append($('<a>').attr({ 'data-action': 'close', 'title': options.tooltips.close }).append($('<span>').addClass(options.icons.close))));
  299. }
  300. return $('<table>').addClass('table-condensed').append($('<tbody>').append($('<tr>').append(row)));
  301. };
  302. var getTemplate = function() {
  303. var template = $('<div>').addClass('bootstrap-datetimepicker-widget dropdown-menu');
  304. var dateView = $('<div>').addClass('datepicker').append(getDatePickerTemplate());
  305. var timeView = $('<div>').addClass('timepicker').append(getTimePickerTemplate());
  306. var content = $('<ul>').addClass('list-unstyled');
  307. var toolbar = $('<li>').addClass('picker-switch' + (options.collapse ? ' accordion-toggle' : '')).append(getToolbar());
  308. if (options.inline) {
  309. template.removeClass('dropdown-menu');
  310. }
  311. if (use24Hours) {
  312. template.addClass('usetwentyfour');
  313. }
  314. if (isEnabled('s') && !use24Hours) {
  315. template.addClass('wider');
  316. }
  317. if (options.sideBySide && hasDate() && hasTime()) {
  318. template.addClass('timepicker-sbs');
  319. if (options.toolbarPlacement === 'top') {
  320. template.append(toolbar);
  321. }
  322. template.append(
  323. $('<div>').addClass('row')
  324. .append(dateView.addClass('col-md-6'))
  325. .append(timeView.addClass('col-md-6'))
  326. );
  327. if (options.toolbarPlacement === 'bottom') {
  328. template.append(toolbar);
  329. }
  330. return template;
  331. }
  332. if (options.toolbarPlacement === 'top') {
  333. content.append(toolbar);
  334. }
  335. if (hasDate()) {
  336. content.append($('<li>').addClass((options.collapse && hasTime() ? 'collapse in' : '')).append(dateView));
  337. }
  338. if (options.toolbarPlacement === 'default') {
  339. content.append(toolbar);
  340. }
  341. if (hasTime()) {
  342. content.append($('<li>').addClass((options.collapse && hasDate() ? 'collapse' : '')).append(timeView));
  343. }
  344. if (options.toolbarPlacement === 'bottom') {
  345. content.append(toolbar);
  346. }
  347. return template.append(content);
  348. };
  349. var dataToOptions = function() {
  350. var eData;
  351. var dataOptions = {};
  352. if (element.is('input') || options.inline) {
  353. eData = element.data();
  354. } else {
  355. eData = element.find('input').data();
  356. }
  357. if (eData.dateOptions && eData.dateOptions instanceof Object) {
  358. dataOptions = $.extend(true, dataOptions, eData.dateOptions);
  359. }
  360. $.each(options, function(key) {
  361. var attributeName = 'date' + key.charAt(0).toUpperCase() + key.slice(1);
  362. if (eData[attributeName] !== undefined) {
  363. dataOptions[key] = eData[attributeName];
  364. }
  365. });
  366. return dataOptions;
  367. };
  368. var place = function() {
  369. var position = (component || element).position();
  370. var offset = (component || element).offset();
  371. var vertical = options.widgetPositioning.vertical;
  372. var horizontal = options.widgetPositioning.horizontal;
  373. var parent;
  374. if (options.widgetParent) {
  375. parent = options.widgetParent.append(widget);
  376. } else if (element.is('input')) {
  377. parent = element.after(widget).parent();
  378. } else if (options.inline) {
  379. parent = element.append(widget);
  380. return;
  381. } else {
  382. parent = element;
  383. element.children().first().after(widget);
  384. }
  385. // Top and bottom logic
  386. if (vertical === 'auto') {
  387. if (offset.top + widget.height() * 1.5 >= $(window).height() + $(window).scrollTop() &&
  388. widget.height() + element.outerHeight() < offset.top) {
  389. vertical = 'top';
  390. } else {
  391. vertical = 'bottom';
  392. }
  393. }
  394. // Left and right logic
  395. if (horizontal === 'auto') {
  396. if (parent.width() < offset.left + widget.outerWidth() / 2 &&
  397. offset.left + widget.outerWidth() > $(window).width()) {
  398. horizontal = 'right';
  399. } else {
  400. horizontal = 'left';
  401. }
  402. }
  403. if (vertical === 'top') {
  404. widget.addClass('top').removeClass('bottom');
  405. } else {
  406. widget.addClass('bottom').removeClass('top');
  407. }
  408. if (horizontal === 'right') {
  409. widget.addClass('pull-right');
  410. } else {
  411. widget.removeClass('pull-right');
  412. }
  413. // find the first parent element that has a non-static css positioning
  414. if (parent.css('position') === 'static') {
  415. parent = parent.parents().filter(function() {
  416. return $(this).css('position') !== 'static';
  417. }).first();
  418. }
  419. if (parent.length === 0) {
  420. throw new Error('datetimepicker component should be placed within a non-static positioned container');
  421. }
  422. widget.css({
  423. top: vertical === 'top' ? 'auto' : position.top + element.outerHeight(),
  424. bottom: vertical === 'top' ? parent.outerHeight() - (parent === element ? 0 : position.top) : 'auto',
  425. left: horizontal === 'left' ? (parent === element ? 0 : position.left) : 'auto',
  426. right: horizontal === 'left' ? 'auto' : parent.outerWidth() - element.outerWidth() - (parent === element ? 0 : position.left)
  427. });
  428. };
  429. var notifyEvent = function(e) {
  430. if (e.type === 'dp.change' && ((e.date && e.date.isSame(e.oldDate)) || (!e.date && !e.oldDate))) {
  431. return;
  432. }
  433. element.trigger(e);
  434. };
  435. var viewUpdate = function(e) {
  436. if (e === 'y') {
  437. e = 'YYYY';
  438. }
  439. notifyEvent({
  440. type: 'dp.update',
  441. change: e,
  442. viewDate: viewDate.clone()
  443. });
  444. };
  445. var showMode = function(dir) {
  446. if (!widget) {
  447. return;
  448. }
  449. if (dir) {
  450. currentViewMode = Math.max(minViewModeNumber, Math.min(3, currentViewMode + dir));
  451. }
  452. widget.find('.datepicker > div').hide().filter('.datepicker-' + datePickerModes[currentViewMode].clsName).show();
  453. };
  454. var fillDow = function() {
  455. var row = $('<tr>');
  456. var currentDate = viewDate.clone().startOf('w').startOf('d');
  457. if (options.calendarWeeks === true) {
  458. row.append($('<th>').addClass('cw').text('#'));
  459. }
  460. while (currentDate.isBefore(viewDate.clone().endOf('w'))) {
  461. row.append($('<th>').addClass('dow').text(currentDate.format('dd')));
  462. currentDate.add(1, 'd');
  463. }
  464. widget.find('.datepicker-days thead').append(row);
  465. };
  466. var isInDisabledDates = function(testDate) {
  467. return options.disabledDates[testDate.format('YYYY-MM-DD')] === true;
  468. };
  469. var isInEnabledDates = function(testDate) {
  470. return options.enabledDates[testDate.format('YYYY-MM-DD')] === true;
  471. };
  472. var isInDisabledHours = function(testDate) {
  473. return options.disabledHours[testDate.format('H')] === true;
  474. };
  475. var isInEnabledHours = function(testDate) {
  476. return options.enabledHours[testDate.format('H')] === true;
  477. };
  478. var isValid = function(targetMoment, granularity) {
  479. if (!targetMoment.isValid()) {
  480. return false;
  481. }
  482. if (options.disabledDates && granularity === 'd' && isInDisabledDates(targetMoment)) {
  483. return false;
  484. }
  485. if (options.enabledDates && granularity === 'd' && !isInEnabledDates(targetMoment)) {
  486. return false;
  487. }
  488. if (options.minDate && targetMoment.isBefore(options.minDate, granularity)) {
  489. return false;
  490. }
  491. if (options.maxDate && targetMoment.isAfter(options.maxDate, granularity)) {
  492. return false;
  493. }
  494. if (options.daysOfWeekDisabled && granularity === 'd' && options.daysOfWeekDisabled.indexOf(targetMoment.day()) !== -1) {
  495. return false;
  496. }
  497. if (options.disabledHours && (granularity === 'h' || granularity === 'm' || granularity === 's') && isInDisabledHours(targetMoment)) {
  498. return false;
  499. }
  500. if (options.enabledHours && (granularity === 'h' || granularity === 'm' || granularity === 's') && !isInEnabledHours(targetMoment)) {
  501. return false;
  502. }
  503. if (options.disabledTimeIntervals && (granularity === 'h' || granularity === 'm' || granularity === 's')) {
  504. var found = false;
  505. $.each(options.disabledTimeIntervals, function() {
  506. if (targetMoment.isBetween(this[0], this[1])) {
  507. found = true;
  508. return false;
  509. }
  510. });
  511. if (found) {
  512. return false;
  513. }
  514. }
  515. return true;
  516. };
  517. var fillMonths = function() {
  518. var spans = [];
  519. var monthsShort = viewDate.clone().startOf('y').startOf('d');
  520. while (monthsShort.isSame(viewDate, 'y')) {
  521. spans.push($('<span>').attr('data-action', 'selectMonth').addClass('month').text(monthsShort.format('MMM')));
  522. monthsShort.add(1, 'M');
  523. }
  524. widget.find('.datepicker-months td').empty().append(spans);
  525. };
  526. var updateMonths = function() {
  527. var monthsView = widget.find('.datepicker-months');
  528. var monthsViewHeader = monthsView.find('th');
  529. var months = monthsView.find('tbody').find('span');
  530. monthsViewHeader.eq(0).find('span').attr('title', options.tooltips.prevYear);
  531. monthsViewHeader.eq(1).attr('title', options.tooltips.selectYear);
  532. monthsViewHeader.eq(2).find('span').attr('title', options.tooltips.nextYear);
  533. monthsView.find('.disabled').removeClass('disabled');
  534. if (!isValid(viewDate.clone().subtract(1, 'y'), 'y')) {
  535. monthsViewHeader.eq(0).addClass('disabled');
  536. }
  537. monthsViewHeader.eq(1).text(viewDate.year());
  538. if (!isValid(viewDate.clone().add(1, 'y'), 'y')) {
  539. monthsViewHeader.eq(2).addClass('disabled');
  540. }
  541. months.removeClass('active');
  542. if (date.isSame(viewDate, 'y') && !unset) {
  543. months.eq(date.month()).addClass('active');
  544. }
  545. months.each(function(index) {
  546. if (!isValid(viewDate.clone().month(index), 'M')) {
  547. $(this).addClass('disabled');
  548. }
  549. });
  550. };
  551. var updateYears = function() {
  552. var yearsView = widget.find('.datepicker-years');
  553. var yearsViewHeader = yearsView.find('th');
  554. var startYear = viewDate.clone().subtract(5, 'y');
  555. var endYear = viewDate.clone().add(6, 'y');
  556. var html = '';
  557. yearsViewHeader.eq(0).find('span').attr('title', options.tooltips.prevDecade);
  558. yearsViewHeader.eq(1).attr('title', options.tooltips.selectDecade);
  559. yearsViewHeader.eq(2).find('span').attr('title', options.tooltips.nextDecade);
  560. yearsView.find('.disabled').removeClass('disabled');
  561. if (options.minDate && options.minDate.isAfter(startYear, 'y')) {
  562. yearsViewHeader.eq(0).addClass('disabled');
  563. }
  564. yearsViewHeader.eq(1).text(startYear.year() + '-' + endYear.year());
  565. if (options.maxDate && options.maxDate.isBefore(endYear, 'y')) {
  566. yearsViewHeader.eq(2).addClass('disabled');
  567. }
  568. while (!startYear.isAfter(endYear, 'y')) {
  569. html += '<span data-action="selectYear" class="year' + (startYear.isSame(date, 'y') && !unset ? ' active' : '') + (!isValid(startYear, 'y') ? ' disabled' : '') + '">' + startYear.year() + '</span>';
  570. startYear.add(1, 'y');
  571. }
  572. yearsView.find('td').html(html);
  573. };
  574. var updateDecades = function() {
  575. var decadesView = widget.find('.datepicker-decades');
  576. var decadesViewHeader = decadesView.find('th');
  577. var startDecade = moment({ y: viewDate.year() - (viewDate.year() % 100) - 1 });
  578. var endDecade = startDecade.clone().add(100, 'y');
  579. var startedAt = startDecade.clone();
  580. var minDateDecade = false;
  581. var maxDateDecade = false;
  582. var endDecadeYear;
  583. var html = '';
  584. decadesViewHeader.eq(0).find('span').attr('title', options.tooltips.prevCentury);
  585. decadesViewHeader.eq(2).find('span').attr('title', options.tooltips.nextCentury);
  586. decadesView.find('.disabled').removeClass('disabled');
  587. if (startDecade.isSame(moment({ y: 1900 })) || (options.minDate && options.minDate.isAfter(startDecade, 'y'))) {
  588. decadesViewHeader.eq(0).addClass('disabled');
  589. }
  590. decadesViewHeader.eq(1).text(startDecade.year() + '-' + endDecade.year());
  591. if (startDecade.isSame(moment({ y: 2000 })) || (options.maxDate && options.maxDate.isBefore(endDecade, 'y'))) {
  592. decadesViewHeader.eq(2).addClass('disabled');
  593. }
  594. while (!startDecade.isAfter(endDecade, 'y')) {
  595. endDecadeYear = startDecade.year() + 12;
  596. minDateDecade = options.minDate && options.minDate.isAfter(startDecade, 'y') && options.minDate.year() <= endDecadeYear;
  597. maxDateDecade = options.maxDate && options.maxDate.isAfter(startDecade, 'y') && options.maxDate.year() <= endDecadeYear;
  598. html += '<span data-action="selectDecade" class="decade' + (date.isAfter(startDecade) && date.year() <= endDecadeYear ? ' active' : '') +
  599. (!isValid(startDecade, 'y') && !minDateDecade && !maxDateDecade ? ' disabled' : '') + '" data-selection="' + (startDecade.year() + 6) + '">' + (startDecade.year() + 1) + ' - ' + (startDecade.year() + 12) + '</span>';
  600. startDecade.add(12, 'y');
  601. }
  602. html += '<span></span><span></span><span></span>'; // push the dangling block over, at least this way it's even
  603. decadesView.find('td').html(html);
  604. decadesViewHeader.eq(1).text((startedAt.year() + 1) + '-' + (startDecade.year()));
  605. };
  606. var fillDate = function() {
  607. var daysView = widget.find('.datepicker-days');
  608. var daysViewHeader = daysView.find('th');
  609. var currentDate;
  610. var html = [];
  611. var row;
  612. var clsNames = [];
  613. var i;
  614. if (!hasDate()) {
  615. return;
  616. }
  617. daysViewHeader.eq(0).find('span').attr('title', options.tooltips.prevMonth);
  618. daysViewHeader.eq(1).attr('title', options.tooltips.selectMonth);
  619. daysViewHeader.eq(2).find('span').attr('title', options.tooltips.nextMonth);
  620. daysView.find('.disabled').removeClass('disabled');
  621. daysViewHeader.eq(1).text(viewDate.format(options.dayViewHeaderFormat));
  622. if (!isValid(viewDate.clone().subtract(1, 'M'), 'M')) {
  623. daysViewHeader.eq(0).addClass('disabled');
  624. }
  625. if (!isValid(viewDate.clone().add(1, 'M'), 'M')) {
  626. daysViewHeader.eq(2).addClass('disabled');
  627. }
  628. currentDate = viewDate.clone().startOf('M').startOf('w').startOf('d');
  629. for (i = 0; i < 42; i++) { // always display 42 days (should show 6 weeks)
  630. if (currentDate.weekday() === 0) {
  631. row = $('<tr>');
  632. if (options.calendarWeeks) {
  633. row.append('<td class="cw">' + currentDate.week() + '</td>');
  634. }
  635. html.push(row);
  636. }
  637. clsNames = ['day'];
  638. if (currentDate.isBefore(viewDate, 'M')) {
  639. clsNames.push('old');
  640. }
  641. if (currentDate.isAfter(viewDate, 'M')) {
  642. clsNames.push('new');
  643. }
  644. if (currentDate.isSame(date, 'd') && !unset) {
  645. clsNames.push('active');
  646. }
  647. if (!isValid(currentDate, 'd')) {
  648. clsNames.push('disabled');
  649. }
  650. if (currentDate.isSame(getMoment(), 'd')) {
  651. clsNames.push('today');
  652. }
  653. if (currentDate.day() === 0 || currentDate.day() === 6) {
  654. clsNames.push('weekend');
  655. }
  656. notifyEvent({
  657. type: 'dp.classify',
  658. date: currentDate,
  659. classNames: clsNames
  660. });
  661. row.append('<td data-action="selectDay" data-day="' + currentDate.format('L') + '" class="' + clsNames.join(' ') + '">' + currentDate.date() + '</td>');
  662. currentDate.add(1, 'd');
  663. }
  664. daysView.find('tbody').empty().append(html);
  665. updateMonths();
  666. updateYears();
  667. updateDecades();
  668. };
  669. var fillHours = function() {
  670. var table = widget.find('.timepicker-hours table');
  671. var currentHour = viewDate.clone().startOf('d');
  672. var html = [];
  673. var row = $('<tr>');
  674. if (viewDate.hour() > 11 && !use24Hours) {
  675. currentHour.hour(12);
  676. }
  677. while (currentHour.isSame(viewDate, 'd') && (use24Hours || (viewDate.hour() < 12 && currentHour.hour() < 12) || viewDate.hour() > 11)) {
  678. if (currentHour.hour() % 4 === 0) {
  679. row = $('<tr>');
  680. html.push(row);
  681. }
  682. row.append('<td data-action="selectHour" class="hour' + (!isValid(currentHour, 'h') ? ' disabled' : '') + '">' + currentHour.format(use24Hours ? 'HH' : 'hh') + '</td>');
  683. currentHour.add(1, 'h');
  684. }
  685. table.empty().append(html);
  686. };
  687. var fillMinutes = function() {
  688. var table = widget.find('.timepicker-minutes table');
  689. var currentMinute = viewDate.clone().startOf('h');
  690. var html = [];
  691. var row = $('<tr>');
  692. var step = options.stepping === 1 ? 5 : options.stepping;
  693. while (viewDate.isSame(currentMinute, 'h')) {
  694. if (currentMinute.minute() % (step * 4) === 0) {
  695. row = $('<tr>');
  696. html.push(row);
  697. }
  698. row.append('<td data-action="selectMinute" class="minute' + (!isValid(currentMinute, 'm') ? ' disabled' : '') + '">' + currentMinute.format('mm') + '</td>');
  699. currentMinute.add(step, 'm');
  700. }
  701. table.empty().append(html);
  702. };
  703. var fillSeconds = function() {
  704. var table = widget.find('.timepicker-seconds table');
  705. var currentSecond = viewDate.clone().startOf('m');
  706. var html = [];
  707. var row = $('<tr>');
  708. while (viewDate.isSame(currentSecond, 'm')) {
  709. if (currentSecond.second() % 20 === 0) {
  710. row = $('<tr>');
  711. html.push(row);
  712. }
  713. row.append('<td data-action="selectSecond" class="second' + (!isValid(currentSecond, 's') ? ' disabled' : '') + '">' + currentSecond.format('ss') + '</td>');
  714. currentSecond.add(5, 's');
  715. }
  716. table.empty().append(html);
  717. };
  718. var fillTime = function() {
  719. var toggle; var newDate; var timeComponents = widget.find('.timepicker span[data-time-component]');
  720. if (!use24Hours) {
  721. toggle = widget.find('.timepicker [data-action=togglePeriod]');
  722. newDate = date.clone().add((date.hours() >= 12) ? -12 : 12, 'h');
  723. toggle.text(date.format('A'));
  724. if (isValid(newDate, 'h')) {
  725. toggle.removeClass('disabled');
  726. } else {
  727. toggle.addClass('disabled');
  728. }
  729. }
  730. timeComponents.filter('[data-time-component=hours]').text(date.format(use24Hours ? 'HH' : 'hh'));
  731. timeComponents.filter('[data-time-component=minutes]').text(date.format('mm'));
  732. timeComponents.filter('[data-time-component=seconds]').text(date.format('ss'));
  733. fillHours();
  734. fillMinutes();
  735. fillSeconds();
  736. };
  737. var update = function() {
  738. if (!widget) {
  739. return;
  740. }
  741. fillDate();
  742. fillTime();
  743. };
  744. var setValue = function(targetMoment) {
  745. var oldDate = unset ? null : date;
  746. // case of calling setValue(null or false)
  747. if (!targetMoment) {
  748. unset = true;
  749. input.val('');
  750. element.data('date', '');
  751. notifyEvent({
  752. type: 'dp.change',
  753. date: false,
  754. oldDate: oldDate
  755. });
  756. update();
  757. return;
  758. }
  759. targetMoment = targetMoment.clone().locale(options.locale);
  760. if (hasTimeZone()) {
  761. targetMoment.tz(options.timeZone);
  762. }
  763. if (options.stepping !== 1) {
  764. targetMoment.minutes((Math.round(targetMoment.minutes() / options.stepping) * options.stepping)).seconds(0);
  765. while (options.minDate && targetMoment.isBefore(options.minDate)) {
  766. targetMoment.add(options.stepping, 'minutes');
  767. }
  768. }
  769. if (isValid(targetMoment)) {
  770. date = targetMoment;
  771. viewDate = date.clone();
  772. input.val(date.format(actualFormat));
  773. element.data('date', date.format(actualFormat));
  774. unset = false;
  775. update();
  776. notifyEvent({
  777. type: 'dp.change',
  778. date: date.clone(),
  779. oldDate: oldDate
  780. });
  781. } else {
  782. if (!options.keepInvalid) {
  783. input.val(unset ? '' : date.format(actualFormat));
  784. } else {
  785. notifyEvent({
  786. type: 'dp.change',
  787. date: targetMoment,
  788. oldDate: oldDate
  789. });
  790. }
  791. notifyEvent({
  792. type: 'dp.error',
  793. date: targetMoment,
  794. oldDate: oldDate
  795. });
  796. }
  797. };
  798. /**
  799. * Hides the widget. Possibly will emit dp.hide
  800. */
  801. var hide = function() {
  802. var transitioning = false;
  803. if (!widget) {
  804. return picker;
  805. }
  806. // Ignore event if in the middle of a picker transition
  807. widget.find('.collapse').each(function() {
  808. var collapseData = $(this).data('collapse');
  809. if (collapseData && collapseData.transitioning) {
  810. transitioning = true;
  811. return false;
  812. }
  813. return true;
  814. });
  815. if (transitioning) {
  816. return picker;
  817. }
  818. if (component && component.hasClass('btn')) {
  819. component.toggleClass('active');
  820. }
  821. widget.hide();
  822. $(window).off('resize', place);
  823. widget.off('click', '[data-action]');
  824. widget.off('mousedown', false);
  825. widget.remove();
  826. widget = false;
  827. notifyEvent({
  828. type: 'dp.hide',
  829. date: date.clone()
  830. });
  831. input.blur();
  832. viewDate = date.clone();
  833. return picker;
  834. };
  835. var clear = function() {
  836. setValue(null);
  837. };
  838. var parseInputDate = function(inputDate) {
  839. if (options.parseInputDate === undefined) {
  840. if (!moment.isMoment(inputDate) || inputDate instanceof Date) {
  841. inputDate = getMoment(inputDate);
  842. }
  843. } else {
  844. inputDate = options.parseInputDate(inputDate);
  845. }
  846. // inputDate.locale(options.locale);
  847. return inputDate;
  848. };
  849. /** ******************************************************************************
  850. *
  851. * Widget UI interaction functions
  852. *
  853. ********************************************************************************/
  854. var actions = {
  855. next: function() {
  856. var navFnc = datePickerModes[currentViewMode].navFnc;
  857. viewDate.add(datePickerModes[currentViewMode].navStep, navFnc);
  858. fillDate();
  859. viewUpdate(navFnc);
  860. },
  861. previous: function() {
  862. var navFnc = datePickerModes[currentViewMode].navFnc;
  863. viewDate.subtract(datePickerModes[currentViewMode].navStep, navFnc);
  864. fillDate();
  865. viewUpdate(navFnc);
  866. },
  867. pickerSwitch: function() {
  868. showMode(1);
  869. },
  870. selectMonth: function(e) {
  871. var month = $(e.target).closest('tbody').find('span').index($(e.target));
  872. viewDate.month(month);
  873. if (currentViewMode === minViewModeNumber) {
  874. setValue(date.clone().year(viewDate.year()).month(viewDate.month()));
  875. if (!options.inline) {
  876. hide();
  877. }
  878. } else {
  879. showMode(-1);
  880. fillDate();
  881. }
  882. viewUpdate('M');
  883. },
  884. selectYear: function(e) {
  885. var year = parseInt($(e.target).text(), 10) || 0;
  886. viewDate.year(year);
  887. if (currentViewMode === minViewModeNumber) {
  888. setValue(date.clone().year(viewDate.year()));
  889. if (!options.inline) {
  890. hide();
  891. }
  892. } else {
  893. showMode(-1);
  894. fillDate();
  895. }
  896. viewUpdate('YYYY');
  897. },
  898. selectDecade: function(e) {
  899. var year = parseInt($(e.target).data('selection'), 10) || 0;
  900. viewDate.year(year);
  901. if (currentViewMode === minViewModeNumber) {
  902. setValue(date.clone().year(viewDate.year()));
  903. if (!options.inline) {
  904. hide();
  905. }
  906. } else {
  907. showMode(-1);
  908. fillDate();
  909. }
  910. viewUpdate('YYYY');
  911. },
  912. selectDay: function(e) {
  913. var day = viewDate.clone();
  914. if ($(e.target).is('.old')) {
  915. day.subtract(1, 'M');
  916. }
  917. if ($(e.target).is('.new')) {
  918. day.add(1, 'M');
  919. }
  920. setValue(day.date(parseInt($(e.target).text(), 10)));
  921. if (!hasTime() && !options.keepOpen && !options.inline) {
  922. hide();
  923. }
  924. },
  925. incrementHours: function() {
  926. var newDate = date.clone().add(1, 'h');
  927. if (isValid(newDate, 'h')) {
  928. setValue(newDate);
  929. }
  930. },
  931. incrementMinutes: function() {
  932. var newDate = date.clone().add(options.stepping, 'm');
  933. if (isValid(newDate, 'm')) {
  934. setValue(newDate);
  935. }
  936. },
  937. incrementSeconds: function() {
  938. var newDate = date.clone().add(1, 's');
  939. if (isValid(newDate, 's')) {
  940. setValue(newDate);
  941. }
  942. },
  943. decrementHours: function() {
  944. var newDate = date.clone().subtract(1, 'h');
  945. if (isValid(newDate, 'h')) {
  946. setValue(newDate);
  947. }
  948. },
  949. decrementMinutes: function() {
  950. var newDate = date.clone().subtract(options.stepping, 'm');
  951. if (isValid(newDate, 'm')) {
  952. setValue(newDate);
  953. }
  954. },
  955. decrementSeconds: function() {
  956. var newDate = date.clone().subtract(1, 's');
  957. if (isValid(newDate, 's')) {
  958. setValue(newDate);
  959. }
  960. },
  961. togglePeriod: function() {
  962. setValue(date.clone().add((date.hours() >= 12) ? -12 : 12, 'h'));
  963. },
  964. togglePicker: function(e) {
  965. var $this = $(e.target);
  966. var $parent = $this.closest('ul');
  967. var expanded = $parent.find('.in');
  968. var closed = $parent.find('.collapse:not(.in)');
  969. var collapseData;
  970. if (expanded && expanded.length) {
  971. collapseData = expanded.data('collapse');
  972. if (collapseData && collapseData.transitioning) {
  973. return;
  974. }
  975. if (expanded.collapse) { // if collapse plugin is available through bootstrap.js then use it
  976. expanded.collapse('hide');
  977. closed.collapse('show');
  978. } else { // otherwise just toggle in class on the two views
  979. expanded.removeClass('in');
  980. closed.addClass('in');
  981. }
  982. if ($this.is('span')) {
  983. $this.toggleClass(options.icons.time + ' ' + options.icons.date);
  984. } else {
  985. $this.find('span').toggleClass(options.icons.time + ' ' + options.icons.date);
  986. }
  987. // NOTE: uncomment if toggled state will be restored in show()
  988. // if (component) {
  989. // component.find('span').toggleClass(options.icons.time + ' ' + options.icons.date);
  990. // }
  991. }
  992. },
  993. showPicker: function() {
  994. widget.find('.timepicker > div:not(.timepicker-picker)').hide();
  995. widget.find('.timepicker .timepicker-picker').show();
  996. },
  997. showHours: function() {
  998. widget.find('.timepicker .timepicker-picker').hide();
  999. widget.find('.timepicker .timepicker-hours').show();
  1000. },
  1001. showMinutes: function() {
  1002. widget.find('.timepicker .timepicker-picker').hide();
  1003. widget.find('.timepicker .timepicker-minutes').show();
  1004. },
  1005. showSeconds: function() {
  1006. widget.find('.timepicker .timepicker-picker').hide();
  1007. widget.find('.timepicker .timepicker-seconds').show();
  1008. },
  1009. selectHour: function(e) {
  1010. var hour = parseInt($(e.target).text(), 10);
  1011. if (!use24Hours) {
  1012. if (date.hours() >= 12) {
  1013. if (hour !== 12) {
  1014. hour += 12;
  1015. }
  1016. } else {
  1017. if (hour === 12) {
  1018. hour = 0;
  1019. }
  1020. }
  1021. }
  1022. setValue(date.clone().hours(hour));
  1023. actions.showPicker.call(picker);
  1024. },
  1025. selectMinute: function(e) {
  1026. setValue(date.clone().minutes(parseInt($(e.target).text(), 10)));
  1027. actions.showPicker.call(picker);
  1028. },
  1029. selectSecond: function(e) {
  1030. setValue(date.clone().seconds(parseInt($(e.target).text(), 10)));
  1031. actions.showPicker.call(picker);
  1032. },
  1033. clear: clear,
  1034. today: function() {
  1035. var todaysDate = getMoment();
  1036. if (isValid(todaysDate, 'd')) {
  1037. setValue(todaysDate);
  1038. }
  1039. },
  1040. close: hide
  1041. };
  1042. var doAction = function(e) {
  1043. if ($(e.currentTarget).is('.disabled')) {
  1044. return false;
  1045. }
  1046. actions[$(e.currentTarget).data('action')].apply(picker, arguments);
  1047. return false;
  1048. };
  1049. /**
  1050. * Shows the widget. Possibly will emit dp.show and dp.change
  1051. */
  1052. var show = function() {
  1053. var currentMoment;
  1054. var useCurrentGranularity = {
  1055. 'year': function(m) {
  1056. return m.month(0).date(1).hours(0).seconds(0).minutes(0);
  1057. },
  1058. 'month': function(m) {
  1059. return m.date(1).hours(0).seconds(0).minutes(0);
  1060. },
  1061. 'day': function(m) {
  1062. return m.hours(0).seconds(0).minutes(0);
  1063. },
  1064. 'hour': function(m) {
  1065. return m.seconds(0).minutes(0);
  1066. },
  1067. 'minute': function(m) {
  1068. return m.seconds(0);
  1069. }
  1070. };
  1071. if (input.prop('disabled') || (!options.ignoreReadonly && input.prop('readonly')) || widget) {
  1072. return picker;
  1073. }
  1074. if (input.val() !== undefined && input.val().trim().length !== 0) {
  1075. setValue(parseInputDate(input.val().trim()));
  1076. } else if (unset && options.useCurrent && (options.inline || (input.is('input') && input.val().trim().length === 0))) {
  1077. currentMoment = getMoment();
  1078. if (typeof options.useCurrent === 'string') {
  1079. currentMoment = useCurrentGranularity[options.useCurrent](currentMoment);
  1080. }
  1081. setValue(currentMoment);
  1082. }
  1083. widget = getTemplate();
  1084. fillDow();
  1085. fillMonths();
  1086. widget.find('.timepicker-hours').hide();
  1087. widget.find('.timepicker-minutes').hide();
  1088. widget.find('.timepicker-seconds').hide();
  1089. update();
  1090. showMode();
  1091. $(window).on('resize', place);
  1092. widget.on('click', '[data-action]', doAction); // this handles clicks on the widget
  1093. widget.on('mousedown', false);
  1094. if (component && component.hasClass('btn')) {
  1095. component.toggleClass('active');
  1096. }
  1097. place();
  1098. widget.show();
  1099. if (options.focusOnShow && !input.is(':focus')) {
  1100. input.focus();
  1101. }
  1102. notifyEvent({
  1103. type: 'dp.show'
  1104. });
  1105. return picker;
  1106. };
  1107. /**
  1108. * Shows or hides the widget
  1109. */
  1110. var toggle = function() {
  1111. return (widget ? hide() : show());
  1112. };
  1113. var keydown = function(e) {
  1114. var handler = null;
  1115. var index;
  1116. var index2;
  1117. var pressedKeys = [];
  1118. var pressedModifiers = {};
  1119. var currentKey = e.which;
  1120. var keyBindKeys;
  1121. var allModifiersPressed;
  1122. var pressed = 'p';
  1123. keyState[currentKey] = pressed;
  1124. for (index in keyState) {
  1125. if (keyState.hasOwnProperty(index) && keyState[index] === pressed) {
  1126. pressedKeys.push(index);
  1127. if (parseInt(index, 10) !== currentKey) {
  1128. pressedModifiers[index] = true;
  1129. }
  1130. }
  1131. }
  1132. for (index in options.keyBinds) {
  1133. if (options.keyBinds.hasOwnProperty(index) && typeof (options.keyBinds[index]) === 'function') {
  1134. keyBindKeys = index.split(' ');
  1135. if (keyBindKeys.length === pressedKeys.length && keyMap[currentKey] === keyBindKeys[keyBindKeys.length - 1]) {
  1136. allModifiersPressed = true;
  1137. for (index2 = keyBindKeys.length - 2; index2 >= 0; index2--) {
  1138. if (!(keyMap[keyBindKeys[index2]] in pressedModifiers)) {
  1139. allModifiersPressed = false;
  1140. break;
  1141. }
  1142. }
  1143. if (allModifiersPressed) {
  1144. handler = options.keyBinds[index];
  1145. break;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. if (handler) {
  1151. handler.call(picker, widget);
  1152. e.stopPropagation();
  1153. e.preventDefault();
  1154. }
  1155. };
  1156. var keyup = function(e) {
  1157. keyState[e.which] = 'r';
  1158. e.stopPropagation();
  1159. e.preventDefault();
  1160. };
  1161. var change = function(e) {
  1162. var val = $(e.target).val().trim();
  1163. var parsedDate = val ? parseInputDate(val) : null;
  1164. setValue(parsedDate);
  1165. e.stopImmediatePropagation();
  1166. return false;
  1167. };
  1168. var attachDatePickerElementEvents = function() {
  1169. input.on({
  1170. 'change': change,
  1171. 'blur': options.debug ? '' : hide,
  1172. 'keydown': keydown,
  1173. 'keyup': keyup,
  1174. 'focus': options.allowInputToggle ? show : ''
  1175. });
  1176. if (element.is('input')) {
  1177. input.on({
  1178. 'focus': show
  1179. });
  1180. } else if (component) {
  1181. component.on('click', toggle);
  1182. component.on('mousedown', false);
  1183. }
  1184. };
  1185. var detachDatePickerElementEvents = function() {
  1186. input.off({
  1187. 'change': change,
  1188. 'blur': blur,
  1189. 'keydown': keydown,
  1190. 'keyup': keyup,
  1191. 'focus': options.allowInputToggle ? hide : ''
  1192. });
  1193. if (element.is('input')) {
  1194. input.off({
  1195. 'focus': show
  1196. });
  1197. } else if (component) {
  1198. component.off('click', toggle);
  1199. component.off('mousedown', false);
  1200. }
  1201. };
  1202. var indexGivenDates = function(givenDatesArray) {
  1203. // Store given enabledDates and disabledDates as keys.
  1204. // This way we can check their existence in O(1) time instead of looping through whole array.
  1205. // (for example: options.enabledDates['2014-02-27'] === true)
  1206. var givenDatesIndexed = {};
  1207. $.each(givenDatesArray, function() {
  1208. var dDate = parseInputDate(this);
  1209. if (dDate.isValid()) {
  1210. givenDatesIndexed[dDate.format('YYYY-MM-DD')] = true;
  1211. }
  1212. });
  1213. return (Object.keys(givenDatesIndexed).length) ? givenDatesIndexed : false;
  1214. };
  1215. var indexGivenHours = function(givenHoursArray) {
  1216. // Store given enabledHours and disabledHours as keys.
  1217. // This way we can check their existence in O(1) time instead of looping through whole array.
  1218. // (for example: options.enabledHours['2014-02-27'] === true)
  1219. var givenHoursIndexed = {};
  1220. $.each(givenHoursArray, function() {
  1221. givenHoursIndexed[this] = true;
  1222. });
  1223. return (Object.keys(givenHoursIndexed).length) ? givenHoursIndexed : false;
  1224. };
  1225. var initFormatting = function() {
  1226. var format = options.format || 'L LT';
  1227. actualFormat = format.replace(/(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})/g, function(formatInput) {
  1228. var newinput = date.localeData().longDateFormat(formatInput) || formatInput;
  1229. return newinput.replace(/(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})/g, function(formatInput2) { // temp fix for #740
  1230. return date.localeData().longDateFormat(formatInput2) || formatInput2;
  1231. });
  1232. });
  1233. parseFormats = options.extraFormats ? options.extraFormats.slice() : [];
  1234. if (parseFormats.indexOf(format) < 0 && parseFormats.indexOf(actualFormat) < 0) {
  1235. parseFormats.push(actualFormat);
  1236. }
  1237. use24Hours = (actualFormat.toLowerCase().indexOf('a') < 1 && actualFormat.replace(/\[.*?\]/g, '').indexOf('h') < 1);
  1238. if (isEnabled('y')) {
  1239. minViewModeNumber = 2;
  1240. }
  1241. if (isEnabled('M')) {
  1242. minViewModeNumber = 1;
  1243. }
  1244. if (isEnabled('d')) {
  1245. minViewModeNumber = 0;
  1246. }
  1247. currentViewMode = Math.max(minViewModeNumber, currentViewMode);
  1248. if (!unset) {
  1249. setValue(date);
  1250. }
  1251. };
  1252. /** ******************************************************************************
  1253. *
  1254. * Public API functions
  1255. * =====================
  1256. *
  1257. * Important: Do not expose direct references to private objects or the options
  1258. * object to the outer world. Always return a clone when returning values or make
  1259. * a clone when setting a private variable.
  1260. *
  1261. ********************************************************************************/
  1262. picker.destroy = function() {
  1263. // /<summary>Destroys the widget and removes all attached event listeners</summary>
  1264. hide();
  1265. detachDatePickerElementEvents();
  1266. element.removeData('DateTimePicker');
  1267. element.removeData('date');
  1268. };
  1269. picker.toggle = toggle;
  1270. picker.show = show;
  1271. picker.hide = hide;
  1272. picker.disable = function() {
  1273. // /<summary>Disables the input element, the component is attached to, by adding a disabled="true" attribute to it.
  1274. // /If the widget was visible before that call it is hidden. Possibly emits dp.hide</summary>
  1275. hide();
  1276. if (component && component.hasClass('btn')) {
  1277. component.addClass('disabled');
  1278. }
  1279. input.prop('disabled', true);
  1280. return picker;
  1281. };
  1282. picker.enable = function() {
  1283. // /<summary>Enables the input element, the component is attached to, by removing disabled attribute from it.</summary>
  1284. if (component && component.hasClass('btn')) {
  1285. component.removeClass('disabled');
  1286. }
  1287. input.prop('disabled', false);
  1288. return picker;
  1289. };
  1290. picker.ignoreReadonly = function(ignoreReadonly) {
  1291. if (arguments.length === 0) {
  1292. return options.ignoreReadonly;
  1293. }
  1294. if (typeof ignoreReadonly !== 'boolean') {
  1295. throw new TypeError('ignoreReadonly () expects a boolean parameter');
  1296. }
  1297. options.ignoreReadonly = ignoreReadonly;
  1298. return picker;
  1299. };
  1300. picker.options = function(newOptions) {
  1301. if (arguments.length === 0) {
  1302. return $.extend(true, {}, options);
  1303. }
  1304. if (!(newOptions instanceof Object)) {
  1305. throw new TypeError('options() options parameter should be an object');
  1306. }
  1307. $.extend(true, options, newOptions);
  1308. $.each(options, function(key, value) {
  1309. if (picker[key] !== undefined) {
  1310. picker[key](value);
  1311. } else {
  1312. throw new TypeError('option ' + key + ' is not recognized!');
  1313. }
  1314. });
  1315. return picker;
  1316. };
  1317. picker.date = function(newDate) {
  1318. // /<signature helpKeyword="$.fn.datetimepicker.date">
  1319. // /<summary>Returns the component's model current date, a moment object or null if not set.</summary>
  1320. // /<returns type="Moment">date.clone()</returns>
  1321. // /</signature>
  1322. // /<signature>
  1323. // /<summary>Sets the components model current moment to it. Passing a null value unsets the components model current moment. Parsing of the newDate parameter is made using moment library with the options.format and options.useStrict components configuration.</summary>
  1324. // /<param name="newDate" locid="$.fn.datetimepicker.date_p:newDate">Takes string, Date, moment, null parameter.</param>
  1325. // /</signature>
  1326. if (arguments.length === 0) {
  1327. if (unset) {
  1328. return null;
  1329. }
  1330. return date.clone();
  1331. }
  1332. if (newDate !== null && typeof newDate !== 'string' && !moment.isMoment(newDate) && !(newDate instanceof Date)) {
  1333. throw new TypeError('date() parameter must be one of [null, string, moment or Date]');
  1334. }
  1335. setValue(newDate === null ? null : parseInputDate(newDate));
  1336. return picker;
  1337. };
  1338. picker.format = function(newFormat) {
  1339. // /<summary>test su</summary>
  1340. // /<param name="newFormat">info about para</param>
  1341. // /<returns type="string|boolean">returns foo</returns>
  1342. if (arguments.length === 0) {
  1343. return options.format;
  1344. }
  1345. if ((typeof newFormat !== 'string') && ((typeof newFormat !== 'boolean') || (newFormat !== false))) {
  1346. throw new TypeError('format() expects a string or boolean:false parameter ' + newFormat);
  1347. }
  1348. options.format = newFormat;
  1349. if (actualFormat) {
  1350. initFormatting(); // reinit formatting
  1351. }
  1352. return picker;
  1353. };
  1354. picker.timeZone = function(newZone) {
  1355. if (arguments.length === 0) {
  1356. return options.timeZone;
  1357. }
  1358. if (typeof newZone !== 'string') {
  1359. throw new TypeError('newZone() expects a string parameter');
  1360. }
  1361. options.timeZone = newZone;
  1362. return picker;
  1363. };
  1364. picker.dayViewHeaderFormat = function(newFormat) {
  1365. if (arguments.length === 0) {
  1366. return options.dayViewHeaderFormat;
  1367. }
  1368. if (typeof newFormat !== 'string') {
  1369. throw new TypeError('dayViewHeaderFormat() expects a string parameter');
  1370. }
  1371. options.dayViewHeaderFormat = newFormat;
  1372. return picker;
  1373. };
  1374. picker.extraFormats = function(formats) {
  1375. if (arguments.length === 0) {
  1376. return options.extraFormats;
  1377. }
  1378. if (formats !== false && !(formats instanceof Array)) {
  1379. throw new TypeError('extraFormats() expects an array or false parameter');
  1380. }
  1381. options.extraFormats = formats;
  1382. if (parseFormats) {
  1383. initFormatting(); // reinit formatting
  1384. }
  1385. return picker;
  1386. };
  1387. picker.disabledDates = function(dates) {
  1388. // /<signature helpKeyword="$.fn.datetimepicker.disabledDates">
  1389. // /<summary>Returns an array with the currently set disabled dates on the component.</summary>
  1390. // /<returns type="array">options.disabledDates</returns>
  1391. // /</signature>
  1392. // /<signature>
  1393. // /<summary>Setting this takes precedence over options.minDate, options.maxDate configuration. Also calling this function removes the configuration of
  1394. // /options.enabledDates if such exist.</summary>
  1395. // /<param name="dates" locid="$.fn.datetimepicker.disabledDates_p:dates">Takes an [ string or Date or moment ] of values and allows the user to select only from those days.</param>
  1396. // /</signature>
  1397. if (arguments.length === 0) {
  1398. return (options.disabledDates ? $.extend({}, options.disabledDates) : options.disabledDates);
  1399. }
  1400. if (!dates) {
  1401. options.disabledDates = false;
  1402. update();
  1403. return picker;
  1404. }
  1405. if (!(dates instanceof Array)) {
  1406. throw new TypeError('disabledDates() expects an array parameter');
  1407. }
  1408. options.disabledDates = indexGivenDates(dates);
  1409. options.enabledDates = false;
  1410. update();
  1411. return picker;
  1412. };
  1413. picker.enabledDates = function(dates) {
  1414. // /<signature helpKeyword="$.fn.datetimepicker.enabledDates">
  1415. // /<summary>Returns an array with the currently set enabled dates on the component.</summary>
  1416. // /<returns type="array">options.enabledDates</returns>
  1417. // /</signature>
  1418. // /<signature>
  1419. // /<summary>Setting this takes precedence over options.minDate, options.maxDate configuration. Also calling this function removes the configuration of options.disabledDates if such exist.</summary>
  1420. // /<param name="dates" locid="$.fn.datetimepicker.enabledDates_p:dates">Takes an [ string or Date or moment ] of values and allows the user to select only from those days.</param>
  1421. // /</signature>
  1422. if (arguments.length === 0) {
  1423. return (options.enabledDates ? $.extend({}, options.enabledDates) : options.enabledDates);
  1424. }
  1425. if (!dates) {
  1426. options.enabledDates = false;
  1427. update();
  1428. return picker;
  1429. }
  1430. if (!(dates instanceof Array)) {
  1431. throw new TypeError('enabledDates() expects an array parameter');
  1432. }
  1433. options.enabledDates = indexGivenDates(dates);
  1434. options.disabledDates = false;
  1435. update();
  1436. return picker;
  1437. };
  1438. picker.daysOfWeekDisabled = function(daysOfWeekDisabled) {
  1439. if (arguments.length === 0) {
  1440. return options.daysOfWeekDisabled.splice(0);
  1441. }
  1442. if ((typeof daysOfWeekDisabled === 'boolean') && !daysOfWeekDisabled) {
  1443. options.daysOfWeekDisabled = false;
  1444. update();
  1445. return picker;
  1446. }
  1447. if (!(daysOfWeekDisabled instanceof Array)) {
  1448. throw new TypeError('daysOfWeekDisabled() expects an array parameter');
  1449. }
  1450. options.daysOfWeekDisabled = daysOfWeekDisabled.reduce(function(previousValue, currentValue) {
  1451. currentValue = parseInt(currentValue, 10);
  1452. if (currentValue > 6 || currentValue < 0 || isNaN(currentValue)) {
  1453. return previousValue;
  1454. }
  1455. if (previousValue.indexOf(currentValue) === -1) {
  1456. previousValue.push(currentValue);
  1457. }
  1458. return previousValue;
  1459. }, []).sort();
  1460. if (options.useCurrent && !options.keepInvalid) {
  1461. var tries = 0;
  1462. while (!isValid(date, 'd')) {
  1463. date.add(1, 'd');
  1464. if (tries === 31) {
  1465. throw new Error('Tried 31 times to find a valid date');
  1466. }
  1467. tries++;
  1468. }
  1469. setValue(date);
  1470. }
  1471. update();
  1472. return picker;
  1473. };
  1474. picker.maxDate = function(maxDate) {
  1475. if (arguments.length === 0) {
  1476. return options.maxDate ? options.maxDate.clone() : options.maxDate;
  1477. }
  1478. if ((typeof maxDate === 'boolean') && maxDate === false) {
  1479. options.maxDate = false;
  1480. update();
  1481. return picker;
  1482. }
  1483. if (typeof maxDate === 'string') {
  1484. if (maxDate === 'now' || maxDate === 'moment') {
  1485. maxDate = getMoment();
  1486. }
  1487. }
  1488. var parsedDate = parseInputDate(maxDate);
  1489. if (!parsedDate.isValid()) {
  1490. throw new TypeError('maxDate() Could not parse date parameter: ' + maxDate);
  1491. }
  1492. if (options.minDate && parsedDate.isBefore(options.minDate)) {
  1493. throw new TypeError('maxDate() date parameter is before options.minDate: ' + parsedDate.format(actualFormat));
  1494. }
  1495. options.maxDate = parsedDate;
  1496. if (options.useCurrent && !options.keepInvalid && date.isAfter(maxDate)) {
  1497. setValue(options.maxDate);
  1498. }
  1499. if (viewDate.isAfter(parsedDate)) {
  1500. viewDate = parsedDate.clone().subtract(options.stepping, 'm');
  1501. }
  1502. update();
  1503. return picker;
  1504. };
  1505. picker.minDate = function(minDate) {
  1506. if (arguments.length === 0) {
  1507. return options.minDate ? options.minDate.clone() : options.minDate;
  1508. }
  1509. if ((typeof minDate === 'boolean') && minDate === false) {
  1510. options.minDate = false;
  1511. update();
  1512. return picker;
  1513. }
  1514. if (typeof minDate === 'string') {
  1515. if (minDate === 'now' || minDate === 'moment') {
  1516. minDate = getMoment();
  1517. }
  1518. }
  1519. var parsedDate = parseInputDate(minDate);
  1520. if (!parsedDate.isValid()) {
  1521. throw new TypeError('minDate() Could not parse date parameter: ' + minDate);
  1522. }
  1523. if (options.maxDate && parsedDate.isAfter(options.maxDate)) {
  1524. throw new TypeError('minDate() date parameter is after options.maxDate: ' + parsedDate.format(actualFormat));
  1525. }
  1526. options.minDate = parsedDate;
  1527. if (options.useCurrent && !options.keepInvalid && date.isBefore(minDate)) {
  1528. setValue(options.minDate);
  1529. }
  1530. if (viewDate.isBefore(parsedDate)) {
  1531. viewDate = parsedDate.clone().add(options.stepping, 'm');
  1532. }
  1533. update();
  1534. return picker;
  1535. };
  1536. picker.defaultDate = function(defaultDate) {
  1537. // /<signature helpKeyword="$.fn.datetimepicker.defaultDate">
  1538. // /<summary>Returns a moment with the options.defaultDate option configuration or false if not set</summary>
  1539. // /<returns type="Moment">date.clone()</returns>
  1540. // /</signature>
  1541. // /<signature>
  1542. // /<summary>Will set the picker's inital date. If a boolean:false value is passed the options.defaultDate parameter is cleared.</summary>
  1543. // /<param name="defaultDate" locid="$.fn.datetimepicker.defaultDate_p:defaultDate">Takes a string, Date, moment, boolean:false</param>
  1544. // /</signature>
  1545. if (arguments.length === 0) {
  1546. return options.defaultDate ? options.defaultDate.clone() : options.defaultDate;
  1547. }
  1548. if (!defaultDate) {
  1549. options.defaultDate = false;
  1550. return picker;
  1551. }
  1552. if (typeof defaultDate === 'string') {
  1553. if (defaultDate === 'now' || defaultDate === 'moment') {
  1554. defaultDate = getMoment();
  1555. } else {
  1556. defaultDate = getMoment(defaultDate);
  1557. }
  1558. }
  1559. var parsedDate = parseInputDate(defaultDate);
  1560. if (!parsedDate.isValid()) {
  1561. throw new TypeError('defaultDate() Could not parse date parameter: ' + defaultDate);
  1562. }
  1563. if (!isValid(parsedDate)) {
  1564. throw new TypeError('defaultDate() date passed is invalid according to component setup validations');
  1565. }
  1566. options.defaultDate = parsedDate;
  1567. if ((options.defaultDate && options.inline) || input.val().trim() === '') {
  1568. setValue(options.defaultDate);
  1569. }
  1570. return picker;
  1571. };
  1572. picker.locale = function(locale) {
  1573. if (arguments.length === 0) {
  1574. return options.locale;
  1575. }
  1576. if (!moment.localeData(locale)) {
  1577. throw new TypeError('locale() locale ' + locale + ' is not loaded from moment locales!');
  1578. }
  1579. options.locale = locale;
  1580. date.locale(options.locale);
  1581. viewDate.locale(options.locale);
  1582. if (actualFormat) {
  1583. initFormatting(); // reinit formatting
  1584. }
  1585. if (widget) {
  1586. hide();
  1587. show();
  1588. }
  1589. return picker;
  1590. };
  1591. picker.stepping = function(stepping) {
  1592. if (arguments.length === 0) {
  1593. return options.stepping;
  1594. }
  1595. stepping = parseInt(stepping, 10);
  1596. if (isNaN(stepping) || stepping < 1) {
  1597. stepping = 1;
  1598. }
  1599. options.stepping = stepping;
  1600. return picker;
  1601. };
  1602. picker.useCurrent = function(useCurrent) {
  1603. var useCurrentOptions = ['year', 'month', 'day', 'hour', 'minute'];
  1604. if (arguments.length === 0) {
  1605. return options.useCurrent;
  1606. }
  1607. if ((typeof useCurrent !== 'boolean') && (typeof useCurrent !== 'string')) {
  1608. throw new TypeError('useCurrent() expects a boolean or string parameter');
  1609. }
  1610. if (typeof useCurrent === 'string' && useCurrentOptions.indexOf(useCurrent.toLowerCase()) === -1) {
  1611. throw new TypeError('useCurrent() expects a string parameter of ' + useCurrentOptions.join(', '));
  1612. }
  1613. options.useCurrent = useCurrent;
  1614. return picker;
  1615. };
  1616. picker.collapse = function(collapse) {
  1617. if (arguments.length === 0) {
  1618. return options.collapse;
  1619. }
  1620. if (typeof collapse !== 'boolean') {
  1621. throw new TypeError('collapse() expects a boolean parameter');
  1622. }
  1623. if (options.collapse === collapse) {
  1624. return picker;
  1625. }
  1626. options.collapse = collapse;
  1627. if (widget) {
  1628. hide();
  1629. show();
  1630. }
  1631. return picker;
  1632. };
  1633. picker.icons = function(icons) {
  1634. if (arguments.length === 0) {
  1635. return $.extend({}, options.icons);
  1636. }
  1637. if (!(icons instanceof Object)) {
  1638. throw new TypeError('icons() expects parameter to be an Object');
  1639. }
  1640. $.extend(options.icons, icons);
  1641. if (widget) {
  1642. hide();
  1643. show();
  1644. }
  1645. return picker;
  1646. };
  1647. picker.tooltips = function(tooltips) {
  1648. if (arguments.length === 0) {
  1649. return $.extend({}, options.tooltips);
  1650. }
  1651. if (!(tooltips instanceof Object)) {
  1652. throw new TypeError('tooltips() expects parameter to be an Object');
  1653. }
  1654. $.extend(options.tooltips, tooltips);
  1655. if (widget) {
  1656. hide();
  1657. show();
  1658. }
  1659. return picker;
  1660. };
  1661. picker.useStrict = function(useStrict) {
  1662. if (arguments.length === 0) {
  1663. return options.useStrict;
  1664. }
  1665. if (typeof useStrict !== 'boolean') {
  1666. throw new TypeError('useStrict() expects a boolean parameter');
  1667. }
  1668. options.useStrict = useStrict;
  1669. return picker;
  1670. };
  1671. picker.sideBySide = function(sideBySide) {
  1672. if (arguments.length === 0) {
  1673. return options.sideBySide;
  1674. }
  1675. if (typeof sideBySide !== 'boolean') {
  1676. throw new TypeError('sideBySide() expects a boolean parameter');
  1677. }
  1678. options.sideBySide = sideBySide;
  1679. if (widget) {
  1680. hide();
  1681. show();
  1682. }
  1683. return picker;
  1684. };
  1685. picker.viewMode = function(viewMode) {
  1686. if (arguments.length === 0) {
  1687. return options.viewMode;
  1688. }
  1689. if (typeof viewMode !== 'string') {
  1690. throw new TypeError('viewMode() expects a string parameter');
  1691. }
  1692. if (viewModes.indexOf(viewMode) === -1) {
  1693. throw new TypeError('viewMode() parameter must be one of (' + viewModes.join(', ') + ') value');
  1694. }
  1695. options.viewMode = viewMode;
  1696. currentViewMode = Math.max(viewModes.indexOf(viewMode), minViewModeNumber);
  1697. showMode();
  1698. return picker;
  1699. };
  1700. picker.toolbarPlacement = function(toolbarPlacement) {
  1701. if (arguments.length === 0) {
  1702. return options.toolbarPlacement;
  1703. }
  1704. if (typeof toolbarPlacement !== 'string') {
  1705. throw new TypeError('toolbarPlacement() expects a string parameter');
  1706. }
  1707. if (toolbarPlacements.indexOf(toolbarPlacement) === -1) {
  1708. throw new TypeError('toolbarPlacement() parameter must be one of (' + toolbarPlacements.join(', ') + ') value');
  1709. }
  1710. options.toolbarPlacement = toolbarPlacement;
  1711. if (widget) {
  1712. hide();
  1713. show();
  1714. }
  1715. return picker;
  1716. };
  1717. picker.widgetPositioning = function(widgetPositioning) {
  1718. if (arguments.length === 0) {
  1719. return $.extend({}, options.widgetPositioning);
  1720. }
  1721. if (({}).toString.call(widgetPositioning) !== '[object Object]') {
  1722. throw new TypeError('widgetPositioning() expects an object variable');
  1723. }
  1724. if (widgetPositioning.horizontal) {
  1725. if (typeof widgetPositioning.horizontal !== 'string') {
  1726. throw new TypeError('widgetPositioning() horizontal variable must be a string');
  1727. }
  1728. widgetPositioning.horizontal = widgetPositioning.horizontal.toLowerCase();
  1729. if (horizontalModes.indexOf(widgetPositioning.horizontal) === -1) {
  1730. throw new TypeError('widgetPositioning() expects horizontal parameter to be one of (' + horizontalModes.join(', ') + ')');
  1731. }
  1732. options.widgetPositioning.horizontal = widgetPositioning.horizontal;
  1733. }
  1734. if (widgetPositioning.vertical) {
  1735. if (typeof widgetPositioning.vertical !== 'string') {
  1736. throw new TypeError('widgetPositioning() vertical variable must be a string');
  1737. }
  1738. widgetPositioning.vertical = widgetPositioning.vertical.toLowerCase();
  1739. if (verticalModes.indexOf(widgetPositioning.vertical) === -1) {
  1740. throw new TypeError('widgetPositioning() expects vertical parameter to be one of (' + verticalModes.join(', ') + ')');
  1741. }
  1742. options.widgetPositioning.vertical = widgetPositioning.vertical;
  1743. }
  1744. update();
  1745. return picker;
  1746. };
  1747. picker.calendarWeeks = function(calendarWeeks) {
  1748. if (arguments.length === 0) {
  1749. return options.calendarWeeks;
  1750. }
  1751. if (typeof calendarWeeks !== 'boolean') {
  1752. throw new TypeError('calendarWeeks() expects parameter to be a boolean value');
  1753. }
  1754. options.calendarWeeks = calendarWeeks;
  1755. update();
  1756. return picker;
  1757. };
  1758. picker.showTodayButton = function(showTodayButton) {
  1759. if (arguments.length === 0) {
  1760. return options.showTodayButton;
  1761. }
  1762. if (typeof showTodayButton !== 'boolean') {
  1763. throw new TypeError('showTodayButton() expects a boolean parameter');
  1764. }
  1765. options.showTodayButton = showTodayButton;
  1766. if (widget) {
  1767. hide();
  1768. show();
  1769. }
  1770. return picker;
  1771. };
  1772. picker.showClear = function(showClear) {
  1773. if (arguments.length === 0) {
  1774. return options.showClear;
  1775. }
  1776. if (typeof showClear !== 'boolean') {
  1777. throw new TypeError('showClear() expects a boolean parameter');
  1778. }
  1779. options.showClear = showClear;
  1780. if (widget) {
  1781. hide();
  1782. show();
  1783. }
  1784. return picker;
  1785. };
  1786. picker.widgetParent = function(widgetParent) {
  1787. if (arguments.length === 0) {
  1788. return options.widgetParent;
  1789. }
  1790. if (typeof widgetParent === 'string') {
  1791. widgetParent = $(widgetParent);
  1792. }
  1793. if (widgetParent !== null && (typeof widgetParent !== 'string' && !(widgetParent instanceof $))) {
  1794. throw new TypeError('widgetParent() expects a string or a jQuery object parameter');
  1795. }
  1796. options.widgetParent = widgetParent;
  1797. if (widget) {
  1798. hide();
  1799. show();
  1800. }
  1801. return picker;
  1802. };
  1803. picker.keepOpen = function(keepOpen) {
  1804. if (arguments.length === 0) {
  1805. return options.keepOpen;
  1806. }
  1807. if (typeof keepOpen !== 'boolean') {
  1808. throw new TypeError('keepOpen() expects a boolean parameter');
  1809. }
  1810. options.keepOpen = keepOpen;
  1811. return picker;
  1812. };
  1813. picker.focusOnShow = function(focusOnShow) {
  1814. if (arguments.length === 0) {
  1815. return options.focusOnShow;
  1816. }
  1817. if (typeof focusOnShow !== 'boolean') {
  1818. throw new TypeError('focusOnShow() expects a boolean parameter');
  1819. }
  1820. options.focusOnShow = focusOnShow;
  1821. return picker;
  1822. };
  1823. picker.inline = function(inline) {
  1824. if (arguments.length === 0) {
  1825. return options.inline;
  1826. }
  1827. if (typeof inline !== 'boolean') {
  1828. throw new TypeError('inline() expects a boolean parameter');
  1829. }
  1830. options.inline = inline;
  1831. return picker;
  1832. };
  1833. picker.clear = function() {
  1834. clear();
  1835. return picker;
  1836. };
  1837. picker.keyBinds = function(keyBinds) {
  1838. if (arguments.length === 0) {
  1839. return options.keyBinds;
  1840. }
  1841. options.keyBinds = keyBinds;
  1842. return picker;
  1843. };
  1844. picker.getMoment = function(d) {
  1845. return getMoment(d);
  1846. };
  1847. picker.debug = function(debug) {
  1848. if (typeof debug !== 'boolean') {
  1849. throw new TypeError('debug() expects a boolean parameter');
  1850. }
  1851. options.debug = debug;
  1852. return picker;
  1853. };
  1854. picker.allowInputToggle = function(allowInputToggle) {
  1855. if (arguments.length === 0) {
  1856. return options.allowInputToggle;
  1857. }
  1858. if (typeof allowInputToggle !== 'boolean') {
  1859. throw new TypeError('allowInputToggle() expects a boolean parameter');
  1860. }
  1861. options.allowInputToggle = allowInputToggle;
  1862. return picker;
  1863. };
  1864. picker.showClose = function(showClose) {
  1865. if (arguments.length === 0) {
  1866. return options.showClose;
  1867. }
  1868. if (typeof showClose !== 'boolean') {
  1869. throw new TypeError('showClose() expects a boolean parameter');
  1870. }
  1871. options.showClose = showClose;
  1872. return picker;
  1873. };
  1874. picker.keepInvalid = function(keepInvalid) {
  1875. if (arguments.length === 0) {
  1876. return options.keepInvalid;
  1877. }
  1878. if (typeof keepInvalid !== 'boolean') {
  1879. throw new TypeError('keepInvalid() expects a boolean parameter');
  1880. }
  1881. options.keepInvalid = keepInvalid;
  1882. return picker;
  1883. };
  1884. picker.datepickerInput = function(datepickerInput) {
  1885. if (arguments.length === 0) {
  1886. return options.datepickerInput;
  1887. }
  1888. if (typeof datepickerInput !== 'string') {
  1889. throw new TypeError('datepickerInput() expects a string parameter');
  1890. }
  1891. options.datepickerInput = datepickerInput;
  1892. return picker;
  1893. };
  1894. picker.parseInputDate = function(parseInputDate) {
  1895. if (arguments.length === 0) {
  1896. return options.parseInputDate;
  1897. }
  1898. if (typeof parseInputDate !== 'function') {
  1899. throw new TypeError('parseInputDate() sholud be as function');
  1900. }
  1901. options.parseInputDate = parseInputDate;
  1902. return picker;
  1903. };
  1904. picker.disabledTimeIntervals = function(disabledTimeIntervals) {
  1905. // /<signature helpKeyword="$.fn.datetimepicker.disabledTimeIntervals">
  1906. // /<summary>Returns an array with the currently set disabled dates on the component.</summary>
  1907. // /<returns type="array">options.disabledTimeIntervals</returns>
  1908. // /</signature>
  1909. // /<signature>
  1910. // /<summary>Setting this takes precedence over options.minDate, options.maxDate configuration. Also calling this function removes the configuration of
  1911. // /options.enabledDates if such exist.</summary>
  1912. // /<param name="dates" locid="$.fn.datetimepicker.disabledTimeIntervals_p:dates">Takes an [ string or Date or moment ] of values and allows the user to select only from those days.</param>
  1913. // /</signature>
  1914. if (arguments.length === 0) {
  1915. return (options.disabledTimeIntervals ? $.extend({}, options.disabledTimeIntervals) : options.disabledTimeIntervals);
  1916. }
  1917. if (!disabledTimeIntervals) {
  1918. options.disabledTimeIntervals = false;
  1919. update();
  1920. return picker;
  1921. }
  1922. if (!(disabledTimeIntervals instanceof Array)) {
  1923. throw new TypeError('disabledTimeIntervals() expects an array parameter');
  1924. }
  1925. options.disabledTimeIntervals = disabledTimeIntervals;
  1926. update();
  1927. return picker;
  1928. };
  1929. picker.disabledHours = function(hours) {
  1930. // /<signature helpKeyword="$.fn.datetimepicker.disabledHours">
  1931. // /<summary>Returns an array with the currently set disabled hours on the component.</summary>
  1932. // /<returns type="array">options.disabledHours</returns>
  1933. // /</signature>
  1934. // /<signature>
  1935. // /<summary>Setting this takes precedence over options.minDate, options.maxDate configuration. Also calling this function removes the configuration of
  1936. // /options.enabledHours if such exist.</summary>
  1937. // /<param name="hours" locid="$.fn.datetimepicker.disabledHours_p:hours">Takes an [ int ] of values and disallows the user to select only from those hours.</param>
  1938. // /</signature>
  1939. if (arguments.length === 0) {
  1940. return (options.disabledHours ? $.extend({}, options.disabledHours) : options.disabledHours);
  1941. }
  1942. if (!hours) {
  1943. options.disabledHours = false;
  1944. update();
  1945. return picker;
  1946. }
  1947. if (!(hours instanceof Array)) {
  1948. throw new TypeError('disabledHours() expects an array parameter');
  1949. }
  1950. options.disabledHours = indexGivenHours(hours);
  1951. options.enabledHours = false;
  1952. if (options.useCurrent && !options.keepInvalid) {
  1953. var tries = 0;
  1954. while (!isValid(date, 'h')) {
  1955. date.add(1, 'h');
  1956. if (tries === 24) {
  1957. throw new Error('Tried 24 times to find a valid date');
  1958. }
  1959. tries++;
  1960. }
  1961. setValue(date);
  1962. }
  1963. update();
  1964. return picker;
  1965. };
  1966. picker.enabledHours = function(hours) {
  1967. // /<signature helpKeyword="$.fn.datetimepicker.enabledHours">
  1968. // /<summary>Returns an array with the currently set enabled hours on the component.</summary>
  1969. // /<returns type="array">options.enabledHours</returns>
  1970. // /</signature>
  1971. // /<signature>
  1972. // /<summary>Setting this takes precedence over options.minDate, options.maxDate configuration. Also calling this function removes the configuration of options.disabledHours if such exist.</summary>
  1973. // /<param name="hours" locid="$.fn.datetimepicker.enabledHours_p:hours">Takes an [ int ] of values and allows the user to select only from those hours.</param>
  1974. // /</signature>
  1975. if (arguments.length === 0) {
  1976. return (options.enabledHours ? $.extend({}, options.enabledHours) : options.enabledHours);
  1977. }
  1978. if (!hours) {
  1979. options.enabledHours = false;
  1980. update();
  1981. return picker;
  1982. }
  1983. if (!(hours instanceof Array)) {
  1984. throw new TypeError('enabledHours() expects an array parameter');
  1985. }
  1986. options.enabledHours = indexGivenHours(hours);
  1987. options.disabledHours = false;
  1988. if (options.useCurrent && !options.keepInvalid) {
  1989. var tries = 0;
  1990. while (!isValid(date, 'h')) {
  1991. date.add(1, 'h');
  1992. if (tries === 24) {
  1993. throw new Error('Tried 24 times to find a valid date');
  1994. }
  1995. tries++;
  1996. }
  1997. setValue(date);
  1998. }
  1999. update();
  2000. return picker;
  2001. };
  2002. /**
  2003. * Returns the component's model current viewDate, a moment object or null if not set. Passing a null value unsets the components model current moment. Parsing of the newDate parameter is made using moment library with the options.format and options.useStrict components configuration.
  2004. * @param {Takes string, viewDate, moment, null parameter.} newDate
  2005. * @returns {viewDate.clone()}
  2006. */
  2007. picker.viewDate = function(newDate) {
  2008. if (arguments.length === 0) {
  2009. return viewDate.clone();
  2010. }
  2011. if (!newDate) {
  2012. viewDate = date.clone();
  2013. return picker;
  2014. }
  2015. if (typeof newDate !== 'string' && !moment.isMoment(newDate) && !(newDate instanceof Date)) {
  2016. throw new TypeError('viewDate() parameter must be one of [string, moment or Date]');
  2017. }
  2018. viewDate = parseInputDate(newDate);
  2019. viewUpdate();
  2020. return picker;
  2021. };
  2022. // initializing element and component attributes
  2023. if (element.is('input')) {
  2024. input = element;
  2025. } else {
  2026. input = element.find(options.datepickerInput);
  2027. if (input.length === 0) {
  2028. input = element.find('input');
  2029. } else if (!input.is('input')) {
  2030. throw new Error('CSS class "' + options.datepickerInput + '" cannot be applied to non input element');
  2031. }
  2032. }
  2033. if (element.hasClass('input-group')) {
  2034. // in case there is more then one 'input-group-addon' Issue #48
  2035. if (element.find('.datepickerbutton').length === 0) {
  2036. component = element.find('.input-group-addon');
  2037. } else {
  2038. component = element.find('.datepickerbutton');
  2039. }
  2040. }
  2041. if (!options.inline && !input.is('input')) {
  2042. throw new Error('Could not initialize DateTimePicker without an input element');
  2043. }
  2044. // Set defaults for date here now instead of in var declaration
  2045. date = getMoment();
  2046. viewDate = date.clone();
  2047. $.extend(true, options, dataToOptions());
  2048. picker.options(options);
  2049. initFormatting();
  2050. attachDatePickerElementEvents();
  2051. if (input.prop('disabled')) {
  2052. picker.disable();
  2053. }
  2054. if (input.is('input') && input.val().trim().length !== 0) {
  2055. setValue(parseInputDate(input.val().trim()));
  2056. } else if (options.defaultDate && input.attr('placeholder') === undefined) {
  2057. setValue(options.defaultDate);
  2058. }
  2059. if (options.inline) {
  2060. show();
  2061. }
  2062. return picker;
  2063. };
  2064. /** ******************************************************************************
  2065. *
  2066. * jQuery plugin constructor and defaults object
  2067. *
  2068. ********************************************************************************/
  2069. /**
  2070. * See (http://jquery.com/).
  2071. * @name jQuery
  2072. * @class
  2073. * See the jQuery Library (http://jquery.com/) for full details. This just
  2074. * documents the function and classes that are added to jQuery by this plug-in.
  2075. */
  2076. /**
  2077. * See (http://jquery.com/)
  2078. * @name fn
  2079. * @class
  2080. * See the jQuery Library (http://jquery.com/) for full details. This just
  2081. * documents the function and classes that are added to jQuery by this plug-in.
  2082. * @memberOf jQuery
  2083. */
  2084. /**
  2085. * Show comments
  2086. * @class datetimepicker
  2087. * @memberOf jQuery.fn
  2088. */
  2089. $.fn.datetimepicker = function(options) {
  2090. options = options || {};
  2091. var args = Array.prototype.slice.call(arguments, 1);
  2092. var isInstance = true;
  2093. var thisMethods = ['destroy', 'hide', 'show', 'toggle'];
  2094. var returnValue;
  2095. if (typeof options === 'object') {
  2096. return this.each(function() {
  2097. var $this = $(this);
  2098. var _options;
  2099. if (!$this.data('DateTimePicker')) {
  2100. // create a private copy of the defaults object
  2101. _options = $.extend(true, {}, $.fn.datetimepicker.defaults, options);
  2102. $this.data('DateTimePicker', dateTimePicker($this, _options));
  2103. }
  2104. });
  2105. } else if (typeof options === 'string') {
  2106. this.each(function() {
  2107. var $this = $(this);
  2108. var instance = $this.data('DateTimePicker');
  2109. if (!instance) {
  2110. throw new Error('bootstrap-datetimepicker("' + options + '") method was called on an element that is not using DateTimePicker');
  2111. }
  2112. returnValue = instance[options].apply(instance, args);
  2113. isInstance = returnValue === instance;
  2114. });
  2115. if (isInstance || $.inArray(options, thisMethods) > -1) {
  2116. return this;
  2117. }
  2118. return returnValue;
  2119. }
  2120. throw new TypeError('Invalid arguments for DateTimePicker: ' + options);
  2121. };
  2122. $.fn.datetimepicker.defaults = {
  2123. timeZone: '',
  2124. format: false,
  2125. dayViewHeaderFormat: 'MMMM YYYY',
  2126. extraFormats: false,
  2127. stepping: 1,
  2128. minDate: false,
  2129. maxDate: false,
  2130. useCurrent: true,
  2131. collapse: true,
  2132. locale: moment.locale(),
  2133. defaultDate: false,
  2134. disabledDates: false,
  2135. enabledDates: false,
  2136. icons: {
  2137. time: 'glyphicon glyphicon-time',
  2138. date: 'glyphicon glyphicon-calendar',
  2139. up: 'glyphicon glyphicon-chevron-up',
  2140. down: 'glyphicon glyphicon-chevron-down',
  2141. previous: 'glyphicon glyphicon-chevron-left',
  2142. next: 'glyphicon glyphicon-chevron-right',
  2143. today: 'glyphicon glyphicon-screenshot',
  2144. clear: 'glyphicon glyphicon-trash',
  2145. close: 'glyphicon glyphicon-remove'
  2146. },
  2147. tooltips: {
  2148. today: 'Go to today',
  2149. clear: 'Clear selection',
  2150. close: 'Close the picker',
  2151. selectMonth: 'Select Month',
  2152. prevMonth: 'Previous Month',
  2153. nextMonth: 'Next Month',
  2154. selectYear: 'Select Year',
  2155. prevYear: 'Previous Year',
  2156. nextYear: 'Next Year',
  2157. selectDecade: 'Select Decade',
  2158. prevDecade: 'Previous Decade',
  2159. nextDecade: 'Next Decade',
  2160. prevCentury: 'Previous Century',
  2161. nextCentury: 'Next Century',
  2162. pickHour: 'Pick Hour',
  2163. incrementHour: 'Increment Hour',
  2164. decrementHour: 'Decrement Hour',
  2165. pickMinute: 'Pick Minute',
  2166. incrementMinute: 'Increment Minute',
  2167. decrementMinute: 'Decrement Minute',
  2168. pickSecond: 'Pick Second',
  2169. incrementSecond: 'Increment Second',
  2170. decrementSecond: 'Decrement Second',
  2171. togglePeriod: 'Toggle Period',
  2172. selectTime: 'Select Time'
  2173. },
  2174. useStrict: false,
  2175. sideBySide: false,
  2176. daysOfWeekDisabled: false,
  2177. calendarWeeks: false,
  2178. viewMode: 'days',
  2179. toolbarPlacement: 'default',
  2180. showTodayButton: false,
  2181. showClear: false,
  2182. showClose: false,
  2183. widgetPositioning: {
  2184. horizontal: 'auto',
  2185. vertical: 'auto'
  2186. },
  2187. widgetParent: null,
  2188. ignoreReadonly: false,
  2189. keepOpen: false,
  2190. focusOnShow: true,
  2191. inline: false,
  2192. keepInvalid: false,
  2193. datepickerInput: '.datepickerinput',
  2194. keyBinds: {
  2195. up: function(widget) {
  2196. if (!widget) {
  2197. return;
  2198. }
  2199. var d = this.date() || this.getMoment();
  2200. if (widget.find('.datepicker').is(':visible')) {
  2201. this.date(d.clone().subtract(7, 'd'));
  2202. } else {
  2203. this.date(d.clone().add(this.stepping(), 'm'));
  2204. }
  2205. },
  2206. down: function(widget) {
  2207. if (!widget) {
  2208. this.show();
  2209. return;
  2210. }
  2211. var d = this.date() || this.getMoment();
  2212. if (widget.find('.datepicker').is(':visible')) {
  2213. this.date(d.clone().add(7, 'd'));
  2214. } else {
  2215. this.date(d.clone().subtract(this.stepping(), 'm'));
  2216. }
  2217. },
  2218. 'control up': function(widget) {
  2219. if (!widget) {
  2220. return;
  2221. }
  2222. var d = this.date() || this.getMoment();
  2223. if (widget.find('.datepicker').is(':visible')) {
  2224. this.date(d.clone().subtract(1, 'y'));
  2225. } else {
  2226. this.date(d.clone().add(1, 'h'));
  2227. }
  2228. },
  2229. 'control down': function(widget) {
  2230. if (!widget) {
  2231. return;
  2232. }
  2233. var d = this.date() || this.getMoment();
  2234. if (widget.find('.datepicker').is(':visible')) {
  2235. this.date(d.clone().add(1, 'y'));
  2236. } else {
  2237. this.date(d.clone().subtract(1, 'h'));
  2238. }
  2239. },
  2240. left: function(widget) {
  2241. if (!widget) {
  2242. return;
  2243. }
  2244. var d = this.date() || this.getMoment();
  2245. if (widget.find('.datepicker').is(':visible')) {
  2246. this.date(d.clone().subtract(1, 'd'));
  2247. }
  2248. },
  2249. right: function(widget) {
  2250. if (!widget) {
  2251. return;
  2252. }
  2253. var d = this.date() || this.getMoment();
  2254. if (widget.find('.datepicker').is(':visible')) {
  2255. this.date(d.clone().add(1, 'd'));
  2256. }
  2257. },
  2258. pageUp: function(widget) {
  2259. if (!widget) {
  2260. return;
  2261. }
  2262. var d = this.date() || this.getMoment();
  2263. if (widget.find('.datepicker').is(':visible')) {
  2264. this.date(d.clone().subtract(1, 'M'));
  2265. }
  2266. },
  2267. pageDown: function(widget) {
  2268. if (!widget) {
  2269. return;
  2270. }
  2271. var d = this.date() || this.getMoment();
  2272. if (widget.find('.datepicker').is(':visible')) {
  2273. this.date(d.clone().add(1, 'M'));
  2274. }
  2275. },
  2276. enter: function() {
  2277. this.hide();
  2278. },
  2279. escape: function() {
  2280. this.hide();
  2281. },
  2282. // tab: function (widget) { //this break the flow of the form. disabling for now
  2283. // var toggle = widget.find('.picker-switch a[data-action="togglePicker"]');
  2284. // if(toggle.length > 0) toggle.click();
  2285. // },
  2286. 'control space': function(widget) {
  2287. if (!widget) {
  2288. return;
  2289. }
  2290. if (widget.find('.timepicker').is(':visible')) {
  2291. widget.find('.btn[data-action="togglePeriod"]').click();
  2292. }
  2293. },
  2294. t: function() {
  2295. this.date(this.getMoment());
  2296. },
  2297. 'delete': function() {
  2298. this.clear();
  2299. }
  2300. },
  2301. debug: false,
  2302. allowInputToggle: false,
  2303. disabledTimeIntervals: false,
  2304. disabledHours: false,
  2305. enabledHours: false,
  2306. viewDate: false
  2307. };
  2308. return $.fn.datetimepicker;
  2309. }));