blob: 979a4b91ac0381df594fdf471abf87182cf592fa [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
Tim van der Lippeee97fa32020-04-23 15:20:565// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Paul Lewis17e384e2020-01-08 15:46:518import * as Common from '../common/common.js';
Paul Lewis9950e182019-12-16 16:06:079import {elementDragStart} from './UIUtils.js';
10
Blink Reformat4c46d092018-04-07 15:32:3711/**
12 * @unrestricted
13 */
Paul Lewis17e384e2020-01-08 15:46:5114export class ResizerWidget extends Common.ObjectWrapper.ObjectWrapper {
Blink Reformat4c46d092018-04-07 15:32:3715 constructor() {
16 super();
17
18 this._isEnabled = true;
Simon Zünd5bbcd5c2020-03-02 09:09:2119 /** @type {!Set<!Element>} */
20 this._elements = new Set();
Blink Reformat4c46d092018-04-07 15:32:3721 this._installDragOnMouseDownBound = this._installDragOnMouseDown.bind(this);
22 this._cursor = 'nwse-resize';
23 }
24
25 /**
26 * @return {boolean}
27 */
28 isEnabled() {
29 return this._isEnabled;
30 }
31
32 /**
33 * @param {boolean} enabled
34 */
35 setEnabled(enabled) {
36 this._isEnabled = enabled;
37 this.updateElementCursors();
38 }
39
40 /**
41 * @return {!Array.<!Element>}
42 */
43 elements() {
Simon Zünd5bbcd5c2020-03-02 09:09:2144 return [...this._elements];
Blink Reformat4c46d092018-04-07 15:32:3745 }
46
47 /**
48 * @param {!Element} element
49 */
50 addElement(element) {
Simon Zünd5bbcd5c2020-03-02 09:09:2151 if (!this._elements.has(element)) {
52 this._elements.add(element);
53 element.addEventListener('mousedown', this._installDragOnMouseDownBound, false);
54 this._updateElementCursor(element);
Tim van der Lippe1d6e57a2019-09-30 11:55:3455 }
Blink Reformat4c46d092018-04-07 15:32:3756 }
57
58 /**
59 * @param {!Element} element
60 */
61 removeElement(element) {
Simon Zünd5bbcd5c2020-03-02 09:09:2162 if (this._elements.has(element)) {
63 this._elements.delete(element);
64 element.removeEventListener('mousedown', this._installDragOnMouseDownBound, false);
65 element.style.removeProperty('cursor');
Tim van der Lippe1d6e57a2019-09-30 11:55:3466 }
Blink Reformat4c46d092018-04-07 15:32:3767 }
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) {
Simon Zünd5bbcd5c2020-03-02 09:09:21103 const element = /** @type {!Element} */ (event.target);
Blink Reformat4c46d092018-04-07 15:32:37104 // Only handle drags of the nodes specified.
Simon Zünd5bbcd5c2020-03-02 09:09:21105 if (!this._elements.has(element)) {
Blink Reformat4c46d092018-04-07 15:32:37106 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34107 }
Tim van der Lippe403a88d2020-05-13 11:51:32108 elementDragStart(element, this._dragStart.bind(this), event => {
109 this._drag(event);
110 }, this._dragEnd.bind(this), this.cursor(), event);
Blink Reformat4c46d092018-04-07 15:32:37111 }
112
113 /**
114 * @param {!MouseEvent} event
115 * @return {boolean}
116 */
117 _dragStart(event) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34118 if (!this._isEnabled) {
Blink Reformat4c46d092018-04-07 15:32:37119 return false;
Tim van der Lippe1d6e57a2019-09-30 11:55:34120 }
Blink Reformat4c46d092018-04-07 15:32:37121 this._startX = event.pageX;
122 this._startY = event.pageY;
123 this.sendDragStart(this._startX, this._startY);
124 return true;
125 }
126
127 /**
128 * @param {number} x
129 * @param {number} y
130 */
131 sendDragStart(x, y) {
Tim van der Lippe0830b3d2019-10-03 13:20:07132 this.dispatchEventToListeners(Events.ResizeStart, {startX: x, currentX: x, startY: y, currentY: y});
Blink Reformat4c46d092018-04-07 15:32:37133 }
134
135 /**
136 * @param {!MouseEvent} event
137 * @return {boolean}
138 */
139 _drag(event) {
140 if (!this._isEnabled) {
141 this._dragEnd(event);
142 return true; // Cancel drag.
143 }
144
145 this.sendDragMove(this._startX, event.pageX, this._startY, event.pageY, event.shiftKey);
146 event.preventDefault();
147 return false; // Continue drag.
148 }
149
150 /**
151 * @param {number} startX
152 * @param {number} currentX
153 * @param {number} startY
154 * @param {number} currentY
155 * @param {boolean} shiftKey
156 */
157 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
158 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07159 Events.ResizeUpdate,
Blink Reformat4c46d092018-04-07 15:32:37160 {startX: startX, currentX: currentX, startY: startY, currentY: currentY, shiftKey: shiftKey});
161 }
162
163 /**
164 * @param {!MouseEvent} event
165 */
166 _dragEnd(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07167 this.dispatchEventToListeners(Events.ResizeEnd);
Blink Reformat4c46d092018-04-07 15:32:37168 delete this._startX;
169 delete this._startY;
170 }
Tim van der Lippe0830b3d2019-10-03 13:20:07171}
Blink Reformat4c46d092018-04-07 15:32:37172
173/** @enum {symbol} */
Tim van der Lippe0830b3d2019-10-03 13:20:07174export const Events = {
Blink Reformat4c46d092018-04-07 15:32:37175 ResizeStart: Symbol('ResizeStart'),
176 ResizeUpdate: Symbol('ResizeUpdate'),
177 ResizeEnd: Symbol('ResizeEnd')
178};
179
Sigurd Schneider46da7db2020-05-20 13:45:11180
Tim van der Lippe0830b3d2019-10-03 13:20:07181export class SimpleResizerWidget extends ResizerWidget {
Blink Reformat4c46d092018-04-07 15:32:37182 constructor() {
183 super();
184 this._isVertical = true;
185 }
186
187 /**
188 * @return {boolean}
189 */
190 isVertical() {
191 return this._isVertical;
192 }
193
194 /**
195 * Vertical widget resizes height (along y-axis).
196 * @param {boolean} vertical
197 */
198 setVertical(vertical) {
199 this._isVertical = vertical;
200 this.updateElementCursors();
201 }
202
203 /**
204 * @override
205 * @return {string}
206 */
207 cursor() {
208 return this._isVertical ? 'ns-resize' : 'ew-resize';
209 }
210
211 /**
212 * @override
213 * @param {number} x
214 * @param {number} y
215 */
216 sendDragStart(x, y) {
217 const position = this._isVertical ? y : x;
Tim van der Lippe0830b3d2019-10-03 13:20:07218 this.dispatchEventToListeners(Events.ResizeStart, {startPosition: position, currentPosition: position});
Blink Reformat4c46d092018-04-07 15:32:37219 }
220
221 /**
222 * @override
223 * @param {number} startX
224 * @param {number} currentX
225 * @param {number} startY
226 * @param {number} currentY
227 * @param {boolean} shiftKey
228 */
229 sendDragMove(startX, currentX, startY, currentY, shiftKey) {
230 if (this._isVertical) {
231 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07232 Events.ResizeUpdate, {startPosition: startY, currentPosition: currentY, shiftKey: shiftKey});
Blink Reformat4c46d092018-04-07 15:32:37233 } else {
234 this.dispatchEventToListeners(
Tim van der Lippe0830b3d2019-10-03 13:20:07235 Events.ResizeUpdate, {startPosition: startX, currentPosition: currentX, shiftKey: shiftKey});
Blink Reformat4c46d092018-04-07 15:32:37236 }
237 }
Tim van der Lippe0830b3d2019-10-03 13:20:07238}