bord 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +193 -0
- data/Rakefile +9 -0
- data/app/assets/javascripts/bord/application.js +2361 -0
- data/app/assets/javascripts/bord/attract.js +319 -0
- data/app/assets/javascripts/bord/attractive.js +1642 -0
- data/app/assets/javascripts/bord/entry.js +67 -0
- data/app/assets/javascripts/bord/request.js +314 -0
- data/app/assets/javascripts/bord/runtime/addons/response.js +51 -0
- data/app/assets/javascripts/bord/runtime/addons/template.js +120 -0
- data/app/assets/javascripts/bord/runtime/debug.js +18 -0
- data/app/assets/stylesheets/bord/application.css +736 -0
- data/app/controllers/bord/actions_controller.rb +29 -0
- data/app/controllers/bord/application_controller.rb +23 -0
- data/app/controllers/bord/pages_controller.rb +13 -0
- data/app/controllers/bord/records_controller.rb +14 -0
- data/app/controllers/bord/widgets_controller.rb +18 -0
- data/app/helpers/bord/application_helper.rb +4 -0
- data/app/jobs/bord/application_job.rb +4 -0
- data/app/models/bord/action.rb +8 -0
- data/app/models/bord/action_parameters.rb +19 -0
- data/app/models/bord/allowed_actions.rb +65 -0
- data/app/models/bord/application_record.rb +5 -0
- data/app/models/bord/attract_actions.rb +101 -0
- data/app/models/bord/category.rb +18 -0
- data/app/models/bord/dashboard.rb +25 -0
- data/app/models/bord/entry.rb +98 -0
- data/app/models/bord/page/decorator.rb +33 -0
- data/app/models/bord/page/record_decorator.rb +44 -0
- data/app/models/bord/page.rb +82 -0
- data/app/models/bord/widget/decorator.rb +69 -0
- data/app/models/bord/widget.rb +40 -0
- data/app/models/concerns/bord/conditionable.rb +8 -0
- data/app/views/bord/pages/show.html.erb +9 -0
- data/app/views/bord/records/show.html.erb +13 -0
- data/app/views/bord/shared/_templates.html.erb +75 -0
- data/app/views/bord/widgets/_action.html.erb +7 -0
- data/app/views/bord/widgets/_widget.html.erb +25 -0
- data/app/views/bord/widgets/index.html.erb +17 -0
- data/app/views/layouts/bord/application.html.erb +38 -0
- data/config/routes.rb +11 -0
- data/lib/bord/configuration.rb +60 -0
- data/lib/bord/engine.rb +11 -0
- data/lib/bord/loader.rb +8 -0
- data/lib/bord/options.rb +17 -0
- data/lib/bord/presets/action_mailbox.json +20 -0
- data/lib/bord/presets/active_storage.json +20 -0
- data/lib/bord/presets/devise.json +1 -0
- data/lib/bord/presets/good_job.json +25 -0
- data/lib/bord/presets/sidekiq.json +28 -0
- data/lib/bord/presets/solid_queue.json +12 -0
- data/lib/bord/presets/stripe.json +30 -0
- data/lib/bord/presets/user.json +22 -0
- data/lib/bord/version.rb +3 -0
- data/lib/bord.rb +71 -0
- data/lib/generators/bord/install_generator.rb +16 -0
- data/lib/generators/bord/presets_generator.rb +62 -0
- metadata +116 -0
|
@@ -0,0 +1,1642 @@
|
|
|
1
|
+
//#region src/core/hooks.js
|
|
2
|
+
var Hooks = class {
|
|
3
|
+
#before = /* @__PURE__ */ new Set();
|
|
4
|
+
#after = /* @__PURE__ */ new Set();
|
|
5
|
+
#error = /* @__PURE__ */ new Set();
|
|
6
|
+
addBefore(callback) {
|
|
7
|
+
this.#before.add(callback);
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
addAfter(callback) {
|
|
11
|
+
this.#after.add(callback);
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
addError(callback) {
|
|
15
|
+
this.#error.add(callback);
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
removeBefore(callback) {
|
|
19
|
+
this.#before.delete(callback);
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
removeAfter(callback) {
|
|
23
|
+
this.#after.delete(callback);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
removeError(callback) {
|
|
27
|
+
this.#error.delete(callback);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
runBefore(context) {
|
|
31
|
+
for (const callback of this.#before) if (callback(context) === false) return false;
|
|
32
|
+
}
|
|
33
|
+
runAfter(context) {
|
|
34
|
+
for (const callback of this.#after) callback(context);
|
|
35
|
+
}
|
|
36
|
+
runError(context) {
|
|
37
|
+
for (const callback of this.#error) callback(context);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/core/registry.js
|
|
42
|
+
var Registry = class {
|
|
43
|
+
#actions = /* @__PURE__ */ new Map();
|
|
44
|
+
#gates = /* @__PURE__ */ new Map();
|
|
45
|
+
#triggers = /* @__PURE__ */ new Map();
|
|
46
|
+
#eventModifiers = /* @__PURE__ */ new Map();
|
|
47
|
+
#activeActions = null;
|
|
48
|
+
addAction(name, action, group = null) {
|
|
49
|
+
this.#actions.set(name, {
|
|
50
|
+
action,
|
|
51
|
+
group
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
getAction(name) {
|
|
55
|
+
const entry = this.#actions.get(name);
|
|
56
|
+
return entry ? entry.action : void 0;
|
|
57
|
+
}
|
|
58
|
+
hasAction(name) {
|
|
59
|
+
return this.#actions.has(name);
|
|
60
|
+
}
|
|
61
|
+
setActiveActions(actionNames) {
|
|
62
|
+
this.#activeActions = actionNames;
|
|
63
|
+
}
|
|
64
|
+
isAllowed(actionName) {
|
|
65
|
+
if (!this.#activeActions) return true;
|
|
66
|
+
return this.#activeActions.has(actionName);
|
|
67
|
+
}
|
|
68
|
+
addGate(name, gate) {
|
|
69
|
+
this.#gates.set(name, gate);
|
|
70
|
+
}
|
|
71
|
+
getGate(name) {
|
|
72
|
+
return this.#gates.get(name);
|
|
73
|
+
}
|
|
74
|
+
hasGate(name) {
|
|
75
|
+
return this.#gates.has(name);
|
|
76
|
+
}
|
|
77
|
+
addTrigger(name, trigger) {
|
|
78
|
+
this.#triggers.set(name, trigger);
|
|
79
|
+
}
|
|
80
|
+
getTrigger(name) {
|
|
81
|
+
return this.#triggers.get(name);
|
|
82
|
+
}
|
|
83
|
+
hasTrigger(name) {
|
|
84
|
+
return this.#triggers.has(name);
|
|
85
|
+
}
|
|
86
|
+
addEventModifier(name, eventModifier) {
|
|
87
|
+
this.#eventModifiers.set(name, eventModifier);
|
|
88
|
+
}
|
|
89
|
+
getEventModifier(name) {
|
|
90
|
+
return this.#eventModifiers.get(name);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/core/attributes.js
|
|
95
|
+
const RESERVED = /* @__PURE__ */ new Set(["@target", "@targets"]);
|
|
96
|
+
function actionAttributes(element) {
|
|
97
|
+
if (!element || !element.attributes) return false;
|
|
98
|
+
for (const attribute of element.attributes) {
|
|
99
|
+
if (RESERVED.has(attribute.name)) continue;
|
|
100
|
+
if (attribute.name.startsWith("@")) return true;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
function getActionAttributes({ on: element }) {
|
|
105
|
+
const attributes = [];
|
|
106
|
+
for (const attribute of element.attributes) {
|
|
107
|
+
if (RESERVED.has(attribute.name)) continue;
|
|
108
|
+
if (attribute.name.startsWith("@")) attributes.push(parseAttribute(attribute.name, attribute.value));
|
|
109
|
+
}
|
|
110
|
+
return attributes;
|
|
111
|
+
}
|
|
112
|
+
function parseAttribute(name, value) {
|
|
113
|
+
if (name === "@action" || name === "@") return {
|
|
114
|
+
event: null,
|
|
115
|
+
modifiers: [],
|
|
116
|
+
value
|
|
117
|
+
};
|
|
118
|
+
const parts = name.slice(1).split(".");
|
|
119
|
+
return {
|
|
120
|
+
event: parts[0],
|
|
121
|
+
modifiers: parts.slice(1).flatMap((part) => part.split("+")),
|
|
122
|
+
value
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function getTargetValue(element) {
|
|
126
|
+
return element.getAttribute("@target");
|
|
127
|
+
}
|
|
128
|
+
function getTargetsValue(element) {
|
|
129
|
+
return element.getAttribute("@targets");
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region src/core/events/evaluate.js
|
|
133
|
+
var Evaluate = class {
|
|
134
|
+
#registry;
|
|
135
|
+
constructor(registry) {
|
|
136
|
+
this.#registry = registry;
|
|
137
|
+
}
|
|
138
|
+
async run(action, { for: event, on: element, using: defaultEventType, triggeredBy: directive, state }, { execute }) {
|
|
139
|
+
if (action.startsWith("js:")) return await execute(action, { with: {
|
|
140
|
+
on: element,
|
|
141
|
+
for: event,
|
|
142
|
+
triggeredBy: directive || null,
|
|
143
|
+
state
|
|
144
|
+
} });
|
|
145
|
+
if (action.includes(":")) action = this.#stripDirectives({
|
|
146
|
+
from: action,
|
|
147
|
+
for: event,
|
|
148
|
+
on: element
|
|
149
|
+
});
|
|
150
|
+
if (action === void 0) return;
|
|
151
|
+
return await execute(action, { with: {
|
|
152
|
+
on: element,
|
|
153
|
+
for: event,
|
|
154
|
+
triggeredBy: directive || null,
|
|
155
|
+
state
|
|
156
|
+
} });
|
|
157
|
+
}
|
|
158
|
+
#stripDirectives({ from: action, for: event, on: element }) {
|
|
159
|
+
const [base, ...directives] = action.split(":");
|
|
160
|
+
if (!directives.filter((name) => !(this.#registry.hasTrigger(name) && event.type === name)).every((name) => this.#passes(name, {
|
|
161
|
+
for: event,
|
|
162
|
+
on: element
|
|
163
|
+
}))) return;
|
|
164
|
+
return base;
|
|
165
|
+
}
|
|
166
|
+
#passes(name, { for: event, on: element }) {
|
|
167
|
+
const gated = this.#registry.getGate(name);
|
|
168
|
+
if (!gated) return event.type === name;
|
|
169
|
+
return gated(element, { event }) !== false;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/debug.js
|
|
174
|
+
var Debug = class {
|
|
175
|
+
static enabled = false;
|
|
176
|
+
static prefix = "🧲 ";
|
|
177
|
+
static log(...args) {
|
|
178
|
+
if (this.enabled) console.log(this.prefix, ...args);
|
|
179
|
+
}
|
|
180
|
+
static warn(...args) {
|
|
181
|
+
if (this.enabled) console.warn(this.prefix, ...args);
|
|
182
|
+
}
|
|
183
|
+
static error(...args) {
|
|
184
|
+
if (this.enabled) console.error(this.prefix, ...args);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/core/events/execute.js
|
|
189
|
+
var Execute = class {
|
|
190
|
+
#registry;
|
|
191
|
+
#hooks;
|
|
192
|
+
#onError;
|
|
193
|
+
#getTargetValue;
|
|
194
|
+
#getTargetsValue;
|
|
195
|
+
constructor(registry, hooks, onError, getTargetValue, getTargetsValue) {
|
|
196
|
+
this.#registry = registry;
|
|
197
|
+
this.#hooks = hooks;
|
|
198
|
+
this.#onError = onError;
|
|
199
|
+
this.#getTargetValue = getTargetValue;
|
|
200
|
+
this.#getTargetsValue = getTargetsValue;
|
|
201
|
+
}
|
|
202
|
+
async run(action, { with: { on: element, for: event, triggeredBy: directive, state } }) {
|
|
203
|
+
const resolved = this.#resolve({ from: action });
|
|
204
|
+
if (resolved === void 0) return;
|
|
205
|
+
const { name, value } = resolved;
|
|
206
|
+
const target = this.#getTargetValue(element);
|
|
207
|
+
const targets = this.#getTargetsValue(element);
|
|
208
|
+
const hookContext = this.#hookContext({
|
|
209
|
+
name,
|
|
210
|
+
value,
|
|
211
|
+
on: element,
|
|
212
|
+
for: event,
|
|
213
|
+
target,
|
|
214
|
+
targets
|
|
215
|
+
});
|
|
216
|
+
const actionContext = this.#actionContext({
|
|
217
|
+
name,
|
|
218
|
+
value,
|
|
219
|
+
on: element,
|
|
220
|
+
for: event,
|
|
221
|
+
target,
|
|
222
|
+
targets,
|
|
223
|
+
triggeredBy: directive,
|
|
224
|
+
state
|
|
225
|
+
});
|
|
226
|
+
if (this.#hooks?.runBefore(hookContext) === false) {
|
|
227
|
+
event?.preventDefault();
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
const startTime = performance.now();
|
|
231
|
+
const result = await this.#invoke({
|
|
232
|
+
name,
|
|
233
|
+
on: element,
|
|
234
|
+
context: actionContext
|
|
235
|
+
});
|
|
236
|
+
const elapsed = performance.now() - startTime;
|
|
237
|
+
this.#hooks?.runAfter({
|
|
238
|
+
...hookContext,
|
|
239
|
+
result
|
|
240
|
+
});
|
|
241
|
+
this.#log({
|
|
242
|
+
name,
|
|
243
|
+
on: element,
|
|
244
|
+
for: event,
|
|
245
|
+
elapsed,
|
|
246
|
+
target
|
|
247
|
+
});
|
|
248
|
+
if (result === false && event) event.preventDefault();
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
#resolve({ from: action }) {
|
|
252
|
+
if (action.startsWith("js:")) return {
|
|
253
|
+
name: "js",
|
|
254
|
+
value: action.slice(3)
|
|
255
|
+
};
|
|
256
|
+
const parts = action.split("#");
|
|
257
|
+
const [possibleAction, fallbackAction, fallbackValue] = parts;
|
|
258
|
+
const name = this.#registry.hasAction(possibleAction) ? possibleAction : this.#registry.hasAction(fallbackAction) ? fallbackAction : action;
|
|
259
|
+
if (!this.#registry.isAllowed(name)) return;
|
|
260
|
+
if (typeof this.#registry.getAction(name) !== "function") return;
|
|
261
|
+
return {
|
|
262
|
+
name,
|
|
263
|
+
value: this.#registry.hasAction(possibleAction) ? parts.slice(1).join("#") : fallbackValue ?? null
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
#hookContext({ name, value, on: element, for: event, target, targets }) {
|
|
267
|
+
return {
|
|
268
|
+
name,
|
|
269
|
+
element,
|
|
270
|
+
options: {
|
|
271
|
+
value,
|
|
272
|
+
target,
|
|
273
|
+
targets
|
|
274
|
+
},
|
|
275
|
+
event: event || null
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
#actionContext({ name, value, on: element, for: event, target, targets, triggeredBy: directive, state }) {
|
|
279
|
+
return {
|
|
280
|
+
value,
|
|
281
|
+
target,
|
|
282
|
+
targets,
|
|
283
|
+
event: event || null,
|
|
284
|
+
actionName: name,
|
|
285
|
+
triggeredBy: directive || null,
|
|
286
|
+
state,
|
|
287
|
+
dataset: element.dataset
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
async #invoke({ name, on: element, context }) {
|
|
291
|
+
try {
|
|
292
|
+
const Action = this.#registry.getAction(name);
|
|
293
|
+
if (typeof Action === "function" && Action.prototype?.run) {
|
|
294
|
+
const instance = new Action(element, context);
|
|
295
|
+
instance.element = element;
|
|
296
|
+
instance.options = context;
|
|
297
|
+
return await instance.run();
|
|
298
|
+
}
|
|
299
|
+
return await Action(element, context);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
this.#reportError({
|
|
302
|
+
name,
|
|
303
|
+
on: element,
|
|
304
|
+
error
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
#reportError({ name: actionName, on: element, error }) {
|
|
309
|
+
const targetId = element.id || this.#getTargetValue(element) || "";
|
|
310
|
+
const message = `${actionName} on ${element.tagName.toLowerCase()}#${targetId}: ${error.message}`;
|
|
311
|
+
Debug.error(message);
|
|
312
|
+
if (this.#hooks) this.#hooks.runError({
|
|
313
|
+
name: actionName,
|
|
314
|
+
element,
|
|
315
|
+
error
|
|
316
|
+
});
|
|
317
|
+
if (this.#onError) this.#onError(error, message, {
|
|
318
|
+
actionName,
|
|
319
|
+
element
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
#log({ name, on: element, for: event, elapsed, target }) {
|
|
323
|
+
const targetId = element.id || target || "";
|
|
324
|
+
const ref = targetId ? `#${targetId}` : "";
|
|
325
|
+
const tag = element.tagName.toLowerCase();
|
|
326
|
+
Debug.log(`${name} → ${tag}${ref} (${elapsed.toFixed(2)}ms) [${event?.type}]`);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/core/events.js
|
|
331
|
+
var Events = class {
|
|
332
|
+
#evaluate;
|
|
333
|
+
#execute;
|
|
334
|
+
constructor(registry, hooks, onError) {
|
|
335
|
+
this.#evaluate = new Evaluate(registry);
|
|
336
|
+
this.#execute = new Execute(registry, hooks, onError, (element) => getTargetValue(element), (element) => getTargetsValue(element));
|
|
337
|
+
}
|
|
338
|
+
async process(event, { on: element, using: defaultEventType, triggeredBy: directive, with: actionValue, state }) {
|
|
339
|
+
if (!element || !actionValue) return;
|
|
340
|
+
for (const action of this.#splitActions(actionValue)) if (await this.#evaluate.run(action, {
|
|
341
|
+
for: event,
|
|
342
|
+
on: element,
|
|
343
|
+
using: defaultEventType,
|
|
344
|
+
triggeredBy: directive,
|
|
345
|
+
state
|
|
346
|
+
}, { execute: (action, context) => this.#execute.run(action, context) }) === false) return false;
|
|
347
|
+
}
|
|
348
|
+
#splitActions(action) {
|
|
349
|
+
if (action.startsWith("js:")) return [action];
|
|
350
|
+
return action.split(" ").filter((action) => action);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/core/event_types.js
|
|
355
|
+
var EventTypes = class {
|
|
356
|
+
getDefault({ from: element }) {
|
|
357
|
+
const tagName = element.tagName.toLowerCase();
|
|
358
|
+
const isInput = tagName === "input";
|
|
359
|
+
const inputType = isInput ? element.type || "text" : null;
|
|
360
|
+
return isInput ? this.#defaultEvents.input[inputType] || this.#defaultEvents.input.default : this.#defaultEvents[tagName] || this.#defaultEvents.default;
|
|
361
|
+
}
|
|
362
|
+
#defaultEvents = {
|
|
363
|
+
a: "click",
|
|
364
|
+
button: "click",
|
|
365
|
+
input: {
|
|
366
|
+
checkbox: "change",
|
|
367
|
+
radio: "change",
|
|
368
|
+
submit: "click",
|
|
369
|
+
button: "click",
|
|
370
|
+
reset: "click",
|
|
371
|
+
default: "input"
|
|
372
|
+
},
|
|
373
|
+
select: "change",
|
|
374
|
+
textarea: "input",
|
|
375
|
+
form: "submit",
|
|
376
|
+
default: "click"
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
//#endregion
|
|
380
|
+
//#region src/core/triggers.js
|
|
381
|
+
var Triggers = class {
|
|
382
|
+
#registry;
|
|
383
|
+
constructor(registry) {
|
|
384
|
+
this.#registry = registry;
|
|
385
|
+
}
|
|
386
|
+
setup({ for: directive, on: element, trigger: run, argument = null }) {
|
|
387
|
+
const trigger = this.#registry.getTrigger(directive);
|
|
388
|
+
if (!trigger) return false;
|
|
389
|
+
trigger(element, run, argument);
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/core/element_lifecycle_hooks.js
|
|
395
|
+
var ElementLifecycleHooks = class {
|
|
396
|
+
#added = /* @__PURE__ */ new Set();
|
|
397
|
+
#removed = /* @__PURE__ */ new Set();
|
|
398
|
+
#beforeRemove = /* @__PURE__ */ new Set();
|
|
399
|
+
#targetAdded = /* @__PURE__ */ new Map();
|
|
400
|
+
#targetRemoved = /* @__PURE__ */ new Map();
|
|
401
|
+
onAdded(callback) {
|
|
402
|
+
this.#added.add(callback);
|
|
403
|
+
return this;
|
|
404
|
+
}
|
|
405
|
+
onRemoved(callback) {
|
|
406
|
+
this.#removed.add(callback);
|
|
407
|
+
return this;
|
|
408
|
+
}
|
|
409
|
+
onBeforeRemove(callback) {
|
|
410
|
+
this.#beforeRemove.add(callback);
|
|
411
|
+
return this;
|
|
412
|
+
}
|
|
413
|
+
onTargetAdded(id, callback) {
|
|
414
|
+
if (!this.#targetAdded.has(id)) this.#targetAdded.set(id, /* @__PURE__ */ new Set());
|
|
415
|
+
this.#targetAdded.get(id).add(callback);
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
onTargetRemoved(id, callback) {
|
|
419
|
+
if (!this.#targetRemoved.has(id)) this.#targetRemoved.set(id, /* @__PURE__ */ new Set());
|
|
420
|
+
this.#targetRemoved.get(id).add(callback);
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
hasTarget(id) {
|
|
424
|
+
return this.#targetAdded.has(id) || this.#targetRemoved.has(id);
|
|
425
|
+
}
|
|
426
|
+
notifyAdded(element) {
|
|
427
|
+
this.#added.forEach((fn) => fn(element));
|
|
428
|
+
}
|
|
429
|
+
notifyRemoved(element) {
|
|
430
|
+
this.#removed.forEach((fn) => fn(element));
|
|
431
|
+
}
|
|
432
|
+
notifyBeforeRemove(element) {
|
|
433
|
+
this.#beforeRemove.forEach((fn) => fn(element));
|
|
434
|
+
}
|
|
435
|
+
notifyTargetAdded(element) {
|
|
436
|
+
this.#targetAdded.get(element.id)?.forEach((fn) => fn(element));
|
|
437
|
+
}
|
|
438
|
+
notifyTargetRemoved(element) {
|
|
439
|
+
this.#targetRemoved.get(element.id)?.forEach((fn) => fn(element));
|
|
440
|
+
}
|
|
441
|
+
clear() {
|
|
442
|
+
this.#added.clear();
|
|
443
|
+
this.#removed.clear();
|
|
444
|
+
this.#beforeRemove.clear();
|
|
445
|
+
this.#targetAdded.clear();
|
|
446
|
+
this.#targetRemoved.clear();
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
//#endregion
|
|
450
|
+
//#region src/core/event_subscriptions.js
|
|
451
|
+
var EventSubscriptions = class {
|
|
452
|
+
#subscriptions = /* @__PURE__ */ new Map();
|
|
453
|
+
#scope = document;
|
|
454
|
+
setScope(scope) {
|
|
455
|
+
this.#scope = scope;
|
|
456
|
+
}
|
|
457
|
+
add(type, callback) {
|
|
458
|
+
if (!this.#subscriptions.has(type)) {
|
|
459
|
+
const listener = (event) => {
|
|
460
|
+
this.#subscriptions.get(type).subscriptions.forEach((fn) => fn(event));
|
|
461
|
+
};
|
|
462
|
+
this.#scope.addEventListener(type, listener);
|
|
463
|
+
this.#subscriptions.set(type, {
|
|
464
|
+
listener,
|
|
465
|
+
subscriptions: /* @__PURE__ */ new Set()
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
this.#subscriptions.get(type).subscriptions.add(callback);
|
|
469
|
+
return this;
|
|
470
|
+
}
|
|
471
|
+
removeAll() {
|
|
472
|
+
this.#subscriptions.forEach(({ listener }, type) => {
|
|
473
|
+
this.#scope.removeEventListener(type, listener);
|
|
474
|
+
});
|
|
475
|
+
this.#subscriptions.clear();
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
//#endregion
|
|
479
|
+
//#region src/core/attribute_prefixes.js
|
|
480
|
+
var AttributePrefixes = class {
|
|
481
|
+
#prefixes = /* @__PURE__ */ new Set();
|
|
482
|
+
add(prefix) {
|
|
483
|
+
this.#prefixes.add(prefix);
|
|
484
|
+
return this;
|
|
485
|
+
}
|
|
486
|
+
matches(element) {
|
|
487
|
+
if (actionAttributes(element)) return true;
|
|
488
|
+
return Array.from(this.#prefixes).some((prefix) => Array.from(element.attributes).some((attribute) => attribute.name.startsWith(prefix)));
|
|
489
|
+
}
|
|
490
|
+
clear() {
|
|
491
|
+
this.#prefixes.clear();
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
//#endregion
|
|
495
|
+
//#region src/core/helpers/debounce.js
|
|
496
|
+
const debounce = () => {
|
|
497
|
+
let timeoutId;
|
|
498
|
+
return (callback, delay) => {
|
|
499
|
+
clearTimeout(timeoutId);
|
|
500
|
+
timeoutId = setTimeout(callback, delay);
|
|
501
|
+
};
|
|
502
|
+
};
|
|
503
|
+
//#endregion
|
|
504
|
+
//#region src/core/scopes.js
|
|
505
|
+
const SCOPE_PROPERTY = "__attractiveScope";
|
|
506
|
+
function registerScope(root) {
|
|
507
|
+
if (root && root.nodeType === 1 && !root[SCOPE_PROPERTY]) Object.defineProperty(root, SCOPE_PROPERTY, {
|
|
508
|
+
value: true,
|
|
509
|
+
configurable: true
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
function unregisterScope(root) {
|
|
513
|
+
if (root && root.nodeType === 1 && root[SCOPE_PROPERTY]) delete root[SCOPE_PROPERTY];
|
|
514
|
+
}
|
|
515
|
+
function insideNestedScope(element, scope) {
|
|
516
|
+
let node = element;
|
|
517
|
+
while (node && node.nodeType === 1 && node !== scope) {
|
|
518
|
+
if (node[SCOPE_PROPERTY]) return true;
|
|
519
|
+
node = node.parentElement;
|
|
520
|
+
}
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
function scopeOf(element) {
|
|
524
|
+
let node = element;
|
|
525
|
+
while (node && node.nodeType === 1) {
|
|
526
|
+
if (node[SCOPE_PROPERTY]) return node;
|
|
527
|
+
node = node.parentElement;
|
|
528
|
+
}
|
|
529
|
+
return document;
|
|
530
|
+
}
|
|
531
|
+
//#endregion
|
|
532
|
+
//#region src/core/actions.js
|
|
533
|
+
const debounceTimers = /* @__PURE__ */ new WeakMap();
|
|
534
|
+
var Actions = class Actions {
|
|
535
|
+
static #nonBubblingEvents = /* @__PURE__ */ new Set([
|
|
536
|
+
"mouseenter",
|
|
537
|
+
"mouseleave",
|
|
538
|
+
"focus",
|
|
539
|
+
"blur",
|
|
540
|
+
"load",
|
|
541
|
+
"error",
|
|
542
|
+
"unload",
|
|
543
|
+
"resize",
|
|
544
|
+
"scroll"
|
|
545
|
+
]);
|
|
546
|
+
#registry;
|
|
547
|
+
#events;
|
|
548
|
+
#eventTypes;
|
|
549
|
+
#triggers;
|
|
550
|
+
#listeners;
|
|
551
|
+
#element;
|
|
552
|
+
#scope;
|
|
553
|
+
constructor(registry, events, eventTypes, triggers, listeners, element) {
|
|
554
|
+
this.#registry = registry;
|
|
555
|
+
this.#events = events;
|
|
556
|
+
this.#eventTypes = eventTypes;
|
|
557
|
+
this.#triggers = triggers;
|
|
558
|
+
this.#listeners = listeners;
|
|
559
|
+
this.#element = element;
|
|
560
|
+
this.#scope = element;
|
|
561
|
+
}
|
|
562
|
+
prepare(element) {
|
|
563
|
+
const attributes = getActionAttributes({ on: element });
|
|
564
|
+
if (attributes.length === 0) return;
|
|
565
|
+
this.#registerDirectListeners({
|
|
566
|
+
on: element,
|
|
567
|
+
from: attributes
|
|
568
|
+
});
|
|
569
|
+
this.#registerDelegationListeners({
|
|
570
|
+
on: element,
|
|
571
|
+
from: attributes
|
|
572
|
+
});
|
|
573
|
+
this.#setupDirectiveTriggers({
|
|
574
|
+
on: element,
|
|
575
|
+
from: attributes
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
process(event, context = null) {
|
|
579
|
+
const element = context ? context.element : this.#actionElement(event.target);
|
|
580
|
+
if (!element) return;
|
|
581
|
+
if (!this.#scope.contains(element)) return;
|
|
582
|
+
if (insideNestedScope(element, this.#scope)) return;
|
|
583
|
+
this.#debounced(() => this.#execute({
|
|
584
|
+
event,
|
|
585
|
+
with: context,
|
|
586
|
+
on: element
|
|
587
|
+
}), element);
|
|
588
|
+
}
|
|
589
|
+
#registerDirectListeners({ on: element, from: attributes }) {
|
|
590
|
+
for (const { event: eventName, modifiers, value } of attributes) {
|
|
591
|
+
const eventType = eventName || this.#defaultEventType({ for: element });
|
|
592
|
+
if (modifiers.includes("window") || modifiers.includes("document")) {
|
|
593
|
+
const target = modifiers.includes("window") ? window : document;
|
|
594
|
+
this.#listeners.addTargetedEventListener({
|
|
595
|
+
for: eventType,
|
|
596
|
+
on: target,
|
|
597
|
+
element
|
|
598
|
+
});
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (Actions.#nonBubblingEvents.has(eventType)) element.addEventListener(eventType, (event) => {
|
|
602
|
+
if (insideNestedScope(element, this.#scope)) return;
|
|
603
|
+
this.#events.process(event, {
|
|
604
|
+
on: element,
|
|
605
|
+
using: this.#defaultEventType({ for: element }),
|
|
606
|
+
with: value
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
#registerDelegationListeners({ on: element, from: attributes }) {
|
|
612
|
+
const eventTypes = /* @__PURE__ */ new Set();
|
|
613
|
+
for (const { event: eventName, modifiers } of attributes) {
|
|
614
|
+
if (modifiers.includes("window") || modifiers.includes("document")) continue;
|
|
615
|
+
const eventType = eventName || this.#defaultEventType({ for: element });
|
|
616
|
+
if (Actions.#nonBubblingEvents.has(eventType)) continue;
|
|
617
|
+
eventTypes.add(eventType);
|
|
618
|
+
}
|
|
619
|
+
eventTypes.forEach((eventType) => {
|
|
620
|
+
this.#listeners.addEventListeners({
|
|
621
|
+
for: eventType,
|
|
622
|
+
on: this.#element
|
|
623
|
+
});
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
#setupDirectiveTriggers({ on: element, from: attributes }) {
|
|
627
|
+
const directives = [...new Set(attributes.filter(({ value }) => !value.startsWith("js:")).flatMap(({ value }) => value.split(" ").filter((name) => name.includes(":")).flatMap((name) => name.split(":").slice(1))))];
|
|
628
|
+
const defaultEventType = this.#defaultEventType({ for: element });
|
|
629
|
+
directives.forEach((name) => {
|
|
630
|
+
this.#triggers.setup({
|
|
631
|
+
for: name,
|
|
632
|
+
on: element,
|
|
633
|
+
trigger: (state) => {
|
|
634
|
+
this.#debounced(() => {
|
|
635
|
+
attributes.forEach(({ value: actionValue }) => {
|
|
636
|
+
this.#events.process({ type: name }, {
|
|
637
|
+
on: element,
|
|
638
|
+
using: defaultEventType,
|
|
639
|
+
triggeredBy: name,
|
|
640
|
+
with: actionValue,
|
|
641
|
+
state
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
}, element);
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
#debounced(run, element) {
|
|
650
|
+
const delay = this.#debounceDelay(element);
|
|
651
|
+
if (delay > 0) {
|
|
652
|
+
let timer = debounceTimers.get(element);
|
|
653
|
+
if (!timer) {
|
|
654
|
+
timer = debounce();
|
|
655
|
+
debounceTimers.set(element, timer);
|
|
656
|
+
}
|
|
657
|
+
timer(run, delay);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
run();
|
|
661
|
+
}
|
|
662
|
+
#debounceDelay(element) {
|
|
663
|
+
return parseInt(element.dataset.debounce ?? element.dataset.formDebounce ?? element.dataset.requestDebounce ?? 0);
|
|
664
|
+
}
|
|
665
|
+
#execute({ event, with: context, on: element }) {
|
|
666
|
+
if (!this.#scope.contains(element)) return;
|
|
667
|
+
if (insideNestedScope(element, this.#scope)) return;
|
|
668
|
+
const defaultEventType = context ? context.eventType : this.#defaultEventType({ for: element });
|
|
669
|
+
const attributes = getActionAttributes({ on: element });
|
|
670
|
+
for (const { event: eventName, modifiers, value } of attributes) {
|
|
671
|
+
if ((eventName !== null ? eventName : defaultEventType) !== event.type) continue;
|
|
672
|
+
if (this.#blockedByEventModifiers({
|
|
673
|
+
for: event,
|
|
674
|
+
on: element,
|
|
675
|
+
modifiers
|
|
676
|
+
})) continue;
|
|
677
|
+
this.#events.process(event, {
|
|
678
|
+
on: element,
|
|
679
|
+
using: defaultEventType,
|
|
680
|
+
with: value
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
#blockedByEventModifiers({ for: event, on: element, modifiers }) {
|
|
685
|
+
return modifiers.some((name) => {
|
|
686
|
+
const eventModifier = this.#registry.getEventModifier(name);
|
|
687
|
+
if (eventModifier) return eventModifier(event, element) === false;
|
|
688
|
+
if (event.key === void 0) return false;
|
|
689
|
+
return event.key.toLowerCase() !== name.toLowerCase();
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
#defaultEventType({ for: element }) {
|
|
693
|
+
return this.#eventTypes.getDefault({ from: element });
|
|
694
|
+
}
|
|
695
|
+
#actionElement(element) {
|
|
696
|
+
while (element) {
|
|
697
|
+
if (actionAttributes(element)) return element;
|
|
698
|
+
element = element.parentElement;
|
|
699
|
+
}
|
|
700
|
+
return null;
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
//#endregion
|
|
704
|
+
//#region src/core/event_listeners.js
|
|
705
|
+
var EventListeners = class {
|
|
706
|
+
#eventListeners = /* @__PURE__ */ new Map();
|
|
707
|
+
#elementListeners = /* @__PURE__ */ new WeakMap();
|
|
708
|
+
#targetedEventListeners = /* @__PURE__ */ new Map();
|
|
709
|
+
#targetedEvents = /* @__PURE__ */ new Map();
|
|
710
|
+
#process;
|
|
711
|
+
constructor(process) {
|
|
712
|
+
this.#process = process;
|
|
713
|
+
}
|
|
714
|
+
addEventListeners({ for: eventType, on: element }) {
|
|
715
|
+
if (this.#eventListeners.has(eventType)) return;
|
|
716
|
+
const processEvent = (event) => this.#process(event);
|
|
717
|
+
element.addEventListener(eventType, processEvent);
|
|
718
|
+
Debug.log("Added event listener for", eventType, "to", element);
|
|
719
|
+
this.#eventListeners.set(eventType, {
|
|
720
|
+
listener: processEvent,
|
|
721
|
+
element
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
addTargetedEventListener({ for: eventType, on: target, element }) {
|
|
725
|
+
const key = `${target === window ? "window" : "document"}:${eventType}`;
|
|
726
|
+
if (!this.#elementListeners.has(element)) this.#elementListeners.set(element, /* @__PURE__ */ new Set());
|
|
727
|
+
this.#elementListeners.get(element).add(key);
|
|
728
|
+
if (!this.#targetedEvents.has(key)) this.#targetedEvents.set(key, /* @__PURE__ */ new Set());
|
|
729
|
+
this.#targetedEvents.get(key).add(element);
|
|
730
|
+
if (!this.#targetedEventListeners.has(key)) {
|
|
731
|
+
const processElements = (event) => {
|
|
732
|
+
const elements = this.#targetedEvents.get(key);
|
|
733
|
+
if (!elements) return;
|
|
734
|
+
elements.forEach((element) => {
|
|
735
|
+
this.#process(event, {
|
|
736
|
+
element,
|
|
737
|
+
eventType
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
};
|
|
741
|
+
target.addEventListener(eventType, processElements);
|
|
742
|
+
this.#targetedEventListeners.set(key, processElements);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
cleanup(element) {
|
|
746
|
+
const keys = this.#elementListeners.get(element);
|
|
747
|
+
if (!keys) return;
|
|
748
|
+
keys.forEach((key) => {
|
|
749
|
+
const events = this.#targetedEvents.get(key);
|
|
750
|
+
if (events) {
|
|
751
|
+
events.delete(element);
|
|
752
|
+
if (events.size === 0) {
|
|
753
|
+
const [targetName, eventType] = key.split(":");
|
|
754
|
+
const listener = this.#targetedEventListeners.get(key);
|
|
755
|
+
(targetName === "window" ? window : document).removeEventListener(eventType, listener);
|
|
756
|
+
this.#targetedEventListeners.delete(key);
|
|
757
|
+
this.#targetedEvents.delete(key);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
this.#elementListeners.delete(element);
|
|
762
|
+
}
|
|
763
|
+
removeAll() {
|
|
764
|
+
for (const [eventType, { listener, element }] of this.#eventListeners) element.removeEventListener(eventType, listener);
|
|
765
|
+
this.#eventListeners.clear();
|
|
766
|
+
for (const [key, listener] of this.#targetedEventListeners) {
|
|
767
|
+
const [targetName, eventType] = key.split(":");
|
|
768
|
+
(targetName === "window" ? window : document).removeEventListener(eventType, listener);
|
|
769
|
+
}
|
|
770
|
+
this.#targetedEventListeners.clear();
|
|
771
|
+
this.#targetedEvents.clear();
|
|
772
|
+
this.#elementListeners = /* @__PURE__ */ new WeakMap();
|
|
773
|
+
}
|
|
774
|
+
};
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region src/core/observer.js
|
|
777
|
+
const ELEMENT_NODE = typeof Node !== "undefined" && Node.ELEMENT_NODE || 1;
|
|
778
|
+
const ATTRIBUTE_PREFIX = "@";
|
|
779
|
+
const RESERVED_ATTRIBUTES = /* @__PURE__ */ new Set(["@target", "@targets"]);
|
|
780
|
+
var Observer = class {
|
|
781
|
+
#prepare;
|
|
782
|
+
#cleanup;
|
|
783
|
+
#beforeCleanup;
|
|
784
|
+
#observer;
|
|
785
|
+
#scope;
|
|
786
|
+
constructor(prepare, cleanup = null, scope = document.documentElement, beforeCleanup = null) {
|
|
787
|
+
this.#prepare = prepare;
|
|
788
|
+
this.#cleanup = cleanup;
|
|
789
|
+
this.#beforeCleanup = beforeCleanup;
|
|
790
|
+
this.#scope = scope;
|
|
791
|
+
}
|
|
792
|
+
start(action) {
|
|
793
|
+
if (!window.MutationObserver) return;
|
|
794
|
+
this.#observer = new MutationObserver((mutations) => {
|
|
795
|
+
const added = /* @__PURE__ */ new Set();
|
|
796
|
+
const removed = /* @__PURE__ */ new Set();
|
|
797
|
+
const changed = /* @__PURE__ */ new Set();
|
|
798
|
+
for (const mutation of mutations) {
|
|
799
|
+
if (mutation.type === "childList") {
|
|
800
|
+
this.#processChildMutation(mutation, {
|
|
801
|
+
for: action,
|
|
802
|
+
elements: {
|
|
803
|
+
added,
|
|
804
|
+
removed
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
if (mutation.type === "attributes" && mutation.attributeName.startsWith(ATTRIBUTE_PREFIX) && !RESERVED_ATTRIBUTES.has(mutation.attributeName)) changed.add(mutation.target);
|
|
810
|
+
}
|
|
811
|
+
added.forEach((element) => {
|
|
812
|
+
Debug.log("Element added:", element.tagName.toLowerCase(), "#" + (element.id || element.dataset.target || ""));
|
|
813
|
+
this.#prepare(element);
|
|
814
|
+
});
|
|
815
|
+
if (this.#beforeCleanup || this.#cleanup) removed.forEach((element) => {
|
|
816
|
+
Debug.log("Element removed:", element.tagName.toLowerCase(), "#" + (element.id || ""));
|
|
817
|
+
this.#beforeCleanup?.(element);
|
|
818
|
+
this.#cleanup?.(element);
|
|
819
|
+
});
|
|
820
|
+
if (this.#cleanup) changed.forEach((element) => {
|
|
821
|
+
if (removed.has(element)) return;
|
|
822
|
+
Debug.log("Attribute changed:", element.tagName.toLowerCase(), "#" + (element.id || ""));
|
|
823
|
+
this.#cleanup(element);
|
|
824
|
+
if (action(element)) this.#prepare(element);
|
|
825
|
+
});
|
|
826
|
+
});
|
|
827
|
+
this.#observer.observe(this.#scope, {
|
|
828
|
+
childList: true,
|
|
829
|
+
subtree: true,
|
|
830
|
+
attributes: true
|
|
831
|
+
});
|
|
832
|
+
return this;
|
|
833
|
+
}
|
|
834
|
+
stop() {
|
|
835
|
+
if (this.#observer) this.#observer.disconnect();
|
|
836
|
+
return this;
|
|
837
|
+
}
|
|
838
|
+
#processChildMutation(mutation, { for: action, elements: { added, removed } }) {
|
|
839
|
+
mutation.addedNodes.forEach((node) => {
|
|
840
|
+
this.#processNode(node, {
|
|
841
|
+
for: action,
|
|
842
|
+
and: added
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
mutation.removedNodes.forEach((node) => {
|
|
846
|
+
this.#processNode(node, {
|
|
847
|
+
for: action,
|
|
848
|
+
and: removed
|
|
849
|
+
});
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
#processNode(node, { for: action, and: elements }) {
|
|
853
|
+
if (node.nodeType !== ELEMENT_NODE) return;
|
|
854
|
+
if (action(node)) elements.add(node);
|
|
855
|
+
if (!node.querySelectorAll) return;
|
|
856
|
+
node.querySelectorAll("*").forEach((element) => {
|
|
857
|
+
if (action(element)) elements.add(element);
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
//#endregion
|
|
862
|
+
//#region src/core/activation.js
|
|
863
|
+
var Activation = class {
|
|
864
|
+
#registry;
|
|
865
|
+
#scope;
|
|
866
|
+
#events;
|
|
867
|
+
#eventTypes;
|
|
868
|
+
#triggers;
|
|
869
|
+
#listeners;
|
|
870
|
+
#elementLifecycle;
|
|
871
|
+
#subscriptions;
|
|
872
|
+
#attributePrefixes;
|
|
873
|
+
#owner;
|
|
874
|
+
#actions;
|
|
875
|
+
#observe;
|
|
876
|
+
#initialized = false;
|
|
877
|
+
constructor(dependencies) {
|
|
878
|
+
this.#registry = dependencies.registry;
|
|
879
|
+
this.#events = dependencies.events;
|
|
880
|
+
this.#eventTypes = dependencies.eventTypes;
|
|
881
|
+
this.#triggers = dependencies.triggers;
|
|
882
|
+
this.#elementLifecycle = dependencies.elementLifecycle;
|
|
883
|
+
this.#subscriptions = dependencies.subscriptions;
|
|
884
|
+
this.#attributePrefixes = dependencies.attributePrefixes;
|
|
885
|
+
this.#owner = dependencies.owner;
|
|
886
|
+
this.#listeners = new EventListeners((event, context) => this.#actions?.process(event, context));
|
|
887
|
+
}
|
|
888
|
+
get active() {
|
|
889
|
+
return this.#initialized;
|
|
890
|
+
}
|
|
891
|
+
activate(options = {}) {
|
|
892
|
+
const { on = document, debug = false, extendWith = [], addActions = {}, addGates = {}, addTriggers = {} } = options;
|
|
893
|
+
Debug.enabled = debug;
|
|
894
|
+
if (!on) {
|
|
895
|
+
Debug.error("scope element not found: activate() requires a valid DOM element");
|
|
896
|
+
return this;
|
|
897
|
+
}
|
|
898
|
+
if (this.#initialized) return this;
|
|
899
|
+
for (const [name, action] of Object.entries(addActions)) this.#registry.addAction(name, action);
|
|
900
|
+
for (const [name, action] of Object.entries(addGates)) this.#registry.addGate(name, action);
|
|
901
|
+
for (const [name, action] of Object.entries(addTriggers)) this.#registry.addTrigger(name, action);
|
|
902
|
+
this.#scope = on;
|
|
903
|
+
registerScope(on);
|
|
904
|
+
this.#subscriptions.setScope(on);
|
|
905
|
+
this.#actions = new Actions(this.#registry, this.#events, this.#eventTypes, this.#triggers, this.#listeners, on);
|
|
906
|
+
this.#observe = new Observer((element) => {
|
|
907
|
+
this.#elementLifecycle.notifyAdded(element);
|
|
908
|
+
this.#elementLifecycle.notifyTargetAdded(element);
|
|
909
|
+
this.#actions.prepare(element);
|
|
910
|
+
}, (element) => {
|
|
911
|
+
this.#listeners.cleanup(element);
|
|
912
|
+
this.#elementLifecycle.notifyRemoved(element);
|
|
913
|
+
this.#elementLifecycle.notifyTargetRemoved(element);
|
|
914
|
+
}, on, (element) => {
|
|
915
|
+
this.#elementLifecycle.notifyBeforeRemove(element);
|
|
916
|
+
});
|
|
917
|
+
const elements = [on, ...on.querySelectorAll("*")];
|
|
918
|
+
const actionElements = [];
|
|
919
|
+
for (const element of elements) if (this.#shouldPrepare(element)) actionElements.push(element);
|
|
920
|
+
for (const extension of extendWith) extension({
|
|
921
|
+
instance: this.#owner,
|
|
922
|
+
registry: this.#registry
|
|
923
|
+
});
|
|
924
|
+
actionElements.forEach((element) => {
|
|
925
|
+
this.#elementLifecycle.notifyAdded(element);
|
|
926
|
+
this.#elementLifecycle.notifyTargetAdded(element);
|
|
927
|
+
});
|
|
928
|
+
actionElements.forEach((element) => {
|
|
929
|
+
this.#actions.prepare(element);
|
|
930
|
+
});
|
|
931
|
+
this.#observe.start((element) => this.#shouldPrepare(element));
|
|
932
|
+
this.#initialized = true;
|
|
933
|
+
if (Debug.enabled) Debug.log(`active — ${actionElements.length} element${actionElements.length === 1 ? "" : "s"} with actions`);
|
|
934
|
+
return this;
|
|
935
|
+
}
|
|
936
|
+
deactivate() {
|
|
937
|
+
if (!this.#initialized) return this;
|
|
938
|
+
unregisterScope(this.#scope);
|
|
939
|
+
this.#listeners.removeAll();
|
|
940
|
+
if (this.#observe) this.#observe.stop();
|
|
941
|
+
this.#subscriptions.removeAll();
|
|
942
|
+
this.#elementLifecycle.clear();
|
|
943
|
+
this.#attributePrefixes.clear();
|
|
944
|
+
this.#initialized = false;
|
|
945
|
+
Debug.log("deactivated");
|
|
946
|
+
return this;
|
|
947
|
+
}
|
|
948
|
+
notifyExistingTargets(id) {
|
|
949
|
+
if (!this.#scope) return this;
|
|
950
|
+
this.#scope.querySelectorAll(`[id="${id}"]`).forEach((element) => {
|
|
951
|
+
this.#elementLifecycle.notifyTargetAdded(element);
|
|
952
|
+
});
|
|
953
|
+
return this;
|
|
954
|
+
}
|
|
955
|
+
#shouldPrepare(element) {
|
|
956
|
+
return (this.#attributePrefixes.matches(element) || this.#elementLifecycle.hasTarget(element.id)) && !insideNestedScope(element, this.#scope);
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
//#endregion
|
|
960
|
+
//#region src/core/default_event_modifier.js
|
|
961
|
+
function defaultEventModifier(registry) {
|
|
962
|
+
registry.addEventModifier("window", () => true);
|
|
963
|
+
registry.addEventModifier("document", () => true);
|
|
964
|
+
}
|
|
965
|
+
//#endregion
|
|
966
|
+
//#region src/core.js
|
|
967
|
+
var Attractive = class Attractive {
|
|
968
|
+
#registry = new Registry();
|
|
969
|
+
#events;
|
|
970
|
+
#eventTypes;
|
|
971
|
+
#triggers;
|
|
972
|
+
#activation;
|
|
973
|
+
#hooks = new Hooks();
|
|
974
|
+
#elementLifecycle = new ElementLifecycleHooks();
|
|
975
|
+
#subscriptions = new EventSubscriptions();
|
|
976
|
+
#attributePrefixes = new AttributePrefixes();
|
|
977
|
+
static activate(options = {}) {
|
|
978
|
+
const instance = new this(options);
|
|
979
|
+
instance.activate(options);
|
|
980
|
+
return instance;
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Returns whether debug logging is enabled.
|
|
984
|
+
*
|
|
985
|
+
* @returns {boolean} — current debug state
|
|
986
|
+
*/
|
|
987
|
+
static get debug() {
|
|
988
|
+
return Debug.enabled;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Toggle debug logging on or off.
|
|
992
|
+
*
|
|
993
|
+
* @param {boolean} value — true to enable debug output
|
|
994
|
+
*/
|
|
995
|
+
static set debug(value) {
|
|
996
|
+
Debug.enabled = value;
|
|
997
|
+
}
|
|
998
|
+
get debug() {
|
|
999
|
+
return Debug.enabled;
|
|
1000
|
+
}
|
|
1001
|
+
set debug(value) {
|
|
1002
|
+
Debug.enabled = value;
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Returns whether the instance is currently active.
|
|
1006
|
+
*
|
|
1007
|
+
* @returns {boolean} — true if activate() has been called and deactivate() has not
|
|
1008
|
+
*/
|
|
1009
|
+
get active() {
|
|
1010
|
+
return this.#activation.active;
|
|
1011
|
+
}
|
|
1012
|
+
static onError(error, message) {
|
|
1013
|
+
console.warn(`[attractive] ${message}`, error);
|
|
1014
|
+
if (typeof window.onerror === "function") window.onerror(message, null, null, null, error);
|
|
1015
|
+
}
|
|
1016
|
+
constructor() {
|
|
1017
|
+
this.#events = new Events(this.#registry, this.#hooks, (error, message, detail) => Attractive.onError(error, message, detail));
|
|
1018
|
+
this.#eventTypes = new EventTypes();
|
|
1019
|
+
this.#triggers = new Triggers(this.#registry);
|
|
1020
|
+
defaultEventModifier(this.#registry);
|
|
1021
|
+
this.#activation = new Activation({
|
|
1022
|
+
registry: this.#registry,
|
|
1023
|
+
events: this.#events,
|
|
1024
|
+
eventTypes: this.#eventTypes,
|
|
1025
|
+
triggers: this.#triggers,
|
|
1026
|
+
elementLifecycle: this.#elementLifecycle,
|
|
1027
|
+
subscriptions: this.#subscriptions,
|
|
1028
|
+
attributePrefixes: this.#attributePrefixes,
|
|
1029
|
+
owner: this
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* Activates the Attractive instance on the given scope.
|
|
1034
|
+
*
|
|
1035
|
+
* @param {Object} [options] — activation options
|
|
1036
|
+
* @param {HTMLElement|Document} [options.on=document] — root element to observe
|
|
1037
|
+
* @param {boolean} [options.debug=false] — enable debug logging
|
|
1038
|
+
* @returns {Attractive} — the instance for chaining
|
|
1039
|
+
*/
|
|
1040
|
+
activate(options = {}) {
|
|
1041
|
+
this.#activation.activate(options);
|
|
1042
|
+
return this;
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Restricts available actions to the given names.
|
|
1046
|
+
*
|
|
1047
|
+
* @param {string[]} [actionNames] — action names to enable (all enabled if empty)
|
|
1048
|
+
* @returns {Attractive} — the instance for chaining
|
|
1049
|
+
*/
|
|
1050
|
+
withActions(actionNames = []) {
|
|
1051
|
+
Debug.log("Initializing with actions", actionNames);
|
|
1052
|
+
if (actionNames.length > 0) this.#registry.setActiveActions(new Set(actionNames));
|
|
1053
|
+
return this;
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Registers a callback that runs before each action.
|
|
1057
|
+
* Return false to cancel the action.
|
|
1058
|
+
*
|
|
1059
|
+
* @param {Function} callback — receives { name, element, options, event }
|
|
1060
|
+
* @returns {Attractive} — the instance for chaining
|
|
1061
|
+
*/
|
|
1062
|
+
beforeAction(callback) {
|
|
1063
|
+
this.#hooks.addBefore(callback);
|
|
1064
|
+
return this;
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Registers a callback that runs after each successful action.
|
|
1068
|
+
*
|
|
1069
|
+
* @param {Function} callback — receives { name, element, options, event, result }
|
|
1070
|
+
* @returns {Attractive} — the instance for chaining
|
|
1071
|
+
*/
|
|
1072
|
+
afterAction(callback) {
|
|
1073
|
+
this.#hooks.addAfter(callback);
|
|
1074
|
+
return this;
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Registers a callback that runs when an action throws an error.
|
|
1078
|
+
*
|
|
1079
|
+
* @param {Function} callback — receives { name, element, options, event, error }
|
|
1080
|
+
* @returns {Attractive} — the instance for chaining
|
|
1081
|
+
*/
|
|
1082
|
+
onError(callback) {
|
|
1083
|
+
this.#hooks.addError(callback);
|
|
1084
|
+
return this;
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Deactivates the instance, removing all event listeners and observer.
|
|
1088
|
+
*
|
|
1089
|
+
* @returns {Attractive} — the instance for chaining
|
|
1090
|
+
*/
|
|
1091
|
+
deactivate() {
|
|
1092
|
+
this.#activation.deactivate();
|
|
1093
|
+
return this;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Deactivates and reactivates the instance with optional new options.
|
|
1097
|
+
*
|
|
1098
|
+
* @param {Object} [options] — same options as activate()
|
|
1099
|
+
* @returns {Attractive} — the instance for chaining
|
|
1100
|
+
*/
|
|
1101
|
+
restart(options = {}) {
|
|
1102
|
+
this.deactivate();
|
|
1103
|
+
return this.activate(options);
|
|
1104
|
+
}
|
|
1105
|
+
onElementAdded(callback) {
|
|
1106
|
+
this.#elementLifecycle.onAdded(callback);
|
|
1107
|
+
return this;
|
|
1108
|
+
}
|
|
1109
|
+
onElementRemoved(callback) {
|
|
1110
|
+
this.#elementLifecycle.onRemoved(callback);
|
|
1111
|
+
return this;
|
|
1112
|
+
}
|
|
1113
|
+
onBeforeElementRemoved(callback) {
|
|
1114
|
+
this.#elementLifecycle.onBeforeRemove(callback);
|
|
1115
|
+
return this;
|
|
1116
|
+
}
|
|
1117
|
+
onTargetConnected(id, callback) {
|
|
1118
|
+
this.#elementLifecycle.onTargetAdded(id, callback);
|
|
1119
|
+
this.#activation.notifyExistingTargets(id);
|
|
1120
|
+
return this;
|
|
1121
|
+
}
|
|
1122
|
+
onTargetDisconnected(id, callback) {
|
|
1123
|
+
this.#elementLifecycle.onTargetRemoved(id, callback);
|
|
1124
|
+
return this;
|
|
1125
|
+
}
|
|
1126
|
+
addEventListener(type, callback) {
|
|
1127
|
+
this.#subscriptions.add(type, callback);
|
|
1128
|
+
return this;
|
|
1129
|
+
}
|
|
1130
|
+
observeAttribute(prefix) {
|
|
1131
|
+
this.#attributePrefixes.add(prefix);
|
|
1132
|
+
return this;
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
//#endregion
|
|
1136
|
+
//#region src/actions/base.js
|
|
1137
|
+
var ActionBase = class {
|
|
1138
|
+
static actionFor(method) {
|
|
1139
|
+
return (element, options = {}) => {
|
|
1140
|
+
return new this(element, options)[method]();
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
constructor(element, options = {}) {
|
|
1144
|
+
if (!element) throw new Error("Current element is required");
|
|
1145
|
+
this.element = element;
|
|
1146
|
+
this.target = options.target;
|
|
1147
|
+
this.targetsSelector = options.targets;
|
|
1148
|
+
this.options = options;
|
|
1149
|
+
}
|
|
1150
|
+
get targets() {
|
|
1151
|
+
const scope = scopeOf(this.element);
|
|
1152
|
+
if (this.targetsSelector) return Array.from(scope.querySelectorAll(this.targetsSelector));
|
|
1153
|
+
if (this.target) {
|
|
1154
|
+
const target = scope.querySelector(`[id="${this.target}"]`);
|
|
1155
|
+
if (!target) Debug.warn(`Target "#${this.target}" not found`);
|
|
1156
|
+
return target ? [target] : [];
|
|
1157
|
+
}
|
|
1158
|
+
return [this.element];
|
|
1159
|
+
}
|
|
1160
|
+
cycledValue(currentValue, nextValues) {
|
|
1161
|
+
const values = Array.isArray(nextValues) ? nextValues : nextValues.split(",").map((value) => value.trim());
|
|
1162
|
+
return values[(values.indexOf(currentValue) + 1) % values.length];
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
//#endregion
|
|
1166
|
+
//#region src/action-class/index.js
|
|
1167
|
+
var Action = class extends ActionBase {
|
|
1168
|
+
get value() {
|
|
1169
|
+
return this.options.value;
|
|
1170
|
+
}
|
|
1171
|
+
get dataset() {
|
|
1172
|
+
return this.element.dataset;
|
|
1173
|
+
}
|
|
1174
|
+
dispatchEvent(name, detail = {}) {
|
|
1175
|
+
this.element.dispatchEvent(new CustomEvent(name, {
|
|
1176
|
+
bubbles: true,
|
|
1177
|
+
detail
|
|
1178
|
+
}));
|
|
1179
|
+
}
|
|
1180
|
+
};
|
|
1181
|
+
//#endregion
|
|
1182
|
+
//#region src/actions/helpers/sanitize.js
|
|
1183
|
+
const sanitize = (value) => {
|
|
1184
|
+
return value?.split(",").map((className) => className.trim()).filter(Boolean) ?? [];
|
|
1185
|
+
};
|
|
1186
|
+
//#endregion
|
|
1187
|
+
//#region src/actions/class.js
|
|
1188
|
+
var Class = class extends ActionBase {
|
|
1189
|
+
constructor(element, options = {}) {
|
|
1190
|
+
super(element, options);
|
|
1191
|
+
this.value = sanitize(options.value);
|
|
1192
|
+
}
|
|
1193
|
+
toggle() {
|
|
1194
|
+
if (!this.value) return;
|
|
1195
|
+
this.targets.forEach((target) => this.#toggleClasses({ forEach: target }));
|
|
1196
|
+
}
|
|
1197
|
+
cycle() {
|
|
1198
|
+
if (!this.value || this.value.length === 0) return;
|
|
1199
|
+
this.targets.forEach((target) => this.#cycleClasses(target));
|
|
1200
|
+
}
|
|
1201
|
+
add() {
|
|
1202
|
+
if (!this.value) return;
|
|
1203
|
+
this.targets.forEach((target) => target.classList.add(...this.value));
|
|
1204
|
+
}
|
|
1205
|
+
set() {
|
|
1206
|
+
const key = this.element.dataset.store;
|
|
1207
|
+
if (key) {
|
|
1208
|
+
const value = globalThis.$store?.get(key);
|
|
1209
|
+
if (value === void 0) return;
|
|
1210
|
+
this.targets.forEach((target) => target.classList.toggle(...this.value, !!value));
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
if (!this.value) return;
|
|
1214
|
+
this.targets.forEach((target) => {
|
|
1215
|
+
target.className = "";
|
|
1216
|
+
target.classList.add(...this.value);
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
remove() {
|
|
1220
|
+
if (!this.value) return;
|
|
1221
|
+
this.targets.forEach((target) => target.classList.remove(...this.value));
|
|
1222
|
+
}
|
|
1223
|
+
#toggleClasses({ forEach: target }) {
|
|
1224
|
+
this.value.forEach((className) => target.classList.toggle(className));
|
|
1225
|
+
}
|
|
1226
|
+
#cycleClasses(target) {
|
|
1227
|
+
const currentClass = this.value.find((className) => target.classList.contains(className)) || "";
|
|
1228
|
+
const nextClass = this.cycledValue(currentClass, this.value);
|
|
1229
|
+
target.classList.remove(...this.value);
|
|
1230
|
+
target.classList.add(nextClass);
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
const actions$9 = {
|
|
1234
|
+
toggleClass: Class.actionFor("toggle"),
|
|
1235
|
+
cycleClass: Class.actionFor("cycle"),
|
|
1236
|
+
addClass: Class.actionFor("add"),
|
|
1237
|
+
setClass: Class.actionFor("set"),
|
|
1238
|
+
removeClass: Class.actionFor("remove")
|
|
1239
|
+
};
|
|
1240
|
+
//#endregion
|
|
1241
|
+
//#region src/actions/helpers/delay.js
|
|
1242
|
+
const delay = () => {
|
|
1243
|
+
let timeoutId;
|
|
1244
|
+
return (callback, delay) => {
|
|
1245
|
+
clearTimeout(timeoutId);
|
|
1246
|
+
timeoutId = setTimeout(callback, delay);
|
|
1247
|
+
};
|
|
1248
|
+
};
|
|
1249
|
+
//#endregion
|
|
1250
|
+
//#region src/actions/clipboard.js
|
|
1251
|
+
const clearFeedback$1 = delay();
|
|
1252
|
+
var Clipboard = class extends ActionBase {
|
|
1253
|
+
constructor(element, options = {}) {
|
|
1254
|
+
super(element, options);
|
|
1255
|
+
this.value = options.value;
|
|
1256
|
+
}
|
|
1257
|
+
async copy() {
|
|
1258
|
+
const textToCopy = this.value || (this.targets[0]?.value ?? this.targets[0]?.textContent);
|
|
1259
|
+
if (textToCopy === void 0) return;
|
|
1260
|
+
try {
|
|
1261
|
+
await navigator.clipboard.writeText(textToCopy);
|
|
1262
|
+
this.#setFeedback(true);
|
|
1263
|
+
} catch (error) {
|
|
1264
|
+
this.#setFeedback(false);
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
#setFeedback(succeeded) {
|
|
1268
|
+
const delay = parseInt(this.element.dataset.copyFeedback);
|
|
1269
|
+
this.targets.forEach((target) => target.setAttribute(this.#attributeName, succeeded));
|
|
1270
|
+
if (!delay) return;
|
|
1271
|
+
clearFeedback$1(() => this.targets.forEach((target) => target.removeAttribute(this.#attributeName)), delay);
|
|
1272
|
+
}
|
|
1273
|
+
get #attributeName() {
|
|
1274
|
+
return "data-copy-success";
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
const actions$8 = { copy: Clipboard.actionFor("copy") };
|
|
1278
|
+
//#endregion
|
|
1279
|
+
//#region src/actions/confirm.js
|
|
1280
|
+
const clearFeedback = delay();
|
|
1281
|
+
var Confirm = class extends ActionBase {
|
|
1282
|
+
confirm() {
|
|
1283
|
+
const message = this.element.dataset.confirmMessage || "Are you sure?";
|
|
1284
|
+
const confirmed = window.confirm(message);
|
|
1285
|
+
this.#setFeedback(confirmed);
|
|
1286
|
+
return confirmed;
|
|
1287
|
+
}
|
|
1288
|
+
#setFeedback(confirmed) {
|
|
1289
|
+
this.element.setAttribute("data-confirm-success", confirmed);
|
|
1290
|
+
const duration = this.element.dataset.confirmFeedback;
|
|
1291
|
+
if (!duration) return;
|
|
1292
|
+
clearFeedback(() => this.element.removeAttribute("data-confirm-success"), parseInt(duration));
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
const actions$7 = { confirm: Confirm.actionFor("confirm") };
|
|
1296
|
+
//#endregion
|
|
1297
|
+
//#region src/actions/dom_attributes.js
|
|
1298
|
+
const attributeOperations = {
|
|
1299
|
+
get(element, name) {
|
|
1300
|
+
return element.getAttribute(name);
|
|
1301
|
+
},
|
|
1302
|
+
set(element, name, value) {
|
|
1303
|
+
element.setAttribute(name, value);
|
|
1304
|
+
},
|
|
1305
|
+
has(element, name) {
|
|
1306
|
+
return element.hasAttribute(name);
|
|
1307
|
+
},
|
|
1308
|
+
remove(element, name) {
|
|
1309
|
+
element.removeAttribute(name);
|
|
1310
|
+
}
|
|
1311
|
+
};
|
|
1312
|
+
const dataAttributeOperations = {
|
|
1313
|
+
get(element, name) {
|
|
1314
|
+
return element.dataset[name];
|
|
1315
|
+
},
|
|
1316
|
+
set(element, name, value) {
|
|
1317
|
+
element.dataset[name] = value;
|
|
1318
|
+
},
|
|
1319
|
+
has(element, name) {
|
|
1320
|
+
return name in element.dataset;
|
|
1321
|
+
},
|
|
1322
|
+
remove(element, name) {
|
|
1323
|
+
delete element.dataset[name];
|
|
1324
|
+
}
|
|
1325
|
+
};
|
|
1326
|
+
var DOMAttribute = class extends ActionBase {
|
|
1327
|
+
constructor(element, options = {}) {
|
|
1328
|
+
super(element, options);
|
|
1329
|
+
const [attribute, value] = options.value.split("=");
|
|
1330
|
+
this.attribute = attribute;
|
|
1331
|
+
this.value = value;
|
|
1332
|
+
}
|
|
1333
|
+
toggle() {
|
|
1334
|
+
if (!this.attribute) return;
|
|
1335
|
+
this.targets.forEach((target) => this.operations.has(target, this.attribute) ? this.operations.remove(target, this.attribute) : this.operations.set(target, this.attribute, this.value || ""));
|
|
1336
|
+
}
|
|
1337
|
+
cycle() {
|
|
1338
|
+
if (!this.value) return;
|
|
1339
|
+
this.targets.forEach((target) => this.#cycleAttribute(target));
|
|
1340
|
+
}
|
|
1341
|
+
add() {
|
|
1342
|
+
if (!this.attribute) return;
|
|
1343
|
+
this.targets.forEach((target) => this.operations.set(target, this.attribute, this.value || ""));
|
|
1344
|
+
}
|
|
1345
|
+
set() {
|
|
1346
|
+
const key = this.element.dataset.store;
|
|
1347
|
+
if (key) {
|
|
1348
|
+
const value = globalThis.$store?.get(key);
|
|
1349
|
+
if (value === void 0) return;
|
|
1350
|
+
this.targets.forEach((target) => value ? this.operations.set(target, this.attribute, this.value || "") : this.operations.remove(target, this.attribute));
|
|
1351
|
+
return;
|
|
1352
|
+
}
|
|
1353
|
+
return this.add();
|
|
1354
|
+
}
|
|
1355
|
+
remove() {
|
|
1356
|
+
if (!this.attribute) return;
|
|
1357
|
+
this.targets.forEach((target) => this.operations.remove(target, this.attribute));
|
|
1358
|
+
}
|
|
1359
|
+
#cycleAttribute(target) {
|
|
1360
|
+
const currentValue = this.operations.get(target, this.attribute);
|
|
1361
|
+
const nextValue = this.cycledValue(currentValue, this.value);
|
|
1362
|
+
this.operations.set(target, this.attribute, nextValue);
|
|
1363
|
+
}
|
|
1364
|
+
};
|
|
1365
|
+
var Attribute = class extends DOMAttribute {
|
|
1366
|
+
get operations() {
|
|
1367
|
+
return attributeOperations;
|
|
1368
|
+
}
|
|
1369
|
+
};
|
|
1370
|
+
var DataAttribute = class extends DOMAttribute {
|
|
1371
|
+
get operations() {
|
|
1372
|
+
return dataAttributeOperations;
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
const toggleAttribute = Attribute.actionFor("toggle");
|
|
1376
|
+
const cycleAttribute = Attribute.actionFor("cycle");
|
|
1377
|
+
const addAttribute = Attribute.actionFor("add");
|
|
1378
|
+
const setAttribute = Attribute.actionFor("set");
|
|
1379
|
+
const removeAttribute = Attribute.actionFor("remove");
|
|
1380
|
+
const toggleDataAttribute = DataAttribute.actionFor("toggle");
|
|
1381
|
+
const cycleDataAttribute = DataAttribute.actionFor("cycle");
|
|
1382
|
+
const addDataAttribute = DataAttribute.actionFor("add");
|
|
1383
|
+
const setDataAttribute = DataAttribute.actionFor("set");
|
|
1384
|
+
const removeDataAttribute = DataAttribute.actionFor("remove");
|
|
1385
|
+
const attributeActions = {
|
|
1386
|
+
toggleAttribute,
|
|
1387
|
+
cycleAttribute,
|
|
1388
|
+
addAttribute,
|
|
1389
|
+
setAttribute,
|
|
1390
|
+
removeAttribute
|
|
1391
|
+
};
|
|
1392
|
+
const dataAttributeActions = {
|
|
1393
|
+
toggleDataAttribute,
|
|
1394
|
+
cycleDataAttribute,
|
|
1395
|
+
addDataAttribute,
|
|
1396
|
+
setDataAttribute,
|
|
1397
|
+
removeDataAttribute
|
|
1398
|
+
};
|
|
1399
|
+
//#endregion
|
|
1400
|
+
//#region src/actions/dialog.js
|
|
1401
|
+
var Dialog = class extends ActionBase {
|
|
1402
|
+
open() {
|
|
1403
|
+
this.targets.forEach((target) => target instanceof HTMLDialogElement && target.show());
|
|
1404
|
+
}
|
|
1405
|
+
openModal() {
|
|
1406
|
+
this.targets.forEach((target) => target instanceof HTMLDialogElement && target.showModal());
|
|
1407
|
+
}
|
|
1408
|
+
close() {
|
|
1409
|
+
this.targets.forEach((target) => target instanceof HTMLDialogElement && target.close());
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1412
|
+
const actions$6 = {
|
|
1413
|
+
open: Dialog.actionFor("open"),
|
|
1414
|
+
openModal: Dialog.actionFor("openModal"),
|
|
1415
|
+
close: Dialog.actionFor("close")
|
|
1416
|
+
};
|
|
1417
|
+
//#endregion
|
|
1418
|
+
//#region src/actions/focus.js
|
|
1419
|
+
var Focus = class extends ActionBase {
|
|
1420
|
+
focus() {
|
|
1421
|
+
this.targets.forEach((target) => {
|
|
1422
|
+
if (typeof target.focus === "function") target.focus();
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
};
|
|
1426
|
+
const actions$5 = { focus: Focus.actionFor("focus") };
|
|
1427
|
+
//#endregion
|
|
1428
|
+
//#region src/actions/form.js
|
|
1429
|
+
var Form = class extends ActionBase {
|
|
1430
|
+
requestSubmit() {
|
|
1431
|
+
this.targets.forEach((target) => {
|
|
1432
|
+
const form = target instanceof HTMLFormElement ? target : target.form || null;
|
|
1433
|
+
if (form) form.requestSubmit();
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
reset() {
|
|
1437
|
+
this.targets.forEach((target) => target instanceof HTMLFormElement && target.reset());
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
const actions$4 = {
|
|
1441
|
+
submit: Form.actionFor("requestSubmit"),
|
|
1442
|
+
reset: Form.actionFor("reset")
|
|
1443
|
+
};
|
|
1444
|
+
//#endregion
|
|
1445
|
+
//#region src/actions/reload.js
|
|
1446
|
+
var Reload = class extends ActionBase {
|
|
1447
|
+
reload() {
|
|
1448
|
+
this.targets.forEach((target) => {
|
|
1449
|
+
if (this.#reloadable(target)) target.reload();
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
get targets() {
|
|
1453
|
+
if (this.target === "window") return [window.location];
|
|
1454
|
+
return super.targets;
|
|
1455
|
+
}
|
|
1456
|
+
#reloadable(target) {
|
|
1457
|
+
return typeof target.reload === "function";
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
const reload = Reload.actionFor("reload");
|
|
1461
|
+
const actions$3 = {
|
|
1462
|
+
reload,
|
|
1463
|
+
refresh: reload
|
|
1464
|
+
};
|
|
1465
|
+
//#endregion
|
|
1466
|
+
//#region src/actions/scroll_to.js
|
|
1467
|
+
var ScrollTo = class extends ActionBase {
|
|
1468
|
+
constructor(element, options = {}) {
|
|
1469
|
+
super(element, options);
|
|
1470
|
+
const validBehaviors = [
|
|
1471
|
+
"auto",
|
|
1472
|
+
"instant",
|
|
1473
|
+
"smooth"
|
|
1474
|
+
];
|
|
1475
|
+
const behavior = options.value;
|
|
1476
|
+
this.behavior = validBehaviors.includes(behavior) ? behavior : "auto";
|
|
1477
|
+
}
|
|
1478
|
+
scroll() {
|
|
1479
|
+
this.targets[0]?.scrollIntoView({ behavior: this.behavior });
|
|
1480
|
+
}
|
|
1481
|
+
};
|
|
1482
|
+
const actions$2 = { scrollTo: ScrollTo.actionFor("scroll") };
|
|
1483
|
+
//#endregion
|
|
1484
|
+
//#region src/actions/style.js
|
|
1485
|
+
var Style = class extends ActionBase {
|
|
1486
|
+
constructor(element, options = {}) {
|
|
1487
|
+
super(element, options);
|
|
1488
|
+
const [prop, value] = (options.value || "").split("=");
|
|
1489
|
+
this.styleProperty = prop;
|
|
1490
|
+
this.styleValue = value;
|
|
1491
|
+
}
|
|
1492
|
+
set() {
|
|
1493
|
+
if (!this.styleProperty) return;
|
|
1494
|
+
this.targets.forEach((target) => target.style.setProperty(this.styleProperty, this.styleValue || ""));
|
|
1495
|
+
}
|
|
1496
|
+
remove() {
|
|
1497
|
+
if (!this.styleProperty) return;
|
|
1498
|
+
this.targets.forEach((target) => target.style.removeProperty(this.styleProperty));
|
|
1499
|
+
}
|
|
1500
|
+
cycle() {
|
|
1501
|
+
if (!this.styleValue) return;
|
|
1502
|
+
this.targets.forEach((target) => {
|
|
1503
|
+
const current = target.style.getPropertyValue(this.styleProperty);
|
|
1504
|
+
const next = this.cycledValue(current, this.styleValue);
|
|
1505
|
+
target.style.setProperty(this.styleProperty, next);
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
toggle() {
|
|
1509
|
+
if (!this.styleProperty) return;
|
|
1510
|
+
this.targets.forEach((target) => {
|
|
1511
|
+
if (target.style.getPropertyValue(this.styleProperty) !== "") target.style.removeProperty(this.styleProperty);
|
|
1512
|
+
else target.style.setProperty(this.styleProperty, this.styleValue || "");
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
};
|
|
1516
|
+
//#endregion
|
|
1517
|
+
//#region src/actions/index.js
|
|
1518
|
+
const actions = {
|
|
1519
|
+
attribute: attributeActions,
|
|
1520
|
+
class: actions$9,
|
|
1521
|
+
clipboard: actions$8,
|
|
1522
|
+
confirm: actions$7,
|
|
1523
|
+
dataAttribute: dataAttributeActions,
|
|
1524
|
+
dialog: actions$6,
|
|
1525
|
+
focus: actions$5,
|
|
1526
|
+
form: actions$4,
|
|
1527
|
+
reload: actions$3,
|
|
1528
|
+
scrollTo: actions$2,
|
|
1529
|
+
style: {
|
|
1530
|
+
setStyle: Style.actionFor("set"),
|
|
1531
|
+
removeStyle: Style.actionFor("remove"),
|
|
1532
|
+
cycleStyle: Style.actionFor("cycle"),
|
|
1533
|
+
toggleStyle: Style.actionFor("toggle")
|
|
1534
|
+
}
|
|
1535
|
+
};
|
|
1536
|
+
const availableActions = (groups = []) => {
|
|
1537
|
+
if (groups.length === 0) return Object.values(actions).reduce((all, group) => ({
|
|
1538
|
+
...all,
|
|
1539
|
+
...group
|
|
1540
|
+
}), {});
|
|
1541
|
+
return groups.reduce((selectedActions, group) => {
|
|
1542
|
+
const groupActions = actions[group];
|
|
1543
|
+
if (!groupActions) {
|
|
1544
|
+
console.warn(`Action "${group}" not found`);
|
|
1545
|
+
return selectedActions;
|
|
1546
|
+
}
|
|
1547
|
+
return {
|
|
1548
|
+
...selectedActions,
|
|
1549
|
+
...groupActions
|
|
1550
|
+
};
|
|
1551
|
+
}, {});
|
|
1552
|
+
};
|
|
1553
|
+
var actions_default = availableActions([]);
|
|
1554
|
+
//#endregion
|
|
1555
|
+
//#region src/core/builtin_directives.js
|
|
1556
|
+
const onceTracker = /* @__PURE__ */ new WeakSet();
|
|
1557
|
+
const intersectionObservers = /* @__PURE__ */ new WeakMap();
|
|
1558
|
+
const whenIntersecting = (check) => (element, trigger) => {
|
|
1559
|
+
let observers = intersectionObservers.get(element);
|
|
1560
|
+
if (!observers) {
|
|
1561
|
+
observers = /* @__PURE__ */ new Map();
|
|
1562
|
+
intersectionObservers.set(element, observers);
|
|
1563
|
+
}
|
|
1564
|
+
observers.get(check)?.disconnect();
|
|
1565
|
+
const observer = new IntersectionObserver((entries) => {
|
|
1566
|
+
if (check(entries)) trigger();
|
|
1567
|
+
});
|
|
1568
|
+
observers.set(check, observer);
|
|
1569
|
+
observer.observe(element);
|
|
1570
|
+
};
|
|
1571
|
+
const builtinTriggers = {
|
|
1572
|
+
mounted: (_, trigger) => {
|
|
1573
|
+
queueMicrotask(trigger);
|
|
1574
|
+
},
|
|
1575
|
+
now: (_, trigger) => {
|
|
1576
|
+
trigger();
|
|
1577
|
+
},
|
|
1578
|
+
whenVisible: whenIntersecting((entries) => entries.some((entry) => entry.isIntersecting)),
|
|
1579
|
+
whenInView: whenIntersecting((entries) => entries[0].isIntersecting)
|
|
1580
|
+
};
|
|
1581
|
+
const builtinGates = {
|
|
1582
|
+
whenOutside: (element, { event }) => {
|
|
1583
|
+
if (typeof element.checkVisibility === "function" && !element.checkVisibility()) return false;
|
|
1584
|
+
return !element.contains(event.target);
|
|
1585
|
+
},
|
|
1586
|
+
once: (element) => {
|
|
1587
|
+
if (onceTracker.has(element)) return false;
|
|
1588
|
+
onceTracker.add(element);
|
|
1589
|
+
return true;
|
|
1590
|
+
},
|
|
1591
|
+
preventDefault: (element, { event }) => {
|
|
1592
|
+
event.preventDefault();
|
|
1593
|
+
return true;
|
|
1594
|
+
},
|
|
1595
|
+
stopPropagation: (element, { event }) => {
|
|
1596
|
+
event.stopPropagation();
|
|
1597
|
+
return true;
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
//#endregion
|
|
1601
|
+
//#region src/helpers/from.js
|
|
1602
|
+
function from(files, { nameFor } = {}) {
|
|
1603
|
+
return Object.fromEntries(Object.entries(files).map(([path, module]) => {
|
|
1604
|
+
const action = module.default ?? module;
|
|
1605
|
+
return [nameFor ? nameFor(path, action) : toCamelCase(filename(path)), action];
|
|
1606
|
+
}));
|
|
1607
|
+
}
|
|
1608
|
+
function filename(path) {
|
|
1609
|
+
return path.split("/").pop().replace(/\.\w+$/, "");
|
|
1610
|
+
}
|
|
1611
|
+
function toCamelCase(string) {
|
|
1612
|
+
return string.replace(/_([a-z])/g, (_, cased) => cased.toUpperCase());
|
|
1613
|
+
}
|
|
1614
|
+
//#endregion
|
|
1615
|
+
//#region src/index.js
|
|
1616
|
+
const allBuiltinActions = actions_default;
|
|
1617
|
+
Attractive.activate = function(options = {}) {
|
|
1618
|
+
const attractive = new this(options);
|
|
1619
|
+
attractive.activate({
|
|
1620
|
+
...options,
|
|
1621
|
+
addActions: {
|
|
1622
|
+
...allBuiltinActions,
|
|
1623
|
+
...options.addActions
|
|
1624
|
+
},
|
|
1625
|
+
addGates: {
|
|
1626
|
+
...builtinGates,
|
|
1627
|
+
...options.addGates
|
|
1628
|
+
},
|
|
1629
|
+
addTriggers: {
|
|
1630
|
+
...builtinTriggers,
|
|
1631
|
+
...options.addTriggers
|
|
1632
|
+
}
|
|
1633
|
+
});
|
|
1634
|
+
return attractive;
|
|
1635
|
+
};
|
|
1636
|
+
if (typeof document !== "undefined" && typeof window !== "undefined" && (document.currentScript ?? document.querySelector("script[src*=\"attractive\"][activate]"))?.hasAttribute("activate")) {
|
|
1637
|
+
window.Attractive = Attractive;
|
|
1638
|
+
document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", () => Attractive.activate()) : Attractive.activate();
|
|
1639
|
+
}
|
|
1640
|
+
var src_default = Attractive;
|
|
1641
|
+
//#endregion
|
|
1642
|
+
export { Action, src_default as default, from };
|