-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
201 lines (177 loc) · 6.55 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
const _colors = {
"purple":"component",
"green":"data",
"default":"default",
"grey":"default"
}
Noodl.defineNode = function(def) {
const _def = {};
_def.name = def.name;
_def.displayNodeName = def.displayName;
_def.usePortAsLabel = def.useInputAsLabel;
_def.color = _colors[def.color || 'default'];
_def.category = def.category || 'Modules';
_def.allowChildren = def.allowChildren;
_def.getInspectInfo = def.getInspectInfo;
_def.docs = def.docs;
_def.initialize = function() {
this.inputs = {};
var _outputs = this.outputs = {};
var _this = this;
// Function for quickly setting outputs
this.setOutputs = function(o) {
for(var key in o) {
_outputs[key] = o[key];
_this.flagOutputDirty(key);
}
}
// Sending warnings
this.clearWarnings = (function() {
if(this.context.editorConnection && this.nodeScope && this.nodeScope.componentOwner)
this.context.editorConnection.clearWarnings(this.nodeScope.componentOwner.name, this.id);
}).bind(this);
this.sendWarning = (function(name,message) {
if(this.context.editorConnection && this.nodeScope && this.nodeScope.componentOwner)
this.context.editorConnection.sendWarning(this.nodeScope.componentOwner.name, this.id, name, {
message: message
});
}).bind(this);
if(typeof def.initialize === 'function')
def.initialize.apply(this);
}
_def.inputs = {};
_def.outputs = {};
for(var key in def.inputs) {
_def.inputs[key] = {
type:(typeof def.inputs[key] === 'object')?def.inputs[key].type:def.inputs[key],
displayName:(typeof def.inputs[key] === 'object')?def.inputs[key].displayName:undefined,
group:(typeof def.inputs[key] === 'object')?def.inputs[key].group:undefined,
default:(typeof def.inputs[key] === 'object')?def.inputs[key].default:undefined,
set:(function() { const _key = key; return function(value) {
this.inputs[_key] = value;
if(def.changed && typeof def.changed[_key] === 'function') {
def.changed[_key].apply(this,[value]);
}
}})()
}
}
for(var key in def.signals) {
_def.inputs[key] = {
type:'signal',
displayName:(typeof def.signals[key] === 'object')?def.signals[key].displayName:undefined,
group:(typeof def.signals[key] === 'object')?def.signals[key].group:undefined,
valueChangedToTrue:(function() { const _key = key; return function() {
const _fn = (typeof def.signals[_key] === 'object')?def.signals[_key].signal:def.signals[_key]
if(typeof _fn === 'function') {
this.scheduleAfterInputsHaveUpdated(() => {
_fn.apply(this);
})
}
}})()
}
}
for(var key in def.outputs) {
if(def.outputs[key] === 'signal') {
_def.outputs[key] = {
type:'signal',
}
}
else {
_def.outputs[key] = {
type:(typeof def.outputs[key] === 'object')?def.outputs[key].type:def.outputs[key],
displayName:(typeof def.outputs[key] === 'object')?def.outputs[key].displayName:undefined,
group:(typeof def.outputs[key] === 'object')?def.outputs[key].group:undefined,
getter:(function() { const _key = key; return function() {
return this.outputs[_key];
}})()
}
}
}
_def.methods = _def.prototypeExtensions = {};
for(var key in def.methods) {
_def.prototypeExtensions[key] = def.methods[key];
}
if(_def.methods.onNodeDeleted) { // Override the onNodeDeleted if required
_def.methods._onNodeDeleted = function() {
this.__proto__.__proto__._onNodeDeleted.call(this);
_def.methods.onNodeDeleted.value.call(this);
}
}
return {node:_def,setup:def.setup};
}
Noodl.defineCollectionNode = function(def) {
const _def = {
name:def.name,
category:def.category,
color:'data',
inputs:def.inputs,
outputs:Object.assign({
Items:'array',
'Fetch Started':'signal',
'Fetch Completed':'signal'
},def.outputs||{}),
signals:Object.assign({
Fetch:function() {
var _this = this;
this.sendSignalOnOutput('Fetch Started');
var a = def.fetch.call(this,function() {
_this.sendSignalOnOutput('Fetch Completed');
});
this.setOutputs({
Items:a
})
}
},def.signals||{})
}
return Noodl.defineNode(_def);
}
Noodl.defineModelNode = function(def) {
const _def = {
name:def.name,
category:def.category,
color:'data',
inputs:{
Id:'string'
},
outputs:{
Fetched:'signal'
},
changed:{
Id:function(value) {
if(this._object && this._changeListener)
this._object.off('change',this._changeListener)
this._object = Noodl.Object.get(value);
this._changeListener = (name,value) => {
const _o = {}
_o[name] = value;
this.setOutputs(_o)
}
this._object.on('change',this._changeListener)
this.setOutputs(this._object.data);
this.sendSignalOnOutput('Fetched');
}
},
initialize:function() {
}
}
for(var key in def.properties) {
_def.inputs[key] = def.properties[key];
_def.outputs[key] = def.properties[key];
_def.changed[key] = (function() { const _key = key; return function(value) {
if(!this._object) return;
this._object.set(_key,value);
}})()
}
return Noodl.defineNode(_def);
}
Noodl.defineReactNode = function(def) {
var _def = Noodl.defineNode(def);
_def.node.getReactComponent = def.getReactComponent;
_def.node.inputProps = def.inputProps;
_def.node.inputCss = def.inputCss;
_def.node.outputProps = def.outputProps;
_def.node.setup = def.setup;
_def.node.frame = def.frame;
return _def.node;
}
module.exports = Noodl;