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.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +193 -0
  4. data/Rakefile +9 -0
  5. data/app/assets/javascripts/bord/application.js +2361 -0
  6. data/app/assets/javascripts/bord/attract.js +319 -0
  7. data/app/assets/javascripts/bord/attractive.js +1642 -0
  8. data/app/assets/javascripts/bord/entry.js +67 -0
  9. data/app/assets/javascripts/bord/request.js +314 -0
  10. data/app/assets/javascripts/bord/runtime/addons/response.js +51 -0
  11. data/app/assets/javascripts/bord/runtime/addons/template.js +120 -0
  12. data/app/assets/javascripts/bord/runtime/debug.js +18 -0
  13. data/app/assets/stylesheets/bord/application.css +736 -0
  14. data/app/controllers/bord/actions_controller.rb +29 -0
  15. data/app/controllers/bord/application_controller.rb +23 -0
  16. data/app/controllers/bord/pages_controller.rb +13 -0
  17. data/app/controllers/bord/records_controller.rb +14 -0
  18. data/app/controllers/bord/widgets_controller.rb +18 -0
  19. data/app/helpers/bord/application_helper.rb +4 -0
  20. data/app/jobs/bord/application_job.rb +4 -0
  21. data/app/models/bord/action.rb +8 -0
  22. data/app/models/bord/action_parameters.rb +19 -0
  23. data/app/models/bord/allowed_actions.rb +65 -0
  24. data/app/models/bord/application_record.rb +5 -0
  25. data/app/models/bord/attract_actions.rb +101 -0
  26. data/app/models/bord/category.rb +18 -0
  27. data/app/models/bord/dashboard.rb +25 -0
  28. data/app/models/bord/entry.rb +98 -0
  29. data/app/models/bord/page/decorator.rb +33 -0
  30. data/app/models/bord/page/record_decorator.rb +44 -0
  31. data/app/models/bord/page.rb +82 -0
  32. data/app/models/bord/widget/decorator.rb +69 -0
  33. data/app/models/bord/widget.rb +40 -0
  34. data/app/models/concerns/bord/conditionable.rb +8 -0
  35. data/app/views/bord/pages/show.html.erb +9 -0
  36. data/app/views/bord/records/show.html.erb +13 -0
  37. data/app/views/bord/shared/_templates.html.erb +75 -0
  38. data/app/views/bord/widgets/_action.html.erb +7 -0
  39. data/app/views/bord/widgets/_widget.html.erb +25 -0
  40. data/app/views/bord/widgets/index.html.erb +17 -0
  41. data/app/views/layouts/bord/application.html.erb +38 -0
  42. data/config/routes.rb +11 -0
  43. data/lib/bord/configuration.rb +60 -0
  44. data/lib/bord/engine.rb +11 -0
  45. data/lib/bord/loader.rb +8 -0
  46. data/lib/bord/options.rb +17 -0
  47. data/lib/bord/presets/action_mailbox.json +20 -0
  48. data/lib/bord/presets/active_storage.json +20 -0
  49. data/lib/bord/presets/devise.json +1 -0
  50. data/lib/bord/presets/good_job.json +25 -0
  51. data/lib/bord/presets/sidekiq.json +28 -0
  52. data/lib/bord/presets/solid_queue.json +12 -0
  53. data/lib/bord/presets/stripe.json +30 -0
  54. data/lib/bord/presets/user.json +22 -0
  55. data/lib/bord/version.rb +3 -0
  56. data/lib/bord.rb +71 -0
  57. data/lib/generators/bord/install_generator.rb +16 -0
  58. data/lib/generators/bord/presets_generator.rb +62 -0
  59. metadata +116 -0
@@ -0,0 +1,67 @@
1
+ import { Action } from "./attractive.js";
2
+
3
+ export default class SetAttributes extends Action {
4
+ run() {
5
+ const { data, target, targets } = this.options;
6
+
7
+ if (!data) return;
8
+
9
+ for (const root of this.#roots({ target, targets })) {
10
+ this.#setLink({ on: root, with: data });
11
+ this.#setIcon({ on: root, with: data });
12
+ this.#setVariant({ on: root, with: data });
13
+ this.#setConfirm({ on: root, with: data });
14
+ }
15
+ }
16
+
17
+ // private
18
+
19
+ #roots({ target, targets }) {
20
+ if (targets) return Array.from(document.querySelectorAll(targets));
21
+
22
+ if (target) {
23
+ const element = document.getElementById(target);
24
+ return element ? [element] : [];
25
+ }
26
+
27
+ return [];
28
+ }
29
+
30
+ #setLink({ on, with: data }) {
31
+ if (!data.href) return;
32
+
33
+ const link = on.matches("[attract-href]") ? on : on.querySelector("[attract-href]");
34
+ if (link) link.setAttribute("href", data.href);
35
+ }
36
+
37
+ #setIcon({ on, with: data }) {
38
+ if (!data.icon) return;
39
+
40
+ const icon = on.querySelector("[attract-icon]");
41
+ if (icon) icon.innerHTML = data.icon;
42
+ }
43
+
44
+ #setVariant({ on, with: data }) {
45
+ if (!data.variant) return;
46
+
47
+ const action = on.matches("[attract-variant]") ? on : on.querySelector("[attract-variant]");
48
+ if (action) action.classList.add(`action--${data.variant}`);
49
+ }
50
+
51
+ #setConfirm({ on, with: data }) {
52
+ if (!data.confirm) return;
53
+
54
+ const form = on.matches("form[attract-confirm]") ? on : on.querySelector("form[attract-confirm]");
55
+ if (!form || form.dataset.confirmBound) return;
56
+
57
+ form.dataset.confirmBound = "true";
58
+ form.dataset.confirm = data.confirm;
59
+
60
+ form.addEventListener("submit", (event) => {
61
+ if (window.confirm(data.confirm)) return;
62
+
63
+ event.preventDefault();
64
+ event.stopImmediatePropagation();
65
+ }, true);
66
+ }
67
+ }
@@ -0,0 +1,314 @@
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/core/scopes.js
17
+ const SCOPE_PROPERTY = "__attractiveScope";
18
+ function scopeOf(element) {
19
+ let node = element;
20
+ while (node && node.nodeType === 1) {
21
+ if (node[SCOPE_PROPERTY]) return node;
22
+ node = node.parentElement;
23
+ }
24
+ return document;
25
+ }
26
+ //#endregion
27
+ //#region src/actions/base.js
28
+ var ActionBase = class {
29
+ static actionFor(method) {
30
+ return (element, options = {}) => {
31
+ return new this(element, options)[method]();
32
+ };
33
+ }
34
+ constructor(element, options = {}) {
35
+ if (!element) throw new Error("Current element is required");
36
+ this.element = element;
37
+ this.target = options.target;
38
+ this.targetsSelector = options.targets;
39
+ this.options = options;
40
+ }
41
+ get targets() {
42
+ const scope = scopeOf(this.element);
43
+ if (this.targetsSelector) return Array.from(scope.querySelectorAll(this.targetsSelector));
44
+ if (this.target) {
45
+ const target = scope.querySelector(`[id="${this.target}"]`);
46
+ if (!target) Debug.warn(`Target "#${this.target}" not found`);
47
+ return target ? [target] : [];
48
+ }
49
+ return [this.element];
50
+ }
51
+ cycledValue(currentValue, nextValues) {
52
+ const values = Array.isArray(nextValues) ? nextValues : nextValues.split(",").map((value) => value.trim());
53
+ return values[(values.indexOf(currentValue) + 1) % values.length];
54
+ }
55
+ };
56
+ //#endregion
57
+ //#region src/actions/request/csrf.js
58
+ const csrf = {
59
+ header: null,
60
+ token: null
61
+ };
62
+ //#endregion
63
+ //#region src/actions/helpers/delay.js
64
+ const delay = () => {
65
+ let timeoutId;
66
+ return (callback, delay) => {
67
+ clearTimeout(timeoutId);
68
+ timeoutId = setTimeout(callback, delay);
69
+ };
70
+ };
71
+ //#endregion
72
+ //#region src/actions/request/helpers.js
73
+ const clearFeedback = delay();
74
+ const requests = /* @__PURE__ */ new WeakMap();
75
+ const activeRequests = {
76
+ get({ on: element }) {
77
+ return requests.get(element);
78
+ },
79
+ set({ on: element, with: controller }) {
80
+ requests.set(element, controller);
81
+ },
82
+ delete({ on: element }) {
83
+ requests.delete(element);
84
+ }
85
+ };
86
+ function setFeedback(state, { on: element, for: targets }) {
87
+ const duration = element.dataset.requestFeedback;
88
+ targets.forEach((target) => {
89
+ if (state === "busy") {
90
+ target.setAttribute("data-request-busy", "true");
91
+ target.removeAttribute("data-request-success");
92
+ } else {
93
+ target.removeAttribute("data-request-busy");
94
+ target.setAttribute("data-request-success", state === "success");
95
+ }
96
+ });
97
+ if (!duration || state === "busy") return;
98
+ clearFeedback(() => {
99
+ targets.forEach((target) => target.removeAttribute("data-request-success"));
100
+ }, parseInt(duration));
101
+ }
102
+ function crossingOrigin({ for: url }) {
103
+ try {
104
+ return new URL(url, window.location.href).origin !== window.location.origin;
105
+ } catch {
106
+ Debug.error("Invalid URL:", url);
107
+ return false;
108
+ }
109
+ }
110
+ //#endregion
111
+ //#region src/actions/request/get.js
112
+ var Get = class {
113
+ #element;
114
+ #url;
115
+ #targets;
116
+ #onJSON;
117
+ constructor(element, url, targets, onJSON) {
118
+ this.#element = element;
119
+ this.#url = url;
120
+ this.#targets = targets;
121
+ this.#onJSON = onJSON;
122
+ }
123
+ async execute() {
124
+ if (!this.#url) {
125
+ Debug.warn("No URL provided in the action value");
126
+ return;
127
+ }
128
+ this.#warnCrossOrigin();
129
+ const controller = this.#start();
130
+ try {
131
+ const response = await this.#fetch(controller);
132
+ await this.#process(response);
133
+ this.#succeed();
134
+ return response;
135
+ } catch (error) {
136
+ this.#fail(error);
137
+ }
138
+ }
139
+ #warnCrossOrigin() {
140
+ if (crossingOrigin({ for: this.#url })) Debug.warn(`Cross-origin request to: ${this.#url}. Missing the correct CORS headers.`);
141
+ }
142
+ #start() {
143
+ const previous = activeRequests.get({ on: this.#element });
144
+ if (previous) previous.abort();
145
+ const controller = new AbortController();
146
+ activeRequests.set({
147
+ on: this.#element,
148
+ with: controller
149
+ });
150
+ setFeedback("busy", {
151
+ on: this.#element,
152
+ for: this.#targets
153
+ });
154
+ return controller;
155
+ }
156
+ #fetch(controller) {
157
+ return fetch(this.#url, {
158
+ method: "GET",
159
+ signal: controller.signal,
160
+ headers: {
161
+ Accept: "application/vnd.attract+json",
162
+ Attract: "true"
163
+ }
164
+ });
165
+ }
166
+ async #process(response) {
167
+ if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
168
+ if (response.headers.get("content-type")?.includes("json")) {
169
+ const json = await response.json();
170
+ if (this.#onJSON) this.#onJSON(json);
171
+ } else {
172
+ const html = await response.text();
173
+ this.#targets.forEach((target) => {
174
+ target.innerHTML = html;
175
+ });
176
+ }
177
+ }
178
+ #succeed() {
179
+ activeRequests.delete({ on: this.#element });
180
+ setFeedback("success", {
181
+ on: this.#element,
182
+ for: this.#targets
183
+ });
184
+ }
185
+ #fail(error) {
186
+ if (error.name === "AbortError") return;
187
+ activeRequests.delete({ on: this.#element });
188
+ setFeedback("error", {
189
+ on: this.#element,
190
+ for: this.#targets
191
+ });
192
+ throw error;
193
+ }
194
+ };
195
+ //#endregion
196
+ //#region src/actions/request.js
197
+ var Request = class extends ActionBase {
198
+ constructor(element, options = {}) {
199
+ super(element, options);
200
+ this.value = options.value;
201
+ }
202
+ async get() {
203
+ return new Get(this.element, this.value, this.targets, this.options.onJSON).execute();
204
+ }
205
+ post() {
206
+ return this.#fetch("POST");
207
+ }
208
+ patch() {
209
+ return this.#fetch("PATCH");
210
+ }
211
+ put() {
212
+ return this.#fetch("PUT");
213
+ }
214
+ #fetch(method) {
215
+ return this.#executeFetch(method);
216
+ }
217
+ #executeFetch(method) {
218
+ if (!this.value) {
219
+ Debug.warn("No URL provided in the action value");
220
+ return;
221
+ }
222
+ if (crossingOrigin({ for: this.value })) Debug.warn(`Cross-origin request to: ${this.value}. Missing the correct CORS headers.`);
223
+ const previous = activeRequests.get({ on: this.element });
224
+ if (previous) previous.abort();
225
+ const controller = new AbortController();
226
+ activeRequests.set({
227
+ on: this.element,
228
+ with: controller
229
+ });
230
+ setFeedback("busy", {
231
+ on: this.element,
232
+ for: this.targets
233
+ });
234
+ const headers = {
235
+ "Content-Type": "application/json",
236
+ Accept: "application/vnd.attract+json",
237
+ Attract: "true"
238
+ };
239
+ if (csrf.token) headers[csrf.header] = typeof csrf.token === "function" ? csrf.token() : csrf.token;
240
+ return fetch(this.value, {
241
+ method,
242
+ signal: controller.signal,
243
+ headers,
244
+ body: JSON.stringify(this.#body)
245
+ }).then(async (response) => {
246
+ if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
247
+ if (response.headers.get("content-type")?.includes("json") && this.options.onJSON) {
248
+ const json = await response.json();
249
+ this.options.onJSON(json);
250
+ }
251
+ setFeedback("success", {
252
+ on: this.element,
253
+ for: this.targets
254
+ });
255
+ activeRequests.delete({ on: this.element });
256
+ return response;
257
+ }).catch((error) => {
258
+ if (error.name === "AbortError") return;
259
+ activeRequests.delete({ on: this.element });
260
+ setFeedback("error", {
261
+ on: this.element,
262
+ for: this.targets
263
+ });
264
+ throw error;
265
+ });
266
+ }
267
+ get #body() {
268
+ const body = {};
269
+ const target = this.targets[0];
270
+ if (target instanceof HTMLFormElement) {
271
+ const formData = new FormData(target);
272
+ for (const [key, value] of formData.entries()) body[key] = value;
273
+ return body;
274
+ }
275
+ if (this.#inputField) {
276
+ const key = this.element.name;
277
+ body[key] = this.element.value;
278
+ }
279
+ return body;
280
+ }
281
+ get #inputField() {
282
+ return this.element instanceof HTMLInputElement || this.element instanceof HTMLSelectElement || this.element instanceof HTMLTextAreaElement && this.element.name;
283
+ }
284
+ };
285
+ function get(element, options) {
286
+ return new Request(element, {
287
+ ...options,
288
+ onJSON: get.onJSON
289
+ }).get();
290
+ }
291
+ get.onJSON = null;
292
+ function post(element, options) {
293
+ return new Request(element, {
294
+ ...options,
295
+ onJSON: post.onJSON
296
+ }).post();
297
+ }
298
+ post.onJSON = null;
299
+ function patch(element, options) {
300
+ return new Request(element, {
301
+ ...options,
302
+ onJSON: patch.onJSON
303
+ }).patch();
304
+ }
305
+ patch.onJSON = null;
306
+ function put(element, options) {
307
+ return new Request(element, {
308
+ ...options,
309
+ onJSON: put.onJSON
310
+ }).put();
311
+ }
312
+ put.onJSON = null;
313
+ //#endregion
314
+ export { get, patch, post, put };
@@ -0,0 +1,51 @@
1
+ import Debug from "../debug.js";
2
+ import { Template } from "./template.js";
3
+
4
+ export function respond(json, { registry }) {
5
+ const actions = Array.isArray(json)
6
+ ? json
7
+ : json.actions
8
+ ? json.actions
9
+ : [json];
10
+
11
+ for (const item of actions) {
12
+ const [name, ...rest] = item.action.split("#");
13
+ const value = rest.join("#");
14
+
15
+ if (item.template) {
16
+ if (!item.data) {
17
+ Debug.warn(`Response: "template" without "data"`);
18
+ } else {
19
+ new Template(item.template).render({
20
+ with: item.data,
21
+ target: item.target,
22
+ targets: item.targets,
23
+ position: name
24
+ });
25
+ }
26
+
27
+ continue;
28
+ }
29
+
30
+ const action = registry.getAction(name);
31
+ if (!action) {
32
+ Debug.warn(`Response: unknown action "${name}"`);
33
+
34
+ continue;
35
+ }
36
+
37
+ const element = item.target
38
+ ? document.getElementById(item.target)
39
+ : item.targets
40
+ ? document.querySelector(item.targets)
41
+ : null;
42
+
43
+ action(element, {
44
+ value: value || item.value || null,
45
+ target: item.target,
46
+ targets: item.targets,
47
+ dataset: {},
48
+ data: item.data ?? null
49
+ });
50
+ }
51
+ }
@@ -0,0 +1,120 @@
1
+ import Debug from "../debug.js";
2
+
3
+ class Template {
4
+ constructor(templateId) {
5
+ this.source = templateId ? document.getElementById(templateId) : null;
6
+
7
+ if (templateId && !this.source) {
8
+ Debug.warn(`Attract: template "#${templateId}" not found`);
9
+ }
10
+ }
11
+
12
+ render({ with: data, target, targets: selector, position }) {
13
+ const targetElements = this.#targets({ target, targets: selector });
14
+ if (!targetElements.length) return [];
15
+
16
+ if (position === "remove") {
17
+ targetElements.forEach((element) => element.remove());
18
+
19
+ return [];
20
+ }
21
+
22
+ if (!this.source) return [];
23
+
24
+ const items = Array.isArray(data) ? data : [data];
25
+ const results = [];
26
+
27
+ targetElements.forEach((targetElement) => {
28
+ items.forEach((item) =>
29
+ results.push(
30
+ this.#renderIn({
31
+ target: targetElement,
32
+ item,
33
+ position
34
+ })
35
+ )
36
+ );
37
+ });
38
+
39
+ return results;
40
+ }
41
+
42
+ // private
43
+
44
+ #targets({ target, targets: selector }) {
45
+ if (selector) return Array.from(document.querySelectorAll(selector));
46
+
47
+ if (target) {
48
+ const element = document.getElementById(target);
49
+
50
+ return element ? [element] : [];
51
+ }
52
+
53
+ return [];
54
+ }
55
+
56
+ #renderIn({ target, item, position }) {
57
+ const template = this.source.content.cloneNode(true).firstElementChild;
58
+
59
+ this.#snapshot({ template, with: item });
60
+
61
+ this.#insert({ template, target, at: position });
62
+
63
+ return template;
64
+ }
65
+
66
+ #snapshot({ template, with: data }) {
67
+ if (!data) return;
68
+
69
+ for (const [key, value] of Object.entries(data)) {
70
+ const elements = [];
71
+
72
+ if (template.getAttribute("attract-field") === key) {
73
+ elements.push(template);
74
+ }
75
+
76
+ elements.push(...template.querySelectorAll(`[attract-field="${key}"]`));
77
+
78
+ for (const element of elements) {
79
+ this.#setValue(element, value);
80
+ }
81
+ }
82
+ }
83
+
84
+ #setValue(element, value) {
85
+ const tag = element.tagName;
86
+ const type = (element.getAttribute("type") || "").toLowerCase();
87
+
88
+ if (type === "checkbox" || type === "radio" || tag === "OPTION") {
89
+ if (typeof value === "boolean") {
90
+ if (tag === "OPTION") {
91
+ element.selected = value;
92
+ } else {
93
+ element.checked = value;
94
+ }
95
+ } else {
96
+ element.value = value ?? "";
97
+ }
98
+
99
+ return;
100
+ }
101
+
102
+ if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") {
103
+ element.value = value ?? "";
104
+
105
+ return;
106
+ }
107
+
108
+ element.textContent = value ?? "";
109
+ }
110
+
111
+ #insert({ template, target, at: position }) {
112
+ if (position === "append") target.append(template);
113
+ else if (position === "prepend") target.prepend(template);
114
+ else if (position === "replace") target.replaceWith(template);
115
+ else if (position === "before") target.before(template);
116
+ else if (position === "after") target.after(template);
117
+ }
118
+ }
119
+
120
+ export { Template };
@@ -0,0 +1,18 @@
1
+ class Debug {
2
+ static enabled = false;
3
+ static prefix = "🧲 ";
4
+
5
+ static log(...args) {
6
+ if (this.enabled) console.log(this.prefix, ...args);
7
+ }
8
+
9
+ static warn(...args) {
10
+ if (this.enabled) console.warn(this.prefix, ...args);
11
+ }
12
+
13
+ static error(...args) {
14
+ if (this.enabled) console.error(this.prefix, ...args);
15
+ }
16
+ }
17
+
18
+ export default Debug;