send relationship type info in initial load, not as AJAX
[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 ) {
56e3972e 204 // Add the relationship types to the keymap list
205 // TODO Make the descriptions (in typedef.description) available somewhere
206 $.each( relationship_types, function(index, typedef) {
207 var elid = 'list_rel_' + typedef.name;
208 $('#keymaplist').append( $('<li>').attr( 'id', elid ).css( "border-color", relation_manager.relation_colors[index] ).text(typedef.name) );
209 });
210 // Now fetch the relationships themselves and add them to the graph
211 var rel_types = $.map( relationship_types, function(t) { return t.name });
212 // Save this list of names to the outer element data so that the relationship
213 // factory can access it
214 $('#keymap').data('relations', rel_types);
5f15640c 215 var textrelpath = getTextURL( 'relationships' );
56e3972e 216 $.getJSON( textrelpath, function(data) {
217 $.each(data, function( index, rel_info ) {
218 var type_index = $.inArray(rel_info.type, rel_types);
219 var source_found = get_ellipse( rel_info.source );
220 var target_found = get_ellipse( rel_info.target );
221 if( type_index != -1 && source_found.size() && target_found.size() ) {
222 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
223 relation.data( 'type', rel_info.type );
224 relation.data( 'scope', rel_info.scope );
225 relation.data( 'note', rel_info.note );
226 var node_obj = get_node_obj(rel_info.source);
227 node_obj.set_draggable( false );
228 node_obj.ellipse.data( 'node_obj', null );
229 node_obj = get_node_obj(rel_info.target);
230 node_obj.set_draggable( false );
231 node_obj.ellipse.data( 'node_obj', null );
232 }
233 });
234 callback_fn.call();
235 });
b28e606e 236}
237
238function get_ellipse( node_id ) {
45ee3b96 239 return $( jq( node_id ) + ' ellipse');
b28e606e 240}
241
242function get_node_obj( node_id ) {
9529f69c 243 var node_ellipse = get_ellipse( node_id );
244 if( node_ellipse.data( 'node_obj' ) == null ) {
245 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
246 };
247 return node_ellipse.data( 'node_obj' );
b28e606e 248}
249
b28e606e 250function node_obj(ellipse) {
251 this.ellipse = ellipse;
252 var self = this;
253
254 this.x = 0;
255 this.y = 0;
256 this.dx = 0;
257 this.dy = 0;
258 this.node_elements = node_elements_for(self.ellipse);
259
260 this.get_id = function() {
45ee3b96 261 return $(self.ellipse).parent().attr('id')
b28e606e 262 }
263
264 this.set_draggable = function( draggable ) {
265 if( draggable ) {
05485bfd 266 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
267 $(self.ellipse).parent().mousedown( this.mousedown_listener );
268 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
269 self.ellipse.siblings('text').attr('class', 'noselect draggable');
b28e606e 270 } else {
05485bfd 271 self.ellipse.siblings('text').attr('class', '');
4c41c02c 272 $(self.ellipse).parent().unbind( 'mouseenter' ).unbind( 'mouseleave' ).unbind( 'mousedown' );
273 color_inactive( self.ellipse );
b28e606e 274 }
275 }
276
277 this.mousedown_listener = function(evt) {
278 evt.stopPropagation();
279 self.x = evt.clientX;
280 self.y = evt.clientY;
281 $('body').mousemove( self.mousemove_listener );
282 $('body').mouseup( self.mouseup_listener );
05485bfd 283 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
b28e606e 284 self.ellipse.attr( 'fill', '#ff66ff' );
285 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
286 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
287 }
288
289 this.mousemove_listener = function(evt) {
9529f69c 290 self.dx = (evt.clientX - self.x) / mouse_scale;
291 self.dy = (evt.clientY - self.y) / mouse_scale;
b28e606e 292 self.move_elements();
05485bfd 293 evt.returnValue = false;
294 evt.preventDefault();
295 return false;
b28e606e 296 }
297
298 this.mouseup_listener = function(evt) {
299 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
45ee3b96 300 var source_node_id = $(self.ellipse).parent().attr('id');
05485bfd 301 var source_node_text = self.ellipse.siblings('text').text();
45ee3b96 302 var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id');
05485bfd 303 var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
b28e606e 304 $('#source_node_id').val( source_node_id );
05485bfd 305 $('#source_node_text').val( source_node_text );
b28e606e 306 $('#target_node_id').val( target_node_id );
05485bfd 307 $('#target_node_text').val( target_node_text );
b28e606e 308 $('#dialog-form').dialog( 'open' );
309 };
310 $('body').unbind('mousemove');
311 $('body').unbind('mouseup');
312 self.ellipse.attr( 'fill', '#fff' );
05485bfd 313 $(self.ellipse).parent().hover( self.enter_node, self.leave_node );
9529f69c 314 self.reset_elements();
b28e606e 315 }
f2fb96fc 316
b28e606e 317 this.cpos = function() {
318 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
319 }
320
321 this.get_g = function() {
322 return self.ellipse.parent('g');
323 }
324
325 this.enter_node = function(evt) {
326 self.ellipse.attr( 'fill', '#ffccff' );
327 }
328
329 this.leave_node = function(evt) {
330 self.ellipse.attr( 'fill', '#fff' );
331 }
332
333 this.greyout_edges = function() {
334 $.each( self.node_elements, function(index, value) {
335 value.grey_out('.edge');
336 });
337 }
338
339 this.ungreyout_edges = function() {
340 $.each( self.node_elements, function(index, value) {
341 value.un_grey_out('.edge');
342 });
343 }
344
345 this.move_elements = function() {
346 $.each( self.node_elements, function(index, value) {
347 value.move(self.dx,self.dy);
348 });
349 }
350
351 this.reset_elements = function() {
352 $.each( self.node_elements, function(index, value) {
353 value.reset();
354 });
355 }
356
357 this.update_elements = function() {
358 self.node_elements = node_elements_for(self.ellipse);
359 }
360
361 self.set_draggable( true );
362}
363
364function svgshape( shape_element ) {
365 this.shape = shape_element;
366 this.move = function(dx,dy) {
367 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
368 }
369 this.reset = function() {
370 this.shape.attr( "transform", "translate( 0, 0 )" );
371 }
372 this.grey_out = function(filter) {
373 if( this.shape.parent(filter).size() != 0 ) {
374 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
375 }
376 }
377 this.un_grey_out = function(filter) {
378 if( this.shape.parent(filter).size() != 0 ) {
379 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
380 }
381 }
382}
383
384function svgpath( path_element, svg_element ) {
385 this.svg_element = svg_element;
386 this.path = path_element;
387 this.x = this.path.x;
388 this.y = this.path.y;
389 this.move = function(dx,dy) {
390 this.path.x = this.x + dx;
391 this.path.y = this.y + dy;
392 }
393 this.reset = function() {
394 this.path.x = this.x;
395 this.path.y = this.y;
396 }
397 this.grey_out = function(filter) {
398 if( this.svg_element.parent(filter).size() != 0 ) {
399 this.svg_element.attr('stroke', '#e5e5e5');
400 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
05485bfd 401 this.svg_element.siblings('text').attr('class', 'noselect');
b28e606e 402 }
403 }
404 this.un_grey_out = function(filter) {
405 if( this.svg_element.parent(filter).size() != 0 ) {
406 this.svg_element.attr('stroke', '#000000');
407 this.svg_element.siblings('text').attr('fill', '#000000');
05485bfd 408 this.svg_element.siblings('text').attr('class', '');
b28e606e 409 }
410 }
411}
412
413function node_elements_for( ellipse ) {
414 node_elements = get_edge_elements_for( ellipse );
415 node_elements.push( new svgshape( ellipse.siblings('text') ) );
416 node_elements.push( new svgshape( ellipse ) );
417 return node_elements;
418}
419
420function get_edge_elements_for( ellipse ) {
421 edge_elements = new Array();
45ee3b96 422 node_id = ellipse.parent().attr('id');
b28e606e 423 edge_in_pattern = new RegExp( node_id + '$' );
424 edge_out_pattern = new RegExp( '^' + node_id );
425 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
426 title = $(this).text();
427 if( edge_in_pattern.test(title) ) {
428 polygon = $(this).siblings('polygon');
429 if( polygon.size() > 0 ) {
430 edge_elements.push( new svgshape( polygon ) );
431 }
432 path_segments = $(this).siblings('path')[0].pathSegList;
433 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
434 }
435 if( edge_out_pattern.test(title) ) {
436 path_segments = $(this).siblings('path')[0].pathSegList;
437 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
438 }
439 });
440 return edge_elements;
441}
442
443function relation_factory() {
444 var self = this;
445 this.color_memo = null;
446 //TODO: colors hard coded for now
447 this.temp_color = '#FFA14F';
448 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
449
450 this.create_temporary = function( source_node_id, target_node_id ) {
45ee3b96 451 var relation_id = get_relation_id( source_node_id, target_node_id );
452 var relation = $( jq( relation_id ) );
9529f69c 453 if( relation.size() == 0 ) {
b28e606e 454 draw_relation( source_node_id, target_node_id, self.temp_color );
455 } else {
456 self.color_memo = relation.children('path').attr( 'stroke' );
457 relation.children('path').attr( 'stroke', self.temp_color );
458 }
459 }
460 this.remove_temporary = function() {
461 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
462 if( self.color_memo != null ) {
463 path_element.attr( 'stroke', self.color_memo );
464 self.color_memo = null;
465 } else {
9529f69c 466 var temporary = path_element.parent('g').remove();
467 temporary.empty();
468 temporary = null;
b28e606e 469 }
470 }
471 this.create = function( source_node_id, target_node_id, color_index ) {
472 //TODO: Protect from (color_)index out of bound..
473 var relation_color = self.relation_colors[ color_index ];
9529f69c 474 var relation = draw_relation( source_node_id, target_node_id, relation_color );
475 get_node_obj( source_node_id ).update_elements();
476 get_node_obj( target_node_id ).update_elements();
477 return relation;
b28e606e 478 }
9529f69c 479 this.toggle_active = function( relation_id ) {
45ee3b96 480 var relation = $( jq( relation_id ) );
9529f69c 481 var relation_path = relation.children('path');
482 if( !relation.data( 'active' ) ) {
483 relation_path.css( {'cursor':'pointer'} );
484 relation_path.mouseenter( function(event) {
485 outerTimer = setTimeout( function() {
486 timer = setTimeout( function() {
45ee3b96 487 var related_nodes = get_related_nodes( relation_id );
488 var source_node_id = related_nodes[0];
489 var target_node_id = related_nodes[1];
9529f69c 490 $('#delete_source_node_id').val( source_node_id );
491 $('#delete_target_node_id').val( target_node_id );
492 self.showinfo(relation);
493 }, 500 )
494 }, 1000 );
495 });
496 relation_path.mouseleave( function(event) {
497 clearTimeout(outerTimer);
498 if( timer != null ) { clearTimeout(timer); }
499 });
500 relation.data( 'active', true );
501 } else {
502 relation_path.unbind( 'mouseenter' );
503 relation_path.unbind( 'mouseleave' );
504 relation_path.css( {'cursor':'inherit'} );
505 relation.data( 'active', false );
506 }
507 }
508 this.showinfo = function(relation) {
69a19c91 509 var htmlstr = 'type: ' + relation.data( 'type' ) + '<br/>scope: ' + relation.data( 'scope' );
510 if( relation.data( 'note' ) ) {
511 htmlstr = htmlstr + '<br/>note: ' + relation.data( 'note' );
512 }
513 $('#delete-form-text').html( htmlstr );
9529f69c 514 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
515 var xs = parseFloat( points[0].split(',')[0] );
516 var xe = parseFloat( points[1].split(',')[0] );
517 var ys = parseFloat( points[0].split(',')[1] );
518 var ye = parseFloat( points[3].split(',')[1] );
519 var p = svg_root.createSVGPoint();
520 p.x = xs + ((xe-xs)*1.1);
521 p.y = ye - ((ye-ys)/2);
76f05423 522 var ctm = svg_root_element.getScreenCTM();
9529f69c 523 var nx = p.matrixTransform(ctm).x;
524 var ny = p.matrixTransform(ctm).y;
525 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
526 $('#delete-form').dialog( 'open' );
527 dialog_aria.offset({ left: nx, top: ny });
528 }
529 this.remove = function( relation_id ) {
45ee3b96 530 var relation = $( jq( relation_id ) );
9529f69c 531 relation.remove();
b28e606e 532 }
533}
534
45ee3b96 535// Utility function to create/return the ID of a relation link between
536// a source and target.
537function get_relation_id( source_id, target_id ) {
538 var idlist = [ source_id, target_id ];
539 idlist.sort();
540 return 'relation-' + idlist[0] + '-...-' + idlist[1];
541}
542
543function get_related_nodes( relation_id ) {
544 var srctotarg = relation_id.substr( 9 );
545 return srctotarg.split('-...-');
546}
547
b28e606e 548function draw_relation( source_id, target_id, relation_color ) {
9529f69c 549 var source_ellipse = get_ellipse( source_id );
550 var target_ellipse = get_ellipse( target_id );
45ee3b96 551 var relation_id = get_relation_id( source_id, target_id );
9529f69c 552 var svg = $('#svgenlargement').children('svg').svg().svg('get');
553 var path = svg.createPath();
554 var sx = parseInt( source_ellipse.attr('cx') );
555 var rx = parseInt( source_ellipse.attr('rx') );
556 var sy = parseInt( source_ellipse.attr('cy') );
557 var ex = parseInt( target_ellipse.attr('cx') );
558 var ey = parseInt( target_ellipse.attr('cy') );
45ee3b96 559 var relation = svg.group( $("#svgenlargement svg g"),
560 { 'class':'relation', 'id':relation_id } );
9529f69c 561 svg.title( relation, source_id + '->' + target_id );
562 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 563 var relation_element = $('#svgenlargement .relation').filter( ':last' );
564 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
9529f69c 565 return relation_element;
b28e606e 566}
567
9529f69c 568
b28e606e 569$(document).ready(function () {
9529f69c 570
571 timer = null;
b28e606e 572 relation_manager = new relation_factory();
9529f69c 573 $('#update_workspace_button').data('locked', false);
b28e606e 574
9529f69c 575 $('#enlargement').mousedown(function (event) {
b28e606e 576 $(this)
9529f69c 577 .data('down', true)
578 .data('x', event.clientX)
579 .data('y', event.clientY)
580 .data('scrollLeft', this.scrollLeft)
76f05423 581 stateTf = svg_root_element.getCTM().inverse();
9529f69c 582 var p = svg_root.createSVGPoint();
583 p.x = event.clientX;
584 p.y = event.clientY;
585 stateOrigin = p.matrixTransform(stateTf);
76f05423 586 event.returnValue = false;
587 event.preventDefault();
9529f69c 588 return false;
b28e606e 589 }).mouseup(function (event) {
9529f69c 590 $(this).data('down', false);
b28e606e 591 }).mousemove(function (event) {
9529f69c 592 if( timer != null ) { clearTimeout(timer); }
593 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
594 var p = svg_root.createSVGPoint();
595 p.x = event.clientX;
596 p.y = event.clientY;
597 p = p.matrixTransform(stateTf);
598 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
599 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
76f05423 600 svg_root_element.setAttribute("transform", s);
b28e606e 601 }
76f05423 602 event.returnValue = false;
603 event.preventDefault();
b28e606e 604 }).mousewheel(function (event, delta) {
9529f69c 605 event.returnValue = false;
606 event.preventDefault();
607 if ( $('#update_workspace_button').data('locked') == false ) {
76f05423 608 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
609 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
9529f69c 610 if( delta < -9 ) { delta = -9 };
611 var z = 1 + delta/10;
76f05423 612 z = delta > 0 ? 1 : -1;
613 var g = svg_root_element;
614 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) {
9529f69c 615 var root = svg_root;
616 var p = root.createSVGPoint();
76f05423 617 p.x = event.originalEvent.clientX;
618 p.y = event.originalEvent.clientY;
9529f69c 619 p = p.matrixTransform(g.getCTM().inverse());
76f05423 620 var scaleLevel = 1+(z/20);
621 var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y);
9529f69c 622 var matrix = g.getCTM().multiply(k);
623 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
624 g.setAttribute("transform", s);
625 }
626 }
b28e606e 627 }).css({
628 'overflow' : 'hidden',
629 'cursor' : '-moz-grab'
630 });
631
632
633 $( "#dialog-form" ).dialog({
634 autoOpen: false,
635 height: 270,
636 width: 290,
637 modal: true,
638 buttons: {
77121e3f 639 "Ok": function( evt ) {
640 $(evt.target).button("disable");
b28e606e 641 $('#status').empty();
76f05423 642 form_values = $('#collapse_node_form').serialize();
5f15640c 643 ncpath = getTextURL( 'relationships' );
b28e606e 644 var jqjson = $.post( ncpath, form_values, function(data) {
645 $.each( data, function(item, source_target) {
7b54e481 646 var source_found = get_ellipse( source_target[0] );
647 var target_found = get_ellipse( source_target[1] );
eeea8fb6 648 var relation_found = $.inArray( source_target[2], $('#keymap').data('relations') );
8073db65 649 if( source_found.size() && target_found.size() && relation_found > -1 ) {
eeea8fb6 650 var relation = relation_manager.create( source_target[0], source_target[1], relation_found );
651 relation.data( 'type', source_target[2] );
7b54e481 652 relation.data( 'scope', $('#scope :selected').text() );
653 relation.data( 'note', $('#note').val() );
45ee3b96 654 relation_manager.toggle_active( relation.attr('id') );
7b54e481 655 }
77121e3f 656 $(evt.target).button("enable");
657 });
b28e606e 658 $( "#dialog-form" ).dialog( "close" );
9529f69c 659 }, 'json' );
b28e606e 660 },
661 Cancel: function() {
b28e606e 662 $( this ).dialog( "close" );
663 }
664 },
665 create: function(event, ui) {
666 $(this).data( 'relation_drawn', false );
56e3972e 667 $.each( relationship_types, function(index, typedef) {
668 $('#rel_type').append( $('<option />').attr( "value", typedef.name ).text(typedef.name) );
669 });
670 $.each( relationship_scopes, function(index, value) {
671 $('#scope').append( $('<option />').attr( "value", value ).text(value) );
b28e606e 672 });
673 },
674 open: function() {
675 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
9529f69c 676 $(".ui-widget-overlay").css("background", "none");
677 $("#dialog_overlay").show();
678 $("#dialog_overlay").height( $("#enlargement_container").height() );
fc018906 679 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
9529f69c 680 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
b28e606e 681 },
682 close: function() {
9529f69c 683 relation_manager.remove_temporary();
b28e606e 684 $( '#status' ).empty();
685 $("#dialog_overlay").hide();
686 }
687 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
6666d111 688 if( ajaxSettings.url == getTextURL('relationships')
689 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
9529f69c 690 var errobj = jQuery.parseJSON( jqXHR.responseText );
691 $('#status').append( '<p class="error">Error: ' + errobj.error + '</br>The relationship cannot be made.</p>' );
b28e606e 692 }
aafcb75b 693 $(event.target).parent().find('.ui-button').button("enable");
b28e606e 694 } );
695
9529f69c 696 $( "#delete-form" ).dialog({
697 autoOpen: false,
69a19c91 698 height: 135,
9529f69c 699 width: 160,
700 modal: false,
701 buttons: {
702 Cancel: function() {
703 $( this ).dialog( "close" );
704 },
705 Delete: function() {
5f15640c 706 form_values = $('#delete_relation_form').serialize();
707 ncpath = getTextURL( 'relationships' );
9529f69c 708 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
709 $.each( data, function(item, source_target) {
45ee3b96 710 relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );
9529f69c 711 });
712 $( "#delete-form" ).dialog( "close" );
713 }, dataType: 'json', type: 'DELETE' });
714 }
715 },
716 create: function(event, ui) {
717 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
718 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
719 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
720 dialog_aria.mouseenter( function() {
721 if( mouseWait != null ) { clearTimeout(mouseWait) };
722 })
723 dialog_aria.mouseleave( function() {
724 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
725 })
726 },
727 open: function() {
728 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
729 },
730 close: function() {
731 }
732 });
733
487674b9 734 // function for reading form dialog should go here;
735 // just hide the element for now if we don't have morphology
736 if( can_morphologize ) {
737 $('#reading-form').dialog({
738 autoOpen: false,
739 // height: 400,
740 width: 450,
741 modal: true,
742 buttons: {
743 Cancel: function() {
744 $( this ).dialog( "close" );
745 },
746 Update: function( evt ) {
747 // Disable the button
748 $(evt.target).button("disable");
749 $('#reading_status').empty();
750 var reading_id = $('#reading_id').val()
751 form_values = {
752 'id' : reading_id,
753 'is_nonsense': $('#reading_is_nonsense').is(':checked'),
754 'grammar_invalid': $('#reading_grammar_invalid').is(':checked'),
755 'normal_form': $('#reading_normal_form').val() };
756 // Add the morphology values
757 $('.reading_morphology').each( function() {
758 if( $(this).val() != '(Click to select)' ) {
759 var rmid = $(this).attr('id');
760 rmid = rmid.substring(8);
761 form_values[rmid] = $(this).val();
762 }
45ee3b96 763 });
487674b9 764 // Make the JSON call
765 ncpath = getReadingURL( reading_id );
766 var reading_element = readingdata[reading_id];
767 // $(':button :contains("Update")').attr("disabled", true);
768 var jqjson = $.post( ncpath, form_values, function(data) {
769 $.each( data, function(key, value) {
770 reading_element[key] = value;
771 });
772 if( $('#update_workspace_button').data('locked') == false ) {
773 color_inactive( get_ellipse( reading_id ) );
774 }
775 $(evt.target).button("enable");
776 $( "#reading-form" ).dialog( "close" );
777 });
778 // Re-color the node if necessary
779 return false;
780 }
781 },
782 create: function() {
783 },
784 open: function() {
785 $(".ui-widget-overlay").css("background", "none");
786 $("#dialog_overlay").show();
787 $('#reading_status').empty();
788 $("#dialog_overlay").height( $("#enlargement_container").height() );
789 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
790 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
791 $("#reading-form").parent().find('.ui-button').button("enable");
792 },
793 close: function() {
794 $("#dialog_overlay").hide();
795 }
796 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
797 if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1
798 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
799 var errobj = jQuery.parseJSON( jqXHR.responseText );
800 $('#reading_status').append( '<p class="error">Error: ' + errobj.error + '</p>' );
801 }
802 $(event.target).parent().find('.ui-button').button("enable");
803 });
804 } else {
805 $('#reading-form').hide();
45ee3b96 806 }
4c41c02c 807
76f05423 808
b28e606e 809 $('#update_workspace_button').click( function() {
810 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
76f05423 811 mouse_scale = svg_root_element.getScreenCTM().a;
9529f69c 812 if( $(this).data('locked') == true ) {
813 $('#svgenlargement ellipse' ).each( function( index ) {
814 if( $(this).data( 'node_obj' ) != null ) {
815 $(this).data( 'node_obj' ).ungreyout_edges();
816 $(this).data( 'node_obj' ).set_draggable( false );
817 var node_id = $(this).data( 'node_obj' ).get_id();
818 toggle_relation_active( node_id );
819 $(this).data( 'node_obj', null );
820 }
b28e606e 821 })
b28e606e 822 $(this).data('locked', false);
9529f69c 823 $(this).css('background-position', '0px 44px');
b28e606e 824 } else {
9529f69c 825 var left = $('#enlargement').offset().left;
826 var right = left + $('#enlargement').width();
76f05423 827 var tf = svg_root_element.getScreenCTM().inverse();
9529f69c 828 var p = svg_root.createSVGPoint();
829 p.x=left;
830 p.y=100;
831 var cx_min = p.matrixTransform(tf).x;
832 p.x=right;
833 var cx_max = p.matrixTransform(tf).x;
834 $('#svgenlargement ellipse').each( function( index ) {
835 var cx = parseInt( $(this).attr('cx') );
836 if( cx > cx_min && cx < cx_max) {
837 if( $(this).data( 'node_obj' ) == null ) {
838 $(this).data( 'node_obj', new node_obj( $(this) ) );
839 } else {
840 $(this).data( 'node_obj' ).set_draggable( true );
841 }
842 $(this).data( 'node_obj' ).greyout_edges();
843 var node_id = $(this).data( 'node_obj' ).get_id();
844 toggle_relation_active( node_id );
b28e606e 845 }
9529f69c 846 });
847 $(this).css('background-position', '0px 0px');
b28e606e 848 $(this).data('locked', true );
b28e606e 849 }
850 });
851
e847b186 852 $('.helptag').popupWindow({
853 height:500,
854 width:800,
855 top:50,
856 left:50,
857 scrollbars:1
858 });
859
860
9529f69c 861 function toggle_relation_active( node_id ) {
862 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
863 matchid = new RegExp( "^" + node_id );
864 if( $(this).text().match( matchid ) != null ) {
45ee3b96 865 var relation_id = $(this).parent().attr('id');
866 relation_manager.toggle_active( relation_id );
9529f69c 867 };
868 });
b28e606e 869 }
b28e606e 870
76f05423 871 expandFillPageClients();
872 $(window).resize(function() {
873 expandFillPageClients();
874 });
875
9529f69c 876});
b28e606e 877
878
76f05423 879function expandFillPageClients() {
880 $('.fillPage').each(function () {
881 $(this).height($(window).height() - $(this).offset().top - MARGIN);
882 });
883}
884
885function loadSVG(svgData) {
886 var svgElement = $('#svgenlargement');
887
888 $(svgElement).svg('destroy');
889
890 $(svgElement).svg({
891 loadURL: svgData,
892 onLoad : svgEnlargementLoaded
893 });
894}
895
896
897/* OS Gadget stuff
898
899function svg_select_callback(topic, data, subscriberData) {
900 svgData = data;
901 loadSVG(svgData);
902}
903
904function loaded() {
905 var prefs = new gadgets.Prefs();
906 var preferredHeight = parseInt(prefs.getString('height'));
907 if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight);
908 expandFillPageClients();
909}
910
911if (gadgets.util.hasFeature('pubsub-2')) {
912 gadgets.HubSettings.onConnect = function(hum, suc, err) {
913 subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback);
914 loaded();
915 };
916}
917else gadgets.util.registerOnLoadHandler(loaded);
918*/