|
23
|
1 |
(function() { |
|
|
2 |
|
|
|
3 |
var root = this; |
|
|
4 |
|
|
|
5 |
var Backbone = root.Backbone; |
|
|
6 |
|
|
|
7 |
var Models = root.Rkns.Models = {}; |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
Models.getUID = function(obj) { |
|
|
11 |
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
|
|
12 |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); |
|
|
13 |
return v.toString(16); |
|
|
14 |
}); |
|
|
15 |
return obj.type + "-" + guid; |
|
|
16 |
}; |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
var RenkanModel = Backbone.RelationalModel.extend({ |
|
|
20 |
idAttribute : "_id", |
|
|
21 |
constructor: function(options) { |
|
|
22 |
|
|
|
23 |
if (typeof options !== "undefined") { |
|
27
|
24 |
options._id = options._id || options.id || Models.getUID(this); |
|
23
|
25 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
|
26 |
options.description = options.description || ""; |
|
|
27 |
options.uri = options.uri || ""; |
|
|
28 |
|
|
|
29 |
if(typeof this.prepare === "function") { |
|
|
30 |
options = this.prepare(options); |
|
|
31 |
} |
|
|
32 |
} |
|
|
33 |
Backbone.RelationalModel.prototype.constructor.call(this, options); |
|
|
34 |
}, |
|
|
35 |
validate: function() { |
|
|
36 |
if(!this.type) { |
|
|
37 |
return "object has no type"; |
|
|
38 |
} |
|
|
39 |
}, |
|
|
40 |
addReference : function(_options, _propName, _list, _id, _default) { |
|
|
41 |
var _element = _list.get(_id); |
|
|
42 |
if (typeof _element === "undefined" && typeof _default !== "undefined") { |
|
|
43 |
_options[_propName ] = _default; |
|
|
44 |
} |
|
|
45 |
else { |
|
|
46 |
_options[_propName ] = _element; |
|
|
47 |
} |
|
|
48 |
} |
|
|
49 |
}); |
|
|
50 |
|
|
|
51 |
// USER |
|
|
52 |
var User = Models.User = RenkanModel.extend({ |
|
|
53 |
type: "user", |
|
|
54 |
prepare: function(options) { |
|
|
55 |
options.color = options.color || "#666666"; |
|
|
56 |
return options; |
|
24
|
57 |
}, |
|
|
58 |
toJSON: function() { |
|
|
59 |
return { |
|
27
|
60 |
_id: this.get("_id"), |
|
24
|
61 |
title: this.get("title"), |
|
|
62 |
uri: this.get("uri"), |
|
|
63 |
description: this.get("description"), |
|
|
64 |
color: this.get("color"), |
|
27
|
65 |
}; |
|
24
|
66 |
}, |
|
23
|
67 |
}); |
|
|
68 |
|
|
|
69 |
// NODE |
|
|
70 |
var Node = Models.Node = RenkanModel.extend({ |
|
|
71 |
type: "node", |
|
|
72 |
relations: [{ |
|
|
73 |
type: Backbone.HasOne, |
|
|
74 |
key: "created_by", |
|
|
75 |
relatedModel: User |
|
|
76 |
}], |
|
|
77 |
prepare: function(options) { |
|
|
78 |
project = options.project; |
|
|
79 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
|
80 |
options.description = options.description || ""; |
|
|
81 |
return options; |
|
24
|
82 |
}, |
|
|
83 |
toJSON: function() { |
|
|
84 |
return { |
|
27
|
85 |
_id: this.get("_id"), |
|
24
|
86 |
title: this.get("title"), |
|
|
87 |
uri: this.get("uri"), |
|
|
88 |
description: this.get("description"), |
|
|
89 |
position: this.get("position"), |
|
37
|
90 |
image: this.get("image"), |
|
52
|
91 |
color: this.get("color"), |
|
45
|
92 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
|
27
|
93 |
}; |
|
24
|
94 |
}, |
|
23
|
95 |
}); |
|
|
96 |
|
|
|
97 |
// EDGE |
|
|
98 |
var Edge = Models.Edge = RenkanModel.extend({ |
|
|
99 |
type: "edge", |
|
|
100 |
relations: [ |
|
|
101 |
{ |
|
|
102 |
type: Backbone.HasOne, |
|
|
103 |
key: "created_by", |
|
|
104 |
relatedModel: User |
|
|
105 |
}, |
|
|
106 |
{ |
|
|
107 |
type: Backbone.HasOne, |
|
|
108 |
key: "from", |
|
|
109 |
relatedModel: Node |
|
|
110 |
}, |
|
|
111 |
{ |
|
|
112 |
type: Backbone.HasOne, |
|
|
113 |
key: "to", |
|
|
114 |
relatedModel: Node |
|
|
115 |
}, |
|
|
116 |
], |
|
|
117 |
prepare: function(options) { |
|
|
118 |
project = options.project; |
|
|
119 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
|
120 |
this.addReference(options, "from", project.get("nodes"), options.from); |
|
|
121 |
this.addReference(options, "to", project.get("nodes"), options.to); |
|
|
122 |
return options; |
|
24
|
123 |
}, |
|
|
124 |
toJSON: function() { |
|
|
125 |
return { |
|
27
|
126 |
_id: this.get("_id"), |
|
24
|
127 |
title: this.get("title"), |
|
|
128 |
uri: this.get("uri"), |
|
|
129 |
description: this.get("description"), |
|
45
|
130 |
from: this.get("from") ? this.get("from").get("_id") : null, |
|
|
131 |
to: this.get("to") ? this.get("to").get("_id") : null, |
|
52
|
132 |
color: this.get("color"), |
|
45
|
133 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
|
27
|
134 |
}; |
|
24
|
135 |
}, |
|
23
|
136 |
}); |
|
|
137 |
|
|
|
138 |
// PROJECT |
|
|
139 |
var Project = Models.Project = RenkanModel.extend({ |
|
|
140 |
type: "project", |
|
|
141 |
relations: [ |
|
|
142 |
{ |
|
|
143 |
type: Backbone.HasMany, |
|
|
144 |
key: "users", |
|
|
145 |
relatedModel: User, |
|
|
146 |
reverseRelation: { |
|
|
147 |
key: 'project', |
|
|
148 |
includeInJSON: '_id' |
|
|
149 |
}, |
|
|
150 |
}, |
|
|
151 |
{ |
|
|
152 |
type: Backbone.HasMany, |
|
|
153 |
key: "nodes", |
|
|
154 |
relatedModel: Node, |
|
|
155 |
reverseRelation: { |
|
|
156 |
key: 'project', |
|
|
157 |
includeInJSON: '_id' |
|
|
158 |
}, |
|
|
159 |
}, |
|
|
160 |
{ |
|
|
161 |
type: Backbone.HasMany, |
|
|
162 |
key: "edges", |
|
|
163 |
relatedModel: Edge, |
|
|
164 |
reverseRelation: { |
|
|
165 |
key: 'project', |
|
|
166 |
includeInJSON: '_id' |
|
|
167 |
}, |
|
|
168 |
} |
|
|
169 |
], |
|
|
170 |
addUser: function(_props) { |
|
|
171 |
_props.project = this; |
|
|
172 |
var _user = new User(_props); |
|
|
173 |
this.get("users").push(_user); |
|
|
174 |
return _user; |
|
|
175 |
}, |
|
|
176 |
addNode: function(_props) { |
|
|
177 |
_props.project = this; |
|
|
178 |
var _node = new Node(_props); |
|
|
179 |
this.get("nodes").push(_node); |
|
|
180 |
return _node; |
|
|
181 |
}, |
|
|
182 |
addEdge: function(_props) { |
|
|
183 |
_props.project = this; |
|
|
184 |
var _edge = new Edge(_props); |
|
|
185 |
this.get("edges").push(_edge); |
|
|
186 |
return _edge; |
|
|
187 |
}, |
|
|
188 |
removeNode: function(_model) { |
|
|
189 |
this.get("nodes").remove(_model); |
|
|
190 |
}, |
|
|
191 |
removeEdge: function(_model) { |
|
|
192 |
this.get("edges").remove(_model); |
|
|
193 |
}, |
|
24
|
194 |
validate: function(options) { |
|
|
195 |
var _project = this; |
|
|
196 |
_(options.users).each(function(_item) { |
|
|
197 |
_item.project = _project; |
|
|
198 |
}); |
|
|
199 |
_(options.nodes).each(function(_item) { |
|
|
200 |
_item.project = _project; |
|
|
201 |
}); |
|
|
202 |
_(options.edges).each(function(_item) { |
|
|
203 |
_item.project = _project; |
|
|
204 |
}); |
|
|
205 |
}, |
|
23
|
206 |
// Add event handler to remove edges when a node is removed |
|
|
207 |
initialize: function() { |
|
|
208 |
var _this = this; |
|
|
209 |
this.on("remove:nodes", function(_node) { |
|
|
210 |
_this.get("edges").remove( |
|
|
211 |
_this.get("edges").filter(function(_edge) { |
|
|
212 |
return _edge.get("from") == _node || _edge.get("to") == _node; |
|
|
213 |
}) |
|
|
214 |
); |
|
|
215 |
}); |
|
|
216 |
} |
|
|
217 |
}); |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
}).call(window); |
|
|
221 |
|