Home | History | Annotate | Download | only in front-end
      1 /*
      2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2009 Google Inc.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *
      9  * 1.  Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  * 2.  Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     15  *     its contributors may be used to endorse or promote products derived
     16  *     from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 WebInspector.Section = function(title, subtitle)
     31 {
     32     this.element = document.createElement("div");
     33     this.element.className = "section";
     34     this.element._section = this;
     35 
     36     this.headerElement = document.createElement("div");
     37     this.headerElement.className = "header";
     38 
     39     this.titleElement = document.createElement("div");
     40     this.titleElement.className = "title";
     41 
     42     this.subtitleElement = document.createElement("div");
     43     this.subtitleElement.className = "subtitle";
     44 
     45     this.headerElement.appendChild(this.subtitleElement);
     46     this.headerElement.appendChild(this.titleElement);
     47 
     48     this.headerElement.addEventListener("click", this.handleClick.bind(this), false);
     49     this.element.appendChild(this.headerElement);
     50 
     51     this.title = title;
     52     this.subtitle = subtitle;
     53     this._expanded = false;
     54 }
     55 
     56 WebInspector.Section.prototype = {
     57     get title()
     58     {
     59         return this._title;
     60     },
     61 
     62     set title(x)
     63     {
     64         if (this._title === x)
     65             return;
     66         this._title = x;
     67 
     68         if (x instanceof Node) {
     69             this.titleElement.removeChildren();
     70             this.titleElement.appendChild(x);
     71         } else
     72           this.titleElement.textContent = x;
     73     },
     74 
     75     get subtitle()
     76     {
     77         return this._subtitle;
     78     },
     79 
     80     set subtitle(x)
     81     {
     82         if (this._subtitle === x)
     83             return;
     84         this._subtitle = x;
     85         this.subtitleElement.textContent = x;
     86     },
     87 
     88     get subtitleAsTextForTest()
     89     {
     90         var result = this.subtitleElement.textContent;
     91         var child = this.subtitleElement.querySelector("[data-uncopyable]");
     92         if (child) {
     93             var linkData = child.getAttribute("data-uncopyable");
     94             if (linkData)
     95                 result += linkData;
     96         }
     97         return result;
     98     },
     99 
    100     get expanded()
    101     {
    102         return this._expanded;
    103     },
    104 
    105     set expanded(x)
    106     {
    107         if (x)
    108             this.expand();
    109         else
    110             this.collapse();
    111     },
    112 
    113     get populated()
    114     {
    115         return this._populated;
    116     },
    117 
    118     set populated(x)
    119     {
    120         this._populated = x;
    121         if (!x && this.onpopulate && this._expanded) {
    122             this.onpopulate(this);
    123             this._populated = true;
    124         }
    125     },
    126 
    127     get nextSibling()
    128     {
    129         var curElement = this.element;
    130         do {
    131             curElement = curElement.nextSibling;
    132         } while (curElement && !curElement._section);
    133 
    134         return curElement ? curElement._section : null;
    135     },
    136 
    137     get previousSibling()
    138     {
    139         var curElement = this.element;
    140         do {
    141             curElement = curElement.previousSibling;
    142         } while (curElement && !curElement._section);
    143 
    144         return curElement ? curElement._section : null;
    145     },
    146 
    147     expand: function()
    148     {
    149         if (this._expanded)
    150             return;
    151         this._expanded = true;
    152         this.element.addStyleClass("expanded");
    153 
    154         if (!this._populated && this.onpopulate) {
    155             this.onpopulate(this);
    156             this._populated = true;
    157         }
    158     },
    159 
    160     collapse: function()
    161     {
    162         if (!this._expanded)
    163             return;
    164         this._expanded = false;
    165         this.element.removeStyleClass("expanded");
    166     },
    167 
    168     toggleExpanded: function()
    169     {
    170         this.expanded = !this.expanded;
    171     },
    172 
    173     handleClick: function(e)
    174     {
    175         this.toggleExpanded();
    176         e.stopPropagation();
    177     }
    178 }
    179