-
Notifications
You must be signed in to change notification settings - Fork 12
/
devboard.js
81 lines (65 loc) · 1.81 KB
/
devboard.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
var createAtom = require('js-atom').createAtom;
var React = require('react');
var ReactDOM = require('react-dom');
var $ = React.createElement;
var createDefinecard = require('./lib/definecard');
var Devboard = require('./lib/components/Devboard');
var DOMNodeBody = require('./lib/components/DOMNodeBody');
var Row = require('./lib/components/Row');
var ROOT_DIV_ID = '__definecard-root';
// Singleton state
// TODO: eliminate?
var catalog = {};
exports.ns = ns;
function ns(name, options) {
options = options || {};
// Because execution of a loaded module is synchronous
// we can expect a reloading namespace to be filled
// in a single pass of the event loop
// therefore we can enqueue a reRender for the next tick
catalog[name] = {
cards: [],
options: options
};
var cards = catalog[name].cards;
function add(card) {
cards.push(card);
enqueueRender();
}
return createDefinecard(add);
}
exports.atom = createAtom;
/**
* Helpers and Wrappers for types we can't infer
*/
exports.Row = Row;
exports.DOMNode = function DOMNode(render, cleanUp) {
return $(DOMNodeBody, { render: render, cleanUp: cleanUp });
};
/**
* Rendering
*/
var customRender = false;
exports.customRender = function(fn) {
customRender = fn;
};
function getRoot() {
/* eslint-env browser */
var root = document.getElementById(ROOT_DIV_ID);
if (root) return root;
var newRoot = document.createElement('div');
newRoot.id = ROOT_DIV_ID;
document.body.appendChild(newRoot);
return newRoot;
}
function enqueueRender() {
cancelAnimationFrame(enqueueRender.timer);
enqueueRender.timer = requestAnimationFrame(renderRoot, 0);
}
function renderRoot() {
var element = $(Devboard, { catalog: catalog });
if (customRender) {
element = customRender(element);
}
ReactDOM.render(element, getRoot());
}