Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 The Native Client SDK Authors. | |
| 2 // Use of this source code is governed by a BSD-style license that can | |
| 3 // be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview This class implements a mouse-drag event. It registers for | |
| 7 * mousedown events, and when it sees one, starts capturing mousemove events | |
| 8 * until it gets a mousup event. It manufactures three drag events: the | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
three +kinds of+ drag events?
I haven't read all t
David Springer
2011/02/03 18:12:27
Done.
| |
| 9 * DRAG_START, DRAG and DRAG_END. | |
| 10 */ | |
| 11 | |
| 12 // The uikit namespace. | |
| 13 var uikit = uikit || {}; | |
| 14 | |
| 15 /** | |
| 16 * Constructor for the Dragger. Register for mousedown events that happen on | |
| 17 * |opt_target|. If |opt_target| is null or undefined, then this object observe s | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
nit: >80 characters
David Springer
2011/02/03 18:12:27
Done.
| |
| 18 * mousedown on the whole document. | |
| 19 * @param {?Element} opt_target The event target. Defaults to the whole | |
| 20 * document. | |
| 21 * @constructor | |
| 22 */ | |
| 23 uikit.Dragger = function(target) { | |
| 24 /** | |
| 25 * The event target. | |
| 26 * @type {Element} | |
| 27 * @private | |
| 28 */ | |
| 29 this.target_ = target || document; | |
| 30 | |
| 31 /** | |
| 32 * The array of objects that get notified of drag events. Each object in | |
| 33 * this array get sent a handleStartDrag(), handleDrag() and handleEndDrag() | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
get->gets (or is)
David Springer
2011/02/03 18:12:27
Done.
| |
| 34 * message. | |
| 35 * @type {Array.<Object>} | |
| 36 * @private | |
| 37 */ | |
| 38 this.listeners_ = []; | |
| 39 | |
| 40 /** | |
| 41 * Flag to indicate whether the object is in a drag sequence or not. | |
| 42 * @type {boolean} | |
| 43 * @private | |
| 44 */ | |
| 45 this.isDragging_ = false; | |
| 46 | |
| 47 /** | |
| 48 * The function objects that get attached as event handlers. These are | |
| 49 * cached so that they can be removed on mouse up. | |
| 50 * @type {function} | |
| 51 * @private | |
| 52 */ | |
| 53 this.boundMouseMove_ = null; | |
| 54 this.boundMouseUp_ = null; | |
| 55 | |
| 56 this.target_.addEventListener('mousedown', | |
| 57 this.onMouseDown.bind(this), | |
| 58 false); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * The ids used for drag event types. | |
| 63 * @enum {string} | |
| 64 */ | |
| 65 uikit.Dragger.DragEvents = { | |
| 66 DRAG_START: 'dragstart', // Start a drag sequence | |
| 67 DRAG: 'drag', // Mouse moved during a drag sequence. | |
| 68 DRAG_END: 'dragend' // End a drag sewquence. | |
| 69 }; | |
| 70 | |
| 71 /** | |
| 72 * Add a drag listener. Each listener should respond to thhree methods: | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
s/thhree/three
David Springer
2011/02/03 18:12:27
Done.
| |
| 73 * handleStartDrag(), handleDrag() and handleEndDrag(). This method assumes | |
| 74 * that |listener| does not already exist in the array of listeners. | |
| 75 * @param {!Object} listener The object that will listen to drag events. | |
| 76 */ | |
| 77 uikit.Dragger.prototype.addDragListener = function(listener) { | |
| 78 this.listeners_.push(listener); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Handle a mousedown event: register for mousemove and mouseup, then tell | |
| 83 * the target that is has a DRAG_START event. | |
| 84 * @param {Event} event The mousedown event that triggered this method. | |
| 85 */ | |
| 86 uikit.Dragger.prototype.onMouseDown = function(event) { | |
| 87 this.boundMouseMove_ = this.onMouseMove.bind(this); | |
| 88 this.boundMouseUp_ = this.onMouseUp.bind(this); | |
| 89 this.target_.addEventListener('mousemove', this.boundMouseMove_); | |
| 90 this.target_.addEventListener('mouseup', this.boundMouseUp_); | |
| 91 this.isDragging_ = true; | |
| 92 var dragStartEvent = { type: uikit.Dragger.DragEvents.DRAG_START, | |
| 93 clientX: event.offsetX, | |
| 94 clientY: event.offsetY }; | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
It might be good to document what this event var c
David Springer
2011/02/03 18:12:27
Good point. It is a DOM Event object, which is do
| |
| 95 var i; | |
| 96 for (i = 0; i < this.listeners_.length; ++i) { | |
| 97 this.listeners_[i].handleStartDrag(this.target_, dragStartEvent); | |
|
dmichael(do not use this one)
2011/02/02 23:03:59
You could abstract this loop out if you wanted; i
David Springer
2011/02/03 18:12:27
for...in in JS can cause problems because of the d
dmichael(do not use this one)
2011/02/03 20:08:44
Nevermind; was neglecting that the function call i
| |
| 98 } | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Handle a mousemove event: tell the target that is has a DRAG event. | |
| 103 * @param {Event} event The mousemove event that triggered this method. | |
| 104 */ | |
| 105 uikit.Dragger.prototype.onMouseMove = function(event) { | |
| 106 if (!this.isDragging_) | |
| 107 return; | |
| 108 var dragEvent = { type: uikit.Dragger.DragEvents.DRAG, | |
| 109 clientX: event.offsetX, | |
| 110 clientY: event.offsetY}; | |
| 111 var i; | |
| 112 for (i = 0; i < this.listeners_.length; ++i) { | |
| 113 this.listeners_[i].handleDrag(this.target_, dragEvent); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Handle a mouseup event: un-register for mousemove and mouseup, then tell | |
| 119 * the target that is has a DRAG_END event. | |
| 120 * @param {Event} event The mouseup event that triggered this method. | |
| 121 */ | |
| 122 uikit.Dragger.prototype.onMouseUp = function(event) { | |
| 123 this.target_.removeEventListener('mouseup', this.boundMouseUp_, false); | |
| 124 this.target_.removeEventListener('mousemove', this.boundMouseMove_, false); | |
| 125 this.boundMouseUp_ = null; | |
| 126 this.boundMouseMove_ = null; | |
| 127 this.isDragging_ = false; | |
| 128 var dragEndEvent = { type: uikit.Dragger.DragEvents.DRAG_END, | |
| 129 clientX: event.offsetX, | |
| 130 clientY: event.offsetY}; | |
| 131 var i; | |
| 132 for (i = 0; i < this.listeners_.length; ++i) { | |
| 133 this.listeners_[i].handleEndDrag(this.target_, dragEndEvent); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Bind a scope to a function. Used to bind an object to |this| for event | |
| 139 * handlers. | |
| 140 * @param {!Object} scope The scope in which the function executes. |scope| | |
| 141 * becomes |this| during function execution. | |
| 142 * @return {function} the bound version of the original function. | |
| 143 */ | |
| 144 Function.prototype.bind = function(scope) { | |
| 145 var boundContext = this; | |
| 146 return function() { | |
| 147 return boundContext.apply(scope, arguments); | |
| 148 } | |
| 149 } | |
| OLD | NEW |