This repository has been archived by the owner on Mar 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
291 lines (235 loc) · 9.65 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
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
'use strict'; // requires NodeList.forEach polyfill for IE
// conditional check due to https://github.com/imagitama/nodelist-foreach-polyfill/issues/7
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
if (typeof Element !== 'undefined') {
require('nodelist-foreach-polyfill');
} // requires CustomEvent polyfill for IE
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent
var CustomEvent = require('custom-event');
var KeyEmitter = require('makeup-key-emitter');
var ExitEmitter = require('makeup-exit-emitter');
var dataSetKey = 'data-makeup-index';
var defaultOptions = {
axis: 'both',
autoInit: 0,
autoReset: null,
wrap: false
};
var itemFilter = function itemFilter(el) {
return !el.hidden;
};
function clearData(els) {
els.forEach(function (el) {
return el.removeAttribute(dataSetKey);
});
}
function setData(els) {
els.forEach(function (el, index) {
return el.setAttribute(dataSetKey, index);
});
}
function onKeyPrev() {
if (!this.atStart()) {
this.index--;
} else if (this.options.wrap) {
this.index = this.filteredItems.length - 1;
}
}
function onKeyNext() {
if (!this.atEnd()) {
this.index++;
} else if (this.options.wrap) {
this.index = 0;
}
}
function onClick(e) {
var element = e.target;
var indexData = element.dataset.makeupIndex; // traverse widget ancestors until interactive element is found
while (element !== this._el && !indexData) {
element = element.parentNode;
indexData = element.dataset.makeupIndex;
}
if (indexData !== undefined) {
this.index = indexData;
}
}
function onKeyHome() {
this.index = 0;
}
function onKeyEnd() {
this.index = this.filteredItems.length;
}
function onFocusExit() {
if (this.options.autoReset !== null) {
this.reset();
}
}
function onMutation() {
// clear data-makeup-index on ALL items
clearData(this.items); // set data-makeup-index only on filtered items (e.g. non-hidden ones)
setData(this.filteredItems);
this._el.dispatchEvent(new CustomEvent('navigationModelMutation'));
}
var NavigationModel = function NavigationModel(el, itemSelector, selectedOptions) {
_classCallCheck(this, NavigationModel);
this.options = _extends({}, defaultOptions, selectedOptions);
this._el = el;
this._itemSelector = itemSelector;
};
var LinearNavigationModel =
/*#__PURE__*/
function (_NavigationModel) {
_inherits(LinearNavigationModel, _NavigationModel);
function LinearNavigationModel(el, itemSelector, selectedOptions) {
var _this;
_classCallCheck(this, LinearNavigationModel);
_this = _possibleConstructorReturn(this, _getPrototypeOf(LinearNavigationModel).call(this, el, itemSelector, selectedOptions));
if (_this.options.autoInit !== null) {
_this._index = _this.options.autoInit;
_this._el.dispatchEvent(new CustomEvent('navigationModelInit', {
detail: {
items: _this.filteredItems,
toIndex: _this.options.autoInit
},
bubbles: false
}));
}
return _this;
}
_createClass(LinearNavigationModel, [{
key: "reset",
value: function reset() {
if (this.options.autoReset !== null) {
this._index = this.options.autoReset; // do not use index setter, it will trigger change event
this._el.dispatchEvent(new CustomEvent('navigationModelReset', {
detail: {
toIndex: this.options.autoReset
},
bubbles: false
}));
}
}
}, {
key: "atEnd",
value: function atEnd() {
return this.index === this.filteredItems.length - 1;
}
}, {
key: "atStart",
value: function atStart() {
return this.index <= 0;
}
}, {
key: "items",
get: function get() {
return this._el.querySelectorAll(this._itemSelector);
}
}, {
key: "filteredItems",
get: function get() {
return Array.prototype.slice.call(this.items).filter(itemFilter);
}
}, {
key: "index",
get: function get() {
return this._index;
},
set: function set(newIndex) {
if (newIndex > -1 && newIndex < this.filteredItems.length && newIndex !== this.index) {
this._el.dispatchEvent(new CustomEvent('navigationModelChange', {
detail: {
fromIndex: this.index,
toIndex: newIndex
},
bubbles: false
}));
this._index = newIndex;
}
}
}]);
return LinearNavigationModel;
}(NavigationModel); // 2D Grid Model will go here
/*
class GridModel extends NavigationModel {
constructor(el, rowSelector, colSelector) {
super();
this._coords = null;
}
}
*/
var NavigationEmitter =
/*#__PURE__*/
function () {
function NavigationEmitter(el, model) {
_classCallCheck(this, NavigationEmitter);
this.model = model;
this.el = el;
this._keyPrevListener = onKeyPrev.bind(model);
this._keyNextListener = onKeyNext.bind(model);
this._keyHomeListener = onKeyHome.bind(model);
this._keyEndListener = onKeyEnd.bind(model);
this._clickListener = onClick.bind(model);
this._focusExitListener = onFocusExit.bind(model);
this._observer = new MutationObserver(onMutation.bind(model));
setData(model.filteredItems);
KeyEmitter.addKeyDown(this.el);
ExitEmitter.addFocusExit(this.el);
var axis = model.options.axis;
if (axis === 'both' || axis === 'x') {
this.el.addEventListener('arrowLeftKeyDown', this._keyPrevListener);
this.el.addEventListener('arrowRightKeyDown', this._keyNextListener);
}
if (axis === 'both' || axis === 'y') {
this.el.addEventListener('arrowUpKeyDown', this._keyPrevListener);
this.el.addEventListener('arrowDownKeyDown', this._keyNextListener);
}
this.el.addEventListener('homeKeyDown', this._keyHomeListener);
this.el.addEventListener('endKeyDown', this._keyEndListener);
this.el.addEventListener('click', this._clickListener);
this.el.addEventListener('focusExit', this._focusExitListener);
this._observer.observe(this.el, {
childList: true,
subtree: true,
attributeFilter: ['hidden'],
attributes: true
});
}
_createClass(NavigationEmitter, [{
key: "destroy",
value: function destroy() {
KeyEmitter.removeKeyDown(this.el);
ExitEmitter.removeFocusExit(this.el);
this.el.removeEventListener('arrowLeftKeyDown', this._keyPrevListener);
this.el.removeEventListener('arrowRightKeyDown', this._keyNextListener);
this.el.removeEventListener('arrowUpKeyDown', this._keyPrevListener);
this.el.removeEventListener('arrowDownKeyDown', this._keyNextListener);
this.el.removeEventListener('homeKeyDown', this._keyHomeListener);
this.el.removeEventListener('endKeyDown', this._keyEndListener);
this.el.removeEventListener('click', this._clickListener);
this.el.removeEventListener('focusExit', this._focusExitListener);
this._observer.disconnect();
}
}], [{
key: "createLinear",
value: function createLinear(el, itemSelector, selectedOptions) {
var model = new LinearNavigationModel(el, itemSelector, selectedOptions);
return new NavigationEmitter(el, model);
}
/*
static createGrid(el, rowSelector, colSelector, selectedOptions) {
return null;
}
*/
}]);
return NavigationEmitter;
}();
module.exports = NavigationEmitter;