Proof of concept that we can detach a node successfully.
[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
a471cb0f 8jQuery.removeFromArray = function(value, arr) {
9 return jQuery.grep(arr, function(elem, index) {
10 return elem !== value;
11 });
12};
13
a84ca4de 14function arrayUnique(array) {
15 var a = array.concat();
16 for(var i=0; i<a.length; ++i) {
17 for(var j=i+1; j<a.length; ++j) {
18 if(a[i] === a[j])
19 a.splice(j--, 1);
20 }
21 }
22 return a;
23};
24
5f15640c 25function getTextURL( which ) {
3f9d7ae5 26 return basepath + textid + '/' + which;
5f15640c 27}
28
29function getReadingURL( reading_id ) {
3f9d7ae5 30 return basepath + textid + '/reading/' + reading_id;
b28e606e 31}
32
45ee3b96 33// Make an XML ID into a valid selector
34function jq(myid) {
35 return '#' + myid.replace(/(:|\.)/g,'\\$1');
36}
37
065c7cf2 38// Actions for opening the reading panel
39function node_dblclick_listener( evt ) {
40 // Open the reading dialogue for the given node.
41 // First get the reading info
42 var reading_id = $(this).attr('id');
43 var reading_info = readingdata[reading_id];
44 // and then populate the dialog box with it.
45 // Set the easy properties first
46 $('#reading-form').dialog( 'option', 'title', 'Reading information for "' + reading_info['text'] + '"' );
47 $('#reading_id').val( reading_id );
798fa939 48 toggle_checkbox( $('#reading_is_nonsense'), reading_info['is_nonsense'] );
49 toggle_checkbox( $('#reading_grammar_invalid'), reading_info['grammar_invalid'] );
065c7cf2 50 // Use .text as a backup for .normal_form
51 var normal_form = reading_info['normal_form'];
52 if( !normal_form ) {
53 normal_form = reading_info['text'];
54 }
55 var nfboxsize = 10;
56 if( normal_form.length > 9 ) {
57 nfboxsize = normal_form.length + 1;
58 }
59 $('#reading_normal_form').attr( 'size', nfboxsize )
60 $('#reading_normal_form').val( normal_form );
5539cba3 61 if( editable ) {
62 // Fill in the witnesses for the de-collation box.
63 $('#reading_decollate_witnesses').empty();
64 $.each( reading_info['witnesses'], function( idx, wit ) {
65 $('#reading_decollate_witnesses').append( $('<option/>').attr(
66 'value', wit ).text( wit ) );
67 });
68 }
065c7cf2 69 // Now do the morphological properties.
0dcdd5ec 70 morphology_form( reading_info['lexemes'] );
065c7cf2 71 // and then open the dialog.
0dcdd5ec 72 $('#reading-form').dialog("open");
73}
74
798fa939 75function toggle_checkbox( box, value ) {
76 if( value == null ) {
77 value = false;
78 }
79 box.attr('checked', value );
80}
81
0dcdd5ec 82function morphology_form ( lexlist ) {
487674b9 83 if( lexlist.length ) {
84 $('#morph_outer').show();
85 $('#morphology').empty();
86 $.each( lexlist, function( idx, lex ) {
87 var morphoptions = [];
88 if( 'wordform_matchlist' in lex ) {
89 $.each( lex['wordform_matchlist'], function( tdx, tag ) {
90 var tagstr = stringify_wordform( tag );
91 morphoptions.push( tagstr );
92 });
93 }
94 var formtag = 'morphology_' + idx;
95 var formstr = '';
96 if( 'form' in lex ) {
97 formstr = stringify_wordform( lex['form'] );
98 }
99 var form_morph_elements = morph_elements(
100 formtag, lex['string'], formstr, morphoptions );
101 $.each( form_morph_elements, function( idx, el ) {
102 $('#morphology').append( el );
08f8443a 103 });
065c7cf2 104 });
487674b9 105 } else {
106 $('#morph_outer').hide();
107 }
065c7cf2 108}
109
110function stringify_wordform ( tag ) {
08f8443a 111 if( tag ) {
112 var elements = tag.split(' // ');
113 return elements[1] + ' // ' + elements[2];
114 }
115 return ''
065c7cf2 116}
117
d1132306 118function morph_elements ( formtag, formtxt, currform, morphoptions ) {
119 var clicktag = '(Click to select)';
120 if ( !currform ) {
121 currform = clicktag;
122 }
065c7cf2 123 var formlabel = $('<label/>').attr( 'id', 'label_' + formtag ).attr(
d1132306 124 'for', 'reading_' + formtag ).text( formtxt + ': ' );
065c7cf2 125 var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr(
0dcdd5ec 126 'name', 'reading_' + formtag ).attr( 'size', '50' ).attr(
127 'class', 'reading_morphology' ).val( currform );
d1132306 128 forminput.autocomplete({ source: morphoptions, minLength: 0 });
129 forminput.focus( function() {
130 if( $(this).val() == clicktag ) {
131 $(this).val('');
132 }
133 $(this).autocomplete('search', '')
134 });
065c7cf2 135 var morphel = [ formlabel, forminput, $('<br/>') ];
136 return morphel;
137}
138
4c41c02c 139function color_inactive ( el ) {
140 var reading_id = $(el).parent().attr('id');
141 var reading_info = readingdata[reading_id];
142 // If the reading info has any non-disambiguated lexemes, color it yellow;
143 // otherwise color it green.
144 $(el).attr( {stroke:'green', fill:'#b3f36d'} );
bb3230b1 145 if( reading_info ) {
146 $.each( reading_info['lexemes'], function ( idx, lex ) {
147 if( !lex['is_disambiguated'] || lex['is_disambiguated'] == 0 ) {
148 $(el).attr( {stroke:'orange', fill:'#fee233'} );
149 }
150 });
151 }
4c41c02c 152}
153
997ebe92 154function relemmatize () {
155 // Send the reading for a new lemmatization and reopen the form.
a0a66634 156 $('#relemmatize_pending').show();
997ebe92 157 var reading_id = $('#reading_id').val()
158 ncpath = getReadingURL( reading_id );
159 form_values = {
160 'normal_form': $('#reading_normal_form').val(),
161 'relemmatize': 1 };
162 var jqjson = $.post( ncpath, form_values, function( data ) {
163 // Update the form with the return
164 if( 'id' in data ) {
165 // We got back a good answer. Stash it
166 readingdata[reading_id] = data;
167 // and regenerate the morphology form.
168 morphology_form( data['lexemes'] );
169 } else {
170 alert("Could not relemmatize as requested: " + data['error']);
171 }
a0a66634 172 $('#relemmatize_pending').hide();
997ebe92 173 });
174}
175
065c7cf2 176// Initialize the SVG once it exists
b28e606e 177function svgEnlargementLoaded() {
fc018906 178 //Give some visual evidence that we are working
179 $('#loading_overlay').show();
180 lo_height = $("#enlargement_container").outerHeight();
181 lo_width = $("#enlargement_container").outerWidth();
182 $("#loading_overlay").height( lo_height );
183 $("#loading_overlay").width( lo_width );
184 $("#loading_overlay").offset( $("#enlargement_container").offset() );
185 $("#loading_message").offset(
186 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
187 'left': lo_width / 2 - $("#loading_message").width() / 2 });
30d0ba1e 188 if( editable ) {
189 // Show the update toggle button.
190 $('#update_workspace_button').data('locked', false);
191 $('#update_workspace_button').css('background-position', '0px 44px');
192 }
065c7cf2 193 $('#svgenlargement ellipse').parent().dblclick( node_dblclick_listener );
9529f69c 194 var graph_svg = $('#svgenlargement svg');
195 var svg_g = $('#svgenlargement svg g')[0];
76f05423 196 if (!svg_g) return;
9529f69c 197 svg_root = graph_svg.svg().svg('get').root();
76f05423 198
199 // Find the real root and ignore any text nodes
200 for (i = 0; i < svg_root.childNodes.length; ++i) {
201 if (svg_root.childNodes[i].nodeName != '#text') {
202 svg_root_element = svg_root.childNodes[i];
203 break;
204 }
205 }
206
30d0ba1e 207 //Set viewbox width and height to width and height of $('#svgenlargement svg').
208 //This is essential to make sure zooming and panning works properly.
9529f69c 209 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
210 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
211 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
212 //This is just to create a nice starting enlargement.
213 var initial_svg_height = 250;
214 var scale = initial_svg_height/graph_svg.attr( 'height' );
215 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
216 var transform = svg_g.getAttribute('transform');
217 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
218 translate += additional_translate;
219 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
220 svg_g.setAttribute('transform', transform);
221 //used to calculate min and max zoom level:
4c41c02c 222 start_element_height = $('#__START__').children('ellipse')[0].getBBox().height;
e538eccb 223 //some use of call backs to ensure succesive execution
224 add_relations( function() {
225 var rdgpath = getTextURL( 'readings' );
226 $.getJSON( rdgpath, function( data ) {
227 readingdata = data;
228 $('#svgenlargement ellipse').each( function( i, el ) { color_inactive( el ) });
a471cb0f 229 detach_node(null);
e538eccb 230 });
231 $('#loading_overlay').hide();
232 });
c1add777 233
234 //initialize marquee
235 marquee = new Marquee();
236
6afcd813 237}
238
fc018906 239function add_relations( callback_fn ) {
56e3972e 240 // Add the relationship types to the keymap list
56e3972e 241 $.each( relationship_types, function(index, typedef) {
671c04b1 242 li_elm = $('<li class="key">').css( "border-color",
243 relation_manager.relation_colors[index] ).text(typedef.name);
244 li_elm.append( $('<div>').attr('class', 'key_tip_container').append(
245 $('<div>').attr('class', 'key_tip').text(typedef.description) ) );
cfefd283 246 $('#keymaplist').append( li_elm );
56e3972e 247 });
248 // Now fetch the relationships themselves and add them to the graph
249 var rel_types = $.map( relationship_types, function(t) { return t.name });
250 // Save this list of names to the outer element data so that the relationship
251 // factory can access it
252 $('#keymap').data('relations', rel_types);
5f15640c 253 var textrelpath = getTextURL( 'relationships' );
56e3972e 254 $.getJSON( textrelpath, function(data) {
255 $.each(data, function( index, rel_info ) {
256 var type_index = $.inArray(rel_info.type, rel_types);
257 var source_found = get_ellipse( rel_info.source );
258 var target_found = get_ellipse( rel_info.target );
259 if( type_index != -1 && source_found.size() && target_found.size() ) {
260 var relation = relation_manager.create( rel_info.source, rel_info.target, type_index );
261 relation.data( 'type', rel_info.type );
262 relation.data( 'scope', rel_info.scope );
263 relation.data( 'note', rel_info.note );
30d0ba1e 264 if( editable ) {
265 var node_obj = get_node_obj(rel_info.source);
e538eccb 266 node_obj.set_selectable( false );
30d0ba1e 267 node_obj.ellipse.data( 'node_obj', null );
268 node_obj = get_node_obj(rel_info.target);
e538eccb 269 node_obj.set_selectable( false );
30d0ba1e 270 node_obj.ellipse.data( 'node_obj', null );
271 }
56e3972e 272 }
273 });
274 callback_fn.call();
275 });
b28e606e 276}
277
278function get_ellipse( node_id ) {
45ee3b96 279 return $( jq( node_id ) + ' ellipse');
b28e606e 280}
281
282function get_node_obj( node_id ) {
9529f69c 283 var node_ellipse = get_ellipse( node_id );
284 if( node_ellipse.data( 'node_obj' ) == null ) {
285 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
286 };
287 return node_ellipse.data( 'node_obj' );
b28e606e 288}
289
b28e606e 290function node_obj(ellipse) {
291 this.ellipse = ellipse;
292 var self = this;
293
294 this.x = 0;
295 this.y = 0;
296 this.dx = 0;
297 this.dy = 0;
298 this.node_elements = node_elements_for(self.ellipse);
299
300 this.get_id = function() {
45ee3b96 301 return $(self.ellipse).parent().attr('id')
b28e606e 302 }
303
e538eccb 304 this.set_selectable = function( clickable ) {
305 if( clickable && editable ) {
306 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
307 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
308 $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } );
309 $(self.ellipse).parent().click( function(evt) {
310 evt.stopPropagation();
311 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
312 $('ellipse[fill="#9999ff"]').each( function() {
313 $(this).data( 'node_obj' ).set_draggable( false );
314 } );
315 }
316 self.set_draggable( true )
317 });
318 } else {
319 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
320 self.ellipse.siblings('text').attr('class', '');
321 $(self.ellipse).parent().unbind();
322 $('body').unbind('mousemove');
323 $('body').unbind('mouseup');
324 }
325 }
326
b28e606e 327 this.set_draggable = function( draggable ) {
30d0ba1e 328 if( draggable && editable ) {
e538eccb 329 $(self.ellipse).attr( {stroke:'black', fill:'#9999ff'} );
05485bfd 330 $(self.ellipse).parent().mousedown( this.mousedown_listener );
e538eccb 331 $(self.ellipse).parent().unbind( 'mouseenter' ).unbind( 'mouseleave' );
05485bfd 332 self.ellipse.siblings('text').attr('class', 'noselect draggable');
b28e606e 333 } else {
e538eccb 334 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
05485bfd 335 self.ellipse.siblings('text').attr('class', '');
e538eccb 336 $(self.ellipse).parent().unbind( 'mousedown ');
337 $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } );
338 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
b28e606e 339 }
340 }
341
342 this.mousedown_listener = function(evt) {
343 evt.stopPropagation();
344 self.x = evt.clientX;
345 self.y = evt.clientY;
346 $('body').mousemove( self.mousemove_listener );
347 $('body').mouseup( self.mouseup_listener );
05485bfd 348 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
e538eccb 349 self.ellipse.attr( 'fill', '#6b6bb2' );
b28e606e 350 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
351 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
352 }
353
354 this.mousemove_listener = function(evt) {
9529f69c 355 self.dx = (evt.clientX - self.x) / mouse_scale;
356 self.dy = (evt.clientY - self.y) / mouse_scale;
b28e606e 357 self.move_elements();
05485bfd 358 evt.returnValue = false;
359 evt.preventDefault();
360 return false;
b28e606e 361 }
362
363 this.mouseup_listener = function(evt) {
364 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
45ee3b96 365 var source_node_id = $(self.ellipse).parent().attr('id');
05485bfd 366 var source_node_text = self.ellipse.siblings('text').text();
45ee3b96 367 var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id');
05485bfd 368 var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
b28e606e 369 $('#source_node_id').val( source_node_id );
05485bfd 370 $('#source_node_text').val( source_node_text );
b28e606e 371 $('#target_node_id').val( target_node_id );
05485bfd 372 $('#target_node_text').val( target_node_text );
b28e606e 373 $('#dialog-form').dialog( 'open' );
374 };
375 $('body').unbind('mousemove');
376 $('body').unbind('mouseup');
e538eccb 377 self.ellipse.attr( 'fill', '#9999ff' );
9529f69c 378 self.reset_elements();
b28e606e 379 }
f2fb96fc 380
b28e606e 381 this.cpos = function() {
382 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
383 }
384
385 this.get_g = function() {
386 return self.ellipse.parent('g');
387 }
388
389 this.enter_node = function(evt) {
390 self.ellipse.attr( 'fill', '#ffccff' );
391 }
392
393 this.leave_node = function(evt) {
394 self.ellipse.attr( 'fill', '#fff' );
395 }
396
397 this.greyout_edges = function() {
398 $.each( self.node_elements, function(index, value) {
399 value.grey_out('.edge');
400 });
401 }
402
403 this.ungreyout_edges = function() {
404 $.each( self.node_elements, function(index, value) {
405 value.un_grey_out('.edge');
406 });
407 }
408
409 this.move_elements = function() {
410 $.each( self.node_elements, function(index, value) {
411 value.move(self.dx,self.dy);
412 });
413 }
414
415 this.reset_elements = function() {
416 $.each( self.node_elements, function(index, value) {
417 value.reset();
418 });
419 }
420
421 this.update_elements = function() {
422 self.node_elements = node_elements_for(self.ellipse);
423 }
424
a84ca4de 425 this.get_witnesses = function() {
426 return readingdata[self.get_id()].witnesses
427 }
428
e538eccb 429 self.set_selectable( true );
b28e606e 430}
431
432function svgshape( shape_element ) {
433 this.shape = shape_element;
434 this.move = function(dx,dy) {
435 this.shape.attr( "transform", "translate(" + dx + " " + dy + ")" );
436 }
437 this.reset = function() {
438 this.shape.attr( "transform", "translate( 0, 0 )" );
439 }
440 this.grey_out = function(filter) {
441 if( this.shape.parent(filter).size() != 0 ) {
442 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
443 }
444 }
445 this.un_grey_out = function(filter) {
446 if( this.shape.parent(filter).size() != 0 ) {
447 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
448 }
449 }
450}
451
452function svgpath( path_element, svg_element ) {
453 this.svg_element = svg_element;
454 this.path = path_element;
455 this.x = this.path.x;
456 this.y = this.path.y;
457 this.move = function(dx,dy) {
458 this.path.x = this.x + dx;
459 this.path.y = this.y + dy;
460 }
461 this.reset = function() {
462 this.path.x = this.x;
463 this.path.y = this.y;
464 }
465 this.grey_out = function(filter) {
466 if( this.svg_element.parent(filter).size() != 0 ) {
467 this.svg_element.attr('stroke', '#e5e5e5');
468 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
05485bfd 469 this.svg_element.siblings('text').attr('class', 'noselect');
b28e606e 470 }
471 }
472 this.un_grey_out = function(filter) {
473 if( this.svg_element.parent(filter).size() != 0 ) {
474 this.svg_element.attr('stroke', '#000000');
475 this.svg_element.siblings('text').attr('fill', '#000000');
05485bfd 476 this.svg_element.siblings('text').attr('class', '');
b28e606e 477 }
478 }
479}
480
481function node_elements_for( ellipse ) {
482 node_elements = get_edge_elements_for( ellipse );
483 node_elements.push( new svgshape( ellipse.siblings('text') ) );
484 node_elements.push( new svgshape( ellipse ) );
485 return node_elements;
486}
487
488function get_edge_elements_for( ellipse ) {
489 edge_elements = new Array();
45ee3b96 490 node_id = ellipse.parent().attr('id');
b28e606e 491 edge_in_pattern = new RegExp( node_id + '$' );
492 edge_out_pattern = new RegExp( '^' + node_id );
493 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
494 title = $(this).text();
495 if( edge_in_pattern.test(title) ) {
496 polygon = $(this).siblings('polygon');
497 if( polygon.size() > 0 ) {
498 edge_elements.push( new svgshape( polygon ) );
499 }
500 path_segments = $(this).siblings('path')[0].pathSegList;
501 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
502 }
503 if( edge_out_pattern.test(title) ) {
504 path_segments = $(this).siblings('path')[0].pathSegList;
505 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
506 }
507 });
508 return edge_elements;
509}
510
511function relation_factory() {
512 var self = this;
513 this.color_memo = null;
514 //TODO: colors hard coded for now
515 this.temp_color = '#FFA14F';
516 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
517
518 this.create_temporary = function( source_node_id, target_node_id ) {
45ee3b96 519 var relation_id = get_relation_id( source_node_id, target_node_id );
520 var relation = $( jq( relation_id ) );
9529f69c 521 if( relation.size() == 0 ) {
b28e606e 522 draw_relation( source_node_id, target_node_id, self.temp_color );
523 } else {
524 self.color_memo = relation.children('path').attr( 'stroke' );
525 relation.children('path').attr( 'stroke', self.temp_color );
526 }
527 }
528 this.remove_temporary = function() {
529 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
530 if( self.color_memo != null ) {
531 path_element.attr( 'stroke', self.color_memo );
532 self.color_memo = null;
533 } else {
9529f69c 534 var temporary = path_element.parent('g').remove();
535 temporary.empty();
536 temporary = null;
b28e606e 537 }
538 }
539 this.create = function( source_node_id, target_node_id, color_index ) {
540 //TODO: Protect from (color_)index out of bound..
541 var relation_color = self.relation_colors[ color_index ];
9529f69c 542 var relation = draw_relation( source_node_id, target_node_id, relation_color );
543 get_node_obj( source_node_id ).update_elements();
544 get_node_obj( target_node_id ).update_elements();
545 return relation;
b28e606e 546 }
9529f69c 547 this.toggle_active = function( relation_id ) {
45ee3b96 548 var relation = $( jq( relation_id ) );
9529f69c 549 var relation_path = relation.children('path');
550 if( !relation.data( 'active' ) ) {
551 relation_path.css( {'cursor':'pointer'} );
552 relation_path.mouseenter( function(event) {
553 outerTimer = setTimeout( function() {
554 timer = setTimeout( function() {
45ee3b96 555 var related_nodes = get_related_nodes( relation_id );
556 var source_node_id = related_nodes[0];
557 var target_node_id = related_nodes[1];
9529f69c 558 $('#delete_source_node_id').val( source_node_id );
559 $('#delete_target_node_id').val( target_node_id );
560 self.showinfo(relation);
561 }, 500 )
562 }, 1000 );
563 });
564 relation_path.mouseleave( function(event) {
565 clearTimeout(outerTimer);
566 if( timer != null ) { clearTimeout(timer); }
567 });
568 relation.data( 'active', true );
569 } else {
570 relation_path.unbind( 'mouseenter' );
571 relation_path.unbind( 'mouseleave' );
572 relation_path.css( {'cursor':'inherit'} );
573 relation.data( 'active', false );
574 }
575 }
576 this.showinfo = function(relation) {
088a14af 577 $('#delete_relation_type').text( relation.data('type') );
578 $('#delete_relation_scope').text( relation.data('scope') );
69a19c91 579 if( relation.data( 'note' ) ) {
088a14af 580 $('#delete_relation_note').text('note: ' + relation.data( 'note' ) );
69a19c91 581 }
9529f69c 582 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
583 var xs = parseFloat( points[0].split(',')[0] );
584 var xe = parseFloat( points[1].split(',')[0] );
585 var ys = parseFloat( points[0].split(',')[1] );
586 var ye = parseFloat( points[3].split(',')[1] );
587 var p = svg_root.createSVGPoint();
588 p.x = xs + ((xe-xs)*1.1);
589 p.y = ye - ((ye-ys)/2);
76f05423 590 var ctm = svg_root_element.getScreenCTM();
9529f69c 591 var nx = p.matrixTransform(ctm).x;
592 var ny = p.matrixTransform(ctm).y;
593 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
594 $('#delete-form').dialog( 'open' );
595 dialog_aria.offset({ left: nx, top: ny });
596 }
597 this.remove = function( relation_id ) {
30d0ba1e 598 if( !editable ) {
599 return;
600 }
45ee3b96 601 var relation = $( jq( relation_id ) );
9529f69c 602 relation.remove();
b28e606e 603 }
604}
605
45ee3b96 606// Utility function to create/return the ID of a relation link between
607// a source and target.
608function get_relation_id( source_id, target_id ) {
609 var idlist = [ source_id, target_id ];
610 idlist.sort();
611 return 'relation-' + idlist[0] + '-...-' + idlist[1];
612}
613
614function get_related_nodes( relation_id ) {
615 var srctotarg = relation_id.substr( 9 );
616 return srctotarg.split('-...-');
617}
618
b28e606e 619function draw_relation( source_id, target_id, relation_color ) {
9529f69c 620 var source_ellipse = get_ellipse( source_id );
621 var target_ellipse = get_ellipse( target_id );
45ee3b96 622 var relation_id = get_relation_id( source_id, target_id );
9529f69c 623 var svg = $('#svgenlargement').children('svg').svg().svg('get');
624 var path = svg.createPath();
625 var sx = parseInt( source_ellipse.attr('cx') );
626 var rx = parseInt( source_ellipse.attr('rx') );
627 var sy = parseInt( source_ellipse.attr('cy') );
628 var ex = parseInt( target_ellipse.attr('cx') );
629 var ey = parseInt( target_ellipse.attr('cy') );
45ee3b96 630 var relation = svg.group( $("#svgenlargement svg g"),
631 { 'class':'relation', 'id':relation_id } );
9529f69c 632 svg.title( relation, source_id + '->' + target_id );
633 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 634 var relation_element = $('#svgenlargement .relation').filter( ':last' );
635 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
9529f69c 636 return relation_element;
b28e606e 637}
638
a471cb0f 639function detach_node( readingsgohere ) {
60c66cd2 640
641 // This method is work in progress
642 // Todos:
643 // 1) Unproven/untested: readings.each will get us in trouble most likely for
644 // duplicating edges in a strand that both are incoming and outgoing
645 // like b and c in -i-> a -ii-> b -iii-> c -iv->
646 // 2) Added edges and nodes look rough and unsmoothed, what the f.?
647 //
648
a471cb0f 649 var readings = { "n127_0":
650 {
651 "grammar_invalid": null,
652 "witnesses": ["Sg524"],
653 "normal_form": "Secundo ",
654 "is_nonsense": null,
655 "lexemes": [],
656 "variants": [],
657 "text": "Secundo",
658 "is_meta": null,
659 "orig_rdg": "n127"
660 }
661 }
662
663 // add new node(s)
664 $.extend( readingdata, readings );
665 // remove from existing readings the witnesses for the new nodes/readings
666 $.each( readings, function( node_id, reading ) {
667 $.each( reading.witnesses, function( index, witness ) {
668 var witnesses = readingdata[ reading.orig_rdg ].witnesses;
669 readingdata[ reading.orig_rdg ].witnesses = $.removeFromArray( witness, witnesses );
670 } );
671 } );
672
a471cb0f 673 detached_edges = [];
8cd2e785 674
675 // here we detach witnesses from the existing edges accoring to what's being relayed by readings
a471cb0f 676 $.each( readings, function( node_id, reading ) {
677 var edges = edges_of( get_ellipse( reading.orig_rdg ) );
678 incoming_remaining = [];
679 outgoing_remaining = [];
680 $.each( reading.witnesses, function( index, witness ) {
681 incoming_remaining.push( witness );
682 outgoing_remaining.push( witness );
683 } );
684 $.each( edges, function( index, edge ) {
685 detached_edge = edge.detach_witnesses( reading.witnesses );
686 if( detached_edge != null ) {
687 detached_edges.push( detached_edge );
688 $.each( detached_edge.witnesses, function( index, witness ) {
689 if( detached_edge.is_incoming == true ) {
690 incoming_remaining = $.removeFromArray( witness, incoming_remaining );
691 } else {
692 outgoing_remaining = $.removeFromArray( witness, outgoing_remaining );
693 }
694 } );
695 }
696 } );
8cd2e785 697
60c66cd2 698 // After detaching we still need to check if for *all* readings
8cd2e785 699 // an edge was detached. It may be that a witness was not
700 // explicitly named on an edge but was part of a 'majority' edge
701 // in which case we need to duplicate and name that edge after those
702 // remaining witnesses.
a471cb0f 703 if( outgoing_remaining.length > 0 ) {
a471cb0f 704 $.each( edges, function( index, edge ) {
a471cb0f 705 if( edge.get_label() == 'majority' && !edge.is_incoming ) {
706 detached_edges.push( edge.clone_for( outgoing_remaining ) );
707 }
708 } );
709 }
710 if( incoming_remaining.length > 0 ) {
a471cb0f 711 $.each( edges, function( index, edge ) {
712 if( edge.get_label() == 'majority' && edge.is_incoming ) {
713 detached_edges.push( edge.clone_for( outgoing_remaining ) );
714 }
715 } );
716 }
60c66cd2 717
718 // Lots of unabstracted knowledge down here :/
719 // Clone original node/reading, rename/id it..
720 duplicate_node = get_ellipse( reading.orig_rdg ).parent().clone();
721 duplicate_node.attr( 'id', node_id );
722 duplicate_node.children( 'title' ).text( node_id );
723
724 // Add the node and all new edges into the graph
725 var graph_root = $('#svgenlargement svg g');
726 graph_root.append( duplicate_node );
727 $.each( detached_edges, function( index, edge ) {
728 edge.g_elem.attr( 'id', ( edge.g_elem.attr( 'id' ) + "_0" ) );
729 edge_title = edge.g_elem.children( 'title' ).text();
730 edge_title = edge_title.replace( reading.orig_rdg, node_id );
731 edge.g_elem.children( 'title' ).text( edge_title );
732 // Reg unabstracted knowledge: isn't it more elegant to make
733 // it edge.append_to( graph_root )?
734 graph_root.append( edge.g_elem );
735 } );
736
737 // Move the node somewhat up for 'dramatic effect' :-p
738 var node_elements = node_elements_for( get_ellipse( node_id ) );
739 $.each( node_elements, function( index, element ) {
740 element.move( 0, -150 );
741 } );
742
a471cb0f 743 } );
60c66cd2 744
a471cb0f 745
746}
747
c1add777 748function Marquee() {
749
750 var self = this;
751
f6516f22 752 this.x = 0;
753 this.y = 0;
754 this.dx = 0;
755 this.dy = 0;
c1add777 756 this.enlargementOffset = $('#svgenlargement').offset();
757 this.svg_rect = $('#svgenlargement svg').svg('get');
758
759 this.show = function( event ) {
760 // TODO: uncolor possible selected
761 // TODO: unless SHIFT?
f6516f22 762 self.x = event.clientX;
763 self.y = event.clientY;
c1add777 764 p = svg_root.createSVGPoint();
765 p.x = event.clientX - self.enlargementOffset.left;
766 p.y = event.clientY - self.enlargementOffset.top;
c1add777 767 self.svg_rect.rect( p.x, p.y, 0, 0, { fill: 'black', 'fill-opacity': '0.1', stroke: 'black', 'stroke-dasharray': '4,2', strokeWidth: '0.02em', id: 'marquee' } );
768 };
769
770 this.expand = function( event ) {
f6516f22 771 self.dx = (event.clientX - self.x);
772 self.dy = (event.clientY - self.y);
c1add777 773 var rect = $('#marquee');
f6516f22 774 if( rect.length != 0 ) {
775 var rect_w = Math.abs( self.dx );
776 var rect_h = Math.abs( self.dy );
777 var rect_x = self.x - self.enlargementOffset.left;
778 var rect_y = self.y - self.enlargementOffset.top;
779 if( self.dx < 0 ) { rect_x = rect_x - rect_w }
780 if( self.dy < 0 ) { rect_y = rect_y - rect_h }
781 rect.attr("x", rect_x).attr("y", rect_y).attr("width", rect_w).attr("height", rect_h);
c1add777 782 }
783 };
784
a84ca4de 785 this.select = function() {
c1add777 786 var rect = $('#marquee');
787 if( rect.length != 0 ) {
e538eccb 788 //unselect any possible selected first
789 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
790 $('ellipse[fill="#9999ff"]').each( function() {
791 $(this).data( 'node_obj' ).set_draggable( false );
792 } );
793 }
794 //compute dimension of marquee
c1add777 795 var left = $('#marquee').offset().left;
796 var top = $('#marquee').offset().top;
797 var right = left + parseInt( $('#marquee').attr( 'width' ) );
798 var bottom = top + parseInt( $('#marquee').attr( 'height' ) );
799 var tf = svg_root_element.getScreenCTM().inverse();
800 var p = svg_root.createSVGPoint();
801 p.x=left;
802 p.y=top;
803 var cx_min = p.matrixTransform(tf).x;
804 var cy_min = p.matrixTransform(tf).y;
805 p.x=right;
806 p.y=bottom;
807 var cx_max = p.matrixTransform(tf).x;
808 var cy_max = p.matrixTransform(tf).y;
e538eccb 809 //select any node with its center inside the marquee
810 //also merge witness sets from nodes
a84ca4de 811 var witnesses = [];
c1add777 812 $('#svgenlargement ellipse').each( function( index ) {
813 var cx = parseInt( $(this).attr('cx') );
814 var cy = parseInt( $(this).attr('cy') );
815 if( cx > cx_min && cx < cx_max) {
816 if( cy > cy_min && cy < cy_max) {
817 // we actually heve no real 'selected' state for nodes, except coloring
e538eccb 818 $(this).attr( 'fill', '#9999ff' );
a84ca4de 819 var this_witnesses = $(this).data( 'node_obj' ).get_witnesses();
820 witnesses = arrayUnique( witnesses.concat( this_witnesses ) );
c1add777 821 }
822 }
823 });
e538eccb 824 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
8cd2e785 825 //add interesectio of witnesses sets to the multi select form and open it
a84ca4de 826 $.each( witnesses, function( index, value ) {
8cd2e785 827 $('#multipleselect-form').append( '<input type="checkbox" name="witnesses" value="' + value + '">' + value + '<br>' );
a84ca4de 828 });
829 $('#multipleselect-form').dialog( 'open' );
830 }
c1add777 831 self.svg_rect.remove( $('#marquee') );
832 }
833 };
834
f6516f22 835 this.unselect = function() {
e538eccb 836 $('ellipse[fill="#9999ff"]').attr( 'fill', '#fff' );
f6516f22 837 }
838
c1add777 839}
840
9529f69c 841
b28e606e 842$(document).ready(function () {
9529f69c 843
844 timer = null;
b28e606e 845 relation_manager = new relation_factory();
846
c1add777 847 $('#update_workspace_button').data('locked', false);
848
9529f69c 849 $('#enlargement').mousedown(function (event) {
b28e606e 850 $(this)
9529f69c 851 .data('down', true)
852 .data('x', event.clientX)
853 .data('y', event.clientY)
854 .data('scrollLeft', this.scrollLeft)
c1add777 855 stateTf = svg_root_element.getCTM().inverse();
856 var p = svg_root.createSVGPoint();
857 p.x = event.clientX;
858 p.y = event.clientY;
859 stateOrigin = p.matrixTransform(stateTf);
860
861 // Activate marquee if in interaction mode
862 if( $('#update_workspace_button').data('locked') == true ) { marquee.show( event ) };
863
864 event.returnValue = false;
865 event.preventDefault();
866 return false;
b28e606e 867 }).mouseup(function (event) {
a84ca4de 868 marquee.select();
c1add777 869 $(this).data('down', false);
b28e606e 870 }).mousemove(function (event) {
9529f69c 871 if( timer != null ) { clearTimeout(timer); }
872 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
873 var p = svg_root.createSVGPoint();
874 p.x = event.clientX;
875 p.y = event.clientY;
876 p = p.matrixTransform(stateTf);
877 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
878 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
76f05423 879 svg_root_element.setAttribute("transform", s);
b28e606e 880 }
c1add777 881 marquee.expand( event );
76f05423 882 event.returnValue = false;
883 event.preventDefault();
b28e606e 884 }).mousewheel(function (event, delta) {
9529f69c 885 event.returnValue = false;
886 event.preventDefault();
887 if ( $('#update_workspace_button').data('locked') == false ) {
76f05423 888 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
889 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
9529f69c 890 if( delta < -9 ) { delta = -9 };
891 var z = 1 + delta/10;
76f05423 892 z = delta > 0 ? 1 : -1;
893 var g = svg_root_element;
894 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) {
9529f69c 895 var root = svg_root;
896 var p = root.createSVGPoint();
76f05423 897 p.x = event.originalEvent.clientX;
898 p.y = event.originalEvent.clientY;
9529f69c 899 p = p.matrixTransform(g.getCTM().inverse());
76f05423 900 var scaleLevel = 1+(z/20);
901 var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y);
9529f69c 902 var matrix = g.getCTM().multiply(k);
903 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
904 g.setAttribute("transform", s);
905 }
906 }
b28e606e 907 }).css({
908 'overflow' : 'hidden',
909 'cursor' : '-moz-grab'
910 });
911
c1add777 912
30d0ba1e 913 if( editable ) {
914 $( "#dialog-form" ).dialog({
915 autoOpen: false,
916 height: 270,
917 width: 290,
918 modal: true,
919 buttons: {
920 "Ok": function( evt ) {
921 $(evt.target).button("disable");
922 $('#status').empty();
923 form_values = $('#collapse_node_form').serialize();
924 ncpath = getTextURL( 'relationships' );
925 var jqjson = $.post( ncpath, form_values, function(data) {
926 $.each( data, function(item, source_target) {
927 var source_found = get_ellipse( source_target[0] );
928 var target_found = get_ellipse( source_target[1] );
929 var relation_found = $.inArray( source_target[2], $('#keymap').data('relations') );
930 if( source_found.size() && target_found.size() && relation_found > -1 ) {
931 var relation = relation_manager.create( source_target[0], source_target[1], relation_found );
eeea8fb6 932 relation.data( 'type', source_target[2] );
7b54e481 933 relation.data( 'scope', $('#scope :selected').text() );
934 relation.data( 'note', $('#note').val() );
45ee3b96 935 relation_manager.toggle_active( relation.attr('id') );
7b54e481 936 }
30d0ba1e 937 $(evt.target).button("enable");
938 });
939 $( "#dialog-form" ).dialog( "close" );
940 }, 'json' );
941 },
942 Cancel: function() {
943 $( this ).dialog( "close" );
944 }
945 },
946 create: function(event, ui) {
947 $(this).data( 'relation_drawn', false );
a166dca8 948 $('#rel_type').data( 'changed_after_open', false );
56e3972e 949 $.each( relationship_types, function(index, typedef) {
950 $('#rel_type').append( $('<option />').attr( "value", typedef.name ).text(typedef.name) );
951 });
952 $.each( relationship_scopes, function(index, value) {
953 $('#scope').append( $('<option />').attr( "value", value ).text(value) );
a166dca8 954 });
955 // Handler to clear the annotation field, the first time the relationship is
956 // changed after opening the form.
957 $('#rel_type').change( function () {
958 if( !$(this).data( 'changed_after_open' ) ) {
959 $('#note').val('');
960 }
961 $(this).data( 'changed_after_open', true );
962 });
30d0ba1e 963 },
964 open: function() {
965 relation_manager.create_temporary( $('#source_node_id').val(), $('#target_node_id').val() );
966 $(".ui-widget-overlay").css("background", "none");
967 $("#dialog_overlay").show();
968 $("#dialog_overlay").height( $("#enlargement_container").height() );
969 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
970 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
a166dca8 971 $('#rel_type').data( 'changed_after_open', false );
30d0ba1e 972 },
973 close: function() {
974 relation_manager.remove_temporary();
975 $( '#status' ).empty();
976 $("#dialog_overlay").hide();
977 }
978 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
21e6ebc7 979 if( ajaxSettings.url == getTextURL('relationships')
980 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
981 var error;
982 if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
983 error = 'You are not authorized to modify this tradition. (Try logging in again?)';
984 } else {
985 try {
986 var errobj = jQuery.parseJSON( jqXHR.responseText );
987 error = errobj.error + '</br>The relationship cannot be made.</p>';
988 } catch(e) {
989 error = jqXHR.responseText;
990 }
991 }
992 $('#status').append( '<p class="error">Error: ' + error );
993 }
994 $(event.target).parent().find('.ui-button').button("enable");
30d0ba1e 995 } );
996 }
b28e606e 997
088a14af 998 var deletion_buttonset = {
999 cancel: function() { $( this ).dialog( "close" ); },
1000 global: function () { delete_relation( true ); },
1001 delete: function() { delete_relation( false ); }
1002 };
a84ca4de 1003
9529f69c 1004 $( "#delete-form" ).dialog({
1005 autoOpen: false,
69a19c91 1006 height: 135,
088a14af 1007 width: 250,
9529f69c 1008 modal: false,
9529f69c 1009 create: function(event, ui) {
30d0ba1e 1010 // TODO What is this logic doing?
a84ca4de 1011 // This scales the buttons in the dialog and makes it look proper
1012 // Not sure how essential it is, does anything break if it's not here?
9529f69c 1013 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
1014 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
a84ca4de 1015 // A: This makes sure that the pop up delete relation dialogue for a hovered over
1016 // relation auto closes if the user doesn't engage (mouseover) with it.
9529f69c 1017 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
1018 dialog_aria.mouseenter( function() {
1019 if( mouseWait != null ) { clearTimeout(mouseWait) };
1020 })
1021 dialog_aria.mouseleave( function() {
1022 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
1023 })
1024 },
1025 open: function() {
088a14af 1026 if( !editable ) {
1027 $( this ).dialog( "option", "buttons",
1028 [{ text: "OK", click: deletion_buttonset['cancel'] }] );
1029 } else if( $('#delete_relation_scope').text() === 'local' ) {
1030 $( this ).dialog( "option", "width", 160 );
1031 $( this ).dialog( "option", "buttons",
1032 [{ text: "Delete", click: deletion_buttonset['delete'] },
1033 { text: "Cancel", click: deletion_buttonset['cancel'] }] );
1034 } else {
1035 $( this ).dialog( "option", "width", 200 );
1036 $( this ).dialog( "option", "buttons",
1037 [{ text: "Delete", click: deletion_buttonset['delete'] },
1038 { text: "Delete all", click: deletion_buttonset['global'] },
1039 { text: "Cancel", click: deletion_buttonset['cancel'] }] );
1040 }
1041
9529f69c 1042 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
1043 },
088a14af 1044 close: function() {}
9529f69c 1045 });
1046
8cd2e785 1047 var multipleselect_buttonset = {
1048 cancel: function() { $( this ).dialog( "close" ); },
1049 button1: function () { },
1050 button2: function() { }
1051 };
1052
a84ca4de 1053 $( "#multipleselect-form" ).dialog({
1054 autoOpen: false,
1055 height: 150,
1056 width: 250,
1057 modal: true,
1058 create: function(event, ui) {
1059 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
1060 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
1061 },
1062 open: function() {
1063 $( this ).dialog( "option", "width", 200 );
8cd2e785 1064 $( this ).dialog( "option", "buttons",
1065 [{ text: "Button_1", click: multipleselect_buttonset['button1'] },
1066 { text: "Button_2", click: multipleselect_buttonset['button2'] },
1067 { text: "Cancel", click: multipleselect_buttonset['cancel'] }] );
e538eccb 1068 $(".ui-widget-overlay").css("background", "none");
1069 $("#dialog_overlay").show();
1070 $("#dialog_overlay").height( $("#enlargement_container").height() );
1071 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
1072 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
a84ca4de 1073 },
e538eccb 1074 close: function() {
1075 marquee.unselect();
1076 $("#dialog_overlay").hide();
1077 }
8cd2e785 1078 });
a84ca4de 1079
088a14af 1080 // Helpers for relationship deletion
1081
1082 function delete_relation( scopewide ) {
1083 form_values = $('#delete_relation_form').serialize();
1084 if( scopewide ) {
1085 form_values += "&scopewide=true";
1086 }
1087 ncpath = getTextURL( 'relationships' );
1088 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
1089 $.each( data, function(item, source_target) {
1090 relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );
1091 });
1092 $( "#delete-form" ).dialog( "close" );
1093 }, dataType: 'json', type: 'DELETE' });
1094 }
1095
1096 function toggle_relation_active( node_id ) {
1097 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
1098 matchid = new RegExp( "^" + node_id );
1099 if( $(this).text().match( matchid ) != null ) {
1100 var relation_id = $(this).parent().attr('id');
1101 relation_manager.toggle_active( relation_id );
1102 };
1103 });
1104 }
1105
487674b9 1106 // function for reading form dialog should go here;
1107 // just hide the element for now if we don't have morphology
1108 if( can_morphologize ) {
5539cba3 1109 if( editable ) {
1110 $('#reading_decollate_witnesses').multiselect();
1111 } else {
1112 $('#decollation').hide();
1113 }
487674b9 1114 $('#reading-form').dialog({
1115 autoOpen: false,
1116 // height: 400,
1117 width: 450,
1118 modal: true,
1119 buttons: {
1120 Cancel: function() {
1121 $( this ).dialog( "close" );
1122 },
1123 Update: function( evt ) {
1124 // Disable the button
1125 $(evt.target).button("disable");
1126 $('#reading_status').empty();
1127 var reading_id = $('#reading_id').val()
1128 form_values = {
1129 'id' : reading_id,
1130 'is_nonsense': $('#reading_is_nonsense').is(':checked'),
1131 'grammar_invalid': $('#reading_grammar_invalid').is(':checked'),
1132 'normal_form': $('#reading_normal_form').val() };
1133 // Add the morphology values
1134 $('.reading_morphology').each( function() {
1135 if( $(this).val() != '(Click to select)' ) {
1136 var rmid = $(this).attr('id');
1137 rmid = rmid.substring(8);
1138 form_values[rmid] = $(this).val();
1139 }
45ee3b96 1140 });
487674b9 1141 // Make the JSON call
1142 ncpath = getReadingURL( reading_id );
1143 var reading_element = readingdata[reading_id];
1144 // $(':button :contains("Update")').attr("disabled", true);
1145 var jqjson = $.post( ncpath, form_values, function(data) {
1146 $.each( data, function(key, value) {
1147 reading_element[key] = value;
1148 });
1149 if( $('#update_workspace_button').data('locked') == false ) {
1150 color_inactive( get_ellipse( reading_id ) );
1151 }
1152 $(evt.target).button("enable");
1153 $( "#reading-form" ).dialog( "close" );
1154 });
1155 // Re-color the node if necessary
1156 return false;
1157 }
1158 },
1159 create: function() {
30d0ba1e 1160 if( !editable ) {
1161 // Get rid of the disallowed editing UI bits
1162 $( this ).dialog( "option", "buttons",
1163 [{ text: "OK", click: function() { $( this ).dialog( "close" ); }}] );
1164 $('#reading_relemmatize').hide();
1165 }
487674b9 1166 },
1167 open: function() {
1168 $(".ui-widget-overlay").css("background", "none");
5539cba3 1169 $('#reading_decollate_witnesses').multiselect("refresh");
1170 $('#reading_decollate_witnesses').multiselect("uncheckAll");
487674b9 1171 $("#dialog_overlay").show();
1172 $('#reading_status').empty();
1173 $("#dialog_overlay").height( $("#enlargement_container").height() );
1174 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
1175 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
1176 $("#reading-form").parent().find('.ui-button').button("enable");
1177 },
1178 close: function() {
1179 $("#dialog_overlay").hide();
1180 }
1181 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
21e6ebc7 1182 if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1
487674b9 1183 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
21e6ebc7 1184 var error;
1185 if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
1186 error = 'You are not authorized to modify this tradition. (Try logging in again?)';
1187 } else {
1188 try {
1189 var errobj = jQuery.parseJSON( jqXHR.responseText );
1190 error = errobj.error + '</br>The relationship cannot be made.</p>';
1191 } catch(e) {
1192 error = jqXHR.responseText;
1193 }
1194 }
1195 $('#status').append( '<p class="error">Error: ' + error );
1196 }
1197 $(event.target).parent().find('.ui-button').button("enable");
487674b9 1198 });
1199 } else {
1200 $('#reading-form').hide();
45ee3b96 1201 }
4c41c02c 1202
76f05423 1203
b28e606e 1204 $('#update_workspace_button').click( function() {
30d0ba1e 1205 if( !editable ) {
1206 return;
1207 }
b28e606e 1208 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
76f05423 1209 mouse_scale = svg_root_element.getScreenCTM().a;
9529f69c 1210 if( $(this).data('locked') == true ) {
1211 $('#svgenlargement ellipse' ).each( function( index ) {
1212 if( $(this).data( 'node_obj' ) != null ) {
1213 $(this).data( 'node_obj' ).ungreyout_edges();
e538eccb 1214 $(this).data( 'node_obj' ).set_selectable( false );
1215 color_inactive( $(this) );
9529f69c 1216 var node_id = $(this).data( 'node_obj' ).get_id();
1217 toggle_relation_active( node_id );
1218 $(this).data( 'node_obj', null );
1219 }
b28e606e 1220 })
b28e606e 1221 $(this).data('locked', false);
9529f69c 1222 $(this).css('background-position', '0px 44px');
b28e606e 1223 } else {
9529f69c 1224 var left = $('#enlargement').offset().left;
1225 var right = left + $('#enlargement').width();
76f05423 1226 var tf = svg_root_element.getScreenCTM().inverse();
9529f69c 1227 var p = svg_root.createSVGPoint();
1228 p.x=left;
1229 p.y=100;
1230 var cx_min = p.matrixTransform(tf).x;
1231 p.x=right;
1232 var cx_max = p.matrixTransform(tf).x;
1233 $('#svgenlargement ellipse').each( function( index ) {
1234 var cx = parseInt( $(this).attr('cx') );
1235 if( cx > cx_min && cx < cx_max) {
1236 if( $(this).data( 'node_obj' ) == null ) {
1237 $(this).data( 'node_obj', new node_obj( $(this) ) );
1238 } else {
e538eccb 1239 $(this).data( 'node_obj' ).set_selectable( true );
9529f69c 1240 }
1241 $(this).data( 'node_obj' ).greyout_edges();
1242 var node_id = $(this).data( 'node_obj' ).get_id();
1243 toggle_relation_active( node_id );
b28e606e 1244 }
9529f69c 1245 });
1246 $(this).css('background-position', '0px 0px');
b28e606e 1247 $(this).data('locked', true );
b28e606e 1248 }
1249 });
30d0ba1e 1250
1251 if( !editable ) {
1252 // Hide the unused elements
1253 $('#dialog-form').hide();
1254 $('#update_workspace_button').hide();
1255 }
1256
b28e606e 1257
e847b186 1258 $('.helptag').popupWindow({
1259 height:500,
1260 width:800,
1261 top:50,
1262 left:50,
1263 scrollbars:1
1264 });
1265
76f05423 1266 expandFillPageClients();
1267 $(window).resize(function() {
1268 expandFillPageClients();
1269 });
1270
9529f69c 1271});
b28e606e 1272
1273
76f05423 1274function expandFillPageClients() {
1275 $('.fillPage').each(function () {
1276 $(this).height($(window).height() - $(this).offset().top - MARGIN);
1277 });
1278}
1279
1280function loadSVG(svgData) {
1281 var svgElement = $('#svgenlargement');
1282
1283 $(svgElement).svg('destroy');
1284
1285 $(svgElement).svg({
1286 loadURL: svgData,
1287 onLoad : svgEnlargementLoaded
1288 });
1289}
1290
1291
c1add777 1292
76f05423 1293/* OS Gadget stuff
1294
1295function svg_select_callback(topic, data, subscriberData) {
1296 svgData = data;
1297 loadSVG(svgData);
1298}
1299
1300function loaded() {
1301 var prefs = new gadgets.Prefs();
1302 var preferredHeight = parseInt(prefs.getString('height'));
1303 if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight);
1304 expandFillPageClients();
1305}
1306
1307if (gadgets.util.hasFeature('pubsub-2')) {
1308 gadgets.HubSettings.onConnect = function(hum, suc, err) {
1309 subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback);
1310 loaded();
1311 };
1312}
1313else gadgets.util.registerOnLoadHandler(loaded);
1314*/