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