Home | History | Annotate | Download | only in front_end
      1 /*
      2  * Copyright (C) 2012 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  * 1. Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *
     11  * 2. Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
     20  * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /**
     30  * @constructor
     31  * @extends {WebInspector.SelectionDialogContentProvider}
     32  * @param {WebInspector.View} view
     33  * @param {WebInspector.UISourceCode} uiSourceCode
     34  */
     35 WebInspector.StyleSheetOutlineDialog = function(view, uiSourceCode)
     36 {
     37     WebInspector.SelectionDialogContentProvider.call(this);
     38 
     39     this._rules = [];
     40     this._view = view;
     41     this._uiSourceCode = uiSourceCode;
     42     this._requestItems();
     43 }
     44 
     45 /**
     46  * @param {WebInspector.View} view
     47  * @param {WebInspector.UISourceCode} uiSourceCode
     48  */
     49 WebInspector.StyleSheetOutlineDialog.show = function(view, uiSourceCode)
     50 {
     51     if (WebInspector.Dialog.currentInstance())
     52         return null;
     53     var delegate = new WebInspector.StyleSheetOutlineDialog(view, uiSourceCode);
     54     var filteredItemSelectionDialog = new WebInspector.FilteredItemSelectionDialog(delegate);
     55     WebInspector.Dialog.show(view.element, filteredItemSelectionDialog);
     56 }
     57 
     58 WebInspector.StyleSheetOutlineDialog.prototype = {
     59     /**
     60      * @return {number}
     61      */
     62     itemCount: function()
     63     {
     64         return this._rules.length;
     65     },
     66 
     67     /**
     68      * @param {number} itemIndex
     69      * @return {string}
     70      */
     71     itemKeyAt: function(itemIndex)
     72     {
     73         return this._rules[itemIndex].selectorText;
     74     },
     75 
     76     /**
     77      * @param {number} itemIndex
     78      * @param {string} query
     79      * @return {number}
     80      */
     81     itemScoreAt: function(itemIndex, query)
     82     {
     83         var rule = this._rules[itemIndex];
     84         return -rule.rawLocation.lineNumber;
     85     },
     86 
     87     /**
     88      * @param {number} itemIndex
     89      * @param {string} query
     90      * @param {Element} titleElement
     91      * @param {Element} subtitleElement
     92      */
     93     renderItem: function(itemIndex, query, titleElement, subtitleElement)
     94     {
     95         var rule = this._rules[itemIndex];
     96         titleElement.textContent = rule.selectorText;
     97         this.highlightRanges(titleElement, query);
     98         subtitleElement.textContent = ":" + (rule.rawLocation.lineNumber + 1);
     99     },
    100 
    101     _requestItems: function()
    102     {
    103         function didGetAllStyleSheets(error, infos)
    104         {
    105             if (error)
    106                 return;
    107 
    108             for (var i = 0; i < infos.length; ++i) {
    109                 var info = infos[i];
    110                 if (info.sourceURL === this._uiSourceCode.url) {
    111                     WebInspector.CSSStyleSheet.createForId(info.styleSheetId, didGetStyleSheet.bind(this));
    112                     return;
    113                 }
    114             }
    115         }
    116 
    117         CSSAgent.getAllStyleSheets(didGetAllStyleSheets.bind(this));
    118 
    119         /**
    120          * @param {?WebInspector.CSSStyleSheet} styleSheet
    121          */
    122         function didGetStyleSheet(styleSheet)
    123         {
    124             if (!styleSheet)
    125                 return;
    126 
    127             this._rules = styleSheet.rules;
    128             this.refresh();
    129         }
    130     },
    131 
    132     /**
    133      * @param {number} itemIndex
    134      * @param {string} promptValue
    135      */
    136     selectItem: function(itemIndex, promptValue)
    137     {
    138         var rule = this._rules[itemIndex];
    139         var lineNumber = rule.rawLocation.lineNumber;
    140         if (!isNaN(lineNumber) && lineNumber >= 0)
    141             this._view.highlightPosition(lineNumber, rule.rawLocation.columnNumber);
    142         this._view.focus();
    143     },
    144 
    145     __proto__: WebInspector.SelectionDialogContentProvider.prototype
    146 }
    147