| author | ymh <ymh.work@gmail.com> |
| Sat, 25 Apr 2015 04:13:53 +0200 | |
| changeset 433 | e457ec945e50 |
| parent 403 | 96781c1a8bbe |
| child 434 | 0d5998b32a7c |
| permissions | -rw-r--r-- |
| 326 | 1 |
define(['jquery', 'underscore', 'requtils', 'renderer/baserepresentation', 'renderer/shapebuilder'], function ($, _, requtils, BaseRepresentation, ShapeBuilder) { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
2 |
'use strict'; |
|
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
3 |
|
| 284 | 4 |
var Utils = requtils.getUtils(); |
5 |
||
6 |
/* Rkns.Renderer.Node Class */ |
|
7 |
||
8 |
/* The representation for the node : A circle, with an image inside and a text label underneath. |
|
9 |
* The circle and the image are drawn on canvas and managed by Paper.js. |
|
10 |
* The text label is an HTML node, managed by jQuery. */ |
|
11 |
||
12 |
//var NodeRepr = Renderer.Node = Utils.inherit(Renderer._BaseRepresentation); |
|
13 |
var NodeRepr = Utils.inherit(BaseRepresentation); |
|
14 |
||
15 |
_(NodeRepr.prototype).extend({ |
|
16 |
_init: function() { |
|
17 |
this.renderer.node_layer.activate(); |
|
18 |
this.type = "Node"; |
|
| 330 | 19 |
this.buildShape(); |
| 284 | 20 |
if (this.options.show_node_circles) { |
21 |
this.circle.strokeWidth = this.options.node_stroke_width; |
|
22 |
this.h_ratio = 1; |
|
23 |
} else { |
|
24 |
this.h_ratio = 0; |
|
25 |
} |
|
26 |
this.title = $('<div class="Rk-Label">').appendTo(this.renderer.labels_$); |
|
| 396 | 27 |
|
| 284 | 28 |
if (this.options.editor_mode) { |
29 |
var Renderer = requtils.getRenderer(); |
|
30 |
this.normal_buttons = [ |
|
31 |
new Renderer.NodeEditButton(this.renderer, null), |
|
32 |
new Renderer.NodeRemoveButton(this.renderer, null), |
|
33 |
new Renderer.NodeLinkButton(this.renderer, null), |
|
34 |
new Renderer.NodeEnlargeButton(this.renderer, null), |
|
35 |
new Renderer.NodeShrinkButton(this.renderer, null) |
|
36 |
]; |
|
37 |
this.pending_delete_buttons = [ |
|
38 |
new Renderer.NodeRevertButton(this.renderer, null) |
|
39 |
]; |
|
40 |
this.all_buttons = this.normal_buttons.concat(this.pending_delete_buttons); |
|
| 396 | 41 |
|
| 284 | 42 |
for (var i = 0; i < this.all_buttons.length; i++) { |
43 |
this.all_buttons[i].source_representation = this; |
|
44 |
} |
|
45 |
this.active_buttons = []; |
|
46 |
} else { |
|
47 |
this.active_buttons = this.all_buttons = []; |
|
48 |
} |
|
49 |
this.last_circle_radius = 1; |
|
50 |
||
51 |
if (this.renderer.minimap) { |
|
52 |
this.renderer.minimap.node_layer.activate(); |
|
53 |
this.minimap_circle = new paper.Path.Circle([0, 0], 1); |
|
54 |
this.minimap_circle.__representation = this.renderer.minimap.miniframe.__representation; |
|
55 |
this.renderer.minimap.node_group.addChild(this.minimap_circle); |
|
56 |
} |
|
57 |
}, |
|
| 330 | 58 |
buildShape: function(){ |
59 |
if(typeof this.model.get("shape_changed")!=="undefined" && this.model.get("shape_changed")===true){ |
|
60 |
this.model.set("shape_changed", false); |
|
| 331 | 61 |
delete this.img; |
| 330 | 62 |
} |
63 |
if(this.circle){ |
|
64 |
this.circle.remove(); |
|
65 |
delete this.circle; |
|
66 |
} |
|
67 |
// "circle" "rectangle" "ellipse" "polygon" "star" "diamond" |
|
68 |
this.shapeBuilder = new ShapeBuilder(this.model.get("shape")); |
|
69 |
this.circle = this.shapeBuilder.getShape(); |
|
70 |
this.circle.__representation = this; |
|
71 |
this.last_circle_radius = 1; |
|
72 |
}, |
|
| 284 | 73 |
redraw: function(_dontRedrawEdges) { |
| 330 | 74 |
if(typeof this.model.get("shape_changed")!=="undefined" && this.model.get("shape_changed")===true){ |
75 |
this.buildShape(); |
|
76 |
} |
|
| 284 | 77 |
var _model_coords = new paper.Point(this.model.get("position")), |
78 |
_baseRadius = this.options.node_size_base * Math.exp((this.model.get("size") || 0) * Utils._NODE_SIZE_STEP); |
|
79 |
if (!this.is_dragging || !this.paper_coords) { |
|
80 |
this.paper_coords = this.renderer.toPaperCoords(_model_coords); |
|
81 |
} |
|
82 |
this.circle_radius = _baseRadius * this.renderer.scale; |
|
83 |
if (this.last_circle_radius !== this.circle_radius) { |
|
84 |
this.all_buttons.forEach(function(b) { |
|
85 |
b.setSectorSize(); |
|
86 |
}); |
|
87 |
this.circle.scale(this.circle_radius / this.last_circle_radius); |
|
88 |
if (this.node_image) { |
|
89 |
this.node_image.scale(this.circle_radius / this.last_circle_radius); |
|
90 |
} |
|
91 |
} |
|
92 |
this.circle.position = this.paper_coords; |
|
93 |
if (this.node_image) { |
|
94 |
this.node_image.position = this.paper_coords.subtract(this.image_delta.multiply(this.circle_radius)); |
|
95 |
} |
|
96 |
this.last_circle_radius = this.circle_radius; |
|
97 |
||
98 |
var old_act_btn = this.active_buttons; |
|
99 |
||
100 |
var opacity = 1; |
|
101 |
if (this.model.get("delete_scheduled")) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
102 |
opacity = 0.5; |
| 284 | 103 |
this.active_buttons = this.pending_delete_buttons; |
104 |
this.circle.dashArray = [2,2]; |
|
105 |
} else { |
|
106 |
opacity = 1; |
|
107 |
this.active_buttons = this.normal_buttons; |
|
108 |
this.circle.dashArray = null; |
|
109 |
} |
|
110 |
||
111 |
if (this.selected && this.renderer.isEditable()) { |
|
112 |
if (old_act_btn !== this.active_buttons) { |
|
113 |
old_act_btn.forEach(function(b) { |
|
114 |
b.hide(); |
|
115 |
}); |
|
116 |
} |
|
117 |
this.active_buttons.forEach(function(b) { |
|
118 |
b.show(); |
|
119 |
}); |
|
120 |
} |
|
121 |
||
122 |
if (this.node_image) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
123 |
this.node_image.opacity = this.highlighted ? opacity * 0.5 : (opacity - 0.01); |
| 284 | 124 |
} |
125 |
||
126 |
this.circle.fillColor = this.highlighted ? this.options.highlighted_node_fill_color : this.options.node_fill_color; |
|
127 |
||
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
128 |
this.circle.opacity = this.options.show_node_circles ? opacity : 0.01; |
| 284 | 129 |
|
130 |
var _text = this.model.get("title") || this.renkan.translate(this.options.label_untitled_nodes) || ""; |
|
131 |
_text = Utils.shortenText(_text, this.options.node_label_max_length); |
|
132 |
||
133 |
if (typeof this.highlighted === "object") { |
|
134 |
this.title.html(this.highlighted.replace(_(_text).escape(),'<span class="Rk-Highlighted">$1</span>')); |
|
135 |
} else { |
|
136 |
this.title.text(_text); |
|
137 |
} |
|
138 |
||
139 |
this.title.css({ |
|
140 |
left: this.paper_coords.x, |
|
141 |
top: this.paper_coords.y + this.circle_radius * this.h_ratio + this.options.node_label_distance, |
|
142 |
opacity: opacity |
|
143 |
}); |
|
144 |
var _color = this.model.get("color") || (this.model.get("created_by") || Utils._USER_PLACEHOLDER(this.renkan)).get("color"); |
|
145 |
this.circle.strokeColor = _color; |
|
146 |
var _pc = this.paper_coords; |
|
147 |
this.all_buttons.forEach(function(b) { |
|
148 |
b.moveTo(_pc); |
|
149 |
}); |
|
150 |
var lastImage = this.img; |
|
151 |
this.img = this.model.get("image"); |
|
152 |
if (this.img && this.img !== lastImage) { |
|
153 |
this.showImage(); |
|
154 |
} |
|
155 |
if (this.node_image && !this.img) { |
|
156 |
this.node_image.remove(); |
|
157 |
delete this.node_image; |
|
158 |
} |
|
159 |
||
160 |
if (this.renderer.minimap) { |
|
161 |
this.minimap_circle.fillColor = _color; |
|
162 |
var minipos = this.renderer.toMinimapCoords(_model_coords), |
|
163 |
miniradius = this.renderer.minimap.scale * _baseRadius, |
|
164 |
minisize = new paper.Size([miniradius, miniradius]); |
|
165 |
this.minimap_circle.fitBounds(minipos.subtract(minisize), minisize.multiply(2)); |
|
166 |
} |
|
167 |
||
168 |
if (!_dontRedrawEdges) { |
|
169 |
var _this = this; |
|
170 |
_.each( |
|
171 |
this.project.get("edges").filter( |
|
172 |
function (ed) { |
|
173 |
return ((ed.get("to") === _this.model) || (ed.get("from") === _this.model)); |
|
174 |
} |
|
175 |
), |
|
176 |
function(edge, index, list) { |
|
177 |
var repr = _this.renderer.getRepresentationByModel(edge); |
|
178 |
if (repr && typeof repr.from_representation !== "undefined" && typeof repr.from_representation.paper_coords !== "undefined" && typeof repr.to_representation !== "undefined" && typeof repr.to_representation.paper_coords !== "undefined") { |
|
179 |
repr.redraw(); |
|
180 |
} |
|
181 |
} |
|
182 |
); |
|
183 |
} |
|
184 |
||
185 |
}, |
|
186 |
showImage: function() { |
|
187 |
var _image = null; |
|
188 |
if (typeof this.renderer.image_cache[this.img] === "undefined") { |
|
189 |
_image = new Image(); |
|
190 |
this.renderer.image_cache[this.img] = _image; |
|
191 |
_image.src = this.img; |
|
192 |
} else { |
|
193 |
_image = this.renderer.image_cache[this.img]; |
|
194 |
} |
|
195 |
if (_image.width) { |
|
196 |
if (this.node_image) { |
|
197 |
this.node_image.remove(); |
|
198 |
} |
|
199 |
this.renderer.node_layer.activate(); |
|
200 |
var width = _image.width, |
|
|
403
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
201 |
height = _image.height, |
|
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
202 |
clipPath = this.model.get("clip_path"), |
|
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
203 |
hasClipPath = (typeof clipPath !== "undefined" && clipPath), |
|
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
204 |
_clip = null, |
|
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
205 |
baseRadius = null, |
|
96781c1a8bbe
correct resizing problems especially in next firefox
ymh <ymh.work@gmail.com>
parents:
396
diff
changeset
|
206 |
centerPoint = null; |
| 396 | 207 |
|
| 284 | 208 |
if (hasClipPath) { |
209 |
_clip = new paper.Path(); |
|
210 |
var instructions = clipPath.match(/[a-z][^a-z]+/gi) || [], |
|
211 |
lastCoords = [0,0], |
|
212 |
minX = Infinity, |
|
213 |
minY = Infinity, |
|
214 |
maxX = -Infinity, |
|
215 |
maxY = -Infinity; |
|
216 |
||
217 |
var transformCoords = function(tabc, relative) { |
|
218 |
var newCoords = tabc.slice(1).map(function(v, k) { |
|
219 |
var res = parseFloat(v), |
|
220 |
isY = k % 2; |
|
221 |
if (isY) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
222 |
res = ( res - 0.5 ) * height; |
| 284 | 223 |
} else { |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
224 |
res = ( res - 0.5 ) * width; |
| 284 | 225 |
} |
226 |
if (relative) { |
|
227 |
res += lastCoords[isY]; |
|
228 |
} |
|
229 |
if (isY) { |
|
230 |
minY = Math.min(minY, res); |
|
231 |
maxY = Math.max(maxY, res); |
|
232 |
} else { |
|
233 |
minX = Math.min(minX, res); |
|
234 |
maxX = Math.max(maxX, res); |
|
235 |
} |
|
236 |
return res; |
|
237 |
}); |
|
238 |
lastCoords = newCoords.slice(-2); |
|
239 |
return newCoords; |
|
240 |
}; |
|
241 |
||
242 |
instructions.forEach(function(instr) { |
|
243 |
var coords = instr.match(/([a-z]|[0-9.-]+)/ig) || [""]; |
|
244 |
switch(coords[0]) { |
|
245 |
case "M": |
|
246 |
_clip.moveTo(transformCoords(coords)); |
|
247 |
break; |
|
248 |
case "m": |
|
249 |
_clip.moveTo(transformCoords(coords, true)); |
|
250 |
break; |
|
251 |
case "L": |
|
252 |
_clip.lineTo(transformCoords(coords)); |
|
253 |
break; |
|
254 |
case "l": |
|
255 |
_clip.lineTo(transformCoords(coords, true)); |
|
256 |
break; |
|
257 |
case "C": |
|
258 |
_clip.cubicCurveTo(transformCoords(coords)); |
|
259 |
break; |
|
260 |
case "c": |
|
261 |
_clip.cubicCurveTo(transformCoords(coords, true)); |
|
262 |
break; |
|
263 |
case "Q": |
|
264 |
_clip.quadraticCurveTo(transformCoords(coords)); |
|
265 |
break; |
|
266 |
case "q": |
|
267 |
_clip.quadraticCurveTo(transformCoords(coords, true)); |
|
268 |
break; |
|
269 |
} |
|
270 |
}); |
|
271 |
||
272 |
baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](maxX - minX, maxY - minY) / 2; |
|
273 |
centerPoint = new paper.Point((maxX + minX) / 2, (maxY + minY) / 2); |
|
274 |
if (!this.options.show_node_circles) { |
|
275 |
this.h_ratio = (maxY - minY) / (2 * baseRadius); |
|
276 |
} |
|
277 |
} else { |
|
278 |
baseRadius = Math[this.options.node_images_fill_mode ? "min" : "max"](width, height) / 2; |
|
279 |
centerPoint = new paper.Point(0,0); |
|
280 |
if (!this.options.show_node_circles) { |
|
281 |
this.h_ratio = height / (2 * baseRadius); |
|
282 |
} |
|
283 |
} |
|
284 |
var _raster = new paper.Raster(_image); |
|
285 |
_raster.locked = true; // Disable mouse events on icon |
|
286 |
if (hasClipPath) { |
|
287 |
_raster = new paper.Group(_clip, _raster); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
288 |
_raster.opacity = 0.99; |
| 284 | 289 |
/* This is a workaround to allow clipping at group level |
290 |
* If opacity was set to 1, paper.js would merge all clipping groups in one (known bug). |
|
291 |
*/ |
|
292 |
_raster.clipped = true; |
|
293 |
_clip.__representation = this; |
|
294 |
} |
|
295 |
if (this.options.clip_node_images) { |
|
| 326 | 296 |
var _circleClip = this.shapeBuilder.getImageShape(centerPoint, baseRadius); |
| 284 | 297 |
_raster = new paper.Group(_circleClip, _raster); |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
298 |
_raster.opacity = 0.99; |
| 284 | 299 |
_raster.clipped = true; |
300 |
_circleClip.__representation = this; |
|
301 |
} |
|
302 |
this.image_delta = centerPoint.divide(baseRadius); |
|
303 |
this.node_image = _raster; |
|
304 |
this.node_image.__representation = _this; |
|
305 |
this.node_image.scale(this.circle_radius / baseRadius); |
|
306 |
this.node_image.position = this.paper_coords.subtract(this.image_delta.multiply(this.circle_radius)); |
|
307 |
this.redraw(); |
|
308 |
this.renderer.throttledPaperDraw(); |
|
309 |
} else { |
|
310 |
var _this = this; |
|
311 |
$(_image).on("load", function() { |
|
312 |
_this.showImage(); |
|
313 |
}); |
|
314 |
} |
|
315 |
}, |
|
316 |
paperShift: function(_delta) { |
|
317 |
if (this.options.editor_mode) { |
|
318 |
if (!this.renkan.read_only) { |
|
319 |
this.is_dragging = true; |
|
320 |
this.paper_coords = this.paper_coords.add(_delta); |
|
321 |
this.redraw(); |
|
322 |
} |
|
323 |
} else { |
|
324 |
this.renderer.paperShift(_delta); |
|
325 |
} |
|
326 |
}, |
|
327 |
openEditor: function() { |
|
328 |
this.renderer.removeRepresentationsOfType("editor"); |
|
329 |
var _editor = this.renderer.addRepresentation("NodeEditor",null); |
|
330 |
_editor.source_representation = this; |
|
331 |
_editor.draw(); |
|
332 |
}, |
|
333 |
select: function() { |
|
334 |
this.selected = true; |
|
335 |
this.circle.strokeWidth = this.options.selected_node_stroke_width; |
|
336 |
if (this.renderer.isEditable()) { |
|
337 |
this.active_buttons.forEach(function(b) { |
|
338 |
b.show(); |
|
339 |
}); |
|
340 |
} |
|
341 |
var _uri = this.model.get("uri"); |
|
342 |
if (_uri) { |
|
343 |
$('.Rk-Bin-Item').each(function() { |
|
344 |
var _el = $(this); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
345 |
if (_el.attr("data-uri") === _uri) { |
| 284 | 346 |
_el.addClass("selected"); |
347 |
} |
|
348 |
}); |
|
349 |
} |
|
350 |
if (!this.options.editor_mode) { |
|
351 |
this.openEditor(); |
|
352 |
} |
|
353 |
||
354 |
if (this.renderer.minimap) { |
|
355 |
this.minimap_circle.strokeWidth = this.options.minimap_highlight_weight; |
|
356 |
this.minimap_circle.strokeColor = this.options.minimap_highlight_color; |
|
357 |
} |
|
358 |
this._super("select"); |
|
359 |
}, |
|
| 396 | 360 |
hideButtons: function() { |
361 |
this.all_buttons.forEach(function(b) { |
|
362 |
b.hide(); |
|
363 |
}); |
|
364 |
delete(this.buttonTimeout); |
|
365 |
}, |
|
| 284 | 366 |
unselect: function(_newTarget) { |
367 |
if (!_newTarget || _newTarget.source_representation !== this) { |
|
368 |
this.selected = false; |
|
| 396 | 369 |
var _this = this; |
370 |
this.buttons_timeout = setTimeout(function() { _this.hideButtons(); }, 200); |
|
| 284 | 371 |
this.circle.strokeWidth = this.options.node_stroke_width; |
372 |
$('.Rk-Bin-Item').removeClass("selected"); |
|
373 |
if (this.renderer.minimap) { |
|
374 |
this.minimap_circle.strokeColor = undefined; |
|
375 |
} |
|
376 |
this._super("unselect"); |
|
377 |
} |
|
378 |
}, |
|
379 |
highlight: function(textToReplace) { |
|
380 |
var hlvalue = textToReplace || true; |
|
381 |
if (this.highlighted === hlvalue) { |
|
382 |
return; |
|
383 |
} |
|
384 |
this.highlighted = hlvalue; |
|
385 |
this.redraw(); |
|
386 |
this.renderer.throttledPaperDraw(); |
|
387 |
}, |
|
388 |
unhighlight: function() { |
|
389 |
if (!this.highlighted) { |
|
390 |
return; |
|
391 |
} |
|
392 |
this.highlighted = false; |
|
393 |
this.redraw(); |
|
394 |
this.renderer.throttledPaperDraw(); |
|
395 |
}, |
|
396 |
saveCoords: function() { |
|
397 |
var _coords = this.renderer.toModelCoords(this.paper_coords), |
|
398 |
_data = { |
|
399 |
position: { |
|
400 |
x: _coords.x, |
|
401 |
y: _coords.y |
|
402 |
} |
|
403 |
}; |
|
404 |
if (this.renderer.isEditable()) { |
|
405 |
this.model.set(_data); |
|
406 |
} |
|
407 |
}, |
|
408 |
mousedown: function(_event, _isTouch) { |
|
409 |
if (_isTouch) { |
|
410 |
this.renderer.unselectAll(); |
|
411 |
this.select(); |
|
412 |
} |
|
413 |
}, |
|
414 |
mouseup: function(_event, _isTouch) { |
|
415 |
if (this.renderer.is_dragging && this.renderer.isEditable()) { |
|
416 |
this.saveCoords(); |
|
417 |
} else { |
|
418 |
if (!_isTouch && !this.model.get("delete_scheduled")) { |
|
419 |
this.openEditor(); |
|
420 |
} |
|
421 |
this.model.trigger("clicked"); |
|
422 |
} |
|
423 |
this.renderer.click_target = null; |
|
424 |
this.renderer.is_dragging = false; |
|
425 |
this.is_dragging = false; |
|
426 |
}, |
|
427 |
destroy: function(_event) { |
|
428 |
this._super("destroy"); |
|
429 |
this.all_buttons.forEach(function(b) { |
|
430 |
b.destroy(); |
|
431 |
}); |
|
432 |
this.circle.remove(); |
|
433 |
this.title.remove(); |
|
434 |
if (this.renderer.minimap) { |
|
435 |
this.minimap_circle.remove(); |
|
436 |
} |
|
437 |
if (this.node_image) { |
|
438 |
this.node_image.remove(); |
|
439 |
} |
|
440 |
} |
|
| 433 | 441 |
}).value(); |
442 |
// }); |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
443 |
|
| 284 | 444 |
return NodeRepr; |
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
445 |
|
| 284 | 446 |
}); |