-
Notifications
You must be signed in to change notification settings - Fork 58
/
es6.js
176 lines (166 loc) · 4.8 KB
/
es6.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
// ┌─────────┐
// │ Helpers │
// └─────────┘
// utilities for working with dom, and removing browser inconsistencies
// with support back to IE9+
import * as dom from './lib/js/helpers/dom';
import * as classy from './lib/js/helpers/classy';
import * as aria from './lib/js/helpers/aria';
import * as event from './lib/js/helpers/event';
import clipboard from './lib/js/helpers/clipboard';
// ┌─────┐
// │ Bus │
// └─────┘
// all event passing takes place over a bus
// this is just an instance of tinyEmitter
import bus from './lib/js/helpers/bus';
// ┌─────────────────┐
// │ Import Patterns │
// └─────────────────┘
// import all interactive patterns
import accordion from './lib/js/patterns/accordion';
import dropdown from './lib/js/patterns/dropdown';
import drawer from './lib/js/patterns/drawers';
import filterDropdown from './lib/js/patterns/filter-dropdown';
import modal from './lib/js/patterns/modal';
import search from './lib/js/patterns/search';
import selectNav from './lib/js/patterns/select-nav';
import sticky from './lib/js/patterns/sticky';
import tabs from './lib/js/patterns/tabs';
// ┌──────────────────────┐
// │ Emit Keyboard Events │
// └──────────────────────┘
// emit presses of escape and return keys
event.add(document, 'keyup', translateKeypress);
function translateKeypress (e) {
if (e.keyCode === 27) {
bus.emit('keyboard:escape');
} else if (e.keyCode === 13) {
bus.emit('keyboard:return');
} else if (e.keyCode === 32) {
bus.emit('keyboard:space');
} else if (e.keyCode === 38) {
bus.emit('keyboard:arrow:up');
} else if (e.keyCode === 40) {
bus.emit('keyboard:arrow:down');
} else if (e.keyCode === 37) {
bus.emit('keyboard:arrow:left');
} else if (e.keyCode === 39) {
bus.emit('keyboard:arrow:right');
}
}
// ┌────────────────────┐
// │ Emit Scroll Events │
// └────────────────────┘
// throttled for performance
event.add(window, 'scroll', event.throttle(isScrolling, 100));
function isScrolling () {
bus.emit('scrolling:at', window.pageYOffset);
}
// ┌────────────────────┐
// │ Initialize Calcite │
// └────────────────────┘
// start up Calcite and attach all the patterns
// optionally pass an array of patterns you'd like to watch
var patterns = [
accordion,
clipboard,
dropdown,
drawer,
filterDropdown,
modal,
search,
selectNav,
sticky,
tabs
];
function init () {
patterns.forEach(function (pattern) {
pattern();
});
}
function extend (plugin) {
// Object Assign Polyfill
if (typeof Object.assign !== 'function') {
Object.assign = function (target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
for (var key in plugin) {
patterns.push(plugin[key]);
}
Object.assign(this, plugin);
}
// ┌────────────┐
// │ Public API │
// └────────────┘
// define all public api methods
var version = '1.2.5';
var click = event.click;
var addEvent = event.add;
var removeEvent = event.remove;
var eventTarget = event.target;
var preventDefault = event.preventDefault;
var stopPropagation = event.stopPropagation;
var throttle = event.throttle;
var hasClass = classy.has;
var addClass = classy.add;
var removeClass = classy.remove;
var toggleClass = classy.toggle;
var removeActive = classy.removeActive;
var addActive = classy.addActive;
var toggleActive = classy.toggleActive;
var toggleAriaHidden = aria.toggleHidden;
var toggleAriaExpanded = aria.toggleExpanded;
var closest = dom.closest;
var nodeListToArray = dom.nodeListToArray;
var findElements = dom.findElements;
export {
version,
click,
addEvent,
removeEvent,
eventTarget,
preventDefault,
stopPropagation,
throttle,
hasClass,
addClass,
removeClass,
toggleClass,
removeActive,
addActive,
toggleActive,
toggleAriaHidden,
toggleAriaExpanded,
closest,
nodeListToArray,
findElements,
bus,
accordion,
dropdown,
drawer,
filterDropdown,
modal,
search,
selectNav,
sticky,
tabs,
extend,
init
};