|
938
|
1 |
/* |
|
|
2 |
* Copyright 2012 Institut de recherche et d'innovation |
|
|
3 |
* contributor(s) : Yves-Marie Haussonne, Raphael Velt, Samuel Huron |
|
|
4 |
* |
|
|
5 |
* contact@iri.centrepompidou.fr |
|
|
6 |
* http://www.iri.centrepompidou.fr |
|
|
7 |
* |
|
|
8 |
* This software is a computer program whose purpose is to show and add annotations on a video . |
|
|
9 |
* This software is governed by the CeCILL-C license under French law and |
|
|
10 |
* abiding by the rules of distribution of free software. You can use, |
|
|
11 |
* modify and/ or redistribute the software under the terms of the CeCILL-C |
|
|
12 |
* license as circulated by CEA, CNRS and INRIA at the following URL |
|
|
13 |
* "http://www.cecill.info". |
|
|
14 |
* |
|
|
15 |
* The fact that you are presently reading this means that you have had |
|
|
16 |
* knowledge of the CeCILL-C license and that you accept its terms. |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
if (typeof Rkns !== "object") { |
|
|
20 |
Rkns = {} |
|
|
21 |
} |
|
|
22 |
|
|
|
23 |
Rkns.$ = jQuery; |
|
|
24 |
|
|
|
25 |
Rkns._ = _; |
|
|
26 |
|
|
|
27 |
Rkns.i18n = { |
|
|
28 |
en: { |
|
|
29 |
zoom_in: "Zoom In", |
|
955
|
30 |
zoom_out: "Zoom Out", |
|
|
31 |
see_in_project: 'See also <b>"{node}"</b> in <b>"{project}"</b>' |
|
938
|
32 |
} |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
Rkns.Utils = { |
|
|
36 |
inherit : function(_baseClass) { |
|
|
37 |
var _class = function() { |
|
|
38 |
_baseClass.apply(this, Array.prototype.slice.call(arguments, 0)); |
|
|
39 |
if (typeof this._init == "function") { |
|
|
40 |
this._init.apply(this, Array.prototype.slice.call(arguments, 0)); |
|
|
41 |
} |
|
|
42 |
} |
|
|
43 |
_class.prototype = new _baseClass(); |
|
|
44 |
return _class; |
|
|
45 |
} |
|
|
46 |
} |
|
|
47 |
|
|
|
48 |
Rkns.Models = {}; |
|
|
49 |
|
|
|
50 |
Rkns.Models.getUID = function(obj) { |
|
|
51 |
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
|
|
52 |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); |
|
|
53 |
return v.toString(16); |
|
|
54 |
}); |
|
|
55 |
return obj.type + "-" + guid; |
|
|
56 |
}; |
|
|
57 |
|
|
|
58 |
Rkns.Models.RenkanModel = Backbone.RelationalModel.extend({ |
|
|
59 |
idAttribute : "_id", |
|
|
60 |
constructor: function(options) { |
|
955
|
61 |
|
|
938
|
62 |
if (typeof options !== "undefined") { |
|
|
63 |
options._id = options._id || options.id || Rkns.Models.getUID(this); |
|
|
64 |
options.title = options.title || "(untitled " + this.type + ")"; |
|
|
65 |
options.description = options.description || ""; |
|
|
66 |
options.uri = options.uri || ""; |
|
955
|
67 |
|
|
938
|
68 |
if(typeof this.prepare === "function") { |
|
|
69 |
options = this.prepare(options); |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
Backbone.RelationalModel.prototype.constructor.call(this, options); |
|
|
73 |
}, |
|
|
74 |
validate: function() { |
|
|
75 |
if(!this.type) { |
|
|
76 |
return "object has no type"; |
|
|
77 |
} |
|
|
78 |
}, |
|
|
79 |
addReference : function(_options, _propName, _list, _id, _default) { |
|
|
80 |
var _element = _list.get(_id); |
|
|
81 |
if (typeof _element === "undefined" && typeof _default !== "undefined") { |
|
|
82 |
_options[_propName ] = _default; |
|
|
83 |
} |
|
|
84 |
else { |
|
|
85 |
_options[_propName ] = _element; |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
}); |
|
|
89 |
|
|
|
90 |
// USER |
|
|
91 |
Rkns.Models.User = Rkns.Models.RenkanModel.extend({ |
|
|
92 |
type: "user", |
|
|
93 |
prepare: function(options) { |
|
|
94 |
options.color = options.color || "#666666"; |
|
|
95 |
return options; |
|
|
96 |
}, |
|
|
97 |
toJSON: function() { |
|
|
98 |
return { |
|
|
99 |
id: this.get("_id"), |
|
|
100 |
title: this.get("title"), |
|
|
101 |
uri: this.get("uri"), |
|
|
102 |
description: this.get("description"), |
|
|
103 |
color: this.get("color"), |
|
|
104 |
} |
|
|
105 |
}, |
|
|
106 |
}); |
|
|
107 |
|
|
|
108 |
// NODE |
|
|
109 |
Rkns.Models.Node = Rkns.Models.RenkanModel.extend({ |
|
|
110 |
type: "node", |
|
|
111 |
relations: [{ |
|
|
112 |
type: Backbone.HasOne, |
|
|
113 |
key: "created_by", |
|
|
114 |
relatedModel: Rkns.Models.User |
|
|
115 |
}], |
|
|
116 |
prepare: function(options) { |
|
|
117 |
project = options.project; |
|
|
118 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
|
119 |
options.description = options.description || ""; |
|
|
120 |
return options; |
|
|
121 |
}, |
|
|
122 |
toJSON: function() { |
|
|
123 |
return { |
|
|
124 |
id: this.get("_id"), |
|
|
125 |
title: this.get("title"), |
|
|
126 |
uri: this.get("uri"), |
|
|
127 |
description: this.get("description"), |
|
|
128 |
position: this.get("position"), |
|
955
|
129 |
image: this.get("image"), |
|
938
|
130 |
created_by: this.get("created_by").get("_id") |
|
|
131 |
} |
|
|
132 |
}, |
|
|
133 |
}); |
|
|
134 |
|
|
|
135 |
// EDGE |
|
|
136 |
Rkns.Models.Edge = Rkns.Models.RenkanModel.extend({ |
|
|
137 |
type: "edge", |
|
|
138 |
relations: [ |
|
|
139 |
{ |
|
|
140 |
type: Backbone.HasOne, |
|
|
141 |
key: "created_by", |
|
|
142 |
relatedModel: Rkns.Models.User |
|
|
143 |
}, |
|
|
144 |
{ |
|
|
145 |
type: Backbone.HasOne, |
|
|
146 |
key: "from", |
|
|
147 |
relatedModel: Rkns.Models.Node |
|
|
148 |
}, |
|
|
149 |
{ |
|
|
150 |
type: Backbone.HasOne, |
|
|
151 |
key: "to", |
|
|
152 |
relatedModel: Rkns.Models.Node |
|
|
153 |
}, |
|
|
154 |
], |
|
|
155 |
prepare: function(options) { |
|
|
156 |
project = options.project; |
|
|
157 |
this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user); |
|
|
158 |
this.addReference(options, "from", project.get("nodes"), options.from); |
|
|
159 |
this.addReference(options, "to", project.get("nodes"), options.to); |
|
|
160 |
return options; |
|
|
161 |
}, |
|
|
162 |
toJSON: function() { |
|
|
163 |
return { |
|
|
164 |
id: this.get("_id"), |
|
|
165 |
title: this.get("title"), |
|
|
166 |
uri: this.get("uri"), |
|
|
167 |
description: this.get("description"), |
|
|
168 |
from: this.get("from").get("_id"), |
|
|
169 |
to: this.get("to").get("_id"), |
|
|
170 |
created_by: this.get("created_by").get("_id"), |
|
|
171 |
} |
|
|
172 |
}, |
|
|
173 |
}); |
|
|
174 |
|
|
|
175 |
// PROJECT |
|
|
176 |
Rkns.Models.Project = Rkns.Models.RenkanModel.extend({ |
|
|
177 |
type: "project", |
|
|
178 |
relations: [ |
|
|
179 |
{ |
|
|
180 |
type: Backbone.HasMany, |
|
|
181 |
key: "users", |
|
|
182 |
relatedModel: Rkns.Models.User, |
|
|
183 |
reverseRelation: { |
|
|
184 |
key: 'project', |
|
|
185 |
includeInJSON: '_id' |
|
|
186 |
}, |
|
|
187 |
}, |
|
|
188 |
{ |
|
|
189 |
type: Backbone.HasMany, |
|
|
190 |
key: "nodes", |
|
|
191 |
relatedModel: Rkns.Models.Node, |
|
|
192 |
reverseRelation: { |
|
|
193 |
key: 'project', |
|
|
194 |
includeInJSON: '_id' |
|
|
195 |
}, |
|
|
196 |
}, |
|
|
197 |
{ |
|
|
198 |
type: Backbone.HasMany, |
|
|
199 |
key: "edges", |
|
|
200 |
relatedModel: Rkns.Models.Edge, |
|
|
201 |
reverseRelation: { |
|
|
202 |
key: 'project', |
|
|
203 |
includeInJSON: '_id' |
|
|
204 |
}, |
|
|
205 |
} |
|
|
206 |
], |
|
|
207 |
addUser: function(_props) { |
|
|
208 |
_props.project = this; |
|
|
209 |
var _user = new Rkns.Models.User(_props); |
|
|
210 |
this.get("users").push(_user); |
|
|
211 |
return _user; |
|
|
212 |
}, |
|
|
213 |
addNode: function(_props) { |
|
|
214 |
_props.project = this; |
|
|
215 |
var _node = new Rkns.Models.Node(_props); |
|
|
216 |
this.get("nodes").push(_node); |
|
|
217 |
return _node; |
|
|
218 |
}, |
|
|
219 |
addEdge: function(_props) { |
|
|
220 |
_props.project = this; |
|
|
221 |
var _edge = new Rkns.Models.Edge(_props); |
|
|
222 |
this.get("edges").push(_edge); |
|
|
223 |
return _edge; |
|
|
224 |
}, |
|
|
225 |
removeNode: function(_model) { |
|
|
226 |
this.get("nodes").remove(_model); |
|
|
227 |
}, |
|
|
228 |
removeEdge: function(_model) { |
|
|
229 |
this.get("edges").remove(_model); |
|
|
230 |
}, |
|
|
231 |
validate: function(options) { |
|
|
232 |
var _project = this; |
|
|
233 |
Rkns._(options.users).each(function(_item) { |
|
|
234 |
_item.project = _project; |
|
|
235 |
}); |
|
|
236 |
Rkns._(options.nodes).each(function(_item) { |
|
|
237 |
_item.project = _project; |
|
|
238 |
}); |
|
|
239 |
Rkns._(options.edges).each(function(_item) { |
|
|
240 |
_item.project = _project; |
|
|
241 |
}); |
|
|
242 |
}, |
|
|
243 |
// Add event handler to remove edges when a node is removed |
|
|
244 |
initialize: function() { |
|
|
245 |
var _this = this; |
|
|
246 |
this.on("remove:nodes", function(_node) { |
|
|
247 |
_this.get("edges").remove( |
|
|
248 |
_this.get("edges").filter(function(_edge) { |
|
|
249 |
return _edge.get("from") == _node || _edge.get("to") == _node; |
|
|
250 |
}) |
|
|
251 |
); |
|
|
252 |
}); |
|
|
253 |
} |
|
|
254 |
}); |
|
|
255 |
|
|
|
256 |
/* Point of entry */ |
|
|
257 |
|
|
|
258 |
Rkns.Renkan = function(_opts) { |
|
|
259 |
if (typeof _opts.language !== "string" || typeof Rkns.i18n[_opts.language] == "undefined") { |
|
|
260 |
_opts.language = "en"; |
|
|
261 |
} |
|
|
262 |
if (typeof _opts.container !== "string") { |
|
|
263 |
_opts.container = "renkan"; |
|
|
264 |
} |
|
|
265 |
if (typeof _opts.search !== "object" || !_opts.search) { |
|
|
266 |
_opts.search = []; |
|
|
267 |
} |
|
955
|
268 |
this.projects = []; |
|
938
|
269 |
this.l10n = Rkns.i18n[_opts.language]; |
|
|
270 |
this.$ = Rkns.$("#" + _opts.container); |
|
|
271 |
this.$.html(this.template()); |
|
955
|
272 |
this.uris = {}; |
|
|
273 |
this.active_project = null; |
|
|
274 |
this.renderer = null; |
|
938
|
275 |
} |
|
955
|
276 |
|
|
938
|
277 |
Rkns.Renkan.prototype.template = Rkns._.template( |
|
955
|
278 |
'<div class="Rk-Render"></div><ul class="Rk-Project-List"></ul>' |
|
938
|
279 |
); |
|
|
280 |
|
|
955
|
281 |
Rkns.Renkan.prototype.addProject = function(_opts) { |
|
|
282 |
var _proj = new Rkns.Models.Project(), |
|
|
283 |
_li = Rkns.$("<li>").addClass("Rk-Project").text("Untitled #" + (1+this.projects.length)); |
|
|
284 |
this.$.find(".Rk-Project-List").append(_li); |
|
|
285 |
Rkns.loadJson(_proj, _opts); |
|
|
286 |
var _this = this; |
|
|
287 |
_li.click(function() { |
|
|
288 |
_this.renderProject(_proj); |
|
|
289 |
}); |
|
|
290 |
_proj.on("change:title", function() { |
|
|
291 |
_li.html(_proj.get("title")); |
|
|
292 |
}); |
|
|
293 |
_proj.on("select", function() { |
|
|
294 |
_this.$.find(".Rk-Project").removeClass("active"); |
|
|
295 |
_li.addClass("active"); |
|
|
296 |
}); |
|
|
297 |
_proj.on("add:nodes", function(_node) { |
|
|
298 |
var _uri = _node.get("uri"); |
|
|
299 |
if (_uri) { |
|
|
300 |
if (typeof _this.uris[_uri] === "undefined") { |
|
|
301 |
_this.uris[_uri] = []; |
|
|
302 |
} |
|
|
303 |
_this.uris[_uri].push(_node); |
|
|
304 |
} |
|
|
305 |
}); |
|
|
306 |
this.projects.push(_proj); |
|
|
307 |
return _proj; |
|
|
308 |
} |
|
|
309 |
|
|
|
310 |
Rkns.Renkan.prototype.renderProject = function(_project) { |
|
|
311 |
if (_project) { |
|
|
312 |
if (this.renderer) { |
|
|
313 |
this.renderer.destroy(); |
|
|
314 |
} |
|
|
315 |
this.active_project = _project; |
|
|
316 |
this.renderer = new Rkns.Renderer.Scene(this, _project); |
|
|
317 |
this.renderer.autoScale(); |
|
|
318 |
_project.trigger("select"); |
|
|
319 |
} |
|
|
320 |
} |
|
|
321 |
|
|
|
322 |
Rkns.Renkan.prototype.renderProjectAt = function(_index) { |
|
|
323 |
this.renderProject(this.projects[_index]); |
|
|
324 |
} |
|
|
325 |
|
|
|
326 |
Rkns.loadJson = function(_proj, _opts) { |
|
938
|
327 |
if (typeof _opts.http_method == "undefined") { |
|
|
328 |
_opts.http_method = 'PUT'; |
|
|
329 |
} |
|
|
330 |
var _load = function() { |
|
|
331 |
Rkns.$.getJSON(_opts.url, function(_data) { |
|
|
332 |
_proj.set(_data); |
|
955
|
333 |
if (typeof _opts.callback === "function") { |
|
|
334 |
_opts.callback(_proj); |
|
|
335 |
} |
|
938
|
336 |
}); |
|
|
337 |
} |
|
|
338 |
_load(); |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
Rkns.Renderer = { |
|
|
342 |
_MARGIN_X: 80, |
|
|
343 |
_MARGIN_Y: 50, |
|
|
344 |
_MIN_DRAG_DISTANCE: 2, |
|
955
|
345 |
_NODE_RADIUS: 20, |
|
938
|
346 |
_NODE_FONT_SIZE: 10, |
|
|
347 |
_EDGE_FONT_SIZE: 9, |
|
|
348 |
_NODE_MAX_CHAR: 30, |
|
|
349 |
_EDGE_MAX_CHAR: 20, |
|
|
350 |
_ARROW_LENGTH: 16, |
|
|
351 |
_ARROW_WIDTH: 8, |
|
|
352 |
_TOOLTIP_ARROW_LENGTH : 15, |
|
|
353 |
_TOOLTIP_ARROW_WIDTH : 26, |
|
|
354 |
_TOOLTIP_MARGIN : 10, |
|
|
355 |
_TOOLTIP_PADDING : 8, |
|
|
356 |
_TOOLTIP_GRADIENT : new paper.Gradient(['#f0f0f0', '#d0d0d0']) |
|
|
357 |
} |
|
|
358 |
|
|
|
359 |
Rkns.Renderer.Utils = { |
|
|
360 |
shortenText : function(_text,_length) { |
|
|
361 |
var _rgxp = new RegExp('^(.{' + _length + '}).+$'); |
|
|
362 |
return _text.replace(/(\n|\r|\r\n)/mg,' ').replace(_rgxp,'$1…'); |
|
|
363 |
}, |
|
|
364 |
drawTooltip : function(_coords, _path, _width, _xmargin, _selector) { |
|
|
365 |
_selector.css({ |
|
|
366 |
width: (_width - 2* Rkns.Renderer._TOOLTIP_PADDING), |
|
|
367 |
}); |
|
|
368 |
var _height = _selector.outerHeight() + 2* Rkns.Renderer._TOOLTIP_PADDING, |
|
|
369 |
_isLeft = (_coords.x < paper.view.center.x ? 1 : -1), |
|
|
370 |
_left = _coords.x + _isLeft * ( _xmargin + Rkns.Renderer._TOOLTIP_ARROW_LENGTH ), |
|
|
371 |
_right = _coords.x + _isLeft * ( _xmargin + Rkns.Renderer._TOOLTIP_ARROW_LENGTH + _width ), |
|
|
372 |
_top = _coords.y - _height / 2; |
|
|
373 |
if (_top < Rkns.Renderer._TOOLTIP_MARGIN) { |
|
|
374 |
_top = Math.min( Rkns.Renderer._TOOLTIP_MARGIN, _coords.y - Rkns.Renderer._TOOLTIP_ARROW_WIDTH / 2 ); |
|
|
375 |
} |
|
|
376 |
var _bottom = _top + _height; |
|
|
377 |
if (_bottom > (paper.view.size.height - Rkns.Renderer._TOOLTIP_MARGIN)) { |
|
|
378 |
_bottom = Math.max( paper.view.size.height - Rkns.Renderer._TOOLTIP_MARGIN, _coords.y + Rkns.Renderer._TOOLTIP_ARROW_WIDTH / 2 ); |
|
|
379 |
_top = _bottom - _height; |
|
|
380 |
} |
|
|
381 |
_path.segments[0].point |
|
|
382 |
= _path.segments[7].point |
|
|
383 |
= _coords.add([_isLeft * _xmargin, 0]); |
|
|
384 |
_path.segments[1].point.x |
|
|
385 |
= _path.segments[2].point.x |
|
|
386 |
= _path.segments[5].point.x |
|
|
387 |
= _path.segments[6].point.x |
|
|
388 |
= _left; |
|
|
389 |
_path.segments[3].point.x |
|
|
390 |
= _path.segments[4].point.x |
|
|
391 |
= _right; |
|
|
392 |
_path.segments[2].point.y |
|
|
393 |
= _path.segments[3].point.y |
|
|
394 |
= _top; |
|
|
395 |
_path.segments[4].point.y |
|
|
396 |
= _path.segments[5].point.y |
|
|
397 |
= _bottom; |
|
|
398 |
_path.segments[1].point.y = _coords.y - Rkns.Renderer._TOOLTIP_ARROW_WIDTH / 2; |
|
|
399 |
_path.segments[6].point.y = _coords.y + Rkns.Renderer._TOOLTIP_ARROW_WIDTH / 2; |
|
|
400 |
_path.closed = true; |
|
|
401 |
_path.fillColor = new paper.GradientColor(Rkns.Renderer._TOOLTIP_GRADIENT, [0,_top], [0, _bottom]); |
|
|
402 |
_selector.css({ |
|
|
403 |
left: (Rkns.Renderer._TOOLTIP_PADDING + Math.min(_left, _right)), |
|
|
404 |
top: (Rkns.Renderer._TOOLTIP_PADDING + _top) |
|
|
405 |
}); |
|
|
406 |
} |
|
|
407 |
} |
|
|
408 |
|
|
|
409 |
Rkns.Renderer._BaseRepresentation = function(_renderer, _model) { |
|
|
410 |
if (typeof _renderer !== "undefined") { |
|
|
411 |
this.renderer = _renderer; |
|
955
|
412 |
this.project = _renderer.project; |
|
938
|
413 |
this.model = _model; |
|
955
|
414 |
if (this.model) { |
|
938
|
415 |
var _this = this; |
|
955
|
416 |
this._selectBinding = function() { |
|
938
|
417 |
_this.select(); |
|
955
|
418 |
}; |
|
|
419 |
this._unselectBinding = function() { |
|
938
|
420 |
_this.unselect(); |
|
955
|
421 |
} |
|
|
422 |
this._changeBinding = function() { |
|
|
423 |
_this.redraw(); |
|
|
424 |
} |
|
|
425 |
this._removeBinding = function() { |
|
|
426 |
_renderer.removeRepresentation(_this); |
|
|
427 |
_renderer.redraw(); |
|
|
428 |
} |
|
|
429 |
this.model.on("change", this._changeBinding ); |
|
|
430 |
this.model.on("remove", this._removeBinding ); |
|
|
431 |
this.model.on("select", this._selectBinding ); |
|
|
432 |
this.model.on("unselect", this._unselectBinding ); |
|
938
|
433 |
} |
|
|
434 |
} |
|
|
435 |
} |
|
|
436 |
|
|
955
|
437 |
Rkns.Renderer._BaseRepresentation.prototype.super = function(_func) { |
|
|
438 |
Rkns.Renderer._BaseRepresentation.prototype[_func].apply(this, Array.prototype.slice.call(arguments, 1)); |
|
|
439 |
} |
|
|
440 |
|
|
938
|
441 |
Rkns.Renderer._BaseRepresentation.prototype.select = function() {} |
|
|
442 |
|
|
|
443 |
Rkns.Renderer._BaseRepresentation.prototype.unselect = function() {} |
|
|
444 |
|
|
|
445 |
Rkns.Renderer._BaseRepresentation.prototype.mouseup = function() {} |
|
|
446 |
|
|
955
|
447 |
Rkns.Renderer._BaseRepresentation.prototype.destroy = function() { |
|
|
448 |
if (this.model) { |
|
|
449 |
this.model.off("change", this._changeBinding ); |
|
|
450 |
this.model.off("remove", this._removeBinding ); |
|
|
451 |
this.model.off("select", this._selectBinding); |
|
|
452 |
this.model.off("unselect", this._unselectBinding); |
|
|
453 |
} |
|
|
454 |
} |
|
938
|
455 |
|
|
|
456 |
Rkns.Renderer.Node = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); |
|
|
457 |
|
|
|
458 |
Rkns.Renderer.Node.prototype._init = function() { |
|
|
459 |
this.renderer.node_layer.activate(); |
|
|
460 |
this.type = "Node"; |
|
|
461 |
this.circle = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS); |
|
|
462 |
this.circle.fillColor = '#ffffff'; |
|
|
463 |
this.circle.__representation = this; |
|
|
464 |
this.title = new paper.PointText([0,0]); |
|
|
465 |
this.title.characterStyle = { |
|
|
466 |
fontSize: Rkns.Renderer._NODE_FONT_SIZE, |
|
|
467 |
fillColor: 'black' |
|
|
468 |
}; |
|
|
469 |
this.title.paragraphStyle.justification = 'center'; |
|
|
470 |
this.title.__representation = this; |
|
|
471 |
this.model_coords = new paper.Point(this.model.get("position")); |
|
|
472 |
} |
|
|
473 |
|
|
|
474 |
Rkns.Renderer.Node.prototype.redraw = function() { |
|
|
475 |
this.paper_coords = this.renderer.toPaperCoords(this.model_coords); |
|
|
476 |
this.circle.position = this.paper_coords; |
|
|
477 |
this.title.content = Rkns.Renderer.Utils.shortenText(this.model.get("title"), Rkns.Renderer._NODE_MAX_CHAR); |
|
|
478 |
this.title.position = this.paper_coords.add([0, 2 * Rkns.Renderer._NODE_RADIUS]); |
|
|
479 |
this.circle.strokeColor = this.model.get("created_by").get("color"); |
|
955
|
480 |
var _img = this.model.get("image"); |
|
|
481 |
if (_img && _img !== this.img) { |
|
|
482 |
var _image = new Image(), |
|
|
483 |
_this = this; |
|
|
484 |
_image.onload = function() { |
|
|
485 |
if (_this.node_image) { |
|
|
486 |
_this.node_image.remove(); |
|
|
487 |
} |
|
|
488 |
_this.renderer.node_layer.activate(); |
|
|
489 |
var _ratio = Math.min(1, 2 * Rkns.Renderer._NODE_RADIUS / _image.width, 2 * Rkns.Renderer._NODE_RADIUS / _image.height ); |
|
|
490 |
var _raster = new paper.Raster(_image); |
|
|
491 |
var _clip = new paper.Path.Circle([0, 0], Rkns.Renderer._NODE_RADIUS); |
|
|
492 |
_raster.scale(_ratio); |
|
|
493 |
_this.node_image = new paper.Group(_clip, _raster); |
|
|
494 |
_this.node_image.opacity = _this.selected ? .5 : .9; |
|
|
495 |
/* This is a workaround to allow clipping at group level */ |
|
|
496 |
_this.node_image.clipped = true; |
|
|
497 |
_this.node_image.position = _this.paper_coords; |
|
|
498 |
_this.node_image.__representation = _this; |
|
|
499 |
paper.view.draw(); |
|
|
500 |
} |
|
|
501 |
_image.src = _img; |
|
|
502 |
} |
|
|
503 |
this.img = _img; |
|
|
504 |
if (this.node_image) { |
|
|
505 |
if (!this.img) { |
|
|
506 |
this.node_image.remove(); |
|
|
507 |
delete this.node_image; |
|
|
508 |
} else { |
|
|
509 |
this.node_image.position = this.paper_coords; |
|
|
510 |
} |
|
|
511 |
} |
|
938
|
512 |
} |
|
|
513 |
|
|
|
514 |
Rkns.Renderer.Node.prototype.paperShift = function(_delta) { |
|
|
515 |
this.paper_coords = this.paper_coords.add(_delta); |
|
|
516 |
this.model_coords = this.renderer.toModelCoords(this.paper_coords); |
|
|
517 |
this.renderer.redraw(); |
|
|
518 |
} |
|
|
519 |
|
|
|
520 |
Rkns.Renderer.Node.prototype.openTooltip = function() { |
|
|
521 |
this.renderer.removeRepresentationsOfType("tooltip"); |
|
|
522 |
var _tooltip = this.renderer.addRepresentation("NodeTooltip",null); |
|
|
523 |
_tooltip.node_representation = this; |
|
955
|
524 |
_tooltip.draw(); |
|
938
|
525 |
} |
|
|
526 |
|
|
|
527 |
Rkns.Renderer.Node.prototype.select = function() { |
|
955
|
528 |
this.selected = true; |
|
938
|
529 |
this.circle.strokeWidth = 3; |
|
955
|
530 |
this.openTooltip(); |
|
|
531 |
this.circle.fillColor = "#ffff80"; |
|
|
532 |
if (this.node_image) { |
|
|
533 |
this.node_image.opacity = .5; |
|
|
534 |
} |
|
938
|
535 |
paper.view.draw(); |
|
|
536 |
} |
|
|
537 |
|
|
|
538 |
Rkns.Renderer.Node.prototype.unselect = function() { |
|
955
|
539 |
this.selected = false; |
|
938
|
540 |
this.circle.strokeWidth = 1; |
|
|
541 |
this.circle.fillColor = "#ffffff"; |
|
955
|
542 |
if (this.node_image) { |
|
|
543 |
this.node_image.opacity = .9; |
|
|
544 |
} |
|
938
|
545 |
paper.view.draw(); |
|
|
546 |
} |
|
|
547 |
|
|
|
548 |
Rkns.Renderer.Node.prototype.mouseup = function(_event) { |
|
|
549 |
} |
|
|
550 |
|
|
|
551 |
Rkns.Renderer.Node.prototype.destroy = function(_event) { |
|
955
|
552 |
this.super("destroy"); |
|
938
|
553 |
this.circle.remove(); |
|
|
554 |
this.title.remove(); |
|
955
|
555 |
if (this.node_image) { |
|
|
556 |
this.node_image.remove(); |
|
|
557 |
} |
|
938
|
558 |
} |
|
|
559 |
|
|
|
560 |
/* */ |
|
|
561 |
|
|
|
562 |
Rkns.Renderer.Edge = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); |
|
|
563 |
|
|
|
564 |
Rkns.Renderer.Edge.prototype._init = function() { |
|
|
565 |
this.renderer.edge_layer.activate(); |
|
|
566 |
this.type = "Edge"; |
|
|
567 |
this.from_representation = this.renderer.getRepresentationByModel(this.model.get("from")); |
|
|
568 |
this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to")); |
|
950
|
569 |
this.bundle = this.renderer.addToBundles(this); |
|
938
|
570 |
this.line = new paper.Path(); |
|
950
|
571 |
this.line.add([0,0],[0,0],[0,0]); |
|
938
|
572 |
this.line.__representation = this; |
|
|
573 |
this.arrow = new paper.Path(); |
|
|
574 |
this.arrow.add([0,0],[Rkns.Renderer._ARROW_LENGTH,Rkns.Renderer._ARROW_WIDTH / 2],[0,Rkns.Renderer._ARROW_WIDTH]); |
|
|
575 |
this.arrow.__representation = this; |
|
|
576 |
this.text = new paper.PointText(); |
|
|
577 |
this.text.characterStyle = { |
|
|
578 |
fontSize: Rkns.Renderer._EDGE_FONT_SIZE, |
|
|
579 |
fillColor: 'black' |
|
|
580 |
}; |
|
|
581 |
this.text.paragraphStyle.justification = 'center'; |
|
|
582 |
this.text.__representation = this; |
|
|
583 |
this.text_angle = 0; |
|
|
584 |
this.arrow_angle = 0; |
|
|
585 |
} |
|
|
586 |
|
|
|
587 |
Rkns.Renderer.Edge.prototype.redraw = function() { |
|
950
|
588 |
var _p0a = this.from_representation.paper_coords, |
|
|
589 |
_p1a = this.to_representation.paper_coords, |
|
|
590 |
_v = _p1a.subtract(_p0a), |
|
938
|
591 |
_r = _v.length, |
|
|
592 |
_u = _v.divide(_r), |
|
950
|
593 |
_group_pos = this.bundle.getPosition(this), |
|
|
594 |
_delta = new paper.Point([- _u.y, _u.x]).multiply( 12 * _group_pos ), |
|
|
595 |
_p0b = _p0a.add(_delta), /* Adding a 4 px difference */ |
|
|
596 |
_p1b = _p1a.add(_delta), /* to differentiate inbound and outbound links */ |
|
938
|
597 |
_a = _v.angle, |
|
950
|
598 |
_handle = _v.divide(3), |
|
938
|
599 |
_color = this.model.get("created_by").get("color"); |
|
950
|
600 |
this.paper_coords = _p0b.add(_p1b).divide(2); |
|
938
|
601 |
this.line.strokeColor = _color; |
|
950
|
602 |
this.line.segments[0].point = _p0a; |
|
|
603 |
this.line.segments[1].point = this.paper_coords; |
|
|
604 |
this.line.segments[1].handleIn = _handle.multiply(-1); |
|
|
605 |
this.line.segments[1].handleOut = _handle; |
|
|
606 |
this.line.segments[2].point = _p1a; |
|
938
|
607 |
this.arrow.rotate(_a - this.arrow_angle); |
|
|
608 |
this.arrow.fillColor = _color; |
|
|
609 |
this.arrow.position = this.paper_coords.subtract(_u.multiply(4)); |
|
|
610 |
this.arrow_angle = _a; |
|
|
611 |
if (_a > 90) { |
|
|
612 |
_a -= 180; |
|
|
613 |
} |
|
|
614 |
if (_a < -90) { |
|
|
615 |
_a += 180; |
|
|
616 |
} |
|
|
617 |
this.text.rotate(_a - this.text_angle); |
|
|
618 |
this.text.content = Rkns.Renderer.Utils.shortenText(this.model.get("title"), Rkns.Renderer._EDGE_MAX_CHAR); |
|
|
619 |
this.text.position = this.paper_coords; |
|
|
620 |
this.text_angle = _a; |
|
|
621 |
} |
|
|
622 |
|
|
|
623 |
Rkns.Renderer.Edge.prototype.openTooltip = function() { |
|
|
624 |
this.renderer.removeRepresentationsOfType("tooltip"); |
|
|
625 |
var _tooltip = this.renderer.addRepresentation("EdgeTooltip",null); |
|
|
626 |
_tooltip.edge_representation = this; |
|
955
|
627 |
_tooltip.draw(); |
|
938
|
628 |
} |
|
|
629 |
|
|
|
630 |
Rkns.Renderer.Edge.prototype.select = function() { |
|
|
631 |
this.line.strokeWidth = 3; |
|
|
632 |
this.openTooltip(); |
|
|
633 |
paper.view.draw(); |
|
|
634 |
} |
|
|
635 |
|
|
|
636 |
Rkns.Renderer.Edge.prototype.unselect = function() { |
|
|
637 |
this.line.strokeWidth = 1; |
|
|
638 |
paper.view.draw(); |
|
|
639 |
} |
|
|
640 |
|
|
|
641 |
Rkns.Renderer.Edge.prototype.mouseup = function(_event) { |
|
|
642 |
} |
|
|
643 |
|
|
|
644 |
Rkns.Renderer.Edge.prototype.paperShift = function(_delta) { |
|
|
645 |
this.from_representation.paperShift(_delta); |
|
|
646 |
this.to_representation.paperShift(_delta); |
|
|
647 |
this.renderer.redraw(); |
|
|
648 |
} |
|
|
649 |
|
|
|
650 |
Rkns.Renderer.Edge.prototype.destroy = function() { |
|
955
|
651 |
this.super("destroy"); |
|
938
|
652 |
this.line.remove(); |
|
|
653 |
this.arrow.remove(); |
|
|
654 |
this.text.remove(); |
|
950
|
655 |
var _this = this; |
|
|
656 |
this.bundle.edges = Rkns._(this.bundle.edges).reject(function(_edge) { |
|
|
657 |
return _edge === _this; |
|
|
658 |
}); |
|
938
|
659 |
} |
|
|
660 |
|
|
|
661 |
/* */ |
|
|
662 |
|
|
|
663 |
Rkns.Renderer.NodeTooltip = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); |
|
|
664 |
|
|
|
665 |
Rkns.Renderer.NodeTooltip.prototype._init = function() { |
|
|
666 |
this.renderer.overlay_layer.activate(); |
|
|
667 |
this.type = "tooltip"; |
|
|
668 |
this.tooltip_block = new paper.Path(); |
|
|
669 |
var _pts = Rkns._(Rkns._.range(8)).map(function() {return [0,0]}); |
|
|
670 |
this.tooltip_block.add.apply(this.tooltip_block, _pts); |
|
|
671 |
this.tooltip_block.strokeWidth = 2; |
|
|
672 |
this.tooltip_block.strokeColor = "#999999"; |
|
|
673 |
this.tooltip_block.fillColor = "#e0e0e0"; |
|
|
674 |
this.tooltip_block.opacity = .8; |
|
|
675 |
this.tooltip_$ = Rkns.$('<div>') |
|
|
676 |
.appendTo(this.renderer.tooltip_$) |
|
|
677 |
.css({ |
|
|
678 |
position: "absolute", |
|
|
679 |
opacity: .8 |
|
|
680 |
}) |
|
|
681 |
.hide(); |
|
|
682 |
} |
|
|
683 |
|
|
|
684 |
Rkns.Renderer.NodeTooltip.prototype.template = Rkns._.template( |
|
|
685 |
'<h2><span class="Rk-CloseX">×</span><%=a%></h2>' |
|
955
|
686 |
+ '<p><%-description%></p>' |
|
|
687 |
+ '<ul class="Rk-Related-List"></ul>' |
|
938
|
688 |
); |
|
|
689 |
|
|
955
|
690 |
Rkns.Renderer.NodeTooltip.prototype.draw = function() { |
|
|
691 |
var _model = this.node_representation.model, |
|
938
|
692 |
_title = _model.get("title"), |
|
|
693 |
_uri = _model.get("uri"); |
|
|
694 |
this.tooltip_$ |
|
|
695 |
.html(this.template({ |
|
|
696 |
a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ), |
|
|
697 |
description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1…') |
|
|
698 |
})) |
|
955
|
699 |
var _this = this, |
|
|
700 |
_renkan = this.renderer.renkan, |
|
|
701 |
_uris = _renkan.uris[_uri]; |
|
|
702 |
Rkns._(_uris).each(function(_othernode) { |
|
|
703 |
if (_othernode !== _model && _othernode.get("project") !== _this.project) { |
|
|
704 |
var _otherproj = _othernode.get("project"), |
|
|
705 |
_nodetitle = _othernode.get("title") || "Untitled node" |
|
|
706 |
_projtitle = _otherproj.get("title") || "Untitled node", |
|
|
707 |
_html = _renkan.l10n.see_in_project.replace('{node}',Rkns._.escape(_nodetitle)).replace('{project}',Rkns._.escape(_projtitle)), |
|
|
708 |
_li = Rkns.$("<li>").addClass("Rk-Related").html(_html); |
|
|
709 |
_li.click(function() { |
|
|
710 |
_renkan.renderProject(_otherproj); |
|
|
711 |
Rkns._.defer(function() { |
|
|
712 |
_othernode.trigger("select"); |
|
|
713 |
}); |
|
|
714 |
}); |
|
|
715 |
_this.tooltip_$.append(_li); |
|
|
716 |
} |
|
|
717 |
}); |
|
938
|
718 |
this.tooltip_$.find(".Rk-CloseX").click(function() { |
|
|
719 |
_this.renderer.removeRepresentation(_this); |
|
|
720 |
paper.view.draw(); |
|
|
721 |
}); |
|
955
|
722 |
this.redraw(); |
|
|
723 |
} |
|
|
724 |
|
|
|
725 |
Rkns.Renderer.NodeTooltip.prototype.redraw = function() { |
|
|
726 |
var _coords = this.node_representation.paper_coords; |
|
|
727 |
Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 15, this.tooltip_$); |
|
|
728 |
this.tooltip_$.show(); |
|
938
|
729 |
} |
|
|
730 |
|
|
|
731 |
Rkns.Renderer.NodeTooltip.prototype.destroy = function() { |
|
|
732 |
this.tooltip_block.remove(); |
|
|
733 |
this.tooltip_$.detach(); |
|
|
734 |
} |
|
|
735 |
|
|
|
736 |
/* */ |
|
|
737 |
|
|
|
738 |
Rkns.Renderer.EdgeTooltip = Rkns.Utils.inherit(Rkns.Renderer._BaseRepresentation); |
|
|
739 |
|
|
|
740 |
Rkns.Renderer.EdgeTooltip.prototype._init = function() { |
|
|
741 |
this.renderer.overlay_layer.activate(); |
|
|
742 |
this.type = "tooltip"; |
|
|
743 |
this.tooltip_block = new paper.Path(); |
|
|
744 |
var _pts = Rkns._(Rkns._.range(8)).map(function() {return [0,0]}); |
|
|
745 |
this.tooltip_block.add.apply(this.tooltip_block, _pts); |
|
|
746 |
this.tooltip_block.strokeWidth = 2; |
|
|
747 |
this.tooltip_block.strokeColor = "#999999"; |
|
|
748 |
this.tooltip_block.fillColor = "#e0e0e0"; |
|
|
749 |
this.tooltip_block.opacity = .8; |
|
|
750 |
this.tooltip_$ = Rkns.$('<div>') |
|
|
751 |
.appendTo(this.renderer.tooltip_$) |
|
|
752 |
.css({ |
|
|
753 |
position: "absolute", |
|
|
754 |
opacity: .8 |
|
|
755 |
}) |
|
|
756 |
.hide(); |
|
|
757 |
} |
|
|
758 |
|
|
|
759 |
Rkns.Renderer.EdgeTooltip.prototype.template = Rkns._.template( |
|
|
760 |
'<h2><span class="Rk-CloseX">×</span><%=a%></h2>' |
|
955
|
761 |
+ '<p><%-description%></p>' |
|
938
|
762 |
); |
|
|
763 |
|
|
955
|
764 |
Rkns.Renderer.EdgeTooltip.prototype.draw = function() { |
|
|
765 |
var _model = this.edge_representation.model, |
|
938
|
766 |
_title = _model.get("title"), |
|
|
767 |
_uri = _model.get("uri"); |
|
|
768 |
this.tooltip_$ |
|
|
769 |
.html(this.template({ |
|
|
770 |
a: (_uri ? '<a href="' + _uri + '" target="_blank">' : '' ) + _title + (_uri ? '</a>' : '' ), |
|
|
771 |
description: _model.get("description").replace(/(\n|\r|\r\n)/mg,' ').substr(0,180).replace(/(^.{150,179})[\s].+$/m,'$1…') |
|
955
|
772 |
})); |
|
|
773 |
this.redraw(); |
|
938
|
774 |
var _this = this; |
|
|
775 |
this.tooltip_$.find(".Rk-CloseX").click(function() { |
|
|
776 |
_this.renderer.removeRepresentation(_this); |
|
|
777 |
paper.view.draw(); |
|
|
778 |
}); |
|
955
|
779 |
} |
|
|
780 |
|
|
|
781 |
Rkns.Renderer.EdgeTooltip.prototype.redraw = function() { |
|
|
782 |
var _coords = this.edge_representation.paper_coords; |
|
|
783 |
Rkns.Renderer.Utils.drawTooltip(_coords, this.tooltip_block, 250, 5, this.tooltip_$); |
|
|
784 |
this.tooltip_$.show(); |
|
938
|
785 |
paper.view.draw(); |
|
|
786 |
} |
|
|
787 |
|
|
|
788 |
Rkns.Renderer.EdgeTooltip.prototype.destroy = function() { |
|
|
789 |
this.tooltip_block.remove(); |
|
|
790 |
this.tooltip_$.detach(); |
|
|
791 |
} |
|
|
792 |
|
|
|
793 |
/* */ |
|
|
794 |
|
|
955
|
795 |
Rkns.Renderer.Scene = function(_renkan, _project) { |
|
938
|
796 |
this.renkan = _renkan; |
|
955
|
797 |
this.project = _project; |
|
938
|
798 |
this.$ = Rkns.$(".Rk-Render"); |
|
|
799 |
this.representations = []; |
|
|
800 |
this.$.html(this.template({ |
|
|
801 |
width: this.$.width(), |
|
|
802 |
height: this.$.height(), |
|
|
803 |
l10n: _renkan.l10n |
|
|
804 |
})) |
|
|
805 |
this.canvas_$ = this.$.find(".Rk-Canvas"); |
|
|
806 |
this.tooltip_$ = this.$.find(".Rk-Editor"); |
|
|
807 |
paper.setup(this.canvas_$[0]); |
|
|
808 |
this.scale = 1; |
|
|
809 |
this.offset = paper.view.center; |
|
|
810 |
this.totalScroll = 0; |
|
|
811 |
this.click_target = null; |
|
|
812 |
this.selected_target = null; |
|
|
813 |
this.edge_layer = new paper.Layer(); |
|
|
814 |
this.node_layer = new paper.Layer(); |
|
|
815 |
this.overlay_layer = new paper.Layer(); |
|
950
|
816 |
this.bundles = []; |
|
938
|
817 |
var _tool = new paper.Tool(), |
|
|
818 |
_this = this; |
|
|
819 |
_tool.minDistance = Rkns.Renderer._MIN_DRAG_DISTANCE; |
|
|
820 |
_tool.onMouseMove = function(_event) { |
|
|
821 |
_this.onMouseMove(_event); |
|
|
822 |
} |
|
|
823 |
_tool.onMouseDown = function(_event) { |
|
|
824 |
_this.onMouseDown(_event); |
|
|
825 |
} |
|
|
826 |
_tool.onMouseDrag = function(_event) { |
|
|
827 |
_this.onMouseDrag(_event); |
|
|
828 |
} |
|
|
829 |
this.canvas_$.mouseup(function(_event) { |
|
|
830 |
_this.onMouseUp(_event); |
|
|
831 |
}); |
|
|
832 |
this.canvas_$.mousewheel(function(_event, _delta) { |
|
|
833 |
_this.onScroll(_event, _delta); |
|
|
834 |
}); |
|
|
835 |
this.tooltip_$.find(".Rk-ZoomOut").click(function() { |
|
|
836 |
_this.offset = new paper.Point([ |
|
|
837 |
_this.canvas_$.width(), |
|
|
838 |
_this.canvas_$.height() |
|
|
839 |
]).multiply( .5 * ( 1 - Math.SQRT1_2 ) ).add(_this.offset.multiply( Math.SQRT1_2 )); |
|
|
840 |
_this.scale *= Math.SQRT1_2; |
|
|
841 |
_this.redraw(); |
|
|
842 |
}); |
|
|
843 |
this.tooltip_$.find(".Rk-ZoomIn").click(function() { |
|
|
844 |
_this.offset = new paper.Point([ |
|
|
845 |
_this.canvas_$.width(), |
|
|
846 |
_this.canvas_$.height() |
|
|
847 |
]).multiply( .5 * ( 1 - Math.SQRT2 ) ).add(_this.offset.multiply( Math.SQRT2 )); |
|
|
848 |
_this.scale *= Math.SQRT2; |
|
|
849 |
_this.redraw(); |
|
|
850 |
}); |
|
|
851 |
paper.view.onResize = function(_event) { |
|
|
852 |
_this.offset = _this.offset.add(_event.delta.divide(2)); |
|
|
853 |
_this.redraw(); |
|
|
854 |
} |
|
|
855 |
|
|
|
856 |
var _thRedraw = Rkns._.throttle(function() { |
|
|
857 |
_this.redraw(); |
|
|
858 |
},50); |
|
|
859 |
|
|
955
|
860 |
this.addRepresentations("Node", this.project.get("nodes")); |
|
|
861 |
this.addRepresentations("Edge", this.project.get("edges")); |
|
938
|
862 |
|
|
955
|
863 |
this._addNodesBinding = function(_node) { |
|
938
|
864 |
_this.addRepresentation("Node", _node); |
|
|
865 |
_thRedraw(); |
|
955
|
866 |
} |
|
|
867 |
this._addEdgesBinding = function(_edge) { |
|
938
|
868 |
_this.addRepresentation("Edge", _edge); |
|
|
869 |
_thRedraw(); |
|
955
|
870 |
} |
|
938
|
871 |
|
|
955
|
872 |
this.project.on("add:nodes", this._addNodesBinding ); |
|
|
873 |
this.project.on("add:edges", this._addEdgesBinding ); |
|
938
|
874 |
this.redraw(); |
|
|
875 |
} |
|
|
876 |
|
|
|
877 |
Rkns.Renderer.Scene.prototype.template = Rkns._.template( |
|
955
|
878 |
'<canvas class="Rk-Canvas" width="<%-width%>" height="<%-height%>"></canvas><div class="Rk-Editor">' |
|
|
879 |
+ '<div class="Rk-ZoomButtons"><div class="Rk-ZoomIn" title="<%-l10n.zoom_in%>"></div><div class="Rk-ZoomOut" title="<%-l10n.zoom_out%>"></div></div>' |
|
938
|
880 |
+ '</div>' |
|
|
881 |
); |
|
|
882 |
|
|
950
|
883 |
Rkns.Renderer.Scene.prototype.addToBundles = function(_edgeRepr) { |
|
|
884 |
var _bundle = Rkns._(this.bundles).find(function(_bundle) { |
|
|
885 |
return ( |
|
|
886 |
( _bundle.from === _edgeRepr.from_representation && _bundle.to === _edgeRepr.to_representation ) |
|
|
887 |
|| ( _bundle.from === _edgeRepr.to_representation && _bundle.to === _edgeRepr.from_representation ) |
|
|
888 |
); |
|
|
889 |
}); |
|
|
890 |
if (typeof _bundle !== "undefined") { |
|
|
891 |
_bundle.edges.push(_edgeRepr) |
|
|
892 |
} else { |
|
|
893 |
_bundle = { |
|
|
894 |
from: _edgeRepr.from_representation, |
|
|
895 |
to: _edgeRepr.to_representation, |
|
|
896 |
edges: [ _edgeRepr ], |
|
|
897 |
getPosition: function(_er) { |
|
|
898 |
var _dir = (_er.from_representation === this.from) ? 1 : -1; |
|
|
899 |
return _dir * ( Rkns._(this.edges).indexOf(_er) - (this.edges.length - 1) / 2 ); |
|
|
900 |
} |
|
|
901 |
} |
|
|
902 |
this.bundles.push(_bundle); |
|
|
903 |
} |
|
|
904 |
return _bundle; |
|
|
905 |
} |
|
|
906 |
|
|
938
|
907 |
Rkns.Renderer.Scene.prototype.autoScale = function() { |
|
955
|
908 |
if (this.project.get("nodes").length) { |
|
|
909 |
var _xx = this.project.get("nodes").map(function(_node) { return _node.get("position").x }), |
|
|
910 |
_yy = this.project.get("nodes").map(function(_node) { return _node.get("position").y }), |
|
938
|
911 |
_minx = Math.min.apply(Math, _xx), |
|
|
912 |
_miny = Math.min.apply(Math, _yy), |
|
|
913 |
_maxx = Math.max.apply(Math, _xx), |
|
|
914 |
_maxy = Math.max.apply(Math, _yy); |
|
|
915 |
this.scale = Math.min((paper.view.size.width - 2 * Rkns.Renderer._MARGIN_X) / (_maxx - _minx), (paper.view.size.height - 2 * Rkns.Renderer._MARGIN_Y) / (_maxy - _miny)); |
|
|
916 |
this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
|
|
917 |
this.redraw(); |
|
|
918 |
} |
|
|
919 |
} |
|
|
920 |
|
|
|
921 |
Rkns.Renderer.Scene.prototype.toPaperCoords = function(_point) { |
|
|
922 |
return _point.multiply(this.scale).add(this.offset); |
|
|
923 |
} |
|
|
924 |
|
|
|
925 |
|
|
|
926 |
Rkns.Renderer.Scene.prototype.toModelCoords = function(_point) { |
|
|
927 |
return _point.subtract(this.offset).divide(this.scale); |
|
|
928 |
} |
|
|
929 |
|
|
|
930 |
Rkns.Renderer.Scene.prototype.addRepresentation = function(_type, _model) { |
|
|
931 |
var _repr = new Rkns.Renderer[_type](this, _model); |
|
|
932 |
this.representations.push(_repr); |
|
|
933 |
return _repr; |
|
|
934 |
} |
|
|
935 |
|
|
|
936 |
Rkns.Renderer.Scene.prototype.addRepresentations = function(_type, _collection) { |
|
|
937 |
var _this = this; |
|
|
938 |
_collection.forEach(function(_model) { |
|
|
939 |
_this.addRepresentation(_type, _model); |
|
|
940 |
}); |
|
|
941 |
} |
|
|
942 |
|
|
|
943 |
Rkns.Renderer.Scene.prototype.removeRepresentation = function(_representation) { |
|
|
944 |
_representation.destroy(); |
|
|
945 |
this.representations = Rkns._(this.representations).reject( |
|
|
946 |
function(_repr) { |
|
|
947 |
return _repr == _representation |
|
|
948 |
} |
|
|
949 |
); |
|
|
950 |
} |
|
|
951 |
|
|
|
952 |
Rkns.Renderer.Scene.prototype.getRepresentationByModel = function(_model) { |
|
|
953 |
return Rkns._(this.representations).find(function(_repr) { |
|
|
954 |
return _repr.model === _model; |
|
|
955 |
}); |
|
|
956 |
} |
|
|
957 |
|
|
|
958 |
Rkns.Renderer.Scene.prototype.removeRepresentationsOfType = function(_type) { |
|
|
959 |
var _representations = Rkns._(this.representations).filter(function(_repr) { |
|
|
960 |
return _repr.type == _type; |
|
|
961 |
}), |
|
|
962 |
_this = this; |
|
|
963 |
Rkns._(_representations).each(function(_repr) { |
|
|
964 |
_this.removeRepresentation(_repr); |
|
|
965 |
}); |
|
|
966 |
} |
|
|
967 |
|
|
|
968 |
Rkns.Renderer.Scene.prototype.unselectAll = function() { |
|
|
969 |
Rkns._(this.representations).each(function(_repr) { |
|
|
970 |
_repr.model.trigger("unselect"); |
|
|
971 |
}); |
|
|
972 |
} |
|
|
973 |
|
|
|
974 |
Rkns.Renderer.Scene.prototype.redraw = function() { |
|
|
975 |
Rkns._(this.representations).each(function(_representation) { |
|
|
976 |
_representation.redraw(); |
|
|
977 |
}); |
|
|
978 |
paper.view.draw(); |
|
|
979 |
} |
|
|
980 |
|
|
|
981 |
Rkns.Renderer.Scene.prototype.addTempEdge = function(_from, _point) { |
|
|
982 |
var _tmpEdge = this.addRepresentation("TempEdge",null); |
|
|
983 |
_tmpEdge.end_pos = _point; |
|
|
984 |
_tmpEdge.from_representation = _from; |
|
|
985 |
_tmpEdge.redraw(); |
|
|
986 |
this.click_target = _tmpEdge; |
|
|
987 |
} |
|
|
988 |
|
|
|
989 |
Rkns.Renderer.Scene.prototype.findTarget = function(_hitResult) { |
|
|
990 |
if (_hitResult && typeof _hitResult.item.__representation !== "undefined") { |
|
|
991 |
var _newTarget = _hitResult.item.__representation; |
|
|
992 |
if (this.selected_target !== _newTarget && _newTarget.model) { |
|
|
993 |
if (this.selected_target) { |
|
|
994 |
this.selected_target.model.trigger("unselect"); |
|
|
995 |
} |
|
|
996 |
_newTarget.model.trigger("select"); |
|
|
997 |
this.selected_target = _newTarget; |
|
|
998 |
} |
|
|
999 |
} else { |
|
|
1000 |
if (!_hitResult) { |
|
|
1001 |
this.removeRepresentationsOfType("tooltip"); |
|
|
1002 |
} |
|
|
1003 |
if (this.selected_target) { |
|
|
1004 |
this.selected_target.model.trigger("unselect"); |
|
|
1005 |
} |
|
|
1006 |
this.selected_target = null; |
|
|
1007 |
} |
|
|
1008 |
} |
|
|
1009 |
|
|
|
1010 |
Rkns.Renderer.Scene.prototype.onMouseMove = function(_event) { |
|
|
1011 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
1012 |
if (this.is_dragging) { |
|
|
1013 |
if (this.click_target && typeof this.click_target.paperShift === "function") { |
|
|
1014 |
this.click_target.paperShift(_event.delta); |
|
|
1015 |
} else { |
|
|
1016 |
this.offset = this.offset.add(_event.delta); |
|
|
1017 |
this.redraw(); |
|
|
1018 |
} |
|
|
1019 |
} else { |
|
|
1020 |
this.findTarget(_hitResult); |
|
|
1021 |
} |
|
|
1022 |
} |
|
|
1023 |
|
|
|
1024 |
Rkns.Renderer.Scene.prototype.onMouseDown = function(_event) { |
|
|
1025 |
this.is_dragging = false; |
|
|
1026 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
1027 |
if (_hitResult && typeof _hitResult.item.__representation !== "undefined") { |
|
|
1028 |
this.click_target = _hitResult.item.__representation; |
|
|
1029 |
} else { |
|
|
1030 |
this.click_target = null; |
|
|
1031 |
} |
|
|
1032 |
} |
|
|
1033 |
|
|
|
1034 |
Rkns.Renderer.Scene.prototype.onMouseDrag = function(_event) { |
|
|
1035 |
this.is_dragging = true; |
|
|
1036 |
this.onMouseMove(_event); |
|
|
1037 |
} |
|
|
1038 |
|
|
|
1039 |
Rkns.Renderer.Scene.prototype.onMouseUp = function(_event) { |
|
|
1040 |
if (this.click_target) { |
|
|
1041 |
var _off = this.canvas_$.offset(); |
|
|
1042 |
if (this.click_target.model) { |
|
|
1043 |
this.click_target.model.trigger("click"); |
|
|
1044 |
} |
|
|
1045 |
} |
|
|
1046 |
this.is_dragging = false; |
|
|
1047 |
this.click_target = null; |
|
|
1048 |
} |
|
|
1049 |
|
|
|
1050 |
Rkns.Renderer.Scene.prototype.onScroll = function(_event, _scrolldelta) { |
|
|
1051 |
this.totalScroll += _scrolldelta; |
|
|
1052 |
if (Math.abs(this.totalScroll) >= 1) { |
|
|
1053 |
var _off = this.canvas_$.offset(), |
|
|
1054 |
_delta = new paper.Point([ |
|
|
1055 |
_event.pageX - _off.left, |
|
|
1056 |
_event.pageY - _off.top |
|
|
1057 |
]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
|
|
1058 |
if (this.totalScroll > 0) { |
|
|
1059 |
this.offset = this.offset.subtract(_delta); |
|
|
1060 |
this.scale *= Math.SQRT2; |
|
|
1061 |
} else { |
|
|
1062 |
this.offset = this.offset.add(_delta.divide( Math.SQRT2 )); |
|
|
1063 |
this.scale *= Math.SQRT1_2; |
|
|
1064 |
} |
|
|
1065 |
this.totalScroll = 0; |
|
|
1066 |
this.redraw(); |
|
|
1067 |
} |
|
|
1068 |
} |
|
955
|
1069 |
|
|
|
1070 |
Rkns.Renderer.Scene.prototype.destroy = function() { |
|
|
1071 |
this.project.off("add:nodes", this._addNodesBinding ); |
|
|
1072 |
this.project.off("add:edges", this._addEdgesBinding ); |
|
|
1073 |
Rkns._(this.representations).each(function(_repr) { |
|
|
1074 |
_repr.destroy(); |
|
|
1075 |
}); |
|
|
1076 |
this.$.html(""); |
|
|
1077 |
paper.remove(); |
|
|
1078 |
} |
|
|
1079 |
|