-
Notifications
You must be signed in to change notification settings - Fork 1
/
nrfe.js
158 lines (146 loc) · 3.51 KB
/
nrfe.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
var nrfe = function(modules, cb)
{
this.widgets = [];
this.inst_table = modules
if (window.hyper)
{
console.log = hyper.log;
}
console.dir(this.inst_table);
cb(this);
window.dump = function (o)
{
for (var p in o)
{
console.log(p + ' -> ' + o[p]);
}
}
};
nrfe.prototype.render = function(fedef, target)
{
//console.log('nrfe.render preparing to add widgets at DOM node '+target);
this.target = target;
console.dir(fedef);
var page = undefined;
if(fedef)
{
fedef.forEach(function(wdef)
{
if(wdef.type === 'page')
{
page = wdef;
}
this.widgets[wdef.id] = wdef
}.bind(this));
this.renderWidget(page, this.target);
}
};
nrfe.prototype.renderWidget = function(widget_def, parentNode)
{
//console.log('NRFE parsing widget '+widget_def.type);
if(widget_def )
{
var node = widget_def.node || this.instantiateWidget(widget_def, parentNode);
widget_def.node = node;
if(parentNode && widget_def.node)
{
console.log('* * appending '+widget_def.type+' under '+parentNode);
//this.appendChildInOrder(parentNode, widget_def, node);
parentNode.appendChild(node)
}
if(widget_def.wires && !widget_def.wired)
{
widget_def.wired = true;
var sortedChildren = [];
widget_def.wires.forEach(function(widget_def_child)
{
widget_def_child.forEach(function(childid)
{
var w = this.widgets[childid];
w._order = w.order || 1;
sortedChildren.push(w)
}.bind(this));
}.bind(this));
sortedChildren.sort(function(a, b)
{
return a._order === b._order ? 0 : (a._order > b._order ? 1 : -1);
});
sortedChildren.forEach(function(widget)
{
this.renderWidget(widget, node);
}.bind(this));
}
if(node && widget_def.events && !widget_def.evented)
{
widget_def.evented = true;
var earr = widget_def.events || [];
var events = earr.split(',');
//var events = ['click','change','keypress','keyup','keydown', 'focus','blur'];
events.forEach(function(handler)
{
//console.log('adding event handler '+handler+' for widget '+def.type);
node.addEventListener(handler, function(e)
{
widget_def.out({payload:e})
});
});
}
}
};
nrfe.prototype.instantiateWidget = function(widget_def, parentNode)
{
console.log('NRFE instantiating widget '+widget_def.type+', under parent node '+parentNode);
if(!parentNode)
{
//zyzyzy();
}
//console.dir(widget_def)
var nodeCreator = this.inst_table[widget_def.type];
var node = undefined;
if(nodeCreator)
{
node = nodeCreator(widget_def, parentNode);
if(node)
{
node._def = widget_def;
}
//console.log('creator returns node '+node);
widget_def.out = function(msg)
{
var wires = this.widgets[widget_def.id].wires;
//console.log("out wires are..");
//console.log(JSON.stringify(wires));
wires.forEach(function(warr)
{
warr.forEach(function(wid)
{
//console.log('sending message to widget '+wid+' type '+widget_def.type);
var w = this.widgets[wid];
//console.log(JSON.stringify(w));
if(!w.in)
{
console.log('***** ERROR: no "in" function defined at widget '+w+' '+widget_def.type)
dump(w);
}
else
{
w.in(msg);
}
}.bind(this))
}.bind(this))
}.bind(this);
if(!widget_def.in)
{
widget_def.in = function(msg)
{
console.log('-- no message handler defined for node '+widget_def.type+' ['+widget_def.id+']');
console.dir(msg);
};
}
}
else
{
console.log('******************************** COULD NOT FIND CREATOR FOR NODE TYPE '+widget_def.type);
}
return node;
};