2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {string} prefix
35 WebInspector.OverviewGrid = function(prefix)
37 this.element = document.createElement("div");
38 this.element.id = prefix + "-overview-container";
40 this._grid = new WebInspector.TimelineGrid();
41 this._grid.element.id = prefix + "-overview-grid";
42 this._grid.setScrollAndDividerTop(0, 0);
44 this.element.appendChild(this._grid.element);
46 this._window = new WebInspector.OverviewGrid.Window(this.element, this._grid.dividersLabelBarElement);
49 WebInspector.OverviewGrid.prototype = {
53 clientWidth: function()
55 return this.element.clientWidth;
59 * @param {!WebInspector.TimelineGrid.Calculator} calculator
61 updateDividers: function(calculator)
63 this._grid.updateDividers(calculator);
67 * @param {!Array.<!Element>} dividers
69 addEventDividers: function(dividers)
71 this._grid.addEventDividers(dividers);
74 removeEventDividers: function()
76 this._grid.removeEventDividers();
80 * @param {?number} start
81 * @param {?number} end
83 setWindowPosition: function(start, end)
85 this._window._setWindowPosition(start, end);
96 windowLeft: function()
98 return this._window.windowLeft;
104 windowRight: function()
106 return this._window.windowRight;
110 * @param {number} left
111 * @param {number} right
113 setWindow: function(left, right)
115 this._window._setWindow(left, right);
119 * @param {string} eventType
120 * @param {function(!WebInspector.Event)} listener
121 * @param {!Object=} thisObject
123 addEventListener: function(eventType, listener, thisObject)
125 this._window.addEventListener(eventType, listener, thisObject);
129 * @param {!number} zoomFactor
130 * @param {!number} referencePoint
132 zoom: function(zoomFactor, referencePoint)
134 this._window._zoom(zoomFactor, referencePoint);
138 * @param {boolean} enabled
140 setResizeEnabled: function(enabled)
142 this._window._setEnabled(!!enabled);
147 WebInspector.OverviewGrid.MinSelectableSize = 14;
149 WebInspector.OverviewGrid.WindowScrollSpeedFactor = .3;
151 WebInspector.OverviewGrid.ResizerOffset = 3.5; // half pixel because offset values are not rounded but ceiled
155 * @extends {WebInspector.Object}
156 * @param {!Element} parentElement
157 * @param {!Element} dividersLabelBarElement
159 WebInspector.OverviewGrid.Window = function(parentElement, dividersLabelBarElement)
161 this._parentElement = parentElement;
162 this._dividersLabelBarElement = dividersLabelBarElement;
164 WebInspector.installDragHandle(this._parentElement, this._startWindowSelectorDragging.bind(this), this._windowSelectorDragging.bind(this), this._endWindowSelectorDragging.bind(this), "ew-resize", null);
165 WebInspector.installDragHandle(this._dividersLabelBarElement, this._startWindowDragging.bind(this), this._windowDragging.bind(this), null, "move");
167 this.windowLeft = 0.0;
168 this.windowRight = 1.0;
170 this._parentElement.addEventListener("mousewheel", this._onMouseWheel.bind(this), true);
171 this._parentElement.addEventListener("dblclick", this._resizeWindowMaximum.bind(this), true);
173 this._overviewWindowElement = parentElement.createChild("div", "overview-grid-window");
174 this._overviewWindowBordersElement = parentElement.createChild("div", "overview-grid-window-rulers");
175 parentElement.createChild("div", "overview-grid-dividers-background");
177 this._leftResizeElement = parentElement.createChild("div", "overview-grid-window-resizer");
178 this._leftResizeElement.style.left = 0;
179 WebInspector.installDragHandle(this._leftResizeElement, this._resizerElementStartDragging.bind(this), this._leftResizeElementDragging.bind(this), null, "ew-resize");
181 this._rightResizeElement = parentElement.createChild("div", "overview-grid-window-resizer overview-grid-window-resizer-right");
182 this._rightResizeElement.style.right = 0;
183 WebInspector.installDragHandle(this._rightResizeElement, this._resizerElementStartDragging.bind(this), this._rightResizeElementDragging.bind(this), null, "ew-resize");
184 this._setEnabled(true);
187 WebInspector.OverviewGrid.Events = {
188 WindowChanged: "WindowChanged"
191 WebInspector.OverviewGrid.Window.prototype = {
194 this.windowLeft = 0.0;
195 this.windowRight = 1.0;
197 this._overviewWindowElement.style.left = "0%";
198 this._overviewWindowElement.style.width = "100%";
199 this._overviewWindowBordersElement.style.left = "0%";
200 this._overviewWindowBordersElement.style.right = "0%";
201 this._leftResizeElement.style.left = "0%";
202 this._rightResizeElement.style.left = "100%";
203 this._setEnabled(true);
207 * @param {boolean} enabled
209 _setEnabled: function(enabled)
212 if (this._enabled === enabled)
214 this._enabled = enabled;
218 * @param {!Event} event
220 _resizerElementStartDragging: function(event)
224 this._resizerParentOffsetLeft = event.pageX - event.offsetX - event.target.offsetLeft;
225 event.preventDefault();
230 * @param {!Event} event
232 _leftResizeElementDragging: function(event)
234 this._resizeWindowLeft(event.pageX - this._resizerParentOffsetLeft);
235 event.preventDefault();
239 * @param {!Event} event
241 _rightResizeElementDragging: function(event)
243 this._resizeWindowRight(event.pageX - this._resizerParentOffsetLeft);
244 event.preventDefault();
248 * @param {!Event} event
251 _startWindowSelectorDragging: function(event)
255 this._offsetLeft = this._parentElement.totalOffsetLeft();
256 var position = event.x - this._offsetLeft;
257 this._overviewWindowSelector = new WebInspector.OverviewGrid.WindowSelector(this._parentElement, position);
262 * @param {!Event} event
264 _windowSelectorDragging: function(event)
266 this._overviewWindowSelector._updatePosition(event.x - this._offsetLeft);
267 event.preventDefault();
271 * @param {!Event} event
273 _endWindowSelectorDragging: function(event)
275 var window = this._overviewWindowSelector._close(event.x - this._offsetLeft);
276 delete this._overviewWindowSelector;
277 if (window.end === window.start) { // Click, not drag.
278 var middle = window.end;
279 window.start = Math.max(0, middle - WebInspector.OverviewGrid.MinSelectableSize / 2);
280 window.end = Math.min(this._parentElement.clientWidth, middle + WebInspector.OverviewGrid.MinSelectableSize / 2);
281 } else if (window.end - window.start < WebInspector.OverviewGrid.MinSelectableSize) {
282 if (this._parentElement.clientWidth - window.end > WebInspector.OverviewGrid.MinSelectableSize)
283 window.end = window.start + WebInspector.OverviewGrid.MinSelectableSize;
285 window.start = window.end - WebInspector.OverviewGrid.MinSelectableSize;
287 this._setWindowPosition(window.start, window.end);
291 * @param {!Event} event
294 _startWindowDragging: function(event)
296 this._dragStartPoint = event.pageX;
297 this._dragStartLeft = this.windowLeft;
298 this._dragStartRight = this.windowRight;
303 * @param {!Event} event
305 _windowDragging: function(event)
307 event.preventDefault();
308 var delta = (event.pageX - this._dragStartPoint) / this._parentElement.clientWidth;
309 if (this._dragStartLeft + delta < 0)
310 delta = -this._dragStartLeft;
312 if (this._dragStartRight + delta > 1)
313 delta = 1 - this._dragStartRight;
315 this._setWindow(this._dragStartLeft + delta, this._dragStartRight + delta);
319 * @param {number} start
321 _resizeWindowLeft: function(start)
326 else if (start > this._rightResizeElement.offsetLeft - 4)
327 start = this._rightResizeElement.offsetLeft - 4;
328 this._setWindowPosition(start, null);
332 * @param {number} end
334 _resizeWindowRight: function(end)
337 if (end > this._parentElement.clientWidth - 10)
338 end = this._parentElement.clientWidth;
339 else if (end < this._leftResizeElement.offsetLeft + WebInspector.OverviewGrid.MinSelectableSize)
340 end = this._leftResizeElement.offsetLeft + WebInspector.OverviewGrid.MinSelectableSize;
341 this._setWindowPosition(null, end);
344 _resizeWindowMaximum: function()
346 this._setWindowPosition(0, this._parentElement.clientWidth);
350 * @param {number} windowLeft
351 * @param {number} windowRight
353 _setWindow: function(windowLeft, windowRight)
355 var left = windowLeft;
356 var right = windowRight;
357 var width = windowRight - windowLeft;
359 // We allow actual time window to be arbitrarily small but don't want the UI window to be too small.
360 var widthInPixels = width * this._parentElement.clientWidth;
361 var minWidthInPixels = WebInspector.OverviewGrid.MinSelectableSize / 2;
362 if (widthInPixels < minWidthInPixels) {
363 var factor = minWidthInPixels / widthInPixels;
364 left = ((windowRight + windowLeft) - width * factor) / 2;
365 right = ((windowRight + windowLeft) + width * factor) / 2;
368 this.windowLeft = windowLeft;
369 this._leftResizeElement.style.left = left * 100 + "%";
370 this.windowRight = windowRight;
371 this._rightResizeElement.style.left = right * 100 + "%";
373 this._overviewWindowElement.style.left = left * 100 + "%";
374 this._overviewWindowBordersElement.style.left = left * 100 + "%";
375 this._overviewWindowElement.style.width = (right - left) * 100 + "%";
376 this._overviewWindowBordersElement.style.right = (1 - right) * 100 + "%";
378 this.dispatchEventToListeners(WebInspector.OverviewGrid.Events.WindowChanged);
382 * @param {?number} start
383 * @param {?number} end
385 _setWindowPosition: function(start, end)
387 var clientWidth = this._parentElement.clientWidth;
388 var windowLeft = typeof start === "number" ? start / clientWidth : this.windowLeft;
389 var windowRight = typeof end === "number" ? end / clientWidth : this.windowRight;
390 this._setWindow(windowLeft, windowRight);
394 * @param {?Event} event
396 _onMouseWheel: function(event)
398 if (typeof event.wheelDeltaY === "number" && event.wheelDeltaY) {
399 const zoomFactor = 1.1;
400 const mouseWheelZoomSpeed = 1 / 120;
402 var reference = event.offsetX / event.target.clientWidth;
403 this._zoom(Math.pow(zoomFactor, -event.wheelDeltaY * mouseWheelZoomSpeed), reference);
405 if (typeof event.wheelDeltaX === "number" && event.wheelDeltaX) {
406 var offset = Math.round(event.wheelDeltaX * WebInspector.OverviewGrid.WindowScrollSpeedFactor);
407 var windowLeft = this._leftResizeElement.offsetLeft + WebInspector.OverviewGrid.ResizerOffset;
408 var windowRight = this._rightResizeElement.offsetLeft + WebInspector.OverviewGrid.ResizerOffset;
410 if (windowLeft - offset < 0)
413 if (windowRight - offset > this._parentElement.clientWidth)
414 offset = windowRight - this._parentElement.clientWidth;
416 this._setWindowPosition(windowLeft - offset, windowRight - offset);
418 event.preventDefault();
423 * @param {number} factor
424 * @param {number} reference
426 _zoom: function(factor, reference)
428 var left = this.windowLeft;
429 var right = this.windowRight;
430 var windowSize = right - left;
431 var newWindowSize = factor * windowSize;
432 if (newWindowSize > 1) {
434 factor = newWindowSize / windowSize;
436 left = reference + (left - reference) * factor;
437 left = Number.constrain(left, 0, 1 - newWindowSize);
439 right = reference + (right - reference) * factor;
440 right = Number.constrain(right, newWindowSize, 1);
441 this._setWindow(left, right);
444 __proto__: WebInspector.Object.prototype
450 WebInspector.OverviewGrid.WindowSelector = function(parent, position)
452 this._startPosition = position;
453 this._width = parent.offsetWidth;
454 this._windowSelector = document.createElement("div");
455 this._windowSelector.className = "overview-grid-window-selector";
456 this._windowSelector.style.left = this._startPosition + "px";
457 this._windowSelector.style.right = this._width - this._startPosition + "px";
458 parent.appendChild(this._windowSelector);
461 WebInspector.OverviewGrid.WindowSelector.prototype = {
462 _close: function(position)
464 position = Math.max(0, Math.min(position, this._width));
465 this._windowSelector.remove();
466 return this._startPosition < position ? {start: this._startPosition, end: position} : {start: position, end: this._startPosition};
469 _updatePosition: function(position)
471 position = Math.max(0, Math.min(position, this._width));
472 if (position < this._startPosition) {
473 this._windowSelector.style.left = position + "px";
474 this._windowSelector.style.right = this._width - this._startPosition + "px";
476 this._windowSelector.style.left = this._startPosition + "px";
477 this._windowSelector.style.right = this._width - position + "px";