-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
488 lines (437 loc) · 14.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('s-js')) :
typeof define === 'function' && define.amd ? define(['exports', 's-js'], factory) :
(global = global || self, factory(global['s-jsx'] = {}, global.S));
}(this, function (exports, S) { 'use strict';
S = S && S.hasOwnProperty('default') ? S['default'] : S;
// This file is copied from https://github.com/adamhaile/surplus-mixin-data
function data(signal, arg1, arg2) {
var event = arg1 || 'input', on = arg1 === undefined ? true : arg1, off = arg2 === undefined ? (on === true ? false : null) : arg2;
return function (node) {
if (node instanceof HTMLInputElement) {
var type = node.type.toUpperCase();
if (type === 'CHECKBOX') {
checkboxData(node, signal, on, off);
}
else if (type === 'RADIO') {
radioData(node, signal, on);
}
else {
valueData(node, signal, event);
}
}
else if (node instanceof HTMLSelectElement || node instanceof HTMLTextAreaElement) {
valueData(node, signal, event);
}
else if (node.isContentEditable) {
textContentData(node, signal, event);
}
else {
throw new Error("@data can only be applied to a form control element, \n"
+ "such as <input/>, <textarea/> or <select/>, or to an element with "
+ "'contentEditable' set. Element ``" + node.nodeName + "'' is \n"
+ "not such an element. Perhaps you applied it to the wrong node?");
}
};
}
function valueData(node, signal, event) {
S(function updateValue() {
node.value = toString(signal());
});
node.addEventListener(event, valueListener, false);
S.cleanup(function () { node.removeEventListener(event, valueListener); });
function valueListener() {
var cur = toString(S.sample(signal)), update = node.value;
if (cur !== update)
signal(update);
return true;
}
}
function checkboxData(node, signal, on, off) {
S(function updateCheckbox() {
node.checked = signal() === on;
});
node.addEventListener("change", checkboxListener, false);
S.cleanup(function () { node.removeEventListener("change", checkboxListener); });
function checkboxListener() {
signal(node.checked ? on : off);
return true;
}
}
function radioData(node, signal, on) {
S(function updateRadio() {
node.checked = (signal() === on);
});
node.addEventListener("change", radioListener, false);
S.cleanup(function () { node.removeEventListener("change", radioListener); });
function radioListener() {
if (node.checked)
signal(on);
return true;
}
}
function textContentData(node, signal, event) {
S(function updateTextContent() {
node.textContent = toString(signal());
});
node.addEventListener(event, textContentListener, false);
S.cleanup(function () { node.removeEventListener(event, textContentListener); });
function textContentListener() {
var cur = toString(S.sample(signal)), update = node.textContent;
if (cur !== update)
signal(update);
return true;
}
}
function toString(v) {
return v == null ? '' : v.toString();
}
function AmbiguousElement(nodeName, props, children) {
this.nodeName = nodeName;
this.props = props;
this.children = children;
}
function isSvgNodeName(nodeName) {
return svgTags.indexOf(nodeName) >= 0
}
function isSvgAmbiguousNodeName(nodeName) {
return ambiguousSvgTags.indexOf(nodeName) >= 0
}
const svgTags = [
'circle',
'clipPath',
'defs',
'desc',
'ellipse',
'feBlend',
'feColorMatrix',
'feComponentTransfer',
'feComposite',
'feConvolveMatrix',
'feDiffuseLighting',
'feDisplacementMap',
'feDistantLight',
'feFlood',
'feFuncA',
'feFuncB',
'feFuncG',
'feFuncR',
'feGaussianBlur',
'feImage',
'feMerge',
'feMergeNode',
'feMorphology',
'feOffset',
'fePointLight',
'feSpecularLighting',
'feSpotLight',
'feTile',
'feTurbulence',
'filter',
'foreignObject',
'g',
'image',
'line',
'linearGradient',
'marker',
'mask',
'metadata',
'path',
'pattern',
'polygon',
'polyline',
'radialGradient',
'rect',
'stop',
'svg',
'switch',
'symbol',
'text',
'textPath',
'tspan',
'use',
'view'
];
const ambiguousSvgTags = ['a', 'font', 'title', 'script', 'style'];
const fnProps = ['fn', 'fn0', 'fn1','fn2', 'fn3', 'fn4', 'fn5', 'fn6', 'fn7', 'fn8', 'fn9' ];
function h(nameOrComponent, attributes) {
let children = [];
for (let i = 2; i < arguments.length; i++) {
let c = arguments[i];
if (c) {
if (Array.isArray(c)) {
c.map(v => children.push(v));
}
else {
children.push(arguments[i]);
}
}
}
if (typeof (nameOrComponent) === 'string') {
if (isSvgAmbiguousNodeName(nameOrComponent)) {
return new AmbiguousElement(nameOrComponent, attributes, children)
}
else {
return createElement(nameOrComponent, attributes, children, isSvgNodeName(nameOrComponent))
}
}
else {
let result = nameOrComponent(attributes, children);
processSpecialProps(result, attributes, nameOrComponent);
return result
}
}
function createElement(nodeName, attributes, children, isSvg) {
let element = isSvg
? document.createElementNS("http://www.w3.org/2000/svg", nodeName)
: document.createElement(nodeName);
setProps(element, attributes, isSvg);
createChildren(element, children);
processSpecialProps(element, attributes, nodeName);
return element
}
function processSpecialProps(element, props, nameOrComponent) {
if (props) {
fnProps.map( p => {
if (props[p]) {
props[p](element, nameOrComponent);
}
});
}
}
h.fragment = function fragment(_, fragment) {
let result = [];
for (let i = 0; i < fragment.length; i++) {
let c = fragment[i];
if (c) {
if (Array.isArray(c)) {
c.map(v => result.push(v));
}
else {
result.push(c);
}
}
}
return result
};
function setProps(element, props, isSvg) {
for (let a in props) {
if (fnProps.indexOf(a) >= 0) {
continue
}
let attrValue = props[a];
if (a === "class") {
a = "className";
}
if (typeof (attrValue) === "function" && a.indexOf("on") !== 0) {
S(() => setPropValue(element, a, attrValue(), isSvg));
}
else {
setPropValue(element, a, attrValue, isSvg);
}
}
}
function getStyleString(obj) {
let result = "";
for(let p in obj) {
result += `${p}:${obj[p]};`;
}
return result
}
function setPropValue(element, propertyName, value, isSvg) {
if (propertyName === "style" && typeof(value) === "object") {
value = getStyleString(value);
}
if (isSvg || propertyName.indexOf('-') >= 0) {
element.setAttribute(propertyName, value);
}
else {
element[propertyName] = value;
}
}
function runFactory(parent, factory) {
if (factory.isComputationFactory) {
let prevChild = null;
S(() => {
let newChild = factory(parent, prevChild);
prevChild = newChild;
});
}
else {
factory(parent, null);
}
}
function createChildren(parent, children) {
let factories = [];
for (let i = 0; i < children.length; i++) {
factories.push(nodeFactoryFromChild(children[i]));
}
for (let i = 0; i < factories.length; i++) {
runFactory(parent, factories[i]);
}
}
function isElement(element) {
return element instanceof Element || element instanceof HTMLDocument || element instanceof Comment
}
function factoryElement(child) {
return function (parent, prevChild) {
if (prevChild) {
parent.replaceChild(child, prevChild);
}
else {
parent.appendChild(child);
}
return child
}
}
function factoryArray(child) {
return function (parent, prevChild) {
if (prevChild) {
if (Array.isArray(prevChild)) {
return reconcileArrays(parent, prevChild, child)
}
else {
return reconcileArrays(parent, [prevChild], child)
}
}
return reconcileArrays(parent, [], child)
}
}
function factoryFunction(child) {
let result = function (parent, prevChild) {
let factory = nodeFactoryFromChild(child());
return factory(parent, prevChild, true)
};
result.isComputationFactory = true;
return result
}
function factoryDefault(child) {
return function (parent, prevChild) {
if (prevChild) {
prevChild.textContent = child;
return prevChild
}
else {
let result = document.createTextNode(child);
parent.appendChild(result);
return result
}
}
}
function factoryNull(child) {
return function (parent, prevChild, calledFromComputation) {
if (!calledFromComputation) {
return null
}
let placeholder = document.createComment("");
if (prevChild) {
if (isElement(prevChild)) {
parent.replaceChild(placeholder, prevChild);
}
else if (Array.isArray(prevChild)) {
for (let i = 1; i < prevChild.length; i++) {
parent.removeChild(prevChild[i]);
}
parent.replaceChild(placeholder, prevChild[0]);
}
else {
throw "Don't know how to remove previous child: " + prevChild
}
return placeholder
}
parent.appendChild(placeholder);
return placeholder
}
}
function factoryAmbiguous(child) {
return function (parent, prevChild, calledFromComputation) {
let isSvg = (parent instanceof SVGElement) && !(parent instanceof SVGForeignObjectElement);
let element = createElement(child.nodeName, child.props, child.children, isSvg);
return factoryElement(element)(parent, prevChild, calledFromComputation)
}
}
function nodeFactoryFromChild(child) {
if (child === null || child === undefined || child === false) {
return factoryNull(child)
}
if (isElement(child)) {
return factoryElement(child)
}
if (Array.isArray(child)) {
return factoryArray(child)
}
if (child instanceof AmbiguousElement) {
return factoryAmbiguous(child)
}
if (typeof (child) === 'function') {
return factoryFunction(child)
}
return factoryDefault(child)
}
function reconcileArrays(parent, prevItems, newItems) {
if (prevItems.length === 0 && newItems.length > 0) {
for (let i = 0; i < newItems.length; i++) {
parent.appendChild(newItems[i]);
}
return newItems
}
if (prevItems.length === 0 && newItems.length === 0) {
let placeholder = document.createComment("");
parent.appendChild(placeholder);
return placeholder
}
if (prevItems.length > 0 && newItems.length === 0) {
let placeholder = document.createComment("");
for (let i = 1; i < prevItems.length; i++) {
parent.removeChild(prevItems[i]);
}
parent.replaceChild(placeholder, prevItems[0]);
return placeholder
}
let p = 0, i = 0;
let lastProcessedItem = null;
while (p < prevItems.length && i < newItems.length) {
let pi = prevItems[p];
let ni = newItems[i];
lastProcessedItem = ni;
if (pi === ni) {
p++;
i++;
continue
}
if (ni.parentNode === parent) {
parent.removeChild(pi);
p++;
continue
}
if (ni.parentNode !== parent) {
parent.insertBefore(ni, pi);
i++;
continue
}
throw "Cannot reconcile array"
}
while (i < newItems.length) {
if (newItems[i].parentNode === null) {
insertAfter(parent, lastProcessedItem, newItems[i]);
}
lastProcessedItem = newItems[i];
i++;
}
while (p < prevItems.length) {
parent.removeChild(prevItems[p]);
p++;
}
return newItems
}
function insertAfter(parent, existingElement, newElement) {
if (parent.lastChild == existingElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, existingElement.nextSibling);
}
}
exports.data = data;
exports.h = h;
Object.defineProperty(exports, '__esModule', { value: true });
}));