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