Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @extends {WebInspector.Widget} | 7 * @extends {WebInspector.Widget} |
| 8 */ | 8 */ |
| 9 WebInspector.ClassesPaneWidget = function() | 9 WebInspector.ClassesPaneWidget = function() |
| 10 { | 10 { |
| 11 WebInspector.Widget.call(this); | 11 WebInspector.Widget.call(this); |
| 12 this.element.className = "styles-element-classes-pane"; | 12 this.element.className = "styles-element-classes-pane"; |
| 13 var container = this.element.createChild("div", "title-container"); | 13 var container = this.element.createChild("div", "title-container"); |
| 14 this._input = container.createChild("input", "new-class-input monospace"); | 14 this._input = container.createChild("div", "new-class-input monospace"); |
| 15 this._input.placeholder = WebInspector.UIString("Add new class"); | 15 this._input.setAttribute("placeholder", WebInspector.UIString("Add new class ")); |
| 16 this._input.addEventListener("keydown", this._onKeyDown.bind(this), false); | |
| 17 this.setDefaultFocusedElement(this._input); | 16 this.setDefaultFocusedElement(this._input); |
| 18 this._classesContainer = this.element.createChild("div", "source-code"); | 17 this._classesContainer = this.element.createChild("div", "source-code"); |
| 19 this._classesContainer.classList.add("styles-element-classes-container"); | 18 this._classesContainer.classList.add("styles-element-classes-container"); |
| 19 this._prompt = new WebInspector.ClassesPaneWidget.ClassNamePrompt(); | |
| 20 this._prompt.setAutocompletionTimeout(0); | |
| 21 this._prompt.renderAsBlock(); | |
| 22 | |
| 23 var proxyElement = this._prompt.attach(this._input); | |
| 24 proxyElement.addEventListener("keydown", this._onKeyDown.bind(this), false); | |
| 20 | 25 |
| 21 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.DOMMutated, this._onDOMMutated, this); | 26 WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspec tor.DOMModel.Events.DOMMutated, this._onDOMMutated, this); |
| 22 /** @type {!Set<!WebInspector.DOMNode>} */ | 27 /** @type {!Set<!WebInspector.DOMNode>} */ |
| 23 this._mutatingNodes = new Set(); | 28 this._mutatingNodes = new Set(); |
| 24 WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._upd ate, this); | 29 WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._upd ate, this); |
| 25 } | 30 } |
| 26 | 31 |
| 27 WebInspector.ClassesPaneWidget._classesSymbol = Symbol("WebInspector.ClassesPane Widget._classesSymbol"); | 32 WebInspector.ClassesPaneWidget._classesSymbol = Symbol("WebInspector.ClassesPane Widget._classesSymbol"); |
| 28 | 33 |
| 29 WebInspector.ClassesPaneWidget.prototype = { | 34 WebInspector.ClassesPaneWidget.prototype = { |
| 30 /** | 35 /** |
| 31 * @param {!Event} event | 36 * @param {!Event} event |
| 32 */ | 37 */ |
| 33 _onKeyDown: function(event) | 38 _onKeyDown: function(event) |
| 34 { | 39 { |
| 35 var text = event.target.value; | 40 var text = event.target.textContent; |
| 36 if (isEscKey(event)) { | 41 if (isEscKey(event)) { |
| 37 event.target.value = ""; | 42 event.target.textContent = ""; |
| 38 if (!text.isWhitespace()) | 43 if (!text.isWhitespace()) |
| 39 event.consume(true); | 44 event.consume(true); |
| 40 return; | 45 return; |
| 41 } | 46 } |
| 42 | 47 |
| 43 if (!isEnterKey(event)) | 48 if (!isEnterKey(event)) |
| 44 return; | 49 return; |
| 45 var node = WebInspector.context.flavor(WebInspector.DOMNode); | 50 var node = WebInspector.context.flavor(WebInspector.DOMNode); |
| 46 if (!node) | 51 if (!node) |
| 47 return; | 52 return; |
| 48 | 53 |
| 49 event.target.value = ""; | 54 this._prompt.clearAutocomplete(); |
| 55 event.target.textContent = ""; | |
| 50 var classNames = text.split(/[.,\s]/); | 56 var classNames = text.split(/[.,\s]/); |
| 51 for (var className of classNames) { | 57 for (var className of classNames) { |
| 52 var className = className.trim(); | 58 var className = className.trim(); |
| 53 if (!className.length) | 59 if (!className.length) |
| 54 continue; | 60 continue; |
| 55 this._toggleClass(node, className, true); | 61 this._toggleClass(node, className, true); |
| 56 } | 62 } |
| 57 this._installNodeClasses(node); | 63 this._installNodeClasses(node); |
| 58 this._update(); | 64 this._update(); |
| 59 event.consume(true); | 65 event.consume(true); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 | 210 |
| 205 /** | 211 /** |
| 206 * @override | 212 * @override |
| 207 * @return {!WebInspector.ToolbarItem} | 213 * @return {!WebInspector.ToolbarItem} |
| 208 */ | 214 */ |
| 209 item: function() | 215 item: function() |
| 210 { | 216 { |
| 211 return this._button; | 217 return this._button; |
| 212 } | 218 } |
| 213 } | 219 } |
| 220 | |
| 221 /** | |
| 222 * @constructor | |
| 223 * @extends {WebInspector.TextPrompt} | |
| 224 */ | |
| 225 WebInspector.ClassesPaneWidget.ClassNamePrompt = function() | |
| 226 { | |
| 227 WebInspector.TextPrompt.call(this, this._buildClassNameCompletions.bind(this ), " "); | |
| 228 this.setSuggestBoxEnabled(true); | |
| 229 this.disableDefaultSuggestionForEmptyInput(); | |
| 230 this._lastQuery = ""; | |
| 231 this._selectedFrameId = ""; | |
| 232 this._completions = new Set(); | |
| 233 } | |
| 234 | |
| 235 WebInspector.ClassesPaneWidget.ClassNamePrompt.prototype = { | |
| 236 /** | |
| 237 * @param {!Element} proxyElement | |
| 238 * @param {!Range} wordRange | |
| 239 * @param {boolean} force | |
| 240 * @param {function(!Array.<string>, number=)} completionsReadyCallback | |
| 241 */ | |
| 242 _buildClassNameCompletions: function(proxyElement, wordRange, force, complet ionsReadyCallback) | |
| 243 { | |
| 244 var prefix = wordRange.toString(); | |
| 245 var selectedNode = WebInspector.context.flavor(WebInspector.DOMNode); | |
| 246 if (!selectedNode || (!prefix && !force && !proxyElement.textContent.len gth)) { | |
| 247 completionsReadyCallback([]); | |
| 248 return; | |
| 249 } | |
| 250 | |
| 251 var promises = []; | |
| 252 if (this._selectedFrameId !== selectedNode.frameId() || !prefix.startsWi th(this._lastQuery)) { | |
| 253 this._completions = new Set(); | |
|
lushnikov
2016/09/29 16:03:15
We usually employ promises to cache values. One gr
ahmetemirercin
2016/09/29 19:25:36
I just see that promises are used like this after
| |
| 254 this._selectedFrameId = selectedNode.frameId() || WebInspector.Resou rceTreeModel.fromTarget(selectedNode.target()).mainFrame.id; | |
| 255 | |
| 256 var cssModel = WebInspector.CSSModel.fromTarget(selectedNode.target( )); | |
| 257 var allStyleSheets = cssModel.allStyleSheets(); | |
| 258 for (var stylesheet of allStyleSheets) { | |
| 259 if (stylesheet.frameId !== this._selectedFrameId) | |
| 260 continue; | |
| 261 var cssPromise = cssModel.classNamesPromise(stylesheet.id).then( classes => this._completions.addAll(classes)); | |
| 262 promises.push(cssPromise); | |
| 263 } | |
| 264 | |
| 265 var domPromise = selectedNode.domModel().classNamesPromise(selectedN ode.ownerDocument.id).then(classes => this._completions.addAll(classes)); | |
| 266 promises.push(domPromise); | |
| 267 } | |
| 268 this._lastQuery = prefix; | |
| 269 | |
| 270 Promise.all(promises).then(() => { | |
| 271 var results = []; | |
| 272 this._completions.valuesArray().forEach((value) => { | |
| 273 if (prefix.substring(0, 1) === ".") | |
| 274 value = "." + value; | |
|
lushnikov
2016/09/29 16:03:15
i agree with this
| |
| 275 if (value.startsWith(prefix)) | |
| 276 results.push(value); | |
| 277 }); | |
| 278 completionsReadyCallback(results, 0); | |
| 279 }); | |
| 280 }, | |
| 281 | |
| 282 __proto__: WebInspector.TextPrompt.prototype | |
| 283 } | |
| OLD | NEW |