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