LayerViewHost.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334
  1. export class LayerView{hoverObject(selection){}
  2. selectObject(selection){}
  3. setLayerTree(layerTree){}}
  4. export class Selection{constructor(type,layer){this._type=type;this._layer=layer;}
  5. static isEqual(a,b){return a&&b?a._isEqual(b):a===b;}
  6. type(){return this._type;}
  7. layer(){return this._layer;}
  8. _isEqual(other){return false;}}
  9. export const Type={Layer:Symbol('Layer'),ScrollRect:Symbol('ScrollRect'),Snapshot:Symbol('Snapshot')};export class LayerSelection extends Selection{constructor(layer){console.assert(layer,'LayerSelection with empty layer');super(Type.Layer,layer);}
  10. _isEqual(other){return other._type===Type.Layer&&other.layer().id()===this.layer().id();}}
  11. export class ScrollRectSelection extends Selection{constructor(layer,scrollRectIndex){super(Type.ScrollRect,layer);this.scrollRectIndex=scrollRectIndex;}
  12. _isEqual(other){return other._type===Type.ScrollRect&&this.layer().id()===other.layer().id()&&this.scrollRectIndex===other.scrollRectIndex;}}
  13. export class SnapshotSelection extends Selection{constructor(layer,snapshot){super(Type.Snapshot,layer);this._snapshot=snapshot;}
  14. _isEqual(other){return other._type===Type.Snapshot&&this.layer().id()===other.layer().id()&&this._snapshot===other._snapshot;}
  15. snapshot(){return this._snapshot;}}
  16. export class LayerViewHost{constructor(){this._views=[];this._selectedObject=null;this._hoveredObject=null;this._showInternalLayersSetting=Common.settings.createSetting('layersShowInternalLayers',false);}
  17. registerView(layerView){this._views.push(layerView);}
  18. setLayerSnapshotMap(snapshotLayers){this._snapshotLayers=snapshotLayers;}
  19. getLayerSnapshotMap(){return this._snapshotLayers;}
  20. setLayerTree(layerTree){this._target=layerTree.target();const selectedLayer=this._selectedObject&&this._selectedObject.layer();if(selectedLayer&&(!layerTree||!layerTree.layerById(selectedLayer.id()))){this.selectObject(null);}
  21. const hoveredLayer=this._hoveredObject&&this._hoveredObject.layer();if(hoveredLayer&&(!layerTree||!layerTree.layerById(hoveredLayer.id()))){this.hoverObject(null);}
  22. for(const view of this._views){view.setLayerTree(layerTree);}}
  23. hoverObject(selection){if(Selection.isEqual(this._hoveredObject,selection)){return;}
  24. this._hoveredObject=selection;const layer=selection&&selection.layer();this._toggleNodeHighlight(layer?layer.nodeForSelfOrAncestor():null);for(const view of this._views){view.hoverObject(selection);}}
  25. selectObject(selection){if(Selection.isEqual(this._selectedObject,selection)){return;}
  26. this._selectedObject=selection;for(const view of this._views){view.selectObject(selection);}}
  27. selection(){return this._selectedObject;}
  28. showContextMenu(contextMenu,selection){contextMenu.defaultSection().appendCheckboxItem(Common.UIString('Show internal layers'),this._toggleShowInternalLayers.bind(this),this._showInternalLayersSetting.get());const node=selection&&selection.layer()&&selection.layer().nodeForSelfOrAncestor();if(node){contextMenu.appendApplicableItems(node);}
  29. contextMenu.show();}
  30. showInternalLayersSetting(){return this._showInternalLayersSetting;}
  31. _toggleShowInternalLayers(){this._showInternalLayersSetting.set(!this._showInternalLayersSetting.get());}
  32. _toggleNodeHighlight(node){if(node){node.highlightForTwoSeconds();return;}
  33. SDK.OverlayModel.hideDOMNodeHighlight();}}
  34. self.LayerViewer=self.LayerViewer||{};LayerViewer=LayerViewer||{};LayerViewer.LayerView=LayerView;LayerViewer.LayerView.Selection=Selection;LayerViewer.LayerView.Selection.Type=Type;LayerViewer.LayerView.LayerSelection=LayerSelection;LayerViewer.LayerView.ScrollRectSelection=ScrollRectSelection;LayerViewer.LayerView.SnapshotSelection=SnapshotSelection;LayerViewer.LayerViewHost=LayerViewHost;