| author | cavaliet |
| Mon, 28 Apr 2014 17:40:41 +0200 | |
| changeset 282 | 12ee99b182cd |
| parent 281 | 9ff388c9bc8d |
| child 293 | fba23fde14ba |
| 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 |
], |
|
| 282 | 150 |
toJSON: function() { |
151 |
return { |
|
152 |
_id: this.get("_id"), |
|
153 |
zoom_level: this.get("zoom_level"), |
|
154 |
offset_x: this.get("offset_x"), |
|
155 |
offset_y: this.get("offset_y"), |
|
156 |
title: this.get("title"), |
|
157 |
description: this.get("description"), |
|
158 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
|
159 |
// Don't need project id |
|
160 |
}; |
|
161 |
} |
|
| 281 | 162 |
}); |
| 23 | 163 |
|
164 |
// PROJECT |
|
165 |
var Project = Models.Project = RenkanModel.extend({ |
|
166 |
type: "project", |
|
167 |
relations: [ |
|
168 |
{ |
|
169 |
type: Backbone.HasMany, |
|
170 |
key: "users", |
|
171 |
relatedModel: User, |
|
172 |
reverseRelation: { |
|
173 |
key: 'project', |
|
174 |
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
|
175 |
} |
| 23 | 176 |
}, |
177 |
{ |
|
178 |
type: Backbone.HasMany, |
|
179 |
key: "nodes", |
|
180 |
relatedModel: Node, |
|
181 |
reverseRelation: { |
|
182 |
key: 'project', |
|
183 |
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
|
184 |
} |
| 23 | 185 |
}, |
186 |
{ |
|
187 |
type: Backbone.HasMany, |
|
188 |
key: "edges", |
|
189 |
relatedModel: Edge, |
|
190 |
reverseRelation: { |
|
191 |
key: 'project', |
|
192 |
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
|
193 |
} |
| 281 | 194 |
}, |
195 |
{ |
|
196 |
type: Backbone.HasMany, |
|
197 |
key: "views", |
|
198 |
relatedModel: View, |
|
199 |
reverseRelation: { |
|
200 |
key: 'project', |
|
201 |
includeInJSON: '_id' |
|
202 |
} |
|
| 23 | 203 |
} |
204 |
], |
|
| 56 | 205 |
addUser: function(_props, _options) { |
| 23 | 206 |
_props.project = this; |
| 56 | 207 |
var _user = User.findOrCreate(_props); |
208 |
this.get("users").push(_user, _options); |
|
| 23 | 209 |
return _user; |
210 |
}, |
|
| 56 | 211 |
addNode: function(_props, _options) { |
212 |
_props.project = this; |
|
213 |
var _node = Node.findOrCreate(_props); |
|
214 |
this.get("nodes").push(_node, _options); |
|
| 23 | 215 |
return _node; |
216 |
}, |
|
| 56 | 217 |
addEdge: function(_props, _options) { |
| 23 | 218 |
_props.project = this; |
| 56 | 219 |
var _edge = Edge.findOrCreate(_props); |
220 |
this.get("edges").push(_edge, _options); |
|
| 23 | 221 |
return _edge; |
222 |
}, |
|
| 281 | 223 |
addView: function(_props, _options) { |
224 |
_props.project = this; |
|
225 |
// TODO: check if need to replace with create only |
|
226 |
var _view = View.findOrCreate(_props); |
|
227 |
// TODO: Should we remember only one view? |
|
228 |
this.get("views").push(_view, _options); |
|
229 |
return _view; |
|
230 |
}, |
|
| 23 | 231 |
removeNode: function(_model) { |
232 |
this.get("nodes").remove(_model); |
|
233 |
}, |
|
234 |
removeEdge: function(_model) { |
|
235 |
this.get("edges").remove(_model); |
|
236 |
}, |
|
| 24 | 237 |
validate: function(options) { |
238 |
var _project = this; |
|
239 |
_(options.users).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
240 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
241 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
242 |
} |
| 24 | 243 |
}); |
244 |
_(options.nodes).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
245 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
246 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
247 |
} |
| 24 | 248 |
}); |
249 |
_(options.edges).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
250 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
251 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
252 |
} |
| 24 | 253 |
}); |
254 |
}, |
|
| 23 | 255 |
// Add event handler to remove edges when a node is removed |
256 |
initialize: function() { |
|
257 |
var _this = this; |
|
258 |
this.on("remove:nodes", function(_node) { |
|
259 |
_this.get("edges").remove( |
|
260 |
_this.get("edges").filter(function(_edge) { |
|
261 |
return _edge.get("from") == _node || _edge.get("to") == _node; |
|
262 |
}) |
|
263 |
); |
|
264 |
}); |
|
265 |
} |
|
266 |
}); |
|
267 |
|
|
| 56 | 268 |
var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
269 |
type: "roster_user", |
| 56 | 270 |
idAttribute : "_id", |
271 |
|
|
272 |
constructor: function(options) { |
|
273 |
|
|
274 |
if (typeof options !== "undefined") { |
|
275 |
options._id = options._id || options.id || Models.getUID(this); |
|
276 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
277 |
options.description = options.description || ""; |
|
278 |
options.uri = options.uri || ""; |
|
279 |
options.project = options.project || null; |
|
280 |
options.site_id = options.site_id || 0; |
|
281 |
|
|
282 |
if(typeof this.prepare === "function") { |
|
283 |
options = this.prepare(options); |
|
284 |
} |
|
285 |
} |
|
286 |
Backbone.Model.prototype.constructor.call(this, options); |
|
287 |
}, |
|
288 |
|
|
289 |
validate: function() { |
|
290 |
if(!this.type) { |
|
291 |
return "object has no type"; |
|
292 |
} |
|
293 |
}, |
|
294 |
|
|
295 |
prepare: function(options) { |
|
296 |
options.color = options.color || "#666666"; |
|
297 |
return options; |
|
298 |
}, |
|
299 |
|
|
300 |
toJSON: function() { |
|
301 |
return { |
|
302 |
_id: this.get("_id"), |
|
303 |
title: this.get("title"), |
|
304 |
uri: this.get("uri"), |
|
305 |
description: this.get("description"), |
|
306 |
color: this.get("color"), |
|
307 |
project: (this.get("project") != null)?this.get("project").get("id"):null, |
|
308 |
site_id: this.get("site_id") |
|
309 |
}; |
|
|
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
|
310 |
} |
| 56 | 311 |
}); |
312 |
|
|
313 |
var UsersList = Models.UsersList = Backbone.Collection.extend({ |
|
| 160 | 314 |
model: RosterUser |
| 56 | 315 |
}); |
316 |
|
|
| 23 | 317 |
|
318 |
}).call(window); |
|
319 |