| 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 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
touchStartEditingTimer: 500, |
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 33 |
|
| 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 |
|
| 50 |
|
| 51 |
if(p.startEditingTimer){ |
| 52 |
clearTimeout(p.startEditingTimer); |
| 53 |
} |
| 54 |
|
| 55 |
this._endItemEditing("touch", false); |
| 56 |
p = null; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
if(!p){ |
| 62 |
|
| 63 |
|
| 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 |
|
| 85 |
lang.mixin(p, this._getTouchesOnRenderers(e, p.editedItem)); |
| 86 |
|
| 87 |
|
| 88 |
this._startTouchItemEditingGesture(e); |
| 89 |
|
| 90 |
} else { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if(e.touches.length > 1){ |
| 95 |
event.stop(e); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 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 |
|
| 120 |
this._edProps.startEditingTimer = setTimeout(lang.hitch(this, function(){ |
| 121 |
|
| 122 |
|
| 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 |
|
| 140 |
this._startItemEditingGesture([this.getTime(e)], "move", "touch", e); |
| 141 |
|
| 142 |
}), this.touchStartEditingTimer); |
| 143 |
|
| 144 |
} |
| 145 |
} |
| 146 |
}, |
| 147 |
|
| 148 |
_docEditingTouchMoveHandler: function(e){ |
| 149 |
|
| 150 |
|
| 151 |
var p = this._edProps; |
| 152 |
|
| 153 |
|
| 154 |
|
| 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 |
|
| 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 |
|
| 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 |
} |
| 224 |
|
| 225 |
}, |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
autoScrollTouchMargin: 10, |
| 230 |
|
| 231 |
_docEditingTouchEndHandler: function(e){ |
| 232 |
|
| 233 |
|
| 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 |
|
| 252 |
this._endItemEditingGesture("touch", e); |
| 253 |
|
| 254 |
if(this.touchEndEditingTimer > 0){ |
| 255 |
|
| 256 |
|
| 257 |
p.endEditingTimer = setTimeout(lang.hitch(this, function(){ |
| 258 |
|
| 259 |
this._endItemEditing("touch", false); |
| 260 |
|
| 261 |
}), this.touchEndEditingTimer); |
| 262 |
} |
| 263 |
|
| 264 |
}else{ |
| 265 |
|
| 266 |
if(this._editingGesture){ |
| 267 |
this._endItemEditingGesture("touch", e); |
| 268 |
} |
| 269 |
|
| 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 |
|
| 284 |
clearTimeout(this._touchSelectionTimer); |
| 285 |
this.selectFromEvent(e, p.item._item, p.renderer, true); |
| 286 |
|
| 287 |
}else if(this._pendingSelectedItem){ |
| 288 |
|
| 289 |
this.dispatchChange(this._saveSelectedItems.length == 0 ? null : this._saveSelectedItems[0], |
| 290 |
this._pendingSelectedItem, null, e); |
| 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 |
|
| 326 |
|
| 327 |
if(this._saveSelectedItems){ |
| 328 |
|
| 329 |
|
| 330 |
|
| 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 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
var p = this._edProps; |
| 351 |
|
| 352 |
var fromResizeStart = p.resizeStartTouchIndex != -1; |
| 353 |
var fromResizeEnd = p.resizeEndTouchIndex != -1; |
| 354 |
|
| 355 |
if(fromResizeStart && fromResizeEnd || |
| 356 |
this._editingGesture && p.touchesLen == 2 && |
| 357 |
(fromResizeEnd && p.editKind == "resizeStart" || |
| 358 |
fromResizeStart && p.editKind =="resizeEnd")){ |
| 359 |
|
| 360 |
if(this._editingGesture && p.editKind != "resizeBoth"){ |
| 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 |
|
| 382 |
this._startItemEditingGesture([this.getTime(e)], "move", "touch", e); |
| 383 |
} |
| 384 |
}, |
| 385 |
|
| 386 |
_getTouchesOnRenderers: function(e, item){ |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 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 |
|
| 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 |
}); |