Upstream version 5.34.104.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.UISourceCode} uiSourceCode
33  * @param {function(number, number)} selectItemCallback
34  */
35 WebInspector.StyleSheetOutlineDialog = function(uiSourceCode, selectItemCallback)
36 {
37     WebInspector.SelectionDialogContentProvider.call(this);
38     this._selectItemCallback = selectItemCallback;
39     this._rules = [];
40     this._outlineWorker = new Worker("ScriptFormatterWorker.js");
41     this._outlineWorker.onmessage = this._didBuildOutlineChunk.bind(this);
42     this._outlineWorker.postMessage({ method: "cssOutline", params: { content: uiSourceCode.workingCopy() } });
43 }
44
45 /**
46  * @param {!WebInspector.View} view
47  * @param {!WebInspector.UISourceCode} uiSourceCode
48  * @param {function(number, number)} selectItemCallback
49  */
50 WebInspector.StyleSheetOutlineDialog.show = function(view, uiSourceCode, selectItemCallback)
51 {
52     if (WebInspector.Dialog.currentInstance())
53         return null;
54     var delegate = new WebInspector.StyleSheetOutlineDialog(uiSourceCode, selectItemCallback);
55     var filteredItemSelectionDialog = new WebInspector.FilteredItemSelectionDialog(delegate);
56     WebInspector.Dialog.show(view.element, filteredItemSelectionDialog);
57 }
58
59 WebInspector.StyleSheetOutlineDialog.prototype = {
60     /**
61      * @param {!MessageEvent} event
62      */
63     _didBuildOutlineChunk: function(event)
64     {
65         var data = /** @type {!WebInspector.StyleSheetOutlineDialog.MessageEventData} */ (event.data);
66         var chunk = data.chunk;
67         for (var i = 0; i < chunk.length; ++i)
68             this._rules.push(chunk[i]);
69
70         if (data.total === data.index)
71             this.dispose();
72
73         this.refresh();
74     },
75
76     /**
77      * @return {number}
78      */
79     itemCount: function()
80     {
81         return this._rules.length;
82     },
83
84     /**
85      * @param {number} itemIndex
86      * @return {string}
87      */
88     itemKeyAt: function(itemIndex)
89     {
90         var rule = this._rules[itemIndex];
91         return rule.selectorText || rule.atRule;
92     },
93
94     /**
95      * @param {number} itemIndex
96      * @param {string} query
97      * @return {number}
98      */
99     itemScoreAt: function(itemIndex, query)
100     {
101         var rule = this._rules[itemIndex];
102         return -rule.lineNumber;
103     },
104
105     /**
106      * @param {number} itemIndex
107      * @param {string} query
108      * @param {!Element} titleElement
109      * @param {!Element} subtitleElement
110      */
111     renderItem: function(itemIndex, query, titleElement, subtitleElement)
112     {
113         var rule = this._rules[itemIndex];
114         titleElement.textContent = rule.selectorText || rule.atRule;
115         this.highlightRanges(titleElement, query);
116         subtitleElement.textContent = ":" + (rule.lineNumber + 1);
117     },
118
119     /**
120      * @param {number} itemIndex
121      * @param {string} promptValue
122      */
123     selectItem: function(itemIndex, promptValue)
124     {
125         var rule = this._rules[itemIndex];
126         var lineNumber = rule.lineNumber;
127         if (!isNaN(lineNumber) && lineNumber >= 0)
128             this._selectItemCallback(lineNumber, rule.columnNumber);
129     },
130
131     dispose: function()
132     {
133         if (this._outlineWorker) {
134             this._outlineWorker.terminate();
135             delete this._outlineWorker;
136         }
137     },
138
139     __proto__: WebInspector.SelectionDialogContentProvider.prototype
140 }
141
142 /**
143  * @typedef {{index: number, total: number, chunk: !Array.<!{selectorText: ?string, atRule: ?string, lineNumber: number, columnNumber: number}>}}
144  */
145 WebInspector.StyleSheetOutlineDialog.MessageEventData;