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