Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / StyleSheetOutlineDialog.js
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  * @param {function(number, number)} selectItemCallback
35  */
36 WebInspector.StyleSheetOutlineDialog = function(view, uiSourceCode, selectItemCallback)
37 {
38     WebInspector.SelectionDialogContentProvider.call(this);
39     this._selectItemCallback = selectItemCallback;
40     this._rules = [];
41     this._view = view;
42     this._uiSourceCode = uiSourceCode;
43     this._requestItems();
44 }
45
46 /**
47  * @param {!WebInspector.View} view
48  * @param {!WebInspector.UISourceCode} uiSourceCode
49  * @param {function(number, number)} selectItemCallback
50  */
51 WebInspector.StyleSheetOutlineDialog.show = function(view, uiSourceCode, selectItemCallback)
52 {
53     if (WebInspector.Dialog.currentInstance())
54         return null;
55     var delegate = new WebInspector.StyleSheetOutlineDialog(view, uiSourceCode, selectItemCallback);
56     var filteredItemSelectionDialog = new WebInspector.FilteredItemSelectionDialog(delegate);
57     WebInspector.Dialog.show(view.element, filteredItemSelectionDialog);
58 }
59
60 WebInspector.StyleSheetOutlineDialog.prototype = {
61     /**
62      * @return {number}
63      */
64     itemCount: function()
65     {
66         return this._rules.length;
67     },
68
69     /**
70      * @param {number} itemIndex
71      * @return {string}
72      */
73     itemKeyAt: function(itemIndex)
74     {
75         return this._rules[itemIndex].selectorText;
76     },
77
78     /**
79      * @param {number} itemIndex
80      * @param {string} query
81      * @return {number}
82      */
83     itemScoreAt: function(itemIndex, query)
84     {
85         var rule = this._rules[itemIndex];
86         return -rule.rawLocation.lineNumber;
87     },
88
89     /**
90      * @param {number} itemIndex
91      * @param {string} query
92      * @param {!Element} titleElement
93      * @param {!Element} subtitleElement
94      */
95     renderItem: function(itemIndex, query, titleElement, subtitleElement)
96     {
97         var rule = this._rules[itemIndex];
98         titleElement.textContent = rule.selectorText;
99         this.highlightRanges(titleElement, query);
100         subtitleElement.textContent = ":" + (rule.rawLocation.lineNumber + 1);
101     },
102
103     _requestItems: function()
104     {
105         /**
106          * @param {?Protocol.Error} error
107          * @param {!Array.<!CSSAgent.CSSStyleSheetHeader>} infos
108          * @this {WebInspector.StyleSheetOutlineDialog}
109          */
110         function didGetAllStyleSheets(error, infos)
111         {
112             if (error)
113                 return;
114
115             for (var i = 0; i < infos.length; ++i) {
116                 var info = infos[i];
117                 if (info.sourceURL === this._uiSourceCode.url) {
118                     WebInspector.CSSStyleSheet.createForId(info.styleSheetId, didGetStyleSheet.bind(this));
119                     return;
120                 }
121             }
122         }
123
124         CSSAgent.getAllStyleSheets(didGetAllStyleSheets.bind(this));
125
126         /**
127          * @param {?WebInspector.CSSStyleSheet} styleSheet
128          * @this {WebInspector.StyleSheetOutlineDialog}
129          */
130         function didGetStyleSheet(styleSheet)
131         {
132             if (!styleSheet)
133                 return;
134
135             this._rules = styleSheet.rules;
136             this.refresh();
137         }
138     },
139
140     /**
141      * @param {number} itemIndex
142      * @param {string} promptValue
143      */
144     selectItem: function(itemIndex, promptValue)
145     {
146         var rule = this._rules[itemIndex];
147         var lineNumber = rule.rawLocation.lineNumber;
148         if (!isNaN(lineNumber) && lineNumber >= 0)
149             this._selectItemCallback(lineNumber, rule.rawLocation.columnNumber);
150     },
151
152     __proto__: WebInspector.SelectionDialogContentProvider.prototype
153 }