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