clapton 0.0.19 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -34
- data/app/helpers/clapton/clapton_helper.rb +1 -0
- data/lib/clapton/engine.rb +3 -0
- data/lib/clapton/javascripts/dist/c +559 -0
- data/lib/clapton/javascripts/dist/c-base.js +589 -0
- data/lib/clapton/javascripts/dist/c-for-test.js +589 -0
- data/lib/clapton/javascripts/dist/c.js +584 -0
- data/lib/clapton/javascripts/dist/client.js +9 -0
- data/lib/clapton/javascripts/dist/components-for-test.js +21 -104
- data/lib/clapton/javascripts/dist/components.js +0 -79
- data/lib/clapton/javascripts/rollup.config.mjs +31 -0
- data/lib/clapton/javascripts/src/c-for-test.ts +160 -0
- data/lib/clapton/javascripts/src/c.ts +160 -0
- data/lib/clapton/javascripts/src/client.ts +11 -0
- data/lib/clapton/javascripts/src/components/component.ts +0 -3
- data/lib/clapton/test_helper/base.rb +1 -2
- data/lib/clapton/version.rb +1 -2
- metadata +7 -2
- data/lib/clapton/javascripts/src/components/presets.ts +0 -105
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27fa8b6f3ac907d41af32dc550fa4556dc127895ace2e296969b49b8dc8f887a
|
4
|
+
data.tar.gz: 8b5099fd20fc5f804d792c48da12b694755b18e8d51a9f65442ad61e1b2a7f6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be4dabb30c708af3c0ce9b1cb0a20eb1b58d79924e2303c880b2d4b0a563eb304d906d456b105b24999baaaebb05053d618136de336c0b5b579581f07817a9b6
|
7
|
+
data.tar.gz: ddd55a8d86a0c9f5666c7fb686ed137c442e8d377341279856eb140d39a2eb08fe5538f05aa3be29ae9448ce1aee6e64eb0d93187c6bf8a1f45b1f4bdf44f12b
|
data/README.md
CHANGED
@@ -34,12 +34,12 @@ To use a Clapton component in your view:
|
|
34
34
|
# app/components/task_list_component.rb
|
35
35
|
class TaskListComponent < Clapton::Component
|
36
36
|
def render
|
37
|
-
box = c
|
37
|
+
box = c(:box)
|
38
38
|
@state.tasks.each do |task|
|
39
39
|
box.add(TaskItemComponent.new(id: task[:id], title: task[:title], due: task[:due], done: task[:done]))
|
40
40
|
end
|
41
|
-
btn = c
|
42
|
-
btn.add(c
|
41
|
+
btn = c(:button)
|
42
|
+
btn.add(c(:text, "Add Task"))
|
43
43
|
btn.add_action(:click, :TaskListState, :add_task)
|
44
44
|
box.add(btn)
|
45
45
|
end
|
@@ -51,15 +51,15 @@ end
|
|
51
51
|
# app/components/task_item_component.rb
|
52
52
|
class TaskItemComponent < Clapton::Component
|
53
53
|
def render
|
54
|
-
box = c
|
55
|
-
btn = c
|
56
|
-
btn.add(c
|
54
|
+
box = c(:box)
|
55
|
+
btn = c(:button)
|
56
|
+
btn.add(c(:text, @state.done ? "✅" : "🟩"))
|
57
57
|
btn.add_action(:click, :TaskListState, :toggle_done)
|
58
58
|
|
59
|
-
tf = c
|
59
|
+
tf = c(:input, @state, :title)
|
60
60
|
tf.add_action(:input, :TaskListState, :update_title)
|
61
61
|
|
62
|
-
dt = c
|
62
|
+
dt = c(:datetime, @state, :due)
|
63
63
|
dt.add_action(:input, :TaskListState, :update_due)
|
64
64
|
|
65
65
|
box.add(btn).add(tf).add(dt)
|
@@ -171,7 +171,7 @@ The `render` event is a special event that is triggered when the component is re
|
|
171
171
|
class TaskListComponent < Clapton::Component
|
172
172
|
def render
|
173
173
|
# ...
|
174
|
-
box = c
|
174
|
+
box = c(:box)
|
175
175
|
box.add_action(:render, :TaskListState, :add_empty_task, debounce: 500)
|
176
176
|
end
|
177
177
|
end
|
@@ -256,31 +256,31 @@ text = Clapton::Text.new("Hello")`
|
|
256
256
|
### Preset Component Methods
|
257
257
|
|
258
258
|
```javascript
|
259
|
-
c
|
260
|
-
c
|
261
|
-
c
|
262
|
-
c
|
263
|
-
c
|
264
|
-
c
|
265
|
-
c
|
266
|
-
c
|
267
|
-
c
|
268
|
-
c
|
269
|
-
c
|
270
|
-
c
|
271
|
-
c
|
272
|
-
c
|
273
|
-
c
|
274
|
-
c
|
275
|
-
c
|
276
|
-
c
|
277
|
-
c
|
278
|
-
c
|
279
|
-
c
|
280
|
-
c
|
281
|
-
c
|
282
|
-
c
|
283
|
-
c
|
259
|
+
c(:bq, ...props)
|
260
|
+
c(:box, ...props)
|
261
|
+
c(:b, ...props)
|
262
|
+
c(:button, ...props)
|
263
|
+
c(:check, ...props)
|
264
|
+
c(:code, ...props)
|
265
|
+
c(:datetime, ...props)
|
266
|
+
c(:el, ...props)
|
267
|
+
c(:embed, ...props)
|
268
|
+
c(:em, ...props)
|
269
|
+
c(:form, ...props)
|
270
|
+
c(:h, ...props)
|
271
|
+
c(:img, ...props)
|
272
|
+
c(:a, ...props)
|
273
|
+
c(:li, ...props)
|
274
|
+
c(:ul, ...props)
|
275
|
+
c(:ol, ...props)
|
276
|
+
c(:p, ...props)
|
277
|
+
c(:q, ...props)
|
278
|
+
c(:radio, ...props)
|
279
|
+
c(:select, ...props)
|
280
|
+
c(:span, ...props)
|
281
|
+
c(:textarea, ...props)
|
282
|
+
c(:input, ...props)
|
283
|
+
c(:text, ...props)
|
284
284
|
```
|
285
285
|
|
286
286
|
### Streaming
|
@@ -9,6 +9,7 @@ module Clapton
|
|
9
9
|
"imports": {
|
10
10
|
"client": "/clapton/client.js",
|
11
11
|
"components": "/clapton/components.js",
|
12
|
+
"c": "/clapton/c.js",
|
12
13
|
#{ all_components.map do
|
13
14
|
|component| "\"#{File.basename(component, ".rb").camelize}\": \"/clapton/#{File.basename(component, ".rb").camelize}.js\""
|
14
15
|
end.join(",\n") }
|
data/lib/clapton/engine.rb
CHANGED
@@ -24,6 +24,7 @@ module Clapton
|
|
24
24
|
FileUtils.mkdir_p(Rails.root.join("public", "clapton")) unless Rails.root.join("public", "clapton").exist?
|
25
25
|
File.write(Rails.root.join("public", "clapton", "components.js"), File.read(File.join(__dir__, "javascripts", "dist", "components.js")))
|
26
26
|
File.write(Rails.root.join("public", "clapton", "client.js"), File.read(File.join(__dir__, "javascripts", "dist", "client.js")))
|
27
|
+
File.write(Rails.root.join("public", "clapton", "c.js"), File.read(File.join(__dir__, "javascripts", "dist", "c.js")))
|
27
28
|
|
28
29
|
compile_components
|
29
30
|
|
@@ -44,6 +45,8 @@ module Clapton
|
|
44
45
|
js = ""
|
45
46
|
js += "import { Clapton } from 'components';"
|
46
47
|
js += "\n"
|
48
|
+
js += "import { c } from 'c';"
|
49
|
+
js += "\n"
|
47
50
|
code.scan(/(\w+)Component\.new/).each do |match|
|
48
51
|
js += "import { #{match[0]}Component } from '#{match[0]}Component';"
|
49
52
|
js += "\n"
|
@@ -0,0 +1,559 @@
|
|
1
|
+
var c = (function (exports) {
|
2
|
+
'use strict';
|
3
|
+
|
4
|
+
const htmlAttributes = (params) => {
|
5
|
+
const customDataAttributes = params.data || {};
|
6
|
+
const others = Object.keys(params).filter(key => key !== "data");
|
7
|
+
const flattenDataAttributes = (data, prefix = "data") => {
|
8
|
+
return Object.keys(data).reduce((acc, key) => {
|
9
|
+
const value = data[key];
|
10
|
+
if (typeof value === "object" && value !== null) {
|
11
|
+
acc.push(...flattenDataAttributes(value, `${prefix}-${key}`));
|
12
|
+
}
|
13
|
+
else {
|
14
|
+
acc.push(`${prefix}-${key}='${escapeHtml(value)}'`);
|
15
|
+
}
|
16
|
+
return acc;
|
17
|
+
}, []);
|
18
|
+
};
|
19
|
+
return [
|
20
|
+
others.map(key => {
|
21
|
+
if (key === "disabled") {
|
22
|
+
if (params[key] === false) {
|
23
|
+
return "";
|
24
|
+
}
|
25
|
+
else {
|
26
|
+
return `${key}`;
|
27
|
+
}
|
28
|
+
}
|
29
|
+
return `${key}='${escapeHtml(params[key])}'`;
|
30
|
+
}).join(" "),
|
31
|
+
flattenDataAttributes(customDataAttributes).join(" ")
|
32
|
+
].filter(Boolean).join(" ");
|
33
|
+
};
|
34
|
+
const escapeHtml = (unsafe) => {
|
35
|
+
if (typeof unsafe !== "string") {
|
36
|
+
return "";
|
37
|
+
}
|
38
|
+
return unsafe
|
39
|
+
.replace(/&/g, "&")
|
40
|
+
.replace(/</g, "<")
|
41
|
+
.replace(/>/g, ">")
|
42
|
+
.replace(/"/g, """)
|
43
|
+
.replace(/'/g, "'");
|
44
|
+
};
|
45
|
+
|
46
|
+
class BlockQuote {
|
47
|
+
constructor(attributes = {}) {
|
48
|
+
this.children = [];
|
49
|
+
this.attributes = attributes;
|
50
|
+
}
|
51
|
+
get renderWrapper() {
|
52
|
+
return `<blockquote ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</blockquote>`;
|
53
|
+
}
|
54
|
+
add(child) {
|
55
|
+
this.children.push(child);
|
56
|
+
return this;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
class Box {
|
61
|
+
constructor(attributes = {}) {
|
62
|
+
this.children = [];
|
63
|
+
this.attributes = attributes;
|
64
|
+
}
|
65
|
+
add(child) {
|
66
|
+
this.children.push(child);
|
67
|
+
return this;
|
68
|
+
}
|
69
|
+
get renderWrapper() {
|
70
|
+
return `<div ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</div>`;
|
71
|
+
}
|
72
|
+
add_action(eventType, stateName, fnName, options = {}) {
|
73
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${eventType}->${stateName}#${fnName}@${options.debounce || 0}`;
|
74
|
+
return this;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
class Button {
|
79
|
+
constructor(attributes = {}) {
|
80
|
+
this.attributes = attributes;
|
81
|
+
this.children = [];
|
82
|
+
}
|
83
|
+
add(child) {
|
84
|
+
this.children.push(child);
|
85
|
+
return this;
|
86
|
+
}
|
87
|
+
get renderWrapper() {
|
88
|
+
return `<button ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</button>`;
|
89
|
+
}
|
90
|
+
add_action(event, klass, fn, options = {}) {
|
91
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
92
|
+
return this;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
class Bold {
|
97
|
+
constructor(attributes = {}) {
|
98
|
+
this.children = [];
|
99
|
+
this.attributes = attributes;
|
100
|
+
}
|
101
|
+
get renderWrapper() {
|
102
|
+
return `<strong ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</strong>`;
|
103
|
+
}
|
104
|
+
add(child) {
|
105
|
+
this.children.push(child);
|
106
|
+
return this;
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
class Checkbox {
|
111
|
+
constructor(state, attribute, attributes = {}) {
|
112
|
+
this.state = state;
|
113
|
+
this.attributes = attributes;
|
114
|
+
this.attribute = attribute;
|
115
|
+
this.attributes["data-attribute"] = attribute;
|
116
|
+
}
|
117
|
+
get renderWrapper() {
|
118
|
+
return `<input type='checkbox' ${htmlAttributes(this.attributes)} value='${this.state[this.attribute] || ""}'/>`;
|
119
|
+
}
|
120
|
+
add_action(event, klass, fn, options = {}) {
|
121
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
122
|
+
return this;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
|
126
|
+
class Code {
|
127
|
+
constructor(attributes = {}) {
|
128
|
+
this.children = [];
|
129
|
+
this.attributes = attributes;
|
130
|
+
}
|
131
|
+
get renderWrapper() {
|
132
|
+
return `<code ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</code>`;
|
133
|
+
}
|
134
|
+
add(child) {
|
135
|
+
this.children.push(child);
|
136
|
+
return this;
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
class DateTimeField {
|
141
|
+
constructor(state, attribute, attributes = {}) {
|
142
|
+
this.attributes = {};
|
143
|
+
this.state = state;
|
144
|
+
this.attribute = attribute;
|
145
|
+
this.attributes = attributes;
|
146
|
+
this.attributes["data-attribute"] = attribute;
|
147
|
+
}
|
148
|
+
get renderWrapper() {
|
149
|
+
const value = this.state[this.attribute] ? this.datetime_local_value(this.state[this.attribute]) : "";
|
150
|
+
return `<input type='datetime-local' ${htmlAttributes(this.attributes)} value='${value}'/>`;
|
151
|
+
}
|
152
|
+
add_action(event, klass, fn, options = {}) {
|
153
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
154
|
+
return this;
|
155
|
+
}
|
156
|
+
datetime_local_value(value) {
|
157
|
+
if (!value) {
|
158
|
+
return "";
|
159
|
+
}
|
160
|
+
const date = new Date(value);
|
161
|
+
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
|
162
|
+
let month = date.getMonth() + 1;
|
163
|
+
let day = date.getDate();
|
164
|
+
let hours = date.getHours();
|
165
|
+
let minutes = date.getMinutes();
|
166
|
+
if (month < 10) {
|
167
|
+
month = `0${month}`;
|
168
|
+
}
|
169
|
+
if (day < 10) {
|
170
|
+
day = `0${day}`;
|
171
|
+
}
|
172
|
+
if (hours < 10) {
|
173
|
+
hours = `0${hours}`;
|
174
|
+
}
|
175
|
+
if (minutes < 10) {
|
176
|
+
minutes = `0${minutes}`;
|
177
|
+
}
|
178
|
+
return `${date.getFullYear()}-${month}-${day}T${hours}:${minutes}`;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
class Element {
|
183
|
+
constructor(type, attributes = {}) {
|
184
|
+
this.children = [];
|
185
|
+
this.type = type;
|
186
|
+
this.attributes = attributes;
|
187
|
+
}
|
188
|
+
get renderWrapper() {
|
189
|
+
return `<${this.type} ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</${this.type}>`;
|
190
|
+
}
|
191
|
+
add(child) {
|
192
|
+
this.children.push(child);
|
193
|
+
return this;
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
class Embed {
|
198
|
+
constructor(html) {
|
199
|
+
this.html = html;
|
200
|
+
}
|
201
|
+
get renderWrapper() {
|
202
|
+
return this.html;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
class Emphasis {
|
207
|
+
constructor(attributes = {}) {
|
208
|
+
this.children = [];
|
209
|
+
this.attributes = attributes;
|
210
|
+
}
|
211
|
+
get renderWrapper() {
|
212
|
+
return `<em ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</em>`;
|
213
|
+
}
|
214
|
+
add(child) {
|
215
|
+
this.children.push(child);
|
216
|
+
return this;
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
class Form {
|
221
|
+
constructor(attributes = {}) {
|
222
|
+
this.children = [];
|
223
|
+
this.attributes = attributes;
|
224
|
+
}
|
225
|
+
get renderWrapper() {
|
226
|
+
return `<form ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</form>`;
|
227
|
+
}
|
228
|
+
add(child) {
|
229
|
+
this.children.push(child);
|
230
|
+
return this;
|
231
|
+
}
|
232
|
+
}
|
233
|
+
|
234
|
+
class Heading {
|
235
|
+
constructor(level, attributes = {}) {
|
236
|
+
this.children = [];
|
237
|
+
this.level = level;
|
238
|
+
this.attributes = attributes;
|
239
|
+
}
|
240
|
+
get renderWrapper() {
|
241
|
+
return `<h${this.level} ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</h${this.level}>`;
|
242
|
+
}
|
243
|
+
add(child) {
|
244
|
+
this.children.push(child);
|
245
|
+
return this;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
class Image {
|
250
|
+
constructor(src, alt, attributes = {}) {
|
251
|
+
this.children = [];
|
252
|
+
this.attributes = attributes;
|
253
|
+
this.src = src;
|
254
|
+
this.alt = alt;
|
255
|
+
}
|
256
|
+
get renderWrapper() {
|
257
|
+
return `<img src='${this.src}' alt='${this.alt}' ${htmlAttributes(this.attributes)}/>`;
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
class Link {
|
262
|
+
constructor(href, attributes = {}) {
|
263
|
+
this.children = [];
|
264
|
+
this.attributes = attributes;
|
265
|
+
this.href = href;
|
266
|
+
}
|
267
|
+
get renderWrapper() {
|
268
|
+
return `<a href='${this.href}' ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</a>`;
|
269
|
+
}
|
270
|
+
add(child) {
|
271
|
+
this.children.push(child);
|
272
|
+
return this;
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
class ListItem {
|
277
|
+
constructor(attributes = {}) {
|
278
|
+
this.children = [];
|
279
|
+
this.attributes = attributes;
|
280
|
+
}
|
281
|
+
add(child) {
|
282
|
+
this.children.push(child);
|
283
|
+
return this;
|
284
|
+
}
|
285
|
+
get renderWrapper() {
|
286
|
+
return `<li ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</li>`;
|
287
|
+
}
|
288
|
+
}
|
289
|
+
|
290
|
+
class List {
|
291
|
+
constructor(attributes = {}) {
|
292
|
+
this.children = [];
|
293
|
+
this.attributes = attributes;
|
294
|
+
}
|
295
|
+
add(child) {
|
296
|
+
this.children.push(child);
|
297
|
+
return this;
|
298
|
+
}
|
299
|
+
get renderWrapper() {
|
300
|
+
return `<ul ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</ul>`;
|
301
|
+
}
|
302
|
+
}
|
303
|
+
|
304
|
+
class OrderedList {
|
305
|
+
constructor(attributes = {}) {
|
306
|
+
this.children = [];
|
307
|
+
this.attributes = attributes;
|
308
|
+
}
|
309
|
+
add(child) {
|
310
|
+
this.children.push(child);
|
311
|
+
return this;
|
312
|
+
}
|
313
|
+
get renderWrapper() {
|
314
|
+
return `<ol ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</ol>`;
|
315
|
+
}
|
316
|
+
}
|
317
|
+
|
318
|
+
class Paragraph {
|
319
|
+
constructor(attributes = {}) {
|
320
|
+
this.children = [];
|
321
|
+
this.attributes = attributes;
|
322
|
+
}
|
323
|
+
get renderWrapper() {
|
324
|
+
return `<p ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</p>`;
|
325
|
+
}
|
326
|
+
add(child) {
|
327
|
+
this.children.push(child);
|
328
|
+
return this;
|
329
|
+
}
|
330
|
+
}
|
331
|
+
|
332
|
+
class Quote {
|
333
|
+
constructor(attributes = {}) {
|
334
|
+
this.children = [];
|
335
|
+
this.attributes = attributes;
|
336
|
+
}
|
337
|
+
get renderWrapper() {
|
338
|
+
return `<q ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</q>`;
|
339
|
+
}
|
340
|
+
add(child) {
|
341
|
+
this.children.push(child);
|
342
|
+
return this;
|
343
|
+
}
|
344
|
+
}
|
345
|
+
|
346
|
+
class RadioButton {
|
347
|
+
constructor(state, attribute, attributes = {}) {
|
348
|
+
this.state = state;
|
349
|
+
this.attributes = attributes;
|
350
|
+
this.attribute = attribute;
|
351
|
+
this.attributes["data-attribute"] = attribute;
|
352
|
+
}
|
353
|
+
get renderWrapper() {
|
354
|
+
return `<input type='radio' ${htmlAttributes(this.attributes)} value='${this.state[this.attribute] || ""}'/>`;
|
355
|
+
}
|
356
|
+
add_action(event, klass, fn, options = {}) {
|
357
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
358
|
+
return this;
|
359
|
+
}
|
360
|
+
}
|
361
|
+
|
362
|
+
class Select {
|
363
|
+
constructor(options = [], state, attribute, attributes = {}) {
|
364
|
+
this.children = [];
|
365
|
+
this.options = options;
|
366
|
+
this.state = state;
|
367
|
+
this.attribute = attribute;
|
368
|
+
this.attributes = attributes;
|
369
|
+
}
|
370
|
+
get renderWrapper() {
|
371
|
+
return `<select ${htmlAttributes(this.attributes)}>${this.options.map(option => `<option value='${option.value}'${option.value === this.state[this.attribute] ? " selected" : ""}>${option.text}</option>`).join("")}${this.children.map(child => child.renderWrapper).join("")}</select>`;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
class Span {
|
376
|
+
constructor(attributes = {}) {
|
377
|
+
this.children = [];
|
378
|
+
this.attributes = attributes;
|
379
|
+
}
|
380
|
+
get renderWrapper() {
|
381
|
+
return `<span ${htmlAttributes(this.attributes)}>${this.children.map(child => child.renderWrapper).join("")}</span>`;
|
382
|
+
}
|
383
|
+
add(child) {
|
384
|
+
this.children.push(child);
|
385
|
+
return this;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
|
389
|
+
class Component {
|
390
|
+
constructor(state = {}, id = Math.random().toString(36).substring(2, 10), errors = []) {
|
391
|
+
this._state = state;
|
392
|
+
this.id = id;
|
393
|
+
this._errors = errors;
|
394
|
+
}
|
395
|
+
get render() {
|
396
|
+
return new Box({});
|
397
|
+
}
|
398
|
+
get renderWrapper() {
|
399
|
+
const root = this.render;
|
400
|
+
if (root.attributes) {
|
401
|
+
root.attributes = { ...root.attributes, data: { ...root.attributes.data, component: this.constructor.name, state: JSON.stringify(this._state), id: this.id, errors: this._errors } };
|
402
|
+
}
|
403
|
+
else {
|
404
|
+
root.attributes = { data: { component: this.constructor.name, state: JSON.stringify(this._state), id: this.id, errors: this._errors } };
|
405
|
+
}
|
406
|
+
return root.renderWrapper;
|
407
|
+
}
|
408
|
+
}
|
409
|
+
|
410
|
+
class TextField {
|
411
|
+
constructor(state, attribute, attributes = {}) {
|
412
|
+
this.state = state;
|
413
|
+
this.attributes = attributes;
|
414
|
+
this.attribute = attribute;
|
415
|
+
this.attributes["data-attribute"] = attribute;
|
416
|
+
}
|
417
|
+
get renderWrapper() {
|
418
|
+
return `<input type='text' ${htmlAttributes(this.attributes)} value='${this.state[this.attribute] || ""}'/>`;
|
419
|
+
}
|
420
|
+
add_action(event, klass, fn, options = {}) {
|
421
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
422
|
+
return this;
|
423
|
+
}
|
424
|
+
}
|
425
|
+
|
426
|
+
class Text {
|
427
|
+
constructor(value) {
|
428
|
+
this.value = value;
|
429
|
+
}
|
430
|
+
get renderWrapper() {
|
431
|
+
return this.value;
|
432
|
+
}
|
433
|
+
}
|
434
|
+
|
435
|
+
class TextArea {
|
436
|
+
constructor(state, attribute, attributes = {}) {
|
437
|
+
this.state = state;
|
438
|
+
this.attributes = attributes;
|
439
|
+
this.attribute = attribute;
|
440
|
+
this.attributes["data-attribute"] = attribute;
|
441
|
+
}
|
442
|
+
get renderWrapper() {
|
443
|
+
return `<textarea ${htmlAttributes(this.attributes)}>${this.state[this.attribute] || ""}</textarea>`;
|
444
|
+
}
|
445
|
+
add_action(event, klass, fn, options = {}) {
|
446
|
+
this.attributes["data-action"] = `${this.attributes["data-action"] || ""} ${event}->${klass}#${fn}@${options.debounce || 0}`;
|
447
|
+
return this;
|
448
|
+
}
|
449
|
+
}
|
450
|
+
|
451
|
+
const Clapton = {
|
452
|
+
Box, Component, Text, TextField, Button, DateTimeField, BlockQuote, Checkbox, Code, Element, Emphasis, Form, Heading, Image, Link, List, ListItem, OrderedList, Paragraph, Quote, RadioButton, Select, Span, Embed, Bold, TextArea
|
453
|
+
};
|
454
|
+
|
455
|
+
const bq = (...props) => {
|
456
|
+
return new Clapton.BlockQuote(...props);
|
457
|
+
};
|
458
|
+
const box = (...props) => {
|
459
|
+
return new Clapton.Box(...props);
|
460
|
+
};
|
461
|
+
const b = (...props) => {
|
462
|
+
return new Clapton.Bold(...props);
|
463
|
+
};
|
464
|
+
const button = (...props) => {
|
465
|
+
return new Clapton.Button(...props);
|
466
|
+
};
|
467
|
+
const check = (...props) => {
|
468
|
+
return new Clapton.Checkbox(props[0], props[1], props[2]);
|
469
|
+
};
|
470
|
+
const code = (...props) => {
|
471
|
+
return new Clapton.Code(...props);
|
472
|
+
};
|
473
|
+
const datetime = (...props) => {
|
474
|
+
return new Clapton.DateTimeField(props[0], props[1], props[2]);
|
475
|
+
};
|
476
|
+
const el = (...props) => {
|
477
|
+
return new Clapton.Element(props[0], props[1]);
|
478
|
+
};
|
479
|
+
const embed = (...props) => {
|
480
|
+
return new Clapton.Embed(props[0]);
|
481
|
+
};
|
482
|
+
const em = (...props) => {
|
483
|
+
return new Clapton.Emphasis(...props);
|
484
|
+
};
|
485
|
+
const form = (...props) => {
|
486
|
+
return new Clapton.Form(...props);
|
487
|
+
};
|
488
|
+
const h = (...props) => {
|
489
|
+
return new Clapton.Heading(props[0], props[1]);
|
490
|
+
};
|
491
|
+
const img = (...props) => {
|
492
|
+
return new Clapton.Image(props[0], props[1], props[2]);
|
493
|
+
};
|
494
|
+
const a = (...props) => {
|
495
|
+
return new Clapton.Link(props[0], props[1]);
|
496
|
+
};
|
497
|
+
const li = (...props) => {
|
498
|
+
return new Clapton.ListItem(...props);
|
499
|
+
};
|
500
|
+
const ul = (...props) => {
|
501
|
+
return new Clapton.List(...props);
|
502
|
+
};
|
503
|
+
const ol = (...props) => {
|
504
|
+
return new Clapton.OrderedList(...props);
|
505
|
+
};
|
506
|
+
const p = (...props) => {
|
507
|
+
return new Clapton.Paragraph(...props);
|
508
|
+
};
|
509
|
+
const q = (...props) => {
|
510
|
+
return new Clapton.Quote(...props);
|
511
|
+
};
|
512
|
+
const radio = (...props) => {
|
513
|
+
return new Clapton.RadioButton(props[0], props[1], props[2]);
|
514
|
+
};
|
515
|
+
const select = (...props) => {
|
516
|
+
return new Clapton.Select(props[0], props[1], props[2]);
|
517
|
+
};
|
518
|
+
const span = (...props) => {
|
519
|
+
return new Clapton.Span(...props);
|
520
|
+
};
|
521
|
+
const textarea = (...props) => {
|
522
|
+
return new Clapton.TextArea(props[0], props[1], props[2]);
|
523
|
+
};
|
524
|
+
const input = (...props) => {
|
525
|
+
return new Clapton.TextField(props[0], props[1], props[2]);
|
526
|
+
};
|
527
|
+
const text = (...props) => {
|
528
|
+
return new Clapton.Text(props[0]);
|
529
|
+
};
|
530
|
+
|
531
|
+
exports.a = a;
|
532
|
+
exports.b = b;
|
533
|
+
exports.box = box;
|
534
|
+
exports.bq = bq;
|
535
|
+
exports.button = button;
|
536
|
+
exports.check = check;
|
537
|
+
exports.code = code;
|
538
|
+
exports.datetime = datetime;
|
539
|
+
exports.el = el;
|
540
|
+
exports.em = em;
|
541
|
+
exports.embed = embed;
|
542
|
+
exports.form = form;
|
543
|
+
exports.h = h;
|
544
|
+
exports.img = img;
|
545
|
+
exports.input = input;
|
546
|
+
exports.li = li;
|
547
|
+
exports.ol = ol;
|
548
|
+
exports.p = p;
|
549
|
+
exports.q = q;
|
550
|
+
exports.radio = radio;
|
551
|
+
exports.select = select;
|
552
|
+
exports.span = span;
|
553
|
+
exports.text = text;
|
554
|
+
exports.textarea = textarea;
|
555
|
+
exports.ul = ul;
|
556
|
+
|
557
|
+
return exports;
|
558
|
+
|
559
|
+
})({});
|