ChangesSidebar.js 3.4 KB

12345678910111213141516
  1. export default class ChangesSidebar extends UI.Widget{constructor(workspaceDiff){super();this._treeoutline=new UI.TreeOutlineInShadow();this._treeoutline.registerRequiredCSS('changes/changesSidebar.css');this._treeoutline.setComparator((a,b)=>a.titleAsText().compareTo(b.titleAsText()));this._treeoutline.addEventListener(UI.TreeOutline.Events.ElementSelected,this._selectionChanged,this);this.element.appendChild(this._treeoutline.element);this._treeElements=new Map();this._workspaceDiff=workspaceDiff;this._workspaceDiff.modifiedUISourceCodes().forEach(this._addUISourceCode.bind(this));this._workspaceDiff.addEventListener(WorkspaceDiff.Events.ModifiedStatusChanged,this._uiSourceCodeMofiedStatusChanged,this);}
  2. selectUISourceCode(uiSourceCode,omitFocus){const treeElement=this._treeElements.get(uiSourceCode);if(!treeElement){return;}
  3. treeElement.select(omitFocus);}
  4. selectedUISourceCode(){return this._treeoutline.selectedTreeElement?this._treeoutline.selectedTreeElement.uiSourceCode:null;}
  5. _selectionChanged(){this.dispatchEventToListeners(Events.SelectedUISourceCodeChanged);}
  6. _uiSourceCodeMofiedStatusChanged(event){if(event.data.isModified){this._addUISourceCode(event.data.uiSourceCode);}else{this._removeUISourceCode(event.data.uiSourceCode);}}
  7. _removeUISourceCode(uiSourceCode){const treeElement=this._treeElements.get(uiSourceCode);this._treeElements.delete(uiSourceCode);if(this._treeoutline.selectedTreeElement===treeElement){const nextElementToSelect=treeElement.previousSibling||treeElement.nextSibling;if(nextElementToSelect){nextElementToSelect.select(true);}else{treeElement.deselect();this._selectionChanged();}}
  8. this._treeoutline.removeChild(treeElement);treeElement.dispose();}
  9. _addUISourceCode(uiSourceCode){const treeElement=new UISourceCodeTreeElement(uiSourceCode);this._treeElements.set(uiSourceCode,treeElement);this._treeoutline.appendChild(treeElement);if(!this._treeoutline.selectedTreeElement){treeElement.select(true);}}}
  10. export const Events={SelectedUISourceCodeChanged:Symbol('SelectedUISourceCodeChanged')};export class UISourceCodeTreeElement extends UI.TreeElement{constructor(uiSourceCode){super();this.uiSourceCode=uiSourceCode;this.listItemElement.classList.add('navigator-'+uiSourceCode.contentType().name()+'-tree-item');let iconType='largeicon-navigator-file';if(Snippets.isSnippetsUISourceCode(this.uiSourceCode)){iconType='largeicon-navigator-snippet';}
  11. const defaultIcon=UI.Icon.create(iconType,'icon');this.setLeadingIcons([defaultIcon]);this._eventListeners=[uiSourceCode.addEventListener(Workspace.UISourceCode.Events.TitleChanged,this._updateTitle,this),uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this._updateTitle,this),uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this._updateTitle,this)];this._updateTitle();}
  12. _updateTitle(){let titleText=this.uiSourceCode.displayName();if(this.uiSourceCode.isDirty()){titleText='*'+titleText;}
  13. this.title=titleText;let tooltip=this.uiSourceCode.url();if(this.uiSourceCode.contentType().isFromSourceMap()){tooltip=Common.UIString('%s (from source map)',this.uiSourceCode.displayName());}
  14. this.tooltip=tooltip;}
  15. dispose(){Common.EventTarget.removeEventListeners(this._eventListeners);}}
  16. self.Changes=self.Changes||{};Changes=Changes||{};Changes.ChangesSidebar=ChangesSidebar;Changes.ChangesSidebar.Events=Events;Changes.ChangesSidebar.UISourceCodeTreeElement=UISourceCodeTreeElement;