Debian Codegraph @

dojox/calendar/Touch.js

raw symbols SCIP
1 define(["dojo/_base/array", "dojo/_base/lang", "dojo/_base/declare", "dojo/dom", "dojo/dom-geometry", "dojo/_base/window", "dojo/on", "dojo/_base/event", "dojo/keys"],
2
3 function(arr, lang, declare, dom, domGeometry, win, on, event, keys){
4
5 return declare("dojox.calendar.Touch", null, {
6
7 // summary:
8 // This plugin is managing the touch interactions on item renderers displayed by a calendar view.
9
10 // touchStartEditingTimer: Integer
11 // The delay of one touch over the renderer before setting the item in editing mode.
12 touchStartEditingTimer: 500,
13
14 // touchEndEditingTimer: Integer
15 // The delay after which the item is leaving the editing mode after the previous editing gesture, in touch context.
16 touchEndEditingTimer: 10000,
17
18 postMixInProperties: function(){
19
20 this.on("rendererCreated", lang.hitch(this, function(irEvent){
21
22 var renderer = irEvent.renderer.renderer;
23
24 this.own(on(renderer.domNode, "touchstart", lang.hitch(this, function(e){
25 this._onRendererTouchStart(e, renderer);
26 })));
27
28 }));
29 },
30
31 _onRendererTouchStart: function(e, renderer){
32 // tags:
33 // private
34 var p = this._edProps;
35
36 if(p && p.endEditingTimer){
37 clearTimeout(p.endEditingTimer);
38 p.endEditingTimer = null;
39 }
40
41 var theItem = renderer.item.item;
42
43 if(p && p.endEditingTimer){
44 clearTimeout(p.endEditingTimer);
45 p.endEditingTimer = null;
46 }
47
48 if(p != null && p.item != theItem){
49 // another item is edited.
50 // stop previous item
51 if(p.startEditingTimer){
52 clearTimeout(p.startEditingTimer);
53 }
54
55 this._endItemEditing("touch", false);
56 p = null;
57
58 }
59
60 // initialize editing properties
61 if(!p){
62
63 // register event listeners to manage gestures.
64 var handles = [];
65
66 handles.push(on(win.doc, "touchend", lang.hitch(this, this._docEditingTouchEndHandler)));
67 handles.push(on(this.itemContainer, "touchmove", lang.hitch(this, this._docEditingTouchMoveHandler)));
68
69 this._setEditingProperties({
70 touchMoved: false,
71 item: theItem,
72 renderer: renderer,
73 rendererKind: renderer.rendererKind,
74 event: e,
75 handles: handles,
76 liveLayout: this.liveLayout
77 });
78
79 p = this._edProps;
80 }
81
82 if(this._isEditing){
83
84 // get info on touches
85 lang.mixin(p, this._getTouchesOnRenderers(e, p.editedItem));
86
87 // start an editing gesture.
88 this._startTouchItemEditingGesture(e);
89
90 } else {
91
92 // initial touch that will trigger or not the editing
93
94 if(e.touches.length > 1){
95 event.stop(e);
96 return;
97 }
98
99 // set the selection state without dispatching (on touch end) after a short amount of time.
100 // to allow a bit of time to scroll without selecting (graphically at least)
101 this._touchSelectionTimer = setTimeout(lang.hitch(this, function(){
102
103 this._saveSelectedItems = this.get("selectedItems");
104
105 var changed = this.selectFromEvent(e, theItem._item, renderer, false);
106
107 if(changed){
108 this._pendingSelectedItem = theItem;
109 }else{
110 delete this._saveSelectedItems;
111 }
112 this._touchSelectionTimer = null;
113 }), 200);
114
115 p.start = {x: e.touches[0].screenX, y: e.touches[0].screenY};
116
117 if(this.isItemEditable(p.item, p.rendererKind)){
118
119 // editing gesture timer
120 this._edProps.startEditingTimer = setTimeout(lang.hitch(this, function(){
121
122 // we are editing, so the item *must* be selected.
123 if(this._touchSelectionTimer){
124 clearTimeout(this._touchSelectionTimer);
125 delete this._touchSelectionTime;
126 }
127 if(this._pendingSelectedItem){
128 this.dispatchChange(this._saveSelectedItems == null ? null : this._saveSelectedItems[0], this._pendingSelectedItem, null, e);
129 delete this._saveSelectedItems;
130 delete this._pendingSelectedItem;
131 }else{
132 this.selectFromEvent(e, theItem._item, renderer);
133 }
134
135 this._startItemEditing(p.item, "touch", e);
136
137 p.moveTouchIndex = 0;
138
139 // A move gesture is initiated even if we don't move
140 this._startItemEditingGesture([this.getTime(e)], "move", "touch", e);
141
142 }), this.touchStartEditingTimer);
143
144 }
145 }
146 },
147
148 _docEditingTouchMoveHandler: function(e){
149 // tags:
150 // private
151 var p = this._edProps;
152
153 // When the screen is touched, it can dispatch move events if the
154 // user press the finger a little more...
155 var touch = {x: e.touches[0].screenX, y: e.touches[0].screenY};
156 if(p.startEditingTimer &&
157 (Math.abs(touch.x - p.start.x) > 25 ||
158 Math.abs(touch.y - p.start.y) > 25)) {
159
160 // scroll use case, do not edit
161 clearTimeout(p.startEditingTimer);
162 p.startEditingTimer = null;
163
164 clearTimeout(this._touchSelectionTimer);
165 this._touchSelectionTimer = null;
166
167 if(this._pendingSelectedItem){
168 delete this._pendingSelectedItem;
169 this.selectFromEvent(e, null, null, false);
170 }
171 }
172
173 // 10px tolerance to select the item (#105)
174 if(!p.touchMoved &&
175 (Math.abs(touch.x - p.start.x) > 10 ||
176 Math.abs(touch.y - p.start.y) > 10)) {
177 p.touchMoved = true;
178 }
179
180 if(this._editingGesture){
181
182 event.stop(e);
183
184 if(p.itemBeginDispatched){
185
186 var times = [];
187 var d = p.editKind == "resizeEnd" ? p.editedItem.endTime : p.editedItem.startTime;
188 var subColumn = p.editedItem.subColumn;
189
190 switch(p.editKind){
191 case "move":
192 var touchIndex = p.moveTouchIndex == null || p.moveTouchIndex < 0 ? 0 : p.moveTouchIndex;
193 times[0] = this.getTime(e, -1, -1, touchIndex);
194 subColumn = this.getSubColumn(e, -1, -1, touchIndex);
195 break;
196 case "resizeStart":
197 times[0] = this.getTime(e, -1, -1, p.resizeStartTouchIndex);
198 break;
199 case "resizeEnd":
200 times[0] = this.getTime(e, -1, -1, p.resizeEndTouchIndex);
201 break;
202 case "resizeBoth":
203 times[0] = this.getTime(e, -1, -1, p.resizeStartTouchIndex);
204 times[1] = this.getTime(e, -1, -1, p.resizeEndTouchIndex);
205 break;
206 }
207
208 this._moveOrResizeItemGesture(times, "touch", e, subColumn);
209
210 if(p.editKind == "move"){
211 if(this.renderData.dateModule.compare(p.editedItem.startTime, d) == -1){
212 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "start", this.autoScrollTouchMargin);
213 }else{
214 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "end", this.autoScrollTouchMargin);
215 }
216 }else if(e.editKind == "resizeStart" || e.editKind == "resizeBoth"){
217 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "start", this.autoScrollTouchMargin);
218 }else{
219 this.ensureVisibility(p.editedItem.startTime, p.editedItem.endTime, "end", this.autoScrollTouchMargin);
220 }
221
222 }
223 } // else scroll, if any, is delegated to sub class
224
225 },
226
227 // autoScrollTouchMargin: Integer
228 // The minimum number of minutes of margin around the edited event.
229 autoScrollTouchMargin: 10,
230
231 _docEditingTouchEndHandler: function(e){
232 // tags:
233 // private
234 event.stop(e);
235
236 var p = this._edProps;
237
238 if(p.startEditingTimer){
239 clearTimeout(p.startEditingTimer);
240 p.startEditingTimer = null;
241 }
242
243 if(this._isEditing){
244
245 lang.mixin(p, this._getTouchesOnRenderers(e, p.editedItem));
246
247 if(this._editingGesture){
248
249 if(p.touchesLen == 0){
250
251 // all touches were removed => end of editing gesture
252 this._endItemEditingGesture("touch", e);
253
254 if(this.touchEndEditingTimer > 0){
255
256 // Timer that trigger the end of the item editing mode.
257 p.endEditingTimer = setTimeout(lang.hitch(this, function(){
258
259 this._endItemEditing("touch", false);
260
261 }), this.touchEndEditingTimer);
262 } // else validation must be explicit
263
264 }else{
265
266 if(this._editingGesture){
267 this._endItemEditingGesture("touch", e);
268 }
269 // there touches of interest on item, process them.
270 this._startTouchItemEditingGesture(e);
271 }
272 }
273
274 }else if(!p.touchMoved){
275
276 event.stop(e);
277
278 arr.forEach(p.handles, function(handle){
279 handle.remove();
280 });
281
282 if(this._touchSelectionTimer){
283 // selection timer was not reached to a proper selection.
284 clearTimeout(this._touchSelectionTimer);
285 this.selectFromEvent(e, p.item._item, p.renderer, true);
286
287 }else if(this._pendingSelectedItem){
288 // selection timer was reached, dispatch change event
289 this.dispatchChange(this._saveSelectedItems.length == 0 ? null : this._saveSelectedItems[0],
290 this._pendingSelectedItem, null, e); // todo renderer ?
291 delete this._saveSelectedItems;
292 delete this._pendingSelectedItem;
293 }
294
295 if(this._pendingDoubleTap && this._pendingDoubleTap.item == p.item){
296 this._onItemDoubleClick({
297 triggerEvent: e,
298 renderer: p.renderer,
299 item: p.item._item
300 });
301
302 clearTimeout(this._pendingDoubleTap.timer);
303
304 delete this._pendingDoubleTap;
305
306 }else{
307
308 this._pendingDoubleTap = {
309 item: p.item,
310 timer: setTimeout(lang.hitch(this, function(){
311 delete this._pendingDoubleTap;
312 }), this.doubleTapDelay)
313 };
314
315 this._onItemClick({
316 triggerEvent: e,
317 renderer: p.renderer,
318 item: p.item._item
319 });
320 }
321
322 this._edProps = null;
323
324 }else{
325 // scroll view has finished.
326
327 if(this._saveSelectedItems){
328
329 // selection without dipatching was done, but the view scrolled,
330 // so revert last selection
331 this.set("selectedItems", this._saveSelectedItems);
332 delete this._saveSelectedItems;
333 delete this._pendingSelectedItem;
334 }
335
336 arr.forEach(p.handles, function(handle){
337 handle.remove();
338 });
339
340 this._edProps = null;
341 }
342 },
343
344 _startTouchItemEditingGesture: function(e){
345 // summary:
346 // Determines if a editing gesture is starting according to touches.
347 // tags:
348 // private
349
350 var p = this._edProps;
351
352 var fromResizeStart = p.resizeStartTouchIndex != -1;
353 var fromResizeEnd = p.resizeEndTouchIndex != -1;
354
355 if(fromResizeStart && fromResizeEnd || // initial gesture using two touches
356 this._editingGesture && p.touchesLen == 2 &&
357 (fromResizeEnd && p.editKind == "resizeStart" ||
358 fromResizeStart && p.editKind =="resizeEnd")){ // gesture one after the other touch
359
360 if(this._editingGesture && p.editKind != "resizeBoth"){ // stop ongoing gesture
361 this._endItemEditingGesture("touch", e);
362 }
363
364 p.editKind = "resizeBoth";
365
366 this._startItemEditingGesture([this.getTime(e, -1, -1, p.resizeStartTouchIndex),
367 this.getTime(e, -1, -1, p.resizeEndTouchIndex)],
368 p.editKind, "touch", e);
369
370 }else if(fromResizeStart && p.touchesLen == 1 && !this._editingGesture){
371
372 this._startItemEditingGesture([this.getTime(e, -1, -1, p.resizeStartTouchIndex)],
373 "resizeStart", "touch", e);
374
375 }else if(fromResizeEnd && p.touchesLen == 1 && !this._editingGesture){
376
377 this._startItemEditingGesture([this.getTime(e, -1, -1, p.resizeEndTouchIndex)],
378 "resizeEnd", "touch", e);
379
380 } else {
381 // A move gesture is initiated even if we don't move
382 this._startItemEditingGesture([this.getTime(e)], "move", "touch", e);
383 }
384 },
385
386 _getTouchesOnRenderers: function(e, item){
387 // summary:
388 // Returns the touch indices that are on a editing handles or body of the renderers
389 // tags:
390 // private
391 // item: Object
392 // The render item.
393 // e: Event
394 // The touch event.
395 // tags:
396 // private
397
398 var irs = this._getStartEndRenderers(item);
399
400 var resizeStartTouchIndex = -1;
401 var resizeEndTouchIndex = -1;
402 var moveTouchIndex = -1;
403 var hasResizeStart = irs[0] != null && irs[0].resizeStartHandle != null;
404 var hasResizeEnd = irs[1] != null && irs[1].resizeEndHandle != null;
405 var len = 0;
406 var touched = false;
407 var list = this.rendererManager.itemToRenderer[item.id];
408
409 for(var i=0; i<e.touches.length; i++){
410
411 if(resizeStartTouchIndex == -1 && hasResizeStart){
412 touched = dom.isDescendant(e.touches[i].target, irs[0].resizeStartHandle);
413 if(touched){
414 resizeStartTouchIndex = i;
415 len++;
416 }
417 }
418
419 if(resizeEndTouchIndex == -1 && hasResizeEnd){
420 touched = dom.isDescendant(e.touches[i].target, irs[1].resizeEndHandle);
421 if(touched){
422 resizeEndTouchIndex = i;
423 len++;
424 }
425 }
426
427 if(resizeStartTouchIndex == -1 && resizeEndTouchIndex == -1){
428
429 for (var j=0; j<list.length; j++){
430 touched = dom.isDescendant(e.touches[i].target, list[j].container);
431 if(touched){
432 moveTouchIndex = i;
433 len++;
434 break;
435 }
436 }
437 }
438
439 if(resizeStartTouchIndex != -1 && resizeEndTouchIndex != -1 && moveTouchIndex != -1){
440 // all touches of interest were found, ignore other ones.
441 break;
442 }
443 }
444
445 return {
446 touchesLen: len,
447 resizeStartTouchIndex: resizeStartTouchIndex,
448 resizeEndTouchIndex: resizeEndTouchIndex,
449 moveTouchIndex: moveTouchIndex
450 };
451 }
452
453 });
454
455 });