ConsoleContextSelector.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export default class ConsoleContextSelector{constructor(){this._items=new UI.ListModel();this._dropDown=new UI.SoftDropDown(this._items,this);this._dropDown.setRowHeight(36);this._toolbarItem=new UI.ToolbarItem(this._dropDown.element);this._toolbarItem.setEnabled(false);this._toolbarItem.setTitle(ls`JavaScript context: Not selected`);this._items.addEventListener(UI.ListModel.Events.ItemsReplaced,()=>this._toolbarItem.setEnabled(!!this._items.length));this._toolbarItem.element.classList.add('toolbar-has-dropdown');SDK.targetManager.addModelListener(SDK.RuntimeModel,SDK.RuntimeModel.Events.ExecutionContextCreated,this._onExecutionContextCreated,this);SDK.targetManager.addModelListener(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);UI.context.addFlavorChangeListener(SDK.ExecutionContext,this._executionContextChangedExternally,this);UI.context.addFlavorChangeListener(SDK.DebuggerModel.CallFrame,this._callFrameSelectedInUI,this);SDK.targetManager.observeModels(SDK.RuntimeModel,this);SDK.targetManager.addModelListener(SDK.DebuggerModel,SDK.DebuggerModel.Events.CallFrameSelected,this._callFrameSelectedInModel,this);}
  2. toolbarItem(){return this._toolbarItem;}
  3. highlightedItemChanged(from,to,fromElement,toElement){SDK.OverlayModel.hideDOMNodeHighlight();if(to&&to.frameId){const overlayModel=to.target().model(SDK.OverlayModel);if(overlayModel){overlayModel.highlightFrame(to.frameId);}}
  4. if(fromElement){fromElement.classList.remove('highlighted');}
  5. if(toElement){toElement.classList.add('highlighted');}}
  6. titleFor(executionContext){const target=executionContext.target();let label=executionContext.label()?target.decorateLabel(executionContext.label()):'';if(executionContext.frameId){const resourceTreeModel=target.model(SDK.ResourceTreeModel);const frame=resourceTreeModel&&resourceTreeModel.frameForId(executionContext.frameId);if(frame){label=label||frame.displayName();}}
  7. label=label||executionContext.origin;return label;}
  8. _depthFor(executionContext){let target=executionContext.target();let depth=0;if(!executionContext.isDefault){depth++;}
  9. if(executionContext.frameId){const resourceTreeModel=target.model(SDK.ResourceTreeModel);let frame=resourceTreeModel&&resourceTreeModel.frameForId(executionContext.frameId);while(frame){frame=frame.parentFrame||frame.crossTargetParentFrame();if(frame){depth++;target=frame.resourceTreeModel().target();}}}
  10. let targetDepth=0;while(target.parentTarget()&&target.type()!==SDK.Target.Type.ServiceWorker){targetDepth++;target=target.parentTarget();}
  11. depth+=targetDepth;return depth;}
  12. _executionContextCreated(executionContext){this._items.insertWithComparator(executionContext,executionContext.runtimeModel.executionContextComparator());if(executionContext===UI.context.flavor(SDK.ExecutionContext)){this._dropDown.selectItem(executionContext);}}
  13. _onExecutionContextCreated(event){const executionContext=(event.data);this._executionContextCreated(executionContext);}
  14. _onExecutionContextChanged(event){const executionContext=(event.data);if(this._items.indexOf(executionContext)===-1){return;}
  15. this._executionContextDestroyed(executionContext);this._executionContextCreated(executionContext);}
  16. _executionContextDestroyed(executionContext){const index=this._items.indexOf(executionContext);if(index===-1){return;}
  17. this._items.remove(index);}
  18. _onExecutionContextDestroyed(event){const executionContext=(event.data);this._executionContextDestroyed(executionContext);}
  19. _executionContextChangedExternally(event){const executionContext=(event.data);this._dropDown.selectItem(executionContext);}
  20. _isTopContext(executionContext){if(!executionContext||!executionContext.isDefault){return false;}
  21. const resourceTreeModel=executionContext.target().model(SDK.ResourceTreeModel);const frame=executionContext.frameId&&resourceTreeModel&&resourceTreeModel.frameForId(executionContext.frameId);if(!frame){return false;}
  22. return frame.isTopFrame();}
  23. _hasTopContext(){return this._items.some(executionContext=>this._isTopContext(executionContext));}
  24. modelAdded(runtimeModel){runtimeModel.executionContexts().forEach(this._executionContextCreated,this);}
  25. modelRemoved(runtimeModel){for(let i=this._items.length-1;i>=0;i--){if(this._items.at(i).runtimeModel===runtimeModel){this._executionContextDestroyed(this._items.at(i));}}}
  26. createElementForItem(item){const element=createElementWithClass('div');const shadowRoot=UI.createShadowRootWithCoreStyles(element,'console/consoleContextSelector.css');const title=shadowRoot.createChild('div','title');title.createTextChild(this.titleFor(item).trimEndWithMaxLength(100));const subTitle=shadowRoot.createChild('div','subtitle');subTitle.createTextChild(this._subtitleFor(item));element.style.paddingLeft=(8+this._depthFor(item)*15)+'px';return element;}
  27. _subtitleFor(executionContext){const target=executionContext.target();let frame;if(executionContext.frameId){const resourceTreeModel=target.model(SDK.ResourceTreeModel);frame=resourceTreeModel&&resourceTreeModel.frameForId(executionContext.frameId);}
  28. if(executionContext.origin.startsWith('chrome-extension://')){return Common.UIString('Extension');}
  29. if(!frame||!frame.parentFrame||frame.parentFrame.securityOrigin!==executionContext.origin){const url=executionContext.origin.asParsedURL();if(url){return url.domain();}}
  30. if(frame){const callFrame=frame.findCreationCallFrame(callFrame=>!!callFrame.url);if(callFrame){return new Common.ParsedURL(callFrame.url).domain();}
  31. return Common.UIString('IFrame');}
  32. return'';}
  33. isItemSelectable(item){const callFrame=item.debuggerModel.selectedCallFrame();const callFrameContext=callFrame&&callFrame.script.executionContext();return!callFrameContext||item===callFrameContext;}
  34. itemSelected(item){this._toolbarItem.element.classList.toggle('warning',!this._isTopContext(item)&&this._hasTopContext());const title=item?ls`JavaScript context: ${this.titleFor(item)}`:ls`JavaScript context: Not selected`;this._toolbarItem.setTitle(title);UI.context.setFlavor(SDK.ExecutionContext,item);}
  35. _callFrameSelectedInUI(){const callFrame=UI.context.flavor(SDK.DebuggerModel.CallFrame);const callFrameContext=callFrame&&callFrame.script.executionContext();if(callFrameContext){UI.context.setFlavor(SDK.ExecutionContext,callFrameContext);}}
  36. _callFrameSelectedInModel(event){const debuggerModel=(event.data);for(const executionContext of this._items){if(executionContext.debuggerModel===debuggerModel){this._dropDown.refreshItem(executionContext);}}}
  37. _frameNavigated(event){const frame=(event.data);const runtimeModel=frame.resourceTreeModel().target().model(SDK.RuntimeModel);if(!runtimeModel){return;}
  38. for(const executionContext of runtimeModel.executionContexts()){if(frame.id===executionContext.frameId){this._dropDown.refreshItem(executionContext);}}}}
  39. self.Console=self.Console||{};Console=Console||{};Console.ConsoleContextSelector=ConsoleContextSelector;