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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
let lastChanged = null;
let lastFocused = null;
const restartButton = $('experiment-restart-button');
/** @type {?function():void} */
let experimentalFeaturesResolver = null;
/** @type {!Promise} */
const experimentalFeaturesReady = new Promise(resolve => {
experimentalFeaturesResolver = resolve;
});
/**
* This variable structure is here to document the structure that the template
* expects to correctly populate the page.
*/
/**
* Takes the |experimentalFeaturesData| input argument which represents data
* about all the current feature entries and populates the html jstemplate with
* that data. It expects an object structure like the above.
* @param {!ExperimentalFeaturesData} experimentalFeaturesData Information about
* all experiments. See returnFlagsExperiments() for the structure of this
* object.
*/
function renderTemplate(experimentalFeaturesData) {
const templateToProcess = jstGetTemplate('tab-content-available-template');
const context = new JsEvalContext(experimentalFeaturesData);
const content = $('tab-content-available');
// Duplicate the template into the content area.
// This prevents the misrendering of available flags when the template
// is rerendered. Example - resetting flags.
content.textContent = '';
content.appendChild(templateToProcess);
// Process the templates: available / unavailable flags.
jstProcess(context, templateToProcess);
// Unavailable flags are not shown on iOS.
const unavailableTemplate = $('tab-content-unavailable');
if (unavailableTemplate) {
jstProcess(context, $('tab-content-unavailable'));
}
showRestartToast(experimentalFeaturesData.needsRestart);
// Add handlers to dynamically created HTML elements.
let elements = document.getElementsByClassName('experiment-select');
for (const element of elements) {
element.onchange = function() {
const selectElement = /** @type {!HTMLSelectElement} */ (element);
handleSelectExperimentalFeatureChoice(
selectElement, selectElement.selectedIndex);
lastChanged = element;
return false;
};
registerFocusEvents(element);
}
elements = document.getElementsByClassName('experiment-enable-disable');
for (const element of elements) {
element.onchange = function() {
const selectElement = /** @type {!HTMLSelectElement} */ (element);
handleEnableExperimentalFeature(
selectElement,
selectElement.options[selectElement.selectedIndex].value ==
'enabled');
lastChanged = selectElement;
return false;
};
registerFocusEvents(element);
}
elements = document.getElementsByClassName('experiment-origin-list-value');
for (const element of elements) {
element.onchange = function() {
handleSetOriginListFlag(element, element.value);
return false;
};
}
assert(restartButton || cr.isIOS);
if (restartButton) {
restartButton.onclick = restartBrowser;
}
// Tab panel selection.
const tabEls = document.getElementsByClassName('tab');
for (let i = 0; i < tabEls.length; ++i) {
tabEls[i].addEventListener('click', function(e) {
e.preventDefault();
for (let j = 0; j < tabEls.length; ++j) {
tabEls[j].parentNode.classList.toggle('selected', tabEls[j] === this);
tabEls[j].setAttribute('aria-selected', tabEls[j] === this);
}
FlagSearch.getInstance().announceSearchResults();
});
}
const smallScreenCheck = window.matchMedia('(max-width: 480px)');
// Toggling of experiment description overflow content on smaller screens.
if(smallScreenCheck.matches){
elements = document.querySelectorAll('.experiment .flex:first-child');
for (const element of elements) {
element.onclick = () => element.classList.toggle('expand');
}
}
$('experiment-reset-all').onclick = resetAllFlags;
highlightReferencedFlag();
const search = FlagSearch.getInstance();
search.init();
}
/**
* Add events to an element in order to keep track of the last focused element.
* Focus restart button if a previous focus target has been set and tab key
* pressed.
* @param {Element} el Element to bind events to.
*/
function registerFocusEvents(el) {
el.addEventListener('keydown', function(e) {
if (lastChanged && e.key === 'Tab' && !e.shiftKey) {
lastFocused = lastChanged;
e.preventDefault();
restartButton.focus();
}
});
el.addEventListener('blur', function() {
lastChanged = null;
});
}
/**
* Highlight an element associated with the page's location's hash. We need to
* fake fragment navigation with '.scrollIntoView()', since the fragment IDs
* don't actually exist until after the template code runs; normal navigation
* therefore doesn't work.
*/
function highlightReferencedFlag() {
if (window.location.hash) {
const el = document.querySelector(window.location.hash);
if (el && !el.classList.contains('referenced')) {
// Unhighlight whatever's highlighted.
if (document.querySelector('.referenced')) {
document.querySelector('.referenced').classList.remove('referenced');
}
// Highlight the referenced element.
el.classList.add('referenced');
// Switch to unavailable tab if the flag is in this section.
if ($('tab-content-unavailable').contains(el)) {
$('tab-available').parentNode.classList.remove('selected');
$('tab-available').setAttribute('aria-selected', 'false');
$('tab-unavailable').parentNode.classList.add('selected');
$('tab-unavailable').setAttribute('aria-selected', 'true');
}
el.scrollIntoView();
}
}
}
/**
* Gets details and configuration about the available features. The
* |returnExperimentalFeatures()| will be called with reply.
*/
function requestExperimentalFeaturesData() {
chrome.send('requestExperimentalFeatures');
}
/** Restart browser and restore tabs. */
function restartBrowser() {
chrome.send('restartBrowser');
}
/**
* Cause a text string to be announced by screen readers
* @param {string} text The text that should be announced.
*/
function announceStatus(text) {
$('screen-reader-status-message').textContent = '';
setTimeout(function() {
$('screen-reader-status-message').textContent = text;
}, 100);
}
/** Reset all flags to their default values and refresh the UI. */
function resetAllFlags() {
chrome.send('resetAllFlags');
FlagSearch.getInstance().clearSearch();
announceStatus(loadTimeData.getString("reset-acknowledged"));
showRestartToast(true);
requestExperimentalFeaturesData();
}
/**
* Show the restart toast.
* @param {boolean} show Setting to toggle showing / hiding the toast.
*/
function showRestartToast(show) {
$('needs-restart').classList.toggle('show', show);
const restartButton = $('experiment-restart-button');
if (restartButton) {
restartButton.setAttribute("tabindex", show ? '9' : '-1');
}
if (show) {
$('needs-restart').setAttribute("role", "alert");
}
}
/**
* @typedef {{
* internal_name: string,
* name: string,
* description: string,
* enabled: boolean,
* is_default: boolean,
* choices: ?Array<{internal_name: string, description: string, selected:
* boolean}>, supported_platforms: !Array<string>
* }}
*/
let Feature;
/**
* @typedef {{
* supportedFeatures: !Array<!Feature>,
* unsupportedFeatures: !Array<!Feature>,
* needsRestart: boolean,
* showBetaChannelPromotion: boolean,
* showDevChannelPromotion: boolean,
* showOwnerWarning: boolean
* }}
*/
let ExperimentalFeaturesData;
/**
* Called by the WebUI to re-populate the page with data representing the
* current state of all experimental features.
* @param {ExperimentalFeaturesData} experimentalFeaturesData Information about
* all experimental
* features in the following format:
* {
* supportedFeatures: [
* {
* internal_name: 'Feature ID string',
* name: 'Feature name',
* description: 'Description',
* // enabled and default are only set if the feature is single valued.
* // enabled is true if the feature is currently enabled.
* // is_default is true if the feature is in its default state.
* enabled: true,
* is_default: false,
* // choices is only set if the entry has multiple values.
* choices: [
* {
* internal_name: 'Experimental feature ID string',
* description: 'description',
* selected: true
* }
* ],
* supported_platforms: [
* 'Mac',
* 'Linux'
* ],
* }
* ],
* unsupportedFeatures: [
* // Mirrors the format of |supportedFeatures| above.
* ],
* needsRestart: false,
* showBetaChannelPromotion: false,
* showDevChannelPromotion: false,
* showOwnerWarning: false
* }
*/
function returnExperimentalFeatures(experimentalFeaturesData) {
const bodyContainer = $('body-container');
renderTemplate(experimentalFeaturesData);
if (experimentalFeaturesData.showBetaChannelPromotion) {
$('channel-promo-beta').hidden = false;
} else if (experimentalFeaturesData.showDevChannelPromotion) {
$('channel-promo-dev').hidden = false;
}
$('promos').hidden = !experimentalFeaturesData.showBetaChannelPromotion &&
!experimentalFeaturesData.showDevChannelPromotion;
bodyContainer.style.visibility = 'visible';
const ownerWarningDiv = $('owner-warning');
if (ownerWarningDiv) {
ownerWarningDiv.hidden = !experimentalFeaturesData.showOwnerWarning;
}
experimentalFeaturesResolver();
}
/**
* Handles updating the UI after experiment selections have been made.
* Adds or removes experiment highlighting depending on whether the experiment
* is set to the default option then shows the restart button.
* @param {HTMLElement} node The select node for the experiment being changed.
* @param {number} index The selected option index.
*/
function experimentChangesUiUpdates(node, index) {
const selected = node.options[index];
/** @suppress {missingProperties} */
const experimentContainerEl = $(node.internal_name).firstElementChild;
const isDefault =
('default' in selected.dataset && selected.dataset.default === '1') ||
(!('default' in selected.dataset) && index === 0);
experimentContainerEl.classList.toggle('experiment-default', isDefault);
experimentContainerEl.classList.toggle('experiment-switched', !isDefault);
showRestartToast(true);
}
/**
* Handles a 'enable' or 'disable' button getting clicked.
* @param {HTMLElement} node The node for the experiment being changed.
* @param {boolean} enable Whether to enable or disable the experiment.
* @suppress {missingProperties}
*/
function handleEnableExperimentalFeature(node, enable) {
/* This function is an onchange handler, which can be invoked during page
* restore - see https://crbug.com/1038638. */
if (!node.internal_name) {
return;
}
chrome.send('enableExperimentalFeature', [String(node.internal_name),
String(enable)]);
experimentChangesUiUpdates(node, enable ? 1 : 0);
}
/** @suppress {missingProperties} */
function handleSetOriginListFlag(node, value) {
/* This function is an onchange handler, which can be invoked during page
* restore - see https://crbug.com/1038638. */
if (!node.internal_name) {
return;
}
chrome.send('setOriginListFlag', [String(node.internal_name), String(value)]);
showRestartToast(true);
}
/**
* Invoked when the selection of a multi-value choice is changed to the
* specified index.
* @param {HTMLElement} node The node for the experiment being changed.
* @param {number} index The index of the option that was selected.
* @suppress {missingProperties}
*/
function handleSelectExperimentalFeatureChoice(node, index) {
/* This function is an onchange handler, which can be invoked during page
* restore - see https://crbug.com/1038638. */
if (!node.internal_name) {
return;
}
chrome.send('enableExperimentalFeature',
[String(node.internal_name) + '@' + index, 'true']);
experimentChangesUiUpdates(node, index);
}
/** @type {!FlagSearch.SearchContent} */
const emptySearchContent = Object.freeze({
link: null,
title: null,
description: null,
});
/**
* Handles in page searching. Matches against the experiment flag name.
* @constructor
*/
const FlagSearch = function() {
FlagSearch.instance_ = this;
/** @private {!FlagSearch.SearchContent} */
this.experiments_ = /** @type {FlagSearch.SearchContent} */ (
Object.assign({}, emptySearchContent));
/** @private {!FlagSearch.SearchContent} */
this.unavailableExperiments_ = /** @type {FlagSearch.SearchContent} */ (
Object.assign({}, emptySearchContent));
this.searchBox_ = $('search');
this.noMatchMsg_ = document.querySelectorAll('.tab-content .no-match');
/** @private {?number} */
this.searchIntervalId_ = null;
this.initialized = false;
};
// Delay in ms following a keypress, before a search is made.
FlagSearch.SEARCH_DEBOUNCE_TIME_MS = 150;
/**
* Object definition for storing the elements which are searched on.
* @typedef {{
* description: ?NodeList<!HTMLElement>,
* link: ?NodeList<!HTMLElement>,
* title: ?NodeList<!HTMLElement>
* }}
*/
FlagSearch.SearchContent;
/**
* Get the singleton instance of FlagSearch.
* @return {Object} Instance of FlagSearch.
*/
FlagSearch.getInstance = function() {
if (FlagSearch.instance_) {
return FlagSearch.instance_;
} else {
return new FlagSearch();
}
};
FlagSearch.prototype = {
/**
* Initialises the in page search. Adding searchbox listeners and
* collates the text elements used for string matching.
*/
init() {
this.experiments_.link = /** @type {!NodeList<!HTMLElement>} */ (
document.querySelectorAll('#tab-content-available .permalink'));
this.experiments_.title = /** @type {!NodeList<!HTMLElement>} */ (
document.querySelectorAll('#tab-content-available .experiment-name'));
this.experiments_.description = /** @type {!NodeList<!HTMLElement>} */ (
document.querySelectorAll('#tab-content-available p'));
this.unavailableExperiments_.link = /** @type {!NodeList<!HTMLElement>} */ (
document.querySelectorAll('#tab-content-unavailable .permalink'));
this.unavailableExperiments_.title =
/** @type {!NodeList<!HTMLElement>} */ (document.querySelectorAll(
'#tab-content-unavailable .experiment-name'));
this.unavailableExperiments_.description =
/** @type {!NodeList<!HTMLElement>} */ (
document.querySelectorAll('#tab-content-unavailable p'));
if (!this.initialized) {
this.searchBox_.addEventListener('input', this.debounceSearch.bind(this));
document.querySelector('.clear-search').addEventListener('click',
this.clearSearch.bind(this));
window.addEventListener('keyup', function(e) {
if (document.activeElement.nodeName === 'TEXTAREA') {
return;
}
switch (e.key) {
case '/':
this.searchBox_.focus();
break;
case 'Escape':
case 'Enter':
this.searchBox_.blur();
break;
}
}.bind(this));
this.searchBox_.focus();
this.initialized = true;
}
},
/**
* Clears a search showing all experiments.
*/
clearSearch() {
this.searchBox_.value = '';
this.doSearch();
},
/**
* Reset existing highlights on an element.
* @param {HTMLElement} el The element to remove all highlighted mark up on.
* @param {string} text Text to reset the element's textContent to.
*/
resetHighlights(el, text) {
if (el.children) {
el.textContent = text;
}
},
/**
* Highlights the search term within a given element.
* @param {string} searchTerm Search term user entered.
* @param {HTMLElement} el The node containing the text to match against.
* @return {boolean} Whether there was a match.
*/
highlightMatchInElement(searchTerm, el) {
// Experiment container.
const parentEl = el.parentNode.parentNode.parentNode;
const text = el.textContent;
const match = text.toLowerCase().indexOf(searchTerm);
parentEl.classList.toggle('hidden', match === -1);
if (match === -1) {
this.resetHighlights(el, text);
return false;
}
if (searchTerm !== '') {
// Clear all nodes.
el.textContent = '';
if (match > 0) {
const textNodePrefix =
document.createTextNode(text.substring(0, match));
el.appendChild(textNodePrefix);
}
const matchEl = document.createElement('mark');
matchEl.textContent = text.substr(match, searchTerm.length);
el.appendChild(matchEl);
const matchSuffix = text.substring(match + searchTerm.length);
if (matchSuffix) {
const textNodeSuffix = document.createTextNode(matchSuffix);
el.appendChild(textNodeSuffix);
}
} else {
this.resetHighlights(el, text);
}
return true;
},
/**
* Goes through all experiment text and highlights the relevant matches.
* Only the first instance of a match in each experiment text block is
* highlighted. This prevents the sea of yellow that happens using the global
* find in page search.
* @param {FlagSearch.SearchContent} searchContent Object containing the
* experiment text elements to search against.
* @param {string} searchTerm
* @return {number} The number of matches found.
*/
highlightAllMatches(searchContent, searchTerm) {
let matches = 0;
for (let i = 0, j = searchContent.link.length; i < j; i++) {
if (this.highlightMatchInElement(searchTerm, searchContent.title[i])) {
this.resetHighlights(searchContent.description[i],
searchContent.description[i].textContent);
this.resetHighlights(searchContent.link[i],
searchContent.link[i].textContent);
matches++;
continue;
}
if (this.highlightMatchInElement(searchTerm,
searchContent.description[i])) {
this.resetHighlights(searchContent.title[i],
searchContent.title[i].textContent);
this.resetHighlights(searchContent.link[i],
searchContent.link[i].textContent);
matches++;
continue;
}
// Match links, replace spaces with hyphens as flag names don't
// have spaces.
if (this.highlightMatchInElement(searchTerm.replace(/\s/, '-'),
searchContent.link[i])) {
this.resetHighlights(searchContent.title[i],
searchContent.title[i].textContent);
this.resetHighlights(searchContent.description[i],
searchContent.description[i].textContent);
matches++;
}
}
return matches;
},
/**
* Performs a search against the experiment title, description, permalink.
*/
doSearch() {
const searchTerm = this.searchBox_.value.trim().toLowerCase();
if (searchTerm || searchTerm === '') {
document.body.classList.toggle('searching', searchTerm);
// Available experiments
this.noMatchMsg_[0].classList.toggle(
'hidden',
this.highlightAllMatches(this.experiments_, searchTerm) > 0);
// Unavailable experiments
this.noMatchMsg_[1].classList.toggle(
'hidden',
this.highlightAllMatches(this.unavailableExperiments_, searchTerm) >
0);
this.announceSearchResults();
}
this.searchIntervalId_ = null;
},
announceSearchResults() {
const searchTerm = this.searchBox_.value.trim().toLowerCase();
if (!searchTerm) {
return;
}
let tabAvailable = true;
const tabEls = document.getElementsByClassName('tab');
for (let i = 0; i < tabEls.length; ++i) {
if (tabEls[i].parentNode.classList.contains('selected')) {
tabAvailable = tabEls[i].id === 'tab-available';
}
}
const seletedTabId =
tabAvailable ? '#tab-content-available' : '#tab-content-unavailable';
const queryString = seletedTabId + ' .experiment:not(.hidden)';
const total = document.querySelectorAll(queryString).length;
if (total) {
announceStatus(
total === 1 ?
loadTimeData.getStringF('searchResultsSingular', searchTerm) :
loadTimeData.getStringF(
'searchResultsPlural', total, searchTerm));
}
},
/**
* Debounces the search to improve performance and prevent too many searches
* from being initiated.
*/
debounceSearch() {
if (this.searchIntervalId_) {
clearTimeout(this.searchIntervalId_);
}
this.searchIntervalId_ = setTimeout(this.doSearch.bind(this),
FlagSearch.SEARCH_DEBOUNCE_TIME_MS);
}
};
/**
* Allows the restart button to jump back to the previously focused experiment
* in the list instead of going to the top of the page.
*/
function setupRestartButton() {
restartButton.addEventListener('keydown', function(e) {
if (e.shiftKey && e.key === 'Tab' && lastFocused) {
e.preventDefault();
lastFocused.focus();
}
});
restartButton.addEventListener('blur', () => {
lastFocused = null;
});
}
document.addEventListener('DOMContentLoaded', function() {
// Get and display the data upon loading.
requestExperimentalFeaturesData();
setupRestartButton();
cr.ui.FocusOutlineManager.forDocument(document);
});
// Update the highlighted flag when the hash changes.
window.addEventListener('hashchange', highlightReferencedFlag);
|