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 | */ |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 23 | _populateRuntimeSettingAsRadio(settingName, parentElement) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 24 | const runtimeSetting = Audits2.RuntimeSettings.find(item => item.setting.name === settingName); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 25 | if (!runtimeSetting || !runtimeSetting.options) |
| 26 | throw new Error(`${settingName} is not a setting with options`); |
| 27 | |
| 28 | const control = new Audits2.RadioSetting(runtimeSetting.options, runtimeSetting.setting); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 29 | control.element.title = runtimeSetting.description; |
| 30 | parentElement.appendChild(control.element); |
| 31 | } |
| 32 | |
| 33 | /** |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 34 | * @param {string} settingName |
| 35 | * @param {!Element} parentElement |
| 36 | */ |
| 37 | _populateRuntimeSettingAsCheckbox(settingName, parentElement) { |
| 38 | const runtimeSetting = Audits2.RuntimeSettings.find(item => item.setting.name === settingName); |
| 39 | if (!runtimeSetting || !runtimeSetting.title) |
| 40 | throw new Error(`${settingName} is not a setting with a title`); |
| 41 | |
| 42 | runtimeSetting.setting.setTitle(runtimeSetting.title); |
| 43 | const control = new UI.ToolbarSettingCheckbox(runtimeSetting.setting, runtimeSetting.description); |
| 44 | parentElement.appendChild(control.element); |
| 45 | } |
| 46 | |
| 47 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 48 | * @param {!UI.Fragment} fragment |
| 49 | */ |
| 50 | _populateFormControls(fragment) { |
| 51 | // Populate the device type |
| 52 | const deviceTypeFormElements = fragment.$('device-type-form-elements'); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 53 | this._populateRuntimeSettingAsRadio('audits2.device_type', deviceTypeFormElements); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 54 | |
| 55 | // Populate the audit categories |
| 56 | const categoryFormElements = fragment.$('categories-form-elements'); |
| 57 | for (const preset of Audits2.Presets) { |
| 58 | preset.setting.setTitle(preset.title); |
| 59 | const checkbox = new UI.ToolbarSettingCheckbox(preset.setting); |
| 60 | const row = categoryFormElements.createChild('div', 'vbox audits2-launcher-row'); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 61 | row.title = preset.description; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 62 | row.appendChild(checkbox.element); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Populate the throttling |
| 66 | const throttlingFormElements = fragment.$('throttling-form-elements'); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 67 | this._populateRuntimeSettingAsRadio('audits2.throttling', throttlingFormElements); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | // Populate other settings |
| 71 | const otherFormElements = fragment.$('other-form-elements'); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 72 | this._populateRuntimeSettingAsCheckbox('audits2.clear_storage', otherFormElements); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | _render() { |
| 76 | this._startButton = UI.createTextButton( |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 77 | ls`Run audits`, () => this._controller.dispatchEventToListeners(Audits2.Events.RequestAuditStart), |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 78 | 'audits2-start-button', true /* primary */); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 79 | this.setDefaultFocusedElement(this._startButton); |
| 80 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 81 | const deviceIcon = UI.Icon.create('largeicon-phone'); |
| 82 | const categoriesIcon = UI.Icon.create('largeicon-checkmark'); |
| 83 | const throttlingIcon = UI.Icon.create('largeicon-settings-gear'); |
| 84 | |
| 85 | const fragment = UI.Fragment.build` |
| 86 | <div class="vbox audits2-start-view"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 87 | <header> |
| 88 | <div class="audits2-logo"></div> |
| 89 | <div class="audits2-start-view-text"> |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame^] | 90 | <h2>${ls`Audits`}</h2> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 91 | <p> |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame^] | 92 | ${ls`Identify and fix common problems that affect your site's performance, |
| 93 | accessibility, and user experience.`} |
| 94 | <span class="link" $="learn-more">${ls`Learn more`}</a> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 95 | </p> |
| 96 | </div> |
| 97 | </header> |
| 98 | <form> |
| 99 | <div class="audits2-form-section"> |
| 100 | <div class="audits2-form-section-label"> |
| 101 | <i>${deviceIcon}</i> |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame^] | 102 | <div class="audits2-icon-label">${ls`Device`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 103 | </div> |
| 104 | <div class="audits2-form-elements" $="device-type-form-elements"></div> |
| 105 | </div> |
| 106 | <div class="audits2-form-section"> |
| 107 | <div class="audits2-form-section-label"> |
| 108 | <i>${categoriesIcon}</i> |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame^] | 109 | <div class="audits2-icon-label">${ls`Audits`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 110 | </div> |
| 111 | <div class="audits2-form-elements" $="categories-form-elements"></div> |
| 112 | </div> |
| 113 | <div class="audits2-form-section"> |
| 114 | <div class="audits2-form-section-label"> |
| 115 | <i>${throttlingIcon}</i> |
Christy Chen | 38dccb5 | 2019-05-08 22:32:15 +0000 | [diff] [blame^] | 116 | <div class="audits2-icon-label">${ls`Throttling`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 117 | </div> |
| 118 | <div class="audits2-form-elements" $="throttling-form-elements"></div> |
| 119 | </div> |
| 120 | <div class="audits2-form-section"> |
| 121 | <div class="audits2-form-section-label"></div> |
| 122 | <div class="audits2-form-elements" $="other-form-elements"></div> |
| 123 | </div> |
| 124 | <div class="audits2-form-section"> |
| 125 | <div class="audits2-form-section-label"></div> |
| 126 | <div class="audits2-form-elements audits2-start-button-container hbox"> |
| 127 | ${this._startButton} |
| 128 | <div $="help-text" class="audits2-help-text hidden"></div> |
| 129 | </div> |
| 130 | </div> |
| 131 | </form> |
| 132 | </div> |
| 133 | `; |
| 134 | |
| 135 | this._helpText = fragment.$('help-text'); |
| 136 | |
| 137 | const learnMoreLink = fragment.$('learn-more'); |
| 138 | learnMoreLink.addEventListener( |
| 139 | 'click', () => InspectorFrontendHost.openInNewTab('https://developers.google.com/web/tools/lighthouse/')); |
| 140 | |
| 141 | this._populateFormControls(fragment); |
| 142 | this.contentElement.appendChild(fragment.element()); |
| 143 | this.contentElement.style.overflow = 'auto'; |
| 144 | } |
| 145 | |
Patrick Hulce | 8d387f1 | 2018-05-29 18:54:54 +0000 | [diff] [blame] | 146 | focusStartButton() { |
| 147 | this._startButton.focus(); |
| 148 | } |
| 149 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 150 | /** |
| 151 | * @param {boolean} isEnabled |
| 152 | */ |
| 153 | setStartButtonEnabled(isEnabled) { |
| 154 | if (this._helpText) |
| 155 | this._helpText.classList.toggle('hidden', isEnabled); |
| 156 | |
| 157 | if (this._startButton) |
| 158 | this._startButton.disabled = !isEnabled; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @param {?string} text |
| 163 | */ |
| 164 | setUnauditableExplanation(text) { |
| 165 | if (this._helpText) |
| 166 | this._helpText.textContent = text; |
| 167 | } |
| 168 | }; |