Commit | Line | Data |
aff524fc |
1 | var MARGIN=30; |
2 | var svg_root = null; |
3 | var svg_root_element = null; |
4 | var start_element_height = 0; |
5 | var reltypes = {}; |
a8928d1d |
6 | var readingdata = {}; |
aff524fc |
7 | |
7fd1d97a |
8 | function getTextPath() { |
aff524fc |
9 | var currpath = window.location.pathname; |
93daee83 |
10 | // Get rid of trailing slash |
7fd1d97a |
11 | if( currpath.lastIndexOf('/') == currpath.length - 1 ) { |
12 | currpath = currpath.slice( 0, currpath.length - 1) |
13 | }; |
93daee83 |
14 | // Get rid of query parameters |
15 | if( currpath.lastIndexOf('?') != -1 ) { |
16 | currpath = currpath.slice( 0, currpath.lastIndexOf('?') ); |
17 | }; |
7fd1d97a |
18 | var path_elements = currpath.split('/'); |
19 | var textid = path_elements.pop(); |
20 | var basepath = path_elements.join( '/' ); |
21 | var path_parts = [ basepath, textid ]; |
22 | return path_parts; |
581aee24 |
23 | } |
24 | |
72874569 |
25 | function getRelativePath() { |
26 | var path_parts = getTextPath(); |
27 | return path_parts[0]; |
28 | } |
29 | |
a8928d1d |
30 | function getTextURL( which ) { |
72874569 |
31 | var path_parts = getTextPath(); |
a8928d1d |
32 | return path_parts[0] + '/' + path_parts[1] + '/' + which; |
33 | } |
34 | |
35 | function getReadingURL( reading_id ) { |
36 | var path_parts = getTextPath(); |
37 | return path_parts[0] + '/' + path_parts[1] + '/reading/' + reading_id; |
581aee24 |
38 | } |
39 | |
28f2ef34 |
40 | // Make an XML ID into a valid selector |
41 | function jq(myid) { |
42 | return '#' + myid.replace(/(:|\.)/g,'\\$1'); |
43 | } |
44 | |
1301020e |
45 | // Actions for opening the reading panel |
46 | function node_dblclick_listener( evt ) { |
47 | // Open the reading dialogue for the given node. |
48 | // First get the reading info |
49 | var reading_id = $(this).attr('id'); |
50 | var reading_info = readingdata[reading_id]; |
51 | // and then populate the dialog box with it. |
52 | // Set the easy properties first |
53 | $('#reading-form').dialog( 'option', 'title', 'Reading information for "' + reading_info['text'] + '"' ); |
54 | $('#reading_id').val( reading_id ); |
55 | $('#reading_is_nonsense').val( reading_info['is_nonsense'] ); |
56 | $('#reading_grammar_invalid').val( reading_info['grammar_invalid'] ); |
57 | // Use .text as a backup for .normal_form |
58 | var normal_form = reading_info['normal_form']; |
59 | if( !normal_form ) { |
60 | normal_form = reading_info['text']; |
61 | } |
62 | var nfboxsize = 10; |
63 | if( normal_form.length > 9 ) { |
64 | nfboxsize = normal_form.length + 1; |
65 | } |
66 | $('#reading_normal_form').attr( 'size', nfboxsize ) |
67 | $('#reading_normal_form').val( normal_form ); |
68 | // Now do the morphological properties. |
69 | // and then open the dialog. |
70 | $('#morphology').empty(); |
71 | $.each( reading_info['lexemes'], function( idx, lex ) { |
72 | var morphoptions = []; |
73 | $.each( lex['wordform_matchlist'], function( tdx, tag ) { |
74 | var tagstr = stringify_wordform( tag ); |
75 | morphoptions.push( tagstr ); |
76 | }); |
77 | var formtag = 'morphology_' + idx; |
78 | var form_morph_elements = morph_elements( formtag ); |
79 | form_morph_elements[0].text( lex['string'] + ': ' ); |
80 | form_morph_elements[1].autocomplete({ |
81 | source: morphoptions, |
82 | minLength: 0 |
83 | }); |
84 | //forminput.autocomplete('search', ''); |
85 | if( lex['form'] ) { |
86 | var formstr = stringify_wordform( lex['form'] ); |
87 | form_morph_elements[1].val( formstr ); |
88 | } |
89 | $.each( form_morph_elements, function( idx, el ) { |
90 | $('#morphology').append( el ); |
91 | }); |
92 | }); |
93 | $('#reading-form').dialog("open"); |
94 | } |
95 | |
96 | function stringify_wordform ( tag ) { |
97 | return tag['lemma'] + ' // ' + tag['morphology']; |
98 | } |
99 | |
100 | function morph_elements ( formtag ) { |
101 | var formlabel = $('<label/>').attr( 'id', 'label_' + formtag ).attr( |
102 | 'for', 'reading_' + formtag ); |
103 | var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr( |
104 | 'name', 'reading_' + formtag ).attr( 'size', '50' ); |
105 | var morphel = [ formlabel, forminput, $('<br/>') ]; |
106 | return morphel; |
107 | } |
108 | |
109 | // Initialize the SVG once it exists |
581aee24 |
110 | function svgEnlargementLoaded() { |
63213207 |
111 | //Give some visual evidence that we are working |
112 | $('#loading_overlay').show(); |
113 | lo_height = $("#enlargement_container").outerHeight(); |
114 | lo_width = $("#enlargement_container").outerWidth(); |
115 | $("#loading_overlay").height( lo_height ); |
116 | $("#loading_overlay").width( lo_width ); |
117 | $("#loading_overlay").offset( $("#enlargement_container").offset() ); |
118 | $("#loading_message").offset( |
119 | { 'top': lo_height / 2 - $("#loading_message").height() / 2, |
120 | 'left': lo_width / 2 - $("#loading_message").width() / 2 }); |
72874569 |
121 | //Set viewbox widht and height to widht and height of $('#svgenlargement svg'). |
aff524fc |
122 | $('#update_workspace_button').data('locked', false); |
123 | $('#update_workspace_button').css('background-position', '0px 44px'); |
72874569 |
124 | //This is essential to make sure zooming and panning works properly. |
125 | $('#svgenlargement ellipse').attr( {stroke:'green', fill:'#b3f36d'} ); |
1301020e |
126 | $('#svgenlargement ellipse').parent().dblclick( node_dblclick_listener ); |
72874569 |
127 | var graph_svg = $('#svgenlargement svg'); |
128 | var svg_g = $('#svgenlargement svg g')[0]; |
aff524fc |
129 | if (!svg_g) return; |
72874569 |
130 | svg_root = graph_svg.svg().svg('get').root(); |
aff524fc |
131 | |
132 | // Find the real root and ignore any text nodes |
133 | for (i = 0; i < svg_root.childNodes.length; ++i) { |
134 | if (svg_root.childNodes[i].nodeName != '#text') { |
135 | svg_root_element = svg_root.childNodes[i]; |
136 | break; |
137 | } |
138 | } |
139 | |
72874569 |
140 | svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' ); |
141 | svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' ); |
142 | //Now set scale and translate so svg height is about 150px and vertically centered in viewbox. |
143 | //This is just to create a nice starting enlargement. |
144 | var initial_svg_height = 250; |
145 | var scale = initial_svg_height/graph_svg.attr( 'height' ); |
146 | var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale); |
147 | var transform = svg_g.getAttribute('transform'); |
148 | var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] ); |
149 | translate += additional_translate; |
150 | var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')'; |
151 | svg_g.setAttribute('transform', transform); |
152 | //used to calculate min and max zoom level: |
28f2ef34 |
153 | start_element_height = $('#__START__ ellipse')[0].getBBox().height; |
63213207 |
154 | add_relations( function() { $('#loading_overlay').hide(); }); |
7fd1d97a |
155 | } |
156 | |
63213207 |
157 | function add_relations( callback_fn ) { |
72874569 |
158 | var basepath = getRelativePath(); |
a8928d1d |
159 | var textrelpath = getTextURL( 'relationships' ); |
72874569 |
160 | $.getJSON( basepath + '/definitions', function(data) { |
7fd1d97a |
161 | var rel_types = data.types.sort(); |
72874569 |
162 | $.getJSON( textrelpath, |
7fd1d97a |
163 | function(data) { |
164 | $.each(data, function( index, rel_info ) { |
165 | var type_index = $.inArray(rel_info.type, rel_types); |
93daee83 |
166 | var source_found = get_ellipse( rel_info.source ); |
167 | var target_found = get_ellipse( rel_info.target ); |
168 | if( type_index != -1 && source_found.size() && target_found.size() ) { |
72874569 |
169 | var relation = relation_manager.create( rel_info.source, rel_info.target, type_index ); |
170 | relation.data( 'type', rel_info.type ); |
171 | relation.data( 'scope', rel_info.scope ); |
31aaf446 |
172 | relation.data( 'note', rel_info.note ); |
72874569 |
173 | var node_obj = get_node_obj(rel_info.source); |
174 | node_obj.set_draggable( false ); |
175 | node_obj.ellipse.data( 'node_obj', null ); |
176 | node_obj = get_node_obj(rel_info.target); |
177 | node_obj.set_draggable( false ); |
178 | node_obj.ellipse.data( 'node_obj', null ); |
7fd1d97a |
179 | } |
63213207 |
180 | }); |
181 | callback_fn.call(); |
72874569 |
182 | }); |
7fd1d97a |
183 | }); |
581aee24 |
184 | } |
185 | |
186 | function get_ellipse( node_id ) { |
28f2ef34 |
187 | return $( jq( node_id ) + ' ellipse'); |
581aee24 |
188 | } |
189 | |
190 | function get_node_obj( node_id ) { |
72874569 |
191 | var node_ellipse = get_ellipse( node_id ); |
192 | if( node_ellipse.data( 'node_obj' ) == null ) { |
193 | node_ellipse.data( 'node_obj', new node_obj(node_ellipse) ); |
194 | }; |
195 | return node_ellipse.data( 'node_obj' ); |
581aee24 |
196 | } |
197 | |
581aee24 |
198 | function node_obj(ellipse) { |
199 | this.ellipse = ellipse; |
200 | var self = this; |
201 | |
202 | this.x = 0; |
203 | this.y = 0; |
204 | this.dx = 0; |
205 | this.dy = 0; |
206 | this.node_elements = node_elements_for(self.ellipse); |
207 | |
208 | this.get_id = function() { |
28f2ef34 |
209 | return $(self.ellipse).parent().attr('id') |
581aee24 |
210 | } |
211 | |
212 | this.set_draggable = function( draggable ) { |
213 | if( draggable ) { |
0fa07e25 |
214 | $(self.ellipse).attr( {stroke:'black', fill:'#fff'} ); |
215 | $(self.ellipse).parent().mousedown( this.mousedown_listener ); |
216 | $(self.ellipse).parent().hover( this.enter_node, this.leave_node ); |
217 | self.ellipse.siblings('text').attr('class', 'noselect draggable'); |
581aee24 |
218 | } else { |
0fa07e25 |
219 | self.ellipse.siblings('text').attr('class', ''); |
220 | $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave').unbind('mousedown'); |
221 | $(self.ellipse).attr( {stroke:'green', fill:'#b3f36d'} ); |
581aee24 |
222 | } |
223 | } |
224 | |
225 | this.mousedown_listener = function(evt) { |
226 | evt.stopPropagation(); |
227 | self.x = evt.clientX; |
228 | self.y = evt.clientY; |
229 | $('body').mousemove( self.mousemove_listener ); |
230 | $('body').mouseup( self.mouseup_listener ); |
0fa07e25 |
231 | $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave') |
581aee24 |
232 | self.ellipse.attr( 'fill', '#ff66ff' ); |
233 | first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" ); |
234 | if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) }; |
235 | } |
236 | |
237 | this.mousemove_listener = function(evt) { |
72874569 |
238 | self.dx = (evt.clientX - self.x) / mouse_scale; |
239 | self.dy = (evt.clientY - self.y) / mouse_scale; |
581aee24 |
240 | self.move_elements(); |
0fa07e25 |
241 | evt.returnValue = false; |
242 | evt.preventDefault(); |
243 | return false; |
581aee24 |
244 | } |
245 | |
246 | this.mouseup_listener = function(evt) { |
247 | if( $('ellipse[fill="#ffccff"]').size() > 0 ) { |
28f2ef34 |
248 | var source_node_id = $(self.ellipse).parent().attr('id'); |
0fa07e25 |
249 | var source_node_text = self.ellipse.siblings('text').text(); |
28f2ef34 |
250 | var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id'); |
0fa07e25 |
251 | var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text(); |
581aee24 |
252 | $('#source_node_id').val( source_node_id ); |
0fa07e25 |
253 | $('#source_node_text').val( source_node_text ); |
581aee24 |
254 | $('#target_node_id').val( target_node_id ); |
0fa07e25 |
255 | $('#target_node_text').val( target_node_text ); |
581aee24 |
256 | $('#dialog-form').dialog( 'open' ); |
257 | }; |
258 | $('body').unbind('mousemove'); |
259 | $('body').unbind('mouseup'); |
260 | self.ellipse.attr( 'fill', '#fff' ); |
0fa07e25 |
261 | $(self.ellipse).parent().hover( self.enter_node, self.leave_node ); |
72874569 |
262 | self.reset_elements(); |
581aee24 |
263 | } |
f26e47cd |
264 | |
581aee24 |
265 | this.cpos = function() { |
266 | return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') }; |
267 | } |
268 | |
269 | this.get_g = function() { |
270 | return self.ellipse.parent('g'); |
271 | } |
272 | |
273 | this.enter_node = function(evt) { |
274 | self.ellipse.attr( 'fill', '#ffccff' ); |
275 | } |
276 | |
277 | this.leave_node = function(evt) { |
278 | self.ellipse.attr( 'fill', '#fff' ); |
279 | } |
280 | |
281 | this.greyout_edges = function() { |
282 | $.each( self.node_elements, function(index, value) { |
283 | value.grey_out('.edge'); |
284 | }); |
285 | } |
286 | |
287 | this.ungreyout_edges = function() { |
288 | $.each( self.node_elements, function(index, value) { |
289 | value.un_grey_out('.edge'); |
290 | }); |
291 | } |
292 | |
293 | this.move_elements = function() { |
294 | $.each( self.node_elements, function(index, value) { |
295 | value.move(self.dx,self.dy); |
296 | }); |
297 | } |
298 | |
299 | this.reset_elements = function() { |
300 | $.each( self.node_elements, function(index, value) { |
301 | value.reset(); |
302 | }); |
303 | } |
304 | |
305 | this.update_elements = function() { |
306 | self.node_elements = node_elements_for(self.ellipse); |
307 | } |
308 | |
309 | self.set_draggable( true ); |
310 | } |
311 | |
312 | function svgshape( shape_element ) { |
313 | this.shape = shape_element; |
314 | this.move = function(dx,dy) { |
315 | this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" ); |
316 | } |
317 | this.reset = function() { |
318 | this.shape.attr( "transform", "translate( 0, 0 )" ); |
319 | } |
320 | this.grey_out = function(filter) { |
321 | if( this.shape.parent(filter).size() != 0 ) { |
322 | this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'}); |
323 | } |
324 | } |
325 | this.un_grey_out = function(filter) { |
326 | if( this.shape.parent(filter).size() != 0 ) { |
327 | this.shape.attr({'stroke':'#000000', 'fill':'#000000'}); |
328 | } |
329 | } |
330 | } |
331 | |
332 | function svgpath( path_element, svg_element ) { |
333 | this.svg_element = svg_element; |
334 | this.path = path_element; |
335 | this.x = this.path.x; |
336 | this.y = this.path.y; |
337 | this.move = function(dx,dy) { |
338 | this.path.x = this.x + dx; |
339 | this.path.y = this.y + dy; |
340 | } |
341 | this.reset = function() { |
342 | this.path.x = this.x; |
343 | this.path.y = this.y; |
344 | } |
345 | this.grey_out = function(filter) { |
346 | if( this.svg_element.parent(filter).size() != 0 ) { |
347 | this.svg_element.attr('stroke', '#e5e5e5'); |
348 | this.svg_element.siblings('text').attr('fill', '#e5e5e5'); |
0fa07e25 |
349 | this.svg_element.siblings('text').attr('class', 'noselect'); |
581aee24 |
350 | } |
351 | } |
352 | this.un_grey_out = function(filter) { |
353 | if( this.svg_element.parent(filter).size() != 0 ) { |
354 | this.svg_element.attr('stroke', '#000000'); |
355 | this.svg_element.siblings('text').attr('fill', '#000000'); |
0fa07e25 |
356 | this.svg_element.siblings('text').attr('class', ''); |
581aee24 |
357 | } |
358 | } |
359 | } |
360 | |
361 | function node_elements_for( ellipse ) { |
362 | node_elements = get_edge_elements_for( ellipse ); |
363 | node_elements.push( new svgshape( ellipse.siblings('text') ) ); |
364 | node_elements.push( new svgshape( ellipse ) ); |
365 | return node_elements; |
366 | } |
367 | |
368 | function get_edge_elements_for( ellipse ) { |
369 | edge_elements = new Array(); |
28f2ef34 |
370 | node_id = ellipse.parent().attr('id'); |
581aee24 |
371 | edge_in_pattern = new RegExp( node_id + '$' ); |
372 | edge_out_pattern = new RegExp( '^' + node_id ); |
373 | $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) { |
374 | title = $(this).text(); |
375 | if( edge_in_pattern.test(title) ) { |
376 | polygon = $(this).siblings('polygon'); |
377 | if( polygon.size() > 0 ) { |
378 | edge_elements.push( new svgshape( polygon ) ); |
379 | } |
380 | path_segments = $(this).siblings('path')[0].pathSegList; |
381 | edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) ); |
382 | } |
383 | if( edge_out_pattern.test(title) ) { |
384 | path_segments = $(this).siblings('path')[0].pathSegList; |
385 | edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) ); |
386 | } |
387 | }); |
388 | return edge_elements; |
389 | } |
390 | |
391 | function relation_factory() { |
392 | var self = this; |
393 | this.color_memo = null; |
394 | //TODO: colors hard coded for now |
395 | this.temp_color = '#FFA14F'; |
396 | this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ]; |
397 | |
398 | this.create_temporary = function( source_node_id, target_node_id ) { |
28f2ef34 |
399 | var relation_id = get_relation_id( source_node_id, target_node_id ); |
400 | var relation = $( jq( relation_id ) ); |
72874569 |
401 | if( relation.size() == 0 ) { |
581aee24 |
402 | draw_relation( source_node_id, target_node_id, self.temp_color ); |
403 | } else { |
404 | self.color_memo = relation.children('path').attr( 'stroke' ); |
405 | relation.children('path').attr( 'stroke', self.temp_color ); |
406 | } |
407 | } |
408 | this.remove_temporary = function() { |
409 | var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]'); |
410 | if( self.color_memo != null ) { |
411 | path_element.attr( 'stroke', self.color_memo ); |
412 | self.color_memo = null; |
413 | } else { |
72874569 |
414 | var temporary = path_element.parent('g').remove(); |
415 | temporary.empty(); |
416 | temporary = null; |
581aee24 |
417 | } |
418 | } |
419 | this.create = function( source_node_id, target_node_id, color_index ) { |
420 | //TODO: Protect from (color_)index out of bound.. |
421 | var relation_color = self.relation_colors[ color_index ]; |
72874569 |
422 | var relation = draw_relation( source_node_id, target_node_id, relation_color ); |
423 | get_node_obj( source_node_id ).update_elements(); |
424 | get_node_obj( target_node_id ).update_elements(); |
425 | return relation; |
581aee24 |
426 | } |
72874569 |
427 | this.toggle_active = function( relation_id ) { |
28f2ef34 |
428 | var relation = $( jq( relation_id ) ); |
72874569 |
429 | var relation_path = relation.children('path'); |
430 | if( !relation.data( 'active' ) ) { |
431 | relation_path.css( {'cursor':'pointer'} ); |
432 | relation_path.mouseenter( function(event) { |
433 | outerTimer = setTimeout( function() { |
434 | timer = setTimeout( function() { |
28f2ef34 |
435 | var related_nodes = get_related_nodes( relation_id ); |
436 | var source_node_id = related_nodes[0]; |
437 | var target_node_id = related_nodes[1]; |
72874569 |
438 | $('#delete_source_node_id').val( source_node_id ); |
439 | $('#delete_target_node_id').val( target_node_id ); |
440 | self.showinfo(relation); |
441 | }, 500 ) |
442 | }, 1000 ); |
443 | }); |
444 | relation_path.mouseleave( function(event) { |
445 | clearTimeout(outerTimer); |
446 | if( timer != null ) { clearTimeout(timer); } |
447 | }); |
448 | relation.data( 'active', true ); |
449 | } else { |
450 | relation_path.unbind( 'mouseenter' ); |
451 | relation_path.unbind( 'mouseleave' ); |
452 | relation_path.css( {'cursor':'inherit'} ); |
453 | relation.data( 'active', false ); |
454 | } |
455 | } |
456 | this.showinfo = function(relation) { |
31aaf446 |
457 | var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' ); |
458 | if( relation.data( 'note' ) ) { |
459 | htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' ); |
460 | } |
461 | $('#delete-form-text').html( htmlstr ); |
72874569 |
462 | var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' '); |
463 | var xs = parseFloat( points[0].split(',')[0] ); |
464 | var xe = parseFloat( points[1].split(',')[0] ); |
465 | var ys = parseFloat( points[0].split(',')[1] ); |
466 | var ye = parseFloat( points[3].split(',')[1] ); |
467 | var p = svg_root.createSVGPoint(); |
468 | p.x = xs + ((xe-xs)*1.1); |
469 | p.y = ye - ((ye-ys)/2); |
aff524fc |
470 | var ctm = svg_root_element.getScreenCTM(); |
72874569 |
471 | var nx = p.matrixTransform(ctm).x; |
472 | var ny = p.matrixTransform(ctm).y; |
473 | var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']"); |
474 | $('#delete-form').dialog( 'open' ); |
475 | dialog_aria.offset({ left: nx, top: ny }); |
476 | } |
477 | this.remove = function( relation_id ) { |
28f2ef34 |
478 | var relation = $( jq( relation_id ) ); |
72874569 |
479 | relation.remove(); |
581aee24 |
480 | } |
481 | } |
482 | |
28f2ef34 |
483 | // Utility function to create/return the ID of a relation link between |
484 | // a source and target. |
485 | function get_relation_id( source_id, target_id ) { |
486 | var idlist = [ source_id, target_id ]; |
487 | idlist.sort(); |
488 | return 'relation-' + idlist[0] + '-...-' + idlist[1]; |
489 | } |
490 | |
491 | function get_related_nodes( relation_id ) { |
492 | var srctotarg = relation_id.substr( 9 ); |
493 | return srctotarg.split('-...-'); |
494 | } |
495 | |
581aee24 |
496 | function draw_relation( source_id, target_id, relation_color ) { |
72874569 |
497 | var source_ellipse = get_ellipse( source_id ); |
498 | var target_ellipse = get_ellipse( target_id ); |
28f2ef34 |
499 | var relation_id = get_relation_id( source_id, target_id ); |
72874569 |
500 | var svg = $('#svgenlargement').children('svg').svg().svg('get'); |
501 | var path = svg.createPath(); |
502 | var sx = parseInt( source_ellipse.attr('cx') ); |
503 | var rx = parseInt( source_ellipse.attr('rx') ); |
504 | var sy = parseInt( source_ellipse.attr('cy') ); |
505 | var ex = parseInt( target_ellipse.attr('cx') ); |
506 | var ey = parseInt( target_ellipse.attr('cy') ); |
28f2ef34 |
507 | var relation = svg.group( $("#svgenlargement svg g"), |
508 | { 'class':'relation', 'id':relation_id } ); |
72874569 |
509 | svg.title( relation, source_id + '->' + target_id ); |
510 | svg.path( relation, path.move( sx, sy ).curveC( sx + (2*rx), sy, ex + (2*rx), ey, ex, ey ), {fill: 'none', stroke: relation_color, strokeWidth: 4}); |
581aee24 |
511 | var relation_element = $('#svgenlargement .relation').filter( ':last' ); |
512 | relation_element.insertBefore( $('#svgenlargement g g').filter(':first') ); |
72874569 |
513 | return relation_element; |
581aee24 |
514 | } |
515 | |
72874569 |
516 | |
581aee24 |
517 | $(document).ready(function () { |
72874569 |
518 | |
519 | timer = null; |
581aee24 |
520 | relation_manager = new relation_factory(); |
72874569 |
521 | $('#update_workspace_button').data('locked', false); |
581aee24 |
522 | |
72874569 |
523 | $('#enlargement').mousedown(function (event) { |
581aee24 |
524 | $(this) |
72874569 |
525 | .data('down', true) |
526 | .data('x', event.clientX) |
527 | .data('y', event.clientY) |
528 | .data('scrollLeft', this.scrollLeft) |
aff524fc |
529 | stateTf = svg_root_element.getCTM().inverse(); |
72874569 |
530 | var p = svg_root.createSVGPoint(); |
531 | p.x = event.clientX; |
532 | p.y = event.clientY; |
533 | stateOrigin = p.matrixTransform(stateTf); |
aff524fc |
534 | event.returnValue = false; |
535 | event.preventDefault(); |
72874569 |
536 | return false; |
581aee24 |
537 | }).mouseup(function (event) { |
72874569 |
538 | $(this).data('down', false); |
581aee24 |
539 | }).mousemove(function (event) { |
72874569 |
540 | if( timer != null ) { clearTimeout(timer); } |
541 | if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) { |
542 | var p = svg_root.createSVGPoint(); |
543 | p.x = event.clientX; |
544 | p.y = event.clientY; |
545 | p = p.matrixTransform(stateTf); |
546 | var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y); |
547 | var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")"; |
aff524fc |
548 | svg_root_element.setAttribute("transform", s); |
581aee24 |
549 | } |
aff524fc |
550 | event.returnValue = false; |
551 | event.preventDefault(); |
581aee24 |
552 | }).mousewheel(function (event, delta) { |
72874569 |
553 | event.returnValue = false; |
554 | event.preventDefault(); |
555 | if ( $('#update_workspace_button').data('locked') == false ) { |
aff524fc |
556 | if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta; |
557 | if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail; |
72874569 |
558 | if( delta < -9 ) { delta = -9 }; |
559 | var z = 1 + delta/10; |
aff524fc |
560 | z = delta > 0 ? 1 : -1; |
561 | var g = svg_root_element; |
562 | if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) { |
72874569 |
563 | var root = svg_root; |
564 | var p = root.createSVGPoint(); |
aff524fc |
565 | p.x = event.originalEvent.clientX; |
566 | p.y = event.originalEvent.clientY; |
72874569 |
567 | p = p.matrixTransform(g.getCTM().inverse()); |
aff524fc |
568 | var scaleLevel = 1+(z/20); |
569 | var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y); |
72874569 |
570 | var matrix = g.getCTM().multiply(k); |
571 | var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")"; |
572 | g.setAttribute("transform", s); |
573 | } |
574 | } |
581aee24 |
575 | }).css({ |
576 | 'overflow' : 'hidden', |
577 | 'cursor' : '-moz-grab' |
578 | }); |
579 | |
a8928d1d |
580 | var rdgpath = getTextURL( 'readings' ); |
581 | $.getJSON( rdgpath, function( data ) { |
582 | readingdata = data; |
583 | }); |
581aee24 |
584 | |
585 | $( "#dialog-form" ).dialog({ |
586 | autoOpen: false, |
587 | height: 270, |
588 | width: 290, |
589 | modal: true, |
590 | buttons: { |
591 | "Ok": function() { |
592 | $('#status').empty(); |
aff524fc |
593 | form_values = $('#collapse_node_form').serialize(); |
a8928d1d |
594 | ncpath = getTextURL( 'relationships' ); |
72874569 |
595 | $(':button :contains("Ok")').attr("disabled", true); |
581aee24 |
596 | var jqjson = $.post( ncpath, form_values, function(data) { |
597 | $.each( data, function(item, source_target) { |
988c7619 |
598 | var source_found = get_ellipse( source_target[0] ); |
599 | var target_found = get_ellipse( source_target[1] ); |
600 | if( source_found.size() && target_found.size() ) { |
28f2ef34 |
601 | var relation = relation_manager.create( source_target[0], source_target[1], $('#rel_type')[0].selectedIndex-1 ); |
988c7619 |
602 | relation.data( 'type', $('#rel_type :selected').text() ); |
603 | relation.data( 'scope', $('#scope :selected').text() ); |
604 | relation.data( 'note', $('#note').val() ); |
28f2ef34 |
605 | relation_manager.toggle_active( relation.attr('id') ); |
988c7619 |
606 | } |
581aee24 |
607 | }); |
581aee24 |
608 | $( "#dialog-form" ).dialog( "close" ); |
72874569 |
609 | }, 'json' ); |
581aee24 |
610 | }, |
611 | Cancel: function() { |
581aee24 |
612 | $( this ).dialog( "close" ); |
613 | } |
614 | }, |
615 | create: function(event, ui) { |
616 | $(this).data( 'relation_drawn', false ); |
617 | //TODO? Err handling? |
72874569 |
618 | var basepath = getRelativePath(); |
619 | var jqjson = $.getJSON( basepath + '/definitions', function(data) { |
581aee24 |
620 | var types = data.types.sort(); |
621 | $.each( types, function(index, value) { |
aff524fc |
622 | $('#rel_type').append( $('<option />').attr( "value", value ).text(value) ); |
581aee24 |
623 | $('#keymaplist').append( $('<li>').css( "border-color", relation_manager.relation_colors[index] ).text(value) ); |
624 | }); |
625 | var scopes = data.scopes; |
626 | $.each( scopes, function(index, value) { |
aff524fc |
627 | $('#scope').append( $('<option />').attr( "value", value ).text(value) ); |
581aee24 |
628 | }); |
629 | }); |
630 | }, |
631 | open: function() { |
632 | relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() ); |
72874569 |
633 | $(".ui-widget-overlay").css("background", "none"); |
634 | $("#dialog_overlay").show(); |
635 | $("#dialog_overlay").height( $("#enlargement_container").height() ); |
63213207 |
636 | $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); |
72874569 |
637 | $("#dialog_overlay").offset( $("#enlargement_container").offset() ); |
581aee24 |
638 | }, |
639 | close: function() { |
72874569 |
640 | relation_manager.remove_temporary(); |
581aee24 |
641 | $( '#status' ).empty(); |
642 | $("#dialog_overlay").hide(); |
643 | } |
644 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
72874569 |
645 | if( ( ajaxSettings.type == 'POST' ) && jqXHR.status == 403 ) { |
646 | var errobj = jQuery.parseJSON( jqXHR.responseText ); |
647 | $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' ); |
581aee24 |
648 | } |
649 | } ); |
650 | |
72874569 |
651 | $( "#delete-form" ).dialog({ |
652 | autoOpen: false, |
31aaf446 |
653 | height: 135, |
72874569 |
654 | width: 160, |
655 | modal: false, |
656 | buttons: { |
657 | Cancel: function() { |
658 | $( this ).dialog( "close" ); |
659 | }, |
660 | Delete: function() { |
a8928d1d |
661 | form_values = $('#delete_relation_form').serialize(); |
662 | ncpath = getTextURL( 'relationships' ); |
72874569 |
663 | var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) { |
664 | $.each( data, function(item, source_target) { |
28f2ef34 |
665 | relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) ); |
72874569 |
666 | }); |
667 | $( "#delete-form" ).dialog( "close" ); |
668 | }, dataType: 'json', type: 'DELETE' }); |
669 | } |
670 | }, |
671 | create: function(event, ui) { |
672 | var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' ); |
673 | buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' ); |
674 | var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']"); |
675 | dialog_aria.mouseenter( function() { |
676 | if( mouseWait != null ) { clearTimeout(mouseWait) }; |
677 | }) |
678 | dialog_aria.mouseleave( function() { |
679 | mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 ); |
680 | }) |
681 | }, |
682 | open: function() { |
683 | mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 ); |
684 | }, |
685 | close: function() { |
686 | } |
687 | }); |
688 | |
aff524fc |
689 | // function for reading form dialog should go here; for now hide the element |
28f2ef34 |
690 | $('#reading-form').dialog({ |
691 | autoOpen: false, |
692 | height: 400, |
f26e47cd |
693 | width: 600, |
28f2ef34 |
694 | modal: true, |
695 | buttons: { |
696 | Cancel: function() { |
697 | $( this ).dialog( "close" ); |
698 | }, |
699 | Update: function() { |
700 | form_values = $('#reading_data_form').serialize(); |
701 | ncpath = getReadingURL(); |
702 | var reading_element = get_node_obj( $('#reading_data_id').val() ); |
703 | $(':button :contains("Update")').attr("disabled", true); |
704 | var jqjson = $.post( ncpath, form_values, function(data) { |
705 | $.each( data, function(key, value) { |
706 | reading_element.data( key, value ); |
707 | }); |
708 | $( "#reading-form" ).dialog( "close" ); |
709 | }); |
710 | } |
711 | }, |
712 | create: function() {}, |
f26e47cd |
713 | open: function() { |
714 | $(".ui-widget-overlay").css("background", "none"); |
715 | $("#dialog_overlay").show(); |
716 | $("#dialog_overlay").height( $("#enlargement_container").height() ); |
717 | $("#dialog_overlay").width( $("#enlargement_container").innerWidth() ); |
718 | $("#dialog_overlay").offset( $("#enlargement_container").offset() ); |
719 | }, |
28f2ef34 |
720 | close: function() { |
721 | $("#dialog_overlay").hide(); |
722 | } |
723 | }); |
aff524fc |
724 | |
581aee24 |
725 | $('#update_workspace_button').click( function() { |
726 | var svg_enlargement = $('#svgenlargement').svg().svg('get').root(); |
aff524fc |
727 | mouse_scale = svg_root_element.getScreenCTM().a; |
72874569 |
728 | if( $(this).data('locked') == true ) { |
729 | $('#svgenlargement ellipse' ).each( function( index ) { |
730 | if( $(this).data( 'node_obj' ) != null ) { |
731 | $(this).data( 'node_obj' ).ungreyout_edges(); |
732 | $(this).data( 'node_obj' ).set_draggable( false ); |
733 | var node_id = $(this).data( 'node_obj' ).get_id(); |
734 | toggle_relation_active( node_id ); |
735 | $(this).data( 'node_obj', null ); |
736 | } |
581aee24 |
737 | }) |
581aee24 |
738 | $(this).data('locked', false); |
72874569 |
739 | $(this).css('background-position', '0px 44px'); |
581aee24 |
740 | } else { |
72874569 |
741 | var left = $('#enlargement').offset().left; |
742 | var right = left + $('#enlargement').width(); |
aff524fc |
743 | var tf = svg_root_element.getScreenCTM().inverse(); |
72874569 |
744 | var p = svg_root.createSVGPoint(); |
745 | p.x=left; |
746 | p.y=100; |
747 | var cx_min = p.matrixTransform(tf).x; |
748 | p.x=right; |
749 | var cx_max = p.matrixTransform(tf).x; |
750 | $('#svgenlargement ellipse').each( function( index ) { |
751 | var cx = parseInt( $(this).attr('cx') ); |
752 | if( cx > cx_min && cx < cx_max) { |
753 | if( $(this).data( 'node_obj' ) == null ) { |
754 | $(this).data( 'node_obj', new node_obj( $(this) ) ); |
755 | } else { |
756 | $(this).data( 'node_obj' ).set_draggable( true ); |
757 | } |
758 | $(this).data( 'node_obj' ).greyout_edges(); |
759 | var node_id = $(this).data( 'node_obj' ).get_id(); |
760 | toggle_relation_active( node_id ); |
581aee24 |
761 | } |
72874569 |
762 | }); |
763 | $(this).css('background-position', '0px 0px'); |
581aee24 |
764 | $(this).data('locked', true ); |
581aee24 |
765 | } |
766 | }); |
767 | |
9ca77245 |
768 | $('.helptag').popupWindow({ |
769 | height:500, |
770 | width:800, |
771 | top:50, |
772 | left:50, |
773 | scrollbars:1 |
774 | }); |
775 | |
776 | |
72874569 |
777 | function toggle_relation_active( node_id ) { |
778 | $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) { |
779 | matchid = new RegExp( "^" + node_id ); |
780 | if( $(this).text().match( matchid ) != null ) { |
28f2ef34 |
781 | var relation_id = $(this).parent().attr('id'); |
782 | relation_manager.toggle_active( relation_id ); |
72874569 |
783 | }; |
784 | }); |
581aee24 |
785 | } |
581aee24 |
786 | |
aff524fc |
787 | expandFillPageClients(); |
788 | $(window).resize(function() { |
789 | expandFillPageClients(); |
790 | }); |
791 | |
72874569 |
792 | }); |
581aee24 |
793 | |
794 | |
aff524fc |
795 | function expandFillPageClients() { |
796 | $('.fillPage').each(function () { |
797 | $(this).height($(window).height() - $(this).offset().top - MARGIN); |
798 | }); |
799 | } |
800 | |
801 | function loadSVG(svgData) { |
802 | var svgElement = $('#svgenlargement'); |
803 | |
804 | $(svgElement).svg('destroy'); |
805 | |
806 | $(svgElement).svg({ |
807 | loadURL: svgData, |
808 | onLoad : svgEnlargementLoaded |
809 | }); |
810 | } |
811 | |
812 | |
813 | /* OS Gadget stuff |
814 | |
815 | function svg_select_callback(topic, data, subscriberData) { |
816 | svgData = data; |
817 | loadSVG(svgData); |
818 | } |
819 | |
820 | function loaded() { |
821 | var prefs = new gadgets.Prefs(); |
822 | var preferredHeight = parseInt(prefs.getString('height')); |
823 | if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight); |
824 | expandFillPageClients(); |
825 | } |
826 | |
827 | if (gadgets.util.hasFeature('pubsub-2')) { |
828 | gadgets.HubSettings.onConnect = function(hum, suc, err) { |
829 | subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback); |
830 | loaded(); |
831 | }; |
832 | } |
833 | else gadgets.util.registerOnLoadHandler(loaded); |
834 | */ |