Implemented zoom and pan on sstemma.
[scpubgit/stemmaweb.git] / root / src / index.tt
1 [% WRAPPER header.tt
2         pagetitle = "Stemmaweb - Text tradition tools"
3         applicationjs = c.uri_for( 'js/componentload.js' )
4 %]
5     <script type="text/javascript">
6 var basepath = window.location.pathname
7 if( basepath.lastIndexOf('/') == basepath.length - 1 ) { 
8         basepath = basepath.slice( 0, basepath.length - 1) 
9 };
10 var selectedTextID;
11 var selectedTextInfo;
12 var selectedStemmaID = -1;
13 var stemmata = [];
14
15 function refreshDirectory () {
16         var lmesg = $('#loading_message').clone();
17         $('#directory').empty().append( lmesg.contents() );
18     $('#directory').load( "[% c.uri_for( 'directory' ) %]", 
19         function(response, status, xhr) {
20                         if (status == "error") {
21                                 var msg = "An error occurred: ";
22                                 $("#directory").html(msg + xhr.status + " " + xhr.statusText);
23                         }
24                 }
25         );
26 }
27
28 function start_upload_dialog() {
29     if( typeof uploader != 'undefined' ){ uploader.destroy() };
30     $('#upload-collation-dialog').dialog('option', 'attach_uploader')();
31     $('#upload_status').empty();
32     $('#upload_button').button('disable');
33     $('#upload-collation-dialog').dialog('open');
34 }
35
36 $(document).ready( function() {
37     // call out to load the directory div
38     $('#textinfo_container').hide();
39     $('#textinfo_waitbox').hide();
40         refreshDirectory();
41         
42         // Set up the textinfo edit dialog
43         $('#textinfo-edit-dialog').dialog({
44                 autoOpen: false,
45                 height: 200,
46                 width: 300,
47                 modal: true,
48                 buttons: {
49                         Save: function (evt) {
50                                 $("#edit_textinfo_status").empty();
51                                 $(evt.target).button("disable");
52                                 var requrl = "[% c.uri_for( '/textinfo' ) %]/" + selectedTextID;
53                                 var reqparam = $('#edit_textinfo').serialize();
54                                 $.post( requrl, reqparam, function (data) {
55                                         // Reload the selected text fields
56                                         selectedTextInfo = data;
57                                         load_textinfo();
58                                         // Reenable the button and close the form
59                                         $(evt.target).button("enable");
60                                         $('#textinfo-edit-dialog').dialog('close');
61                                 }, 'json' );
62                         },
63                         Cancel: function() {
64                                 $('#textinfo-edit-dialog').dialog('close');
65                         }
66                 },
67                 open: function() {
68                         $("#edit_textinfo_status").empty();
69                         // Populate the form fields with the current values
70                         // edit_(name, language, public, owner)
71                         $.each([ 'name', 'language', 'owner' ], function( idx, k ) {
72                                 var fname = '#edit_' + k;
73                                 // Special case: language Default is basically language null
74                                 if( k == 'language' && selectedTextInfo[k] == 'Default' ) {
75                                         $(fname).val( "" );
76                                 } else {
77                                         $(fname).val( selectedTextInfo[k] );
78                                 }
79                         });
80                         if( selectedTextInfo['public'] == true ) {
81                                 $('#edit_public').attr('checked','true');
82                         } else {
83                                 $('#edit_public').removeAttr('checked');
84                         }
85                 },
86         }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
87                 $(event.target).parent().find('.ui-button').button("enable");
88         if( ajaxSettings.url.indexOf( 'textinfo' ) > -1 
89                 && ajaxSettings.type == 'POST' ) {
90                         display_error( jqXHR, $("#edit_textinfo_status") );
91         }
92         });
93
94         
95         // Set up the stemma editor dialog
96         $('#stemma-edit-dialog').dialog({
97                 autoOpen: false,
98                 height: 700,
99                 width: 600,
100                 modal: true,
101                 buttons: {
102                         Save: function (evt) {
103                                 $("#edit_stemma_status").empty();
104                                 $(evt.target).button("disable");
105                                 var stemmaseq = $('#stemmaseq').val();
106                                 var requrl = "[% c.uri_for( '/stemma' ) %]/" + selectedTextID + "/" + stemmaseq;
107                                 var reqparam = { 'dot': $('#dot_field').val() };
108                                 // TODO We need to stash the literal SVG string in stemmata
109                                 // somehow. Implement accept header on server side to decide
110                                 // whether to send application/json or application/xml?
111                                 $.post( requrl, reqparam, function (data) {
112                                         // We received a stemma SVG string in return. 
113                                         // Update the current stemma sequence number
114                                         selectedStemmaID = data.stemmaid;
115                                         // Stash the answer in our SVG array
116                                         stemmata[selectedStemmaID] = data.stemmasvg;
117                                         // Display the new stemma
118                                         load_stemma( selectedStemmaID );
119                                         // Reenable the button and close the form
120                                         $(evt.target).button("enable");
121                                         $('#stemma-edit-dialog').dialog('close');
122                                 }, 'json' );
123                         },
124                         Cancel: function() {
125                                 $('#stemma-edit-dialog').dialog('close');
126                         }
127                 },
128                 open: function(evt) {
129                         $("#edit_stemma_status").empty();
130                         var stemmaseq = $('#stemmaseq').val();
131                         if( stemmaseq == 'n' ) {
132                                 // If we are creating a new stemma, populate the textarea with a
133                                 // bare digraph.
134                                 $(evt.target).dialog('option', 'title', 'Add a new stemma')
135                                 $('#dot_field').val( "digraph stemma {\n\n}" );
136                         } else {
137                                 // If we are editing a stemma, grab its stemmadot and populate the
138                                 // textarea with that.
139                                 $(evt.target).dialog('option', 'title', 'Edit selected stemma')
140                                 $('#dot_field').val( 'Loading, please wait...' );
141                                 var doturl = "[% c.uri_for( '/stemmadot' ) %]/" + selectedTextID + "/" + stemmaseq;
142                                 $.getJSON( doturl, function (data) {
143                                         // Re-insert the line breaks
144                                         var dotstring = data.dot.replace(/\|n/gm, "\n");                                        
145                                         $('#dot_field').val( dotstring );
146                                 });
147                         }
148                 },
149         }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
150                 $(event.target).parent().find('.ui-button').button("enable");
151         if( ajaxSettings.url.indexOf( 'stemma' ) > -1 
152                 && ajaxSettings.type == 'POST' ) {
153                         display_error( jqXHR, $("#edit_stemma_status") );
154         }
155         });
156                 
157         $('#upload-collation-dialog').dialog({
158                 autoOpen: false,
159                 height: 325,
160                 width: 480,
161                 modal: true,
162                 buttons: {
163                   pick: {
164                     text: "Pick File",
165                     id: "pick_uploadfile_button",
166                     click: function() {}       
167                   },
168                   upload: {
169                     text: 'Upload',
170                     id: 'upload_button',
171                     click: function() {
172                             $('#upload_status').empty();
173                 uploader.start();
174                 return false;
175             }
176                   },
177                   Cancel: function() {
178                     $('#upload-collation-dialog').dialog('close');
179                   }
180                 },
181                 attach_uploader: function() {
182                     create_uploader( "[% c.uri_for ( '/newtradition' ) %]" );
183                     $('#filelist').empty().html( 'Use the \'Pick\' button to choose a source file…' );
184                 }
185         });
186         
187         $('#stemma_graph').mousedown( function(evt) {
188         evt.stopPropagation();
189         $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] );
190         $('body').mousemove( function(evt) {
191             mouse_scale = 1; // for now, was:  mouse_scale = svg_root_element.getScreenCTM().a;
192             dx = (evt.clientX - $('#stemma_graph').data( 'mousedown_xy' )[0]) / mouse_scale;
193             dy = (evt.clientY - $('#stemma_graph').data( 'mousedown_xy' )[1]) / mouse_scale;
194             $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] );
195             var svg_root = $('#stemma_graph svg').svg().svg('get').root();
196             var g = $('g.graph', svg_root).get(0);
197             current_translate = g.getAttribute( 'transform' ).split(/translate\(/)[1].split(')',1)[0].split(' ');
198             new_transform = g.getAttribute( 'transform' ).replace( /translate\([^\)]*\)/, 'translate(' + (parseFloat(current_translate[0]) + dx) + ' ' + (parseFloat(current_translate[1]) + dy) + ')' );
199             g.setAttribute( 'transform', new_transform );
200             evt.returnValue = false;
201             evt.preventDefault();
202             return false;
203         });
204         $('body').mouseup( function(evt) {
205             $('body').unbind('mousemove');
206             $('body').unbind('mouseup');
207         });
208         });
209          
210         $('#stemma_graph').mousewheel(function (event, delta) {
211         event.returnValue = false;
212         event.preventDefault();
213         if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
214         if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
215         if( delta < -9 ) { delta = -9 }; 
216         var z = 1 + delta/10;
217         z = delta > 0 ? 1 : -1;
218         var svg_root = $('#stemma_graph svg').svg().svg('get').root();
219         var g = $('g.graph', svg_root).get(0);
220         if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 1000))) {
221             var scaleLevel = z/10;
222             current_scale = parseFloat( g.getAttribute( 'transform' ).split(/scale\(/)[1].split(')',1)[0].split(' ')[0] );
223             new_transform = g.getAttribute( 'transform' ).replace( /scale\([^\)]*\)/, 'scale(' + (current_scale + scaleLevel) + ')' );
224             g.setAttribute( 'transform', new_transform );
225         }
226     });
227     
228 });
229     </script>
230
231 [% END %]
232
233     <div id="topbanner">
234       <h1>Stemmaweb - a collection of tools for analysis of collated texts</h1>
235       <span class="mainnav">[% IF c.user_exists %]Hello! [% c.user.get_object.email %] <a class="navlink" href="[% c.uri_for( '/logout' ) %]">Sign out</a> | [% ELSE %]<a class="navlink" onclick="window.open('[% c.uri_for( '/login' ) %]', 'loginwindow', 'height=385,width=445')">Login</a> | <a class="navlink" onclick="window.open('[% c.uri_for( '/register' ) %]', 'regwindow', 'height=385,width=445')">Register</a> | [% END %]<a class="navlink" href="[% c.uri_for( 'about.html' ) %]">About</a> </span>
236     </div>
237     <div id="directory_container">
238       <h2>Text directory</h2>
239       <div id="directory"></div>
240 [% IF c.user_exists -%]
241           <div class="button" id="new_trad_button" onClick="start_upload_dialog();">
242             <span>Add a new text tradition</span>
243           </div>
244 [% END %]
245     </div>
246     <div id="textinfo_waitbox">
247         <h3>Loading tradition information, please wait...</h3>
248         <img src="[% c.uri_for( 'images', 'ajax-loader.gif' ) %]" alt="Loading tradition info..." />
249     </div>
250     <div id="textinfo_container">
251       <div id="textinfo_load_status"></div>
252       <h2>Text <span class="texttitle"></span></h2>
253       <ul>
254           <li>is owned by <span id="owner_id"></span></li>
255           <li>is <span id="not_public"></span>public</li>
256           <li>has <span id="marked_language"></span> as its primary language</li>
257               <li>has <span id="witness_num"></span> witnesses: <span id="witness_list"></span></li>
258       </ul>
259       
260       <!-- TODO buttons on either side of the graph div to flip through the stemmata -->
261       <div id="textinfo_container_buttons">
262           <form id="open_textinfo_edit" action="" method="GET" name="edit_textinfo">
263             <div class="button" id="edit_textinfo_button"
264                 onClick="$('#textinfo-edit-dialog').dialog('open')">
265                   <span>Modify information about this tradition</span>
266             </div>
267           </form>
268           <form id="stemma_pager" action="" method="GET" name="stemma_pager">
269             <div class="button" id="stemma_pager_button" onClick="$('#stemma_pager').submit()">
270                   <span>Left &amp; right go here</span>
271             </div>
272           </form>
273           <form id="open_stemma_add" action="" method="GET" name="add_new_stemma">
274             <div class="button" id="stemma_add_button" 
275                 onClick="$('#stemmaseq').val('n'); $('#stemma-edit-dialog').dialog('open');">
276                   <span>Add a new stemma</span>
277             </div>
278           </form>
279           <form id="open_stemma_edit" action="" method="GET" name="edit_current_stemma">
280             <div class="button" id="stemma_edit_button" 
281                 onClick="$('#stemmaseq').val(selectedStemmaID); $('#stemma-edit-dialog').dialog('open');">
282                   <span>Edit this stemma</span>
283             </div>
284           </form>
285           <form id="run_stexaminer" action="" method="GET" name="run_stexaminer">
286             <div class="button" id="stexaminer_button" onClick="$('#run_stexaminer').submit()">
287                   <span>Examine variants against this stemma</span>
288             </div>
289           </form>
290           <form id="run_relater" action="" method="GET" name="run_relater">
291             <div class="button" id="relater_button" onClick="$('#run_relater').submit()">
292               <span>Run relationship mapper</span>
293             </div>
294           </form>
295       </div>
296       <div id="stemma_graph"></div>
297     </div>
298
299     <!-- Interim 'loading' message for directory box -->
300     <div id="loading_message">
301         <h3>Loading texts, please wait...</h3>
302         <img src="[% c.uri_for( 'images', 'ajax-loader.gif' ) %]" alt="Loading tradition list..."/>
303     </div>
304     
305     <!-- Textinfo editor dialog -->
306     <div id="textinfo-edit-dialog" title="Edit information about this tradition">
307       <div id="textinfo_edit_container">
308         <form id="edit_textinfo">
309                 <label for="edit_name">Tradition name: </label>
310                 <input id="edit_name" type="text" size="30" name="name"/><br/>
311                 <label for="edit_language">Language: </label>
312                 <input id="edit_language" type="text" size="12" name="language"/>
313                 <label for="edit_public">Publicly viewable: </label>
314                 <input id="edit_public" type="checkbox" name="public"/><br/>
315 [% IF c.user_exists -%]
316 [% IF c.user.get_object.is_admin -%]
317                 <label for="edit_owner">Tradition owner: </label>
318                 <input id="edit_owner" type="text" size="30" name="owner"/><br/>
319 [% END -%]
320 [% END -%]
321                 </form>
322                 <div id="edit_textinfo_status"></div>
323           </div>
324     </div>
325     
326     <!-- Stemma dot editor dialog, simple textarea for now -->
327     <div id="stemma-edit-dialog">
328       <div id="stemma_edit_container">
329         <form id="edit_stemma">
330                 <label for="dot_field">Dot definition for this stemma: </label><br/>
331                 <textarea id="dot_field" rows="30" cols="40"></textarea>
332                 <input id="stemmaseq" type="hidden" name="stemmaseq" val="n"/>
333                         <div id="edit_instructions">
334                                 <p>All definitions begin with the line
335                                         <pre>digraph stemma {</pre>
336                                 and end with the line 
337                                         <pre>}</pre>Please do not change these lines.</p>
338                                 <p>First list each witness in your stemma, whether extant or lost /
339                                 reconstructed / hypothetical, and assign them a class of either "extant"
340                                 or "hypothetical". For example:</p><pre>  
341         α [ class=hypothetical ]
342         C [ class=extant ]
343                                 </pre>
344                                 <p>Next, list the direct links between witnesses, one per line. For example, if 
345                                 witness C descends directly from witness α, note it as follows:</p><pre>
346         α -> C
347                                 </pre>
348                                 <p>A witness may be the exemplar for any number of other witnesses, whether 
349                                 extant or not; likewise, a witness may inherit from any number of other 
350                                 witnesses. Use as may "A -> B" pairings as necessary to describe the links.</p>
351                         </div>
352         </form>
353         <div id="edit_stemma_status"></div>
354       </div>
355     </div>
356
357     <!-- File upload dialog box -->
358     <div id="upload-collation-dialog" title="Upload a collation">
359       <div id="upload_container">
360         <form id="new_tradition">
361             <label for="new_name">Name of this text / tradition: </label>
362             <input id="new_name" type="text" name="name" size="40"/><br/>
363             <label for="new_lang">Primary language of the text: </label>
364             <input id="new_lang" type="text" name="language" size="20"/><br/>
365             <label for="new_public">Allow public display: </label>
366             <input id="new_public" name="public" type="checkbox"/><br/>
367             <div id="filelist"></div>
368         <form>
369         <div id="upload_status"></div>
370         <div>
371           <h4>Supported file types / extensions:</h4>
372           <ul>
373             <li>*.txt - spreadsheet collation, tab-separated values</li>
374             <li>*.csv - spreadsheet collation, comma-separated values</li>
375             <li>*.xls - spreadsheet collation, Excel 97-2004 format</li>
376             <li>*.xlsx - spreadsheet collation, Excel 2007 XML format</li>
377             <li>*.xml - TEI XML parallel segmentation format</li>
378             <li>*.xml - TEI XML export from Classical Text Editor</li>
379             <li>*.xml - GraphML export from the CollateX tool</li>
380           </ul>
381           <p>All spreadsheet collations should be arranged with the witness sigla in the first row, and the words aligned by row each in its correct witness column.</p>
382         </div>
383       </div>
384     </div>    
385 [% PROCESS footer.tt %]