Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Unified Diff: third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js

Issue 2842843003: DevTools: Display product information in ConsoleContextSelector (Closed)
Patch Set: cleanup Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js
diff --git a/third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js b/third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js
index 127d7cee01d324c80b26e766a40696f8690f824c..653a3a02cd0b1a9bd285aeca5be5f5146e584814 100644
--- a/third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js
+++ b/third_party/WebKit/Source/devtools/front_end/console/ConsoleContextSelector.js
@@ -3,14 +3,69 @@
// found in the LICENSE file.
/**
* @implements {SDK.SDKModelObserver<!SDK.RuntimeModel>}
+ * @implements {UI.ListDelegate<!SDK.ExecutionContext>}
* @unrestricted
*/
-Console.ConsoleContextSelector = class {
- /**
- * @param {!Element} selectElement
- */
- constructor(selectElement) {
- this._selectElement = selectElement;
+Console.ConsoleContextSelector = class extends UI.ToolbarItem {
+ constructor() {
+ super(createElementWithClass('button', 'console-context'));
+ this._titleElement = this.element.createChild('span');
+ this._titleElement.textContent = '';
+ this._titleElement.style.paddingRight = '5px';
+ this._titleElement.style.width = '120px';
+ this._titleElement.style.overflow = 'hidden';
+ this._titleElement.style.textOverflow = 'ellipsis ';
+ this._productRegistry = null;
+
+ ProductRegistry.instance().then(registry => {
+ this._productRegistry = registry;
+ this._list.refreshAllItems();
+ });
+ this.element.classList.add('toolbar-has-dropdown');
+ this.element.tabIndex = 0;
+ this._glassPane = new UI.GlassPane();
+ this._glassPane.setMaxContentSize(new UI.Size(315, 310));
+ this._glassPane.setMarginBehavior(UI.GlassPane.MarginBehavior.NoMargin);
+ this._glassPane.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
+ this._glassPane.setAnchorBehavior(UI.GlassPane.AnchorBehavior.PreferBottom);
+ this._glassPane.setOutsideClickCallback(this._hide.bind(this));
+ /** @type {!Array<!SDK.ExecutionContext>} */
+ this._executionContexts = [];
+ this._list = new UI.ListControl(this, UI.ListMode.NonViewport);
+ this._list.element.classList.add('context-list');
+ this._list.element.tabIndex = -1;
+ var shadowRoot =
+ UI.createShadowRootWithCoreStyles(this._glassPane.contentElement, 'console/consoleContextSelector.css');
+ shadowRoot.appendChild(this._list.element);
+
+ this._canSelect = false;
+ this.element.addEventListener('mousedown', event => {
+ if (this._canSelect)
+ this._hide(event);
+ else
+ this._show(event);
+ }, false);
+ this.element.addEventListener('click', event => {
+ if (!this._canSelect)
+ this._show(event);
+ }, false);
+ this.element.addEventListener('keydown', this._onKeyDown.bind(this), false);
+ this.element.addEventListener('blur', this._hide.bind(this), false);
+ this._list.element.addEventListener('mousedown', event => {
+ this._hide(event);
+ this._updateSelectedContext();
+ }, false);
+ this._list.element.addEventListener('mouseup', event => {
+ if (!this._canSelect)
+ return;
+ this._hide(event);
+ this._updateSelectedContext();
+ }, false);
+
+ var dropdownArrowIcon = UI.Icon.create('smallicon-triangle-down', 'toolbar-dropdown-arrow');
+ this.element.appendChild(dropdownArrowIcon);
+
+
/**
* @type {!Map.<!SDK.ExecutionContext, !Element>}
*/
@@ -22,8 +77,9 @@ Console.ConsoleContextSelector = class {
SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this._onExecutionContextChanged, this);
SDK.targetManager.addModelListener(
SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, this._onExecutionContextDestroyed, this);
+ SDK.targetManager.addModelListener(
+ SDK.ResourceTreeModel, SDK.ResourceTreeModel.Events.FrameNavigated, this._frameNavigated, this);
- this._selectElement.addEventListener('change', this._executionContextChanged.bind(this), false);
UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionContextChangedExternally, this);
UI.context.addFlavorChangeListener(SDK.DebuggerModel.CallFrame, this._callFrameSelectedInUI, this);
SDK.targetManager.observeModels(SDK.RuntimeModel, this);
@@ -32,27 +88,139 @@ Console.ConsoleContextSelector = class {
}
/**
+ * @param {!Event} event
+ */
+ _show(event) {
+ if (this._glassPane.isShowing())
+ return;
+ this._glassPane.setContentAnchorBox(this.element.boxInWindow());
+ if (this.element.ownerDocument)
+ this._glassPane.show(this.element.ownerDocument);
+ this._list.viewportResized();
+ var selectedItem = this._list.selectedItem();
+ if (selectedItem)
+ this._list.scrollItemIntoView(selectedItem, true);
+ this.element.focus();
+ event.consume(true);
+ setTimeout(() => this._canSelect = true, 200);
+ }
+
+ /**
+ * @param {!Event} event
+ */
+ _hide(event) {
+ setTimeout(() => this._canSelect = false, 200);
+ this._glassPane.hide();
+ SDK.OverlayModel.hideDOMNodeHighlight();
+ event.consume(true);
+ }
+
+ /**
+ * @param {!Event} event
+ */
+ _onKeyDown(event) {
+ var handled = false;
+ switch (event.key) {
+ case 'ArrowUp':
+ handled = this._list.selectPreviousItem(false, false);
+ break;
+ case 'ArrowDown':
+ handled = this._list.selectNextItem(false, false);
+ break;
+ case 'ArrowRight':
+ var currentExecutionContext = this._list.selectedItem();
+ if (!currentExecutionContext)
+ break;
+ var nextExecutionContext =
+ this._executionContexts[this._executionContexts.indexOf(currentExecutionContext) + 1];
+ if (nextExecutionContext && this._depthFor(currentExecutionContext) < this._depthFor(nextExecutionContext))
+ handled = this._list.selectNextItem(false, false);
+ break;
+ case 'ArrowLeft':
+ var currentExecutionContext = this._list.selectedItem();
+ if (!currentExecutionContext)
+ break;
+ var depth = this._depthFor(currentExecutionContext);
+ for (var i = this._executionContexts.indexOf(currentExecutionContext) - 1; i >= 0; i--) {
+ if (this._depthFor(this._executionContexts[i]) < depth) {
+ handled = true;
+ this._list.selectItem(this._executionContexts[i], false);
+ break;
+ }
+ }
+ break;
+ case 'PageUp':
+ handled = this._list.selectItemPreviousPage(false);
+ break;
+ case 'PageDown':
+ handled = this._list.selectItemNextPage(false);
+ break;
+ case 'Escape':
+ if (this._glassPane.isShowing())
+ event.consume(true);
+ this._glassPane.hide();
+ return;
+ case 'Enter':
+ handled = this._glassPane.isShowing();
+ this._glassPane.hide();
+ break;
+ default:
+ if (event.key.length === 1) {
+ var selectedItem = this._list.selectedItem();
+ var selectedIndex = selectedItem ? this._executionContexts.indexOf(selectedItem) : -1;
+ var context =
+ this._executionContexts.slice(selectedIndex + 1)
+ .find(context => this._titleFor(context).toUpperCase().startsWith(event.key.toUpperCase())) ||
+ this._executionContexts.slice(0, selectedIndex)
+ .find(context => this._titleFor(context).toUpperCase().startsWith(event.key.toUpperCase()));
+ if (context)
+ this._list.selectItem(context);
+ handled = true;
+ }
+ break;
+ }
+
+ if (handled) {
+ event.consume(true);
+ this._updateSelectedContext();
+ }
+ }
+
+ /**
* @param {!SDK.ExecutionContext} executionContext
* @return {string}
*/
_titleFor(executionContext) {
var target = executionContext.target();
- var depth = 0;
var label = executionContext.label() ? target.decorateLabel(executionContext.label()) : '';
+ if (executionContext.frameId) {
+ var resourceTreeModel = target.model(SDK.ResourceTreeModel);
+ var frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId);
+ if (frame)
+ label = label || frame.displayName();
+ }
+ label = label || executionContext.origin;
+
+ return label;
+ }
+
+ /**
+ * @param {!SDK.ExecutionContext} executionContext
+ * @return {number}
+ */
+ _depthFor(executionContext) {
+ var target = executionContext.target();
+ var depth = 0;
if (!executionContext.isDefault)
depth++;
if (executionContext.frameId) {
var resourceTreeModel = target.model(SDK.ResourceTreeModel);
var frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId);
- if (frame) {
- label = label || frame.displayName();
- while (frame.parentFrame) {
- depth++;
- frame = frame.parentFrame;
- }
+ while (frame && frame.parentFrame) {
+ depth++;
+ frame = frame.parentFrame;
}
}
- label = label || executionContext.origin;
var targetDepth = 0;
while (target.parentTarget()) {
if (target.parentTarget().hasJSCapability()) {
@@ -64,11 +232,8 @@ Console.ConsoleContextSelector = class {
}
target = target.parentTarget();
}
-
depth += targetDepth;
- var prefix = new Array(4 * depth + 1).join('\u00a0');
- var maxLength = 50;
- return (prefix + label).trimMiddle(maxLength);
+ return depth;
}
/**
@@ -80,25 +245,14 @@ Console.ConsoleContextSelector = class {
if (!executionContext.target().hasJSCapability())
return;
- var newOption = createElement('option');
- newOption.__executionContext = executionContext;
- newOption.text = this._titleFor(executionContext);
- this._optionByExecutionContext.set(executionContext, newOption);
- var options = this._selectElement.options;
- var contexts = Array.prototype.map.call(options, mapping);
- var index = contexts.lowerBound(executionContext, executionContext.runtimeModel.executionContextComparator());
- this._selectElement.insertBefore(newOption, options[index]);
-
- if (executionContext === UI.context.flavor(SDK.ExecutionContext))
- this._select(newOption);
- this._updateOptionDisabledState(newOption);
+ var index = this._executionContexts.lowerBound(
+ executionContext, executionContext.runtimeModel.executionContextComparator());
+ this._executionContexts.splice(index, 0, executionContext);
+ this._list.insertItemAtIndex(index, executionContext);
- /**
- * @param {!Element} option
- * @return {!SDK.ExecutionContext}
- */
- function mapping(option) {
- return option.__executionContext;
+ if (executionContext === UI.context.flavor(SDK.ExecutionContext)) {
+ this._list.selectItem(executionContext);
+ this._updateSelectedContext();
}
}
@@ -116,9 +270,10 @@ Console.ConsoleContextSelector = class {
*/
_onExecutionContextChanged(event) {
var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data);
- var option = this._optionByExecutionContext.get(executionContext);
- if (option)
- option.text = this._titleFor(executionContext);
+ if (this._executionContexts.indexOf(executionContext) === -1)
+ return;
+ this._executionContextDestroyed(executionContext);
+ this._executionContextCreated(executionContext);
this._updateSelectionWarning();
}
@@ -126,8 +281,8 @@ Console.ConsoleContextSelector = class {
* @param {!SDK.ExecutionContext} executionContext
*/
_executionContextDestroyed(executionContext) {
- var option = this._optionByExecutionContext.remove(executionContext);
- option.remove();
+ if (this._executionContexts.remove(executionContext, true))
+ this._list.removeItem(executionContext);
}
/**
@@ -144,27 +299,15 @@ Console.ConsoleContextSelector = class {
*/
_executionContextChangedExternally(event) {
var executionContext = /** @type {?SDK.ExecutionContext} */ (event.data);
- if (!executionContext)
+ if (!executionContext || this._executionContexts.indexOf(executionContext) === -1)
return;
-
- var options = this._selectElement.options;
- for (var i = 0; i < options.length; ++i) {
- if (options[i].__executionContext === executionContext)
- this._select(options[i]);
- }
- }
-
- _executionContextChanged() {
- var option = this._selectedOption();
- var newContext = option ? option.__executionContext : null;
- UI.context.setFlavor(SDK.ExecutionContext, newContext);
- this._updateSelectionWarning();
+ this._list.selectItem(executionContext);
+ this._updateSelectedContext();
}
_updateSelectionWarning() {
var executionContext = UI.context.flavor(SDK.ExecutionContext);
- this._selectElement.parentElement.classList.toggle(
- 'warning', !this._isTopContext(executionContext) && this._hasTopContext());
+ this.element.classList.toggle('warning', !this._isTopContext(executionContext) && this._hasTopContext());
}
/**
@@ -185,12 +328,7 @@ Console.ConsoleContextSelector = class {
* @return {boolean}
*/
_hasTopContext() {
- var options = this._selectElement.options;
- for (var i = 0; i < options.length; i++) {
- if (this._isTopContext(options[i].__executionContext))
- return true;
- }
- return false;
+ return !!this._executionContexts.find(executionContext => this._isTopContext(executionContext));
}
/**
@@ -214,42 +352,121 @@ Console.ConsoleContextSelector = class {
}
/**
- * @param {!Element} option
+ * @param {!SDK.ExecutionContext} executionContext
*/
- _select(option) {
- this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);
- this._updateSelectionWarning();
+ _onSelect(executionContext) {
}
/**
- * @return {?Element}
+ * @override
+ * @param {!SDK.ExecutionContext} item
+ * @return {!Element}
*/
- _selectedOption() {
- if (this._selectElement.selectedIndex >= 0)
- return this._selectElement[this._selectElement.selectedIndex];
- return null;
+ createElementForItem(item) {
+ var element = createElementWithClass('div', 'context');
+ element.style.paddingLeft = (8 + this._depthFor(item) * 15) + 'px';
+ element.createChild('div', 'title').textContent = this._titleFor(item);
+ element.createChild('div', 'subtitle').textContent = this._subtitleFor(item);
+ element.addEventListener('mousemove', e => {
+ if (e.movementX || e.movementY)
+ this._list.selectItem(item, false, /* Don't scroll */ true);
+ });
+ element.classList.toggle('disabled', !this.isItemSelectable(item));
+ return element;
}
/**
- * @param {!Common.Event} event
+ * @param {!SDK.ExecutionContext} executionContext
+ * @return {string}
*/
- _callFrameSelectedInModel(event) {
- var debuggerModel = /** @type {!SDK.DebuggerModel} */ (event.data);
- var options = this._selectElement.options;
- for (var i = 0; i < options.length; i++) {
- if (options[i].__executionContext.debuggerModel === debuggerModel)
- this._updateOptionDisabledState(options[i]);
+ _subtitleFor(executionContext) {
+ var target = executionContext.target();
+ if (executionContext.frameId) {
+ var resourceTreeModel = target.model(SDK.ResourceTreeModel);
+ var frame = resourceTreeModel && resourceTreeModel.frameForId(executionContext.frameId);
+ }
+ if (executionContext.origin.startsWith('chrome-extension://'))
+ return Common.UIString('Extension');
+ if (!frame || !frame.parentFrame || frame.parentFrame.securityOrigin !== executionContext.origin) {
+ var url = executionContext.origin.asParsedURL();
+ if (url) {
+ if (this._productRegistry) {
+ var product = this._productRegistry.nameForUrl(url);
+ if (product)
+ return product;
+ }
+ return url.domain();
+ }
}
+
+ if (frame) {
+ var stackTrace = frame.creationStackTrace();
+ while (stackTrace) {
+ for (var stack of stackTrace.callFrames) {
+ if (stack.url) {
+ var url = new Common.ParsedURL(stack.url);
+ if (this._productRegistry) {
+ var product = this._productRegistry.nameForUrl(url);
+ if (product)
+ return product;
+ }
+ return url.domain();
+ }
+ }
+ stackTrace = frame.parent;
+ }
+ }
+ return '';
}
/**
- * @param {!Element} option
+ * @override
+ * @param {!SDK.ExecutionContext} item
+ * @return {number}
*/
- _updateOptionDisabledState(option) {
- var executionContext = option.__executionContext;
- var callFrame = executionContext.debuggerModel.selectedCallFrame();
+ heightForItem(item) {
+ return 20;
+ }
+
+ /**
+ * @override
+ * @param {!SDK.ExecutionContext} item
+ * @return {boolean}
+ */
+ isItemSelectable(item) {
+ var callFrame = item.debuggerModel.selectedCallFrame();
var callFrameContext = callFrame && callFrame.script.executionContext();
- option.disabled = callFrameContext && executionContext !== callFrameContext;
+ return !callFrameContext || item === callFrameContext;
+ }
+
+ /**
+ * @override
+ * @param {?SDK.ExecutionContext} from
+ * @param {?SDK.ExecutionContext} to
+ * @param {?Element} fromElement
+ * @param {?Element} toElement
+ */
+ selectedItemChanged(from, to, fromElement, toElement) {
+ if (fromElement)
+ fromElement.classList.remove('selected');
+ if (toElement)
+ toElement.classList.add('selected');
+ SDK.OverlayModel.hideDOMNodeHighlight();
+ if (to && to.frameId) {
+ var resourceTreeModel = to.target().model(SDK.ResourceTreeModel);
+ if (resourceTreeModel)
+ resourceTreeModel.domModel().overlayModel().highlightFrame(to.frameId);
+ }
+ }
+
+ _updateSelectedContext() {
+ var context = this._list.selectedItem();
+ if (context)
+ this._titleElement.textContent = this._titleFor(context);
+ else
+ this._titleElement.textContent = '';
+ UI.context.setFlavor(SDK.ExecutionContext, context);
+ this._updateSelectionWarning();
}
_callFrameSelectedInUI() {
@@ -258,4 +475,26 @@ Console.ConsoleContextSelector = class {
if (callFrameContext)
UI.context.setFlavor(SDK.ExecutionContext, callFrameContext);
}
+
+ /**
+ * @param {!Common.Event} event
+ */
+ _callFrameSelectedInModel(event) {
+ var debuggerModel = /** @type {!SDK.DebuggerModel} */ (event.data);
+ for (var i = 0; i < this._executionContexts.length; i++) {
+ if (this._executionContexts[i].debuggerModel === debuggerModel)
+ this._list.refreshItemsInRange(i, i + 1);
+ }
+ }
+
+ /**
+ * @param {!Common.Event} event
+ */
+ _frameNavigated(event) {
+ var frameId = event.data.id;
+ for (var i = 0; i < this._executionContexts.length; i++) {
+ if (frameId === this._executionContexts[i].frameId)
+ this._list.refreshItemsInRange(i, i + 1);
+ }
+ }
};

Powered by Google App Engine
This is Rietveld 408576698