-
Notifications
You must be signed in to change notification settings - Fork 7
/
Object.js
executable file
·117 lines (113 loc) · 2.55 KB
/
Object.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
define(['./jo', './Class' ], function(jo, Class){
jo.Object = jo.Class.extend(
/**
* @lends jo.Object.prototype
*/
{
/**
* holds the type of the object, for serialisation reviving from JSON
* must be the official name without the 'jo.'
*/
joObject : 'Object',
/**
* @constructor
* all jo Objects take just one options argument with the constructor for easy serialisation
*/
init : function(options){
this.options = options;
this.obj = {};
this.joObject = this.joObject;
this.call={};
},
hasCall: function(name){
return typeof this.call[name] === 'function';
},
/**
* adds a callback with a given name, overwrites existing ones with the same name
* @param name
* @param fn
*/
addCall: function(name, fn){
if(typeof fn !== 'function'){
throw 'expecting a function';
}
this.call[name] = fn;
},
removeCall: function(name){
if(typeof this.call[name] !== 'undefined'){
this.call[name]= undefined;
}
},
applyCall: function(name, args ){
if(typeof this.call[name] === 'function'){
this.call[name].apply(this, args);
}
},
draw: function(options, position, surface){
for(var i in this.obj){
this.obj[i].draw(options, position, surface);
}
},
update: function(time){
for(var i in this.obj){
this.obj[i].update(time);
}
},
addObject: function(name, obj){
if(typeof this.obj[name] === 'undefined'){
obj._parent = this;
obj._name = name;
this.obj[name] = obj;
}else {
this.obj[name].addObject(name, obj);
}
},
getObject: function(id){
return this.obj[id];
},
removeObject: function(name){
delete this.obj[name];
this.obj[name] = undefined;
},
removeAllObjects: function(){
this.obj = {};
},
stringify: function(filter){
return JSON.stringify(this, filter || jo.Object.replacer);
},
_postParse: function(){
for(var i in this.obj){
this.obj[i]._name = i;
this.obj[i]._parent=this;
}
}
});
jo.Object.replacer = function(key, value) {
if(key === '_parent'){
value = key;
}
return value;
};
jo.Object.revive = function(key, value){
var val = 0;
if(value && value.joObject ){
val = new jo[value.joObject](value.options);
val.adapt(value, true);
value = val;
jo.log(val);
return val;
}
if(value){
if(value.isPoint){
return (new jo.Point().copy(value));
}
}
return value;
};
jo.Object.parse = function(text){
var obj = JSON.parse(text, jo.Object.revive);
obj._postParse();
return obj;
};
return jo.Object;
});