-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswiss.js
256 lines (214 loc) · 6.93 KB
/
swiss.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
//
// swiss.js - can:
//
// - Add or remove classes on a target or the same element
// - Dispatch a custom event
// - Call a function
//
//
// Data attributes ---------------------------------------------------------------------------------
//
// Required:
// data-swiss
//
//
// Event type:
// data-swiss-event-type - string: any valid js event. e.g 'mouseenter', 'click'
// (Default: 'click')
//
//
// Class manipulation:
// data-swiss-this-class - string: manipulate class/classes on the same element
// e.g 'my-active-class', 'o-100 scale-10'
// data-swiss-target - string: the #id of another element to target
// for class manipulation
// data-swiss-target-class - string: class to add to the target el specified
// by data-swiss-target
//
//
// Trigger event:
// data-swiss-trigger-event - string: Trigger a customEvent on the same element
// e.g 'image-set/init-lazy-wrapper'
//
//
// Run a function:
// data-swiss-run - string: e.g 'window.dispatchEvent(new CustomEvent('your-event'))'
//
//
// Optional:
// data-swiss-classlist-action - string: toggle|add|remove (default: 'toggle')
// data-swiss-bp-min - string: bind swiss events "ABOVE" the 'sm|md...'
// breakpoint (defined in scss)
// data-swiss-bp-max - string: bind swiss events "BELOW" the 'sm|md...'
// breakpoint (defined in scss)
// data-swiss-reset-resize - bool: reset all the class toggling that has been done
// on resize
import styleVars from "./StyleVars";
// swiss
// -------------------------------------
function swiss(el) {
console.log("swiss");
let opts;
let target = null;
function is_supported_classlist_action(action) {
const supported_classlist_actions = ["toggle", "add", "remove"];
return supported_classlist_actions.includes(action);
}
// item_settings
// -------------------------------------
function item_settings() {
opts = {
classlist_action: "toggle",
this_class: null,
this_initial_state: null,
target_class: null,
target: null,
target_initial_state: null,
event_type: "click",
resize_reset: false,
bp_min: null,
bp_max: null,
run: null,
trigger_event: null,
};
// set_opt
// -------------------------------------
function set_opt(opt_name, attr) {
if (el.hasAttribute(attr)) {
opts[opt_name] = el.getAttribute(attr);
}
}
set_opt("classlist_action", "data-swiss-classlist_action");
set_opt("this_class", "data-swiss-this-class");
set_opt("target_class", "data-swiss-target-class");
set_opt("target", "data-swiss-target");
set_opt("bp_min", "data-swiss-bp-min");
set_opt("bp_max", "data-swiss-bp-max");
set_opt("event_type", "data-swiss-event-type");
set_opt("reset_resize", "data-swiss-reset-resize");
set_opt("run", "data-swiss-run");
set_opt("trigger_event", "data-swiss-trigger-event");
}
// toggle_class_names
// -------------------------------------
function toggle_class_names(element, action, class_string) {
const class_names = class_string.split(" ");
class_names.forEach((class_name) => element.classList[action](class_name));
}
// set_classes
// -------------------------------------
function set_classes() {
if (opts.this_class) {
toggle_class_names(el, opts.classlist_action, opts.this_class);
}
if (opts.target_class && target) {
toggle_class_names(target, opts.classlist_action, opts.target_class);
}
if (opts.classlist_action !== "toggle") {
unbind();
}
if (opts.run) {
const f = new Function(opts.run);
f();
}
if (opts.trigger_event) {
console.log("trigger", opts.trigger_event);
el.dispatchEvent(new CustomEvent(opts.trigger_event));
}
}
// reset_classes
// -------------------------------------
function reset_classes() {
const this_classlist_action = opts.this_initial_state ? "add" : "remove";
const target_classlist_action = opts.target_initial_state
? "add"
: "remove";
if (opts.this_class) {
toggle_class_names(el, this_classlist_action, opts.this_class);
}
if (opts.target_class && target) {
toggle_class_names(target, target_classlist_action, opts.target_class);
}
}
// bind & unbind
// -------------------------------------
function bind() {
el.addEventListener(opts.event_type, set_classes);
}
function unbind() {
el.removeEventListener(opts.event_type, set_classes);
}
// bp_acitve
// -------------------------------------
function bp_active() {
let bp_is_active = false;
if (opts.bp_max) {
bp_is_active = window.matchMedia(
`(max-width:${styleVars.bp_max[opts.bp_max]})`
).matches;
}
if (opts.bp_min) {
bp_is_active = window.matchMedia(
`(min-width:${styleVars.bp_min[opts.bp_min]})`
).matches;
}
return bp_is_active;
}
// bind_or_unbind_listeners_based_on_bp
// -------------------------------------
function bind_or_unbind_listeners_based_on_bp() {
if (bp_active()) {
bind();
} else {
unbind();
reset_classes();
}
}
// store_initial_state
// -------------------------------------
function store_initial_state() {
if (opts.this_class) {
// Later, swiss may automatically unload itself (e.g. depending on breakpoint)
// - to do this, save whether the classes were initially present on the element
// before swiss ran.
const this_classes = opts.this_class.split(" ");
opts.this_initial_state = el.classList.contains(this_classes[0]);
}
if (opts.target_class && target) {
const target_classes = opts.target_class.split(" ");
opts.target_initial_state = target.classList.contains(target_classes[0]);
}
}
// init
// -------------------------------------
function init() {
item_settings(el);
console.log(opts);
if (!is_supported_classlist_action(opts.classlist_action)) {
return;
}
if (opts.target) {
target = document.getElementById(opts.target);
}
store_initial_state();
if (opts.resize_reset) {
window.addEventListener("resize", reset_classes);
}
if (opts.bp_min || opts.bp_max) {
window.addEventListener("resize", bind_or_unbind_listeners_based_on_bp);
bind_or_unbind_listeners_based_on_bp();
} else {
bind();
}
}
init();
}
// init_all
// ------------------------------------
function init_all() {
console.log("init_all");
const items = Array.from(document.querySelectorAll("[data-swiss]"));
console.log(items);
items.forEach(swiss);
}
init_all();