Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview Behavior for handling dragging elements in a container. | |
| 7 * Draggable elements must have the 'draggable' attribute set. | |
| 8 */ | |
| 9 | |
| 10 /** | |
| 11 * @typedef {{ | |
| 12 * x: number, | |
| 13 * y: number | |
| 14 * }} | |
| 15 */ | |
| 16 var DragPosition; | |
| 17 | |
| 18 /** @polymerBehavior */ | |
| 19 var DragBehavior = { | |
| 20 /** | |
| 21 * The id of the element being dragged, or empty if not dragging. | |
| 22 * @private {string} | |
| 23 */ | |
| 24 dragId_: '', | |
| 25 | |
| 26 /** @private {boolean} */ | |
| 27 enabled_: false, | |
| 28 | |
| 29 /** @private {!HTMLDivElement|undefined} */ | |
| 30 container_: undefined, | |
| 31 | |
| 32 /** @private {?function(string, ?DragPosition):void} */ | |
| 33 callback_: null, | |
| 34 | |
| 35 /** @private {?DragPosition} */ | |
|
michaelpg
2016/06/24 21:00:10
is it really nullable? (if so, initialize to null
stevenjb
2016/06/24 21:44:12
Done.
| |
| 36 dragStartLocation_: {x: 0, y: 0}, | |
|
michaelpg
2016/06/24 21:00:11
used elsewhere without the underscore
stevenjb
2016/06/24 21:44:12
<rant> It never ceases to surprise me what Closure
| |
| 37 | |
| 38 /** | |
| 39 * Used to ignore unnecessary drag events. | |
| 40 * @private {?DragPosition} | |
| 41 */ | |
| 42 lastTouchLocation_: null, | |
| 43 | |
| 44 /** @private {?function(!Event)} */ | |
| 45 mouseUpListener_: null, | |
| 46 | |
| 47 /** | |
| 48 * @param {boolean} enabled | |
| 49 * @param {!HTMLDivElement=} opt_container | |
| 50 * @param {!function(string, ?DragPosition):void=} opt_callback | |
| 51 */ | |
| 52 initializeDrag: function(enabled, opt_container, opt_callback) { | |
| 53 this.enabled_ = enabled; | |
| 54 if (!enabled) { | |
| 55 if (this.container) { | |
| 56 this.container.onmousemove = null; | |
|
michaelpg
2016/06/24 21:00:11
throughout: use addEventListener/removeEventListen
stevenjb
2016/06/24 21:44:12
Ugh. Yeah. Done.
| |
| 57 this.container.ontouchmove = null; | |
| 58 this.container.ontouchend = null; | |
| 59 } | |
| 60 if (this.mouseUpListener_) { | |
| 61 window.removeEventListener('keydown', this.mouseUpListener_); | |
| 62 this.mouseUpListener_ = null; | |
| 63 } | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 if (opt_container !== undefined) | |
| 68 this.container_ = opt_container; | |
| 69 var container = this.container_; | |
| 70 assert(container); | |
| 71 // Mouse | |
| 72 container.onmousedown = this.onMouseDown_.bind(this); | |
| 73 container.onmousemove = this.onMouseMove_.bind(this); | |
| 74 this.mouseUpListener_ = this.endDrag_.bind(this); | |
| 75 window.addEventListener('mouseup', this.mouseUpListener_, true); | |
| 76 // Touch | |
| 77 container.ontouchstart = this.onTouchStart_.bind(this); | |
| 78 container.ontouchmove = this.onTouchMove_.bind(this); | |
| 79 container.ontouchend = this.endDrag_.bind(this); | |
| 80 | |
| 81 if (opt_callback !== undefined) | |
| 82 this.callback_ = opt_callback; | |
| 83 }, | |
| 84 | |
| 85 /** | |
| 86 * @param {Event} e The mouse down event. | |
| 87 * @return {boolean} | |
| 88 * @private | |
| 89 */ | |
| 90 onMouseDown_: function(e) { | |
| 91 if (e.button != 0) | |
| 92 return true; | |
| 93 if (!e.target.getAttribute('draggable')) | |
| 94 return true; | |
| 95 e.preventDefault(); | |
| 96 var target = assertInstanceof(e.target, HTMLElement); | |
| 97 return this.startDrag_(target, {x: e.pageX, y: e.pageY}); | |
| 98 }, | |
| 99 | |
| 100 /** | |
| 101 * @param {Event} e The mouse move event. | |
| 102 * @return {boolean} | |
| 103 * @private | |
| 104 */ | |
| 105 onMouseMove_: function(e) { | |
| 106 e.preventDefault(); | |
| 107 return this.processDrag_(e, {x: e.pageX, y: e.pageY}); | |
| 108 }, | |
| 109 | |
| 110 /** | |
| 111 * @param {Event} e The touch start event. | |
| 112 * @return {boolean} | |
| 113 * @private | |
| 114 */ | |
| 115 onTouchStart_: function(e) { | |
| 116 if (e.touches.length != 1) | |
| 117 return false; | |
| 118 | |
| 119 e.preventDefault(); | |
| 120 var touch = e.touches[0]; | |
| 121 this.lastTouchLocation_ = {x: touch.pageX, y: touch.pageY}; | |
| 122 var target = assertInstanceof(e.target, HTMLElement); | |
| 123 return this.startDrag_(target, this.lastTouchLocation_); | |
| 124 }, | |
| 125 | |
| 126 /** | |
| 127 * @param {Event} e The touch move event. | |
| 128 * @return {boolean} | |
| 129 * @private | |
| 130 */ | |
| 131 onTouchMove_: function(e) { | |
| 132 if (e.touches.length != 1) | |
| 133 return true; | |
| 134 | |
| 135 var touchLocation = {x: e.touches[0].pageX, y: e.touches[0].pageY}; | |
| 136 // Touch move events can happen even if the touch location doesn't change | |
| 137 // and on small unintentional finger movements. Ignore these small changes. | |
| 138 if (this.lastTouchLocation_) { | |
| 139 /** @const */ var IGNORABLE_TOUCH_MOVE_PX = 1; | |
|
michaelpg
2016/06/24 21:00:10
nice
stevenjb
2016/06/24 21:44:12
Acknowledged.
| |
| 140 var xDiff = Math.abs(touchLocation.x - this.lastTouchLocation_.x); | |
| 141 var yDiff = Math.abs(touchLocation.y - this.lastTouchLocation_.y); | |
| 142 if (xDiff <= IGNORABLE_TOUCH_MOVE_PX && yDiff <= IGNORABLE_TOUCH_MOVE_PX) | |
| 143 return true; | |
| 144 } | |
| 145 this.lastTouchLocation_ = touchLocation; | |
| 146 e.preventDefault(); | |
| 147 return this.processDrag_(e, touchLocation); | |
| 148 }, | |
| 149 | |
| 150 /** | |
| 151 * @param {!HTMLElement} target | |
| 152 * @param {!DragPosition} eventLocation | |
| 153 * @return {boolean} | |
| 154 * @private | |
| 155 */ | |
| 156 startDrag_: function(target, eventLocation) { | |
| 157 console.log('Start Drag: ' + target.id); | |
|
michaelpg
2016/06/24 21:00:11
ahem
stevenjb
2016/06/24 21:44:12
Removed.
| |
| 158 this.dragId_ = target.id; | |
| 159 this.dragStartLocation = eventLocation; | |
| 160 return false; | |
| 161 }, | |
| 162 | |
| 163 /** | |
| 164 * @param {Event} e | |
| 165 * @return {boolean} | |
| 166 * @private | |
| 167 */ | |
| 168 endDrag_: function(e) { | |
| 169 if (this.dragId_ && this.callback_) | |
| 170 this.callback_(this.dragId_, null); | |
| 171 this.dragId_ = ''; | |
| 172 this.lastTouchLocation_ = null; | |
| 173 return false; | |
| 174 }, | |
| 175 | |
| 176 /** | |
| 177 * @param {Event} e The event which triggers this drag. | |
| 178 * @param {DragPosition} eventLocation The location of the event. | |
| 179 * @return {boolean} | |
| 180 * @private | |
| 181 */ | |
| 182 processDrag_: function(e, eventLocation) { | |
| 183 if (!this.dragId_) | |
| 184 return true; | |
| 185 if (this.callback_) { | |
| 186 var delta = { | |
| 187 x: eventLocation.x - this.dragStartLocation.x, | |
| 188 y: eventLocation.y - this.dragStartLocation.y, | |
| 189 }; | |
| 190 this.callback_(this.dragId_, delta); | |
| 191 } | |
| 192 return false; | |
| 193 }, | |
| 194 }; | |
| OLD | NEW |