blob: 92636e97745921bf88a521d4e287e39a925321fe [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.
Paul Lewis9950e182019-12-16 16:06:074
Paul Lewis17e384e2020-01-08 15:46:515import * as Common from '../common/common.js';
Paul Lewis9950e182019-12-16 16:06:076import {elementDragStart} from './UIUtils.js';
7
Blink Reformat4c46d092018-04-07 15:32:378/**
9 * @unrestricted
10 */
Paul Lewis17e384e2020-01-08 15:46:5111export class ResizerWidget extends Common.ObjectWrapper.ObjectWrapper {
Blink Reformat4c46d092018-04-07 15:32:3712 constructor() {
13 super();
14
15 this._isEnabled = true;
16 this._elements = [];
17 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this);
18 this._cursor = 'nwse-resize';
19 }
20
21 /**
22 * @return {boolean}
23 */
24 isEnabled() {
25 return this._isEnabled;
26 }
27
28 /**
29 * @param {boolean} enabled
30 */
31 setEnabled(enabled) {
32 this._isEnabled = enabled;
33 this.updateElementCursors();
34 }
35
36 /**
37 * @return {!Array.<!Element>}
38 */
39 elements() {
40 return this._elements.slice();
41 }
42
43 /**
44 * @param {!Element} element
45 */
46 addElement(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3447 if (this._elements.indexOf(element) !== -1) {
Blink Reformat4c46d092018-04-07 15:32:3748 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3449 }
Blink Reformat4c46d092018-04-07 15:32:3750
51 this._elements.push(element);
52 element.addEventListener('mousedown', this._installDragOnMouseDownBound, false);
53 this._updateElementCursor(element);
54 }
55
56 /**
57 * @param {!Element} element
58 */
59 removeElement(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3460 if (this._elements.indexOf(element) === -1) {
Blink Reformat4c46d092018-04-07 15:32:3761 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3462 }
Blink Reformat4c46d092018-04-07 15:32:3763
64 this._elements.remove(element);
65 element.removeEventListener('mousedown', this._installDragOnMouseDownBound, false);
66 element.style.removeProperty('cursor');
67 }
68
69 updateElementCursors() {
70 this._elements.forEach(this._updateElementCursor.bind(this));
71 }
72
73 /**
74 * @param {!Element} element
75 */
76 _updateElementCursor(element) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3477 if (this._isEnabled) {
Blink Reformat4c46d092018-04-07 15:32:3778 element.style.setProperty('cursor', this.cursor());
Tim van der Lippe1d6e57a2019-09-30 11:55:3479 } else {
Blink Reformat4c46d092018-04-07 15:32:3780 element.style.removeProperty('cursor');
Tim van der Lippe1d6e57a2019-09-30 11:55:3481 }
Blink Reformat4c46d092018-04-07 15:32:3782 }
83
84 /**
85 * @return {string}
86 */
87 cursor() {
88 return this._cursor;
89 }
90
91 /**
92 * @param {string} cursor
93 */
94 setCursor(cursor) {
95 this._cursor = cursor;
96 this.updateElementCursors();
97 }
98
99 /**
100 * @param {!Event} event
101 */
102 _installDragOnMouseDown(event) {
103 // Only handle drags of the nodes specified.
Tim van der Lippe1d6e57a2019-09-30 11:55:34104 if (this._elements.indexOf(event.target) === -1) {
Blink Reformat4c46d092018-04-07 15:32:37105 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34106 }
Paul Lewis9950e182019-12-16 16:06:07107 elementDragStart(
Blink Reformat4c46d092018-04-07 15:32:37108 /** @type {!Element} */ (event.target), this._dragStart.bind(this), this._drag.bind(this),
109 this._dragEnd.bind(this), this.cursor(), event);
110 }
111
112 /**
113 * @param {!MouseEvent} event
114 * @return {boolean}
115 */
116 _dragStart(event) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34117 if (!this._isEnabled) {
Blink Reformat4c46d092018-04-07 15:32:37118 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34119 }
Blink Reformat4c46d092018-04-07 15:32:37120 this._startX = event.pageX;
121 this._startY = event.pageY;
122 this.sendDragStart(this._startX, this._startY);
123 return true;
124 }
125
126 /**
127 * @param {number} x
128 * @param {number} y
129 */
130 sendDragStart(x, y) {
Tim van der Lippe0830b3d2019-10-03 13:20:07131 this.dispatchEventToListeners(Events.ResizeStart, {startX: x, currentX: x, startY: y, currentY: y});
Blink Reformat4c46d092018-04-07 15:32:37132 }
133
134 /**
135 * @param {!MouseEvent} event
136 * @return {boolean}
137 */
138 _drag(event) {
139 if (!this._isEnabled) {
140 this._dragEnd(event);
141 return true; // Cancel drag.
142 }
143
144 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, event.shiftKey);
145 event.preventDefault();
146 return false; // Continue drag.
147 }
148
149 /**
150 * @param {number} startX
151 * @param {number} currentX
152 * @param {number} startY
153 * @param {number} currentY
154 * @param {boolean} shiftKey
155 */
156 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
157 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07158 Events.ResizeUpdate,
Blink Reformat4c46d092018-04-07 15:32:37159 {startX: startX, currentX: currentX, startY: startY, currentY: currentY, shiftKey: shiftKey});
160 }
161
162 /**
163 * @param {!MouseEvent} event
164 */
165 _dragEnd(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07166 this.dispatchEventToListeners(Events.ResizeEnd);
Blink Reformat4c46d092018-04-07 15:32:37167 delete this._startX;
168 delete this._startY;
169 }
Tim van der Lippe0830b3d2019-10-03 13:20:07170}
Blink Reformat4c46d092018-04-07 15:32:37171
172/** @enum {symbol} */
Tim van der Lippe0830b3d2019-10-03 13:20:07173export const Events = {
Blink Reformat4c46d092018-04-07 15:32:37174 ResizeStart: Symbol('ResizeStart'),
175 ResizeUpdate: Symbol('ResizeUpdate'),
176 ResizeEnd: Symbol('ResizeEnd')
177};
178
179/**
180 * @unrestricted
181 */
Tim van der Lippe0830b3d2019-10-03 13:20:07182export class SimpleResizerWidget extends ResizerWidget {
Blink Reformat4c46d092018-04-07 15:32:37183 constructor() {
184 super();
185 this._isVertical = true;
186 }
187
188 /**
189 * @return {boolean}
190 */
191 isVertical() {
192 return this._isVertical;
193 }
194
195 /**
196 * Vertical widget resizes height (along y-axis).
197 * @param {boolean} vertical
198 */
199 setVertical(vertical) {
200 this._isVertical = vertical;
201 this.updateElementCursors();
202 }
203
204 /**
205 * @override
206 * @return {string}
207 */
208 cursor() {
209 return this._isVertical ? 'ns-resize' : 'ew-resize';
210 }
211
212 /**
213 * @override
214 * @param {number} x
215 * @param {number} y
216 */
217 sendDragStart(x, y) {
218 const position = this._isVertical ? y : x;
Tim van der Lippe0830b3d2019-10-03 13:20:07219 this.dispatchEventToListeners(Events.ResizeStart, {startPosition: position, currentPosition: position});
Blink Reformat4c46d092018-04-07 15:32:37220 }
221
222 /**
223 * @override
224 * @param {number} startX
225 * @param {number} currentX
226 * @param {number} startY
227 * @param {number} currentY
228 * @param {boolean} shiftKey
229 */
230 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
231 if (this._isVertical) {
232 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07233 Events.ResizeUpdate, {startPosition: startY, currentPosition: currentY, shiftKey: shiftKey});
Blink Reformat4c46d092018-04-07 15:32:37234 } else {
235 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07236 Events.ResizeUpdate, {startPosition: startX, currentPosition: currentX, shiftKey: shiftKey});
Blink Reformat4c46d092018-04-07 15:32:37237 }
238 }
Tim van der Lippe0830b3d2019-10-03 13:20:07239}