Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/layer_viewer/TransformController.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The Chromium Authors. All rights reserved. 2 * Copyright 2014 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6
7 /** 6 /**
8 * @constructor 7 * @unrestricted
9 * @extends {WebInspector.Object}
10 * @param {!Element} element
11 * @param {boolean=} disableRotate
12 */ 8 */
13 WebInspector.TransformController = function(element, disableRotate) 9 WebInspector.TransformController = class extends WebInspector.Object {
14 { 10 /**
11 * @param {!Element} element
12 * @param {boolean=} disableRotate
13 */
14 constructor(element, disableRotate) {
15 super();
15 this._shortcuts = {}; 16 this._shortcuts = {};
16 this.element = element; 17 this.element = element;
17 if (this.element.tabIndex < 0) 18 if (this.element.tabIndex < 0)
18 this.element.tabIndex = 0; 19 this.element.tabIndex = 0;
19 this._registerShortcuts(); 20 this._registerShortcuts();
20 WebInspector.installDragHandle(element, this._onDragStart.bind(this), this._ onDrag.bind(this), this._onDragEnd.bind(this), "move", null); 21 WebInspector.installDragHandle(
21 element.addEventListener("keydown", this._onKeyDown.bind(this), false); 22 element, this._onDragStart.bind(this), this._onDrag.bind(this), this._on DragEnd.bind(this), 'move', null);
22 element.addEventListener("keyup", this._onKeyUp.bind(this), false); 23 element.addEventListener('keydown', this._onKeyDown.bind(this), false);
23 element.addEventListener("mousewheel", this._onMouseWheel.bind(this), false) ; 24 element.addEventListener('keyup', this._onKeyUp.bind(this), false);
25 element.addEventListener('mousewheel', this._onMouseWheel.bind(this), false) ;
24 this._minScale = 0; 26 this._minScale = 0;
25 this._maxScale = Infinity; 27 this._maxScale = Infinity;
26 28
27 this._controlPanelToolbar = new WebInspector.Toolbar("transform-control-pane l"); 29 this._controlPanelToolbar = new WebInspector.Toolbar('transform-control-pane l');
28 30
29 /** @type {!Object<string, !WebInspector.ToolbarToggle>} */ 31 /** @type {!Object<string, !WebInspector.ToolbarToggle>} */
30 this._modeButtons = {}; 32 this._modeButtons = {};
31 if (!disableRotate) { 33 if (!disableRotate) {
32 var panModeButton = new WebInspector.ToolbarToggle(WebInspector.UIString ("Pan mode (X)"), "pan-toolbar-item"); 34 var panModeButton = new WebInspector.ToolbarToggle(WebInspector.UIString(' Pan mode (X)'), 'pan-toolbar-item');
33 panModeButton.addEventListener("click", this._setMode.bind(this, WebInsp ector.TransformController.Modes.Pan)); 35 panModeButton.addEventListener('click', this._setMode.bind(this, WebInspec tor.TransformController.Modes.Pan));
34 this._modeButtons[WebInspector.TransformController.Modes.Pan] = panModeB utton; 36 this._modeButtons[WebInspector.TransformController.Modes.Pan] = panModeBut ton;
35 this._controlPanelToolbar.appendToolbarItem(panModeButton); 37 this._controlPanelToolbar.appendToolbarItem(panModeButton);
36 var rotateModeButton = new WebInspector.ToolbarToggle(WebInspector.UIStr ing("Rotate mode (V)"), "rotate-toolbar-item"); 38 var rotateModeButton =
37 rotateModeButton.addEventListener("click", this._setMode.bind(this, WebI nspector.TransformController.Modes.Rotate)); 39 new WebInspector.ToolbarToggle(WebInspector.UIString('Rotate mode (V)' ), 'rotate-toolbar-item');
38 this._modeButtons[WebInspector.TransformController.Modes.Rotate] = rotat eModeButton; 40 rotateModeButton.addEventListener(
39 this._controlPanelToolbar.appendToolbarItem(rotateModeButton); 41 'click', this._setMode.bind(this, WebInspector.TransformController.Mod es.Rotate));
42 this._modeButtons[WebInspector.TransformController.Modes.Rotate] = rotateM odeButton;
43 this._controlPanelToolbar.appendToolbarItem(rotateModeButton);
40 } 44 }
41 this._setMode(WebInspector.TransformController.Modes.Pan); 45 this._setMode(WebInspector.TransformController.Modes.Pan);
42 46
43 var resetButton = new WebInspector.ToolbarButton(WebInspector.UIString("Rese t transform (0)"), "center-toolbar-item"); 47 var resetButton =
44 resetButton.addEventListener("click", this.resetAndNotify.bind(this, undefin ed)); 48 new WebInspector.ToolbarButton(WebInspector.UIString('Reset transform (0 )'), 'center-toolbar-item');
49 resetButton.addEventListener('click', this.resetAndNotify.bind(this, undefin ed));
45 this._controlPanelToolbar.appendToolbarItem(resetButton); 50 this._controlPanelToolbar.appendToolbarItem(resetButton);
46 51
47 this._reset(); 52 this._reset();
53 }
54
55 /**
56 * @return {!WebInspector.Toolbar}
57 */
58 toolbar() {
59 return this._controlPanelToolbar;
60 }
61
62 _onKeyDown(event) {
63 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Shift.code) {
64 this._toggleMode();
65 return;
66 }
67
68 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEventIgnoringModi fiers(event);
69 var handler = this._shortcuts[shortcutKey];
70 if (handler && handler(event))
71 event.consume();
72 }
73
74 _onKeyUp(event) {
75 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Shift.code)
76 this._toggleMode();
77 }
78
79 _addShortcuts(keys, handler) {
80 for (var i = 0; i < keys.length; ++i)
81 this._shortcuts[keys[i].key] = handler;
82 }
83
84 _registerShortcuts() {
85 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.ResetVi ew, this.resetAndNotify.bind(this));
86 this._addShortcuts(
87 WebInspector.ShortcutsScreen.LayersPanelShortcuts.PanMode,
88 this._setMode.bind(this, WebInspector.TransformController.Modes.Pan));
89 this._addShortcuts(
90 WebInspector.ShortcutsScreen.LayersPanelShortcuts.RotateMode,
91 this._setMode.bind(this, WebInspector.TransformController.Modes.Rotate)) ;
92 var zoomFactor = 1.1;
93 this._addShortcuts(
94 WebInspector.ShortcutsScreen.LayersPanelShortcuts.ZoomIn, this._onKeyboa rdZoom.bind(this, zoomFactor));
95 this._addShortcuts(
96 WebInspector.ShortcutsScreen.LayersPanelShortcuts.ZoomOut, this._onKeybo ardZoom.bind(this, 1 / zoomFactor));
97 this._addShortcuts(
98 WebInspector.ShortcutsScreen.LayersPanelShortcuts.Up, this._onKeyboardPa nOrRotate.bind(this, 0, -1));
99 this._addShortcuts(
100 WebInspector.ShortcutsScreen.LayersPanelShortcuts.Down, this._onKeyboard PanOrRotate.bind(this, 0, 1));
101 this._addShortcuts(
102 WebInspector.ShortcutsScreen.LayersPanelShortcuts.Left, this._onKeyboard PanOrRotate.bind(this, -1, 0));
103 this._addShortcuts(
104 WebInspector.ShortcutsScreen.LayersPanelShortcuts.Right, this._onKeyboar dPanOrRotate.bind(this, 1, 0));
105 }
106
107 _postChangeEvent() {
108 this.dispatchEventToListeners(WebInspector.TransformController.Events.Transf ormChanged);
109 }
110
111 _reset() {
112 this._scale = 1;
113 this._offsetX = 0;
114 this._offsetY = 0;
115 this._rotateX = 0;
116 this._rotateY = 0;
117 }
118
119 _toggleMode() {
120 this._setMode(
121 this._mode === WebInspector.TransformController.Modes.Pan ? WebInspector .TransformController.Modes.Rotate :
122 WebInspector .TransformController.Modes.Pan);
123 }
124
125 /**
126 * @param {!WebInspector.TransformController.Modes} mode
127 */
128 _setMode(mode) {
129 if (this._mode === mode)
130 return;
131 this._mode = mode;
132 this._updateModeButtons();
133 this.element.focus();
134 }
135
136 _updateModeButtons() {
137 for (var mode in this._modeButtons)
138 this._modeButtons[mode].setToggled(mode === this._mode);
139 }
140
141 /**
142 * @param {!Event=} event
143 */
144 resetAndNotify(event) {
145 this._reset();
146 this._postChangeEvent();
147 if (event)
148 event.preventDefault();
149 this.element.focus();
150 }
151
152 /**
153 * @param {number} minScale
154 * @param {number} maxScale
155 */
156 setScaleConstraints(minScale, maxScale) {
157 this._minScale = minScale;
158 this._maxScale = maxScale;
159 this._scale = Number.constrain(this._scale, minScale, maxScale);
160 }
161
162 /**
163 * @param {number} minX
164 * @param {number} maxX
165 * @param {number} minY
166 * @param {number} maxY
167 */
168 clampOffsets(minX, maxX, minY, maxY) {
169 this._offsetX = Number.constrain(this._offsetX, minX, maxX);
170 this._offsetY = Number.constrain(this._offsetY, minY, maxY);
171 }
172
173 /**
174 * @return {number}
175 */
176 scale() {
177 return this._scale;
178 }
179
180 /**
181 * @return {number}
182 */
183 offsetX() {
184 return this._offsetX;
185 }
186
187 /**
188 * @return {number}
189 */
190 offsetY() {
191 return this._offsetY;
192 }
193
194 /**
195 * @return {number}
196 */
197 rotateX() {
198 return this._rotateX;
199 }
200
201 /**
202 * @return {number}
203 */
204 rotateY() {
205 return this._rotateY;
206 }
207
208 /**
209 * @param {number} scaleFactor
210 * @param {number} x
211 * @param {number} y
212 */
213 _onScale(scaleFactor, x, y) {
214 scaleFactor = Number.constrain(this._scale * scaleFactor, this._minScale, th is._maxScale) / this._scale;
215 this._scale *= scaleFactor;
216 this._offsetX -= (x - this._offsetX) * (scaleFactor - 1);
217 this._offsetY -= (y - this._offsetY) * (scaleFactor - 1);
218 this._postChangeEvent();
219 }
220
221 /**
222 * @param {number} offsetX
223 * @param {number} offsetY
224 */
225 _onPan(offsetX, offsetY) {
226 this._offsetX += offsetX;
227 this._offsetY += offsetY;
228 this._postChangeEvent();
229 }
230
231 /**
232 * @param {number} rotateX
233 * @param {number} rotateY
234 */
235 _onRotate(rotateX, rotateY) {
236 this._rotateX = rotateX;
237 this._rotateY = rotateY;
238 this._postChangeEvent();
239 }
240
241 /**
242 * @param {number} zoomFactor
243 */
244 _onKeyboardZoom(zoomFactor) {
245 this._onScale(zoomFactor, this.element.clientWidth / 2, this.element.clientH eight / 2);
246 }
247
248 /**
249 * @param {number} xMultiplier
250 * @param {number} yMultiplier
251 */
252 _onKeyboardPanOrRotate(xMultiplier, yMultiplier) {
253 var panStepInPixels = 6;
254 var rotateStepInDegrees = 5;
255
256 if (this._mode === WebInspector.TransformController.Modes.Rotate) {
257 // Sic! _onRotate treats X and Y as "rotate around X" and "rotate around Y ", so swap X/Y multiplers.
258 this._onRotate(
259 this._rotateX + yMultiplier * rotateStepInDegrees, this._rotateY + xMu ltiplier * rotateStepInDegrees);
260 } else {
261 this._onPan(xMultiplier * panStepInPixels, yMultiplier * panStepInPixels);
262 }
263 }
264
265 /**
266 * @param {!Event} event
267 */
268 _onMouseWheel(event) {
269 /** @const */
270 var zoomFactor = 1.1;
271 /** @const */
272 var mouseWheelZoomSpeed = 1 / 120;
273 var scaleFactor = Math.pow(zoomFactor, event.wheelDeltaY * mouseWheelZoomSpe ed);
274 this._onScale(
275 scaleFactor, event.clientX - this.element.totalOffsetLeft(), event.clien tY - this.element.totalOffsetTop());
276 }
277
278 /**
279 * @param {!Event} event
280 */
281 _onDrag(event) {
282 if (this._mode === WebInspector.TransformController.Modes.Rotate) {
283 this._onRotate(
284 this._oldRotateX + (this._originY - event.clientY) / this.element.clie ntHeight * 180,
285 this._oldRotateY - (this._originX - event.clientX) / this.element.clie ntWidth * 180);
286 } else {
287 this._onPan(event.clientX - this._originX, event.clientY - this._originY);
288 this._originX = event.clientX;
289 this._originY = event.clientY;
290 }
291 }
292
293 /**
294 * @param {!MouseEvent} event
295 */
296 _onDragStart(event) {
297 this.element.focus();
298 this._originX = event.clientX;
299 this._originY = event.clientY;
300 this._oldRotateX = this._rotateX;
301 this._oldRotateY = this._rotateY;
302 return true;
303 }
304
305 _onDragEnd() {
306 delete this._originX;
307 delete this._originY;
308 delete this._oldRotateX;
309 delete this._oldRotateY;
310 }
48 }; 311 };
49 312
50 /** @enum {symbol} */ 313 /** @enum {symbol} */
51 WebInspector.TransformController.Events = { 314 WebInspector.TransformController.Events = {
52 TransformChanged: Symbol("TransformChanged") 315 TransformChanged: Symbol('TransformChanged')
53 }; 316 };
54 317
55 /** 318 /**
56 * @enum {string} 319 * @enum {string}
57 */ 320 */
58 WebInspector.TransformController.Modes = { 321 WebInspector.TransformController.Modes = {
59 Pan: "Pan", 322 Pan: 'Pan',
60 Rotate: "Rotate", 323 Rotate: 'Rotate',
61 }; 324 };
62
63 WebInspector.TransformController.prototype = {
64 /**
65 * @return {!WebInspector.Toolbar}
66 */
67 toolbar: function()
68 {
69 return this._controlPanelToolbar;
70 },
71
72 _onKeyDown: function(event)
73 {
74 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Shift.code) {
75 this._toggleMode();
76 return;
77 }
78
79 var shortcutKey = WebInspector.KeyboardShortcut.makeKeyFromEventIgnoring Modifiers(event);
80 var handler = this._shortcuts[shortcutKey];
81 if (handler && handler(event))
82 event.consume();
83 },
84
85 _onKeyUp: function(event)
86 {
87 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Shift.code)
88 this._toggleMode();
89 },
90
91 _addShortcuts: function(keys, handler)
92 {
93 for (var i = 0; i < keys.length; ++i)
94 this._shortcuts[keys[i].key] = handler;
95 },
96
97 _registerShortcuts: function()
98 {
99 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Res etView, this.resetAndNotify.bind(this));
100 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Pan Mode, this._setMode.bind(this, WebInspector.TransformController.Modes.Pan));
101 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Rot ateMode, this._setMode.bind(this, WebInspector.TransformController.Modes.Rotate) );
102 var zoomFactor = 1.1;
103 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Zoo mIn, this._onKeyboardZoom.bind(this, zoomFactor));
104 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Zoo mOut, this._onKeyboardZoom.bind(this, 1 / zoomFactor));
105 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Up, this._onKeyboardPanOrRotate.bind(this, 0, -1));
106 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Dow n, this._onKeyboardPanOrRotate.bind(this, 0, 1));
107 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Lef t, this._onKeyboardPanOrRotate.bind(this, -1, 0));
108 this._addShortcuts(WebInspector.ShortcutsScreen.LayersPanelShortcuts.Rig ht, this._onKeyboardPanOrRotate.bind(this, 1, 0));
109 },
110
111 _postChangeEvent: function()
112 {
113 this.dispatchEventToListeners(WebInspector.TransformController.Events.Tr ansformChanged);
114 },
115
116 _reset: function()
117 {
118 this._scale = 1;
119 this._offsetX = 0;
120 this._offsetY = 0;
121 this._rotateX = 0;
122 this._rotateY = 0;
123 },
124
125 _toggleMode: function()
126 {
127 this._setMode(this._mode === WebInspector.TransformController.Modes.Pan ? WebInspector.TransformController.Modes.Rotate : WebInspector.TransformControll er.Modes.Pan);
128 },
129
130 /**
131 * @param {!WebInspector.TransformController.Modes} mode
132 */
133 _setMode: function(mode)
134 {
135 if (this._mode === mode)
136 return;
137 this._mode = mode;
138 this._updateModeButtons();
139 this.element.focus();
140 },
141
142 _updateModeButtons: function()
143 {
144 for (var mode in this._modeButtons)
145 this._modeButtons[mode].setToggled(mode === this._mode);
146 },
147
148 /**
149 * @param {!Event=} event
150 */
151 resetAndNotify: function(event)
152 {
153 this._reset();
154 this._postChangeEvent();
155 if (event)
156 event.preventDefault();
157 this.element.focus();
158 },
159
160 /**
161 * @param {number} minScale
162 * @param {number} maxScale
163 */
164 setScaleConstraints: function(minScale, maxScale)
165 {
166 this._minScale = minScale;
167 this._maxScale = maxScale;
168 this._scale = Number.constrain(this._scale, minScale, maxScale);
169 },
170
171 /**
172 * @param {number} minX
173 * @param {number} maxX
174 * @param {number} minY
175 * @param {number} maxY
176 */
177 clampOffsets: function(minX, maxX, minY, maxY)
178 {
179 this._offsetX = Number.constrain(this._offsetX, minX, maxX);
180 this._offsetY = Number.constrain(this._offsetY, minY, maxY);
181 },
182
183 /**
184 * @return {number}
185 */
186 scale: function()
187 {
188 return this._scale;
189 },
190
191 /**
192 * @return {number}
193 */
194 offsetX: function()
195 {
196 return this._offsetX;
197 },
198
199 /**
200 * @return {number}
201 */
202 offsetY: function()
203 {
204 return this._offsetY;
205 },
206
207 /**
208 * @return {number}
209 */
210 rotateX: function()
211 {
212 return this._rotateX;
213 },
214
215 /**
216 * @return {number}
217 */
218 rotateY: function()
219 {
220 return this._rotateY;
221 },
222
223 /**
224 * @param {number} scaleFactor
225 * @param {number} x
226 * @param {number} y
227 */
228 _onScale: function(scaleFactor, x, y)
229 {
230 scaleFactor = Number.constrain(this._scale * scaleFactor, this._minScale , this._maxScale) / this._scale;
231 this._scale *= scaleFactor;
232 this._offsetX -= (x - this._offsetX) * (scaleFactor - 1);
233 this._offsetY -= (y - this._offsetY) * (scaleFactor - 1);
234 this._postChangeEvent();
235 },
236
237 /**
238 * @param {number} offsetX
239 * @param {number} offsetY
240 */
241 _onPan: function(offsetX, offsetY)
242 {
243 this._offsetX += offsetX;
244 this._offsetY += offsetY;
245 this._postChangeEvent();
246 },
247
248 /**
249 * @param {number} rotateX
250 * @param {number} rotateY
251 */
252 _onRotate: function(rotateX, rotateY)
253 {
254 this._rotateX = rotateX;
255 this._rotateY = rotateY;
256 this._postChangeEvent();
257 },
258
259 /**
260 * @param {number} zoomFactor
261 */
262 _onKeyboardZoom: function(zoomFactor)
263 {
264 this._onScale(zoomFactor, this.element.clientWidth / 2, this.element.cli entHeight / 2);
265 },
266
267 /**
268 * @param {number} xMultiplier
269 * @param {number} yMultiplier
270 */
271 _onKeyboardPanOrRotate: function(xMultiplier, yMultiplier)
272 {
273 var panStepInPixels = 6;
274 var rotateStepInDegrees = 5;
275
276 if (this._mode === WebInspector.TransformController.Modes.Rotate) {
277 // Sic! _onRotate treats X and Y as "rotate around X" and "rotate ar ound Y", so swap X/Y multiplers.
278 this._onRotate(this._rotateX + yMultiplier * rotateStepInDegrees, th is._rotateY + xMultiplier * rotateStepInDegrees);
279 } else {
280 this._onPan(xMultiplier * panStepInPixels, yMultiplier * panStepInPi xels);
281 }
282 },
283
284 /**
285 * @param {!Event} event
286 */
287 _onMouseWheel: function(event)
288 {
289 /** @const */
290 var zoomFactor = 1.1;
291 /** @const */
292 var mouseWheelZoomSpeed = 1 / 120;
293 var scaleFactor = Math.pow(zoomFactor, event.wheelDeltaY * mouseWheelZoo mSpeed);
294 this._onScale(scaleFactor, event.clientX - this.element.totalOffsetLeft( ), event.clientY - this.element.totalOffsetTop());
295 },
296
297 /**
298 * @param {!Event} event
299 */
300 _onDrag: function(event)
301 {
302 if (this._mode === WebInspector.TransformController.Modes.Rotate) {
303 this._onRotate(this._oldRotateX + (this._originY - event.clientY) / this.element.clientHeight * 180, this._oldRotateY - (this._originX - event.clien tX) / this.element.clientWidth * 180);
304 } else {
305 this._onPan(event.clientX - this._originX, event.clientY - this._ori ginY);
306 this._originX = event.clientX;
307 this._originY = event.clientY;
308 }
309 },
310
311 /**
312 * @param {!MouseEvent} event
313 */
314 _onDragStart: function(event)
315 {
316 this.element.focus();
317 this._originX = event.clientX;
318 this._originY = event.clientY;
319 this._oldRotateX = this._rotateX;
320 this._oldRotateY = this._rotateY;
321 return true;
322 },
323
324 _onDragEnd: function()
325 {
326 delete this._originX;
327 delete this._originY;
328 delete this._oldRotateX;
329 delete this._oldRotateY;
330 },
331
332 __proto__: WebInspector.Object.prototype
333 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698