AuditsReportSelector.js 2.2 KB

123456789101112131415161718
  1. export default class ReportSelector{constructor(renderNewAuditView){this._renderNewAuditView=renderNewAuditView;this._newAuditItem=createElement('option');this._comboBox=new UI.ToolbarComboBox(this._handleChange.bind(this),ls`Reports`,'audits-report');this._comboBox.setMaxWidth(180);this._comboBox.setMinWidth(140);this._itemByOptionElement=new Map();this._setEmptyState();}
  2. _setEmptyState(){this._comboBox.selectElement().removeChildren();this._comboBox.setEnabled(false);this._newAuditItem=createElement('option');this._newAuditItem.label=Common.UIString('(new audit)');this._comboBox.selectElement().appendChild(this._newAuditItem);this._comboBox.select(this._newAuditItem);}
  3. _handleChange(event){const item=this._selectedItem();if(item){item.select();}else{this._renderNewAuditView();}}
  4. _selectedItem(){const option=this._comboBox.selectedOption();return this._itemByOptionElement.get(option);}
  5. hasCurrentSelection(){return!!this._selectedItem();}
  6. hasItems(){return this._itemByOptionElement.size>0;}
  7. comboBox(){return this._comboBox;}
  8. prepend(item){const optionEl=item.optionElement();const selectEl=this._comboBox.selectElement();this._itemByOptionElement.set(optionEl,item);selectEl.insertBefore(optionEl,selectEl.firstElementChild);this._comboBox.setEnabled(true);this._comboBox.select(optionEl);item.select();}
  9. clearAll(){for(const elem of this._comboBox.options()){if(elem===this._newAuditItem){continue;}
  10. this._itemByOptionElement.get(elem).delete();this._itemByOptionElement.delete(elem);}
  11. this._setEmptyState();}
  12. selectNewAudit(){this._comboBox.select(this._newAuditItem);}}
  13. export class Item{constructor(lighthouseResult,renderReport,showLandingCallback){this._lighthouseResult=lighthouseResult;this._renderReport=renderReport;this._showLandingCallback=showLandingCallback;const url=new Common.ParsedURL(lighthouseResult.finalUrl);const timestamp=lighthouseResult.fetchTime;this._element=createElement('option');this._element.label=`${new Date(timestamp).toLocaleTimeString()} - ${url.domain()}`;}
  14. select(){this._renderReport();}
  15. optionElement(){return this._element;}
  16. delete(){if(this._element){this._element.remove();}
  17. this._showLandingCallback();}}
  18. self.Audits=self.Audits||{};Audits=Audits||{};Audits.ReportSelector=ReportSelector;Audits.ReportSelector.Item=Item;