Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js b/third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js |
| index b0bada022b10ab39a9c8379afb97d078f1c7817a..cac4162053d9ba7584272d8b88ac4dd93c2d311f 100644 |
| --- a/third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js |
| +++ b/third_party/WebKit/Source/devtools/front_end/elements/ClassesPaneWidget.js |
| @@ -11,14 +11,29 @@ WebInspector.ClassesPaneWidget = function() |
| WebInspector.Widget.call(this); |
| this.element.className = "styles-element-classes-pane"; |
| var container = this.element.createChild("div", "title-container"); |
| - this._input = container.createChild("input", "new-class-input monospace"); |
| - this._input.placeholder = WebInspector.UIString("Add new class"); |
| - this._input.addEventListener("keydown", this._onKeyDown.bind(this), false); |
| + this._input = container.createChild("div", "new-class-input monospace"); |
| + this._input.setAttribute("placeholder" , WebInspector.UIString("Add new class")); |
| this.setDefaultFocusedElement(this._input); |
| + |
| this._classesContainer = this.element.createChild("div", "source-code"); |
| this._classesContainer.classList.add("styles-element-classes-container"); |
| + this._prompt = new WebInspector.ClassesPaneWidget.ClassNamePrompt(); |
| + this._prompt.setAutocompletionTimeout(0); |
| + this._prompt.renderAsBlock(); |
| + |
| + this._frameClasses = new Map(); |
| + this._selectedNode = null; |
| + this._requestedFrames = new Set(); |
| + var proxyElement = this._prompt.attach(this._input); |
| + proxyElement.addEventListener("keydown", this._onKeyDown.bind(this), false); |
| WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspector.DOMModel.Events.DOMMutated, this._onDOMMutated, this); |
| + WebInspector.targetManager.addModelListener(WebInspector.CSSModel, WebInspector.CSSModel.Events.StyleSheetAdded, this._styleSheetAdded, this); |
| + WebInspector.targetManager.addModelListener(WebInspector.CSSModel, WebInspector.CSSModel.Events.StyleSheetChanged, this._styleSheetChanged, this); |
| + WebInspector.targetManager.addModelListener(WebInspector.CSSModel, WebInspector.CSSModel.Events.StyleSheetRemoved, this._styleSheetRemoved, this); |
| + WebInspector.targetManager.addModelListener(WebInspector.DOMModel, WebInspector.DOMModel.Events.DocumentUpdated, this._documentUpdated, this); |
| + WebInspector.targetManager.addEventListener(WebInspector.TargetManager.Events.MainFrameNavigated, this._mainFrameNavigated, this); |
| + |
| /** @type {!Set<!WebInspector.DOMNode>} */ |
| this._mutatingNodes = new Set(); |
| WebInspector.context.addFlavorChangeListener(WebInspector.DOMNode, this._update, this); |
| @@ -32,9 +47,10 @@ WebInspector.ClassesPaneWidget.prototype = { |
| */ |
| _onKeyDown: function(event) |
| { |
| - var text = event.target.value; |
| + var text = event.target.innerText; |
| if (isEscKey(event)) { |
| - event.target.value = ""; |
| + this._hidePrompt(); |
| + event.target.innerText = ""; |
| if (!text.isWhitespace()) |
| event.consume(true); |
| return; |
| @@ -45,8 +61,8 @@ WebInspector.ClassesPaneWidget.prototype = { |
| var node = WebInspector.context.flavor(WebInspector.DOMNode); |
| if (!node) |
| return; |
| - |
| - event.target.value = ""; |
| + this._hidePrompt(); |
| + event.target.innerText = ""; |
| var classNames = text.split(/[.,\s]/); |
| for (var className of classNames) { |
| var className = className.trim(); |
| @@ -62,6 +78,182 @@ WebInspector.ClassesPaneWidget.prototype = { |
| /** |
| * @param {!WebInspector.Event} event |
| */ |
| + _documentUpdated: function(event) |
|
lushnikov
2016/09/16 16:40:01
let's pull the classnames whenever the text prompt
ahmetemirercin
2016/09/16 16:51:14
But what if these events occurs when the text prom
lushnikov
2016/09/16 17:09:17
If the completions change while the suggest box is
ahmetemirercin
2016/09/16 19:23:00
Done.
|
| + { |
| + if (event.data && this.isShowing()) { |
| + var updatedDocument = /** @type {!WebInspector.DOMDocument} */ (event.data); |
| + this._getDomClasses(updatedDocument); |
| + } |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _styleSheetAdded: function(event) |
| + { |
| + var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data); |
| + this._getStylesheetClasses(header); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _styleSheetChanged: function(event) |
| + { |
| + var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data); |
| + |
| + this._collectFrameClasses(header.frameId); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _styleSheetRemoved: function(event) |
| + { |
| + var header = /** @type {!WebInspector.CSSStyleSheetHeader} */ (event.data); |
| + this._collectFrameClasses(header.frameId); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| + _mainFrameNavigated: function(event) |
| + { |
| + this._selectedNode = null; |
| + this._requestedFrames = new Set(); |
| + this._frameClasses = new Map(); |
| + this._prompt.clearCompletions(); |
| + }, |
| + |
| + _hidePrompt: function() |
| + { |
| + this._prompt.clearAutoComplete(); |
| + }, |
| + |
| + /** |
| + * @param {?WebInspector.DOMDocument=} inspectedDocument |
| + */ |
| + _getDomClasses: function(inspectedDocument) |
| + { |
| + if (!inspectedDocument && this._selectedNode) |
| + inspectedDocument = this._selectedNode.ownerDocument; |
| + |
| + if (!inspectedDocument) |
| + return; |
| + |
| + var frameId = inspectedDocument.frameId() || WebInspector.ResourceTreeModel.fromTarget(inspectedDocument.target()).mainFrame.id; |
| + /** |
| + * @param {!Array.<string>} classNames |
| + * @this {!WebInspector.ClassesPaneWidget} |
| + */ |
| + function classNamesCallback(classNames) |
| + { |
| + this._addFrameClasses(frameId, classNames); |
| + } |
| + |
| + inspectedDocument.domModel().classNamesPromise(inspectedDocument.id).then(classNamesCallback.bind(this)); |
| + }, |
| + |
| + |
| + _getCssClasses: function() |
| + { |
| + if (!this._selectedNode) |
| + return; |
| + |
| + var frameId = this._selectedNode.frameId(); |
| + var cssModel = WebInspector.CSSModel.fromTarget(this._selectedNode.target()); |
| + var headers = cssModel.allStyleSheets(); |
| + for (var stylesheet of cssModel.allStyleSheets()) |
| + if (stylesheet.frameId === frameId) |
| + this._getStylesheetClasses(stylesheet); |
| + }, |
| + |
| + /** |
| + * @param {?PageAgent.FrameId} frameId |
| + * @return {!Set<string>} |
| + */ |
| + _getFrameClasses: function(frameId) |
| + { |
| + if (!this._frameClasses.get(frameId)) |
| + this._frameClasses.set(frameId, new Set()); |
| + |
| + return this._frameClasses.get(frameId); |
| + }, |
| + |
| + _selectedFrameChanged: function() |
| + { |
| + var newFrameId = this._selectedFrameId(); |
| + this._prompt.sourceFrameId = newFrameId; |
| + this._updateCompletions(newFrameId); |
| + |
| + if (this.isShowing() && !this._requestedFrames.has(newFrameId)) |
| + this._collectFrameClasses(newFrameId); |
| + }, |
| + |
| + /** |
| + * @return {?PageAgent.FrameId} |
| + */ |
| + _selectedFrameId: function() |
| + { |
| + return this._selectedNode ? this._selectedNode.frameId() || WebInspector.ResourceTreeModel.fromTarget(this._selectedNode.target()).mainFrame.id : 0; |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.CSSStyleSheetHeader} stylesheet |
| + */ |
| + _getStylesheetClasses: function(stylesheet) |
| + { |
| + if (!this.isShowing()) |
| + return; |
| + |
| + /** |
| + * @param {!WebInspector.CSSStyleSheetHeader} stylsheet |
| + * @param {!Array.<string>} classNames |
| + * @this {!WebInspector.ClassesPaneWidget} |
| + */ |
| + function classNamesCallback(stylsheet, classNames) |
| + { |
| + this._addFrameClasses(stylsheet.frameId, classNames); |
| + } |
| + |
| + var cssModel = stylesheet.cssModel(); |
| + cssModel.classNamesPromise(stylesheet.id).then(classNamesCallback.bind(this, stylesheet)); |
| + }, |
| + |
| + /** |
| + * @param {?PageAgent.FrameId} frameId |
| + * @param {!Array.<string>} classNames |
| + */ |
| + _addFrameClasses: function(frameId, classNames) |
| + { |
| + if (!classNames) |
| + return; |
| + this._getFrameClasses(frameId).addAll(classNames); |
| + this._updateCompletions(frameId); |
| + }, |
| + |
| + /** |
| + * @param {?PageAgent.FrameId} frameId |
| + */ |
| + _updateCompletions: function(frameId) |
| + { |
| + if (frameId === this._prompt.sourceFrameId) |
| + this._prompt.updateCompletions(this._getFrameClasses(frameId).valuesArray()); |
| + }, |
| + |
| + /** |
| + * @param {?PageAgent.FrameId} frameId |
| + */ |
| + _collectFrameClasses: function(frameId) |
| + { |
| + this._requestedFrames.add(frameId); |
| + this._getDomClasses(); |
| + this._getCssClasses(); |
| + }, |
| + |
| + /** |
| + * @param {!WebInspector.Event} event |
| + */ |
| _onDOMMutated: function(event) |
| { |
| var node = /** @type {!WebInspector.DOMNode} */(event.data); |
| @@ -83,7 +275,6 @@ WebInspector.ClassesPaneWidget.prototype = { |
| { |
| if (!this.isShowing()) |
| return; |
| - |
| var node = WebInspector.context.flavor(WebInspector.DOMNode); |
| if (node) |
| node = node.enclosingElementOrSelf(); |
| @@ -94,6 +285,10 @@ WebInspector.ClassesPaneWidget.prototype = { |
| if (!node) |
| return; |
| + var oldFrameId = this._selectedFrameId(); |
| + this._selectedNode = node; |
| + if (this._selectedFrameId() !== oldFrameId) |
| + this._selectedFrameChanged(); |
| var classes = this._nodeClasses(node); |
| var keys = classes.keysArray(); |
| keys.sort(String.caseInsensetiveComparator); |
| @@ -211,3 +406,72 @@ WebInspector.ClassesPaneWidget.ButtonProvider.prototype = { |
| return this._button; |
| } |
| } |
| + |
| +/** |
| + * @constructor |
| + * @extends {WebInspector.TextPrompt} |
| + */ |
| +WebInspector.ClassesPaneWidget.ClassNamePrompt = function() |
| +{ |
| + WebInspector.TextPrompt.call(this, this._buildClassNameCompletions.bind(this), " "); |
| + this.setSuggestBoxEnabled(true); |
| + this.disableDefaultSuggestionForEmptyInput(); |
| + this._classNameCompletions = []; |
| + this.sourceFrameId = 0; |
| +} |
| + |
| +WebInspector.ClassesPaneWidget.ClassNamePrompt.prototype = { |
| + /** |
| + * @override |
| + * @param {!Event} event |
| + */ |
| + onKeyDown: function(event) |
| + { |
| + switch (event.key) { |
| + case "Enter": |
| + // Accept any available autocompletions and advance to the next field. |
| + if (this.autoCompleteElement && this.autoCompleteElement.textContent.length) { |
| + this.acceptAutoComplete(); |
| + return; |
| + } |
| + break; |
| + } |
| + |
| + WebInspector.TextPrompt.prototype.onKeyDown.call(this, event); |
| + }, |
| + |
| + /** |
| + * @param {!Array.<string>} classNameList |
| + */ |
| + updateCompletions: function(classNameList) |
| + { |
| + this._classNameCompletions = classNameList; |
| + }, |
| + |
| + clearCompletions: function() |
| + { |
| + this._classNameCompletions = []; |
| + }, |
| + |
| + /** |
| + * @param {!Element} proxyElement |
| + * @param {!Range} wordRange |
| + * @param {boolean} force |
| + * @param {function(!Array.<string>, number=)} completionsReadyCallback |
| + */ |
| + _buildClassNameCompletions: function(proxyElement, wordRange, force, completionsReadyCallback) |
| + { |
| + var prefix = wordRange.toString(); |
| + |
| + if (!prefix && !force && !proxyElement.textContent.length) { |
| + completionsReadyCallback([]); |
| + return; |
| + } |
| + |
| + var results = this._classNameCompletions.filter((value) => value.startsWith(prefix)); |
| + var selectedIndex = 0; |
| + completionsReadyCallback(results, selectedIndex); |
| + }, |
| + |
| + __proto__: WebInspector.TextPrompt.prototype |
| +} |