| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 * @extends {WebInspector.Object} | |
| 8 */ | 6 */ |
| 9 WebInspector.ResizerWidget = function() | 7 WebInspector.ResizerWidget = class extends WebInspector.Object { |
| 10 { | 8 constructor() { |
| 11 WebInspector.Object.call(this); | 9 super(); |
| 12 | 10 |
| 13 this._isEnabled = true; | 11 this._isEnabled = true; |
| 14 this._elements = []; | 12 this._elements = []; |
| 15 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this); | 13 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this); |
| 16 this._cursor = "nwse-resize"; | 14 this._cursor = 'nwse-resize'; |
| 15 } |
| 16 |
| 17 /** |
| 18 * @return {boolean} |
| 19 */ |
| 20 isEnabled() { |
| 21 return this._isEnabled; |
| 22 } |
| 23 |
| 24 /** |
| 25 * @param {boolean} enabled |
| 26 */ |
| 27 setEnabled(enabled) { |
| 28 this._isEnabled = enabled; |
| 29 this.updateElementCursors(); |
| 30 } |
| 31 |
| 32 /** |
| 33 * @return {!Array.<!Element>} |
| 34 */ |
| 35 elements() { |
| 36 return this._elements.slice(); |
| 37 } |
| 38 |
| 39 /** |
| 40 * @param {!Element} element |
| 41 */ |
| 42 addElement(element) { |
| 43 if (this._elements.indexOf(element) !== -1) |
| 44 return; |
| 45 |
| 46 this._elements.push(element); |
| 47 element.addEventListener('mousedown', this._installDragOnMouseDownBound, fal
se); |
| 48 this._updateElementCursor(element); |
| 49 } |
| 50 |
| 51 /** |
| 52 * @param {!Element} element |
| 53 */ |
| 54 removeElement(element) { |
| 55 if (this._elements.indexOf(element) === -1) |
| 56 return; |
| 57 |
| 58 this._elements.remove(element); |
| 59 element.removeEventListener('mousedown', this._installDragOnMouseDownBound,
false); |
| 60 element.style.removeProperty('cursor'); |
| 61 } |
| 62 |
| 63 updateElementCursors() { |
| 64 this._elements.forEach(this._updateElementCursor.bind(this)); |
| 65 } |
| 66 |
| 67 /** |
| 68 * @param {!Element} element |
| 69 */ |
| 70 _updateElementCursor(element) { |
| 71 if (this._isEnabled) |
| 72 element.style.setProperty('cursor', this.cursor()); |
| 73 else |
| 74 element.style.removeProperty('cursor'); |
| 75 } |
| 76 |
| 77 /** |
| 78 * @return {string} |
| 79 */ |
| 80 cursor() { |
| 81 return this._cursor; |
| 82 } |
| 83 |
| 84 /** |
| 85 * @param {string} cursor |
| 86 */ |
| 87 setCursor(cursor) { |
| 88 this._cursor = cursor; |
| 89 this.updateElementCursors(); |
| 90 } |
| 91 |
| 92 /** |
| 93 * @param {!Event} event |
| 94 */ |
| 95 _installDragOnMouseDown(event) { |
| 96 // Only handle drags of the nodes specified. |
| 97 if (this._elements.indexOf(event.target) === -1) |
| 98 return false; |
| 99 WebInspector.elementDragStart( |
| 100 /** @type {!Element} */ (event.target), this._dragStart.bind(this), this
._drag.bind(this), |
| 101 this._dragEnd.bind(this), this.cursor(), event); |
| 102 } |
| 103 |
| 104 /** |
| 105 * @param {!MouseEvent} event |
| 106 * @return {boolean} |
| 107 */ |
| 108 _dragStart(event) { |
| 109 if (!this._isEnabled) |
| 110 return false; |
| 111 this._startX = event.pageX; |
| 112 this._startY = event.pageY; |
| 113 this.sendDragStart(this._startX, this._startY); |
| 114 return true; |
| 115 } |
| 116 |
| 117 /** |
| 118 * @param {number} x |
| 119 * @param {number} y |
| 120 */ |
| 121 sendDragStart(x, y) { |
| 122 this.dispatchEventToListeners( |
| 123 WebInspector.ResizerWidget.Events.ResizeStart, {startX: x, currentX: x,
startY: y, currentY: y}); |
| 124 } |
| 125 |
| 126 /** |
| 127 * @param {!MouseEvent} event |
| 128 * @return {boolean} |
| 129 */ |
| 130 _drag(event) { |
| 131 if (!this._isEnabled) { |
| 132 this._dragEnd(event); |
| 133 return true; // Cancel drag. |
| 134 } |
| 135 |
| 136 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, even
t.shiftKey); |
| 137 event.preventDefault(); |
| 138 return false; // Continue drag. |
| 139 } |
| 140 |
| 141 /** |
| 142 * @param {number} startX |
| 143 * @param {number} currentX |
| 144 * @param {number} startY |
| 145 * @param {number} currentY |
| 146 * @param {boolean} shiftKey |
| 147 */ |
| 148 sendDragMove(startX, currentX, startY, currentY, shiftKey) { |
| 149 this.dispatchEventToListeners( |
| 150 WebInspector.ResizerWidget.Events.ResizeUpdate, |
| 151 {startX: startX, currentX: currentX, startY: startY, currentY: currentY,
shiftKey: shiftKey}); |
| 152 } |
| 153 |
| 154 /** |
| 155 * @param {!MouseEvent} event |
| 156 */ |
| 157 _dragEnd(event) { |
| 158 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeEnd); |
| 159 delete this._startX; |
| 160 delete this._startY; |
| 161 } |
| 17 }; | 162 }; |
| 18 | 163 |
| 19 /** @enum {symbol} */ | 164 /** @enum {symbol} */ |
| 20 WebInspector.ResizerWidget.Events = { | 165 WebInspector.ResizerWidget.Events = { |
| 21 ResizeStart: Symbol("ResizeStart"), | 166 ResizeStart: Symbol('ResizeStart'), |
| 22 ResizeUpdate: Symbol("ResizeUpdate"), | 167 ResizeUpdate: Symbol('ResizeUpdate'), |
| 23 ResizeEnd: Symbol("ResizeEnd") | 168 ResizeEnd: Symbol('ResizeEnd') |
| 24 }; | 169 }; |
| 25 | 170 |
| 26 WebInspector.ResizerWidget.prototype = { | 171 /** |
| 27 /** | 172 * @unrestricted |
| 28 * @return {boolean} | 173 */ |
| 29 */ | 174 WebInspector.SimpleResizerWidget = class extends WebInspector.ResizerWidget { |
| 30 isEnabled: function() | 175 constructor() { |
| 31 { | 176 super(); |
| 32 return this._isEnabled; | 177 this._isVertical = true; |
| 33 }, | 178 } |
| 34 | 179 |
| 35 /** | 180 /** |
| 36 * @param {boolean} enabled | 181 * @return {boolean} |
| 37 */ | 182 */ |
| 38 setEnabled: function(enabled) | 183 isVertical() { |
| 39 { | 184 return this._isVertical; |
| 40 this._isEnabled = enabled; | 185 } |
| 41 this.updateElementCursors(); | 186 |
| 42 }, | 187 /** |
| 43 | 188 * Vertical widget resizes height (along y-axis). |
| 44 /** | 189 * @param {boolean} vertical |
| 45 * @return {!Array.<!Element>} | 190 */ |
| 46 */ | 191 setVertical(vertical) { |
| 47 elements: function() | 192 this._isVertical = vertical; |
| 48 { | 193 this.updateElementCursors(); |
| 49 return this._elements.slice(); | 194 } |
| 50 }, | 195 |
| 51 | 196 /** |
| 52 /** | 197 * @override |
| 53 * @param {!Element} element | 198 * @return {string} |
| 54 */ | 199 */ |
| 55 addElement: function(element) | 200 cursor() { |
| 56 { | 201 return this._isVertical ? 'ns-resize' : 'ew-resize'; |
| 57 if (this._elements.indexOf(element) !== -1) | 202 } |
| 58 return; | 203 |
| 59 | 204 /** |
| 60 this._elements.push(element); | 205 * @override |
| 61 element.addEventListener("mousedown", this._installDragOnMouseDownBound,
false); | 206 * @param {number} x |
| 62 this._updateElementCursor(element); | 207 * @param {number} y |
| 63 }, | 208 */ |
| 64 | 209 sendDragStart(x, y) { |
| 65 /** | 210 var position = this._isVertical ? y : x; |
| 66 * @param {!Element} element | 211 this.dispatchEventToListeners( |
| 67 */ | 212 WebInspector.ResizerWidget.Events.ResizeStart, {startPosition: position,
currentPosition: position}); |
| 68 removeElement: function(element) | 213 } |
| 69 { | 214 |
| 70 if (this._elements.indexOf(element) === -1) | 215 /** |
| 71 return; | 216 * @override |
| 72 | 217 * @param {number} startX |
| 73 this._elements.remove(element); | 218 * @param {number} currentX |
| 74 element.removeEventListener("mousedown", this._installDragOnMouseDownBou
nd, false); | 219 * @param {number} startY |
| 75 element.style.removeProperty("cursor"); | 220 * @param {number} currentY |
| 76 }, | 221 * @param {boolean} shiftKey |
| 77 | 222 */ |
| 78 updateElementCursors: function() | 223 sendDragMove(startX, currentX, startY, currentY, shiftKey) { |
| 79 { | 224 if (this._isVertical) |
| 80 this._elements.forEach(this._updateElementCursor.bind(this)); | 225 this.dispatchEventToListeners( |
| 81 }, | 226 WebInspector.ResizerWidget.Events.ResizeUpdate, |
| 82 | 227 {startPosition: startY, currentPosition: currentY, shiftKey: shiftKey}
); |
| 83 /** | 228 else |
| 84 * @param {!Element} element | 229 this.dispatchEventToListeners( |
| 85 */ | 230 WebInspector.ResizerWidget.Events.ResizeUpdate, |
| 86 _updateElementCursor: function(element) | 231 {startPosition: startX, currentPosition: currentX, shiftKey: shiftKey}
); |
| 87 { | 232 } |
| 88 if (this._isEnabled) | |
| 89 element.style.setProperty("cursor", this.cursor()); | |
| 90 else | |
| 91 element.style.removeProperty("cursor"); | |
| 92 }, | |
| 93 | |
| 94 /** | |
| 95 * @return {string} | |
| 96 */ | |
| 97 cursor: function() | |
| 98 { | |
| 99 return this._cursor; | |
| 100 }, | |
| 101 | |
| 102 /** | |
| 103 * @param {string} cursor | |
| 104 */ | |
| 105 setCursor: function(cursor) | |
| 106 { | |
| 107 this._cursor = cursor; | |
| 108 this.updateElementCursors(); | |
| 109 }, | |
| 110 | |
| 111 /** | |
| 112 * @param {!Event} event | |
| 113 */ | |
| 114 _installDragOnMouseDown: function(event) | |
| 115 { | |
| 116 // Only handle drags of the nodes specified. | |
| 117 if (this._elements.indexOf(event.target) === -1) | |
| 118 return false; | |
| 119 WebInspector.elementDragStart(/** @type {!Element} */(event.target), thi
s._dragStart.bind(this), this._drag.bind(this), this._dragEnd.bind(this), this.c
ursor(), event); | |
| 120 }, | |
| 121 | |
| 122 /** | |
| 123 * @param {!MouseEvent} event | |
| 124 * @return {boolean} | |
| 125 */ | |
| 126 _dragStart: function(event) | |
| 127 { | |
| 128 if (!this._isEnabled) | |
| 129 return false; | |
| 130 this._startX = event.pageX; | |
| 131 this._startY = event.pageY; | |
| 132 this.sendDragStart(this._startX, this._startY); | |
| 133 return true; | |
| 134 }, | |
| 135 | |
| 136 /** | |
| 137 * @param {number} x | |
| 138 * @param {number} y | |
| 139 */ | |
| 140 sendDragStart: function(x, y) | |
| 141 { | |
| 142 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeSt
art, { startX: x, currentX: x, startY: y, currentY: y }); | |
| 143 }, | |
| 144 | |
| 145 /** | |
| 146 * @param {!MouseEvent} event | |
| 147 * @return {boolean} | |
| 148 */ | |
| 149 _drag: function(event) | |
| 150 { | |
| 151 if (!this._isEnabled) { | |
| 152 this._dragEnd(event); | |
| 153 return true; // Cancel drag. | |
| 154 } | |
| 155 | |
| 156 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY,
event.shiftKey); | |
| 157 event.preventDefault(); | |
| 158 return false; // Continue drag. | |
| 159 }, | |
| 160 | |
| 161 /** | |
| 162 * @param {number} startX | |
| 163 * @param {number} currentX | |
| 164 * @param {number} startY | |
| 165 * @param {number} currentY | |
| 166 * @param {boolean} shiftKey | |
| 167 */ | |
| 168 sendDragMove: function(startX, currentX, startY, currentY, shiftKey) | |
| 169 { | |
| 170 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeUp
date, { startX: startX, currentX: currentX, startY: startY, currentY: currentY,
shiftKey: shiftKey }); | |
| 171 }, | |
| 172 | |
| 173 /** | |
| 174 * @param {!MouseEvent} event | |
| 175 */ | |
| 176 _dragEnd: function(event) | |
| 177 { | |
| 178 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeEn
d); | |
| 179 delete this._startX; | |
| 180 delete this._startY; | |
| 181 }, | |
| 182 | |
| 183 __proto__: WebInspector.Object.prototype | |
| 184 }; | 233 }; |
| 185 | |
| 186 /** | |
| 187 * @constructor | |
| 188 * @extends {WebInspector.ResizerWidget} | |
| 189 */ | |
| 190 WebInspector.SimpleResizerWidget = function() | |
| 191 { | |
| 192 WebInspector.ResizerWidget.call(this); | |
| 193 this._isVertical = true; | |
| 194 }; | |
| 195 | |
| 196 WebInspector.SimpleResizerWidget.prototype = { | |
| 197 /** | |
| 198 * @return {boolean} | |
| 199 */ | |
| 200 isVertical: function() | |
| 201 { | |
| 202 return this._isVertical; | |
| 203 }, | |
| 204 | |
| 205 /** | |
| 206 * Vertical widget resizes height (along y-axis). | |
| 207 * @param {boolean} vertical | |
| 208 */ | |
| 209 setVertical: function(vertical) | |
| 210 { | |
| 211 this._isVertical = vertical; | |
| 212 this.updateElementCursors(); | |
| 213 }, | |
| 214 | |
| 215 /** | |
| 216 * @override | |
| 217 * @return {string} | |
| 218 */ | |
| 219 cursor: function() | |
| 220 { | |
| 221 return this._isVertical ? "ns-resize" : "ew-resize"; | |
| 222 }, | |
| 223 | |
| 224 /** | |
| 225 * @override | |
| 226 * @param {number} x | |
| 227 * @param {number} y | |
| 228 */ | |
| 229 sendDragStart: function(x, y) | |
| 230 { | |
| 231 var position = this._isVertical ? y : x; | |
| 232 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeSt
art, { startPosition: position, currentPosition: position }); | |
| 233 }, | |
| 234 | |
| 235 /** | |
| 236 * @override | |
| 237 * @param {number} startX | |
| 238 * @param {number} currentX | |
| 239 * @param {number} startY | |
| 240 * @param {number} currentY | |
| 241 * @param {boolean} shiftKey | |
| 242 */ | |
| 243 sendDragMove: function(startX, currentX, startY, currentY, shiftKey) | |
| 244 { | |
| 245 if (this._isVertical) | |
| 246 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.Resi
zeUpdate, { startPosition: startY, currentPosition: currentY, shiftKey: shiftKey
}); | |
| 247 else | |
| 248 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.Resi
zeUpdate, { startPosition: startX, currentPosition: currentX, shiftKey: shiftKey
}); | |
| 249 }, | |
| 250 | |
| 251 __proto__: WebInspector.ResizerWidget.prototype | |
| 252 }; | |
| OLD | NEW |