Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / ui / ResizerWidget.js
blob2903bf3439a42f068e35b07bd7c55f7e8c2a6392
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
3 // found in the LICENSE file.
5 /**
6 * @constructor
7 * @extends {WebInspector.Object}
8 */
9 WebInspector.ResizerWidget = function()
11 WebInspector.Object.call(this);
13 this._isEnabled = true;
14 this._elements = [];
15 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this);
16 this._cursor = "nwse-resize";
19 WebInspector.ResizerWidget.Events = {
20 ResizeStart: "ResizeStart",
21 ResizeUpdate: "ResizeUpdate",
22 ResizeEnd: "ResizeEnd"
25 WebInspector.ResizerWidget.prototype = {
26 /**
27 * @return {boolean}
29 isEnabled: function()
31 return this._isEnabled;
34 /**
35 * @param {boolean} enabled
37 setEnabled: function(enabled)
39 this._isEnabled = enabled;
40 this.updateElementCursors();
43 /**
44 * @return {!Array.<!Element>}
46 elements: function()
48 return this._elements.slice();
51 /**
52 * @param {!Element} element
54 addElement: function(element)
56 if (this._elements.indexOf(element) !== -1)
57 return;
59 this._elements.push(element);
60 element.addEventListener("mousedown", this._installDragOnMouseDownBound, false);
61 this._updateElementCursor(element);
64 /**
65 * @param {!Element} element
67 removeElement: function(element)
69 if (this._elements.indexOf(element) === -1)
70 return;
72 this._elements.remove(element);
73 element.removeEventListener("mousedown", this._installDragOnMouseDownBound, false);
74 element.style.removeProperty("cursor");
77 updateElementCursors: function()
79 this._elements.forEach(this._updateElementCursor.bind(this));
82 /**
83 * @param {!Element} element
85 _updateElementCursor: function(element)
87 if (this._isEnabled)
88 element.style.setProperty("cursor", this.cursor());
89 else
90 element.style.removeProperty("cursor");
93 /**
94 * @return {string}
96 cursor: function()
98 return this._cursor;
102 * @param {string} cursor
104 setCursor: function(cursor)
106 this._cursor = cursor;
107 this.updateElementCursors();
111 * @param {!Event} event
113 _installDragOnMouseDown: function(event)
115 // Only handle drags of the nodes specified.
116 if (this._elements.indexOf(event.target) === -1)
117 return false;
118 WebInspector.elementDragStart(this._dragStart.bind(this), this._drag.bind(this), this._dragEnd.bind(this), this.cursor(), event);
122 * @param {!MouseEvent} event
123 * @return {boolean}
125 _dragStart: function(event)
127 if (!this._isEnabled)
128 return false;
129 this._startX = event.pageX;
130 this._startY = event.pageY;
131 this.sendDragStart(this._startX, this._startY);
132 return true;
136 * @param {number} x
137 * @param {number} y
139 sendDragStart: function(x, y)
141 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeStart, { startX: x, currentX: x, startY: y, currentY: y });
145 * @param {!MouseEvent} event
146 * @return {boolean}
148 _drag: function(event)
150 if (!this._isEnabled) {
151 this._dragEnd(event);
152 return true; // Cancel drag.
155 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, event.shiftKey);
156 event.preventDefault();
157 return false; // Continue drag.
161 * @param {number} startX
162 * @param {number} currentX
163 * @param {number} startY
164 * @param {number} currentY
165 * @param {boolean} shiftKey
167 sendDragMove: function(startX, currentX, startY, currentY, shiftKey)
169 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeUpdate, { startX: startX, currentX: currentX, startY: startY, currentY: currentY, shiftKey: shiftKey });
173 * @param {!MouseEvent} event
175 _dragEnd: function(event)
177 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeEnd);
178 delete this._startX;
179 delete this._startY;
182 __proto__: WebInspector.Object.prototype
186 * @constructor
187 * @extends {WebInspector.ResizerWidget}
189 WebInspector.SimpleResizerWidget = function()
191 WebInspector.ResizerWidget.call(this);
192 this._isVertical = true;
195 WebInspector.SimpleResizerWidget.prototype = {
197 * @return {boolean}
199 isVertical: function()
201 return this._isVertical;
205 * Vertical widget resizes height (along y-axis).
206 * @param {boolean} vertical
208 setVertical: function(vertical)
210 this._isVertical = vertical;
211 this.updateElementCursors();
215 * @override
216 * @return {string}
218 cursor: function()
220 return this._isVertical ? "ns-resize" : "ew-resize";
224 * @override
225 * @param {number} x
226 * @param {number} y
228 sendDragStart: function(x, y)
230 var position = this._isVertical ? y : x;
231 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeStart, { startPosition: position, currentPosition: position });
235 * @override
236 * @param {number} startX
237 * @param {number} currentX
238 * @param {number} startY
239 * @param {number} currentY
240 * @param {boolean} shiftKey
242 sendDragMove: function(startX, currentX, startY, currentY, shiftKey)
244 if (this._isVertical)
245 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeUpdate, { startPosition: startY, currentPosition: currentY, shiftKey: shiftKey });
246 else
247 this.dispatchEventToListeners(WebInspector.ResizerWidget.Events.ResizeUpdate, { startPosition: startX, currentPosition: currentX, shiftKey: shiftKey });
250 __proto__: WebInspector.ResizerWidget.prototype