make stexaminer use JQuery SVG library for load
[scpubgit/stemmaweb.git] / root / js / stexaminer.js
CommitLineData
b8a92065 1var colors = ['#ffeeaa','#afc6e9','#d5fff6','#ffccaa','#ffaaaa','#e5ff80','#e5d5ff','#ffd5e5'];
2var row_triggered = false;
be8bf746 3
4function handle_row_click( row ) {
5 var ridx = row.parent().parent().index()
6 var rs = readingstats[ridx];
dd0c01e7 7 var imghtml = $('<img>').attr( 'src', baseurl + "../images/ajax-loader.gif" ).attr( 'alt', "Loading SVG..." );
be8bf746 8 $('#stemma_graph').empty();
9 $('#stemma_graph').append( imghtml );
10 if( rs.layerwits ) {
11 var stemma_form = { 'dot': graphdot, 'layerwits': rs.layerwits };
f6bfb763 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() {
be8bf746 22 color_row( row );
23 show_stats( rs );
24 });
be8bf746 25 }
26}
d5514865 27
f6bfb763 28// Load the SVG we are given
29function 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 }
52 });
53}
54
d5514865 55function color_row( row ) {
b8a92065 56 row_triggered = true;
f007ac1e 57 $('ellipse').attr( {stroke:'white', fill:'#fff'} );
b8a92065 58 $('.node').children('polygon').attr( {stroke:'#fff', fill:'#fff'} );
59 $('.node').children('text').attr( {stroke:'none', fill:'#000'} );
60 $('tr.active_variant_row').children('td').removeClass('cellb0 cellb1 cellb2 cellb3 cellb4 cellb5 cellb6 cellb7');
d5514865 61 row.parent().nextAll('.clickable').children('span').click();
b8a92065 62 $('td.active_variant_cell').removeClass('active_variant_cell');
63 row_triggered = false;
d5514865 64}
65
b8a92065 66function color_nodes( column_index, arr_node_ids, arr_greynode_ids ) {
67 if( !row_triggered ) {
68 $('tr.active_variant_row').children('td').removeClass('cellb0 cellb1 cellb2 cellb3 cellb4 cellb5 cellb6 cellb7');
69 $('td.active_variant_cell').removeClass('active_variant_cell');
f007ac1e 70 $('ellipse').attr( {stroke:'white', fill:'#fff'} );
b8a92065 71 $('.node').children('polygon').attr( {stroke:'#fff', fill:'#fff'} );
72 $('.node').children('text').attr( {stroke:'none', fill:'#000'} );
73 };
74 $('tr.active_variant_row').removeClass('active_variant_row')
75 jQuery.each( arr_greynode_ids, function(index,value) {
76 nodes = $('.node').children('title').filter( function(index) {
77 return $(this).text() == value;
78 })
79 nodes.siblings('ellipse, polygon, text').each( function( index ) {
80 $(this).attr( {stroke:'#ddd', fill:'#f8f8f8'} );
81 });
82 });
83 jQuery.each( arr_node_ids, function(index,value) {
84 $('.node').children('title').filter( function(index) {
85 return $(this).text() == value;
86 }).siblings('ellipse').each( function( index ) {
87 $(this).attr( {stroke:'black', fill:colors[column_index-1]} );
88 });
89 });
90}
ef3db56f 91
be8bf746 92function show_stats( rs ) {
ef3db56f 93 var rshtml = $('#stats_template').clone();
94 rshtml.find('#statrank').append( rs.id );
d35db27a 95 if( "unsolved" in rs ) {
ec0d9b5b 96 var nocalcmsg;
97 if( rs.unsolved == 'IDP error' ) {
98 nocalcmsg = $('<span>').attr('class', 'error').append(
99 "(Could not reach calculation server - are you offline?)" );
100 } else {
101 nocalcmsg = "(Not yet calculated for this location - please try later)";
102 }
103 rshtml.find('.solutionstatus').append( nocalcmsg );
d35db27a 104 } else {
105 $.each( rs.readings, function( idx, rdghash ) {
106 var rdgstats = $('#reading_template').clone();
107 rdgstats.find('.readinglabel').append( rdghash.text );
108 rdgstats.find('.reading_copied').append( rdghash.followed );
109 rdgstats.find('.reading_changed').append( rdghash.not_followed );
110 rdgstats.find('.reading_unclear').append( rdghash.follow_unknown );
111 rdgstats.find('.readingroots').append( rdghash.independent_occurrence );
112 if( rdghash.is_reverted ) {
113 rdgstats.find('.reversionroots').append( rdghash.reversions );
114 } else {
115 rdgstats.find('.readingreversions').empty();
116 }
117 var rdgsourcehtml = fill_parent_template( rdghash, 'source' );
118 var rdgreverthtml = fill_parent_template( rdghash, 'reversion' );
119 rdgstats.find('.reading_statistics').append( rdgsourcehtml );
120 rdgstats.find('.reading_statistics').append( rdgreverthtml );
121 // If neither, append a small spacer
122 if( !rdgsourcehtml && !rdgreverthtml ) {
123 rdgstats.find('.reading_statistics').append( '<br/>' );
124 }
125 rshtml.append( rdgstats.contents() );
126 });
127 }
ef3db56f 128 $('#row_statistics').empty();
129 $('#row_statistics').append( rshtml.contents() );
f7371955 130
ef3db56f 131};
be8bf746 132
f7371955 133function fill_parent_template( rdghash, type ) {
134 var objname = type + '_parents';
135 var template_id = '#reading_' + type + '_template';
136 var list_class = '.reading_' + type + '_list';
137 if( ! $.isEmptyObject( rdghash[objname] ) ) {
138 var parentstats = $( template_id ).clone();
139 $.each( rdghash[objname], function( parentid, pdata ) {
140 var parentdesc = pdata.label;
141 if( pdata.relation ) {
142 parentdesc += ' - variant type ' + pdata.relation.type;
143 if( pdata.relation.annotation ) {
144 parentdesc += ' [ ' + pdata.relation.annotation + ' ]';
145 }
146 } else {
147 parentdesc += ' - no syntactic relation';
148 }
149 var parentitem = $('<li>').append( parentdesc );
150 parentstats.find( list_class ).append( parentitem );
151 });
152 return( parentstats.contents() );
153 }
154}
155
be8bf746 156// Save the original unextended SVG for when we need it.
157$(document).ready(function () {
f6bfb763 158 loadSVG( original_svg );
2fd8f80c 159
160 $('#aboutlink').popupWindow({
161 height:500,
162 width:800,
163 top:50,
164 left:50,
165 scrollbars:1
166 });
167 $('#options').dialog({
168 autoOpen: false,
169 height: 200,
170 width: 300,
171 modal: true,
172 buttons: {
173 Cancel: function() {
174 $(this).dialog( "close" );
175 },
176 Reanalyze: function() {
177 $('#use_variants_form').submit();
178 },
179 }
180 });
181
be8bf746 182});