blob: bf345c77488c0857605dc9d71951dc527823cd72 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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.
4/**
5 * @unrestricted
6 */
7UI.ResizerWidget = class extends Common.Object {
8 constructor() {
9 super();
10
11 this._isEnabled = true;
12 this._elements = [];
13 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this);
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) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3443 if (this._elements.indexOf(element) !== -1) {
Blink Reformat4c46d092018-04-07 15:32:3744 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3445 }
Blink Reformat4c46d092018-04-07 15:32:3746
47 this._elements.push(element);
48 element.addEventListener('mousedown', this._installDragOnMouseDownBound, false);
49 this._updateElementCursor(element);
50 }
51
52 /**
53 * @param {!Element} element
54 */
55 removeElement(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3456 if (this._elements.indexOf(element) === -1) {
Blink Reformat4c46d092018-04-07 15:32:3757 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3458 }
Blink Reformat4c46d092018-04-07 15:32:3759
60 this._elements.remove(element);
61 element.removeEventListener('mousedown', this._installDragOnMouseDownBound, false);
62 element.style.removeProperty('cursor');
63 }
64
65 updateElementCursors() {
66 this._elements.forEach(this._updateElementCursor.bind(this));
67 }
68
69 /**
70 * @param {!Element} element
71 */
72 _updateElementCursor(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3473 if (this._isEnabled) {
Blink Reformat4c46d092018-04-07 15:32:3774 element.style.setProperty('cursor', this.cursor());
Tim van der Lippe1d6e57a2019-09-30 11:55:3475 } else {
Blink Reformat4c46d092018-04-07 15:32:3776 element.style.removeProperty('cursor');
Tim van der Lippe1d6e57a2019-09-30 11:55:3477 }
Blink Reformat4c46d092018-04-07 15:32:3778 }
79
80 /**
81 * @return {string}
82 */
83 cursor() {
84 return this._cursor;
85 }
86
87 /**
88 * @param {string} cursor
89 */
90 setCursor(cursor) {
91 this._cursor = cursor;
92 this.updateElementCursors();
93 }
94
95 /**
96 * @param {!Event} event
97 */
98 _installDragOnMouseDown(event) {
99 // Only handle drags of the nodes specified.
Tim van der Lippe1d6e57a2019-09-30 11:55:34100 if (this._elements.indexOf(event.target) === -1) {
Blink Reformat4c46d092018-04-07 15:32:37101 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34102 }
Blink Reformat4c46d092018-04-07 15:32:37103 UI.elementDragStart(
104 /** @type {!Element} */ (event.target), this._dragStart.bind(this), this._drag.bind(this),
105 this._dragEnd.bind(this), this.cursor(), event);
106 }
107
108 /**
109 * @param {!MouseEvent} event
110 * @return {boolean}
111 */
112 _dragStart(event) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34113 if (!this._isEnabled) {
Blink Reformat4c46d092018-04-07 15:32:37114 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34115 }
Blink Reformat4c46d092018-04-07 15:32:37116 this._startX = event.pageX;
117 this._startY = event.pageY;
118 this.sendDragStart(this._startX, this._startY);
119 return true;
120 }
121
122 /**
123 * @param {number} x
124 * @param {number} y
125 */
126 sendDragStart(x, y) {
127 this.dispatchEventToListeners(
128 UI.ResizerWidget.Events.ResizeStart, {startX: x, currentX: x, startY: y, currentY: y});
129 }
130
131 /**
132 * @param {!MouseEvent} event
133 * @return {boolean}
134 */
135 _drag(event) {
136 if (!this._isEnabled) {
137 this._dragEnd(event);
138 return true; // Cancel drag.
139 }
140
141 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, event.shiftKey);
142 event.preventDefault();
143 return false; // Continue drag.
144 }
145
146 /**
147 * @param {number} startX
148 * @param {number} currentX
149 * @param {number} startY
150 * @param {number} currentY
151 * @param {boolean} shiftKey
152 */
153 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
154 this.dispatchEventToListeners(
155 UI.ResizerWidget.Events.ResizeUpdate,
156 {startX: startX, currentX: currentX, startY: startY, currentY: currentY, shiftKey: shiftKey});
157 }
158
159 /**
160 * @param {!MouseEvent} event
161 */
162 _dragEnd(event) {
163 this.dispatchEventToListeners(UI.ResizerWidget.Events.ResizeEnd);
164 delete this._startX;
165 delete this._startY;
166 }
167};
168
169/** @enum {symbol} */
170UI.ResizerWidget.Events = {
171 ResizeStart: Symbol('ResizeStart'),
172 ResizeUpdate: Symbol('ResizeUpdate'),
173 ResizeEnd: Symbol('ResizeEnd')
174};
175
176/**
177 * @unrestricted
178 */
179UI.SimpleResizerWidget = class extends UI.ResizerWidget {
180 constructor() {
181 super();
182 this._isVertical = true;
183 }
184
185 /**
186 * @return {boolean}
187 */
188 isVertical() {
189 return this._isVertical;
190 }
191
192 /**
193 * Vertical widget resizes height (along y-axis).
194 * @param {boolean} vertical
195 */
196 setVertical(vertical) {
197 this._isVertical = vertical;
198 this.updateElementCursors();
199 }
200
201 /**
202 * @override
203 * @return {string}
204 */
205 cursor() {
206 return this._isVertical ? 'ns-resize' : 'ew-resize';
207 }
208
209 /**
210 * @override
211 * @param {number} x
212 * @param {number} y
213 */
214 sendDragStart(x, y) {
215 const position = this._isVertical ? y : x;
216 this.dispatchEventToListeners(
217 UI.ResizerWidget.Events.ResizeStart, {startPosition: position, currentPosition: position});
218 }
219
220 /**
221 * @override
222 * @param {number} startX
223 * @param {number} currentX
224 * @param {number} startY
225 * @param {number} currentY
226 * @param {boolean} shiftKey
227 */
228 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
229 if (this._isVertical) {
230 this.dispatchEventToListeners(
231 UI.ResizerWidget.Events.ResizeUpdate, {startPosition: startY, currentPosition: currentY, shiftKey: shiftKey});
232 } else {
233 this.dispatchEventToListeners(
234 UI.ResizerWidget.Events.ResizeUpdate, {startPosition: startX, currentPosition: currentX, shiftKey: shiftKey});
235 }
236 }
237};