clapton 0.0.18 → 0.0.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,589 @@
1
+ var c = (function () {
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, "&lt;")
41
+ .replace(/>/g, "&gt;")
42
+ .replace(/"/g, "&quot;")
43
+ .replace(/'/g, "&#039;");
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
+ const c = (name, ...props) => {
531
+ switch (name) {
532
+ case "bq":
533
+ return bq(...props);
534
+ case "box":
535
+ return box(...props);
536
+ case "b":
537
+ return b(...props);
538
+ case "button":
539
+ return button(...props);
540
+ case "check":
541
+ return check(...props);
542
+ case "code":
543
+ return code(...props);
544
+ case "datetime":
545
+ return datetime(...props);
546
+ case "el":
547
+ return el(...props);
548
+ case "embed":
549
+ return embed(...props);
550
+ case "em":
551
+ return em(...props);
552
+ case "form":
553
+ return form(...props);
554
+ case "h":
555
+ return h(...props);
556
+ case "img":
557
+ return img(...props);
558
+ case "a":
559
+ return a(...props);
560
+ case "li":
561
+ return li(...props);
562
+ case "ul":
563
+ return ul(...props);
564
+ case "ol":
565
+ return ol(...props);
566
+ case "p":
567
+ return p(...props);
568
+ case "q":
569
+ return q(...props);
570
+ case "radio":
571
+ return radio(...props);
572
+ case "select":
573
+ return select(...props);
574
+ case "span":
575
+ return span(...props);
576
+ case "textarea":
577
+ return textarea(...props);
578
+ case "input":
579
+ return input(...props);
580
+ case "text":
581
+ return text(...props);
582
+ default:
583
+ return new Clapton.Component(...props);
584
+ }
585
+ };
586
+
587
+ return c;
588
+
589
+ })();