UNPKG

17.4 kBJavaScriptView Raw
1var components = (function () {
2 'use strict';
3
4 var is_1 = is;
5 is.fn = isFunction;
6 is.str = isString;
7 is.num = isNumber;
8 is.obj = isObject;
9 is.lit = isLiteral;
10 is.bol = isBoolean;
11 is.truthy = isTruthy;
12 is.falsy = isFalsy;
13 is.arr = isArray;
14 is.null = isNull;
15 is.def = isDef;
16 is.in = isIn;
17 is.promise = isPromise;
18 is.stream = isStream;
19 function is(v) {
20 return function (d) {
21 return d == v;
22 };
23 }
24
25 function isFunction(d) {
26 return typeof d == 'function';
27 }
28
29 function isBoolean(d) {
30 return typeof d == 'boolean';
31 }
32
33 function isString(d) {
34 return typeof d == 'string';
35 }
36
37 function isNumber(d) {
38 return typeof d == 'number';
39 }
40
41 function isObject(d) {
42 return typeof d == 'object';
43 }
44
45 function isLiteral(d) {
46 return d.constructor == Object;
47 }
48
49 function isTruthy(d) {
50 return !(!d) == true;
51 }
52
53 function isFalsy(d) {
54 return !(!d) == false;
55 }
56
57 function isArray(d) {
58 return d instanceof Array;
59 }
60
61 function isNull(d) {
62 return d === null;
63 }
64
65 function isDef(d) {
66 return typeof d !== 'undefined';
67 }
68
69 function isPromise(d) {
70 return d instanceof Promise;
71 }
72
73 function isStream(d) {
74 return !(!(d && d.next));
75 }
76
77 function isIn(set) {
78 return function (d) {
79 return !set ? false : set.indexOf ? ~set.indexOf(d) : d in set;
80 };
81 }
82
83 var is$1 = /*#__PURE__*/Object.freeze({
84 default: is_1,
85 __moduleExports: is_1
86 });
87
88 var is$2 = ( is$1 && is_1 ) || is$1;
89
90 var to = {
91 arr: toArray,
92 obj: toObject
93 };
94 function toArray(d) {
95 return Array.prototype.slice.call(d, 0);
96 }
97
98 function toObject(d) {
99 var by = 'id';
100 return arguments.length == 1 ? (by = d, reduce) : reduce.apply(this, arguments);
101 function reduce(p, v, i) {
102 if (i === 0)
103 { p = {}; }
104 p[is$2.fn(by) ? by(v, i) : v[by]] = v;
105 return p;
106 }
107
108 }
109 var to_1 = to.arr;
110 var to_2 = to.obj;
111
112 var to$1 = /*#__PURE__*/Object.freeze({
113 default: to,
114 __moduleExports: to,
115 arr: to_1,
116 obj: to_2
117 });
118
119 var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
120
121 function createCommonjsModule(fn, module) {
122 return module = { exports: {} }, fn(module, module.exports), module.exports;
123 }
124
125 var client = typeof window != 'undefined';
126
127 var client$1 = /*#__PURE__*/Object.freeze({
128 default: client,
129 __moduleExports: client
130 });
131
132 var client$2 = ( client$1 && client ) || client$1;
133
134 var owner = client$2 ? window : commonjsGlobal;
135
136 var owner$1 = /*#__PURE__*/Object.freeze({
137 default: owner,
138 __moduleExports: owner
139 });
140
141 var to$2 = ( to$1 && to ) || to$1;
142
143 var owner$2 = ( owner$1 && owner ) || owner$1;
144
145 var log = function log(ns) {
146 return function (d) {
147 if (!owner$2.console || !console.log.apply)
148 { return d; }
149 is$2.arr(arguments[2]) && (arguments[2] = arguments[2].length);
150 var args = to$2.arr(arguments), prefix = '[log][' + new Date().toISOString() + ']' + ns;
151 args.unshift(prefix.grey ? prefix.grey : prefix);
152 return console.log.apply(console, args), d;
153 };
154 };
155
156 var log$1 = /*#__PURE__*/Object.freeze({
157 default: log,
158 __moduleExports: log
159 });
160
161 var ready = function ready(fn) {
162 return document.body ? fn() : document.addEventListener('DOMContentLoaded', fn.bind(this));
163 };
164
165 var ready$1 = /*#__PURE__*/Object.freeze({
166 default: ready,
167 __moduleExports: ready
168 });
169
170 var _class = function (definition) { return assign(definition.class ? definition.class : !definition.prototype ? classed(definition) : definition.prototype.render ? definition : definition.prototype.connected ? definition : classed(definition)); };
171 var assign = Object.assign;
172 var classed = function (render) { return render.class = render.class || (function () {
173 function anonymous () {}
174
175 anonymous.prototype.render = function render$1 () {
176 render.apply(this, arguments);
177 };
178
179 return anonymous;
180 }()); };
181
182 var _class$1 = /*#__PURE__*/Object.freeze({
183 default: _class,
184 __moduleExports: _class
185 });
186
187 var promise_1 = promise;
188 function promise() {
189 var resolve, reject, p = new Promise(function (res, rej) {
190 resolve = res, reject = rej;
191 });
192 arguments.length && resolve(arguments[0]);
193 p.resolve = resolve;
194 p.reject = reject;
195 return p;
196 }
197
198 var promise$1 = /*#__PURE__*/Object.freeze({
199 default: promise_1,
200 __moduleExports: promise_1
201 });
202
203 var flatten = function flatten(p, v) {
204 if (v instanceof Array)
205 { v = v.reduce(flatten, []); }
206 return p = p || [], p.concat(v);
207 };
208
209 var flatten$1 = /*#__PURE__*/Object.freeze({
210 default: flatten,
211 __moduleExports: flatten
212 });
213
214 var has = function has(o, k) {
215 return k in o;
216 };
217
218 var has$1 = /*#__PURE__*/Object.freeze({
219 default: has,
220 __moduleExports: has
221 });
222
223 var has$2 = ( has$1 && has ) || has$1;
224
225 var def = function def(o, p, v, w) {
226 if (o.host && o.host.nodeName)
227 { o = o.host; }
228 if (p.name)
229 { v = p, p = p.name; }
230 !has$2(o, p) && Object.defineProperty(o, p, {
231 value: v,
232 writable: w
233 });
234 return o[p];
235 };
236
237 var def$1 = /*#__PURE__*/Object.freeze({
238 default: def,
239 __moduleExports: def
240 });
241
242 var promise$2 = ( promise$1 && promise_1 ) || promise$1;
243
244 var flatten$2 = ( flatten$1 && flatten ) || flatten$1;
245
246 var def$2 = ( def$1 && def ) || def$1;
247
248 var noop = function () {};
249 var emitterify = function emitterify(body, hooks) {
250 body = body || {};
251 hooks = hooks || {};
252 def$2(body, 'emit', emit, 1);
253 def$2(body, 'once', once, 1);
254 def$2(body, 'off', off, 1);
255 def$2(body, 'on', on, 1);
256 body.on['*'] = body.on['*'] || [];
257 return body;
258 function emit(type, pm, filter) {
259 var li = body.on[type.split('.')[0]] || [], results = [];
260 for (var i = 0;i < li.length; i++)
261 { if (!li[i].ns || !filter || filter(li[i].ns))
262 { results.push(call(li[i].isOnce ? li.splice(i--, 1)[0] : li[i], pm)); } }
263 for (var i = 0;i < body.on['*'].length; i++)
264 { results.push(call(body.on['*'][i], [type,pm])); }
265 return results.reduce(flatten$2, []);
266 }
267
268 function call(cb, pm) {
269 return cb.next ? cb.next(pm) : pm instanceof Array ? cb.apply(body, pm) : cb.call(body, pm);
270 }
271
272 function on(type, opts, isOnce) {
273 var id = type.split('.')[0], ns = type.split('.')[1], li = body.on[id] = body.on[id] || [], cb = typeof opts == 'function' ? opts : 0;
274 return !cb && ns ? (cb = body.on[id]['$' + ns]) ? cb : push(observable(body, opts)) : !cb && !ns ? push(observable(body, opts)) : cb && ns ? push((remove(li, body.on[id]['$' + ns] || -1), cb)) : cb && !ns ? push(cb) : false;
275 function push(cb) {
276 cb.isOnce = isOnce;
277 cb.type = id;
278 if (ns)
279 { body.on[id]['$' + (cb.ns = ns)] = cb; }
280 li.push(cb);
281 (hooks.on || noop)(cb);
282 return cb.next ? cb : body;
283 }
284
285 }
286
287 function once(type, callback) {
288 return body.on(type, callback, true);
289 }
290
291 function remove(li, cb) {
292 var i = li.length;
293 while (~--i)
294 { if (cb == li[i] || cb == li[i].fn || !cb)
295 { (hooks.off || noop)(li.splice(i, 1)[0]); } }
296 }
297
298 function off(type, cb) {
299 remove(body.on[type] || [], cb);
300 if (cb && cb.ns)
301 { delete body.on[type]['$' + cb.ns]; }
302 return body;
303 }
304
305 function observable(parent, opts) {
306 opts = opts || {};
307 var o = emitterify(opts.base || promise$2());
308 o.i = 0;
309 o.li = [];
310 o.fn = opts.fn;
311 o.parent = parent;
312 o.source = opts.fn ? o.parent.source : o;
313 o.on('stop', function (reason) {
314 o.type ? o.parent.off(o.type, o) : o.parent.off(o);
315 return o.reason = reason;
316 });
317 o.each = function (fn) {
318 var n = fn.next ? fn : observable(o, {
319 fn: fn
320 });
321 o.li.push(n);
322 return n;
323 };
324 o.pipe = function (fn) {
325 return fn(o);
326 };
327 o.map = function (fn) {
328 return o.each(function (d, i, n) {
329 return n.next(fn(d, i, n));
330 });
331 };
332 o.filter = function (fn) {
333 return o.each(function (d, i, n) {
334 return fn(d, i, n) && n.next(d);
335 });
336 };
337 o.reduce = function (fn, acc) {
338 return o.each(function (d, i, n) {
339 return n.next(acc = fn(acc, d, i, n));
340 });
341 };
342 o.unpromise = function () {
343 var n = observable(o, {
344 base: {},
345 fn: function (d) {
346 return n.next(d);
347 }
348 });
349 o.li.push(n);
350 return n;
351 };
352 o.next = function (value) {
353 o.resolve && o.resolve(value);
354 return o.li.length ? o.li.map(function (n) {
355 return n.fn(value, n.i++, n);
356 }) : value;
357 };
358 o.until = function (stop) {
359 return stop.each ? stop.each(o.stop) : stop.then ? stop.then(o.stop) : stop.call ? o.filter(stop).map(o.stop) : 0;
360 };
361 o.off = function (fn) {
362 return remove(o.li, fn), o;
363 };
364 o.start = function () {
365 o.source.emit('start');
366 return o;
367 };
368 o.stop = function (reason) {
369 return o.source.emit('stop', reason);
370 };
371 o[Symbol.asyncIterator] = function () {
372 return {
373 next: function () {
374 return o.wait = new Promise(function (resolve) {
375 o.wait = true;
376 o.map(function (d, i, n) {
377 delete o.wait;
378 o.off(n);
379 resolve({
380 value: d,
381 done: false
382 });
383 });
384 o.emit('pull', o);
385 });
386 }
387 };
388 };
389 return o;
390 }
391
392 };
393
394 var emitterify$1 = /*#__PURE__*/Object.freeze({
395 default: emitterify,
396 __moduleExports: emitterify
397 });
398
399 var emitterify$2 = ( emitterify$1 && emitterify ) || emitterify$1;
400
401 var event = function event(node, index) {
402 node = node.host && node.host.nodeName ? node.host : node;
403 if (node.on)
404 { return; }
405 node.listeners = {};
406 var on = function (o) {
407 var type = o.type.split('.').shift();
408 if (!node.listeners[type])
409 { node.addEventListener(type, node.listeners[type] = (function (event) { return !event.detail || !event.detail.emitted ? emit(type, [event,
410 node.state,node]) : 0; })); }
411 };
412 var off = function (o) {
413 if (!node.on[o.type] || !node.on[o.type].length) {
414 node.removeEventListener(o.type, node.listeners[o.type]);
415 delete node.listeners[o.type];
416 }
417 };
418 emitterify$2(node, {
419 on: on,
420 off: off
421 });
422 var emit = node.emit;
423 node.emit = function (type, params) {
424 var detail = {
425 params: params,
426 emitted: true
427 }, event = new CustomEvent(type, {
428 detail: detail,
429 bubbles: false,
430 cancelable: true
431 });
432 node.dispatchEvent(event);
433 return emit(type, event);
434 };
435 };
436
437 var event$1 = /*#__PURE__*/Object.freeze({
438 default: event,
439 __moduleExports: event
440 });
441
442 var classed$1 = ( _class$1 && _class ) || _class$1;
443
444 var event$2 = ( event$1 && event ) || event$1;
445
446 var define = createCommonjsModule(function (module) {
447 var noop = function () {}, HTMLElement = client$2 && window.HTMLElement || (function () {
448 function anonymous () {}
449
450 return anonymous;
451 }()), registry = client$2 && window.customElements || {};
452 module.exports = function define(name, component) {
453 if (arguments.length == 1) {
454 component = name, name = "anon-" + (registry.anon++);
455 }
456 if (component.hasOwnProperty('wrapper'))
457 { return component.wrapper; }
458 if (!name.includes('-'))
459 { return; }
460 if (!client$2)
461 { return wrap(classed$1(component)); }
462 var wrapped = registry.get(name);
463 if (wrapped) {
464 if (wrapped.class == classed$1(component))
465 { return wrapped; }
466 wrapped.class = classed$1(component);
467 var instances = Array.from(document.querySelectorAll(name));
468 instances.map(function (node) {
469 node.disconnectedCallback();
470 node.methods.map(function (method) {
471 delete node[method];
472 });
473 node.connectedCallback();
474 });
475 } else {
476 registry.define(name, wrapped = wrap(classed$1(component)));
477 }
478 return wrapped;
479 };
480 var wrap = function (component) {
481 if (!component.hasOwnProperty('wrapper'))
482 { component.wrapper = (function (HTMLElement) {
483 function undefined () {
484 HTMLElement.apply(this, arguments);
485 }
486
487 if ( HTMLElement ) undefined.__proto__ = HTMLElement;
488 undefined.prototype = Object.create( HTMLElement && HTMLElement.prototype );
489 undefined.prototype.constructor = undefined;
490
491 undefined.prototype.connectedCallback = function connectedCallback () {
492 var this$1 = this;
493
494 var ref = component.wrapper.class;
495 var prototype = ref.prototype;
496 event$2(this);
497 this.ready = this.once('ready');
498 this.state = this.state || {};
499 this.methods = Object.getOwnPropertyNames(prototype).filter(function (method) { return !(method in disallowed); }).map(function (method) { return (this$1[method] = prototype[method].bind(this$1), method); });
500 return Promise.resolve((this.connected || noop).call(this, this, this.state)).then(function (d) {
501 this$1.emit('ready');
502 return this$1.render();
503 });
504 };
505 undefined.prototype.render = function render () {
506 var this$1 = this;
507
508 var ref = component.wrapper.class;
509 var prototype = ref.prototype;
510 return this.pending = this.pending || this.ready.then(function () {
511 delete this$1.pending;
512 return prototype.render.call(this$1, this$1, this$1.state);
513 });
514 };
515 undefined.prototype.disconnectedCallback = function disconnectedCallback () {
516 (this.disconnected || noop).call(this, this, this.state);
517 this.dispatchEvent(new CustomEvent('disconnected'));
518 this.initialised = false;
519 };
520 undefined.prototype.get = function get (sel) {
521 return this.querySelector(sel);
522 };
523
524 return undefined;
525 }(HTMLElement)); }
526 component.wrapper.class = component;
527 return component.wrapper;
528 };
529 var disallowed = {
530 length: 1,
531 prototype: 1,
532 name: 1,
533 render: 1
534 };
535 registry.anon = registry.anon || 1;
536 });
537
538 var define$1 = /*#__PURE__*/Object.freeze({
539 default: define,
540 __moduleExports: define
541 });
542
543 var require$$0 = ( log$1 && log ) || log$1;
544
545 var ready$2 = ( ready$1 && ready ) || ready$1;
546
547 var define$2 = ( define$1 && define ) || define$1;
548
549 var components = function components(ripple) {
550 if (!client$2)
551 { return ripple; }
552 log$2('creating');
553 Node.prototype.render = function () {
554 var name = this.nodeName.toLowerCase();
555 if (name.includes('-'))
556 { return this.fn$ = this.fn$ || ripple.subscribe(name).map(function (component) { return define$2(name, component); }); }
557 };
558 Node.prototype.draw = function () {
559 this.render();
560 };
561 ready$2(function () { return Array.from(document.querySelectorAll('*')).filter(function (d) { return d.nodeName.includes('-'); }).map(function (node) { return node.render(); }); });
562 return ripple;
563 };
564 var log$2 = require$$0('[ri/components]');
565
566 return components;
567
568}());