| author | ymh <ymh.work@gmail.com> |
| Wed, 04 Jun 2014 12:36:17 +0200 | |
| changeset 299 | c5086f714631 |
| parent 298 | 2f35c2ae7de8 |
| child 332 | 1a4b145c0607 |
| permissions | -rw-r--r-- |
| 23 | 1 |
(function() { |
| 195 | 2 |
"use strict"; |
| 23 | 3 |
var root = this; |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
4 |
|
| 23 | 5 |
var Backbone = root.Backbone; |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
6 |
|
| 23 | 7 |
var Models = root.Rkns.Models = {}; |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
8 |
|
|
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
9 |
|
| 23 | 10 |
Models.getUID = function(obj) { |
11 |
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
12 |
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8); |
| 23 | 13 |
return v.toString(16); |
14 |
}); |
|
| 298 | 15 |
if(typeof obj !== 'undefined') { |
16 |
return obj.type + "-" + guid; |
|
17 |
} |
|
18 |
else { |
|
19 |
return guid; |
|
20 |
} |
|
| 23 | 21 |
}; |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
22 |
|
|
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
23 |
|
| 23 | 24 |
var RenkanModel = Backbone.RelationalModel.extend({ |
25 |
idAttribute : "_id", |
|
26 |
constructor: function(options) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
27 |
|
| 23 | 28 |
if (typeof options !== "undefined") { |
| 27 | 29 |
options._id = options._id || options.id || Models.getUID(this); |
| 68 | 30 |
options.title = options.title || ""; |
| 23 | 31 |
options.description = options.description || ""; |
32 |
options.uri = options.uri || ""; |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
33 |
|
| 23 | 34 |
if(typeof this.prepare === "function") { |
35 |
options = this.prepare(options); |
|
36 |
} |
|
37 |
} |
|
38 |
Backbone.RelationalModel.prototype.constructor.call(this, options); |
|
39 |
}, |
|
40 |
validate: function() { |
|
41 |
if(!this.type) { |
|
42 |
return "object has no type"; |
|
43 |
} |
|
44 |
}, |
|
45 |
addReference : function(_options, _propName, _list, _id, _default) { |
|
46 |
var _element = _list.get(_id); |
|
47 |
if (typeof _element === "undefined" && typeof _default !== "undefined") { |
|
48 |
_options[_propName ] = _default; |
|
49 |
} |
|
50 |
else { |
|
51 |
_options[_propName ] = _element; |
|
52 |
} |
|
53 |
} |
|
54 |
}); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
55 |
|
| 23 | 56 |
// USER |
57 |
var User = Models.User = RenkanModel.extend({ |
|
58 |
type: "user", |
|
59 |
prepare: function(options) { |
|
60 |
options.color = options.color || "#666666"; |
|
61 |
return options; |
|
| 24 | 62 |
}, |
63 |
toJSON: function() { |
|
64 |
return { |
|
| 27 | 65 |
_id: this.get("_id"), |
| 24 | 66 |
title: this.get("title"), |
67 |
uri: this.get("uri"), |
|
68 |
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
|
69 |
color: this.get("color") |
| 27 | 70 |
}; |
|
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
|
71 |
} |
| 23 | 72 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
73 |
|
| 23 | 74 |
// NODE |
75 |
var Node = Models.Node = RenkanModel.extend({ |
|
76 |
type: "node", |
|
77 |
relations: [{ |
|
78 |
type: Backbone.HasOne, |
|
79 |
key: "created_by", |
|
80 |
relatedModel: User |
|
81 |
}], |
|
82 |
prepare: function(options) { |
|
| 195 | 83 |
var project = options.project; |
| 23 | 84 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
85 |
options.description = options.description || ""; |
|
86 |
return options; |
|
| 24 | 87 |
}, |
88 |
toJSON: function() { |
|
89 |
return { |
|
| 27 | 90 |
_id: this.get("_id"), |
| 24 | 91 |
title: this.get("title"), |
92 |
uri: this.get("uri"), |
|
93 |
description: this.get("description"), |
|
94 |
position: this.get("position"), |
|
| 37 | 95 |
image: this.get("image"), |
| 52 | 96 |
color: this.get("color"), |
| 67 | 97 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null, |
| 175 | 98 |
size: this.get("size"), |
| 271 | 99 |
clip_path: this.get("clip_path") |
| 27 | 100 |
}; |
|
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
|
101 |
} |
| 23 | 102 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
103 |
|
| 23 | 104 |
// EDGE |
105 |
var Edge = Models.Edge = RenkanModel.extend({ |
|
106 |
type: "edge", |
|
107 |
relations: [ |
|
108 |
{ |
|
109 |
type: Backbone.HasOne, |
|
110 |
key: "created_by", |
|
111 |
relatedModel: User |
|
112 |
}, |
|
113 |
{ |
|
114 |
type: Backbone.HasOne, |
|
115 |
key: "from", |
|
116 |
relatedModel: Node |
|
117 |
}, |
|
118 |
{ |
|
119 |
type: Backbone.HasOne, |
|
120 |
key: "to", |
|
121 |
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
|
122 |
} |
| 23 | 123 |
], |
124 |
prepare: function(options) { |
|
| 195 | 125 |
var project = options.project; |
| 23 | 126 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
127 |
this.addReference(options, "from", project.get("nodes"), options.from); |
|
128 |
this.addReference(options, "to", project.get("nodes"), options.to); |
|
129 |
return options; |
|
| 24 | 130 |
}, |
131 |
toJSON: function() { |
|
132 |
return { |
|
| 27 | 133 |
_id: this.get("_id"), |
| 24 | 134 |
title: this.get("title"), |
135 |
uri: this.get("uri"), |
|
136 |
description: this.get("description"), |
|
| 45 | 137 |
from: this.get("from") ? this.get("from").get("_id") : null, |
138 |
to: this.get("to") ? this.get("to").get("_id") : null, |
|
| 52 | 139 |
color: this.get("color"), |
| 45 | 140 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
| 27 | 141 |
}; |
|
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
|
142 |
} |
| 23 | 143 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
144 |
|
| 281 | 145 |
// View |
146 |
var View = Models.View = RenkanModel.extend({ |
|
147 |
type: "view", |
|
148 |
relations: [ |
|
149 |
{ |
|
150 |
type: Backbone.HasOne, |
|
151 |
key: "created_by", |
|
152 |
relatedModel: User |
|
153 |
} |
|
154 |
], |
|
| 299 | 155 |
prepare: function(options) { |
156 |
var project = options.project; |
|
157 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
158 |
options.description = options.description || ""; |
|
159 |
if(typeof options.offset !== "undefined") { |
|
160 |
var offset = {}; |
|
161 |
if (Array.isArray(options.offset)) { |
|
162 |
offset.x = options.offset[0]; |
|
163 |
offset.y = options.offset.length > 1 ? options.offset[1] : options.offset[0]; |
|
164 |
} |
|
165 |
else if (options.offset.x != null) { |
|
166 |
offset.x = options.offset.x; |
|
167 |
offset.y = options.offset.y; |
|
168 |
} |
|
169 |
options.offset = offset; |
|
170 |
} |
|
171 |
return options; |
|
172 |
}, |
|
| 282 | 173 |
toJSON: function() { |
174 |
return { |
|
175 |
_id: this.get("_id"), |
|
176 |
zoom_level: this.get("zoom_level"), |
|
| 299 | 177 |
offset: this.get("offset"), |
| 282 | 178 |
title: this.get("title"), |
179 |
description: this.get("description"), |
|
180 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
|
181 |
// Don't need project id |
|
182 |
}; |
|
183 |
} |
|
| 281 | 184 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
185 |
|
| 23 | 186 |
// PROJECT |
187 |
var Project = Models.Project = RenkanModel.extend({ |
|
188 |
type: "project", |
|
189 |
relations: [ |
|
190 |
{ |
|
191 |
type: Backbone.HasMany, |
|
192 |
key: "users", |
|
193 |
relatedModel: User, |
|
194 |
reverseRelation: { |
|
195 |
key: 'project', |
|
196 |
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
|
197 |
} |
| 23 | 198 |
}, |
199 |
{ |
|
200 |
type: Backbone.HasMany, |
|
201 |
key: "nodes", |
|
202 |
relatedModel: Node, |
|
203 |
reverseRelation: { |
|
204 |
key: 'project', |
|
205 |
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
|
206 |
} |
| 23 | 207 |
}, |
208 |
{ |
|
209 |
type: Backbone.HasMany, |
|
210 |
key: "edges", |
|
211 |
relatedModel: Edge, |
|
212 |
reverseRelation: { |
|
213 |
key: 'project', |
|
214 |
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
|
215 |
} |
| 281 | 216 |
}, |
217 |
{ |
|
218 |
type: Backbone.HasMany, |
|
219 |
key: "views", |
|
220 |
relatedModel: View, |
|
221 |
reverseRelation: { |
|
222 |
key: 'project', |
|
223 |
includeInJSON: '_id' |
|
224 |
} |
|
| 23 | 225 |
} |
226 |
], |
|
| 56 | 227 |
addUser: function(_props, _options) { |
| 23 | 228 |
_props.project = this; |
| 56 | 229 |
var _user = User.findOrCreate(_props); |
230 |
this.get("users").push(_user, _options); |
|
| 23 | 231 |
return _user; |
232 |
}, |
|
| 56 | 233 |
addNode: function(_props, _options) { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
234 |
_props.project = this; |
| 56 | 235 |
var _node = Node.findOrCreate(_props); |
236 |
this.get("nodes").push(_node, _options); |
|
| 23 | 237 |
return _node; |
238 |
}, |
|
| 56 | 239 |
addEdge: function(_props, _options) { |
| 23 | 240 |
_props.project = this; |
| 56 | 241 |
var _edge = Edge.findOrCreate(_props); |
242 |
this.get("edges").push(_edge, _options); |
|
| 23 | 243 |
return _edge; |
244 |
}, |
|
| 281 | 245 |
addView: function(_props, _options) { |
246 |
_props.project = this; |
|
247 |
// TODO: check if need to replace with create only |
|
248 |
var _view = View.findOrCreate(_props); |
|
249 |
// TODO: Should we remember only one view? |
|
250 |
this.get("views").push(_view, _options); |
|
251 |
return _view; |
|
252 |
}, |
|
| 23 | 253 |
removeNode: function(_model) { |
254 |
this.get("nodes").remove(_model); |
|
255 |
}, |
|
256 |
removeEdge: function(_model) { |
|
257 |
this.get("edges").remove(_model); |
|
258 |
}, |
|
| 24 | 259 |
validate: function(options) { |
260 |
var _project = this; |
|
| 299 | 261 |
_([].concat(options.users, options.nodes, options.edges, options.views)).each(function(_item) { |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
262 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
263 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
264 |
} |
| 24 | 265 |
}); |
266 |
}, |
|
| 23 | 267 |
// Add event handler to remove edges when a node is removed |
268 |
initialize: function() { |
|
269 |
var _this = this; |
|
270 |
this.on("remove:nodes", function(_node) { |
|
271 |
_this.get("edges").remove( |
|
272 |
_this.get("edges").filter(function(_edge) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
273 |
return _edge.get("from") === _node || _edge.get("to") === _node; |
| 23 | 274 |
}) |
275 |
); |
|
276 |
}); |
|
277 |
} |
|
278 |
}); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
279 |
|
| 56 | 280 |
var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
281 |
type: "roster_user", |
| 56 | 282 |
idAttribute : "_id", |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
283 |
|
| 56 | 284 |
constructor: function(options) { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
285 |
|
| 56 | 286 |
if (typeof options !== "undefined") { |
287 |
options._id = options._id || options.id || Models.getUID(this); |
|
288 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
289 |
options.description = options.description || ""; |
|
290 |
options.uri = options.uri || ""; |
|
291 |
options.project = options.project || null; |
|
292 |
options.site_id = options.site_id || 0; |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
293 |
|
| 56 | 294 |
if(typeof this.prepare === "function") { |
295 |
options = this.prepare(options); |
|
296 |
} |
|
297 |
} |
|
298 |
Backbone.Model.prototype.constructor.call(this, options); |
|
299 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
300 |
|
| 56 | 301 |
validate: function() { |
302 |
if(!this.type) { |
|
303 |
return "object has no type"; |
|
304 |
} |
|
305 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
306 |
|
| 56 | 307 |
prepare: function(options) { |
308 |
options.color = options.color || "#666666"; |
|
309 |
return options; |
|
310 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
311 |
|
| 56 | 312 |
toJSON: function() { |
313 |
return { |
|
314 |
_id: this.get("_id"), |
|
315 |
title: this.get("title"), |
|
316 |
uri: this.get("uri"), |
|
317 |
description: this.get("description"), |
|
318 |
color: this.get("color"), |
|
319 |
project: (this.get("project") != null)?this.get("project").get("id"):null, |
|
320 |
site_id: this.get("site_id") |
|
321 |
}; |
|
|
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
|
322 |
} |
| 56 | 323 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
324 |
|
| 56 | 325 |
var UsersList = Models.UsersList = Backbone.Collection.extend({ |
| 160 | 326 |
model: RosterUser |
| 56 | 327 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
328 |
|
| 23 | 329 |
|
330 |
}).call(window); |