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 | |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 5 | import {Events, LighthouseController, Presets, RuntimeSettings} from './LighthouseController.js'; // eslint-disable-line no-unused-vars |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 6 | import {RadioSetting} from './RadioSetting.js'; |
| 7 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 8 | /** |
| 9 | * @unrestricted |
| 10 | */ |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 11 | export class StartView extends UI.Widget { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 12 | /** |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 13 | * @param {!LighthouseController} controller |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 14 | */ |
| 15 | constructor(controller) { |
| 16 | super(); |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 17 | this.registerRequiredCSS('lighthouse/lighthouseStartView.css'); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 18 | this._controller = controller; |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 19 | this._settingsToolbar = new UI.Toolbar(''); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 20 | this._render(); |
| 21 | } |
| 22 | |
| 23 | /** |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 24 | * @return {!UI.Toolbar} |
| 25 | */ |
| 26 | settingsToolbar() { |
| 27 | return this._settingsToolbar; |
| 28 | } |
| 29 | |
| 30 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 31 | * @param {string} settingName |
John Emau | 7347716 | 2019-07-17 00:30:51 +0000 | [diff] [blame] | 32 | * @param {string} label |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 33 | * @param {!Element} parentElement |
| 34 | */ |
John Emau | 7347716 | 2019-07-17 00:30:51 +0000 | [diff] [blame] | 35 | _populateRuntimeSettingAsRadio(settingName, label, parentElement) { |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 36 | const runtimeSetting = RuntimeSettings.find(item => item.setting.name === settingName); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 37 | if (!runtimeSetting || !runtimeSetting.options) { |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 38 | throw new Error(`${settingName} is not a setting with options`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 39 | } |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 40 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 41 | const control = new RadioSetting(runtimeSetting.options, runtimeSetting.setting, runtimeSetting.description); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 42 | parentElement.appendChild(control.element); |
John Emau | 7347716 | 2019-07-17 00:30:51 +0000 | [diff] [blame] | 43 | UI.ARIAUtils.setAccessibleName(control.element, label); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 47 | * @param {string} settingName |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 48 | * @param {!UI.Toolbar} toolbar |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 49 | */ |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 50 | _populateRuntimeSettingAsToolbarCheckbox(settingName, toolbar) { |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 51 | const runtimeSetting = RuntimeSettings.find(item => item.setting.name === settingName); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 52 | if (!runtimeSetting || !runtimeSetting.title) { |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 53 | throw new Error(`${settingName} is not a setting with a title`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 54 | } |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 55 | |
| 56 | runtimeSetting.setting.setTitle(runtimeSetting.title); |
| 57 | const control = new UI.ToolbarSettingCheckbox(runtimeSetting.setting, runtimeSetting.description); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 58 | toolbar.appendToolbarItem(control); |
| 59 | if (runtimeSetting.learnMore) { |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 60 | const link = UI.XLink.create(runtimeSetting.learnMore, ls`Learn more`, 'lighthouse-learn-more'); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 61 | link.style.padding = '5px'; |
| 62 | control.element.appendChild(link); |
| 63 | } |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 67 | * @param {!UI.Fragment} fragment |
| 68 | */ |
| 69 | _populateFormControls(fragment) { |
| 70 | // Populate the device type |
| 71 | const deviceTypeFormElements = fragment.$('device-type-form-elements'); |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 72 | this._populateRuntimeSettingAsRadio('lighthouse.device_type', ls`Device`, deviceTypeFormElements); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 73 | |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 74 | // Populate the categories |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 75 | const categoryFormElements = fragment.$('categories-form-elements'); |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 76 | const pluginFormElements = fragment.$('plugins-form-elements'); |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 77 | for (const preset of Presets) { |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 78 | const formElements = preset.plugin ? pluginFormElements : categoryFormElements; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 79 | preset.setting.setTitle(preset.title); |
| 80 | const checkbox = new UI.ToolbarSettingCheckbox(preset.setting); |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 81 | const row = formElements.createChild('div', 'vbox lighthouse-launcher-row'); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 82 | row.title = preset.description; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 83 | row.appendChild(checkbox.element); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 84 | } |
John Emau | 62fdcee | 2019-10-16 00:08:52 +0000 | [diff] [blame] | 85 | UI.ARIAUtils.markAsGroup(categoryFormElements); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 86 | UI.ARIAUtils.setAccessibleName(categoryFormElements, ls`Categories`); |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 87 | UI.ARIAUtils.markAsGroup(pluginFormElements); |
| 88 | UI.ARIAUtils.setAccessibleName(pluginFormElements, ls`Community Plugins (beta)`); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | _render() { |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 92 | this._populateRuntimeSettingAsToolbarCheckbox('lighthouse.clear_storage', this._settingsToolbar); |
| 93 | this._populateRuntimeSettingAsToolbarCheckbox('lighthouse.throttling', this._settingsToolbar); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 94 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 95 | this._startButton = UI.createTextButton( |
Brandon Goddard | 04d5ba9 | 2019-12-19 08:31:55 -0800 | [diff] [blame] | 96 | ls`Generate report`, |
| 97 | () => this._controller.dispatchEventToListeners( |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 98 | Events.RequestLighthouseStart, |
Brandon Goddard | 04d5ba9 | 2019-12-19 08:31:55 -0800 | [diff] [blame] | 99 | /* keyboardInitiated */ UI.elementIsFocusedByKeyboard(this._startButton)), |
| 100 | /* className */ '', /* primary */ true); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 101 | this.setDefaultFocusedElement(this._startButton); |
| 102 | |
Christy Chen | 1eea03c | 2019-06-26 19:31:24 +0000 | [diff] [blame] | 103 | const auditsDescription = ls |
| 104 | `Identify and fix common problems that affect your site's performance, accessibility, and user experience.`; // crbug.com/972969 |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 105 | |
| 106 | const fragment = UI.Fragment.build` |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 107 | <div class="vbox lighthouse-start-view"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 108 | <header> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 109 | <div class="lighthouse-logo"></div> |
| 110 | <div class="lighthouse-start-button-container hbox"> |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 111 | ${this._startButton} |
| 112 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 113 | <div $="help-text" class="lighthouse-help-text hidden"></div> |
| 114 | <div class="lighthouse-start-view-text"> |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 115 | <span>${auditsDescription}</span> |
John Emau | 97832ab | 2019-07-16 00:37:34 +0000 | [diff] [blame] | 116 | ${UI.XLink.create('https://developers.google.com/web/tools/lighthouse/', ls`Learn more`)} |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 117 | </div> |
| 118 | </header> |
| 119 | <form> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 120 | <div class="lighthouse-form-categories"> |
| 121 | <div class="lighthouse-form-section"> |
| 122 | <div class="lighthouse-form-section-label"> |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 123 | ${ls`Categories`} |
| 124 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 125 | <div class="lighthouse-form-elements" $="categories-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 126 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 127 | <div class="lighthouse-form-section"> |
| 128 | <div class="lighthouse-form-section-label"> |
| 129 | <div class="lighthouse-icon-label">${ls`Community Plugins (beta)`}</div> |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 130 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 131 | <div class="lighthouse-form-elements" $="plugins-form-elements"></div> |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 132 | </div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 133 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 134 | <div class="lighthouse-form-section"> |
| 135 | <div class="lighthouse-form-section-label"> |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 136 | ${ls`Device`} |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 137 | </div> |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 138 | <div class="lighthouse-form-elements" $="device-type-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 139 | </div> |
| 140 | </form> |
| 141 | </div> |
| 142 | `; |
| 143 | |
| 144 | this._helpText = fragment.$('help-text'); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 145 | this._populateFormControls(fragment); |
| 146 | this.contentElement.appendChild(fragment.element()); |
| 147 | this.contentElement.style.overflow = 'auto'; |
| 148 | } |
| 149 | |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 150 | /** |
| 151 | * @override |
| 152 | */ |
| 153 | onResize() { |
| 154 | const useNarrowLayout = this.contentElement.offsetWidth < 560; |
Connor Clark | 2bc3be2 | 2020-02-14 14:34:19 -0800 | [diff] [blame^] | 155 | const startViewEl = this.contentElement.querySelector('.lighthouse-start-view'); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 156 | startViewEl.classList.toggle('hbox', !useNarrowLayout); |
| 157 | startViewEl.classList.toggle('vbox', useNarrowLayout); |
| 158 | } |
| 159 | |
Patrick Hulce | 8d387f1 | 2018-05-29 18:54:54 +0000 | [diff] [blame] | 160 | focusStartButton() { |
| 161 | this._startButton.focus(); |
| 162 | } |
| 163 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 164 | /** |
| 165 | * @param {boolean} isEnabled |
| 166 | */ |
| 167 | setStartButtonEnabled(isEnabled) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 168 | if (this._helpText) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 169 | this._helpText.classList.toggle('hidden', isEnabled); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 170 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 171 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 172 | if (this._startButton) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 173 | this._startButton.disabled = !isEnabled; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 174 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param {?string} text |
| 179 | */ |
| 180 | setUnauditableExplanation(text) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 181 | if (this._helpText) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 182 | this._helpText.textContent = text; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 183 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 184 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 185 | } |