-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.js
167 lines (136 loc) · 4.87 KB
/
module.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
// Sparky
// Sparky colour scheme
//
// #d0e84a
// #b9d819 - Principal bg colour
// #a3b31f
// #858720
// #d34515
// #915133
// #723f24
// #6894ab
// #4a6a7a
// #25343c
// #282a2b
if (window.console && window.console.log) {
console.log('%cSparky%c - https://labs.cruncher.ch/sparky', 'color: #a3b31f; font-weight: 600;', 'color: inherit; font-weight: 300;');
}
const DEBUG = false; //window.DEBUG === true || window.DEBUG === 'sparky';
import { requestTick } from '../fn/module.js';
import { element } from '../dom/module.js';
import { cue, uncue } from './modules/timer.js';
import { log, logNode } from './modules/log.js';
import config from './config.js';
import Sparky, { setupSparky, mountSparky } from './modules/sparky.js';
// Register base set of Sparky functions
import './modules/fn-after.js';
import './modules/fn-before.js';
import './modules/fn-append.js';
import './modules/fn-prepend.js';
import './modules/fn-debug.js';
import './modules/fn-each.js';
import './modules/fn-entries.js';
import './modules/fn-fetch.js';
import './modules/fn-get.js';
import './modules/fn-on.js';
import './modules/fn-rest.js';
import './modules/fn-scope.js';
import './modules/fn-take.js';
import fn from './modules/fn.js';
import pipe from './modules/pipe.js';
Sparky.fn = fn;
Sparky.pipe = pipe;
// Export API
export default Sparky;
export { config, cue, uncue };
export { Stream, Observer, observe } from '../fn/module.js';
//export { Fn, get, id, noop, nothing, notify, Observer, Observable, observe, set, Stream, Target } from '../fn/module.js';
//export { events, trigger } from '../dom/module.js';
export { default as mount } from './modules/mount.js';
export { getScope } from './modules/fn-scope.js';
export { transforms, transformers, register as registerTransform } from './modules/transforms.js';
export { register } from './modules/fn.js';
export { default as delegate } from './modules/delegate.js';
export { default as ObserveFn } from './modules/fn-observe.js';
const assign = Object.assign;
/*
Sparky templates
First, import Sparky:
```js
import '/sparky/module.js';
```
Sparky registers the `is="sparky-template"` custom element. Sparky templates
in the DOM are automatically replaced with their own rendered content:
```html
<template is="sparky-template">
Hello!
</template>
```
```html
Hello!
```
Sparky templates extend HTML with 3 features: **template tags**, **functions**
and **includes**.
*/
// Register customised built-in element <template is="sparky-template">
//
// While loading we must wait a tick for sparky functions to register before
// declaring the customised template element. This is a little pants, I admit.
requestTick(function() {
var supportsCustomBuiltIn = false;
element('sparky-template', {
extends: 'template',
properties: {},
attributes: {
src: function(src) {
this.options.src = src;
},
fn: function(fn) {
this.options.fn = fn;
}
},
construct: function(elem) {
elem.options = assign({
mount: mountSparky
}, config);
// Flag
supportsCustomBuiltIn = true;
},
connect: function(elem) {
if (DEBUG) { logNode(elem, elem.options.fn, elem.options.src); }
if (elem.options.fn) {
setupSparky(elem, elem, elem.options);
}
else {
// If there is no attribute fn, there is no way for this sparky
// to launch as it will never get scope. Enable sparky templates
// with just an include by passing in blank scope.
setupSparky(elem, elem, elem.options).push({});
}
}
});
// If one has not been found already, test for customised built-in element
// support by force creating a <template is="sparky-template">
if (!supportsCustomBuiltIn) {
document.createElement('template', { is: 'sparky-template' });
}
// If still not supported, fallback to a dom query for [is="sparky-template"]
if (!supportsCustomBuiltIn) {
log("Browser does not support custom built-in elements so we're doin' it oldskool selector stylee.");
window.document
.querySelectorAll('[is="sparky-template"]')
.forEach((template) => {
const fn = template.getAttribute(config.attributeFn) || undefined;
const src = template.getAttribute(config.attributeSrc) || undefined;
if (fn) {
Sparky(template, { fn: fn, src: src });
}
else {
// If there is no attribute fn, there is no way for this sparky
// to launch as it will never get scope. Enable sparky templates
// with just an include by passing in blank scope.
Sparky(template, { src: src }).push({});
}
});
}
});