forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevelopment-ui.js
More file actions
326 lines (284 loc) · 8.61 KB
/
development-ui.js
File metadata and controls
326 lines (284 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {LogLevel, dev} from '../../../src/log';
import {Services} from '../../../src/services';
import {isArray} from '../../../src/types';
import {removeChildren} from '../../../src/dom';
import {toggle} from '../../../src/style';
/**
* @param {!../../../src/service/vsync-impl.Vsync} vsync
* @param {!Element} el
* @param {boolean} isHidden
*/
function toggleHiddenAttribute(vsync, el, isHidden) {
vsync.mutate(() => {
toggle(el, !isHidden);
});
}
/**
* @param {!Window} win
* @param {string|!Array<string>} classNameOrList
* @param {function(Event)} handler
* @return {!Element}
*/
function createButton(win, classNameOrList, handler) {
const button = win.document.createElement('div');
button.setAttribute('role', 'button');
if (isArray(classNameOrList)) {
classNameOrList.forEach((className) => button.classList.add(className));
} else {
button.classList.add(/** @type {string} */ (classNameOrList));
}
button.classList.add('i-amphtml-story-button');
button.addEventListener('click', handler);
return button;
}
/**
* Development mode logs buttons.
*/
export class DevelopmentModeLogButtonSet {
/**
* @param {!Window} win
*/
constructor(win) {
/** @private @const {!Window} */
this.win_ = win;
/** @private {?Element} */
this.root_ = null;
/** @private {?Element} */
this.errorButton_ = null;
/** @private {?Element} */
this.warningButton_ = null;
/** @private {?Element} */
this.successButton_ = null;
}
/**
* @param {!Window} win
* @return {!DevelopmentModeLogButtonSet}
*/
static create(win) {
return new DevelopmentModeLogButtonSet(win);
}
/**
* Builds the developer log button set element.
* @param {function()} logButtonActionFn A callback function to be invoked when
* the log buttons are clicked.
* @return {?Element}
*/
build(logButtonActionFn) {
this.errorButton_ = createButton(
this.win_,
['i-amphtml-story-error-button', 'i-amphtml-story-dev-logs-button'],
() => logButtonActionFn()
);
this.warningButton_ = createButton(
this.win_,
['i-amphtml-story-warning-button', 'i-amphtml-story-dev-logs-button'],
() => logButtonActionFn()
);
this.successButton_ = createButton(
this.win_,
['i-amphtml-story-success-button', 'i-amphtml-story-dev-logs-button'],
() => logButtonActionFn()
);
this.root_ = this.win_.document.createElement('div');
this.root_.appendChild(this.errorButton_);
this.root_.appendChild(this.warningButton_);
this.root_.appendChild(this.successButton_);
return this.root_;
}
/**
* Gets the button associated to a given log entry.
* @param {!./logging.AmpStoryLogEntryDef} logEntry The log entry for which
* the associated button shouldbe retrieved.
* @return {?Element} The button associated to the specified log entry, if one
* exists.
* @private
*/
getButtonForLogEntry_(logEntry) {
if (logEntry.conforms) {
return this.successButton_;
}
switch (logEntry.level) {
case LogLevel.ERROR:
return this.errorButton_;
case LogLevel.WARN:
return this.warningButton_;
default:
return null;
}
}
/**
* Logs an individual entry into the developer log.
* @param {!./logging.AmpStoryLogEntryDef} logEntry The entry to log.
*/
log(logEntry) {
const button = this.getButtonForLogEntry_(logEntry);
if (!button) {
return;
}
const oldCount = parseInt(button.getAttribute('data-count') || 0, 10);
button.setAttribute('data-count', oldCount + 1);
}
/**
* Clears any error state held by the buttons.
*/
clear() {
this.errorButton_.setAttribute('data-count', 0);
this.warningButton_.setAttribute('data-count', 0);
this.successButton_.setAttribute('data-count', 0);
}
}
/**
* Development mode log for <amp-story>.
*/
export class DevelopmentModeLog {
/**
* @param {!Window} win
*/
constructor(win) {
/** @private @const {!Window} */
this.win_ = win;
/** @private {?Element} */
this.root_ = null;
/** @private {?Element} */
this.entriesEl_ = null;
/** @private {?Element} */
this.contextStringEl_ = null;
}
/**
* @param {!Window} win
* @return {!DevelopmentModeLog}
*/
static create(win) {
return new DevelopmentModeLog(win);
}
/**
* Builds the developer log element.
* @return {?Element}
*/
build() {
this.contextStringEl_ = this.win_.document.createElement('span');
this.contextStringEl_.classList.add(
'i-amphtml-story-developer-log-context'
);
const titleEl = this.win_.document.createElement('div');
titleEl.textContent = 'Developer logs for page ';
titleEl.appendChild(this.contextStringEl_);
const closeDeveloperLogEl = createButton(
this.win_,
'i-amphtml-story-developer-log-close',
() => this.hide()
);
const headerEl = this.win_.document.createElement('div');
headerEl.classList.add('i-amphtml-story-developer-log-header');
headerEl.appendChild(titleEl);
headerEl.appendChild(closeDeveloperLogEl);
this.entriesEl_ = this.win_.document.createElement('ul');
this.entriesEl_.classList.add('i-amphtml-story-developer-log-entries');
this.root_ = this.win_.document.createElement('div');
this.root_.classList.add('i-amphtml-story-developer-log');
toggle(this.root_, false);
this.root_.appendChild(headerEl);
this.root_.appendChild(this.entriesEl_);
this.clear();
return this.root_;
}
/**
* @param {!LogLevel} logLevel
* @return {?string} The CSS class to be applied to the log entry, given the
* specified log level, or null if no class should be added.
* @private
*/
getCssLogLevelClass_(logLevel) {
switch (logLevel) {
case LogLevel.WARN:
return 'i-amphtml-story-developer-log-entry-warning';
case LogLevel.ERROR:
return 'i-amphtml-story-developer-log-entry-error';
default:
return null;
}
}
/**
* @param {boolean} conforms Whether the log entry is for an element that
* conforms to a best practice.
* @return {?string} The CSS class to be applied to the log entry, given the
* element's conformance to a best practice, or null if no class should be
* added.
* @private
*/
getCssConformanceClass_(conforms) {
if (conforms) {
return 'i-amphtml-story-developer-log-entry-success';
}
return null;
}
/**
* @param {!./logging.AmpStoryLogEntryDef} logEntry The entry to be logged.
*/
log(logEntry) {
const logLevelClass = this.getCssLogLevelClass_(logEntry.level);
const conformanceClass = this.getCssConformanceClass_(logEntry.conforms);
const logEntryUi = this.win_.document.createElement('li');
logEntryUi.classList.add('i-amphtml-story-developer-log-entry');
if (logLevelClass) {
logEntryUi.classList.add(logLevelClass);
}
if (conformanceClass) {
logEntryUi.classList.add(conformanceClass);
}
logEntryUi.textContent = logEntry.message;
this.entriesEl_.appendChild(logEntryUi);
}
/**
* Clears all entries from the developer logs.
*/
clear() {
Services.vsyncFor(this.win_).mutate(() => {
removeChildren(dev().assertElement(this.entriesEl_));
});
}
/**
* Sets the string providing context for the developer logs window. This is
* often the name or ID of the element that all logs are for (e.g. the page).
* @param {string} contextString
*/
setContextString(contextString) {
this.contextStringEl_.textContent = contextString;
}
/**
* Toggles the visibility of the developer log.
*/
toggle() {
const newHiddenState = !this.root_.hasAttribute('hidden');
toggleHiddenAttribute(
Services.vsyncFor(this.win_),
dev().assertElement(this.root_),
newHiddenState
);
}
/**
* Hides the developer log in the UI.
*/
hide() {
toggleHiddenAttribute(
Services.vsyncFor(this.win_),
dev().assertElement(this.root_),
/* isHidden */ true
);
}
}