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 |
| 21 | * @param {!Element} parentElement |
| 22 | */ |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 23 | _populateRuntimeSettingAsRadio(settingName, parentElement) { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 24 | const runtimeSetting = Audits.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 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 28 | const control = new Audits.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) { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 38 | const runtimeSetting = Audits.RuntimeSettings.find(item => item.setting.name === settingName); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 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'); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 53 | this._populateRuntimeSettingAsRadio('audits.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'); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 57 | for (const preset of Audits.Presets) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 58 | preset.setting.setTitle(preset.title); |
| 59 | const checkbox = new UI.ToolbarSettingCheckbox(preset.setting); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 60 | const row = categoryFormElements.createChild('div', 'vbox audits-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'); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 67 | this._populateRuntimeSettingAsRadio('audits.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'); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 72 | this._populateRuntimeSettingAsCheckbox('audits.clear_storage', otherFormElements); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | _render() { |
| 76 | this._startButton = UI.createTextButton( |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 77 | ls`Run audits`, () => this._controller.dispatchEventToListeners(Audits.Events.RequestAuditStart), |
| 78 | 'audits-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'); |
Christy Chen | 1eea03c | 2019-06-26 19:31:24 +0000 | [diff] [blame^] | 84 | const auditsDescription = ls |
| 85 | `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] | 86 | |
| 87 | const fragment = UI.Fragment.build` |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 88 | <div class="vbox audits-start-view"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 89 | <header> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 90 | <div class="audits-logo"></div> |
| 91 | <div class="audits-start-view-text"> |
Christy Chen | 1eea03c | 2019-06-26 19:31:24 +0000 | [diff] [blame^] | 92 | <h2>${ls`Audits`}</h2> |
| 93 | <p> |
| 94 | <span class="text">${auditsDescription}</span> |
| 95 | <span class="link" $="learn-more">${ls`Learn more`}</a> |
| 96 | </p> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 97 | </div> |
| 98 | </header> |
| 99 | <form> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 100 | <div class="audits-form-section"> |
| 101 | <div class="audits-form-section-label"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 102 | <i>${deviceIcon}</i> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 103 | <div class="audits-icon-label">${ls`Device`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 104 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 105 | <div class="audits-form-elements" $="device-type-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 106 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 107 | <div class="audits-form-section"> |
| 108 | <div class="audits-form-section-label"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 109 | <i>${categoriesIcon}</i> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 110 | <div class="audits-icon-label">${ls`Audits`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 111 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 112 | <div class="audits-form-elements" $="categories-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 113 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 114 | <div class="audits-form-section"> |
| 115 | <div class="audits-form-section-label"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 116 | <i>${throttlingIcon}</i> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 117 | <div class="audits-icon-label">${ls`Throttling`}</div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 118 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 119 | <div class="audits-form-elements" $="throttling-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 120 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 121 | <div class="audits-form-section"> |
| 122 | <div class="audits-form-section-label"></div> |
| 123 | <div class="audits-form-elements" $="other-form-elements"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 124 | </div> |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 125 | <div class="audits-form-section"> |
| 126 | <div class="audits-form-section-label"></div> |
| 127 | <div class="audits-form-elements audits-start-button-container hbox"> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 128 | ${this._startButton} |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 129 | <div $="help-text" class="audits-help-text hidden"></div> |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 130 | </div> |
| 131 | </div> |
| 132 | </form> |
| 133 | </div> |
| 134 | `; |
| 135 | |
| 136 | this._helpText = fragment.$('help-text'); |
| 137 | |
| 138 | const learnMoreLink = fragment.$('learn-more'); |
| 139 | learnMoreLink.addEventListener( |
| 140 | 'click', () => InspectorFrontendHost.openInNewTab('https://developers.google.com/web/tools/lighthouse/')); |
| 141 | |
| 142 | this._populateFormControls(fragment); |
| 143 | this.contentElement.appendChild(fragment.element()); |
| 144 | this.contentElement.style.overflow = 'auto'; |
| 145 | } |
| 146 | |
Patrick Hulce | 8d387f1 | 2018-05-29 18:54:54 +0000 | [diff] [blame] | 147 | focusStartButton() { |
| 148 | this._startButton.focus(); |
| 149 | } |
| 150 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 151 | /** |
| 152 | * @param {boolean} isEnabled |
| 153 | */ |
| 154 | setStartButtonEnabled(isEnabled) { |
| 155 | if (this._helpText) |
| 156 | this._helpText.classList.toggle('hidden', isEnabled); |
| 157 | |
| 158 | if (this._startButton) |
| 159 | this._startButton.disabled = !isEnabled; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param {?string} text |
| 164 | */ |
| 165 | setUnauditableExplanation(text) { |
| 166 | if (this._helpText) |
| 167 | this._helpText.textContent = text; |
| 168 | } |
| 169 | }; |