-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWebObject.js
More file actions
489 lines (397 loc) · 13.7 KB
/
Copy pathWebObject.js
File metadata and controls
489 lines (397 loc) · 13.7 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*******************************************************************************
Copyright 2015 CREATE-NET
Developed for COMPOSE project (compose-project.eu)
@author Luca Capra <luca.capra@create-net.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
(function(){
var wolib = {};
wolib.setup = function(compose) {
var ComposeError = compose.error.ComposeError;
var copyVal = compose.util.copyVal;
if(!compose) {
throw new ComposeError("compose.io module reference not provided, quitting..");
}
/**
*
* A list of Channel of a Stream
*
* @constructor
* @augments ObjectList
*/
var ChannelsList = function(channels) {
compose.util.List.ObjectList.apply(this);
this.initialize(channels);
};
compose.util.extend(ChannelsList, compose.util.List.ObjectList);
ChannelsList.prototype.validate = function(channel) {
// if(!channel.name) {
// throw new ValidationError("Channel must have a `name` property");
// }
//
// if(!channel.type) {
// throw new ValidationError("Channel must have a `type` property");
// }
//
// if(!channel.unit) {
// throw new ValidationError("Channel must have a `unit` property");
// }
//
// if(channel.type !== 'Number' || channel.type !== 'String' || channel.type !== 'Boolean' ) {
// throw new ValidationError("Channel `type` must be one of Number, String or Boolean");
// }
return channel;
};
/**
*
* A list of Stream objects of a WebObject
*
* @constructor
* @augments ObjectList
*/
var StreamList = function(streams) {
compose.util.List.ObjectList.apply(this);
if(this instanceof StreamList) {
this.initialize(streams);
}
};
compose.util.extend(StreamList, compose.util.List.ObjectList);
StreamList.prototype.add = function(name, obj) {
if (typeof name === "object") {
for (var i in name) {
this.add(i, name[i]);
}
return this;
}
// handle arrays using the obj.name property
if(obj.name && (typeof (parseFloat(name)) === 'number')) {
name = obj.name;
}
if(!obj.name) {
obj.name = name;
}
var stream = this.validate(obj);
this.getList()[name] = stream;
return stream;
};
/**
* @param {String} name Identifier name
* @return {Number} Return the index or -1 if not found
*/
StreamList.prototype.getIndex = function(name, key) {
var list = this.getList();
if(list[name]) {
return name;
}
key = key || 'name';
var _size = this.size();
for (var i = 0; i < _size; i++) {
if (list[i][key] === name) {
return i;
}
}
return -1;
};
StreamList.prototype.validate = function(stream) {
var streamObj = new Stream(stream);
streamObj.container(this.container());
return streamObj;
};
/*
*
* @param {boolean} asString Return as string if true, object otherwise
* @returns {Object|String}
*/
StreamList.prototype.toJson = function(asString) {
var list = this.getList();
var json = copyVal(list);
return asString ? JSON.stringify(json) : json;
};
StreamList.prototype.toString = function() {
return this.toJson(true);
};
/**
*
* A Stream object
*
* @constructor
*/
var Stream = function(obj) {
if(this instanceof Stream) {
this.initialize(obj);
}
};
Stream.prototype.__$container;
Stream.prototype.container = function(o) {
this.__$container = o || this.__$container;
return this.__$container;
};
/**
* Add a list of elements provided as argument to the stream
* @param {Object} obj An object with the properties to set for the Stream
*/
Stream.prototype.initialize = function(obj) {
obj = obj || {};
for (var i in obj) {
if (!this[i]) {
this[i] = obj[i];
}
}
this.channels = new ChannelsList(obj.channels || {});
this.channels.container(this.container());
};
/**
* Add or updates a channel. This function handles multiple arguments, eg.
*
* - addChannel(name, channel)
* - addChannel(name, unit, type, value)
*
* @param {String} name Name of the channel
* @param {String|Object} channel|unit Channel object (or unit value, when arguments count is >= 3)
* @param {String} type Type of value
*
* @return {Stream} The current stream
* */
Stream.prototype.addChannel = function(name, channel, a3, a4) {
if (arguments.length >= 3) {
name = arguments[0];
channel = {
"unit": arguments[1],
"type": arguments[2]
};
}
this.channels.add(name, channel);
return this;
};
/**
* Add or updates a list of channels
*
* @param {Object} channels List of channels
*
* @return {Stream} The current stream
* */
Stream.prototype.addChannels = function(channels) {
this.channels.add(channels);
return this;
};
/**
* @return {ChannelsList} The list of channels
*/
Stream.prototype.getChannels = function() {
return this.channels;
};
/**
* @param {String} name The channel name
* @return {Object} The requested channel or null if not available
*/
Stream.prototype.getChannel = function(name) {
return this.channels.get(name);
};
/*
*
* @param {boolean} asString Return as string if true, object otherwise
* @returns {Object|String}
*/
Stream.prototype.toJson = function(asString) {
var json = {};
copyVal(this, json);
json.channels = this.channels.toJson();
return asString ? JSON.stringify(json) : json;
};
Stream.prototype.toString = function() {
return this.toJson(true);
};
/**
* Creates a WebObject instance
*/
var WebObject = function(objdef) {
var me = this;
this.properties = [];
this.customFields = {};
if(this instanceof WebObject) {
this.initialize(objdef);
}
};
WebObject.prototype.__$streams = null;
WebObject.prototype.__$actions = null;
/**
* Take an object and set the fields defining the WO accordingly
* This method will overwrite any previous information
*
* Minimum information required are
* `{ properties: { name: "<wo name>", id: "<wo id>" } }`
*
* @param {Object} obj An object with the definition of the WO.
* @return {WebObject} A webobject instace
*/
WebObject.prototype.initialize = function(obj) {
obj = obj || {};
for (var i in obj) {
if (typeof obj[i] !== 'function') {
this[i] = obj[i];
}
}
this.customFields = obj.customFields || {};
this.properties = obj.properties || [];
this.setStreams(copyVal(obj.streams || {}));
this.setActions(copyVal(obj.actions || {}));
return this;
};
WebObject.prototype.getStreams = function() {
return this.__$streams;
};
/**
*
*/
WebObject.prototype.setStreams = function(streams) {
var _streams = new StreamList(streams);
_streams.container(this);
this.__$streams = _streams;
};
/**
*
* @param {String} name The stream name
* @return {Object} The Streamobject
*/
WebObject.prototype.getStream = function(name) {
return this.getStreams().get(name);
};
WebObject.prototype.getActions = function() {
return this.__$actions;
};
/**
*
* @param {Object} actions
* @returns {WebObject} self reference
*/
WebObject.prototype.setActions = function(actions) {
var _actions = this.getActions();
_actions = new compose.util.List.ArrayList(actions);
_actions.container(this);
this.__$actions = _actions;
return this;
};
/**
* @param {String} name The action name
* @return {Object} The Action object
*/
WebObject.prototype.getAction = function(name) {
return this.getActions().get(name);
};
/**
* @param {Object} key The object name
* @param {Object} stream The object with stream data
*
* @return {Object} The Stream object
*/
WebObject.prototype.addStream = function(key, stream) {
return this.getStreams().add(key, stream);
};
/**
* @param {Array} streams List of objects to add
* @return {WebObject} The WO object
*/
WebObject.prototype.addStreams = function(streams) {
if (typeof streams === "object") {
for (var i in streams) {
this.addStream((typeof parseFloat(i) === 'number') ? streams[i].name : i, streams[i]);
}
}
return this;
};
/**
* @param {Object} action The object to add
* @return {Object} The Action object
*/
WebObject.prototype.addAction = function(action) {
return this.getActions().add(action);
};
/**
* @param {Array} actions List of objects to add
* @return {WebObject} The WO object
*/
WebObject.prototype.addActions = function(actions) {
if (actions instanceof Array) {
for (var i = 0; i < actions.length; i++) {
this.getActions().add(actions[i]);
}
}
return this;
};
/*
*
* @param {boolean} asString Return as string if true, object otherwise
* @returns {Object|String}
*/
WebObject.prototype.toJson = function(asString) {
var json = {};
for (var i in this) {
if (typeof this[i] !== 'function' && i.substr(0, 3) !== '__$') {
if(this[i] !== null) {
json[i] = this[i];
}
}
}
var streams = this.getStreams();
json.streams = streams ? streams.toJson() : {};
var actions = this.getActions();
json.actions = actions ? actions.toJson() : [];
return asString ? JSON.stringify(json, null) : json;
};
WebObject.prototype.toString = function() {
return this.toJson(true);
};
/**
* StreamList class
*/
wolib.StreamList = StreamList;
/**
* Stream class
*/
wolib.Stream = Stream;
/**
* WebObject class
*/
wolib.WebObject = WebObject;
/**
* Creates a new instance of a WebObject
*
* @param {Object} wo An object with WebObject properties
*/
wolib.create = function(wo) {
return new WebObject(wo || {});
};
// // read a json file by name @todo need to be ported and adapted
// wolib.read = function(name) {
// var platform = getPlatformImpl();
// var content = platform.readFile(name, { definitionsPath: getDefinitionPath() });
// return content ? JSON.parse(content) : {};
// };
};
//-- multiplatform support
(function(libname, lib, deps) {
deps = (deps instanceof Array) ? deps : ['compose'];
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = lib;
}
else {
if (typeof define === 'function' && define.amd) {
define(deps, function(compose) {
return lib;
});
}
if(typeof window !== 'undefined') {
window.__$$composeioRegistry[libname] = lib;
}
}
})
('WebObject', wolib, ['compose', 'utils/List']);
})();