Debian Codegraph @

dijit/_TimePicker.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach
3 "dojo/date", // date.compare
4 "dojo/date/locale", // locale.format
5 "dojo/date/stamp", // stamp.fromISOString stamp.toISOString
6 "dojo/_base/declare", // declare
7 "dojo/dom-class", // domClass.add domClass.contains domClass.toggle
8 "dojo/dom-construct", // domConstruct.create
9 "dojo/_base/kernel", // deprecated
10 "dojo/keys", // keys
11 "dojo/_base/lang", // lang.mixin
12 "dojo/sniff", // has(...)
13 "dojo/query", // query
14 "dojo/mouse", // mouse.wheel
15 "dojo/on",
16 "./_WidgetBase",
17 "./form/_ListMouseMixin"
18 ], function(array, ddate, locale, stamp, declare, domClass, domConstruct, kernel, keys, lang, has, query, mouse, on,
19 _WidgetBase, _ListMouseMixin){
20
21 // module:
22 // dijit/_TimePicker
23
24
25 var TimePicker = declare("dijit._TimePicker", [_WidgetBase, _ListMouseMixin], {
26 // summary:
27 // A time picker dropdown, used by dijit/form/TimeTextBox.
28 // This widget is not available as a standalone widget due to lack of accessibility support.
29
30 // baseClass: [protected] String
31 // The root className to use for the various states of this widget
32 baseClass: "dijitTimePicker",
33
34 // pickerMin: String
35 // ISO-8601 string representing the time of the first
36 // visible element in the time picker.
37 // Set in local time, without a time zone.
38 pickerMin: "T00:00:00",
39
40 // pickerMax: String
41 // ISO-8601 string representing the last (possible) time
42 // added to the time picker.
43 // Set in local time, without a time zone.
44 pickerMax: "T23:59:59",
45
46 // clickableIncrement: String
47 // ISO-8601 string representing the interval between choices in the time picker.
48 // Set in local time, without a time zone.
49 // Example: `T00:15:00` creates 15 minute increments
50 // Must divide dijit/_TimePicker.visibleIncrement evenly
51 clickableIncrement: "T00:15:00",
52
53 // visibleIncrement: String
54 // ISO-8601 string representing the interval between "major" choices in the time picker.
55 // Each theme will highlight the major choices with a larger font / different color / etc.
56 // Set in local time, without a time zone.
57 // Example: `T01:00:00` creates text in every 1 hour increment
58 visibleIncrement: "T01:00:00",
59
60 // value: String
61 // Time to display.
62 // Defaults to current time.
63 // Can be a Date object or an ISO-8601 string.
64 // If you specify the GMT time zone (`-01:00`),
65 // the time will be converted to the local time in the local time zone.
66 // Otherwise, the time is considered to be in the local time zone.
67 // If you specify the date and isDate is true, the date is used.
68 // Example: if your local time zone is `GMT -05:00`,
69 // `T10:00:00` becomes `T10:00:00-05:00` (considered to be local time),
70 // `T10:00:00-01:00` becomes `T06:00:00-05:00` (4 hour difference),
71 // `T10:00:00Z` becomes `T05:00:00-05:00` (5 hour difference between Zulu and local time)
72 // `yyyy-mm-ddThh:mm:ss` is the format to set the date and time
73 // Example: `2007-06-01T09:00:00`
74 value: new Date(),
75
76 _visibleIncrement: 2,
77 _clickableIncrement: 1,
78 _totalIncrements: 10,
79
80 // constraints: TimePicker.__Constraints
81 // Specifies valid range of times (start time, end time), and also used by TimeTextBox to pass other
82 // options to the TimePicker: pickerMin, pickerMax, clickableIncrement, and visibleIncrement.
83 constraints: {},
84
85 /*=====
86 serialize: function(val, options){
87 // summary:
88 // User overridable function used to convert the attr('value') result to a String
89 // val: Date
90 // The current value
91 // options: Object?
92 // tags:
93 // protected
94 },
95 =====*/
96 serialize: stamp.toISOString,
97
98 /*=====
99 // filterString: string
100 // The string to filter by
101 filterString: "",
102 =====*/
103
104 buildRendering: function(){
105 this.inherited(arguments);
106 this.containerNode = this.domNode; // expected by _ListBase
107 this.timeMenu = this.domNode; // for back-compat
108 },
109
110 setValue: function(/*Date*/ value){
111 // summary:
112 // Deprecated. Used set('value') instead.
113 // tags:
114 // deprecated
115 kernel.deprecated("dijit._TimePicker:setValue() is deprecated. Use set('value', ...) instead.", "", "2.0");
116 this.set('value', value);
117 },
118
119 _setValueAttr: function(/*Date*/ date){
120 // summary:
121 // Hook so set('value', ...) works.
122 // description:
123 // Set the value of the TimePicker.
124 // Redraws the TimePicker around the new date.
125 // tags:
126 // protected
127 this._set("value", date);
128 this._showText();
129 },
130
131 _setFilterStringAttr: function(val){
132 // summary:
133 // Called by TimeTextBox to filter the values shown in my list
134 this._set("filterString", val);
135 this._showText();
136 },
137
138 isDisabledDate: function(/*===== dateObject, locale =====*/){
139 // summary:
140 // May be overridden to disable certain dates in the TimePicker e.g. `isDisabledDate=locale.isWeekend`
141 // dateObject: Date
142 // locale: String?
143 // type:
144 // extension
145 return false; // Boolean
146 },
147
148 _getFilteredNodes: function(/*number*/ start, /*number*/ maxNum, /*Boolean*/ before, /*DOMNode*/ lastNode){
149 // summary:
150 // Returns a DocumentFragment of nodes with the filter applied. At most maxNum nodes
151 // will be returned - but fewer may be returned as well. If the
152 // before parameter is set to true, then it will return the elements
153 // before the given index
154 // tags:
155 // private
156
157 var nodes = this.ownerDocument.createDocumentFragment();
158
159 for(var i = 0 ; i < this._maxIncrement; i++){
160 var n = this._createOption(i);
161 if(n){
162 nodes.appendChild(n);
163 }
164 }
165
166 return nodes;
167 },
168
169 _showText: function(){
170 // summary:
171 // Displays the relevant choices in the drop down list
172 // tags:
173 // private
174 var fromIso = stamp.fromISOString;
175 this.domNode.innerHTML = "";
176 this._clickableIncrementDate = fromIso(this.clickableIncrement);
177 this._visibleIncrementDate = fromIso(this.visibleIncrement);
178 // get the value of the increments to find out how many divs to create
179 var
180 sinceMidnight = function(/*Date*/ date){
181 return date.getHours() * 60 * 60 + date.getMinutes() * 60 + date.getSeconds();
182 },
183 clickableIncrementSeconds = sinceMidnight(this._clickableIncrementDate),
184 visibleIncrementSeconds = sinceMidnight(this._visibleIncrementDate),
185 // round reference date to previous visible increment
186 time = (this.value || this.currentFocus).getTime();
187
188 this._refDate = fromIso(this.pickerMin);
189 this._refDate.setFullYear(1970, 0, 1); // match parse defaults
190
191 // assume clickable increment is the smallest unit
192 this._clickableIncrement = 1;
193 // divide the visible range by the clickable increment to get the number of divs to create
194 // example: 10:00:00/00:15:00 -> display 40 divs
195 // divide the visible increments by the clickable increments to get how often to display the time inline
196 // example: 01:00:00/00:15:00 -> display the time every 4 divs
197 this._visibleIncrement = visibleIncrementSeconds / clickableIncrementSeconds;
198
199 // get the number of increments (i.e. number of entries in the picker)
200 var endDate = fromIso(this.pickerMax);
201 endDate.setFullYear(1970, 0, 1);
202 var visibleRange = (endDate.getTime() - this._refDate.getTime()) * 0.001;
203 this._maxIncrement = Math.ceil((visibleRange + 1) / clickableIncrementSeconds);
204
205 var nodes = this._getFilteredNodes();
206
207 // never show empty due to a bad filter
208 if(!nodes.firstChild && this.filterString){
209 this.filterString = '';
210 this._showText();
211 }else{
212 this.domNode.appendChild(nodes);
213 }
214 },
215
216 constructor: function(/*===== params, srcNodeRef =====*/){
217 // summary:
218 // Create the widget.
219 // params: Object|null
220 // Hash of initialization parameters for widget, including scalar values (like title, duration etc.)
221 // and functions, typically callbacks like onClick.
222 // The hash can contain any of the widget's properties, excluding read-only properties.
223 // srcNodeRef: DOMNode|String?
224 // If a srcNodeRef (DOM node) is specified, replace srcNodeRef with my generated DOM tree
225
226 this.constraints = {};
227 },
228
229 postMixInProperties: function(){
230 this.inherited(arguments);
231 this._setConstraintsAttr(this.constraints); // this needs to happen now (and later) due to codependency on _set*Attr calls
232 },
233
234 // For historical reasons TimeTextBox sends all the options for the _TimePicker inside of a constraints{} object
235 _setConstraintsAttr: function(/* Object */ constraints){
236 // brings in increments, etc.
237 for (var key in { clickableIncrement: 1, visibleIncrement: 1, pickerMin: 1, pickerMax: 1 }) {
238 if (key in constraints) {
239 this[key] = constraints[key];
240 }
241 }
242
243 // locale needs the lang in the constraints as locale
244 if(!constraints.locale){
245 constraints.locale = this.lang;
246 }
247 },
248
249 _createOption: function(/*Number*/ index){
250 // summary:
251 // Creates a clickable time option, or returns null if the specified index doesn't match the filter
252 // tags:
253 // private
254 var date = new Date(this._refDate);
255 var incrementDate = this._clickableIncrementDate;
256 date.setHours(date.getHours() + incrementDate.getHours() * index,
257 date.getMinutes() + incrementDate.getMinutes() * index,
258 date.getSeconds() + incrementDate.getSeconds() * index);
259 if(this.constraints.selector == "time"){
260 date.setFullYear(1970, 0, 1); // make sure each time is for the same date
261 }
262 var dateString = locale.format(date, this.constraints);
263 if(this.filterString && dateString.toLowerCase().indexOf(this.filterString) !== 0){
264 // Doesn't match the filter - return null
265 return null;
266 }
267
268 var div = this.ownerDocument.createElement("div");
269 div.className = this.baseClass + "Item";
270 div.date = date;
271 div.idx = index;
272 domConstruct.create('div', {
273 "class": this.baseClass + "ItemInner",
274 innerHTML: dateString
275 }, div);
276
277 var marker = index % this._visibleIncrement < 1 && index % this._visibleIncrement > -1,
278 tick = !marker && !(index % this._clickableIncrement);
279 if(marker){
280 div.className += " " + this.baseClass + "Marker";
281 }else if(tick){
282 div.className += " " + this.baseClass + "Tick";
283 }
284
285 if(this.isDisabledDate(date)){
286 // set disabled
287 div.className += " " + this.baseClass + "ItemDisabled";
288 }
289 if(this.value && !ddate.compare(this.value, date, this.constraints.selector)){
290 div.selected = true;
291 div.className += " " + this.baseClass + "ItemSelected";
292 this._selectedDiv = div;
293 if(marker){
294 div.className += " " + this.baseClass + "MarkerSelected";
295 }else if(tick){
296 div.className += " " + this.baseClass + "TickSelected";
297 }
298
299 // Initially highlight the current value. User can change highlight by up/down arrow keys
300 // or mouse movement.
301 this._highlightOption(div, true);
302 }
303 return div;
304 },
305
306 onOpen: function(){
307 this.inherited(arguments);
308
309 // Since _ListBase::_setSelectedAttr() calls scrollIntoView(), shouldn't call it until list is visible.
310 this.set("selected", this._selectedDiv);
311 },
312
313 _onOptionSelected: function(/*Object*/ tgt, /*Boolean*/ change){
314 // summary:
315 // Called when user clicks or keys to an option in the drop down list
316 // tgt: Object
317 // tgt.target specifies the node that was clicked
318 // change: Boolean
319 // If true, fire "change" event, otherwise just fire "input" event.
320 // tags:
321 // private
322 var tdate = tgt.target.date || tgt.target.parentNode.date;
323 if(!tdate || this.isDisabledDate(tdate)){
324 return;
325 }
326 this._set('value', tdate);
327 this.emit("input");
328 if(change) {
329 this._highlighted_option = null;
330 this.set('value', tdate);
331 this.onChange(tdate);
332 }
333 },
334
335 onChange: function(/*Date*/ /*===== time =====*/){
336 // summary:
337 // Notification that a time was selected. It may be the same as the previous value.
338 // tags:
339 // public
340 },
341
342 _highlightOption: function(/*node*/ node, /*Boolean*/ highlight){
343 // summary:
344 // Turns on/off highlight effect on a node based on mouse out/over event
345 // tags:
346 // private
347 if(!node){
348 return;
349 }
350 if(highlight){
351 if(this._highlighted_option){
352 this._highlightOption(this._highlighted_option, false);
353 }
354 this._highlighted_option = node;
355 }else if(this._highlighted_option !== node){
356 return;
357 }else{
358 this._highlighted_option = null;
359 }
360 domClass.toggle(node, this.baseClass + "ItemHover", highlight);
361 if(domClass.contains(node, this.baseClass + "Marker")){
362 domClass.toggle(node, this.baseClass + "MarkerHover", highlight);
363 }else{
364 domClass.toggle(node, this.baseClass + "TickHover", highlight);
365 }
366 },
367
368 handleKey: function(/*Event*/ e){
369 // summary:
370 // Called from `dijit/form/_DateTimeTextBox` to pass a keypress event
371 // from the `dijit/form/TimeTextBox` to be handled in this widget
372 // tags:
373 // protected
374 if(e.keyCode == keys.DOWN_ARROW){
375 this.selectNextNode();
376 this._onOptionSelected({target: this._highlighted_option}, false);
377 e.stopPropagation();
378 e.preventDefault();
379 return false;
380 }else if(e.keyCode == keys.UP_ARROW){
381 this.selectPreviousNode();
382 this._onOptionSelected({target: this._highlighted_option}, false);
383 e.stopPropagation();
384 e.preventDefault();
385 return false;
386 }else if(e.keyCode == keys.ENTER || e.keyCode === keys.TAB){
387 // mouse hover followed by TAB is NO selection
388 if(!this._keyboardSelected && e.keyCode === keys.TAB){
389 return true; // true means don't call stopEvent()
390 }
391
392 // Accept the currently-highlighted option as the value
393 if(this._highlighted_option){
394 this._onOptionSelected({target: this._highlighted_option}, true);
395 }
396
397 // Call stopEvent() for ENTER key so that form doesn't submit,
398 // but not for TAB, so that TAB does switch focus
399 return e.keyCode === keys.TAB;
400 }
401 return undefined;
402 },
403
404 // Implement abstract methods for _ListBase
405 onHover: function(/*DomNode*/ node){
406 this._highlightOption(node, true);
407 },
408
409 onUnhover: function(/*DomNode*/ node){
410 this._highlightOption(node, false);
411 },
412
413 onSelect: function(/*DomNode*/ node){
414 this._highlightOption(node, true);
415 },
416
417 onDeselect: function(/*DomNode*/ node){
418 this._highlightOption(node, false);
419 },
420
421 onClick: function(/*DomNode*/ node){
422 this._onOptionSelected({target: node}, true);
423 }
424 });
425
426 /*=====
427 TimePicker.__Constraints = declare(locale.__FormatOptions, {
428 // clickableIncrement: String
429 // See `dijit/_TimePicker.clickableIncrement`
430 clickableIncrement: "T00:15:00"
431 });
432 =====*/
433
434 return TimePicker;
435 });