-
Notifications
You must be signed in to change notification settings - Fork 278
/
index.js
120 lines (103 loc) · 3.49 KB
/
index.js
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
/**
* The entry point for tota11y.
*
* Builds and mounts the toolbar.
*/
// Require the base tota11y styles right away so they can be overwritten
require("./less/tota11y.less");
const $ = require("jquery");
const plugins = require("./plugins");
const logoTemplate = require("./templates/logo.handlebars");
// Chrome Accessibility Developer Tools - required once as a global
require("script-loader!./node_modules/accessibility-developer-tools/dist/js/axs_testing.js");
class Toolbar {
constructor() {
this.activePlugin = null;
}
/**
* Manages the state of the toolbar when a plugin is clicked, and toggles
* the appropriate plugins on and off.
*/
handlePluginClick(plugin) {
// If the plugin was already selected, toggle it off
if (plugin === this.activePlugin) {
plugin.deactivate();
this.activePlugin = null;
} else {
// Deactivate the active plugin if there is one
if (this.activePlugin) {
this.activePlugin.deactivate();
}
// Activate the selected plugin
plugin.activate();
this.activePlugin = plugin;
}
}
/**
* Renders the toolbar and appends it to the specified element.
*/
appendTo($el) {
let $logo = $(logoTemplate());
let $toolbar;
let $defaultPlugins = plugins.default.map((Plugin) => { // eslint-disable-line no-unused-vars
return <Plugin onClick={this.handlePluginClick.bind(this)} />;
});
let $experimentalPlugins = null;
if (plugins.experimental.length) {
$experimentalPlugins = (
<li>
<div className="tota11y-plugins-separator">
Experimental
</div>
<ul>
{
plugins.experimental.map((Plugin) => { // eslint-disable-line no-unused-vars
return (
<Plugin onClick={this.handlePluginClick.bind(this)} />
);
})
}
</ul>
</li>
);
}
let $plugins = (
<ul className="tota11y-plugins">
{$defaultPlugins}
{$experimentalPlugins}
</ul>
);
let handleToggleClick = (e) => {
e.preventDefault();
e.stopPropagation();
$toolbar.toggleClass("tota11y-expanded");
$toolbar.attr("aria-expanded", $toolbar.is(".tota11y-expanded"));
};
let $toggle = (
<button aria-controls="tota11y-toolbar"
className="tota11y-toolbar-toggle"
onClick={handleToggleClick}
aria-label="[tota11y] Toggle menu">
<div aria-hidden="true" className="tota11y-toolbar-logo">
{$logo}
</div>
</button>
);
$toolbar = (
<div id="tota11y-toolbar" className="tota11y tota11y-toolbar"
role="region"
aria-expanded="false">
<div className="tota11y-toolbar-body">
{$plugins}
</div>
{$toggle}
</div>
);
$el.append($toolbar);
}
}
$(function() {
var bar = new Toolbar();
// TODO: Make this customizable
bar.appendTo($("body"));
});