picker_date.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Date and time pickers
  4. *
  5. * Demo JS code for picker_date.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var DateTimePickers = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Daterange picker
  15. var _componentDaterange = function() {
  16. if (!$().daterangepicker) {
  17. console.warn('Warning - daterangepicker.js is not loaded.');
  18. return;
  19. }
  20. // Basic initialization
  21. $('.daterange-basic').daterangepicker({
  22. applyClass: 'bg-slate-600',
  23. cancelClass: 'btn-light'
  24. });
  25. // Display week numbers
  26. $('.daterange-weeknumbers').daterangepicker({
  27. showWeekNumbers: true,
  28. applyClass: 'bg-slate-600',
  29. cancelClass: 'btn-light'
  30. });
  31. // Button class options
  32. $('.daterange-buttons').daterangepicker({
  33. applyClass: 'btn-success',
  34. cancelClass: 'btn-danger'
  35. });
  36. // Display time picker
  37. $('.daterange-time').daterangepicker({
  38. timePicker: true,
  39. applyClass: 'bg-slate-600',
  40. cancelClass: 'btn-light',
  41. locale: {
  42. format: 'MM/DD/YYYY h:mm a'
  43. }
  44. });
  45. // Show calendars on left
  46. $('.daterange-left').daterangepicker({
  47. opens: 'left',
  48. applyClass: 'bg-slate-600',
  49. cancelClass: 'btn-light'
  50. });
  51. // Single picker
  52. $('.daterange-single').daterangepicker({
  53. singleDatePicker: true
  54. });
  55. // Display date dropdowns
  56. $('.daterange-datemenu').daterangepicker({
  57. showDropdowns: true,
  58. opens: 'left',
  59. applyClass: 'bg-slate-600',
  60. cancelClass: 'btn-light'
  61. });
  62. // 10 minute increments
  63. $('.daterange-increments').daterangepicker({
  64. timePicker: true,
  65. opens: 'left',
  66. applyClass: 'bg-slate-600',
  67. cancelClass: 'btn-light',
  68. timePickerIncrement: 10,
  69. locale: {
  70. format: 'MM/DD/YYYY h:mm a'
  71. }
  72. });
  73. // Localization
  74. $('.daterange-locale').daterangepicker({
  75. applyClass: 'bg-slate-600',
  76. cancelClass: 'btn-light',
  77. opens: 'left',
  78. ranges: {
  79. 'Сегодня': [moment(), moment()],
  80. 'Вчера': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
  81. 'Последние 7 дней': [moment().subtract(6, 'days'), moment()],
  82. 'Последние 30 дней': [moment().subtract(29, 'days'), moment()],
  83. 'Этот месяц': [moment().startOf('month'), moment().endOf('month')],
  84. 'Прошедший месяц': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  85. },
  86. locale: {
  87. applyLabel: 'Вперед',
  88. cancelLabel: 'Отмена',
  89. startLabel: 'Начальная дата',
  90. endLabel: 'Конечная дата',
  91. customRangeLabel: 'Выбрать дату',
  92. daysOfWeek: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт','Сб'],
  93. monthNames: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
  94. firstDay: 1
  95. }
  96. });
  97. //
  98. // Pre-defined ranges and callback
  99. //
  100. // Initialize with options
  101. $('.daterange-predefined').daterangepicker(
  102. {
  103. startDate: moment().subtract(29, 'days'),
  104. endDate: moment(),
  105. minDate: '01/01/2014',
  106. maxDate: '12/31/2019',
  107. dateLimit: { days: 60 },
  108. ranges: {
  109. 'Today': [moment(), moment()],
  110. 'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
  111. 'Last 7 Days': [moment().subtract(6, 'days'), moment()],
  112. 'Last 30 Days': [moment().subtract(29, 'days'), moment()],
  113. 'This Month': [moment().startOf('month'), moment().endOf('month')],
  114. 'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  115. },
  116. opens: 'left',
  117. applyClass: 'btn-sm bg-slate',
  118. cancelClass: 'btn-sm btn-light'
  119. },
  120. function(start, end) {
  121. $('.daterange-predefined span').html(start.format('MMMM D, YYYY') + '   -   ' + end.format('MMMM D, YYYY'));
  122. $.jGrowl('Date range has been changed', { header: 'Update', theme: 'bg-primary', position: 'center', life: 1500 });
  123. }
  124. );
  125. // Display date format
  126. $('.daterange-predefined span').html(moment().subtract(29, 'days').format('MMMM D, YYYY') + '   -   ' + moment().format('MMMM D, YYYY'));
  127. //
  128. // Inside button
  129. //
  130. // Initialize with options
  131. $('.daterange-ranges').daterangepicker(
  132. {
  133. startDate: moment().subtract(29, 'days'),
  134. endDate: moment(),
  135. minDate: '01/01/2012',
  136. maxDate: '12/31/2019',
  137. dateLimit: { days: 60 },
  138. ranges: {
  139. 'Today': [moment(), moment()],
  140. 'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
  141. 'Last 7 Days': [moment().subtract(6, 'days'), moment()],
  142. 'Last 30 Days': [moment().subtract(29, 'days'), moment()],
  143. 'This Month': [moment().startOf('month'), moment().endOf('month')],
  144. 'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
  145. },
  146. opens: 'left',
  147. applyClass: 'btn-sm bg-slate-600',
  148. cancelClass: 'btn-sm btn-light'
  149. },
  150. function(start, end) {
  151. $('.daterange-ranges span').html(start.format('MMMM D, YYYY') + '   -   ' + end.format('MMMM D, YYYY'));
  152. }
  153. );
  154. // Display date format
  155. $('.daterange-ranges span').html(moment().subtract(29, 'days').format('MMMM D, YYYY') + '   -   ' + moment().format('MMMM D, YYYY'));
  156. };
  157. // Pickadate picker
  158. var _componentPickadate = function() {
  159. if (!$().pickadate) {
  160. console.warn('Warning - picker.js and/or picker.date.js is not loaded.');
  161. return;
  162. }
  163. // Basic options
  164. $('.pickadate').pickadate();
  165. // Change day names
  166. $('.pickadate-strings').pickadate({
  167. weekdaysShort: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  168. showMonthsShort: true
  169. });
  170. // Button options
  171. $('.pickadate-buttons').pickadate({
  172. today: '',
  173. close: '',
  174. clear: 'Clear selection'
  175. });
  176. // Accessibility labels
  177. $('.pickadate-accessibility').pickadate({
  178. labelMonthNext: 'Go to the next month',
  179. labelMonthPrev: 'Go to the previous month',
  180. labelMonthSelect: 'Pick a month from the dropdown',
  181. labelYearSelect: 'Pick a year from the dropdown',
  182. selectMonths: true,
  183. selectYears: true
  184. });
  185. // Localization
  186. $('.pickadate-translated').pickadate({
  187. monthsFull: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
  188. weekdaysShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
  189. today: 'aujourd\'hui',
  190. clear: 'effacer',
  191. formatSubmit: 'yyyy/mm/dd'
  192. });
  193. // Format options
  194. $('.pickadate-format').pickadate({
  195. // Escape any “rule” characters with an exclamation mark (!).
  196. format: 'You selecte!d: dddd, dd mmm, yyyy',
  197. formatSubmit: 'yyyy/mm/dd',
  198. hiddenPrefix: 'prefix__',
  199. hiddenSuffix: '__suffix'
  200. });
  201. // Editable input
  202. var $input_date = $('.pickadate-editable').pickadate({
  203. editable: true,
  204. onClose: function() {
  205. $('.datepicker').focus();
  206. }
  207. });
  208. var picker_date = $input_date.pickadate('picker');
  209. $input_date.on('click', function(event) {
  210. if (picker_date.get('open')) {
  211. picker_date.close();
  212. } else {
  213. picker_date.open();
  214. }
  215. event.stopPropagation();
  216. });
  217. // Dropdown selectors
  218. $('.pickadate-selectors').pickadate({
  219. selectYears: true,
  220. selectMonths: true
  221. });
  222. // Year selector
  223. $('.pickadate-year').pickadate({
  224. selectYears: 4
  225. });
  226. // Set first weekday
  227. $('.pickadate-weekday').pickadate({
  228. firstDay: 1
  229. });
  230. // Date limits
  231. $('.pickadate-limits').pickadate({
  232. min: [2014,3,20],
  233. max: [2014,7,14]
  234. });
  235. // Disable certain dates
  236. $('.pickadate-disable').pickadate({
  237. disable: [
  238. [2015,8,3],
  239. [2015,8,12],
  240. [2015,8,20]
  241. ]
  242. });
  243. // Disable date range
  244. $('.pickadate-disable-range').pickadate({
  245. disable: [
  246. 5,
  247. [2013, 10, 21, 'inverted'],
  248. { from: [2014, 3, 15], to: [2014, 3, 25] },
  249. [2014, 3, 20, 'inverted'],
  250. { from: [2014, 3, 17], to: [2014, 3, 18], inverted: true }
  251. ]
  252. });
  253. // Events
  254. $('.pickadate-events').pickadate({
  255. onStart: function() {
  256. console.log('Hello there :)')
  257. },
  258. onRender: function() {
  259. console.log('Whoa.. rendered anew')
  260. },
  261. onOpen: function() {
  262. console.log('Opened up')
  263. },
  264. onClose: function() {
  265. console.log('Closed now')
  266. },
  267. onStop: function() {
  268. console.log('See ya.')
  269. },
  270. onSet: function(context) {
  271. console.log('Just set stuff:', context)
  272. }
  273. });
  274. };
  275. // Pickatime picker
  276. var _componentPickatime = function() {
  277. if (!$().pickatime) {
  278. console.warn('Warning - picker.js and/or picker.time.js is not loaded.');
  279. return;
  280. }
  281. // Default functionality
  282. $('.pickatime').pickatime();
  283. // Clear button
  284. $('.pickatime-clear').pickatime({
  285. clear: ''
  286. });
  287. // Time formats
  288. $('.pickatime-format').pickatime({
  289. // Escape any “rule” characters with an exclamation mark (!).
  290. format: 'T!ime selected: h:i a',
  291. formatLabel: '<b>h</b>:i <!i>a</!i>',
  292. formatSubmit: 'HH:i',
  293. hiddenPrefix: 'prefix__',
  294. hiddenSuffix: '__suffix'
  295. });
  296. // Send hidden value
  297. $('.pickatime-hidden').pickatime({
  298. formatSubmit: 'HH:i',
  299. hiddenName: true
  300. });
  301. // Editable input
  302. var $input_time = $('.pickatime-editable').pickatime({
  303. editable: true,
  304. onClose: function() {
  305. $('.datepicker').focus();
  306. }
  307. });
  308. var picker_time = $input_time.pickatime('picker');
  309. $input_time.on('click', function(event) {
  310. if (picker_time.get('open')) {
  311. picker_time.close();
  312. } else {
  313. picker_time.open();
  314. }
  315. event.stopPropagation();
  316. });
  317. // Time intervals
  318. $('.pickatime-intervals').pickatime({
  319. interval: 150
  320. });
  321. // Time limits
  322. $('.pickatime-limits').pickatime({
  323. min: [7,30],
  324. max: [14,0]
  325. });
  326. // Using integers as hours
  327. $('.pickatime-limits-integers').pickatime({
  328. disable: [
  329. 3, 5, 7
  330. ]
  331. });
  332. // Disable times
  333. $('.pickatime-disabled').pickatime({
  334. disable: [
  335. [0,30],
  336. [2,0],
  337. [8,30],
  338. [9,0]
  339. ]
  340. });
  341. // Disabling ranges
  342. $('.pickatime-range').pickatime({
  343. disable: [
  344. 1,
  345. [1, 30, 'inverted'],
  346. { from: [4, 30], to: [10, 30] },
  347. [6, 30, 'inverted'],
  348. { from: [8, 0], to: [9, 0], inverted: true }
  349. ]
  350. });
  351. // Disable all with exeption
  352. $('.pickatime-disableall').pickatime({
  353. disable: [
  354. true,
  355. 3, 5, 7,
  356. [0,30],
  357. [2,0],
  358. [8,30],
  359. [9,0]
  360. ]
  361. });
  362. // Events
  363. $('.pickatime-events').pickatime({
  364. onStart: function() {
  365. console.log('Hello there :)')
  366. },
  367. onRender: function() {
  368. console.log('Whoa.. rendered anew')
  369. },
  370. onOpen: function() {
  371. console.log('Opened up')
  372. },
  373. onClose: function() {
  374. console.log('Closed now')
  375. },
  376. onStop: function() {
  377. console.log('See ya.')
  378. },
  379. onSet: function(context) {
  380. console.log('Just set stuff:', context)
  381. }
  382. });
  383. };
  384. // Anytime picker
  385. var _componentAnytime = function() {
  386. if (!$().AnyTime_picker) {
  387. console.warn('Warning - anytime.min.js is not loaded.');
  388. return;
  389. }
  390. // Basic usage
  391. $('#anytime-date').AnyTime_picker({
  392. format: '%W, %M %D in the Year %z %E',
  393. firstDOW: 1
  394. });
  395. // Time picker
  396. $('#anytime-time').AnyTime_picker({
  397. format: '%H:%i'
  398. });
  399. // Display hours only
  400. $('#anytime-time-hours').AnyTime_picker({
  401. format: '%l %p'
  402. });
  403. // Date and time
  404. $('#anytime-both').AnyTime_picker({
  405. format: '%M %D %H:%i',
  406. });
  407. // Custom display format
  408. $('#anytime-weekday').AnyTime_picker({
  409. format: '%W, %D of %M, %Z'
  410. });
  411. // Numeric date
  412. $('#anytime-month-numeric').AnyTime_picker({
  413. format: '%d/%m/%Z'
  414. });
  415. // Month and day
  416. $('#anytime-month-day').AnyTime_picker({
  417. format: '%D of %M'
  418. });
  419. // On demand picker
  420. $('#ButtonCreationDemoButton').on('click', function (e) {
  421. $('#ButtonCreationDemoInput').AnyTime_noPicker().AnyTime_picker().focus();
  422. e.preventDefault();
  423. });
  424. //
  425. // Date range
  426. //
  427. // Options
  428. var oneDay = 24*60*60*1000;
  429. var rangeDemoFormat = '%e-%b-%Y';
  430. var rangeDemoConv = new AnyTime.Converter({format:rangeDemoFormat});
  431. // Set today's date
  432. $('#rangeDemoToday').on('click', function (e) {
  433. $('#rangeDemoStart').val(rangeDemoConv.format(new Date())).trigger('change');
  434. });
  435. // Clear dates
  436. $('#rangeDemoClear').on('click', function (e) {
  437. $('#rangeDemoStart').val('').trigger('change');
  438. });
  439. // Start date
  440. $('#rangeDemoStart').AnyTime_picker({
  441. format: rangeDemoFormat
  442. });
  443. // On value change
  444. $('#rangeDemoStart').on('change', function(e) {
  445. try {
  446. var fromDay = rangeDemoConv.parse($('#rangeDemoStart').val()).getTime();
  447. var dayLater = new Date(fromDay+oneDay);
  448. dayLater.setHours(0,0,0,0);
  449. var ninetyDaysLater = new Date(fromDay+(90*oneDay));
  450. ninetyDaysLater.setHours(23,59,59,999);
  451. // End date
  452. $('#rangeDemoFinish')
  453. .AnyTime_noPicker()
  454. .removeAttr('disabled')
  455. .val(rangeDemoConv.format(dayLater))
  456. .AnyTime_picker({
  457. earliest: dayLater,
  458. format: rangeDemoFormat,
  459. latest: ninetyDaysLater
  460. });
  461. }
  462. catch(e) {
  463. // Disable End date field
  464. $('#rangeDemoFinish').val('').attr('disabled', 'disabled');
  465. }
  466. });
  467. };
  468. //
  469. // Return objects assigned to module
  470. //
  471. return {
  472. init: function() {
  473. _componentDaterange();
  474. _componentPickadate();
  475. _componentPickatime();
  476. _componentAnytime();
  477. }
  478. }
  479. }();
  480. // Initialize module
  481. // ------------------------------
  482. document.addEventListener('DOMContentLoaded', function() {
  483. DateTimePickers.init();
  484. });