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