| author | ymh <ymh.work@gmail.com> |
| Fri, 19 Dec 2014 15:48:50 +0100 | |
| changeset 345 | cb5c0007c9f9 |
| parent 332 | 1a4b145c0607 |
| child 362 | 8f33cbeb4f32 |
| 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"), |
| 332 | 99 |
clip_path: this.get("clip_path"), |
100 |
shape: this.get("shape") |
|
| 27 | 101 |
}; |
|
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
|
102 |
} |
| 23 | 103 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
104 |
|
| 23 | 105 |
// EDGE |
106 |
var Edge = Models.Edge = RenkanModel.extend({ |
|
107 |
type: "edge", |
|
108 |
relations: [ |
|
109 |
{ |
|
110 |
type: Backbone.HasOne, |
|
111 |
key: "created_by", |
|
112 |
relatedModel: User |
|
113 |
}, |
|
114 |
{ |
|
115 |
type: Backbone.HasOne, |
|
116 |
key: "from", |
|
117 |
relatedModel: Node |
|
118 |
}, |
|
119 |
{ |
|
120 |
type: Backbone.HasOne, |
|
121 |
key: "to", |
|
122 |
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
|
123 |
} |
| 23 | 124 |
], |
125 |
prepare: function(options) { |
|
| 195 | 126 |
var project = options.project; |
| 23 | 127 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
128 |
this.addReference(options, "from", project.get("nodes"), options.from); |
|
129 |
this.addReference(options, "to", project.get("nodes"), options.to); |
|
130 |
return options; |
|
| 24 | 131 |
}, |
132 |
toJSON: function() { |
|
133 |
return { |
|
| 27 | 134 |
_id: this.get("_id"), |
| 24 | 135 |
title: this.get("title"), |
136 |
uri: this.get("uri"), |
|
137 |
description: this.get("description"), |
|
| 45 | 138 |
from: this.get("from") ? this.get("from").get("_id") : null, |
139 |
to: this.get("to") ? this.get("to").get("_id") : null, |
|
| 52 | 140 |
color: this.get("color"), |
| 45 | 141 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
| 27 | 142 |
}; |
|
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
|
143 |
} |
| 23 | 144 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
145 |
|
| 281 | 146 |
// View |
147 |
var View = Models.View = RenkanModel.extend({ |
|
148 |
type: "view", |
|
149 |
relations: [ |
|
150 |
{ |
|
151 |
type: Backbone.HasOne, |
|
152 |
key: "created_by", |
|
153 |
relatedModel: User |
|
154 |
} |
|
155 |
], |
|
| 299 | 156 |
prepare: function(options) { |
157 |
var project = options.project; |
|
158 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
159 |
options.description = options.description || ""; |
|
160 |
if(typeof options.offset !== "undefined") { |
|
161 |
var offset = {}; |
|
162 |
if (Array.isArray(options.offset)) { |
|
163 |
offset.x = options.offset[0]; |
|
164 |
offset.y = options.offset.length > 1 ? options.offset[1] : options.offset[0]; |
|
165 |
} |
|
166 |
else if (options.offset.x != null) { |
|
167 |
offset.x = options.offset.x; |
|
168 |
offset.y = options.offset.y; |
|
169 |
} |
|
170 |
options.offset = offset; |
|
171 |
} |
|
172 |
return options; |
|
173 |
}, |
|
| 282 | 174 |
toJSON: function() { |
175 |
return { |
|
176 |
_id: this.get("_id"), |
|
177 |
zoom_level: this.get("zoom_level"), |
|
| 299 | 178 |
offset: this.get("offset"), |
| 282 | 179 |
title: this.get("title"), |
180 |
description: this.get("description"), |
|
181 |
created_by: this.get("created_by") ? this.get("created_by").get("_id") : null |
|
182 |
// Don't need project id |
|
183 |
}; |
|
184 |
} |
|
| 281 | 185 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
186 |
|
| 23 | 187 |
// PROJECT |
188 |
var Project = Models.Project = RenkanModel.extend({ |
|
189 |
type: "project", |
|
190 |
relations: [ |
|
191 |
{ |
|
192 |
type: Backbone.HasMany, |
|
193 |
key: "users", |
|
194 |
relatedModel: User, |
|
195 |
reverseRelation: { |
|
196 |
key: 'project', |
|
197 |
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
|
198 |
} |
| 23 | 199 |
}, |
200 |
{ |
|
201 |
type: Backbone.HasMany, |
|
202 |
key: "nodes", |
|
203 |
relatedModel: Node, |
|
204 |
reverseRelation: { |
|
205 |
key: 'project', |
|
206 |
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
|
207 |
} |
| 23 | 208 |
}, |
209 |
{ |
|
210 |
type: Backbone.HasMany, |
|
211 |
key: "edges", |
|
212 |
relatedModel: Edge, |
|
213 |
reverseRelation: { |
|
214 |
key: 'project', |
|
215 |
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
|
216 |
} |
| 281 | 217 |
}, |
218 |
{ |
|
219 |
type: Backbone.HasMany, |
|
220 |
key: "views", |
|
221 |
relatedModel: View, |
|
222 |
reverseRelation: { |
|
223 |
key: 'project', |
|
224 |
includeInJSON: '_id' |
|
225 |
} |
|
| 23 | 226 |
} |
227 |
], |
|
| 56 | 228 |
addUser: function(_props, _options) { |
| 23 | 229 |
_props.project = this; |
| 56 | 230 |
var _user = User.findOrCreate(_props); |
231 |
this.get("users").push(_user, _options); |
|
| 23 | 232 |
return _user; |
233 |
}, |
|
| 56 | 234 |
addNode: function(_props, _options) { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
235 |
_props.project = this; |
| 56 | 236 |
var _node = Node.findOrCreate(_props); |
237 |
this.get("nodes").push(_node, _options); |
|
| 23 | 238 |
return _node; |
239 |
}, |
|
| 56 | 240 |
addEdge: function(_props, _options) { |
| 23 | 241 |
_props.project = this; |
| 56 | 242 |
var _edge = Edge.findOrCreate(_props); |
243 |
this.get("edges").push(_edge, _options); |
|
| 23 | 244 |
return _edge; |
245 |
}, |
|
| 281 | 246 |
addView: function(_props, _options) { |
247 |
_props.project = this; |
|
248 |
// TODO: check if need to replace with create only |
|
249 |
var _view = View.findOrCreate(_props); |
|
250 |
// TODO: Should we remember only one view? |
|
251 |
this.get("views").push(_view, _options); |
|
252 |
return _view; |
|
253 |
}, |
|
| 23 | 254 |
removeNode: function(_model) { |
255 |
this.get("nodes").remove(_model); |
|
256 |
}, |
|
257 |
removeEdge: function(_model) { |
|
258 |
this.get("edges").remove(_model); |
|
259 |
}, |
|
| 24 | 260 |
validate: function(options) { |
261 |
var _project = this; |
|
| 299 | 262 |
_([].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
|
263 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
264 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
265 |
} |
| 24 | 266 |
}); |
267 |
}, |
|
| 23 | 268 |
// Add event handler to remove edges when a node is removed |
269 |
initialize: function() { |
|
270 |
var _this = this; |
|
271 |
this.on("remove:nodes", function(_node) { |
|
272 |
_this.get("edges").remove( |
|
273 |
_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
|
274 |
return _edge.get("from") === _node || _edge.get("to") === _node; |
| 23 | 275 |
}) |
276 |
); |
|
277 |
}); |
|
278 |
} |
|
279 |
}); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
280 |
|
| 56 | 281 |
var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
282 |
type: "roster_user", |
| 56 | 283 |
idAttribute : "_id", |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
284 |
|
| 56 | 285 |
constructor: function(options) { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
286 |
|
| 56 | 287 |
if (typeof options !== "undefined") { |
288 |
options._id = options._id || options.id || Models.getUID(this); |
|
289 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
290 |
options.description = options.description || ""; |
|
291 |
options.uri = options.uri || ""; |
|
292 |
options.project = options.project || null; |
|
293 |
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
|
294 |
|
| 56 | 295 |
if(typeof this.prepare === "function") { |
296 |
options = this.prepare(options); |
|
297 |
} |
|
298 |
} |
|
299 |
Backbone.Model.prototype.constructor.call(this, options); |
|
300 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
301 |
|
| 56 | 302 |
validate: function() { |
303 |
if(!this.type) { |
|
304 |
return "object has no type"; |
|
305 |
} |
|
306 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
307 |
|
| 56 | 308 |
prepare: function(options) { |
309 |
options.color = options.color || "#666666"; |
|
310 |
return options; |
|
311 |
}, |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
312 |
|
| 56 | 313 |
toJSON: function() { |
314 |
return { |
|
315 |
_id: this.get("_id"), |
|
316 |
title: this.get("title"), |
|
317 |
uri: this.get("uri"), |
|
318 |
description: this.get("description"), |
|
319 |
color: this.get("color"), |
|
320 |
project: (this.get("project") != null)?this.get("project").get("id"):null, |
|
321 |
site_id: this.get("site_id") |
|
322 |
}; |
|
|
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
|
323 |
} |
| 56 | 324 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
325 |
|
| 56 | 326 |
var UsersList = Models.UsersList = Backbone.Collection.extend({ |
| 160 | 327 |
model: RosterUser |
| 56 | 328 |
}); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
282
diff
changeset
|
329 |
|
| 23 | 330 |
|
331 |
}).call(window); |