Debian Codegraph @

dojox/calendar/Keyboard.js

raw symbols SCIP
1 define(["dojo/_base/array", "dojo/_base/lang", "dojo/_base/declare", "dojo/on", "dojo/_base/event", "dojo/keys"],
2 function(arr, lang, declare, on, event, keys){
3
4 return declare("dojox.calendar.Keyboard", null, {
5
6 // summary:
7 // This mixin is managing the keyboard interactions on a calendar view.
8
9 // keyboardUpDownUnit: String
10 // Unit used during editing of an event using the keyboard and the up or down keys were pressed. Valid values are "week", "day", "hours" "minute".
11 keyboardUpDownUnit: "minute",
12
13 // keyboardUpDownSteps: Integer
14 // Steps used during editing of an event using the keyboard and the up or down keys were pressed.
15 keyboardUpDownSteps: 15,
16
17 // keyboardLeftRightUnit: String
18 // Unit used during editing of an event using the keyboard and the left or right keys were pressed. Valid values are "week", "day", "hours" "minute".
19 keyboardLeftRightUnit: "day",
20
21 // keyboardLeftRightSteps: Integer
22 // Unit used during editing of an event using the keyboard and the left or right keys were pressed.
23 keyboardLeftRightSteps: 1,
24
25 // allDayKeyboardUpDownUnit: Integer
26 // Steps used during editing of an all day event using the keyboard and the up or down keys were pressed.
27 allDayKeyboardUpDownUnit: "day",
28
29 // allDayKeyboardUpDownSteps: String
30 // Unit used during editing of an all day event using the keyboard and the up or down keys were pressed. Valid values are "week", "day", "hours" "minute".
31 allDayKeyboardUpDownSteps: 7,
32
33 // allDayKeyboardLeftRightUnit: Integer
34 // Steps used during editing of an all day event using the keyboard and the up or down keys were pressed.
35 allDayKeyboardLeftRightUnit: "day",
36
37 // allDayKeyboardLeftRightSteps: String
38 // Unit used during editing of an all day event using the keyboard and the left or right keys were pressed. Valid values are "week", "day", "hours" "minute".
39 allDayKeyboardLeftRightSteps: 1,
40
41 postCreate: function(){
42 this.inherited(arguments);
43 this._viewHandles.push(on(this.domNode, "keydown", lang.hitch(this, this._onKeyDown)));
44 },
45
46 // resizeModfier: String
47 // The modifier used to determine if the item is resized instead moved during the editing on an item.
48 resizeModifier: "ctrl",
49
50 // maxScrollAnimationDuration: Number
51 // The duration in milliseconds to scroll the entire view.
52 // The scroll speed is constant when scrolling to show an item renderer.
53 maxScrollAnimationDuration: 1000,
54
55 ///////////////////////////////////////////////////////////////
56 //
57 // Focus management
58 //
59 //////////////////////////////////////////////////////////////
60
61 // tabIndex: String
62 // Order fields are traversed when user hits the tab key
63 tabIndex: "0",
64
65 // focusedItem: Object
66 // The data item that currently has the focus.
67 focusedItem: null,
68
69 _isItemFocused: function(item){
70 return this.focusedItem != null && this.focusedItem.id == item.id;
71 },
72
73 _setFocusedItemAttr: function(value){
74 if(value != this.focusedItem){
75 var old = this.focusedItem;
76 this._set("focusedItem", value);
77 this.updateRenderers([old, this.focusedItem], true);
78 this.onFocusChange({
79 oldValue: old,
80 newValue: value
81 });
82 }
83 if(value != null){
84 if(this.owner != null && this.owner.get("focusedItem") != null){
85 this.owner.set("focusedItem", null);
86 }
87 if(this._secondarySheet != null && this._secondarySheet.set("focusedItem") != null){
88 this._secondarySheet.set("focusedItem", null);
89 }
90 }
91 },
92
93 onFocusChange: function(e){
94 // summary:
95 // Event dispatched when the focus has changed.
96 // tags:
97 // callback
98
99 },
100
101 // showFocus: Boolean
102 // Show or hide the focus graphic feedback on item renderers.
103 showFocus: false,
104
105 _focusNextItem: function(dir){
106 // summary:
107 // Moves the focus to the next item in the specified direction.
108 // If there is no current child focused, the first (dir == 1) or last (dir == -1) is focused.
109 // dir: Integer
110 // The direction of the next child to focus.
111 //
112 // - 1: Move focus to the next item in the list.
113 // - -1: Move focus to the previous item in the list.
114
115 if(!this.renderData || !this.renderData.items || this.renderData.items.length == 0){
116 return null;
117 }
118
119 var index = -1;
120 var list = this.renderData.items;
121 var max = list.length - 1;
122 var focusedItem = this.get("focusedItem");
123
124 // find current index.
125 if(focusedItem == null){
126 index = dir > 0 ? 0 : max;
127 }else{
128 arr.some(list, lang.hitch(this, function(item, i){
129 var found = item.id == focusedItem.id;
130 if(found){
131 index = i;
132 }
133 return found;
134 }));
135 index = this._focusNextItemImpl(dir, index, max);
136 }
137
138 // find the first item with renderers.
139 var reachedOnce = false;
140 var old = -1;
141
142 while(old != index && (!reachedOnce || index != 0)){
143
144 if(!reachedOnce && index == 0){
145 reachedOnce = true;
146 }
147
148 var item = list[index];
149
150 if(this.rendererManager.itemToRenderer[item.id] != null){
151 // found item
152 this.set("focusedItem", item);
153 return;
154 }
155 old = index;
156 index = this._focusNextItemImpl(dir, index, max);
157
158 }
159 },
160
161 _focusNextItemImpl: function(dir, index, max){
162 // tags:
163 // private
164
165 if(index == -1){ // not found should not occur
166 index = dir > 0 ? 0 : max;
167 }else{
168 if(index == 0 && dir == -1 || index == max && dir == 1){
169 return index;
170 }
171 index = dir > 0 ? ++index : --index;
172 }
173 return index;
174 },
175
176 ///////////////////////////////////////////////////////////
177 //
178 // Keyboard
179 //
180 //////////////////////////////////////////////////////////
181
182 _handlePrevNextKeyCode: function(e, dir){
183 // tags:
184 // private
185
186 if(!this.isLeftToRight()){
187 dir = dir == 1 ? -1 : 1;
188 }
189 this.showFocus = true;
190 this._focusNextItem(dir);
191
192 var focusedItem = this.get("focusedItem");
193
194 if(!e.ctrlKey && focusedItem){
195 this.set("selectedItem", focusedItem);
196 }
197
198 if(focusedItem){
199 this.ensureVisibility(focusedItem.startTime, focusedItem.endTime, "both", undefined, this.maxScrollAnimationDuration);
200 }
201 },
202
203 _checkDir: function(dir, value){
204 return this.isLeftToRight() && dir == value || !this.isLeftToRight() && dir == (value=="left"?"right":"left");
205 },
206
207 _keyboardItemEditing: function(e, dir){
208 // tags:
209 // private
210
211 event.stop(e);
212
213 var p = this._edProps;
214
215 var unit, steps;
216
217 if(p.editedItem.allDay || this.roundToDay || p.rendererKind == "label"){
218 unit = dir == "up" || dir == "down" ? this.allDayKeyboardUpDownUnit : this.allDayKeyboardLeftRightUnit;
219 steps = dir == "up" || dir == "down" ? this.allDayKeyboardUpDownSteps : this.allDayKeyboardLeftRightSteps;
220 }else{
221 unit = dir == "up" || dir == "down" ? this.keyboardUpDownUnit : this.keyboardLeftRightUnit;
222 steps = dir == "up" || dir == "down" ? this.keyboardUpDownSteps : this.keyboardLeftRightSteps;
223 }
224
225 if(dir == "up" || this._checkDir(dir, "left")){
226 steps = -steps;
227 }
228
229 var editKind = e[this.resizeModifier+"Key"] ? "resizeEnd" : "move";
230
231 var d = editKind == "resizeEnd" ? p.editedItem.endTime : p.editedItem.startTime;
232
233 var newTime = d;
234 var subColumn = p.editedItem.subColumn;
235
236 if(editKind == "move" && this.subColumns && this.subColumns.length > 1){
237 var idx = this.getSubColumnIndex(subColumn);
238 var updateTime = true;
239 if(idx != -1){
240 if(this._checkDir(dir, "left")){
241 if(idx == 0){
242 subColumn = this.subColumns[this.subColumns.length-1];
243 }else{
244 updateTime = false;
245 subColumn = this.subColumns[idx-1];
246 }
247 }else if(this._checkDir(dir, "right")){
248 if(idx == this.subColumns.length-1){
249 subColumn = this.subColumns[0];
250 }else{
251 updateTime = false;
252 subColumn = this.subColumns[idx+1];
253 }
254 }
255 if(updateTime){
256 newTime = this.renderData.dateModule.add(d, unit, steps);
257 }
258 }
259 }else{
260 newTime = this.renderData.dateModule.add(d, unit, steps);
261 }
262
263 this._startItemEditingGesture([d], editKind, "keyboard", e);
264 this._moveOrResizeItemGesture([newTime], "keyboard", e, subColumn);
265 this._endItemEditingGesture(editKind, "keyboard", e, false);
266
267 if(editKind == "move"){
268 if(this.renderData.dateModule.compare(newTime, d) == -1){
269 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "start");
270 }else{
271 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "end");
272 }
273 }else{ // resize end only
274 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "end");
275 }
276 },
277
278 _onKeyDown: function(e){
279 // tags:
280 // private
281
282 var focusedItem = this.get("focusedItem");
283
284 switch(e.keyCode){
285
286 case keys.ESCAPE:
287
288 if(this._isEditing){
289
290 if(this._editingGesture){
291 this._endItemEditingGesture("keyboard", e, true);
292 }
293
294 this._endItemEditing("keyboard", true);
295
296 this._edProps = null;
297 }
298 break;
299
300 case keys.SPACE:
301
302 event.stop(e); // prevent browser shortcut
303
304 if(focusedItem != null){
305 this.setItemSelected(focusedItem, e.ctrlKey ? !this.isItemSelected(focusedItem) : true);
306 }
307 break;
308
309 case keys.ENTER:
310
311 event.stop(e); // prevent browser shortcut
312
313 if(focusedItem != null){
314
315 if(this._isEditing){
316 this._endItemEditing("keyboard", false);
317 }else{
318
319 var renderers = this.rendererManager.itemToRenderer[focusedItem.id];
320
321 if(renderers && renderers.length > 0 && this.isItemEditable(focusedItem, renderers[0].kind)){
322
323 this._edProps = {
324 renderer: renderers[0],
325 rendererKind: renderers[0].kind,
326 tempEditedItem: focusedItem,
327 liveLayout: this.liveLayout
328 };
329
330 this.set("selectedItem", focusedItem);
331
332 this._startItemEditing(focusedItem, "keyboard");
333 }
334 }
335 }
336 break;
337
338 case keys.LEFT_ARROW:
339
340 event.stop(e); // prevent browser shortcut
341
342 if(this._isEditing){
343 this._keyboardItemEditing(e, "left");
344 }else{
345 this._handlePrevNextKeyCode(e, -1);
346 }
347 break;
348
349 case keys.RIGHT_ARROW:
350
351 event.stop(e); // prevent browser shortcut
352
353 if(this._isEditing){
354 this._keyboardItemEditing(e, "right");
355 }else{
356 this._handlePrevNextKeyCode(e, 1);
357 }
358 break;
359
360 case keys.UP_ARROW:
361 if(this._isEditing){
362 this._keyboardItemEditing(e, "up");
363 }else if(this.scrollable){
364 this.scrollView(-1);
365 }
366 break;
367
368 case keys.DOWN_ARROW:
369 if(this._isEditing){
370 this._keyboardItemEditing(e, "down");
371 }else if(this.scrollable){
372 this.scrollView(1);
373 }
374 break;
375
376 }
377
378 }
379 });
380 });