| author | cavaliet |
| Mon, 28 Apr 2014 16:11:21 +0200 | |
| changeset 281 | 9ff388c9bc8d |
| parent 271 | b2cc6f238b0d |
| child 282 | 12ee99b182cd |
| permissions | -rw-r--r-- |
| 23 | 1 |
(function() { |
| 195 | 2 |
"use strict"; |
| 23 | 3 |
var root = this; |
| 281 | 4 |
|
| 23 | 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); |
| 68 | 25 |
options.title = options.title || ""; |
| 23 | 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"), |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
64 |
color: this.get("color") |
| 27 | 65 |
}; |
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
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) { |
|
| 195 | 78 |
var project = options.project; |
| 23 | 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"), |
| 67 | 92 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null, |
| 175 | 93 |
size: this.get("size"), |
| 271 | 94 |
clip_path: this.get("clip_path") |
| 27 | 95 |
}; |
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
96 |
} |
| 23 | 97 |
}); |
98 |
|
|
99 |
// EDGE |
|
100 |
var Edge = Models.Edge = RenkanModel.extend({ |
|
101 |
type: "edge", |
|
102 |
relations: [ |
|
103 |
{ |
|
104 |
type: Backbone.HasOne, |
|
105 |
key: "created_by", |
|
106 |
relatedModel: User |
|
107 |
}, |
|
108 |
{ |
|
109 |
type: Backbone.HasOne, |
|
110 |
key: "from", |
|
111 |
relatedModel: Node |
|
112 |
}, |
|
113 |
{ |
|
114 |
type: Backbone.HasOne, |
|
115 |
key: "to", |
|
116 |
relatedModel: Node |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
117 |
} |
| 23 | 118 |
], |
119 |
prepare: function(options) { |
|
| 195 | 120 |
var project = options.project; |
| 23 | 121 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
122 |
this.addReference(options, "from", project.get("nodes"), options.from); |
|
123 |
this.addReference(options, "to", project.get("nodes"), options.to); |
|
124 |
return options; |
|
| 24 | 125 |
}, |
126 |
toJSON: function() { |
|
127 |
return { |
|
| 27 | 128 |
_id: this.get("_id"), |
| 24 | 129 |
title: this.get("title"), |
130 |
uri: this.get("uri"), |
|
131 |
description: this.get("description"), |
|
| 45 | 132 |
from: this.get("from") ? this.get("from").get("_id") : null, |
133 |
to: this.get("to") ? this.get("to").get("_id") : null, |
|
| 52 | 134 |
color: this.get("color"), |
| 45 | 135 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
| 27 | 136 |
}; |
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
137 |
} |
| 23 | 138 |
}); |
| 281 | 139 |
|
140 |
// View |
|
141 |
var View = Models.View = RenkanModel.extend({ |
|
142 |
type: "view", |
|
143 |
relations: [ |
|
144 |
{ |
|
145 |
type: Backbone.HasOne, |
|
146 |
key: "created_by", |
|
147 |
relatedModel: User |
|
148 |
} |
|
149 |
], |
|
150 |
}); |
|
| 23 | 151 |
|
152 |
// PROJECT |
|
153 |
var Project = Models.Project = RenkanModel.extend({ |
|
154 |
type: "project", |
|
155 |
relations: [ |
|
156 |
{ |
|
157 |
type: Backbone.HasMany, |
|
158 |
key: "users", |
|
159 |
relatedModel: User, |
|
160 |
reverseRelation: { |
|
161 |
key: 'project', |
|
162 |
includeInJSON: '_id' |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
163 |
} |
| 23 | 164 |
}, |
165 |
{ |
|
166 |
type: Backbone.HasMany, |
|
167 |
key: "nodes", |
|
168 |
relatedModel: Node, |
|
169 |
reverseRelation: { |
|
170 |
key: 'project', |
|
171 |
includeInJSON: '_id' |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
172 |
} |
| 23 | 173 |
}, |
174 |
{ |
|
175 |
type: Backbone.HasMany, |
|
176 |
key: "edges", |
|
177 |
relatedModel: Edge, |
|
178 |
reverseRelation: { |
|
179 |
key: 'project', |
|
180 |
includeInJSON: '_id' |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
181 |
} |
| 281 | 182 |
}, |
183 |
{ |
|
184 |
type: Backbone.HasMany, |
|
185 |
key: "views", |
|
186 |
relatedModel: View, |
|
187 |
reverseRelation: { |
|
188 |
key: 'project', |
|
189 |
includeInJSON: '_id' |
|
190 |
} |
|
| 23 | 191 |
} |
192 |
], |
|
| 56 | 193 |
addUser: function(_props, _options) { |
| 23 | 194 |
_props.project = this; |
| 56 | 195 |
var _user = User.findOrCreate(_props); |
196 |
this.get("users").push(_user, _options); |
|
| 23 | 197 |
return _user; |
198 |
}, |
|
| 56 | 199 |
addNode: function(_props, _options) { |
200 |
_props.project = this; |
|
201 |
var _node = Node.findOrCreate(_props); |
|
202 |
this.get("nodes").push(_node, _options); |
|
| 23 | 203 |
return _node; |
204 |
}, |
|
| 56 | 205 |
addEdge: function(_props, _options) { |
| 23 | 206 |
_props.project = this; |
| 56 | 207 |
var _edge = Edge.findOrCreate(_props); |
208 |
this.get("edges").push(_edge, _options); |
|
| 23 | 209 |
return _edge; |
210 |
}, |
|
| 281 | 211 |
addView: function(_props, _options) { |
212 |
_props.project = this; |
|
213 |
// TODO: check if need to replace with create only |
|
214 |
var _view = View.findOrCreate(_props); |
|
215 |
// TODO: Should we remember only one view? |
|
216 |
this.get("views").push(_view, _options); |
|
217 |
return _view; |
|
218 |
}, |
|
| 23 | 219 |
removeNode: function(_model) { |
220 |
this.get("nodes").remove(_model); |
|
221 |
}, |
|
222 |
removeEdge: function(_model) { |
|
223 |
this.get("edges").remove(_model); |
|
224 |
}, |
|
| 24 | 225 |
validate: function(options) { |
226 |
var _project = this; |
|
227 |
_(options.users).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
228 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
229 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
230 |
} |
| 24 | 231 |
}); |
232 |
_(options.nodes).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
233 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
234 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
235 |
} |
| 24 | 236 |
}); |
237 |
_(options.edges).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
238 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
239 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
240 |
} |
| 24 | 241 |
}); |
242 |
}, |
|
| 23 | 243 |
// Add event handler to remove edges when a node is removed |
244 |
initialize: function() { |
|
245 |
var _this = this; |
|
246 |
this.on("remove:nodes", function(_node) { |
|
247 |
_this.get("edges").remove( |
|
248 |
_this.get("edges").filter(function(_edge) { |
|
249 |
return _edge.get("from") == _node || _edge.get("to") == _node; |
|
250 |
}) |
|
251 |
); |
|
252 |
}); |
|
253 |
} |
|
254 |
}); |
|
255 |
|
|
| 56 | 256 |
var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
257 |
type: "roster_user", |
| 56 | 258 |
idAttribute : "_id", |
259 |
|
|
260 |
constructor: function(options) { |
|
261 |
|
|
262 |
if (typeof options !== "undefined") { |
|
263 |
options._id = options._id || options.id || Models.getUID(this); |
|
264 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
265 |
options.description = options.description || ""; |
|
266 |
options.uri = options.uri || ""; |
|
267 |
options.project = options.project || null; |
|
268 |
options.site_id = options.site_id || 0; |
|
269 |
|
|
270 |
if(typeof this.prepare === "function") { |
|
271 |
options = this.prepare(options); |
|
272 |
} |
|
273 |
} |
|
274 |
Backbone.Model.prototype.constructor.call(this, options); |
|
275 |
}, |
|
276 |
|
|
277 |
validate: function() { |
|
278 |
if(!this.type) { |
|
279 |
return "object has no type"; |
|
280 |
} |
|
281 |
}, |
|
282 |
|
|
283 |
prepare: function(options) { |
|
284 |
options.color = options.color || "#666666"; |
|
285 |
return options; |
|
286 |
}, |
|
287 |
|
|
288 |
toJSON: function() { |
|
289 |
return { |
|
290 |
_id: this.get("_id"), |
|
291 |
title: this.get("title"), |
|
292 |
uri: this.get("uri"), |
|
293 |
description: this.get("description"), |
|
294 |
color: this.get("color"), |
|
295 |
project: (this.get("project") != null)?this.get("project").get("id"):null, |
|
296 |
site_id: this.get("site_id") |
|
297 |
}; |
|
|
211
d87f6bdee43d
upgrade libs + add no minified versions of libs + improve build + allow disabling zoom on scroll
ymh <ymh.work@gmail.com>
parents:
195
diff
changeset
|
298 |
} |
| 56 | 299 |
}); |
300 |
|
|
301 |
var UsersList = Models.UsersList = Backbone.Collection.extend({ |
|
| 160 | 302 |
model: RosterUser |
| 56 | 303 |
}); |
304 |
|
|
| 23 | 305 |
|
306 |
}).call(window); |
|
307 |