blob: c8c822fe5a537762310da365b1897350df52186f [file] [log] [blame]
Patrick Hulcea087f622018-05-18 00:37:53 +00001// 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 */
8Audits2.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 Hulce05c18ce2018-05-24 00:34:56 +000023 _populateRuntimeSettingAsRadio(settingName, parentElement) {
Patrick Hulcea087f622018-05-18 00:37:53 +000024 const runtimeSetting = Audits2.RuntimeSettings.find(item => item.setting.name === settingName);
Patrick Hulce05c18ce2018-05-24 00:34:56 +000025 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 Hulcea087f622018-05-18 00:37:53 +000029 control.element.title = runtimeSetting.description;
30 parentElement.appendChild(control.element);
31 }
32
33 /**
Patrick Hulce05c18ce2018-05-24 00:34:56 +000034 * @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 Hulcea087f622018-05-18 00:37:53 +000048 * @param {!UI.Fragment} fragment
49 */
50 _populateFormControls(fragment) {
51 // Populate the device type
52 const deviceTypeFormElements = fragment.$('device-type-form-elements');
Patrick Hulce05c18ce2018-05-24 00:34:56 +000053 this._populateRuntimeSettingAsRadio('audits2.device_type', deviceTypeFormElements);
Patrick Hulcea087f622018-05-18 00:37:53 +000054
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 Hulce05c18ce2018-05-24 00:34:56 +000061 row.title = preset.description;
Patrick Hulcea087f622018-05-18 00:37:53 +000062 row.appendChild(checkbox.element);
Patrick Hulcea087f622018-05-18 00:37:53 +000063 }
64
65 // Populate the throttling
66 const throttlingFormElements = fragment.$('throttling-form-elements');
Patrick Hulce05c18ce2018-05-24 00:34:56 +000067 this._populateRuntimeSettingAsRadio('audits2.throttling', throttlingFormElements);
Patrick Hulcea087f622018-05-18 00:37:53 +000068
69
70 // Populate other settings
71 const otherFormElements = fragment.$('other-form-elements');
Patrick Hulce05c18ce2018-05-24 00:34:56 +000072 this._populateRuntimeSettingAsCheckbox('audits2.clear_storage', otherFormElements);
Patrick Hulcea087f622018-05-18 00:37:53 +000073 }
74
75 _render() {
76 this._startButton = UI.createTextButton(
Patrick Hulce05c18ce2018-05-24 00:34:56 +000077 ls`Run audits`, () => this._controller.dispatchEventToListeners(Audits2.Events.RequestAuditStart),
Patrick Hulcea087f622018-05-18 00:37:53 +000078 'audits2-start-button', true /* primary */);
Patrick Hulce05c18ce2018-05-24 00:34:56 +000079 this.setDefaultFocusedElement(this._startButton);
80
Patrick Hulcea087f622018-05-18 00:37:53 +000081 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 Hulcea087f622018-05-18 00:37:53 +000087 <header>
88 <div class="audits2-logo"></div>
89 <div class="audits2-start-view-text">
90 <h2>Audits</h2>
91 <p>
92 Identify and fix common problems that affect your site's performance, accessibility, and user experience.
Patrick Hulce05c18ce2018-05-24 00:34:56 +000093 <span class="link" $="learn-more">Learn more</a>
Patrick Hulcea087f622018-05-18 00:37:53 +000094 </p>
95 </div>
96 </header>
97 <form>
98 <div class="audits2-form-section">
99 <div class="audits2-form-section-label">
100 <i>${deviceIcon}</i>
101 <div class="audits2-icon-label">Device</div>
102 </div>
103 <div class="audits2-form-elements" $="device-type-form-elements"></div>
104 </div>
105 <div class="audits2-form-section">
106 <div class="audits2-form-section-label">
107 <i>${categoriesIcon}</i>
108 <div class="audits2-icon-label">Audits</div>
109 </div>
110 <div class="audits2-form-elements" $="categories-form-elements"></div>
111 </div>
112 <div class="audits2-form-section">
113 <div class="audits2-form-section-label">
114 <i>${throttlingIcon}</i>
115 <div class="audits2-icon-label">Throttling</div>
116 </div>
117 <div class="audits2-form-elements" $="throttling-form-elements"></div>
118 </div>
119 <div class="audits2-form-section">
120 <div class="audits2-form-section-label"></div>
121 <div class="audits2-form-elements" $="other-form-elements"></div>
122 </div>
123 <div class="audits2-form-section">
124 <div class="audits2-form-section-label"></div>
125 <div class="audits2-form-elements audits2-start-button-container hbox">
126 ${this._startButton}
127 <div $="help-text" class="audits2-help-text hidden"></div>
128 </div>
129 </div>
130 </form>
131 </div>
132 `;
133
134 this._helpText = fragment.$('help-text');
135
136 const learnMoreLink = fragment.$('learn-more');
137 learnMoreLink.addEventListener(
138 'click', () => InspectorFrontendHost.openInNewTab('https://developers.google.com/web/tools/lighthouse/'));
139
140 this._populateFormControls(fragment);
141 this.contentElement.appendChild(fragment.element());
142 this.contentElement.style.overflow = 'auto';
143 }
144
145 /**
146 * @param {boolean} isEnabled
147 */
148 setStartButtonEnabled(isEnabled) {
149 if (this._helpText)
150 this._helpText.classList.toggle('hidden', isEnabled);
151
152 if (this._startButton)
153 this._startButton.disabled = !isEnabled;
154 }
155
156 /**
157 * @param {?string} text
158 */
159 setUnauditableExplanation(text) {
160 if (this._helpText)
161 this._helpText.textContent = text;
162 }
163};