Home | History | Annotate | Download | only in front-end
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2008, 2009 Anthony Ricaud <rik (at) webkit.org>
      4  * Copyright (C) 2009 Google Inc. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 WebInspector.TimelineGrid = function()
     32 {
     33     this.element = document.createElement("div");
     34 
     35     this._itemsGraphsElement = document.createElement("div");
     36     this._itemsGraphsElement.id = "resources-graphs";
     37     this.element.appendChild(this._itemsGraphsElement);
     38 
     39     this._dividersElement = document.createElement("div");
     40     this._dividersElement.id = "resources-dividers";
     41     this.element.appendChild(this._dividersElement);
     42 
     43     this._eventDividersElement = document.createElement("div");
     44     this._eventDividersElement.id = "resources-event-dividers";
     45     this.element.appendChild(this._eventDividersElement);
     46 
     47     this._dividersLabelBarElement = document.createElement("div");
     48     this._dividersLabelBarElement.id = "resources-dividers-label-bar";
     49     this.element.appendChild(this._dividersLabelBarElement);
     50 }
     51 
     52 WebInspector.TimelineGrid.prototype = {
     53     get itemsGraphsElement()
     54     {
     55         return this._itemsGraphsElement;
     56     },
     57 
     58     updateDividers: function(force, calculator, paddingLeft)
     59     {
     60         var dividerCount = Math.round(this._dividersElement.offsetWidth / 64);
     61         var slice = calculator.boundarySpan / dividerCount;
     62         if (!force && this._currentDividerSlice === slice)
     63             return false;
     64 
     65         if (typeof paddingLeft !== "number")
     66             paddingLeft = 0;
     67         this._currentDividerSlice = slice;
     68 
     69         this._eventDividersElement.removeChildren();
     70         // Reuse divider elements and labels.
     71         var divider = this._dividersElement.firstChild;
     72         var dividerLabelBar = this._dividersLabelBarElement.firstChild;
     73 
     74         var dividersLabelBarElementClientWidth = this._dividersLabelBarElement.clientWidth;
     75         var clientWidth = dividersLabelBarElementClientWidth - paddingLeft;
     76         for (var i = paddingLeft ? 0 : 1; i <= dividerCount; ++i) {
     77             if (!divider) {
     78                 divider = document.createElement("div");
     79                 divider.className = "resources-divider";
     80                 this._dividersElement.appendChild(divider);
     81 
     82                 dividerLabelBar = document.createElement("div");
     83                 dividerLabelBar.className = "resources-divider";
     84                 var label = document.createElement("div");
     85                 label.className = "resources-divider-label";
     86                 dividerLabelBar._labelElement = label;
     87                 dividerLabelBar.appendChild(label);
     88                 this._dividersLabelBarElement.appendChild(dividerLabelBar);
     89                 dividersLabelBarElementClientWidth = this._dividersLabelBarElement.clientWidth;
     90             }
     91 
     92             if (i === dividerCount)
     93                 divider.addStyleClass("last");
     94             else
     95                 divider.removeStyleClass("last");
     96 
     97             var left = paddingLeft + clientWidth * (i / dividerCount);
     98             var percentLeft = 100 * left / dividersLabelBarElementClientWidth;
     99             this._setDividerAndBarLeft(divider, dividerLabelBar, percentLeft);
    100 
    101             if (!isNaN(slice))
    102                 dividerLabelBar._labelElement.textContent = calculator.formatValue(slice * i);
    103             else
    104                 dividerLabelBar._labelElement.textContent = "";
    105 
    106             divider = divider.nextSibling;
    107             dividerLabelBar = dividerLabelBar.nextSibling;
    108         }
    109 
    110         // Remove extras.
    111         while (divider) {
    112             var nextDivider = divider.nextSibling;
    113             this._dividersElement.removeChild(divider);
    114             divider = nextDivider;
    115         }
    116         while (dividerLabelBar) {
    117             var nextDivider = dividerLabelBar.nextSibling;
    118             this._dividersLabelBarElement.removeChild(dividerLabelBar);
    119             dividerLabelBar = nextDivider;
    120         }
    121         return true;
    122     },
    123 
    124     _setDividerAndBarLeft: function(divider, dividerLabelBar, percentLeft)
    125     {
    126         var percentStyleLeft = parseFloat(divider.style.left);
    127         if (!isNaN(percentStyleLeft) && Math.abs(percentStyleLeft - percentLeft) < 0.1)
    128             return;
    129         divider.style.left = percentLeft + "%";
    130         dividerLabelBar.style.left = percentLeft + "%";
    131     },
    132 
    133     addEventDivider: function(divider)
    134     {
    135         this._eventDividersElement.appendChild(divider);
    136     },
    137 
    138     setScrollAndDividerTop: function(scrollTop, dividersTop)
    139     {
    140         this._dividersElement.style.top = scrollTop + "px";
    141         this._eventDividersElement.style.top = scrollTop + "px";
    142         this._dividersLabelBarElement.style.top = dividersTop + "px";
    143     }
    144 }
    145