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,319 @@
|
|
|
1
|
+
//#region src/debug.js
|
|
2
|
+
var Debug = class {
|
|
3
|
+
static enabled = false;
|
|
4
|
+
static prefix = "🧲 ";
|
|
5
|
+
static log(...args) {
|
|
6
|
+
if (this.enabled) console.log(this.prefix, ...args);
|
|
7
|
+
}
|
|
8
|
+
static warn(...args) {
|
|
9
|
+
if (this.enabled) console.warn(this.prefix, ...args);
|
|
10
|
+
}
|
|
11
|
+
static error(...args) {
|
|
12
|
+
if (this.enabled) console.error(this.prefix, ...args);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/addons/template.js
|
|
17
|
+
var Template = class {
|
|
18
|
+
constructor(templateId) {
|
|
19
|
+
this.source = templateId ? document.getElementById(templateId) : null;
|
|
20
|
+
if (templateId && !this.source) Debug.warn(`Attract: template "#${templateId}" not found`);
|
|
21
|
+
}
|
|
22
|
+
render({ with: data, target, targets: selector, position }) {
|
|
23
|
+
const targetElements = this.#targets({
|
|
24
|
+
target,
|
|
25
|
+
targets: selector
|
|
26
|
+
});
|
|
27
|
+
if (!targetElements.length) return [];
|
|
28
|
+
if (position === "remove") {
|
|
29
|
+
targetElements.forEach((element) => element.remove());
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
if (!this.source) return [];
|
|
33
|
+
const items = Array.isArray(data) ? data : [data];
|
|
34
|
+
const results = [];
|
|
35
|
+
targetElements.forEach((targetElement) => {
|
|
36
|
+
items.forEach((item) => results.push(this.#renderIn({
|
|
37
|
+
target: targetElement,
|
|
38
|
+
item,
|
|
39
|
+
position
|
|
40
|
+
})));
|
|
41
|
+
});
|
|
42
|
+
return results;
|
|
43
|
+
}
|
|
44
|
+
#targets({ target, targets: selector }) {
|
|
45
|
+
if (selector) return Array.from(document.querySelectorAll(selector));
|
|
46
|
+
if (target) {
|
|
47
|
+
const element = document.getElementById(target);
|
|
48
|
+
return element ? [element] : [];
|
|
49
|
+
}
|
|
50
|
+
return [];
|
|
51
|
+
}
|
|
52
|
+
#renderIn({ target, item, position }) {
|
|
53
|
+
const template = this.source.content.cloneNode(true).firstElementChild;
|
|
54
|
+
this.#snapshot({
|
|
55
|
+
template,
|
|
56
|
+
with: item
|
|
57
|
+
});
|
|
58
|
+
this.#insert({
|
|
59
|
+
template,
|
|
60
|
+
target,
|
|
61
|
+
at: position
|
|
62
|
+
});
|
|
63
|
+
return template;
|
|
64
|
+
}
|
|
65
|
+
#snapshot({ template, with: data }) {
|
|
66
|
+
if (!data) return;
|
|
67
|
+
for (const [key, value] of Object.entries(data)) {
|
|
68
|
+
const elements = [];
|
|
69
|
+
if (template.getAttribute("attract-field") === key) elements.push(template);
|
|
70
|
+
elements.push(...template.querySelectorAll(`[attract-field="${key}"]`));
|
|
71
|
+
for (const element of elements) this.#setValue(element, value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
#setValue(element, value) {
|
|
75
|
+
const tag = element.tagName;
|
|
76
|
+
const type = (element.getAttribute("type") || "").toLowerCase();
|
|
77
|
+
if (type === "checkbox" || type === "radio" || tag === "OPTION") {
|
|
78
|
+
if (typeof value === "boolean") if (tag === "OPTION") element.selected = value;
|
|
79
|
+
else element.checked = value;
|
|
80
|
+
else element.value = value ?? "";
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") {
|
|
84
|
+
element.value = value ?? "";
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
element.textContent = value ?? "";
|
|
88
|
+
}
|
|
89
|
+
#insert({ template, target, at: position }) {
|
|
90
|
+
if (position === "append") target.append(template);
|
|
91
|
+
else if (position === "prepend") target.prepend(template);
|
|
92
|
+
else if (position === "replace") target.replaceWith(template);
|
|
93
|
+
else if (position === "before") target.before(template);
|
|
94
|
+
else if (position === "after") target.after(template);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/addons/attract/actions.js
|
|
99
|
+
function positionAction(position) {
|
|
100
|
+
return (element, { value, target, targets, dataset }) => {
|
|
101
|
+
const templateId = value || dataset?.template || dataset?.attractTemplate;
|
|
102
|
+
if (!templateId) return;
|
|
103
|
+
new Template(templateId).render({
|
|
104
|
+
target: target || dataset?.attractTarget,
|
|
105
|
+
targets,
|
|
106
|
+
position
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const append = positionAction("append");
|
|
111
|
+
const prepend = positionAction("prepend");
|
|
112
|
+
const replace = positionAction("replace");
|
|
113
|
+
const before = positionAction("before");
|
|
114
|
+
const after = positionAction("after");
|
|
115
|
+
function remove(element, { target, targets }) {
|
|
116
|
+
if (target || targets) {
|
|
117
|
+
new Template(null).render({
|
|
118
|
+
target,
|
|
119
|
+
targets,
|
|
120
|
+
position: "remove",
|
|
121
|
+
with: {}
|
|
122
|
+
});
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
element?.remove();
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/actions/request/csrf.js
|
|
129
|
+
const csrf = {
|
|
130
|
+
header: null,
|
|
131
|
+
token: null
|
|
132
|
+
};
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/addons/response.js
|
|
135
|
+
function respond(json, { registry }) {
|
|
136
|
+
const actions = Array.isArray(json) ? json : json.actions ? json.actions : [json];
|
|
137
|
+
for (const item of actions) {
|
|
138
|
+
const [name, ...rest] = item.action.split("#");
|
|
139
|
+
const value = rest.join("#");
|
|
140
|
+
if (item.template) {
|
|
141
|
+
if (!item.data) Debug.warn(`Response: "template" without "data"`);
|
|
142
|
+
else new Template(item.template).render({
|
|
143
|
+
with: item.data,
|
|
144
|
+
target: item.target,
|
|
145
|
+
targets: item.targets,
|
|
146
|
+
position: name
|
|
147
|
+
});
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const action = registry.getAction(name);
|
|
151
|
+
if (!action) {
|
|
152
|
+
Debug.warn(`Response: unknown action "${name}"`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
action(item.target ? document.getElementById(item.target) : item.targets ? document.querySelector(item.targets) : null, {
|
|
156
|
+
value: value || item.value || null,
|
|
157
|
+
target: item.target,
|
|
158
|
+
targets: item.targets,
|
|
159
|
+
dataset: {},
|
|
160
|
+
data: item.data ?? null
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/addons/attract/form.js
|
|
166
|
+
var Form = class {
|
|
167
|
+
#optimisticElements = [];
|
|
168
|
+
constructor({ form, registry }) {
|
|
169
|
+
this.form = form;
|
|
170
|
+
this.registry = registry;
|
|
171
|
+
}
|
|
172
|
+
submit() {
|
|
173
|
+
const send = (event) => this.#send(event);
|
|
174
|
+
this.form.addEventListener("submit", send);
|
|
175
|
+
return () => this.form.removeEventListener("submit", send);
|
|
176
|
+
}
|
|
177
|
+
async #send(event) {
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
const action = this.form.getAttribute("action");
|
|
180
|
+
const method = (this.form.querySelector("input[name=\"_method\"]")?.value || this.form.getAttribute("method") || "post").toUpperCase();
|
|
181
|
+
if (!action) return;
|
|
182
|
+
const body = this.#data();
|
|
183
|
+
this.#render(body);
|
|
184
|
+
this.#busy();
|
|
185
|
+
try {
|
|
186
|
+
const request = await this.#fetch({
|
|
187
|
+
action,
|
|
188
|
+
method,
|
|
189
|
+
body
|
|
190
|
+
});
|
|
191
|
+
if (!request) {
|
|
192
|
+
this.#clear({ error: true });
|
|
193
|
+
this.#removeOptimistic();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
this.#clear(request.ok ? { success: true } : { error: true });
|
|
197
|
+
if (request.json.errors) {
|
|
198
|
+
this.#validity({ errors: request.json.errors });
|
|
199
|
+
this.#removeOptimistic();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
this.#actions(request.json);
|
|
203
|
+
} catch {
|
|
204
|
+
this.#clear({ error: true });
|
|
205
|
+
this.#removeOptimistic();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
#data() {
|
|
209
|
+
const formData = new FormData(this.form);
|
|
210
|
+
const body = {};
|
|
211
|
+
for (const [key, value] of formData.entries()) {
|
|
212
|
+
if (key === "_method") continue;
|
|
213
|
+
body[key] = value;
|
|
214
|
+
}
|
|
215
|
+
return body;
|
|
216
|
+
}
|
|
217
|
+
#render(body) {
|
|
218
|
+
const templateId = this.form.dataset.template || this.form.dataset.attractTemplate || null;
|
|
219
|
+
const target = this.form.dataset.attractTarget || this.form.getAttribute("@target") || null;
|
|
220
|
+
const position = this.form.dataset.attractPosition || "append";
|
|
221
|
+
if (templateId && target && Object.keys(body).length > 0) this.#optimisticElements = new Template(templateId).render({
|
|
222
|
+
target,
|
|
223
|
+
position,
|
|
224
|
+
with: body
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
#removeOptimistic() {
|
|
228
|
+
for (const element of this.#optimisticElements) element.remove();
|
|
229
|
+
this.#optimisticElements = [];
|
|
230
|
+
}
|
|
231
|
+
#busy() {
|
|
232
|
+
this.form.removeAttribute("data-attract-busy");
|
|
233
|
+
this.form.removeAttribute("data-attract-success");
|
|
234
|
+
this.form.removeAttribute("data-attract-error");
|
|
235
|
+
this.form.setAttribute("data-attract-busy", "");
|
|
236
|
+
}
|
|
237
|
+
async #fetch({ action, method, body }) {
|
|
238
|
+
const headers = {
|
|
239
|
+
"Content-Type": "application/json",
|
|
240
|
+
Accept: "application/vnd.attract+json",
|
|
241
|
+
Attract: "true"
|
|
242
|
+
};
|
|
243
|
+
if (csrf.token) headers[csrf.header] = typeof csrf.token === "function" ? csrf.token() : csrf.token;
|
|
244
|
+
const response = await fetch(action, {
|
|
245
|
+
method,
|
|
246
|
+
headers,
|
|
247
|
+
body: method === "GET" ? void 0 : JSON.stringify(body)
|
|
248
|
+
});
|
|
249
|
+
if (!response.headers.get("content-type")?.includes("json")) return null;
|
|
250
|
+
let json;
|
|
251
|
+
try {
|
|
252
|
+
json = await response.json();
|
|
253
|
+
} catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
ok: response.ok,
|
|
258
|
+
json
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
#clear({ success, error }) {
|
|
262
|
+
this.form.removeAttribute("data-attract-busy");
|
|
263
|
+
if (success) this.form.setAttribute("data-attract-success", "true");
|
|
264
|
+
if (error) this.form.setAttribute("data-attract-error", "true");
|
|
265
|
+
}
|
|
266
|
+
#validity({ errors }) {
|
|
267
|
+
for (const [name, message] of Object.entries(errors)) {
|
|
268
|
+
const field = this.form.elements[name];
|
|
269
|
+
if (field) {
|
|
270
|
+
field.setCustomValidity(message);
|
|
271
|
+
field.addEventListener("input", () => field.setCustomValidity(""), { once: true });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this.form.reportValidity();
|
|
275
|
+
}
|
|
276
|
+
#actions(json) {
|
|
277
|
+
respond(json, { registry: this.registry });
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
//#endregion
|
|
281
|
+
//#region src/addons/attract/index.js
|
|
282
|
+
const forms = /* @__PURE__ */ new WeakMap();
|
|
283
|
+
function attract({ instance, registry }) {
|
|
284
|
+
registry.addAction("append", append);
|
|
285
|
+
registry.addAction("prepend", prepend);
|
|
286
|
+
registry.addAction("replace", replace);
|
|
287
|
+
registry.addAction("before", before);
|
|
288
|
+
registry.addAction("after", after);
|
|
289
|
+
registry.addAction("remove", remove);
|
|
290
|
+
instance.onElementAdded((element) => {
|
|
291
|
+
const attract = element.getAttribute("@attract");
|
|
292
|
+
if (element.tagName === "FORM" && attract !== null) {
|
|
293
|
+
if (forms.has(element)) return;
|
|
294
|
+
const cleanup = new Form({
|
|
295
|
+
form: element,
|
|
296
|
+
registry
|
|
297
|
+
}).submit();
|
|
298
|
+
forms.set(element, cleanup);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (attract !== null) element.querySelectorAll("form").forEach((form) => {
|
|
302
|
+
if (forms.has(form)) return;
|
|
303
|
+
const cleanup = new Form({
|
|
304
|
+
form,
|
|
305
|
+
registry
|
|
306
|
+
}).submit();
|
|
307
|
+
forms.set(form, cleanup);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
instance.onElementRemoved((element) => {
|
|
311
|
+
const cleanup = forms.get(element);
|
|
312
|
+
if (cleanup) {
|
|
313
|
+
cleanup();
|
|
314
|
+
forms.delete(element);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
//#endregion
|
|
319
|
+
export { attract };
|