Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame^] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /** |
| 6 | * @unrestricted |
| 7 | */ |
| 8 | Audits2.StartView = class extends UI.Widget { |
| 9 | /** |
| 10 | * @param {!Audits2.AuditController} controller |
| 11 | */ |
| 12 | constructor(controller) { |
| 13 | super(); |
| 14 | this.registerRequiredCSS('audits2/audits2StartView.css'); |
| 15 | this._controller = controller; |
| 16 | this._render(); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param {string} settingName |
| 21 | * @param {!Element} parentElement |
| 22 | */ |
| 23 | _populateRuntimeSettingAsComboBox(settingName, parentElement) { |
| 24 | const runtimeSetting = Audits2.RuntimeSettings.find(item => item.setting.name === settingName); |
| 25 | const control = new UI.ToolbarSettingComboBox(runtimeSetting.options, runtimeSetting.setting); |
| 26 | control.element.title = runtimeSetting.description; |
| 27 | parentElement.appendChild(control.element); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param {!UI.Fragment} fragment |
| 32 | */ |
| 33 | _populateFormControls(fragment) { |
| 34 | // Populate the device type |
| 35 | const deviceTypeFormElements = fragment.$('device-type-form-elements'); |
| 36 | this._populateRuntimeSettingAsComboBox('audits2.device_type', deviceTypeFormElements); |
| 37 | |
| 38 | // Populate the audit categories |
| 39 | const categoryFormElements = fragment.$('categories-form-elements'); |
| 40 | for (const preset of Audits2.Presets) { |
| 41 | preset.setting.setTitle(preset.title); |
| 42 | const checkbox = new UI.ToolbarSettingCheckbox(preset.setting); |
| 43 | const row = categoryFormElements.createChild('div', 'vbox audits2-launcher-row'); |
| 44 | row.appendChild(checkbox.element); |
| 45 | row.createChild('span', 'audits2-launcher-description dimmed').textContent = preset.description; |
| 46 | } |
| 47 | |
| 48 | // Populate the throttling |
| 49 | const throttlingFormElements = fragment.$('throttling-form-elements'); |
| 50 | this._populateRuntimeSettingAsComboBox('audits2.throttling', throttlingFormElements); |
| 51 | |
| 52 | |
| 53 | // Populate other settings |
| 54 | const otherFormElements = fragment.$('other-form-elements'); |
| 55 | this._populateRuntimeSettingAsComboBox('audits2.storage_reset', otherFormElements); |
| 56 | } |
| 57 | |
| 58 | _render() { |
| 59 | this._startButton = UI.createTextButton( |
| 60 | ls`Run audit`, () => this._controller.dispatchEventToListeners(Audits2.Events.RequestAuditStart), |
| 61 | 'audits2-start-button', true /* primary */); |
| 62 | const deviceIcon = UI.Icon.create('largeicon-phone'); |
| 63 | const categoriesIcon = UI.Icon.create('largeicon-checkmark'); |
| 64 | const throttlingIcon = UI.Icon.create('largeicon-settings-gear'); |
| 65 | |
| 66 | const fragment = UI.Fragment.build` |
| 67 | <div class="vbox audits2-start-view"> |
| 68 | <div class="audits2-dialog-overlay"></div> |
| 69 | <header> |
| 70 | <div class="audits2-logo"></div> |
| 71 | <div class="audits2-start-view-text"> |
| 72 | <h2>Audits</h2> |
| 73 | <p> |
| 74 | Identify and fix common problems that affect your site's performance, accessibility, and user experience. |
| 75 | <span class="link" $="learn-more">Learn more.</a> |
| 76 | </p> |
| 77 | </div> |
| 78 | </header> |
| 79 | <form> |
| 80 | <div class="audits2-form-section"> |
| 81 | <div class="audits2-form-section-label"> |
| 82 | <i>${deviceIcon}</i> |
| 83 | <div class="audits2-icon-label">Device</div> |
| 84 | </div> |
| 85 | <div class="audits2-form-elements" $="device-type-form-elements"></div> |
| 86 | </div> |
| 87 | <div class="audits2-form-section"> |
| 88 | <div class="audits2-form-section-label"> |
| 89 | <i>${categoriesIcon}</i> |
| 90 | <div class="audits2-icon-label">Audits</div> |
| 91 | </div> |
| 92 | <div class="audits2-form-elements" $="categories-form-elements"></div> |
| 93 | </div> |
| 94 | <div class="audits2-form-section"> |
| 95 | <div class="audits2-form-section-label"> |
| 96 | <i>${throttlingIcon}</i> |
| 97 | <div class="audits2-icon-label">Throttling</div> |
| 98 | </div> |
| 99 | <div class="audits2-form-elements" $="throttling-form-elements"></div> |
| 100 | </div> |
| 101 | <div class="audits2-form-section"> |
| 102 | <div class="audits2-form-section-label"></div> |
| 103 | <div class="audits2-form-elements" $="other-form-elements"></div> |
| 104 | </div> |
| 105 | <div class="audits2-form-section"> |
| 106 | <div class="audits2-form-section-label"></div> |
| 107 | <div class="audits2-form-elements audits2-start-button-container hbox"> |
| 108 | ${this._startButton} |
| 109 | <div $="help-text" class="audits2-help-text hidden"></div> |
| 110 | </div> |
| 111 | </div> |
| 112 | </form> |
| 113 | </div> |
| 114 | `; |
| 115 | |
| 116 | this._helpText = fragment.$('help-text'); |
| 117 | |
| 118 | const learnMoreLink = fragment.$('learn-more'); |
| 119 | learnMoreLink.addEventListener( |
| 120 | 'click', () => InspectorFrontendHost.openInNewTab('https://developers.google.com/web/tools/lighthouse/')); |
| 121 | |
| 122 | this._populateFormControls(fragment); |
| 123 | this.contentElement.appendChild(fragment.element()); |
| 124 | this.contentElement.style.overflow = 'auto'; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param {boolean} isEnabled |
| 129 | */ |
| 130 | setStartButtonEnabled(isEnabled) { |
| 131 | if (this._helpText) |
| 132 | this._helpText.classList.toggle('hidden', isEnabled); |
| 133 | |
| 134 | if (this._startButton) |
| 135 | this._startButton.disabled = !isEnabled; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @param {?string} text |
| 140 | */ |
| 141 | setUnauditableExplanation(text) { |
| 142 | if (this._helpText) |
| 143 | this._helpText.textContent = text; |
| 144 | } |
| 145 | }; |