| author | ymh <ymh.work@gmail.com> |
| Wed, 03 Jun 2015 13:16:46 +0200 | |
| changeset 459 | 98cae534083d |
| parent 458 | 423bdf56d103 |
| child 461 | 48235ed6b07d |
| permissions | -rw-r--r-- |
| 284 | 1 |
|
2 |
define(['jquery', 'underscore', 'requtils', 'renderer/baserepresentation'], function ($, _, requtils, BaseRepresentation) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
3 |
'use strict'; |
|
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
4 |
|
| 284 | 5 |
var Utils = requtils.getUtils(); |
6 |
||
7 |
/* Edge Class Begin */ |
|
8 |
||
9 |
//var Edge = Renderer.Edge = Utils.inherit(Renderer._BaseRepresentation); |
|
10 |
var Edge = Utils.inherit(BaseRepresentation); |
|
11 |
||
12 |
_(Edge.prototype).extend({ |
|
13 |
_init: function() { |
|
14 |
this.renderer.edge_layer.activate(); |
|
15 |
this.type = "Edge"; |
|
16 |
this.from_representation = this.renderer.getRepresentationByModel(this.model.get("from")); |
|
17 |
this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to")); |
|
18 |
this.bundle = this.renderer.addToBundles(this); |
|
19 |
this.line = new paper.Path(); |
|
20 |
this.line.add([0,0],[0,0],[0,0]); |
|
21 |
this.line.__representation = this; |
|
22 |
this.line.strokeWidth = this.options.edge_stroke_width; |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
23 |
this.arrow_scale = 1; |
| 284 | 24 |
this.arrow = new paper.Path(); |
25 |
this.arrow.add( |
|
26 |
[ 0, 0 ], |
|
27 |
[ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], |
|
28 |
[ 0, this.options.edge_arrow_width ] |
|
29 |
); |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
30 |
this.arrow.pivot = new paper.Point([ this.options.edge_arrow_length / 2, this.options.edge_arrow_width / 2 ]); |
| 284 | 31 |
this.arrow.__representation = this; |
32 |
this.text = $('<div class="Rk-Label Rk-Edge-Label">').appendTo(this.renderer.labels_$); |
|
33 |
this.arrow_angle = 0; |
|
34 |
if (this.options.editor_mode) { |
|
35 |
var Renderer = requtils.getRenderer(); |
|
36 |
this.normal_buttons = [ |
|
37 |
new Renderer.EdgeEditButton(this.renderer, null), |
|
38 |
new Renderer.EdgeRemoveButton(this.renderer, null) |
|
39 |
]; |
|
40 |
this.pending_delete_buttons = [ |
|
41 |
new Renderer.EdgeRevertButton(this.renderer, null) |
|
42 |
]; |
|
43 |
this.all_buttons = this.normal_buttons.concat(this.pending_delete_buttons); |
|
44 |
for (var i = 0; i < this.all_buttons.length; i++) { |
|
45 |
this.all_buttons[i].source_representation = this; |
|
46 |
} |
|
47 |
this.active_buttons = []; |
|
48 |
} else { |
|
49 |
this.active_buttons = this.all_buttons = []; |
|
50 |
} |
|
51 |
||
52 |
if (this.renderer.minimap) { |
|
53 |
this.renderer.minimap.edge_layer.activate(); |
|
54 |
this.minimap_line = new paper.Path(); |
|
55 |
this.minimap_line.add([0,0],[0,0]); |
|
56 |
this.minimap_line.__representation = this.renderer.minimap.miniframe.__representation; |
|
57 |
this.minimap_line.strokeWidth = 1; |
|
58 |
} |
|
59 |
}, |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
60 |
_getStrokeWidth: function() { |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
61 |
var thickness = (this.model.has('style') && this.model.get('style').thickness) || 1; |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
62 |
return this.options.edge_stroke_width + (thickness-1) * (this.options.edge_stroke_max_width - this.options.edge_stroke_width) / (this.options.edge_stroke_witdh_scale-1); |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
63 |
}, |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
64 |
_getSelectedStrokeWidth: function() { |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
65 |
var thickness = (this.model.has('style') && this.model.get('style').thickness) || 1; |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
66 |
return this.options.selected_edge_stroke_width + (thickness-1) * (this.options.selected_edge_stroke_max_width - this.options.selected_edge_stroke_width) / (this.options.edge_stroke_witdh_scale-1); |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
67 |
}, |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
68 |
_getArrowScale: function() { |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
69 |
var thickness = (this.model.has('style') && this.model.get('style').thickness) || 1; |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
70 |
return 1 + (thickness-1) * ((this.options.edge_arrow_max_width / this.options.edge_arrow_width) - 1) / (this.options.edge_stroke_witdh_scale-1); |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
71 |
}, |
| 284 | 72 |
redraw: function() { |
73 |
var from = this.model.get("from"), |
|
74 |
to = this.model.get("to"); |
|
75 |
if (!from || !to) { |
|
76 |
return; |
|
77 |
} |
|
78 |
this.from_representation = this.renderer.getRepresentationByModel(from); |
|
79 |
this.to_representation = this.renderer.getRepresentationByModel(to); |
|
80 |
if (typeof this.from_representation === "undefined" || typeof this.to_representation === "undefined") { |
|
81 |
return; |
|
82 |
} |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
83 |
var _strokeWidth = this._getStrokeWidth(), |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
84 |
_arrow_scale = this._getArrowScale(), |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
85 |
_p0a = this.from_representation.paper_coords, |
|
458
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
86 |
_p1a = this.to_representation.paper_coords, |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
87 |
_v = _p1a.subtract(_p0a), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
88 |
_r = _v.length, |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
89 |
_u = _v.divide(_r), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
90 |
_ortho = new paper.Point([- _u.y, _u.x]), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
91 |
_group_pos = this.bundle.getPosition(this), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
92 |
_delta = _ortho.multiply( this.options.edge_gap_in_bundles * _group_pos ), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
93 |
_p0b = _p0a.add(_delta), /* Adding a 4 px difference */ |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
94 |
_p1b = _p1a.add(_delta), /* to differentiate bundled links */ |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
95 |
_a = _v.angle, |
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
96 |
_textdelta = _ortho.multiply(this.options.edge_label_distance + 0.5 * _arrow_scale * this.options.edge_arrow_width), |
|
458
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
97 |
_handle = _v.divide(3), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
98 |
_color = (this.model.has("style") && this.model.get("style").color) || (this.model.get("created_by") || Utils._USER_PLACEHOLDER(this.renkan)).get("color"), |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
99 |
_dash = (this.model.has("style") && this.model.get("style").dash) ? this.options.default_dash_array : null, |
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
100 |
opacity = 1; |
| 284 | 101 |
|
102 |
if (this.model.get("delete_scheduled") || this.from_representation.model.get("delete_scheduled") || this.to_representation.model.get("delete_scheduled")) { |
|
|
293
fba23fde14ba
Correct jshint errors and force it on build
ymh <ymh.work@gmail.com>
parents:
284
diff
changeset
|
103 |
opacity = 0.5; |
| 284 | 104 |
this.line.dashArray = [2, 2]; |
105 |
} else { |
|
106 |
opacity = 1; |
|
107 |
this.line.dashArray = null; |
|
108 |
} |
|
109 |
||
110 |
var old_act_btn = this.active_buttons; |
|
111 |
||
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
112 |
var array_opacity = |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
113 |
(this.model.has("style") && this.model.get("style").arrow) || !this.model.has("style") || (typeof this.model.get("style").arrow === 'undefined') ? opacity : 0; |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
114 |
|
| 284 | 115 |
this.active_buttons = this.model.get("delete_scheduled") ? this.pending_delete_buttons : this.normal_buttons; |
116 |
||
117 |
if (this.selected && this.renderer.isEditable() && old_act_btn !== this.active_buttons) { |
|
118 |
old_act_btn.forEach(function(b) { |
|
119 |
b.hide(); |
|
120 |
}); |
|
121 |
this.active_buttons.forEach(function(b) { |
|
122 |
b.show(); |
|
123 |
}); |
|
124 |
} |
|
125 |
||
|
458
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
126 |
|
|
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
127 |
|
| 284 | 128 |
this.paper_coords = _p0b.add(_p1b).divide(2); |
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
129 |
this.line.strokeWidth = _strokeWidth; |
| 284 | 130 |
this.line.strokeColor = _color; |
|
458
423bdf56d103
migrated to style, added dash style to client + small refactoring for shapes + triangle
ymh <ymh.work@gmail.com>
parents:
434
diff
changeset
|
131 |
this.line.dashArray = _dash; |
| 284 | 132 |
this.line.opacity = opacity; |
133 |
this.line.segments[0].point = _p0a; |
|
134 |
this.line.segments[1].point = this.paper_coords; |
|
135 |
this.line.segments[1].handleIn = _handle.multiply(-1); |
|
136 |
this.line.segments[1].handleOut = _handle; |
|
137 |
this.line.segments[2].point = _p1a; |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
138 |
this.arrow.scale(_arrow_scale / this.arrow_scale); |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
139 |
this.arrow_scale = _arrow_scale; |
| 284 | 140 |
this.arrow.fillColor = _color; |
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
141 |
this.arrow.opacity = array_opacity; |
|
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
142 |
this.arrow.rotate(_a - this.arrow_angle, this.arrow.bounds.center); |
| 284 | 143 |
this.arrow.position = this.paper_coords; |
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
144 |
|
| 284 | 145 |
this.arrow_angle = _a; |
146 |
if (_a > 90) { |
|
147 |
_a -= 180; |
|
148 |
_textdelta = _textdelta.multiply(-1); |
|
149 |
} |
|
150 |
if (_a < -90) { |
|
151 |
_a += 180; |
|
152 |
_textdelta = _textdelta.multiply(-1); |
|
153 |
} |
|
154 |
var _text = this.model.get("title") || this.renkan.translate(this.options.label_untitled_edges) || ""; |
|
155 |
_text = Utils.shortenText(_text, this.options.node_label_max_length); |
|
156 |
this.text.text(_text); |
|
157 |
var _textpos = this.paper_coords.add(_textdelta); |
|
158 |
this.text.css({ |
|
159 |
left: _textpos.x, |
|
160 |
top: _textpos.y, |
|
161 |
transform: "rotate(" + _a + "deg)", |
|
162 |
"-moz-transform": "rotate(" + _a + "deg)", |
|
163 |
"-webkit-transform": "rotate(" + _a + "deg)", |
|
164 |
opacity: opacity |
|
165 |
}); |
|
166 |
this.text_angle = _a; |
|
167 |
||
168 |
var _pc = this.paper_coords; |
|
169 |
this.all_buttons.forEach(function(b) { |
|
170 |
b.moveTo(_pc); |
|
171 |
}); |
|
172 |
||
173 |
if (this.renderer.minimap) { |
|
174 |
this.minimap_line.strokeColor = _color; |
|
175 |
this.minimap_line.segments[0].point = this.renderer.toMinimapCoords(new paper.Point(this.from_representation.model.get("position"))); |
|
176 |
this.minimap_line.segments[1].point = this.renderer.toMinimapCoords(new paper.Point(this.to_representation.model.get("position"))); |
|
177 |
} |
|
178 |
}, |
|
179 |
openEditor: function() { |
|
180 |
this.renderer.removeRepresentationsOfType("editor"); |
|
181 |
var _editor = this.renderer.addRepresentation("EdgeEditor",null); |
|
182 |
_editor.source_representation = this; |
|
183 |
_editor.draw(); |
|
184 |
}, |
|
185 |
select: function() { |
|
186 |
this.selected = true; |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
187 |
this.line.strokeWidth = this._getSelectedStrokeWidth(); |
| 284 | 188 |
if (this.renderer.isEditable()) { |
189 |
this.active_buttons.forEach(function(b) { |
|
190 |
b.show(); |
|
191 |
}); |
|
192 |
} |
|
193 |
if (!this.options.editor_mode) { |
|
194 |
this.openEditor(); |
|
195 |
} |
|
196 |
this._super("select"); |
|
197 |
}, |
|
198 |
unselect: function(_newTarget) { |
|
199 |
if (!_newTarget || _newTarget.source_representation !== this) { |
|
200 |
this.selected = false; |
|
201 |
if (this.options.editor_mode) { |
|
202 |
this.all_buttons.forEach(function(b) { |
|
203 |
b.hide(); |
|
204 |
}); |
|
205 |
} |
|
|
459
98cae534083d
add node and edge stroke width + adjust text + arrow placement + conrol arrow visibility
ymh <ymh.work@gmail.com>
parents:
458
diff
changeset
|
206 |
this.line.strokeWidth = this._getStrokeWidth(); |
| 284 | 207 |
this._super("unselect"); |
208 |
} |
|
209 |
}, |
|
210 |
mousedown: function(_event, _isTouch) { |
|
211 |
if (_isTouch) { |
|
212 |
this.renderer.unselectAll(); |
|
213 |
this.select(); |
|
214 |
} |
|
215 |
}, |
|
216 |
mouseup: function(_event, _isTouch) { |
|
217 |
if (!this.renkan.read_only && this.renderer.is_dragging) { |
|
218 |
this.from_representation.saveCoords(); |
|
219 |
this.to_representation.saveCoords(); |
|
220 |
this.from_representation.is_dragging = false; |
|
221 |
this.to_representation.is_dragging = false; |
|
222 |
} else { |
|
223 |
if (!_isTouch) { |
|
224 |
this.openEditor(); |
|
225 |
} |
|
226 |
this.model.trigger("clicked"); |
|
227 |
} |
|
228 |
this.renderer.click_target = null; |
|
229 |
this.renderer.is_dragging = false; |
|
230 |
}, |
|
231 |
paperShift: function(_delta) { |
|
232 |
if (this.options.editor_mode) { |
|
233 |
if (!this.options.read_only) { |
|
234 |
this.from_representation.paperShift(_delta); |
|
235 |
this.to_representation.paperShift(_delta); |
|
236 |
} |
|
237 |
} else { |
|
238 |
this.renderer.paperShift(_delta); |
|
239 |
} |
|
240 |
}, |
|
241 |
destroy: function() { |
|
242 |
this._super("destroy"); |
|
243 |
this.line.remove(); |
|
244 |
this.arrow.remove(); |
|
245 |
this.text.remove(); |
|
246 |
if (this.renderer.minimap) { |
|
247 |
this.minimap_line.remove(); |
|
248 |
} |
|
249 |
this.all_buttons.forEach(function(b) { |
|
250 |
b.destroy(); |
|
251 |
}); |
|
252 |
var _this = this; |
|
| 433 | 253 |
this.bundle.edges = _.reject(this.bundle.edges, function(_edge) { |
| 284 | 254 |
return _this === _edge; |
255 |
}); |
|
256 |
} |
|
| 433 | 257 |
}).value(); |
| 284 | 258 |
|
259 |
return Edge; |
|
260 |
||
261 |
}); |