Made graph size fluid.
[scpubgit/stemmaweb.git] / root / js / stexaminer.js
1 var colors = ['#ffeeaa','#afc6e9','#d5fff6','#ffccaa','#ffaaaa','#e5ff80','#e5d5ff','#ffd5e5'];
2 var row_triggered = false;
3
4 function handle_row_click( row ) {
5         var ridx = row.parent().parent().index()
6         var rs = readingstats[ridx];
7     var imghtml = $('<img>').attr( 'src', baseurl + "../images/ajax-loader.gif" ).attr( 'alt', "Loading SVG..." );
8     $('#stemma_graph').empty();
9     $('#stemma_graph').append( imghtml );
10         if( rs.layerwits ) {
11                 var stemma_form = { 'dot': graphdot, 'layerwits': rs.layerwits };
12                 $.post( baseurl + 'graphsvg', stemma_form, function( data ) {
13                         var oSerializer = new XMLSerializer();
14                         var xmlString = oSerializer.serializeToString( data.documentElement );
15                         loadSVG( xmlString, function () { 
16                                 color_row( row );
17                                 show_stats( rs );
18                         });
19                 });
20         } else {
21                 loadSVG( original_svg, function() {
22                         color_row( row );
23                         show_stats( rs );
24                 });
25         }
26 }
27
28 // Load the SVG we are given
29 function loadSVG(svgData, cb) {
30         var svgElement = $('#stemma_graph');
31
32         $(svgElement).svg('destroy');
33
34         $(svgElement).svg({
35                 loadURL: svgData,
36                 onLoad : function () {
37                         var theSVG = svgElement.find('svg');
38                         var svgoffset = theSVG.offset();
39                         // Firefox needs a different offset, stupidly enough
40                         var browseroffset = 1;
41                         if( navigator.userAgent.indexOf('Firefox') > -1 ) {
42                                 browseroffset = 3;
43                         }
44                         var topoffset = theSVG.position().top - svgElement.position().top - browseroffset;
45                         // If we are on Safari, we need to get rid of the 'pt' in the width/height
46                         // specifications
47                         theSVG.offset({ top: svgoffset.top - topoffset, left: svgoffset.left });
48                         if( cb ) {
49                                 cb.call();
50                         }
51                         theSVG.width( "100%" );
52                 }
53         });
54 }
55
56 function color_row( row ) {
57     row_triggered = true;
58     $('ellipse').attr( {stroke:'white', fill:'#fff'} );
59     $('.node').children('polygon').attr( {stroke:'#fff', fill:'#fff'} );
60     $('.node').children('text').attr( {stroke:'none', fill:'#000'} );
61     $('tr.active_variant_row').children('td').removeClass('cellb0 cellb1 cellb2 cellb3 cellb4 cellb5 cellb6 cellb7'); 
62     row.parent().nextAll('.clickable').children('span').click();
63     $('td.active_variant_cell').removeClass('active_variant_cell');
64     row_triggered = false;
65 }
66
67 function color_nodes( column_index, arr_node_ids, arr_greynode_ids ) {
68   if( !row_triggered ) {
69     $('tr.active_variant_row').children('td').removeClass('cellb0 cellb1 cellb2 cellb3 cellb4 cellb5 cellb6 cellb7'); 
70     $('td.active_variant_cell').removeClass('active_variant_cell');
71     $('ellipse').attr( {stroke:'white', fill:'#fff'} );
72     $('.node').children('polygon').attr( {stroke:'#fff', fill:'#fff'} );
73     $('.node').children('text').attr( {stroke:'none', fill:'#000'} );
74   }; 
75   $('tr.active_variant_row').removeClass('active_variant_row') 
76   jQuery.each( arr_greynode_ids, function(index,value) {
77     nodes = $('.node').children('title').filter( function(index) {
78       return $(this).text() == value;
79     })
80     nodes.siblings('ellipse, polygon, text').each( function( index ) {
81         $(this).attr( {stroke:'#ddd', fill:'#f8f8f8'} );
82       });
83   });
84   jQuery.each( arr_node_ids, function(index,value) {
85     $('.node').children('title').filter( function(index) {
86       return $(this).text() == value;
87     }).siblings('ellipse').each( function( index ) {
88         $(this).attr( {stroke:'black', fill:colors[column_index-1]} );
89       });
90   });
91 }
92
93 function show_stats( rs ) {
94         // Update the title
95         $('#stats_title').empty().text('Statistics for reading at ' + rs.id + ':' );
96         var rshtml = $('#stats_template').clone();
97         if( "unsolved" in rs ) {
98                 var nocalcmsg;
99                 if( rs.unsolved == 'IDP error' ) {
100                         nocalcmsg = $('<span>').attr('class', 'error').append(
101                                 "(Could not reach calculation server - are you offline?)" );
102                 } else {
103                         nocalcmsg = "(Not yet calculated for this location - please try later)";
104                 }
105                 rshtml.find('.solutionstatus').append( nocalcmsg );
106         } else {
107                 $.each( rs.readings, function( idx, rdghash ) {
108                         var rdgstats = $('#reading_template').clone();
109                         rdgstats.find('.readinglabel').append( rdghash.text );
110                         rdgstats.find('.reading_copied').append( rdghash.followed );
111                         rdgstats.find('.reading_changed').append( rdghash.not_followed );
112                         rdgstats.find('.reading_unclear').append( rdghash.follow_unknown );
113                         rdgstats.find('.readingroots').append( rdghash.independent_occurrence );
114                         if( rdghash.is_reverted ) {
115                                 rdgstats.find('.reversionroots').append( rdghash.reversions );
116                         } else {
117                                 rdgstats.find('.readingreversions').empty();
118                         }
119                         var rdgsourcehtml = fill_parent_template( rdghash, 'source' );
120                         var rdgreverthtml = fill_parent_template( rdghash, 'reversion' );
121                         rdgstats.find('.reading_statistics').append( rdgsourcehtml );
122                         rdgstats.find('.reading_statistics').append( rdgreverthtml );
123                         // If neither, append a small spacer
124                         if( !rdgsourcehtml && !rdgreverthtml ) {
125                                 rdgstats.find('.reading_statistics').append( '<br/>' );
126                         }
127                         rshtml.append( rdgstats.contents() );
128                 });
129         }
130         $('#stats_container').empty().append( rshtml.contents() );
131         
132 };
133
134 function fill_parent_template( rdghash, type ) {
135         var objname = type + '_parents';
136         var template_id = '#reading_' + type + '_template';
137         var list_class = '.reading_' + type + '_list';
138         if( ! $.isEmptyObject( rdghash[objname] ) ) {
139                 var parentstats = $( template_id ).clone();
140                 $.each( rdghash[objname], function( parentid, pdata ) {
141                         var parentdesc = pdata.label;
142                         if( pdata.relation ) {
143                                 parentdesc += ' - variant type ' + pdata.relation.type;
144                                 if( pdata.relation.annotation ) {
145                                         parentdesc += ' [ ' + pdata.relation.annotation + ' ]';
146                                 }
147                         } else {
148                                 parentdesc += ' - no syntactic relation';
149                         }
150                         var parentitem = $('<li>').append( parentdesc );
151                         parentstats.find( list_class ).append( parentitem );
152                 });
153                 return( parentstats.contents() );
154         }
155 }
156
157 // Save the original unextended SVG for when we need it.
158 $(document).ready(function () {
159         loadSVG( original_svg );
160         
161         $('#aboutlink').popupWindow({ 
162                 height:500, 
163                 width:800, 
164                 top:50, 
165                 left:50,
166                 scrollbars:1 
167         }); 
168         $('#options').dialog({
169                 autoOpen: false,
170                 // height: 'auto',
171                 width: 300,
172                 modal: true,
173                 buttons: {
174                         Cancel: function() {
175                                 $(this).dialog( "close" );
176                         },
177                         Reanalyze: function() {
178                                 $('#use_variants_form').submit();
179                         },
180                 }
181         });
182
183 });