| author | ymh <ymh.work@gmail.com> |
| Thu, 16 Jan 2014 01:28:38 +0100 | |
| changeset 242 | 570e18094e87 |
| parent 212 | ee7b5831d382 |
| child 271 | b2cc6f238b0d |
| permissions | -rw-r--r-- |
| 23 | 1 |
(function() { |
| 195 | 2 |
"use strict"; |
| 23 | 3 |
var root = this; |
4 |
|
|
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"), |
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 |
}); |
139 |
|
|
140 |
// PROJECT |
|
141 |
var Project = Models.Project = RenkanModel.extend({ |
|
142 |
type: "project", |
|
143 |
relations: [ |
|
144 |
{ |
|
145 |
type: Backbone.HasMany, |
|
146 |
key: "users", |
|
147 |
relatedModel: User, |
|
148 |
reverseRelation: { |
|
149 |
key: 'project', |
|
150 |
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
|
151 |
} |
| 23 | 152 |
}, |
153 |
{ |
|
154 |
type: Backbone.HasMany, |
|
155 |
key: "nodes", |
|
156 |
relatedModel: Node, |
|
157 |
reverseRelation: { |
|
158 |
key: 'project', |
|
159 |
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
|
160 |
} |
| 23 | 161 |
}, |
162 |
{ |
|
163 |
type: Backbone.HasMany, |
|
164 |
key: "edges", |
|
165 |
relatedModel: Edge, |
|
166 |
reverseRelation: { |
|
167 |
key: 'project', |
|
168 |
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
|
169 |
} |
| 23 | 170 |
} |
171 |
], |
|
| 56 | 172 |
addUser: function(_props, _options) { |
| 23 | 173 |
_props.project = this; |
| 56 | 174 |
var _user = User.findOrCreate(_props); |
175 |
this.get("users").push(_user, _options); |
|
| 23 | 176 |
return _user; |
177 |
}, |
|
| 56 | 178 |
addNode: function(_props, _options) { |
179 |
_props.project = this; |
|
180 |
var _node = Node.findOrCreate(_props); |
|
181 |
this.get("nodes").push(_node, _options); |
|
| 23 | 182 |
return _node; |
183 |
}, |
|
| 56 | 184 |
addEdge: function(_props, _options) { |
| 23 | 185 |
_props.project = this; |
| 56 | 186 |
var _edge = Edge.findOrCreate(_props); |
187 |
this.get("edges").push(_edge, _options); |
|
| 23 | 188 |
return _edge; |
189 |
}, |
|
190 |
removeNode: function(_model) { |
|
191 |
this.get("nodes").remove(_model); |
|
192 |
}, |
|
193 |
removeEdge: function(_model) { |
|
194 |
this.get("edges").remove(_model); |
|
195 |
}, |
|
| 24 | 196 |
validate: function(options) { |
197 |
var _project = this; |
|
198 |
_(options.users).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
199 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
200 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
201 |
} |
| 24 | 202 |
}); |
203 |
_(options.nodes).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
204 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
205 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
206 |
} |
| 24 | 207 |
}); |
208 |
_(options.edges).each(function(_item) { |
|
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
209 |
if(_item) { |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
210 |
_item.project = _project; |
|
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
211 |
} |
| 24 | 212 |
}); |
213 |
}, |
|
| 23 | 214 |
// Add event handler to remove edges when a node is removed |
215 |
initialize: function() { |
|
216 |
var _this = this; |
|
217 |
this.on("remove:nodes", function(_node) { |
|
218 |
_this.get("edges").remove( |
|
219 |
_this.get("edges").filter(function(_edge) { |
|
220 |
return _edge.get("from") == _node || _edge.get("to") == _node; |
|
221 |
}) |
|
222 |
); |
|
223 |
}); |
|
224 |
} |
|
225 |
}); |
|
226 |
|
|
| 56 | 227 |
var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
|
212
ee7b5831d382
correct model to accept null objects ?
ymh <ymh.work@gmail.com>
parents:
211
diff
changeset
|
228 |
type: "roster_user", |
| 56 | 229 |
idAttribute : "_id", |
230 |
|
|
231 |
constructor: function(options) { |
|
232 |
|
|
233 |
if (typeof options !== "undefined") { |
|
234 |
options._id = options._id || options.id || Models.getUID(this); |
|
235 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
236 |
options.description = options.description || ""; |
|
237 |
options.uri = options.uri || ""; |
|
238 |
options.project = options.project || null; |
|
239 |
options.site_id = options.site_id || 0; |
|
240 |
|
|
241 |
if(typeof this.prepare === "function") { |
|
242 |
options = this.prepare(options); |
|
243 |
} |
|
244 |
} |
|
245 |
Backbone.Model.prototype.constructor.call(this, options); |
|
246 |
}, |
|
247 |
|
|
248 |
validate: function() { |
|
249 |
if(!this.type) { |
|
250 |
return "object has no type"; |
|
251 |
} |
|
252 |
}, |
|
253 |
|
|
254 |
prepare: function(options) { |
|
255 |
options.color = options.color || "#666666"; |
|
256 |
return options; |
|
257 |
}, |
|
258 |
|
|
259 |
toJSON: function() { |
|
260 |
return { |
|
261 |
_id: this.get("_id"), |
|
262 |
title: this.get("title"), |
|
263 |
uri: this.get("uri"), |
|
264 |
description: this.get("description"), |
|
265 |
color: this.get("color"), |
|
266 |
project: (this.get("project") != null)?this.get("project").get("id"):null, |
|
267 |
site_id: this.get("site_id") |
|
268 |
}; |
|
|
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
|
269 |
} |
| 56 | 270 |
}); |
271 |
|
|
272 |
var UsersList = Models.UsersList = Backbone.Collection.extend({ |
|
| 160 | 273 |
model: RosterUser |
| 56 | 274 |
}); |
275 |
|
|
| 23 | 276 |
|
277 |
}).call(window); |
|
278 |