summaryrefslogtreecommitdiffstats
path: root/devtools/client/accessibility/accessibility-view.js
blob: 36b5aa9e6ec147088098550732b4c82fb9f85e44 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

/* global EVENTS */

const nodeConstants = require("resource://devtools/shared/dom-node-constants.js");

// React & Redux
const {
  createFactory,
  createElement,
} = require("resource://devtools/client/shared/vendor/react.js");
const ReactDOM = require("resource://devtools/client/shared/vendor/react-dom.js");
const {
  Provider,
} = require("resource://devtools/client/shared/vendor/react-redux.js");

// Accessibility Panel
const MainFrame = createFactory(
  require("resource://devtools/client/accessibility/components/MainFrame.js")
);

// Store
const createStore = require("resource://devtools/client/shared/redux/create-store.js");

// Reducers
const {
  reducers,
} = require("resource://devtools/client/accessibility/reducers/index.js");
const thunkOptions = { options: {} };
const store = createStore(reducers, {
  // Thunk options will be updated, when we [re]initialize the accessibility
  // view.
  thunkOptions,
});

// Actions
const {
  reset,
} = require("resource://devtools/client/accessibility/actions/ui.js");
const {
  select,
  highlight,
} = require("resource://devtools/client/accessibility/actions/accessibles.js");

/**
 * This object represents view of the Accessibility panel and is responsible
 * for rendering the content. It renders the top level ReactJS
 * component: the MainFrame.
 */
function AccessibilityView(localStore) {
  addEventListener("devtools/chrome/message", this.onMessage.bind(this), true);
  this.store = localStore;
}

AccessibilityView.prototype = {
  /**
   * Initialize accessibility view, create its top level component and set the
   * data store.
   *
   * @param {Object}
   *        Object that contains the following properties:
   * - supports                               {JSON}
   *                                          a collection of flags indicating
   *                                          which accessibility panel features
   *                                          are supported by the current
   *                                          serverside version.
   * - fluentBundles                          {Array}
   *                                          array of FluentBundles elements
   *                                          for localization
   * - toolbox                                {Object}
   *                                          devtools toolbox.
   * - getAccessibilityTreeRoot               {Function}
   *                                          Returns the topmost accessibiliity
   *                                          walker that is used as the root of
   *                                          the accessibility tree.
   * - startListeningForAccessibilityEvents   {Function}
   *                                          Add listeners for specific
   *                                          accessibility events.
   * - stopListeningForAccessibilityEvents    {Function}
   *                                          Remove listeners for specific
   *                                          accessibility events.
   * - audit                                  {Function}
   *                                          Audit function that will start
   *                                          accessibility audit for given types
   *                                          of accessibility issues.
   * - simulate                               {null|Function}
   *                                          Apply simulation of a given type
   *                                          (by setting color matrices in
   *                                          docShell).
   * - toggleDisplayTabbingOrder              {Function}
   *                                          Toggle the highlight of focusable
   *                                          elements along with their tabbing
   *                                          index.
   * - enableAccessibility                    {Function}
   *                                          Enable accessibility services.
   * - resetAccessiblity                      {Function}
   *                                          Reset the state of the
   *                                          accessibility services.
   * - startListeningForLifecycleEvents       {Function}
   *                                          Add listeners for accessibility
   *                                          service lifecycle events.
   * - stopListeningForLifecycleEvents        {Function}
   *                                          Remove listeners for accessibility
   *                                          service lifecycle events.
   * - startListeningForParentLifecycleEvents {Function}
   *                                          Add listeners for parent process
   *                                          accessibility service lifecycle
   *                                          events.
   * - stopListeningForParentLifecycleEvents  {Function}
   *                                          Remove listeners for parent
   *                                          process accessibility service
   *                                          lifecycle events.
   * - highlightAccessible                    {Function}
   *                                          Highlight accessible object.
   * - unhighlightAccessible                  {Function}
   *                                          Unhighlight accessible object.
   */
  async initialize({
    supports,
    fluentBundles,
    toolbox,
    getAccessibilityTreeRoot,
    startListeningForAccessibilityEvents,
    stopListeningForAccessibilityEvents,
    audit,
    simulate,
    toggleDisplayTabbingOrder,
    enableAccessibility,
    resetAccessiblity,
    startListeningForLifecycleEvents,
    stopListeningForLifecycleEvents,
    startListeningForParentLifecycleEvents,
    stopListeningForParentLifecycleEvents,
    highlightAccessible,
    unhighlightAccessible,
  }) {
    // Make sure state is reset every time accessibility panel is initialized.
    await this.store.dispatch(reset(resetAccessiblity, supports));
    const container = document.getElementById("content");
    const mainFrame = MainFrame({
      fluentBundles,
      toolbox,
      getAccessibilityTreeRoot,
      startListeningForAccessibilityEvents,
      stopListeningForAccessibilityEvents,
      audit,
      simulate,
      enableAccessibility,
      resetAccessiblity,
      startListeningForLifecycleEvents,
      stopListeningForLifecycleEvents,
      startListeningForParentLifecycleEvents,
      stopListeningForParentLifecycleEvents,
      highlightAccessible,
      unhighlightAccessible,
    });
    thunkOptions.options.toggleDisplayTabbingOrder = toggleDisplayTabbingOrder;
    // Render top level component
    const provider = createElement(Provider, { store: this.store }, mainFrame);
    window.once(EVENTS.PROPERTIES_UPDATED).then(() => {
      window.emit(EVENTS.INITIALIZED);
    });
    this.mainFrame = ReactDOM.render(provider, container);
  },

  destroy() {
    const container = document.getElementById("content");
    ReactDOM.unmountComponentAtNode(container);
  },

  async selectAccessible(accessible) {
    await this.store.dispatch(select(accessible));
    window.emit(EVENTS.NEW_ACCESSIBLE_FRONT_INSPECTED);
  },

  async highlightAccessible(accessible) {
    await this.store.dispatch(highlight(accessible));
    window.emit(EVENTS.NEW_ACCESSIBLE_FRONT_HIGHLIGHTED);
  },

  async selectNodeAccessible(node) {
    if (!node) {
      return;
    }

    const accessibilityFront = await node.targetFront.getFront("accessibility");
    const accessibleWalkerFront = await accessibilityFront.getWalker();
    let accessible = await accessibleWalkerFront.getAccessibleFor(node);
    if (accessible) {
      await accessible.hydrate();
    }

    // If node does not have an accessible object, try to find node's child text node and
    // try to retrieve an accessible object for that child instead. This is the best
    // effort approach until there's accessibility API to retrieve accessible object at
    // point.
    if (!accessible || accessible.indexInParent < 0) {
      const { nodes: children } = await node.walkerFront.children(node);
      for (const child of children) {
        if (child.nodeType === nodeConstants.TEXT_NODE) {
          accessible = await accessibleWalkerFront.getAccessibleFor(child);
          // indexInParent property is only available with additional request
          // for data (hydration) about the accessible object.
          if (accessible) {
            await accessible.hydrate();
            if (accessible.indexInParent >= 0) {
              break;
            }
          }
        }
      }
    }

    // Attempt to find closest accessible ancestor for a given node.
    if (!accessible || accessible.indexInParent < 0) {
      let parentNode = node.parentNode();
      while (parentNode) {
        accessible = await accessibleWalkerFront.getAccessibleFor(parentNode);
        if (accessible) {
          await accessible.hydrate();
          if (accessible.indexInParent >= 0) {
            break;
          }
        }

        parentNode = parentNode.parentNode();
      }
    }

    // Do not set the selected state if there is no corresponding accessible.
    if (!accessible) {
      console.warn(
        `No accessible object found for a node or a node in its ancestry: ${node.actorID}`
      );
      return;
    }

    await this.store.dispatch(select(accessible));
    window.emit(EVENTS.NEW_ACCESSIBLE_FRONT_INSPECTED);
  },

  /**
   * Process message from accessibility panel.
   *
   * @param {Object} event  message type and data.
   */
  onMessage(event) {
    const data = event.data;
    const method = data.type;

    if (typeof this[method] === "function") {
      this[method](...data.args);
    }
  },
};

window.view = new AccessibilityView(store);