Cosmetic fixes to state after stemweb request and query. #29
[scpubgit/stemmaweb.git] / root / js / componentload.js
CommitLineData
3f9d7ae5 1// Global state variables
2var selectedTextID;
3var selectedTextInfo;
4var selectedStemmaID = -1;
5var stemmata = [];
6
7// Load the names of the appropriate traditions into the directory div.
8function refreshDirectory () {
9 var lmesg = $('#loading_message').clone();
10 $('#directory').empty().append( lmesg.contents() );
11 $('#directory').load( _get_url(["directory"]),
12 function(response, status, xhr) {
13 if (status == "error") {
14 var msg = "An error occurred: ";
15 $("#directory").html(msg + xhr.status + " " + xhr.statusText);
16 } else {
17 if( textOnLoad != "" ) {
18 // Call the click callback for the relevant text, if it is
19 // in the page.
20 $('#'+textOnLoad).click();
21 textOnLoad = "";
22 }
23 }
24 }
25 );
26}
27
28// Load a tradition with its information and stemmata into the tradition
29// view pane. Calls load_textinfo.
98a45925 30function loadTradition( textid, textname, editable ) {
31 selectedTextID = textid;
32 // First insert the placeholder image and register an error handler
04469f3e 33 $('#textinfo_load_status').empty();
5f0eda3f 34 $('#stemma_graph').empty();
98a45925 35 $('#textinfo_waitbox').show();
75354c3a 36 $('#textinfo_container').hide().ajaxError(
37 function(event, jqXHR, ajaxSettings, thrownError) {
38 if( ajaxSettings.url.indexOf( 'textinfo' ) > -1 && ajaxSettings.type == 'GET' ) {
39 $('#textinfo_waitbox').hide();
40 $('#textinfo_container').show();
41 display_error( jqXHR, $("#textinfo_load_status") );
98a45925 42 }
75354c3a 43 });
44
45 // Hide the functionality that is irrelevant
46 if( editable ) {
ce1c5863 47 $('#open_stemma_add').show();
ce1c5863 48 $('#open_textinfo_edit').show();
cbd23059 49 $('#relatebutton_label').text('View collation and edit relationships');
75354c3a 50 } else {
ce1c5863 51 $('#open_stemma_add').hide();
db234220 52 $('#open_stemweb_ui').hide();
c2b80bba 53 $('#query_stemweb_ui').hide();
ce1c5863 54 $('#open_textinfo_edit').hide();
cbd23059 55 $('#relatebutton_label').text('View collation and relationships');
75354c3a 56 }
57
62723740 58 // Then get and load the actual content.
98a45925 59 // TODO: scale #stemma_graph both horizontally and vertically
f6a8db89 60 // TODO: load svgs from SVG.Jquery (to make scaling react in Safari)
3f9d7ae5 61 $.getJSON( _get_url([ "textinfo", textid ]), function (textdata) {
98a45925 62 // Add the scalar data
75354c3a 63 selectedTextInfo = textdata;
64 load_textinfo();
63378fe0 65 // Add the stemma(ta)
98a45925 66 stemmata = textdata.stemmata;
67 if( stemmata.length ) {
68 selectedStemmaID = 0;
65a0c9c6 69 } else {
57e3a008 70 selectedStemmaID = -1;
65a0c9c6 71 }
63378fe0 72 load_stemma( selectedStemmaID, editable );
98a45925 73 // Set up the relationship mapper button
3f9d7ae5 74 $('#run_relater').attr( 'action', _get_url([ "relation", textid ]) );
38627d20 75 // Set up the download button
76 $('#dl_tradition').attr( 'href', _get_url([ "download", textid ]) );
77 $('#dl_tradition').attr( 'download', selectedTextInfo.name + '.xml' );
98a45925 78 });
79}
80
3f9d7ae5 81// Load the metadata about a tradition into the appropriate div.
75354c3a 82function load_textinfo() {
83 $('#textinfo_waitbox').hide();
84 $('#textinfo_load_status').empty();
85 $('#textinfo_container').show();
86 $('.texttitle').empty().append( selectedTextInfo.name );
87 // Witnesses
88 $('#witness_num').empty().append( selectedTextInfo.witnesses.size );
89 $('#witness_list').empty().append( selectedTextInfo.witnesses.join( ', ' ) );
90 // Who the owner is
91 $('#owner_id').empty().append('no one');
92 if( selectedTextInfo.owner ) {
897a22fc 93 var owneremail = selectedTextInfo.owner;
94 var chop = owneremail.indexOf( '@' );
95 if( chop > -1 ) {
96 owneremail = owneremail.substr( 0, chop + 1 ) + '...';
97 }
98 $('#owner_id').empty().append( owneremail );
75354c3a 99 }
100 // Whether or not it is public
101 $('#not_public').empty();
102 if( selectedTextInfo['public'] == false ) {
103 $('#not_public').append('NOT ');
104 }
105 // What language setting it has, if any
106 $('#marked_language').empty().append('no language set');
107 if( selectedTextInfo.language && selectedTextInfo.language != 'Default' ) {
108 $('#marked_language').empty().append( selectedTextInfo.language );
109 }
110}
111
3f9d7ae5 112// Enable / disable the appropriate buttons for paging through the stemma.
63378fe0 113function show_stemmapager ( editable ) {
bf81fb57 114 $('.pager_left_button').unbind('click').addClass( 'greyed_out' );
115 $('.pager_right_button').unbind('click').addClass( 'greyed_out' );
65a0c9c6 116 if( selectedStemmaID > 0 ) {
117 $('.pager_left_button').click( function () {
63378fe0 118 load_stemma( selectedStemmaID - 1, editable );
bf81fb57 119 }).removeClass( 'greyed_out' );
65a0c9c6 120 }
121 if( selectedStemmaID + 1 < stemmata.length ) {
122 $('.pager_right_button').click( function () {
63378fe0 123 load_stemma( selectedStemmaID + 1, editable );
bf81fb57 124 }).removeClass( 'greyed_out' );
65a0c9c6 125 }
126}
127
3f9d7ae5 128// Load a given stemma SVG into the stemmagraph box.
63378fe0 129function load_stemma( idx, editable ) {
65a0c9c6 130 // Load the stemma at idx
131 selectedStemmaID = idx;
63378fe0 132 show_stemmapager( editable );
133 $('#open_stemma_edit').hide();
134 $('#run_stexaminer').hide();
135 $('#stemma_identifier').empty();
c2b80bba 136 // Add the relevant Stemweb functionality
137 if( editable ) {
138 if( selectedTextInfo.stemweb_jobid == 0 ) {
139 $('#open_stemweb_ui').show();
2c514a6f 140 $('#query_stemweb_ui').hide();
c2b80bba 141 } else {
142 $('#query_stemweb_ui').show();
2c514a6f 143 $('#open_stemweb_ui').hide();
c2b80bba 144 }
145 }
98a45925 146 if( idx > -1 ) {
63378fe0 147 // Load the stemma and its properties
ec2f89ff 148 var stemmadata = stemmata[idx];
63378fe0 149 if( editable ) {
150 $('#open_stemma_edit').show();
151 }
152 if( stemmadata.directed ) {
153 // Stexaminer submit action
154 var stexpath = _get_url([ "stexaminer", selectedTextID, idx ]);
155 $('#run_stexaminer').attr( 'action', stexpath );
156 $('#run_stexaminer').show();
157 }
158 loadSVG( stemmadata.svg );
159 $('#stemma_identifier').text( stemmadata.name );
40803b80 160 setTimeout( 'start_element_height = $("#stemma_graph .node")[0].getBBox().height;', 500 );
98a45925 161 }
5ba6c2b4 162}
75354c3a 163
c2b80bba 164function query_stemweb_progress() {
165 var requrl = _get_url([ "stemweb", "query", selectedTextInfo.stemweb_jobid ]);
166 $.getJSON( requrl, function (data) {
167 // Look for a status message, either success, running, or notfound.
168 if( data.status === 'success' ) {
169 // Add the new stemmata to the textinfo and tell the user.
2c514a6f 170 selectedTextInfo.stemweb_jobid = 0;
c2b80bba 171 if( data.stemmata.length > 0 ) {
172 stemmata = stemmata.concat( data.stemmata );
173 if( selectedStemmaID == -1 ) {
174 // We have a stemma for the first time; load the first one.
2c514a6f 175 load_stemma( 0, true );
e883f11b 176 } else {
177 // Move to the index of the first added stemma.
178 var newIdx = stemmata.length - data.stemmata.length;
179 load_stemma( newIdx, true );
c2b80bba 180 }
181 alert( 'You have one or more new stemmata!' );
182 } else {
183 alert( 'Stemweb run finished with no stemmata...huh?!' );
184 }
185 } else if( data.status === 'running' ) {
186 // Just tell the user.
187 alert( 'Your Stemweb query is still running!' );
188 } else if( data.status === 'notfound' ) {
189 // Ask the user to refresh, for now.
190 alert( 'Your Stemweb query probably finished and reported back. Please reload to check.' );
191 }
192 });
193}
194
bd3ccd15 195// Load the SVG we are given
196function loadSVG(svgData) {
197 var svgElement = $('#stemma_graph');
198
199 $(svgElement).svg('destroy');
200
201 $(svgElement).svg({
202 loadURL: svgData,
203 onLoad : function () {
204 var theSVG = svgElement.find('svg');
205 var svgoffset = theSVG.offset();
bd3ccd15 206 var browseroffset = 1;
23f8bfc2 207 // Firefox needs a different offset, stupidly enough
bd3ccd15 208 if( navigator.userAgent.indexOf('Firefox') > -1 ) {
23f8bfc2 209 browseroffset = 3; // works for tall images
210 // ...but if the SVG is wider than it is tall, Firefox treats
211 // the top as being the top of the graph, loaded into the middle
212 // of the canvas, but then the margin at the top of the canvas
213 // extends upward. So we have to find the actual top of the canvas
214 // and correct for *that* instead.
215 var vbdim = svgElement.svg().svg('get').root().viewBox.baseVal;
216 if( vbdim.height < vbdim.width ) {
217 var vbscale = svgElement.width() / vbdim.width;
218 var vbrealheight = vbdim.height * vbscale;
219 browseroffset = 3 + ( svgElement.height() - vbrealheight ) / 2;
220 }
bd3ccd15 221 }
222 var topoffset = theSVG.position().top - svgElement.position().top - browseroffset;
bd3ccd15 223 theSVG.offset({ top: svgoffset.top - topoffset, left: svgoffset.left });
224 }
225 });
226}
227
3f9d7ae5 228// General-purpose error-handling function.
229// TODO make sure this gets used throughout, where appropriate.
75354c3a 230function display_error( jqXHR, el ) {
ce1c5863 231 var errmsg;
232 if( jqXHR.responseText == "" ) {
233 errmsg = "perhaps the server went down?"
75354c3a 234 } else {
ce1c5863 235 var errobj;
236 try {
237 errobj = jQuery.parseJSON( jqXHR.responseText );
238 errmsg = errobj.error;
239 } catch ( parse_err ) {
240 errmsg = "something went wrong on the server."
241 }
75354c3a 242 }
ce1c5863 243 var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg );
75354c3a 244 $(el).empty().append( msghtml ).show();
3f9d7ae5 245}
246
e0b90236 247// Event to enable the upload button when a file has been selected
248function file_selected( e ) {
249 if( e.files.length == 1 ) {
250 $('#upload_button').button('enable');
ab0d1218 251 $('#new_file_name_container').html( '<span id="new_file_name">' + e.files[0].name + '</span>' );
e0b90236 252 } else {
253 $('#upload_button').button('disable');
ab0d1218 254 $('#new_file_name_container').html( '(Use \'pick file\' to select a tradition file to upload.)' );
e0b90236 255 }
256}
257
2ece58b3 258// Implement our own AJAX method that uses the features of XMLHttpRequest2
259// but try to let it have a similar interface to jquery.post
260// The data var needs to be a FormData() object.
261// The callback will be given a single argument, which is the response data
262// of the given type.
263
264function post_xhr2( url, data, cb, type ) {
265 if( !type ) {
266 type = 'json';
267 }
268 var xhr = new XMLHttpRequest();
269 // Set the expected response type
270 if( type === 'data' ) {
271 xhr.responseType = 'blob';
272 } else if( type === 'xml' ) {
273 xhr.responseType = 'document';
274 }
275 // Post the form
276 // Gin up an AJAX settings object
277 $.ajaxSetup({ url: url, type: 'POST' });
278 xhr.open( 'POST', url, true );
279 // Handle the results
280 xhr.onload = function( e ) {
281 // Get the response and parse it
282 // Call the callback with the response, whatever it was
283 var xhrs = e.target;
284 if( xhrs.status > 199 && xhrs.status < 300 ) { // Success
285 var resp;
286 if( type === 'json' ) {
287 resp = $.parseJSON( xhrs.responseText );
288 } else if ( type === 'xml' ) {
289 resp = xhrs.responseXML;
290 } else if ( type === 'text' ) {
291 resp = xhrs.responseText;
292 } else {
293 resp = xhrs.response;
294 }
295 cb( resp );
296 } else {
297 // Trigger the ajaxError...
298 _trigger_ajaxerror( e );
299 }
300 };
301 xhr.onerror = _trigger_ajaxerror;
302 xhr.onabort = _trigger_ajaxerror;
303 xhr.send( data );
304}
305
306function _trigger_ajaxerror( e ) {
307 var xhr = e.target;
308 var thrown = xhr.statusText || 'Request error';
309 jQuery.event.trigger( 'ajaxError', [ xhr, $.ajaxSettings, thrown ]);
310}
311
e0b90236 312function upload_new () {
313 // Serialize the upload form, get the file and attach it to the request,
314 // POST the lot and handle the response.
315 var newfile = $('#new_file').get(0).files[0];
316 var reader = new FileReader();
317 reader.onload = function( evt ) {
2ece58b3 318 var data = new FormData();
319 $.each( $('#new_tradition').serializeArray(), function( i, o ) {
320 data.append( o.name, o.value );
321 });
322 data.append( 'file', newfile );
e0b90236 323 var upload_url = _get_url([ 'newtradition' ]);
2ece58b3 324 post_xhr2( upload_url, data, function( ret ) {
e0b90236 325 if( ret.id ) {
326 $('#upload-collation-dialog').dialog('close');
327 refreshDirectory();
328 loadTradition( ret.id, ret.name, 1 );
329 } else if( ret.error ) {
330 $('#upload_status').empty().append(
331 $('<span>').attr('class', 'error').append( ret.error ) );
332 }
2ece58b3 333 }, 'json' );
e0b90236 334 };
335 reader.onerror = function( evt ) {
336 var err_resp = 'File read error';
337 if( e.name == 'NotFoundError' ) {
338 err_resp = 'File not found';
339 } else if ( e.name == 'NotReadableError' ) {
340 err_resp == 'File unreadable - is it yours?';
341 } else if ( e.name == 'EncodingError' ) {
342 err_resp == 'File cannot be encoded - is it too long?';
343 } else if ( e.name == 'SecurityError' ) {
344 err_resp == 'File read security error';
345 }
346 // Fake a jqXHR object that we can pass to our generic error handler.
347 var jqxhr = { responseText: '{error:"' + err_resp + '"}' };
348 display_error( jqxhr, $('#upload_status') );
349 $('#upload_button').button('disable');
350 }
351
352 reader.readAsBinaryString( newfile );
3f9d7ae5 353}
354
355// Utility function to neatly construct an application URL
356function _get_url( els ) {
357 return basepath + els.join('/');
358}
359
bd3ccd15 360
3f9d7ae5 361$(document).ready( function() {
50778a5d 362 // See if we have the browser functionality we need
363 // TODO Also think of a test for SVG readiness
7c25980f 364 if( !!window.FileReader && !!window.File ) {
50778a5d 365 $('#compatibility_check').empty();
366 }
367
3f9d7ae5 368 // call out to load the directory div
369 $('#textinfo_container').hide();
370 $('#textinfo_waitbox').hide();
371 refreshDirectory();
372
373 // Set up the textinfo edit dialog
374 $('#textinfo-edit-dialog').dialog({
375 autoOpen: false,
376 height: 200,
377 width: 300,
378 modal: true,
379 buttons: {
380 Save: function (evt) {
381 $("#edit_textinfo_status").empty();
382 $(evt.target).button("disable");
383 var requrl = _get_url([ "textinfo", selectedTextID ]);
384 var reqparam = $('#edit_textinfo').serialize();
385 $.post( requrl, reqparam, function (data) {
386 // Reload the selected text fields
387 selectedTextInfo = data;
388 load_textinfo();
389 // Reenable the button and close the form
390 $(evt.target).button("enable");
391 $('#textinfo-edit-dialog').dialog('close');
392 }, 'json' );
393 },
394 Cancel: function() {
395 $('#textinfo-edit-dialog').dialog('close');
396 }
397 },
398 open: function() {
399 $("#edit_textinfo_status").empty();
400 // Populate the form fields with the current values
401 // edit_(name, language, public, owner)
402 $.each([ 'name', 'language', 'owner' ], function( idx, k ) {
403 var fname = '#edit_' + k;
404 // Special case: language Default is basically language null
405 if( k == 'language' && selectedTextInfo[k] == 'Default' ) {
406 $(fname).val( "" );
407 } else {
408 $(fname).val( selectedTextInfo[k] );
409 }
410 });
411 if( selectedTextInfo['public'] == true ) {
412 $('#edit_public').attr('checked','true');
413 } else {
414 $('#edit_public').removeAttr('checked');
415 }
416 },
417 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
418 $(event.target).parent().find('.ui-button').button("enable");
419 if( ajaxSettings.url.indexOf( 'textinfo' ) > -1
420 && ajaxSettings.type == 'POST' ) {
421 display_error( jqXHR, $("#edit_textinfo_status") );
422 }
423 });
424
425
426 // Set up the stemma editor dialog
427 $('#stemma-edit-dialog').dialog({
428 autoOpen: false,
429 height: 700,
430 width: 600,
431 modal: true,
432 buttons: {
433 Save: function (evt) {
434 $("#edit_stemma_status").empty();
435 $(evt.target).button("disable");
436 var stemmaseq = $('#stemmaseq').val();
437 var requrl = _get_url([ "stemma", selectedTextID, stemmaseq ]);
438 var reqparam = { 'dot': $('#dot_field').val() };
439 // TODO We need to stash the literal SVG string in stemmata
440 // somehow. Implement accept header on server side to decide
441 // whether to send application/json or application/xml?
442 $.post( requrl, reqparam, function (data) {
443 // We received a stemma SVG string in return.
444 // Update the current stemma sequence number
445 selectedStemmaID = data.stemmaid;
be536c89 446 delete data.stemmaid;
447 // Stash the answer in the appropriate spot in our stemma array
448 stemmata[selectedStemmaID] = data;
3f9d7ae5 449 // Display the new stemma
63378fe0 450 load_stemma( selectedStemmaID, true );
3f9d7ae5 451 // Reenable the button and close the form
452 $(evt.target).button("enable");
453 $('#stemma-edit-dialog').dialog('close');
454 }, 'json' );
455 },
456 Cancel: function() {
457 $('#stemma-edit-dialog').dialog('close');
458 }
459 },
460 open: function(evt) {
461 $("#edit_stemma_status").empty();
462 var stemmaseq = $('#stemmaseq').val();
463 if( stemmaseq == 'n' ) {
464 // If we are creating a new stemma, populate the textarea with a
465 // bare digraph.
466 $(evt.target).dialog('option', 'title', 'Add a new stemma')
db234220 467 $('#dot_field').val( "digraph \"NAME STEMMA HERE\" {\n\n}" );
3f9d7ae5 468 } else {
469 // If we are editing a stemma, grab its stemmadot and populate the
470 // textarea with that.
471 $(evt.target).dialog('option', 'title', 'Edit selected stemma')
472 $('#dot_field').val( 'Loading, please wait...' );
473 var doturl = _get_url([ "stemmadot", selectedTextID, stemmaseq ]);
474 $.getJSON( doturl, function (data) {
475 // Re-insert the line breaks
476 var dotstring = data.dot.replace(/\|n/gm, "\n");
477 $('#dot_field').val( dotstring );
478 });
479 }
480 },
481 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
482 $(event.target).parent().find('.ui-button').button("enable");
483 if( ajaxSettings.url.indexOf( 'stemma' ) > -1
484 && ajaxSettings.type == 'POST' ) {
485 display_error( jqXHR, $("#edit_stemma_status") );
486 }
487 });
db234220 488
489 $('#stemweb-ui-dialog').dialog({
490 autoOpen: false,
70744367 491 height: 160,
492 width: 225,
db234220 493 modal: true,
494 buttons: {
495 Run: function (evt) {
496 $("#stemweb_run_status").empty();
497 $(evt.target).button("disable");
70744367 498 var requrl = _get_url([ "stemweb", "request" ]);
499 var reqparam = $('#call_stemweb').serialize();
db234220 500 // TODO We need to stash the literal SVG string in stemmata
501 // somehow. Implement accept header on server side to decide
502 // whether to send application/json or application/xml?
70744367 503 $.getJSON( requrl, reqparam, function (data) {
504 // Job ID is in data.jobid. TODO do something with it.
e883f11b 505 selectedTextInfo.stemweb_jobid = data.jobid;
db234220 506 $(evt.target).button("enable");
e883f11b 507 $('#stemweb-ui-dialog').dialog('close');
2c514a6f 508 // Reload the current stemma to rejigger the buttons
509 load_stemma( selectedStemmaID, true );
db234220 510 }, 'json' );
511 },
512 Cancel: function() {
513 $('#stemweb-ui-dialog').dialog('close');
514 }
515 },
516 create: function(evt) {
517 // Call out to Stemweb to get the algorithm options, with which we
518 // populate the form.
519 var algorithmTypes = {};
520 var algorithmArgs = {};
521 $.each( stemwebAlgorithms, function( i, o ) {
db234220 522 if( o.model === 'algorithms.algorithm' ) {
70744367 523 // it's an algorithm.
db234220 524 algorithmTypes[ o.pk ] = o.fields;
70744367 525 } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) {
526 // it's an option for an algorithm that we should display.
db234220 527 algorithmArgs[ o.pk ] = o.fields;
528 }
529 });
530 $.each( algorithmTypes, function( pk, fields ) {
531 var algopt = $('<option>').attr( 'value', pk ).append( fields.name );
532 $('#stemweb_algorithm').append( algopt );
533 });
70744367 534 // Set up the relevant options for whichever algorithm is chosen.
535 // "key" -> form name, option ID "stemweb_$key_opt"
536 // "name" -> form label
537 $('#stemweb_algorithm').change( function() {
538 var pk = $(this).val();
539 $('#stemweb_runtime_options').empty();
540 $.each( algorithmTypes[pk].args, function( i, apk ) {
541 var argInfo = algorithmArgs[apk];
542 if( argInfo ) {
543 // Make the element ID
544 var optId = 'stemweb_' + argInfo.key + '_opt';
545 // Make the label
546 var optLabel = $('<label>').attr( 'for', optId )
547 .append( argInfo.name + ": " );
548 var optCtrl;
549 var argType = argInfo.value;
550 if( argType === 'positive_integer' ) {
551 // Make it an input field of smallish size.
552 optCtrl = $('<input>').attr( 'size', 4 );
553 } else if ( argType === 'boolean' ) {
554 // Make it a checkbox.
555 optCtrl = $('<checkbox>');
556 }
557 // Add the name and element ID
ed0ce314 558 optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId );
70744367 559 // Append the label and the option itself to the form.
560 $('#stemweb_runtime_options').append( optLabel )
561 .append( optCtrl ).append( $('<br>') );
562 }
563 });
564 });
565 // Prime the initial options
566 $('#stemweb_algorithm').change();
db234220 567 },
568 open: function(evt) {
70744367 569 $('#stemweb_run_status').empty();
b8f3a8c8 570 $('#stemweb_tradition').attr('value', selectedTextID );
db234220 571 },
572 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
573 $(event.target).parent().find('.ui-button').button("enable");
ed0ce314 574 if( ajaxSettings.url.indexOf( 'stemweb/request' ) > -1 ) {
db234220 575 display_error( jqXHR, $("#stemweb_run_status") );
576 }
577 });
3f9d7ae5 578
579 $('#upload-collation-dialog').dialog({
580 autoOpen: false,
e0b90236 581 height: 360,
3f9d7ae5 582 width: 480,
583 modal: true,
584 buttons: {
3f9d7ae5 585 upload: {
586 text: 'Upload',
587 id: 'upload_button',
588 click: function() {
589 $('#upload_status').empty();
e0b90236 590 $('#upload_button').button("disable");
591 upload_new();
3f9d7ae5 592 }
593 },
ab0d1218 594 pick_file: {
595 text: 'Pick File',
596 id: 'pick_file_button',
597 click: function() {
598 $('#new_file').click();
599 }
600 },
3f9d7ae5 601 Cancel: function() {
602 $('#upload-collation-dialog').dialog('close');
603 }
604 },
e0b90236 605 open: function() {
606 // Set the upload button to its correct state based on
607 // whether a file is loaded
608 file_selected( $('#new_file').get(0) );
2ece58b3 609 $('#upload_status').empty();
3f9d7ae5 610 }
e0b90236 611 }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
612 // Reset button state
613 file_selected( $('#new_file').get(0) );
614 // Display error message if applicable
615 if( ajaxSettings.url.indexOf( 'newtradition' ) > -1
616 && ajaxSettings.type == 'POST' ) {
617 display_error( jqXHR, $("#upload_status") );
618 }
619 });;
3f9d7ae5 620
621 $('#stemma_graph').mousedown( function(evt) {
622 evt.stopPropagation();
623 $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] );
624 $('body').mousemove( function(evt) {
625 mouse_scale = 1; // for now, was: mouse_scale = svg_root_element.getScreenCTM().a;
626 dx = (evt.clientX - $('#stemma_graph').data( 'mousedown_xy' )[0]) / mouse_scale;
627 dy = (evt.clientY - $('#stemma_graph').data( 'mousedown_xy' )[1]) / mouse_scale;
628 $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] );
629 var svg_root = $('#stemma_graph svg').svg().svg('get').root();
630 var g = $('g.graph', svg_root).get(0);
631 current_translate = g.getAttribute( 'transform' ).split(/translate\(/)[1].split(')',1)[0].split(' ');
632 new_transform = g.getAttribute( 'transform' ).replace( /translate\([^\)]*\)/, 'translate(' + (parseFloat(current_translate[0]) + dx) + ' ' + (parseFloat(current_translate[1]) + dy) + ')' );
633 g.setAttribute( 'transform', new_transform );
634 evt.returnValue = false;
635 evt.preventDefault();
636 return false;
637 });
638 $('body').mouseup( function(evt) {
639 $('body').unbind('mousemove');
640 $('body').unbind('mouseup');
641 });
642 });
643
644 $('#stemma_graph').mousewheel(function (event, delta) {
645 event.returnValue = false;
646 event.preventDefault();
647 if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta;
648 if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail;
649 if( delta < -9 ) { delta = -9 };
650 var z = 1 + delta/10;
651 z = delta > 0 ? 1 : -1;
652 var svg_root = $('#stemma_graph svg').svg().svg('get').root();
653 var g = $('g.graph', svg_root).get(0);
654 if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 1000))) {
655 var scaleLevel = z/10;
656 current_scale = parseFloat( g.getAttribute( 'transform' ).split(/scale\(/)[1].split(')',1)[0].split(' ')[0] );
657 new_transform = g.getAttribute( 'transform' ).replace( /scale\([^\)]*\)/, 'scale(' + (current_scale + scaleLevel) + ')' );
658 g.setAttribute( 'transform', new_transform );
659 }
660 });
661
662});