| author | rougeronj |
| Fri, 22 May 2015 17:48:18 +0200 | |
| changeset 453 | 04b7d46e9d67 |
| parent 450 | 88e8673aaeeb |
| 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"; |
|
| 450 | 16 |
this.hidden = false; |
17 |
this.ghost = false; |
|
| 284 | 18 |
this.from_representation = this.renderer.getRepresentationByModel(this.model.get("from")); |
19 |
this.to_representation = this.renderer.getRepresentationByModel(this.model.get("to")); |
|
20 |
this.bundle = this.renderer.addToBundles(this); |
|
21 |
this.line = new paper.Path(); |
|
22 |
this.line.add([0,0],[0,0],[0,0]); |
|
23 |
this.line.__representation = this; |
|
24 |
this.line.strokeWidth = this.options.edge_stroke_width; |
|
25 |
this.arrow = new paper.Path(); |
|
26 |
this.arrow.add( |
|
27 |
[ 0, 0 ], |
|
28 |
[ this.options.edge_arrow_length, this.options.edge_arrow_width / 2 ], |
|
29 |
[ 0, this.options.edge_arrow_width ] |
|
30 |
); |
|
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 |
}, |
|
60 |
redraw: function() { |
|
61 |
var from = this.model.get("from"), |
|
62 |
to = this.model.get("to"); |
|
| 450 | 63 |
if (!from || !to || (this.hidden && !this.ghost)) { |
| 284 | 64 |
return; |
65 |
} |
|
66 |
this.from_representation = this.renderer.getRepresentationByModel(from); |
|
67 |
this.to_representation = this.renderer.getRepresentationByModel(to); |
|
| 450 | 68 |
if (typeof this.from_representation === "undefined" || typeof this.to_representation === "undefined" || |
| 453 | 69 |
(this.from_representation.hidden && !this.from_representation.ghost) || |
| 450 | 70 |
(this.to_representation.hidden && !this.to_representation.ghost)){ |
| 453 | 71 |
this.hide(); |
| 284 | 72 |
return; |
73 |
} |
|
74 |
var _p0a = this.from_representation.paper_coords, |
|
75 |
_p1a = this.to_representation.paper_coords, |
|
76 |
_v = _p1a.subtract(_p0a), |
|
77 |
_r = _v.length, |
|
78 |
_u = _v.divide(_r), |
|
79 |
_ortho = new paper.Point([- _u.y, _u.x]), |
|
80 |
_group_pos = this.bundle.getPosition(this), |
|
81 |
_delta = _ortho.multiply( this.options.edge_gap_in_bundles * _group_pos ), |
|
82 |
_p0b = _p0a.add(_delta), /* Adding a 4 px difference */ |
|
83 |
_p1b = _p1a.add(_delta), /* to differentiate bundled links */ |
|
84 |
_a = _v.angle, |
|
85 |
_textdelta = _ortho.multiply(this.options.edge_label_distance), |
|
86 |
_handle = _v.divide(3), |
|
87 |
_color = this.model.get("color") || this.model.get("color") || (this.model.get("created_by") || Utils._USER_PLACEHOLDER(this.renkan)).get("color"), |
|
88 |
opacity = 1; |
|
89 |
||
90 |
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
|
91 |
opacity = 0.5; |
| 284 | 92 |
this.line.dashArray = [2, 2]; |
93 |
} else { |
|
94 |
opacity = 1; |
|
95 |
this.line.dashArray = null; |
|
96 |
} |
|
97 |
||
98 |
var old_act_btn = this.active_buttons; |
|
99 |
||
100 |
this.active_buttons = this.model.get("delete_scheduled") ? this.pending_delete_buttons : this.normal_buttons; |
|
101 |
||
102 |
if (this.selected && this.renderer.isEditable() && old_act_btn !== this.active_buttons) { |
|
103 |
old_act_btn.forEach(function(b) { |
|
104 |
b.hide(); |
|
105 |
}); |
|
106 |
this.active_buttons.forEach(function(b) { |
|
107 |
b.show(); |
|
108 |
}); |
|
109 |
} |
|
110 |
||
111 |
this.paper_coords = _p0b.add(_p1b).divide(2); |
|
112 |
this.line.strokeColor = _color; |
|
| 450 | 113 |
this.line.opacity = this.ghost ? 0.3 : opacity; |
| 284 | 114 |
this.line.segments[0].point = _p0a; |
115 |
this.line.segments[1].point = this.paper_coords; |
|
116 |
this.line.segments[1].handleIn = _handle.multiply(-1); |
|
117 |
this.line.segments[1].handleOut = _handle; |
|
118 |
this.line.segments[2].point = _p1a; |
|
119 |
this.arrow.rotate(_a - this.arrow_angle); |
|
120 |
this.arrow.fillColor = _color; |
|
| 450 | 121 |
this.arrow.opacity = this.ghost ? 0.3 : opacity; |
| 284 | 122 |
this.arrow.position = this.paper_coords; |
123 |
this.arrow_angle = _a; |
|
124 |
if (_a > 90) { |
|
125 |
_a -= 180; |
|
126 |
_textdelta = _textdelta.multiply(-1); |
|
127 |
} |
|
128 |
if (_a < -90) { |
|
129 |
_a += 180; |
|
130 |
_textdelta = _textdelta.multiply(-1); |
|
131 |
} |
|
132 |
var _text = this.model.get("title") || this.renkan.translate(this.options.label_untitled_edges) || ""; |
|
133 |
_text = Utils.shortenText(_text, this.options.node_label_max_length); |
|
134 |
this.text.text(_text); |
|
135 |
var _textpos = this.paper_coords.add(_textdelta); |
|
136 |
this.text.css({ |
|
137 |
left: _textpos.x, |
|
138 |
top: _textpos.y, |
|
139 |
transform: "rotate(" + _a + "deg)", |
|
140 |
"-moz-transform": "rotate(" + _a + "deg)", |
|
141 |
"-webkit-transform": "rotate(" + _a + "deg)", |
|
| 450 | 142 |
opacity: this.ghost ? 0.3 : opacity |
| 284 | 143 |
}); |
144 |
this.text_angle = _a; |
|
145 |
||
146 |
var _pc = this.paper_coords; |
|
147 |
this.all_buttons.forEach(function(b) { |
|
148 |
b.moveTo(_pc); |
|
149 |
}); |
|
150 |
||
151 |
if (this.renderer.minimap) { |
|
152 |
this.minimap_line.strokeColor = _color; |
|
153 |
this.minimap_line.segments[0].point = this.renderer.toMinimapCoords(new paper.Point(this.from_representation.model.get("position"))); |
|
154 |
this.minimap_line.segments[1].point = this.renderer.toMinimapCoords(new paper.Point(this.to_representation.model.get("position"))); |
|
155 |
} |
|
156 |
}, |
|
| 450 | 157 |
hide: function(){ |
158 |
this.hidden = true; |
|
159 |
this.ghost = false; |
|
160 |
|
|
161 |
this.text.hide(); |
|
162 |
this.line.visible = false; |
|
163 |
this.arrow.visible = false; |
|
164 |
this.minimap_line.visible = false; |
|
165 |
}, |
|
166 |
show: function(ghost){ |
|
167 |
this.ghost = ghost; |
|
168 |
if (this.ghost){ |
|
169 |
this.text.css('opacity', 0.3); |
|
170 |
this.line.opacity = 0.3; |
|
171 |
this.arrow.opacity = 0.3; |
|
172 |
this.minimap_line.opacity = 0.3; |
|
173 |
}else{ |
|
174 |
this.hidden = false; |
|
175 |
|
|
176 |
this.text.css('opacity', 1); |
|
177 |
this.line.opacity = 1; |
|
178 |
this.arrow.opacity = 1; |
|
179 |
this.minimap_line.opacity = 1; |
|
180 |
} |
|
181 |
this.text.show(); |
|
182 |
this.line.visible = true; |
|
183 |
this.arrow.visible = true; |
|
184 |
this.minimap_line.visible = true; |
|
185 |
this.redraw(); |
|
186 |
}, |
|
| 284 | 187 |
openEditor: function() { |
188 |
this.renderer.removeRepresentationsOfType("editor"); |
|
189 |
var _editor = this.renderer.addRepresentation("EdgeEditor",null); |
|
190 |
_editor.source_representation = this; |
|
191 |
_editor.draw(); |
|
192 |
}, |
|
193 |
select: function() { |
|
194 |
this.selected = true; |
|
195 |
this.line.strokeWidth = this.options.selected_edge_stroke_width; |
|
196 |
if (this.renderer.isEditable()) { |
|
197 |
this.active_buttons.forEach(function(b) { |
|
198 |
b.show(); |
|
199 |
}); |
|
200 |
} |
|
201 |
if (!this.options.editor_mode) { |
|
202 |
this.openEditor(); |
|
203 |
} |
|
204 |
this._super("select"); |
|
205 |
}, |
|
206 |
unselect: function(_newTarget) { |
|
207 |
if (!_newTarget || _newTarget.source_representation !== this) { |
|
208 |
this.selected = false; |
|
209 |
if (this.options.editor_mode) { |
|
210 |
this.all_buttons.forEach(function(b) { |
|
211 |
b.hide(); |
|
212 |
}); |
|
213 |
} |
|
214 |
this.line.strokeWidth = this.options.edge_stroke_width; |
|
215 |
this._super("unselect"); |
|
216 |
} |
|
217 |
}, |
|
218 |
mousedown: function(_event, _isTouch) { |
|
219 |
if (_isTouch) { |
|
220 |
this.renderer.unselectAll(); |
|
221 |
this.select(); |
|
222 |
} |
|
223 |
}, |
|
224 |
mouseup: function(_event, _isTouch) { |
|
225 |
if (!this.renkan.read_only && this.renderer.is_dragging) { |
|
226 |
this.from_representation.saveCoords(); |
|
227 |
this.to_representation.saveCoords(); |
|
228 |
this.from_representation.is_dragging = false; |
|
229 |
this.to_representation.is_dragging = false; |
|
230 |
} else { |
|
231 |
if (!_isTouch) { |
|
232 |
this.openEditor(); |
|
233 |
} |
|
234 |
this.model.trigger("clicked"); |
|
235 |
} |
|
236 |
this.renderer.click_target = null; |
|
237 |
this.renderer.is_dragging = false; |
|
238 |
}, |
|
239 |
paperShift: function(_delta) { |
|
240 |
if (this.options.editor_mode) { |
|
241 |
if (!this.options.read_only) { |
|
242 |
this.from_representation.paperShift(_delta); |
|
243 |
this.to_representation.paperShift(_delta); |
|
244 |
} |
|
245 |
} else { |
|
246 |
this.renderer.paperShift(_delta); |
|
247 |
} |
|
248 |
}, |
|
249 |
destroy: function() { |
|
250 |
this._super("destroy"); |
|
251 |
this.line.remove(); |
|
252 |
this.arrow.remove(); |
|
253 |
this.text.remove(); |
|
254 |
if (this.renderer.minimap) { |
|
255 |
this.minimap_line.remove(); |
|
256 |
} |
|
257 |
this.all_buttons.forEach(function(b) { |
|
258 |
b.destroy(); |
|
259 |
}); |
|
260 |
var _this = this; |
|
| 433 | 261 |
this.bundle.edges = _.reject(this.bundle.edges, function(_edge) { |
| 284 | 262 |
return _this === _edge; |
263 |
}); |
|
264 |
} |
|
| 433 | 265 |
}).value(); |
| 284 | 266 |
|
267 |
return Edge; |
|
268 |
||
269 |
}); |