UNPKG

4.55 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.promise.js");
4
5require("core-js/modules/es.object.to-string.js");
6
7require("regenerator-runtime/runtime.js");
8
9var _global = _interopRequireDefault(require("global"));
10
11var _axeCore = _interopRequireDefault(require("axe-core"));
12
13var _addons = require("@storybook/addons");
14
15var _constants = require("./constants");
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
20
21function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
22
23var document = _global.default.document,
24 globalWindow = _global.default.window;
25
26if (module && module.hot && module.hot.decline) {
27 module.hot.decline();
28}
29
30var channel = _addons.addons.getChannel(); // Holds axe core running state
31
32
33var active = false; // Holds latest story we requested a run
34
35var activeStoryId;
36
37var getElement = function getElement() {
38 var storyRoot = document.getElementById('story-root');
39 return storyRoot ? storyRoot.children : document.getElementById('root');
40};
41/**
42 * Handle A11yContext events.
43 * Because the event are sent without manual check, we split calls
44 */
45
46
47var handleRequest = function handleRequest(storyId) {
48 var _getParams = getParams(storyId),
49 manual = _getParams.manual;
50
51 if (!manual) {
52 run(storyId);
53 }
54};
55
56var run = /*#__PURE__*/function () {
57 var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(storyId) {
58 var input, _input$element, element, config, _input$options, options, result;
59
60 return regeneratorRuntime.wrap(function _callee$(_context) {
61 while (1) {
62 switch (_context.prev = _context.next) {
63 case 0:
64 activeStoryId = storyId;
65 _context.prev = 1;
66 input = getParams(storyId);
67
68 if (active) {
69 _context.next = 13;
70 break;
71 }
72
73 active = true;
74 channel.emit(_constants.EVENTS.RUNNING);
75 _input$element = input.element, element = _input$element === void 0 ? getElement() : _input$element, config = input.config, _input$options = input.options, options = _input$options === void 0 ? {} : _input$options;
76
77 _axeCore.default.reset();
78
79 if (config) {
80 _axeCore.default.configure(config);
81 }
82
83 _context.next = 11;
84 return _axeCore.default.run(element, options);
85
86 case 11:
87 result = _context.sent;
88
89 // It's possible that we requested a new run on a different story.
90 // Unfortunately, axe doesn't support a cancel method to abort current run.
91 // We check if the story we run against is still the current one,
92 // if not, trigger a new run using the current story
93 if (activeStoryId === storyId) {
94 channel.emit(_constants.EVENTS.RESULT, result);
95 } else {
96 active = false;
97 run(activeStoryId);
98 }
99
100 case 13:
101 _context.next = 18;
102 break;
103
104 case 15:
105 _context.prev = 15;
106 _context.t0 = _context["catch"](1);
107 channel.emit(_constants.EVENTS.ERROR, _context.t0);
108
109 case 18:
110 _context.prev = 18;
111 active = false;
112 return _context.finish(18);
113
114 case 21:
115 case "end":
116 return _context.stop();
117 }
118 }
119 }, _callee, null, [[1, 15, 18, 21]]);
120 }));
121
122 return function run(_x) {
123 return _ref.apply(this, arguments);
124 };
125}();
126/** Returns story parameters or default ones. */
127
128
129var getParams = function getParams(storyId) {
130 var _ref2 = globalWindow.__STORYBOOK_STORY_STORE__.fromId(storyId) || {},
131 parameters = _ref2.parameters;
132
133 return parameters.a11y || {
134 config: {},
135 options: {
136 restoreScroll: true
137 }
138 };
139};
140
141channel.on(_constants.EVENTS.REQUEST, handleRequest);
142channel.on(_constants.EVENTS.MANUAL, run);
\No newline at end of file