UNPKG

10.7 kBJavaScriptView Raw
1(function (global, factory) {
2 if (typeof define === "function" && define.amd) {
3 define(["exports", "./navigation-menu-panel", "../../globals/js/misc/on", "../../globals/js/settings", "../../globals/js/misc/on-focus-by-keyboard"], factory);
4 } else if (typeof exports !== "undefined") {
5 factory(exports, require("./navigation-menu-panel"), require("../../globals/js/misc/on"), require("../../globals/js/settings"), require("../../globals/js/misc/on-focus-by-keyboard"));
6 } else {
7 var mod = {
8 exports: {}
9 };
10 factory(mod.exports, global.navigationMenuPanel, global.on, global.settings, global.onFocusByKeyboard);
11 global.productSwitcher = mod.exports;
12 }
13})(this, function (_exports, _navigationMenuPanel, _on, _settings, _onFocusByKeyboard) {
14 "use strict";
15
16 Object.defineProperty(_exports, "__esModule", {
17 value: true
18 });
19 _exports.default = void 0;
20 _navigationMenuPanel = _interopRequireDefault(_navigationMenuPanel);
21 _on = _interopRequireDefault(_on);
22 _settings = _interopRequireDefault(_settings);
23 _onFocusByKeyboard = _interopRequireDefault(_onFocusByKeyboard);
24
25 function _interopRequireDefault(obj) {
26 return obj && obj.__esModule ? obj : {
27 default: obj
28 };
29 }
30
31 function _typeof(obj) {
32 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
33 _typeof = function _typeof(obj) {
34 return typeof obj;
35 };
36 } else {
37 _typeof = function _typeof(obj) {
38 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
39 };
40 }
41
42 return _typeof(obj);
43 }
44
45 function _classCallCheck(instance, Constructor) {
46 if (!(instance instanceof Constructor)) {
47 throw new TypeError("Cannot call a class as a function");
48 }
49 }
50
51 function _defineProperties(target, props) {
52 for (var i = 0; i < props.length; i++) {
53 var descriptor = props[i];
54 descriptor.enumerable = descriptor.enumerable || false;
55 descriptor.configurable = true;
56 if ("value" in descriptor) descriptor.writable = true;
57 Object.defineProperty(target, descriptor.key, descriptor);
58 }
59 }
60
61 function _createClass(Constructor, protoProps, staticProps) {
62 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
63 if (staticProps) _defineProperties(Constructor, staticProps);
64 return Constructor;
65 }
66
67 function _possibleConstructorReturn(self, call) {
68 if (call && (_typeof(call) === "object" || typeof call === "function")) {
69 return call;
70 }
71
72 return _assertThisInitialized(self);
73 }
74
75 function _assertThisInitialized(self) {
76 if (self === void 0) {
77 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
78 }
79
80 return self;
81 }
82
83 function _get(target, property, receiver) {
84 if (typeof Reflect !== "undefined" && Reflect.get) {
85 _get = Reflect.get;
86 } else {
87 _get = function _get(target, property, receiver) {
88 var base = _superPropBase(target, property);
89
90 if (!base) return;
91 var desc = Object.getOwnPropertyDescriptor(base, property);
92
93 if (desc.get) {
94 return desc.get.call(receiver);
95 }
96
97 return desc.value;
98 };
99 }
100
101 return _get(target, property, receiver || target);
102 }
103
104 function _superPropBase(object, property) {
105 while (!Object.prototype.hasOwnProperty.call(object, property)) {
106 object = _getPrototypeOf(object);
107 if (object === null) break;
108 }
109
110 return object;
111 }
112
113 function _getPrototypeOf(o) {
114 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
115 return o.__proto__ || Object.getPrototypeOf(o);
116 };
117 return _getPrototypeOf(o);
118 }
119
120 function _inherits(subClass, superClass) {
121 if (typeof superClass !== "function" && superClass !== null) {
122 throw new TypeError("Super expression must either be null or a function");
123 }
124
125 subClass.prototype = Object.create(superClass && superClass.prototype, {
126 constructor: {
127 value: subClass,
128 writable: true,
129 configurable: true
130 }
131 });
132 if (superClass) _setPrototypeOf(subClass, superClass);
133 }
134
135 function _setPrototypeOf(o, p) {
136 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
137 o.__proto__ = p;
138 return o;
139 };
140
141 return _setPrototypeOf(o, p);
142 }
143
144 var ProductSwitcher =
145 /*#__PURE__*/
146 function (_NavigationMenuPanel) {
147 _inherits(ProductSwitcher, _NavigationMenuPanel);
148 /**
149 * A navigation menu
150 * @extends NavigationMenuPanel
151 * @param {HTMLElement} element The element working as a selector.
152 * @param {Object} [options] The component options.
153 * @param {string} [options.selectorInit] The CSS class to find product
154 * switchers
155 * @param {string} [options.attribInitTarget] The attribute name in the
156 * launcher buttons to find target product switcher
157 * @param {string} [options.classProductSwitcherExpanded] The CSS class
158 * for an expanded product switcher
159 */
160
161
162 function ProductSwitcher(element, options) {
163 var _this;
164
165 _classCallCheck(this, ProductSwitcher);
166
167 _this = _possibleConstructorReturn(this, _getPrototypeOf(ProductSwitcher).call(this, element, options));
168 _this.current = '';
169 _this.triggerButtonIds = new Set();
170
171 _this._handleFocusOut = function (event) {
172 if (_this.element.contains(event.relatedTarget)) {
173 return;
174 }
175
176 var currentTriggerButton = _this.element.ownerDocument.getElementById(_this.current);
177
178 if (currentTriggerButton && event.relatedTarget && !event.relatedTarget.matches(_this.options.selectorFloatingMenus)) {
179 currentTriggerButton.focus();
180 }
181 };
182
183 _this._handleKeyDown = function (event) {
184 var isExpanded = !_this.element.hasAttribute('hidden');
185
186 if (event.which === 27 && isExpanded) {
187 var triggerButton = _this.current;
188
189 _this.changeState(_this.constructor.SELECT_NONE);
190
191 _this.element.ownerDocument.getElementById(triggerButton).focus();
192 }
193 };
194
195 _this.createdByLauncher = function (event) {
196 var isExpanded = _this.element.classList.contains(_this.options.classProductSwitcherExpanded);
197
198 var launcher = event.delegateTarget;
199
200 if (!launcher.id) {
201 launcher.id = "__carbon-product-switcher-launcher-".concat(Math.random().toString(36).substr(2));
202 }
203
204 var current = launcher.id;
205
206 _this.changeState(isExpanded && _this.current === current ? _this.constructor.SELECT_NONE : current);
207 };
208
209 _this.shouldStateBeChanged = function (current) {
210 return _this.current !== current;
211 };
212
213 _this._changeState = function (state, callback) {
214 _this.element.classList.toggle(_this.options.classProductSwitcherExpanded, state !== _this.constructor.SELECT_NONE);
215
216 _this.current = state;
217
218 if (_this.current !== _this.constructor.SELECT_NONE) {
219 _this.triggerButtonIds.add(_this.current);
220 } // deactivate all other trigger buttons
221
222
223 _this.triggerButtonIds.forEach(function (id) {
224 var button = _this.element.ownerDocument.getElementById(id);
225
226 var label = button.getAttribute(_this.options.attribLabelExpand);
227 button.classList.remove(_this.options.classNavigationMenuPanelHeaderActionActive);
228 button.setAttribute('aria-label', label);
229 button.setAttribute('title', label);
230 }); // set active trigger button attributes
231
232
233 var currentTriggerButton = _this.element.ownerDocument.getElementById(_this.current);
234
235 if (currentTriggerButton) {
236 var label = currentTriggerButton.getAttribute(_this.options.attribLabelCollapse);
237 currentTriggerButton.classList.toggle(_this.options.classNavigationMenuPanelHeaderActionActive);
238 currentTriggerButton.setAttribute('aria-label', label);
239 currentTriggerButton.setAttribute('title', label);
240 }
241
242 if (state !== _this.constructor.SELECT_NONE) {
243 _this.element.setAttribute('tabindex', '0');
244
245 _this.element.focus();
246 } else {
247 _this.element.setAttribute('tabindex', '-1');
248 }
249
250 callback();
251 };
252
253 _this.manage((0, _on.default)(element, 'keydown', _this._handleKeyDown));
254
255 _this.manage((0, _onFocusByKeyboard.default)(element, 'blur', _this._handleFocusOut));
256
257 return _this;
258 }
259 /**
260 * id of currently active trigger button
261 * @type {string}
262 */
263
264
265 _createClass(ProductSwitcher, [{
266 key: "release",
267 value: function release() {
268 this.triggerButtonIds.clear();
269 return _get(_getPrototypeOf(ProductSwitcher.prototype), "release", this).call(this);
270 }
271 /**
272 * The map associating DOM element and ProductSwitcher instance.
273 * @member ProductSwitcher.components
274 * @type {WeakMap}
275 */
276
277 }], [{
278 key: "options",
279
280 /**
281 * The component options.
282 * If `options` is specified in the constructor,
283 * {@linkcode ProductSwitcher.create .create()}, or
284 * {@linkcode ProductSwitcher.init .init()},
285 * properties in this object are overriden for the instance being create and
286 * how {@linkcode ProductSwitcher.init .init()} works.
287 * @member ProductSwitcher.options
288 * @type {Object}
289 * @property {string} selectorInit The CSS class to find popup navs.
290 * @property {string} attribInitTarget The attribute name in the
291 * launcher buttons to find target popup nav.
292 * @property {string[]} initEventNames The events that the component
293 * will handles
294 */
295 get: function get() {
296 var prefix = _settings.default.prefix;
297 return Object.assign(Object.create(_navigationMenuPanel.default.options), {
298 selectorInit: '[data-product-switcher]',
299 selectorFloatingMenus: "\n .".concat(prefix, "--overflow-menu-options,\n .").concat(prefix, "--overflow-menu-options *,\n .").concat(prefix, "--tooltip,\n .").concat(prefix, "--tooltip *,\n .flatpicker-calendar,\n .flatpicker-calendar *\n "),
300 attribInitTarget: 'data-product-switcher-target',
301 classProductSwitcherExpanded: "".concat(prefix, "--panel--expanded")
302 });
303 }
304 }]);
305
306 ProductSwitcher.SELECT_NONE = '__carbon-product-switcher-launcher-NONE';
307 ProductSwitcher.components = new WeakMap();
308 return ProductSwitcher;
309 }(_navigationMenuPanel.default);
310
311 _exports.default = ProductSwitcher;
312});
\No newline at end of file