4e3c9ad1789809b00e4bf78d662c2b5f409bcc15
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / DateTimePicker.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * contains all logic for the date time picker used in FormEngine
16 * and EXT:belog and EXT:scheduler
17 */
18 define('TYPO3/CMS/Backend/DateTimePicker', ['jquery'], function ($) {
19
20 var DateTimePicker = {
21 options: {
22 fieldSelector: '.t3js-datetimepicker',
23 format: (opener ? opener.top : top).TYPO3.settings.DateTimePicker.DateFormat
24 }
25 };
26
27 /**
28 * initialize date fields to add a datepicker to each field
29 * note: this function can be called multiple times (e.g. after AJAX requests) because it only
30 * applies to fields which haven't been used yet.
31 */
32 DateTimePicker.initialize = function() {
33 // fetch the date time fields that heven't been initialized yet
34 var $dateTimeFields = $(DateTimePicker.options.fieldSelector).filter(function() {
35 return ($(this).data('DateTimePicker') == undefined);
36 });
37
38 if ($dateTimeFields.length > 0) {
39 require(['moment', 'twbs/bootstrap-datetimepicker'], function(moment) {
40
41 // initialize the datepicker on each selected element
42 $dateTimeFields.each(function() {
43 var $element = $(this);
44 var format = DateTimePicker.options.format;
45 var type = $element.data('dateType');
46 var options = {
47 pick12HourFormat: false,
48 pickDate: true,
49 pickTime: true,
50 useSeconds: false,
51 sideBySide: true,
52 icons: {
53 time: 'fa fa-clock-o',
54 date: 'fa fa-calendar',
55 up: 'fa fa-arrow-up',
56 down: 'fa fa-arrow-down'
57 }
58 };
59
60 // set options based on type
61 switch (type) {
62 case 'datetime':
63 options.format = format[1];
64 break;
65 case 'date':
66 options.format = format[0];
67 options.pickTime = false;
68 break;
69 case 'time':
70 options.pickDate = false;
71 options.format = 'hh:mm';
72 break;
73 case 'timesec':
74 options.format = 'hh:mm:ss';
75 options.pickDate = false;
76 options.useSeconds = true;
77 break;
78 case 'year':
79 options.format = 'YYYY';
80 options.pickDate = true;
81 options.pickTime = false;
82 break;
83 }
84
85 // initialize the date time picker on this element
86 $element.datetimepicker(options);
87 });
88
89 $dateTimeFields.on('blur', function(event) {
90
91 var $element = $(this);
92 var $hiddenField = $element.parent().find('input[type=hidden]');
93 var calculateTimeZoneOffset = $element.data('date-offset');
94
95 if ($element.val() === '') {
96 $hiddenField.val('');
97 } else {
98 var format = $element.data('DateTimePicker').format;
99 var date = moment($element.val(), format);
100 if (typeof calculateTimeZoneOffset !== 'undefined') {
101 var timeZoneOffset = parseInt(calculateTimeZoneOffset);
102 } else {
103 var timeZoneOffset = date.zone() * 60;
104 }
105
106 if (date.isValid()) {
107 $hiddenField.val(date.unix() - timeZoneOffset);
108 } else {
109 date = moment($hiddenField.val() + timeZoneOffset, 'X');
110 $element.val(date.format(format));
111 }
112 }
113 });
114
115 // on datepicker change, write the selected date with the timezone offset to the hidden field
116 $dateTimeFields.on('dp.change', function(event) {
117 var $element = $(this);
118 var date = event.date;
119 var calculateTimeZoneOffset = $element.data('date-offset');
120 if (typeof calculateTimeZoneOffset !== 'undefined') {
121 var timeZoneOffset = parseInt(calculateTimeZoneOffset);
122 } else {
123 var timeZoneOffset = date.zone() * 60;
124 }
125 var $hiddenField = $element.parent().find('input[type=hidden]');
126 $hiddenField.val(date.unix() - timeZoneOffset);
127 });
128 });
129 }
130 };
131
132 /**
133 * initialize function
134 */
135 return function() {
136 DateTimePicker.initialize();
137 return DateTimePicker;
138 }();
139 });