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