3 var svg_root_element = null;
4 var start_element_height = 0;
8 jQuery.removeFromArray = function(value, arr) {
9 return jQuery.grep(arr, function(elem, index) {
10 return elem !== value;
14 function 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) {
25 function getTextURL( which ) {
26 return basepath + textid + '/' + which;
29 function getReadingURL( reading_id ) {
30 return basepath + textid + '/reading/' + reading_id;
33 // Make an XML ID into a valid selector
35 return '#' + myid.replace(/(:|\.)/g,'\\$1');
38 // Actions for opening the reading panel
39 function 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 );
48 toggle_checkbox( $('#reading_is_nonsense'), reading_info['is_nonsense'] );
49 toggle_checkbox( $('#reading_grammar_invalid'), reading_info['grammar_invalid'] );
50 // Use .text as a backup for .normal_form
51 var normal_form = reading_info['normal_form'];
53 normal_form = reading_info['text'];
56 if( normal_form.length > 9 ) {
57 nfboxsize = normal_form.length + 1;
59 $('#reading_normal_form').attr( 'size', nfboxsize )
60 $('#reading_normal_form').val( normal_form );
61 // Now do the morphological properties.
62 morphology_form( reading_info['lexemes'] );
63 // and then open the dialog.
64 $('#reading-form').dialog("open");
67 function toggle_checkbox( box, value ) {
71 box.attr('checked', value );
74 function morphology_form ( lexlist ) {
75 if( lexlist.length ) {
76 $('#morph_outer').show();
77 $('#morphology').empty();
78 $.each( lexlist, function( idx, lex ) {
79 var morphoptions = [];
80 if( 'wordform_matchlist' in lex ) {
81 $.each( lex['wordform_matchlist'], function( tdx, tag ) {
82 var tagstr = stringify_wordform( tag );
83 morphoptions.push( tagstr );
86 var formtag = 'morphology_' + idx;
89 formstr = stringify_wordform( lex['form'] );
91 var form_morph_elements = morph_elements(
92 formtag, lex['string'], formstr, morphoptions );
93 $.each( form_morph_elements, function( idx, el ) {
94 $('#morphology').append( el );
98 $('#morph_outer').hide();
102 function stringify_wordform ( tag ) {
104 var elements = tag.split(' // ');
105 return elements[1] + ' // ' + elements[2];
110 function morph_elements ( formtag, formtxt, currform, morphoptions ) {
111 var clicktag = '(Click to select)';
115 var formlabel = $('<label/>').attr( 'id', 'label_' + formtag ).attr(
116 'for', 'reading_' + formtag ).text( formtxt + ': ' );
117 var forminput = $('<input/>').attr( 'id', 'reading_' + formtag ).attr(
118 'name', 'reading_' + formtag ).attr( 'size', '50' ).attr(
119 'class', 'reading_morphology' ).val( currform );
120 forminput.autocomplete({ source: morphoptions, minLength: 0 });
121 forminput.focus( function() {
122 if( $(this).val() == clicktag ) {
125 $(this).autocomplete('search', '')
127 var morphel = [ formlabel, forminput, $('<br/>') ];
131 function color_inactive ( el ) {
132 var reading_id = $(el).parent().attr('id');
133 var reading_info = readingdata[reading_id];
134 // If the reading info has any non-disambiguated lexemes, color it yellow;
135 // otherwise color it green.
136 $(el).attr( {stroke:'green', fill:'#b3f36d'} );
138 $.each( reading_info['lexemes'], function ( idx, lex ) {
139 if( !lex['is_disambiguated'] || lex['is_disambiguated'] == 0 ) {
140 $(el).attr( {stroke:'orange', fill:'#fee233'} );
146 function relemmatize () {
147 // Send the reading for a new lemmatization and reopen the form.
148 $('#relemmatize_pending').show();
149 var reading_id = $('#reading_id').val()
150 ncpath = getReadingURL( reading_id );
152 'normal_form': $('#reading_normal_form').val(),
154 var jqjson = $.post( ncpath, form_values, function( data ) {
155 // Update the form with the return
157 // We got back a good answer. Stash it
158 readingdata[reading_id] = data;
159 // and regenerate the morphology form.
160 morphology_form( data['lexemes'] );
162 alert("Could not relemmatize as requested: " + data['error']);
164 $('#relemmatize_pending').hide();
168 // Initialize the SVG once it exists
169 function svgEnlargementLoaded() {
170 //Give some visual evidence that we are working
171 $('#loading_overlay').show();
172 lo_height = $("#enlargement_container").outerHeight();
173 lo_width = $("#enlargement_container").outerWidth();
174 $("#loading_overlay").height( lo_height );
175 $("#loading_overlay").width( lo_width );
176 $("#loading_overlay").offset( $("#enlargement_container").offset() );
177 $("#loading_message").offset(
178 { 'top': lo_height / 2 - $("#loading_message").height() / 2,
179 'left': lo_width / 2 - $("#loading_message").width() / 2 });
181 // Show the update toggle button.
182 $('#update_workspace_button').data('locked', false);
183 $('#update_workspace_button').css('background-position', '0px 44px');
185 $('#svgenlargement ellipse').parent().dblclick( node_dblclick_listener );
186 var graph_svg = $('#svgenlargement svg');
187 var svg_g = $('#svgenlargement svg g')[0];
189 svg_root = graph_svg.svg().svg('get').root();
191 // Find the real root and ignore any text nodes
192 for (i = 0; i < svg_root.childNodes.length; ++i) {
193 if (svg_root.childNodes[i].nodeName != '#text') {
194 svg_root_element = svg_root.childNodes[i];
199 //Set viewbox width and height to width and height of $('#svgenlargement svg').
200 //This is essential to make sure zooming and panning works properly.
201 svg_root.viewBox.baseVal.width = graph_svg.attr( 'width' );
202 svg_root.viewBox.baseVal.height = graph_svg.attr( 'height' );
203 //Now set scale and translate so svg height is about 150px and vertically centered in viewbox.
204 //This is just to create a nice starting enlargement.
205 var initial_svg_height = 250;
206 var scale = initial_svg_height/graph_svg.attr( 'height' );
207 var additional_translate = (graph_svg.attr( 'height' ) - initial_svg_height)/(2*scale);
208 var transform = svg_g.getAttribute('transform');
209 var translate = parseFloat( transform.match( /translate\([^\)]*\)/ )[0].split('(')[1].split(' ')[1].split(')')[0] );
210 translate += additional_translate;
211 var transform = 'rotate(0) scale(' + scale + ') translate(4 ' + translate + ')';
212 svg_g.setAttribute('transform', transform);
213 //used to calculate min and max zoom level:
214 start_element_height = $('#__START__').children('ellipse')[0].getBBox().height;
215 //some use of call backs to ensure succesive execution
216 add_relations( function() {
217 var rdgpath = getTextURL( 'readings' );
218 $.getJSON( rdgpath, function( data ) {
220 $('#svgenlargement ellipse').each( function( i, el ) { color_inactive( el ) });
221 $('#loading_overlay').hide();
226 marquee = new Marquee();
228 if (text_direction == 'RL') {
233 function add_relations( callback_fn ) {
234 // Add the relationship types to the keymap list
235 $.each( relationship_types, function(index, typedef) {
236 li_elm = $('<li class="key">').css( "border-color",
237 relation_manager.relation_colors[index] ).text(typedef.name);
238 li_elm.append( $('<div>').attr('class', 'key_tip_container').append(
239 $('<div>').attr('class', 'key_tip').text(typedef.description) ) );
240 $('#keymaplist').append( li_elm );
242 // Now fetch the relationships themselves and add them to the graph
243 var rel_types = $.map( relationship_types, function(t) { return t.name });
244 // Save this list of names to the outer element data so that the relationship
245 // factory can access it
246 $('#keymap').data('relations', rel_types);
247 var textrelpath = getTextURL( 'relationships' );
248 $.getJSON( textrelpath, function(data) {
249 $.each(data, function( index, rel_info ) {
250 var type_index = $.inArray(rel_info.type, rel_types);
251 var source_found = get_ellipse( rel_info.source_id );
252 var target_found = get_ellipse( rel_info.target_id );
253 var emphasis = rel_info.is_significant;
254 if( type_index != -1 && source_found.size() && target_found.size() ) {
255 var relation = relation_manager.create( rel_info.source_id, rel_info.target_id, type_index, emphasis );
256 // Save the relationship data too.
257 $.each( rel_info, function( k, v ) {
258 relation.data( k, v );
261 var node_obj = get_node_obj(rel_info.source_id);
262 node_obj.set_selectable( false );
263 node_obj.ellipse.data( 'node_obj', null );
264 node_obj = get_node_obj(rel_info.target_id);
265 node_obj.set_selectable( false );
266 node_obj.ellipse.data( 'node_obj', null );
274 function get_ellipse( node_id ) {
275 return $( jq( node_id ) + ' ellipse');
278 function get_node_obj( node_id ) {
279 var node_ellipse = get_ellipse( node_id );
280 if( node_ellipse.data( 'node_obj' ) == null ) {
281 node_ellipse.data( 'node_obj', new node_obj(node_ellipse) );
283 return node_ellipse.data( 'node_obj' );
286 function node_obj(ellipse) {
287 this.ellipse = ellipse;
294 this.node_elements = node_elements_for(self.ellipse);
296 this.get_id = function() {
297 return $(self.ellipse).parent().attr('id')
300 this.set_selectable = function( clickable ) {
301 if( clickable && editable ) {
302 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
303 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
304 $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } );
305 $(self.ellipse).parent().click( function(evt) {
306 evt.stopPropagation();
307 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
308 $('ellipse[fill="#9999ff"]').each( function() {
309 $(this).data( 'node_obj' ).set_draggable( false );
312 self.set_draggable( true )
315 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
316 self.ellipse.siblings('text').attr('class', '');
317 $(self.ellipse).parent().unbind();
318 $('body').unbind('mousemove');
319 $('body').unbind('mouseup');
323 this.set_draggable = function( draggable ) {
324 if( draggable && editable ) {
325 $(self.ellipse).attr( {stroke:'black', fill:'#9999ff'} );
326 $(self.ellipse).parent().mousedown( this.mousedown_listener );
327 $(self.ellipse).parent().unbind( 'mouseenter' ).unbind( 'mouseleave' );
328 self.ellipse.siblings('text').attr('class', 'noselect draggable');
330 $(self.ellipse).attr( {stroke:'black', fill:'#fff'} );
331 self.ellipse.siblings('text').attr('class', '');
332 $(self.ellipse).parent().unbind( 'mousedown ');
333 $(self.ellipse).parent().mousedown( function(evt) { evt.stopPropagation() } );
334 $(self.ellipse).parent().hover( this.enter_node, this.leave_node );
338 this.mousedown_listener = function(evt) {
339 evt.stopPropagation();
340 self.x = evt.clientX;
341 self.y = evt.clientY;
342 $('body').mousemove( self.mousemove_listener );
343 $('body').mouseup( self.mouseup_listener );
344 $(self.ellipse).parent().unbind('mouseenter').unbind('mouseleave')
345 self.ellipse.attr( 'fill', '#6b6bb2' );
346 first_node_g_element = $("#svgenlargement g .node" ).filter( ":first" );
347 if( first_node_g_element.attr('id') !== self.get_g().attr('id') ) { self.get_g().insertBefore( first_node_g_element ) };
350 this.mousemove_listener = function(evt) {
351 self.dx = (evt.clientX - self.x) / mouse_scale;
352 self.dy = (evt.clientY - self.y) / mouse_scale;
353 self.move_elements();
354 evt.returnValue = false;
355 evt.preventDefault();
359 this.mouseup_listener = function(evt) {
360 if( $('ellipse[fill="#ffccff"]').size() > 0 ) {
361 var source_node_id = $(self.ellipse).parent().attr('id');
362 var source_node_text = self.ellipse.siblings('text').text();
363 var target_node_id = $('ellipse[fill="#ffccff"]').parent().attr('id');
364 var target_node_text = $('ellipse[fill="#ffccff"]').siblings("text").text();
365 $('#source_node_id').val( source_node_id );
366 $('#source_node_text').val( source_node_text );
367 $('.rel_rdg_a').text( "'" + source_node_text + "'" );
368 $('#target_node_id').val( target_node_id );
369 $('#target_node_text').val( target_node_text );
370 $('.rel_rdg_b').text( "'" + target_node_text + "'" );
371 $('#dialog-form').dialog( 'open' );
373 $('body').unbind('mousemove');
374 $('body').unbind('mouseup');
375 self.ellipse.attr( 'fill', '#9999ff' );
376 self.reset_elements();
379 this.cpos = function() {
380 return { x: self.ellipse.attr('cx'), y: self.ellipse.attr('cy') };
383 this.get_g = function() {
384 return self.ellipse.parent('g');
387 this.enter_node = function(evt) {
388 self.ellipse.attr( 'fill', '#ffccff' );
391 this.leave_node = function(evt) {
392 self.ellipse.attr( 'fill', '#fff' );
395 this.greyout_edges = function() {
396 $.each( self.node_elements, function(index, value) {
397 value.grey_out('.edge');
401 this.ungreyout_edges = function() {
402 $.each( self.node_elements, function(index, value) {
403 value.un_grey_out('.edge');
407 this.reposition = function( dx, dy ) {
408 $.each( self.node_elements, function(index, value) {
409 value.reposition( dx, dy );
413 this.move_elements = function() {
414 $.each( self.node_elements, function(index, value) {
415 value.move( self.dx, self.dy );
419 this.reset_elements = function() {
420 $.each( self.node_elements, function(index, value) {
425 this.update_elements = function() {
426 self.node_elements = node_elements_for(self.ellipse);
429 this.get_witnesses = function() {
430 return readingdata[self.get_id()].witnesses
433 self.set_selectable( true );
436 function svgshape( shape_element ) {
437 this.shape = shape_element;
440 this.repositioned = this.shape.parent().data( 'repositioned' );
441 if( this.repositioned != null ) {
442 this.reposx = this.repositioned[0];
443 this.reposy = this.repositioned[1];
445 this.reposition = function (dx, dy) {
447 this.reposx = this.reposx + dx;
448 this.reposy = this.reposy + dy;
449 this.shape.parent().data( 'repositioned', [this.reposx,this.reposy] );
451 this.move = function(dx,dy) {
452 this.shape.attr( "transform", "translate( " + (this.reposx + dx) + " " + (this.reposy + dy) + " )" );
454 this.reset = function() {
455 this.shape.attr( "transform", "translate( " + this.reposx + " " + this.reposy + " )" );
457 this.grey_out = function(filter) {
458 if( this.shape.parent(filter).size() != 0 ) {
459 this.shape.attr({'stroke':'#e5e5e5', 'fill':'#e5e5e5'});
462 this.un_grey_out = function(filter) {
463 if( this.shape.parent(filter).size() != 0 ) {
464 this.shape.attr({'stroke':'#000000', 'fill':'#000000'});
469 function svgpath( path_element, svg_element ) {
470 this.svg_element = svg_element;
471 this.path = path_element;
472 this.x = this.path.x;
473 this.y = this.path.y;
475 this.reposition = function (dx, dy) {
476 this.x = this.x + dx;
477 this.y = this.y + dy;
478 this.path.x = this.x;
479 this.path.y = this.y;
482 this.move = function(dx,dy) {
483 this.path.x = this.x + dx;
484 this.path.y = this.y + dy;
487 this.reset = function() {
488 this.path.x = this.x;
489 this.path.y = this.y;
492 this.grey_out = function(filter) {
493 if( this.svg_element.parent(filter).size() != 0 ) {
494 this.svg_element.attr('stroke', '#e5e5e5');
495 this.svg_element.siblings('text').attr('fill', '#e5e5e5');
496 this.svg_element.siblings('text').attr('class', 'noselect');
499 this.un_grey_out = function(filter) {
500 if( this.svg_element.parent(filter).size() != 0 ) {
501 this.svg_element.attr('stroke', '#000000');
502 this.svg_element.siblings('text').attr('fill', '#000000');
503 this.svg_element.siblings('text').attr('class', '');
508 function node_elements_for( ellipse ) {
509 node_elements = get_edge_elements_for( ellipse );
510 node_elements.push( new svgshape( ellipse.siblings('text') ) );
511 node_elements.push( new svgshape( ellipse ) );
512 return node_elements;
515 function get_edge_elements_for( ellipse ) {
516 edge_elements = new Array();
517 node_id = ellipse.parent().attr('id');
518 edge_in_pattern = new RegExp( node_id + '$' );
519 edge_out_pattern = new RegExp( '^' + node_id + '-' );
520 $.each( $('#svgenlargement .edge,#svgenlargement .relation').children('title'), function(index) {
521 title = $(this).text();
522 if( edge_in_pattern.test(title) ) {
523 polygon = $(this).siblings('polygon');
524 if( polygon.size() > 0 ) {
525 edge_elements.push( new svgshape( polygon ) );
527 path_segments = $(this).siblings('path')[0].pathSegList;
528 edge_elements.push( new svgpath( path_segments.getItem(path_segments.numberOfItems - 1), $(this).siblings('path') ) );
530 if( edge_out_pattern.test(title) ) {
531 path_segments = $(this).siblings('path')[0].pathSegList;
532 edge_elements.push( new svgpath( path_segments.getItem(0), $(this).siblings('path') ) );
535 return edge_elements;
538 function relation_factory() {
540 this.color_memo = null;
541 //TODO: colors hard coded for now
542 this.temp_color = '#FFA14F';
543 this.relation_colors = [ "#5CCCCC", "#67E667", "#F9FE72", "#6B90D4", "#FF7673", "#E467B3", "#AA67D5", "#8370D8", "#FFC173" ];
545 this.create_temporary = function( source_node_id, target_node_id ) {
546 var relation_id = get_relation_id( source_node_id, target_node_id );
547 var relation = $( jq( relation_id ) );
548 if( relation.size() == 0 ) {
549 draw_relation( source_node_id, target_node_id, self.temp_color );
551 self.color_memo = relation.children('path').attr( 'stroke' );
552 relation.children('path').attr( 'stroke', self.temp_color );
555 this.remove_temporary = function() {
556 var path_element = $('#svgenlargement .relation').children('path[stroke="' + self.temp_color + '"]');
557 if( self.color_memo != null ) {
558 path_element.attr( 'stroke', self.color_memo );
559 self.color_memo = null;
561 var temporary = path_element.parent('g').remove();
566 this.create = function( source_node_id, target_node_id, color_index, emphasis ) {
567 //TODO: Protect from (color_)index out of bound..
568 var relation_color = self.relation_colors[ color_index ];
569 var relation = draw_relation( source_node_id, target_node_id, relation_color, emphasis );
570 get_node_obj( source_node_id ).update_elements();
571 get_node_obj( target_node_id ).update_elements();
574 this.toggle_active = function( relation_id ) {
575 var relation = $( jq( relation_id ) );
576 var relation_path = relation.children('path');
577 if( !relation.data( 'active' ) ) {
578 relation_path.css( {'cursor':'pointer'} );
579 relation_path.mouseenter( function(event) {
580 outerTimer = setTimeout( function() {
581 timer = setTimeout( function() {
582 var related_nodes = get_related_nodes( relation_id );
583 var source_node_id = related_nodes[0];
584 var target_node_id = related_nodes[1];
585 $('#delete_source_node_id').val( source_node_id );
586 $('#delete_target_node_id').val( target_node_id );
587 self.showinfo(relation);
591 relation_path.mouseleave( function(event) {
592 clearTimeout(outerTimer);
593 if( timer != null ) { clearTimeout(timer); }
595 relation.data( 'active', true );
597 relation_path.unbind( 'mouseenter' );
598 relation_path.unbind( 'mouseleave' );
599 relation_path.css( {'cursor':'inherit'} );
600 relation.data( 'active', false );
603 this.showinfo = function(relation) {
604 $('#delete_relation_type').text( relation.data('type') );
605 $('#delete_relation_scope').text( relation.data('scope') );
606 $('#delete_relation_attributes').empty();
607 var significance = ' is not ';
608 if( relation.data( 'is_significant' ) === 'yes') {
609 significance = ' is ';
610 } else if ( relation.data( 'is_significant' ) === 'maybe' ) {
611 significance = ' might be ';
613 $('#delete_relation_attributes').append(
614 "This relationship" + significance + "stemmatically significant<br/>");
615 if( relation.data( 'a_derivable_from_b' ) ) {
616 $('#delete_relation_attributes').append(
617 "'" + relation.data('source_text') + "' derivable from '" + relation.data('target_text') + "'<br/>");
619 if( relation.data( 'b_derivable_from_a' ) ) {
620 $('#delete_relation_attributes').append(
621 "'" + relation.data('target_text') + "' derivable from '" + relation.data('source_text') + "'<br/>");
623 if( relation.data( 'non_independent' ) ) {
624 $('#delete_relation_attributes').append(
625 "Variance unlikely to arise coincidentally<br/>");
627 if( relation.data( 'note' ) ) {
628 $('#delete_relation_note').text('note: ' + relation.data( 'note' ) );
630 var points = relation.children('path').attr('d').slice(1).replace('C',' ').split(' ');
631 var xs = parseFloat( points[0].split(',')[0] );
632 var xe = parseFloat( points[1].split(',')[0] );
633 var ys = parseFloat( points[0].split(',')[1] );
634 var ye = parseFloat( points[3].split(',')[1] );
635 var p = svg_root.createSVGPoint();
636 p.x = xs + ((xe-xs)*1.1);
637 p.y = ye - ((ye-ys)/2);
638 var ctm = svg_root_element.getScreenCTM();
639 var nx = p.matrixTransform(ctm).x;
640 var ny = p.matrixTransform(ctm).y;
641 var dialog_aria = $ ("div[aria-labelledby='ui-dialog-title-delete-form']");
642 $('#delete-form').dialog( 'open' );
643 dialog_aria.offset({ left: nx, top: ny });
645 this.remove = function( relation_id ) {
649 var relation = $( jq( relation_id ) );
654 // Utility function to create/return the ID of a relation link between
655 // a source and target.
656 function get_relation_id( source_id, target_id ) {
657 var idlist = [ source_id, target_id ];
659 return 'relation-' + idlist[0] + '-...-' + idlist[1];
662 function get_related_nodes( relation_id ) {
663 var srctotarg = relation_id.substr( 9 );
664 return srctotarg.split('-...-');
667 function draw_relation( source_id, target_id, relation_color, emphasis ) {
668 var source_ellipse = get_ellipse( source_id );
669 var target_ellipse = get_ellipse( target_id );
670 var relation_id = get_relation_id( source_id, target_id );
671 var svg = $('#svgenlargement').children('svg').svg().svg('get');
672 var path = svg.createPath();
673 var sx = parseInt( source_ellipse.attr('cx') );
674 var rx = parseInt( source_ellipse.attr('rx') );
675 var sy = parseInt( source_ellipse.attr('cy') );
676 var ex = parseInt( target_ellipse.attr('cx') );
677 var ey = parseInt( target_ellipse.attr('cy') );
678 var relation = svg.group( $("#svgenlargement svg g"), { 'class':'relation', 'id':relation_id } );
679 svg.title( relation, source_id + '->' + target_id );
680 var stroke_width = emphasis === "yes" ? 6 : emphasis === "maybe" ? 4 : 2;
681 svg.path( relation, path.move( sx, sy ).curveC( sx + (2*rx), sy, ex + (2*rx), ey, ex, ey ), {fill: 'none', stroke: relation_color, strokeWidth: stroke_width });
682 var relation_element = $('#svgenlargement .relation').filter( ':last' );
683 relation_element.insertBefore( $('#svgenlargement g g').filter(':first') );
684 return relation_element;
687 function detach_node( readings ) {
688 // separate out the deleted relationships, discard for now
689 if( 'DELETED' in readings ) {
690 // Remove each of the deleted relationship links.
691 $.each( readings['DELETED'], function( idx, pair ) {
692 var relation_id = get_relation_id( pair[0], pair[1] );
693 var relation = $( jq( relation_id ) );
694 if( relation.size() == 0 ) {
695 relation_id = get_relation_id( pair[1], pair[0] );
697 relation_manager.remove( relation_id );
699 delete readings['DELETED'];
702 $.extend( readingdata, readings );
703 // remove from existing readings the witnesses for the new nodes/readings
704 $.each( readings, function( node_id, reading ) {
705 $.each( reading.witnesses, function( index, witness ) {
706 var witnesses = readingdata[ reading.orig_rdg ].witnesses;
707 readingdata[ reading.orig_rdg ].witnesses = $.removeFromArray( witness, witnesses );
713 // here we detach witnesses from the existing edges accoring to what's being relayed by readings
714 $.each( readings, function( node_id, reading ) {
715 var edges = edges_of( get_ellipse( reading.orig_rdg ) );
716 incoming_remaining = [];
717 outgoing_remaining = [];
718 $.each( reading.witnesses, function( index, witness ) {
719 incoming_remaining.push( witness );
720 outgoing_remaining.push( witness );
722 $.each( edges, function( index, edge ) {
723 detached_edge = edge.detach_witnesses( reading.witnesses );
724 if( detached_edge != null ) {
725 detached_edges.push( detached_edge );
726 $.each( detached_edge.witnesses, function( index, witness ) {
727 if( detached_edge.is_incoming == true ) {
728 incoming_remaining = $.removeFromArray( witness, incoming_remaining );
730 outgoing_remaining = $.removeFromArray( witness, outgoing_remaining );
736 // After detaching we still need to check if for *all* readings
737 // an edge was detached. It may be that a witness was not
738 // explicitly named on an edge but was part of a 'majority' edge
739 // in which case we need to duplicate and name that edge after those
740 // remaining witnesses.
741 if( outgoing_remaining.length > 0 ) {
742 $.each( edges, function( index, edge ) {
743 if( edge.get_label() == 'majority' && !edge.is_incoming ) {
744 detached_edges.push( edge.clone_for( outgoing_remaining ) );
748 if( incoming_remaining.length > 0 ) {
749 $.each( edges, function( index, edge ) {
750 if( edge.get_label() == 'majority' && edge.is_incoming ) {
751 detached_edges.push( edge.clone_for( incoming_remaining ) );
756 // Finally multiple selected nodes may share edges
758 $.each( detached_edges, function( index, edge ) {
760 $.each( copy_array, function( index, copy_edge ) {
761 if( copy_edge.g_elem.attr( 'id' ) == edge.g_elem.attr( 'id' ) ) { do_copy = false }
763 if( do_copy == true ) {
764 copy_array.push( edge );
767 detached_edges = copy_array;
769 // Lots of unabstracted knowledge down here :/
770 // Clone original node/reading, rename/id it..
771 duplicate_node = get_ellipse( reading.orig_rdg ).parent().clone();
772 duplicate_node.attr( 'id', node_id );
773 duplicate_node.children( 'title' ).text( node_id );
775 // This needs somehow to move to node or even to shapes! #repositioned
776 duplicate_node_data = get_ellipse( reading.orig_rdg ).parent().data( 'repositioned' );
777 if( duplicate_node_data != null ) {
778 duplicate_node.children( 'ellipse' ).parent().data( 'repositioned', duplicate_node_data );
781 // Add the node and all new edges into the graph
782 var graph_root = $('#svgenlargement svg g.graph');
783 graph_root.append( duplicate_node );
784 $.each( detached_edges, function( index, edge ) {
785 id_suffix = node_id.slice( node_id.indexOf( '_' ) );
786 edge.g_elem.attr( 'id', ( edge.g_elem.attr( 'id' ) + id_suffix ) );
787 edge_title = edge.g_elem.children( 'title' ).text();
788 edge_weight = 0.8 + ( 0.2 * edge.witnesses.length );
789 edge_title = edge_title.replace( reading.orig_rdg, node_id );
790 edge.g_elem.children( 'title' ).text( edge_title );
791 edge.g_elem.children( 'path').attr( 'stroke-width', edge_weight );
792 // Reg unabstracted knowledge: isn't it more elegant to make
793 // it edge.append_to( graph_root )?
794 graph_root.append( edge.g_elem );
797 // Make the detached node a real node_obj
798 var ellipse_elem = get_ellipse( node_id );
799 var new_node = new node_obj( ellipse_elem );
800 ellipse_elem.data( 'node_obj', new_node );
802 // Move the node somewhat up for 'dramatic effect' :-p
803 new_node.reposition( 0, -70 );
809 function merge_nodes( source_node_id, target_node_id, consequences ) {
810 if( consequences.status != null && consequences.status == 'ok' ) {
811 merge_node( source_node_id, target_node_id );
812 if( consequences.checkalign != null ) {
813 $.each( consequences.checkalign, function( index, node_ids ) {
814 var temp_relation = draw_relation( node_ids[0], node_ids[1], "#89a02c" );
815 var sy = parseInt( temp_relation.children('path').attr('d').split('C')[0].split(',')[1] );
816 var ey = parseInt( temp_relation.children('path').attr('d').split(' ')[2].split(',')[1] );
817 var yC = ey + (( sy - ey )/2);
818 // TODO: compute xC to be always the same distance to the amplitude of the curve
819 var xC = parseInt( temp_relation.children('path').attr('d').split(' ')[1].split(',')[0] );
820 var svg = $('#svgenlargement').children('svg').svg('get');
821 parent_g = svg.group( $('#svgenlargement svg g') );
822 var ids_text = node_ids[0] + '-' + node_ids[1];
823 var merge_id = 'merge-' + ids_text;
824 var yes = svg.image( parent_g, xC, (yC-8), 16, 16, merge_button_yes, { id: merge_id } );
825 var no = svg.image( parent_g, (xC+20), (yC-8), 16, 16, merge_button_no, { id: 'no' + merge_id } );
826 $(yes).hover( function(){ $(this).addClass( 'draggable' ) }, function(){ $(this).removeClass( 'draggable' ) } );
827 $(no).hover( function(){ $(this).addClass( 'draggable' ) }, function(){ $(this).removeClass( 'draggable' ) } );
828 $(yes).click( function( evt ){
829 merge_node( node_ids[0], node_ids[1] );
830 temp_relation.remove();
831 $(evt.target).parent().remove();
833 var ncpath = getTextURL( 'merge' );
834 var form_values = "source_id=" + node_ids[0] + "&target_id=" + node_ids[1] + "&single=true";
835 $.post( ncpath, form_values );
837 $(no).click( function( evt ) {
838 temp_relation.remove();
839 $(evt.target).parent().remove();
846 function merge_node( source_node_id, target_node_id ) {
847 $.each( edges_of( get_ellipse( source_node_id ) ), function( index, edge ) {
848 if( edge.is_incoming == true ) {
849 edge.attach_endpoint( target_node_id );
851 edge.attach_startpoint( target_node_id );
854 $( jq( source_node_id ) ).remove();
857 function compress_nodes(readings) {
858 //add text of other readings to 1st reading
860 var first = get_ellipse(readings[0]);
861 var first_title = first.parent().find('text')[0];
863 for (var i = 1; i < readings.length; i++) {
864 var cur = get_ellipse(readings[i]);
865 var cur_title = cur.parent().find('text')[0];
867 first_title.textContent += " " + cur_title.textContent;
871 for (var i = 1; i < readings.length; i++) {
872 var node = get_ellipse(readings[i]);
873 var rid = readings[i-1] + '->' + readings[i];
875 //[].slice.call(s.getElementsByTagName('title')).find(function(elem){return elem.textContent=='r64.2->r66.2'}).parentNode.remove()
877 var titles = svg_root.getElementsByTagName('title');
878 var titlesArray = [].slice.call(titles);
880 if (titlesArray.length > 0) {
881 var title = titlesArray.find(function(elem){
882 return elem.textContent === rid;
885 if (title && title.parentNode) {
886 title.parentNode.remove();
890 var x = parseInt(first[0].getAttribute('cx'), 10);
892 first[0].setAttribute('rx', 4.5 * first_title.textContent.length);
894 if (text_direction !== "BI") {
895 first[0].setAttribute('cx', x + first_title.textContent.length + 20);
896 first_title.setAttribute('x', first[0].getAttribute('cx'));
899 merge_node(readings[i], readings[0]);
900 //node.parent().remove();
912 this.enlargementOffset = $('#svgenlargement').offset();
913 this.svg_rect = $('#svgenlargement svg').svg('get');
915 this.show = function( event ) {
916 self.x = event.clientX;
917 self.y = event.clientY;
918 p = svg_root.createSVGPoint();
919 p.x = event.clientX - self.enlargementOffset.left;
920 p.y = event.clientY - self.enlargementOffset.top;
921 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' } );
924 this.expand = function( event ) {
925 self.dx = (event.clientX - self.x);
926 self.dy = (event.clientY - self.y);
927 var rect = $('#marquee');
928 if( rect.length != 0 ) {
929 var rect_w = Math.abs( self.dx );
930 var rect_h = Math.abs( self.dy );
931 var rect_x = self.x - self.enlargementOffset.left;
932 var rect_y = self.y - self.enlargementOffset.top;
933 if( self.dx < 0 ) { rect_x = rect_x - rect_w }
934 if( self.dy < 0 ) { rect_y = rect_y - rect_h }
935 rect.attr("x", rect_x).attr("y", rect_y).attr("width", rect_w).attr("height", rect_h);
939 this.select = function() {
940 var rect = $('#marquee');
941 if( rect.length != 0 ) {
942 //unselect any possible selected first
943 //TODO: unless SHIFT?
944 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
945 $('ellipse[fill="#9999ff"]').each( function() {
946 $(this).data( 'node_obj' ).set_draggable( false );
949 //compute dimension of marquee
950 var left = $('#marquee').offset().left;
951 var top = $('#marquee').offset().top;
952 var right = left + parseInt( $('#marquee').attr( 'width' ) );
953 var bottom = top + parseInt( $('#marquee').attr( 'height' ) );
954 var tf = svg_root_element.getScreenCTM().inverse();
955 var p = svg_root.createSVGPoint();
958 var cx_min = p.matrixTransform(tf).x;
959 var cy_min = p.matrixTransform(tf).y;
962 var cx_max = p.matrixTransform(tf).x;
963 var cy_max = p.matrixTransform(tf).y;
964 //select any node with its center inside the marquee
966 //also merge witness sets from nodes
968 $('#svgenlargement ellipse').each( function( index ) {
969 var cx = parseInt( $(this).attr('cx') );
970 var cy = parseInt( $(this).attr('cy') );
972 // This needs somehow to move to node or even to shapes! #repositioned
973 // We should ask something more aling the lines of: nodes.each { |item| node.selected? }
974 var org_translate = $(this).parent().data( 'repositioned' );
975 if( org_translate != null ) {
976 cx = cx + org_translate[0];
977 cy = cy + org_translate[1];
980 if( cx > cx_min && cx < cx_max) {
981 if( cy > cy_min && cy < cy_max) {
982 // we actually heve no real 'selected' state for nodes, except coloring
983 $(this).attr( 'fill', '#9999ff' );
984 // Take note of the selected reading(s) and applicable witness(es)
985 // so we can populate the multipleselect-form
986 readings.push( $(this).parent().attr('id') );
987 var this_witnesses = $(this).data( 'node_obj' ).get_witnesses();
988 witnesses = arrayUnique( witnesses.concat( this_witnesses ) );
992 if( $('ellipse[fill="#9999ff"]').size() > 0 ) {
993 //add intersection of witnesses sets to the multi select form and open it
994 $('#detach_collated_form').empty();
996 $.each( readings, function( index, value ) {
997 $('#detach_collated_form').append( $('<input>').attr(
998 "type", "hidden").attr("name", "readings[]").attr(
1001 $.each( witnesses, function( index, value ) {
1002 $('#detach_collated_form').append(
1003 '<input type="checkbox" name="witnesses[]" value="' + value
1004 + '">' + value + '<br>' );
1006 $('#multiple_selected_readings').attr('value', readings.join(',') );
1008 if ($('#action-merge')[0].checked) {
1009 $('#detach_collated_form').hide();
1010 $('#multipleselect-form-text').hide();
1012 $('#detach_btn').hide();
1013 $('#merge_btn').show();
1015 $('#detach_collated_form').show();
1016 $('#multipleselect-form-text').show();
1018 $('#detach_btn').show();
1019 $('#merge_btn').hide();
1022 $('#action-detach').change(function() {
1023 if ($('#action-detach')[0].checked) {
1024 $('#detach_collated_form').show();
1025 $('#multipleselect-form-text').show();
1027 $('#detach_btn').show();
1028 $('#merge_btn').hide();
1032 $('#action-merge').change(function() {
1033 if ($('#action-merge')[0].checked) {
1034 $('#detach_collated_form').hide();
1035 $('#multipleselect-form-text').hide();
1037 $('#detach_btn').hide();
1038 $('#merge_btn').show();
1042 $('#multipleselect-form').dialog( 'open' );
1044 self.svg_rect.remove( $('#marquee') );
1048 this.unselect = function() {
1049 $('ellipse[fill="#9999ff"]').attr( 'fill', '#fff' );
1054 function readings_equivalent( source, target ) {
1055 var sourcetext = readingdata[source].text;
1056 var targettext = readingdata[target].text;
1057 if( sourcetext === targettext ) {
1060 // Lowercase and strip punctuation from both and compare again
1061 var nonwc = XRegExp('[^\\p{L}\\s]|_');
1062 var stlc = XRegExp.replace( sourcetext.toLocaleLowerCase(), nonwc, "", 'all' );
1063 var ttlc = XRegExp.replace( targettext.toLocaleLowerCase(), nonwc, "", 'all' );
1064 if( stlc === ttlc ) {
1070 function scrollToEnd() {
1071 var stateTf = svg_root_element.getCTM().inverse();
1073 var vbdim = svg_root.viewBox.baseVal;
1074 var width = Math.floor(svg_root_element.getBoundingClientRect().width) - vbdim.width;
1076 var p = svg_root.createSVGPoint();
1079 p = p.matrixTransform(stateTf);
1081 var matrix = stateTf.inverse().translate(-p.x, -100);
1082 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
1083 svg_root_element.setAttribute("transform", s);
1086 $(document).ready(function () {
1089 relation_manager = new relation_factory();
1091 $('#update_workspace_button').data('locked', false);
1093 // Set up the mouse events on the SVG enlargement
1094 $('#enlargement').mousedown(function (event) {
1097 .data('x', event.clientX)
1098 .data('y', event.clientY)
1099 .data('scrollLeft', this.scrollLeft)
1100 stateTf = svg_root_element.getCTM().inverse();
1101 var p = svg_root.createSVGPoint();
1102 p.x = event.clientX;
1103 p.y = event.clientY;
1104 stateOrigin = p.matrixTransform(stateTf);
1106 // Activate marquee if in interaction mode
1107 if( $('#update_workspace_button').data('locked') == true ) { marquee.show( event ) };
1109 event.returnValue = false;
1110 event.preventDefault();
1112 }).mouseup(function (event) {
1114 $(this).data('down', false);
1115 }).mousemove(function (event) {
1116 if( timer != null ) { clearTimeout(timer); }
1117 if ( ($(this).data('down') == true) && ($('#update_workspace_button').data('locked') == false) ) {
1118 var p = svg_root.createSVGPoint();
1119 p.x = event.clientX;
1120 p.y = event.clientY;
1121 p = p.matrixTransform(stateTf);
1122 var matrix = stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y);
1123 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
1124 svg_root_element.setAttribute("transform", s);
1126 marquee.expand( event );
1127 event.returnValue = false;
1128 event.preventDefault();
1129 }).mousewheel(function (event, delta) {
1130 event.returnValue = false;
1131 event.preventDefault();
1132 if ( $('#update_workspace_button').data('locked') == false ) {
1133 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
1134 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
1135 if( delta < -9 ) { delta = -9 };
1136 var z = 1 + delta/10;
1137 z = delta > 0 ? 1 : -1;
1138 var g = svg_root_element;
1139 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 100))) {
1140 var root = svg_root;
1141 var p = root.createSVGPoint();
1142 p.x = event.originalEvent.clientX;
1143 p.y = event.originalEvent.clientY;
1144 p = p.matrixTransform(g.getCTM().inverse());
1145 var scaleLevel = 1+(z/20);
1146 var k = root.createSVGMatrix().translate(p.x, p.y).scale(scaleLevel).translate(-p.x, -p.y);
1147 var matrix = g.getCTM().multiply(k);
1148 var s = "matrix(" + matrix.a + "," + matrix.b + "," + matrix.c + "," + matrix.d + "," + matrix.e + "," + matrix.f + ")";
1149 g.setAttribute("transform", s);
1153 'overflow' : 'hidden',
1154 'cursor' : '-moz-grab'
1158 // Set up the relationship creation dialog. This also functions as the reading
1159 // merge dialog where appropriate.
1162 $( '#dialog-form' ).dialog( {
1168 'Merge readings': function( evt ) {
1169 var mybuttons = $(evt.target).closest('button').parent().find('button');
1170 mybuttons.button( 'disable' );
1171 $( '#status' ).empty();
1172 form_values = $( '#collapse_node_form' ).serialize();
1173 ncpath = getTextURL( 'merge' );
1174 var jqjson = $.post( ncpath, form_values, function( data ) {
1175 merge_nodes( $( '#source_node_id' ).val(), $( '#target_node_id' ).val(), data );
1176 mybuttons.button( 'enable' );
1177 $( '#dialog-form' ).dialog( 'close' );
1180 OK: function( evt ) {
1181 var mybuttons = $(evt.target).closest('button').parent().find('button');
1182 mybuttons.button( 'disable' );
1183 $( '#status' ).empty();
1184 form_values = $( '#collapse_node_form' ).serialize();
1185 ncpath = getTextURL( 'relationships' );
1186 var jqjson = $.post( ncpath, form_values, function( data ) {
1187 $.each( data, function( item, source_target ) {
1188 var source_found = get_ellipse( source_target[0] );
1189 var target_found = get_ellipse( source_target[1] );
1190 var relation_found = $.inArray( source_target[2], $( '#keymap' ).data( 'relations' ) );
1191 if( source_found.size() && target_found.size() && relation_found > -1 ) {
1192 var emphasis = $('#is_significant option:selected').attr('value');
1193 var relation = relation_manager.create( source_target[0], source_target[1], relation_found, emphasis );
1194 relation_manager.toggle_active( relation.attr('id') );
1195 $.each( $('#collapse_node_form').serializeArray(), function( i, k ) {
1196 relation.data( k.name, k.value );
1199 mybuttons.button( 'enable' );
1201 $( '#dialog-form' ).dialog( 'close' );
1204 Cancel: function() {
1205 $( this ).dialog( 'close' );
1208 create: function(event, ui) {
1209 $(this).data( 'relation_drawn', false );
1210 $('#rel_type').data( 'changed_after_open', false );
1211 $.each( relationship_types, function(index, typedef) {
1212 $('#rel_type').append( $('<option />').attr( "value", typedef.name ).text(typedef.name) );
1214 $.each( relationship_scopes, function(index, value) {
1215 $('#scope').append( $('<option />').attr( "value", value ).text(value) );
1217 $.each( ternary_values, function( index, value ) {
1218 $('#is_significant').append( $('<option />').attr( "value", value ).text(value) );
1220 // Handler to reset fields to default, the first time the relationship
1221 // is changed after opening the form.
1222 $('#rel_type').change( function () {
1223 if( !$(this).data( 'changed_after_open' ) ) {
1225 $(this).find(':checked').removeAttr('checked');
1227 $(this).data( 'changed_after_open', true );
1231 relation_manager.create_temporary(
1232 $('#source_node_id').val(), $('#target_node_id').val() );
1233 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' )
1234 if( readings_equivalent( $('#source_node_id').val(),
1235 $('#target_node_id').val() ) ) {
1236 buttonset.find( "button:contains('Merge readings')" ).show();
1238 buttonset.find( "button:contains('Merge readings')" ).hide();
1240 $(".ui-widget-overlay").css("background", "none");
1241 $("#dialog_overlay").show();
1242 $("#dialog_overlay").height( $("#enlargement_container").height() );
1243 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
1244 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
1245 $('#rel_type').data( 'changed_after_open', false );
1248 relation_manager.remove_temporary();
1249 $( '#status' ).empty();
1250 $("#dialog_overlay").hide();
1252 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
1253 if( ( ajaxSettings.url == getTextURL('relationships')
1254 || ajaxSettings.url == getTextURL('merge') )
1255 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
1257 if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
1258 error = 'You are not authorized to modify this tradition. (Try logging in again?)';
1261 var errobj = jQuery.parseJSON( jqXHR.responseText );
1262 error = errobj.error + '</br>The relationship cannot be made.</p>';
1264 error = jqXHR.responseText;
1267 $('#status').append( '<p class="error">Error: ' + error );
1269 $(event.target).parent().find('.ui-button').button("enable");
1273 // Set up the relationship info display and deletion dialog.
1274 $( "#delete-form" ).dialog({
1280 OK: function() { $( this ).dialog( "close" ); },
1281 "Delete all": function () { delete_relation( true ); },
1282 Delete: function() { delete_relation( false ); }
1284 create: function(event, ui) {
1285 // TODO What is this logic doing?
1286 // This scales the buttons in the dialog and makes it look proper
1287 // Not sure how essential it is, does anything break if it's not here?
1288 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
1289 buttonset.find( "button:contains('OK')" ).css( 'float', 'right' );
1290 // A: This makes sure that the pop up delete relation dialogue for a hovered over
1291 // relation auto closes if the user doesn't engage (mouseover) with it.
1292 var dialog_aria = $("div[aria-labelledby='ui-dialog-title-delete-form']");
1293 dialog_aria.mouseenter( function() {
1294 if( mouseWait != null ) { clearTimeout(mouseWait) };
1296 dialog_aria.mouseleave( function() {
1297 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
1301 // Show the appropriate buttons...
1302 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' )
1303 // If the user can't edit, show only the OK button
1305 buttonset.find( "button:contains('Delete')" ).hide();
1306 // If the relationship scope is local, show only OK and Delete
1307 } else if( $('#delete_relation_scope').text() === 'local' ) {
1308 $( this ).dialog( "option", "width", 160 );
1309 buttonset.find( "button:contains('Delete')" ).show();
1310 buttonset.find( "button:contains('Delete all')" ).hide();
1311 // Otherwise, show all three
1313 $( this ).dialog( "option", "width", 200 );
1314 buttonset.find( "button:contains('Delete')" ).show();
1316 mouseWait = setTimeout( function() { $("#delete-form").dialog( "close" ) }, 2000 );
1318 close: function() {}
1321 $( "#multipleselect-form" ).dialog({
1327 Cancel: function() {
1328 document.getElementById('duplicate-merge-error').innerHTML = "";
1329 $( this ).dialog( "close" );
1331 Detach: function ( evt ) {
1332 evt.target.id = 'detach_btn';
1335 var mybuttons = $(evt.target).closest('button').parent().find('button');
1336 mybuttons.button( 'disable' );
1337 var form_values = $('#detach_collated_form').serialize();
1338 ncpath = getTextURL( 'duplicate' );
1339 var jqjson = $.post( ncpath, form_values, function(data) {
1340 detach_node( data );
1341 mybuttons.button("enable");
1342 self.dialog( "close" );
1345 Merge: function (evt) {
1346 evt.target.id = 'merge_btn';
1349 var mybuttons = $(evt.target).closest('button').parent().find('button');
1350 mybuttons.button('disable');
1352 var ncpath = getTextURL('compress');
1353 var form_values = $('#detach_collated_form').serialize();
1355 var jqjson = $.post(ncpath, form_values, function(data) {
1357 document.getElementById('duplicate-merge-error').innerHTML = "";
1360 compress_nodes(data.nodes);
1363 mybuttons.button('enable');
1364 self.dialog('close');
1365 } else if (data.error_msg) {
1366 document.getElementById('duplicate-merge-error').innerHTML = data.error_msg;
1367 mybuttons.button('enable');
1373 create: function(event, ui) {
1374 var buttonset = $(this).parent().find( '.ui-dialog-buttonset' ).css( 'width', '100%' );
1375 buttonset.find( "button:contains('Cancel')" ).css( 'float', 'right' );
1378 $( this ).dialog( "option", "width", 200 );
1379 $(".ui-widget-overlay").css("background", "none");
1380 $('#multipleselect-form-status').empty();
1381 $("#dialog_overlay").show();
1382 $("#dialog_overlay").height( $("#enlargement_container").height() );
1383 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
1384 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
1386 var mybuttons = $(this).parent().find('button');
1388 mybuttons[1].id = 'detach_btn';
1389 mybuttons[2].id = 'merge_btn';
1393 $("#dialog_overlay").hide();
1395 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
1396 if( ajaxSettings.url == getTextURL('duplicate')
1397 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
1399 if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
1400 error = 'You are not authorized to modify this tradition. (Try logging in again?)';
1403 var errobj = jQuery.parseJSON( jqXHR.responseText );
1404 error = errobj.error + '</br>The relationship cannot be made.</p>';
1406 error = jqXHR.responseText;
1409 $('#multipleselect-form-status').append( '<p class="error">Error: ' + error );
1411 $(event.target).parent().find('.ui-button').button("enable");
1415 // Helpers for relationship deletion
1417 function delete_relation( scopewide ) {
1418 form_values = $('#delete_relation_form').serialize();
1420 form_values += "&scopewide=true";
1422 ncpath = getTextURL( 'relationships' );
1423 var jqjson = $.ajax({ url: ncpath, data: form_values, success: function(data) {
1424 $.each( data, function(item, source_target) {
1425 relation_manager.remove( get_relation_id( source_target[0], source_target[1] ) );
1427 $( "#delete-form" ).dialog( "close" );
1428 }, dataType: 'json', type: 'DELETE' });
1431 function toggle_relation_active( node_id ) {
1432 $('#svgenlargement .relation').find( "title:contains('" + node_id + "')" ).each( function(index) {
1433 matchid = new RegExp( "^" + node_id );
1434 if( $(this).text().match( matchid ) != null ) {
1435 var relation_id = $(this).parent().attr('id');
1436 relation_manager.toggle_active( relation_id );
1441 // function for reading form dialog should go here;
1442 // just hide the element for now if we don't have morphology
1443 if( can_morphologize ) {
1444 $('#reading-form').dialog({
1450 Cancel: function() {
1451 $( this ).dialog( "close" );
1453 Update: function( evt ) {
1454 // Disable the button
1455 var mybuttons = $(evt.target).closest('button').parent().find('button');
1456 mybuttons.button( 'disable' );
1457 $('#reading_status').empty();
1458 var reading_id = $('#reading_id').val()
1461 'is_nonsense': $('#reading_is_nonsense').is(':checked'),
1462 'grammar_invalid': $('#reading_grammar_invalid').is(':checked'),
1463 'normal_form': $('#reading_normal_form').val() };
1464 // Add the morphology values
1465 $('.reading_morphology').each( function() {
1466 if( $(this).val() != '(Click to select)' ) {
1467 var rmid = $(this).attr('id');
1468 rmid = rmid.substring(8);
1469 form_values[rmid] = $(this).val();
1472 // Make the JSON call
1473 ncpath = getReadingURL( reading_id );
1474 var reading_element = readingdata[reading_id];
1475 // $(':button :contains("Update")').attr("disabled", true);
1476 var jqjson = $.post( ncpath, form_values, function(data) {
1477 $.each( data, function(key, value) {
1478 reading_element[key] = value;
1480 if( $('#update_workspace_button').data('locked') == false ) {
1481 color_inactive( get_ellipse( reading_id ) );
1483 mybuttons.button("enable");
1484 $( "#reading-form" ).dialog( "close" );
1486 // Re-color the node if necessary
1490 create: function() {
1492 // Get rid of the disallowed editing UI bits
1493 $( this ).dialog( "option", "buttons",
1494 [{ text: "OK", click: function() { $( this ).dialog( "close" ); }}] );
1495 $('#reading_relemmatize').hide();
1499 $(".ui-widget-overlay").css("background", "none");
1500 $("#dialog_overlay").show();
1501 $('#reading_status').empty();
1502 $("#dialog_overlay").height( $("#enlargement_container").height() );
1503 $("#dialog_overlay").width( $("#enlargement_container").innerWidth() );
1504 $("#dialog_overlay").offset( $("#enlargement_container").offset() );
1505 $("#reading-form").parent().find('.ui-button').button("enable");
1508 $("#dialog_overlay").hide();
1510 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
1511 if( ajaxSettings.url.lastIndexOf( getReadingURL('') ) > -1
1512 && ajaxSettings.type == 'POST' && jqXHR.status == 403 ) {
1514 if( jqXHR.responseText.indexOf('do not have permission to modify') > -1 ) {
1515 error = 'You are not authorized to modify this tradition. (Try logging in again?)';
1518 var errobj = jQuery.parseJSON( jqXHR.responseText );
1519 error = errobj.error + '</br>The relationship cannot be made.</p>';
1521 error = jqXHR.responseText;
1524 $('#status').append( '<p class="error">Error: ' + error );
1526 $(event.target).parent().find('.ui-button').button("enable");
1529 $('#reading-form').hide();
1533 $('#update_workspace_button').click( function() {
1537 var svg_enlargement = $('#svgenlargement').svg().svg('get').root();
1538 mouse_scale = svg_root_element.getScreenCTM().a;
1539 if( $(this).data('locked') == true ) {
1540 $('#svgenlargement ellipse' ).each( function( index ) {
1541 if( $(this).data( 'node_obj' ) != null ) {
1542 $(this).data( 'node_obj' ).ungreyout_edges();
1543 $(this).data( 'node_obj' ).set_selectable( false );
1544 color_inactive( $(this) );
1545 var node_id = $(this).data( 'node_obj' ).get_id();
1546 toggle_relation_active( node_id );
1547 $(this).data( 'node_obj', null );
1550 $(this).data('locked', false);
1551 $(this).css('background-position', '0px 44px');
1553 var left = $('#enlargement').offset().left;
1554 var right = left + $('#enlargement').width();
1555 var tf = svg_root_element.getScreenCTM().inverse();
1556 var p = svg_root.createSVGPoint();
1559 var cx_min = p.matrixTransform(tf).x;
1561 var cx_max = p.matrixTransform(tf).x;
1562 $('#svgenlargement ellipse').each( function( index ) {
1563 var cx = parseInt( $(this).attr('cx') );
1564 if( cx > cx_min && cx < cx_max) {
1565 if( $(this).data( 'node_obj' ) == null ) {
1566 $(this).data( 'node_obj', new node_obj( $(this) ) );
1568 $(this).data( 'node_obj' ).set_selectable( true );
1570 $(this).data( 'node_obj' ).greyout_edges();
1571 var node_id = $(this).data( 'node_obj' ).get_id();
1572 toggle_relation_active( node_id );
1575 $(this).css('background-position', '0px 0px');
1576 $(this).data('locked', true );
1581 // Hide the unused elements
1582 $('#dialog-form').hide();
1583 $('#update_workspace_button').hide();
1587 $('.helptag').popupWindow({
1595 expandFillPageClients();
1596 $(window).resize(function() {
1597 expandFillPageClients();
1603 function expandFillPageClients() {
1604 $('.fillPage').each(function () {
1605 $(this).height($(window).height() - $(this).offset().top - MARGIN);
1609 function loadSVG(svgData) {
1610 var svgElement = $('#svgenlargement');
1612 $(svgElement).svg('destroy');
1616 onLoad : svgEnlargementLoaded
1624 function svg_select_callback(topic, data, subscriberData) {
1630 var prefs = new gadgets.Prefs();
1631 var preferredHeight = parseInt(prefs.getString('height'));
1632 if (gadgets.util.hasFeature('dynamic-height')) gadgets.window.adjustHeight(preferredHeight);
1633 expandFillPageClients();
1636 if (gadgets.util.hasFeature('pubsub-2')) {
1637 gadgets.HubSettings.onConnect = function(hum, suc, err) {
1638 subId = gadgets.Hub.subscribe("interedition.svg.selected", svg_select_callback);
1642 else gadgets.util.registerOnLoadHandler(loaded);