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