Commit | Line | Data |
3f9d7ae5 |
1 | // Global state variables |
2 | var selectedTextID; |
3 | var selectedTextInfo; |
4 | var selectedStemmaID = -1; |
5 | var stemmata = []; |
6 | |
7 | // Load the names of the appropriate traditions into the directory div. |
8 | function 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 |
30 | function 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 |
82 | function 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 |
113 | function 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 |
129 | function 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 |
164 | function 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 |
196 | function 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 }); |
b63f3a77 |
224 | set_stemma_interactive( theSVG ); |
bd3ccd15 |
225 | } |
226 | }); |
227 | } |
228 | |
b63f3a77 |
229 | function set_stemma_interactive( svg_element ) { |
230 | $( "#root_tree_dialog_button_ok" ).click( function() { |
231 | // AJAX call goes here |
232 | } ); |
233 | $.each( $( 'ellipse', svg_element ), function(index) { |
39b8b37b |
234 | var ellipse = $(this); |
235 | var g = ellipse.parent( 'g' ); |
236 | g.click( function(evt) { |
237 | if( typeof root_tree_dialog_timeout !== 'undefined' ) { clearTimeout( root_tree_dialog_timeout ) }; |
238 | g.unbind( 'mouseleave' ); |
b63f3a77 |
239 | var dialog = $( '#root_tree_dialog' ); |
39b8b37b |
240 | dialog.hide(); |
241 | dialog.css( 'top', evt.pageY + 3 ); |
242 | dialog.css( 'left', evt.pageX + 3 ); |
b63f3a77 |
243 | dialog.show(); |
39b8b37b |
244 | root_tree_dialog_timeout = setTimeout( function() { |
245 | $( '#root_tree_dialog' ).hide(); |
246 | ellipse.removeClass( 'stemma_node_highlight' ); |
247 | g.mouseleave( function() { ellipse.removeClass( 'stemma_node_highlight' ) } ); |
248 | }, 3000 ); |
b63f3a77 |
249 | } ); |
39b8b37b |
250 | g.mouseenter( function() { |
251 | $( 'ellipse.stemma_node_highlight' ).removeClass( 'stemma_node_highlight' ); |
252 | ellipse.addClass( 'stemma_node_highlight' ) |
253 | } ); |
254 | g.mouseleave( function() { ellipse.removeClass( 'stemma_node_highlight' ) } ); |
255 | } ); |
b63f3a77 |
256 | } |
39b8b37b |
257 | |
3f9d7ae5 |
258 | // General-purpose error-handling function. |
259 | // TODO make sure this gets used throughout, where appropriate. |
75354c3a |
260 | function display_error( jqXHR, el ) { |
ce1c5863 |
261 | var errmsg; |
262 | if( jqXHR.responseText == "" ) { |
263 | errmsg = "perhaps the server went down?" |
75354c3a |
264 | } else { |
ce1c5863 |
265 | var errobj; |
266 | try { |
267 | errobj = jQuery.parseJSON( jqXHR.responseText ); |
268 | errmsg = errobj.error; |
269 | } catch ( parse_err ) { |
270 | errmsg = "something went wrong on the server." |
271 | } |
75354c3a |
272 | } |
ce1c5863 |
273 | var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg ); |
75354c3a |
274 | $(el).empty().append( msghtml ).show(); |
3f9d7ae5 |
275 | } |
276 | |
e0b90236 |
277 | // Event to enable the upload button when a file has been selected |
278 | function file_selected( e ) { |
279 | if( e.files.length == 1 ) { |
280 | $('#upload_button').button('enable'); |
ab0d1218 |
281 | $('#new_file_name_container').html( '<span id="new_file_name">' + e.files[0].name + '</span>' ); |
e0b90236 |
282 | } else { |
283 | $('#upload_button').button('disable'); |
ab0d1218 |
284 | $('#new_file_name_container').html( '(Use \'pick file\' to select a tradition file to upload.)' ); |
e0b90236 |
285 | } |
286 | } |
287 | |
2ece58b3 |
288 | // Implement our own AJAX method that uses the features of XMLHttpRequest2 |
289 | // but try to let it have a similar interface to jquery.post |
290 | // The data var needs to be a FormData() object. |
291 | // The callback will be given a single argument, which is the response data |
292 | // of the given type. |
293 | |
294 | function post_xhr2( url, data, cb, type ) { |
295 | if( !type ) { |
296 | type = 'json'; |
297 | } |
298 | var xhr = new XMLHttpRequest(); |
299 | // Set the expected response type |
300 | if( type === 'data' ) { |
301 | xhr.responseType = 'blob'; |
302 | } else if( type === 'xml' ) { |
303 | xhr.responseType = 'document'; |
304 | } |
305 | // Post the form |
306 | // Gin up an AJAX settings object |
307 | $.ajaxSetup({ url: url, type: 'POST' }); |
308 | xhr.open( 'POST', url, true ); |
309 | // Handle the results |
310 | xhr.onload = function( e ) { |
311 | // Get the response and parse it |
312 | // Call the callback with the response, whatever it was |
313 | var xhrs = e.target; |
314 | if( xhrs.status > 199 && xhrs.status < 300 ) { // Success |
315 | var resp; |
316 | if( type === 'json' ) { |
317 | resp = $.parseJSON( xhrs.responseText ); |
318 | } else if ( type === 'xml' ) { |
319 | resp = xhrs.responseXML; |
320 | } else if ( type === 'text' ) { |
321 | resp = xhrs.responseText; |
322 | } else { |
323 | resp = xhrs.response; |
324 | } |
325 | cb( resp ); |
326 | } else { |
327 | // Trigger the ajaxError... |
328 | _trigger_ajaxerror( e ); |
329 | } |
330 | }; |
331 | xhr.onerror = _trigger_ajaxerror; |
332 | xhr.onabort = _trigger_ajaxerror; |
333 | xhr.send( data ); |
334 | } |
335 | |
336 | function _trigger_ajaxerror( e ) { |
337 | var xhr = e.target; |
338 | var thrown = xhr.statusText || 'Request error'; |
339 | jQuery.event.trigger( 'ajaxError', [ xhr, $.ajaxSettings, thrown ]); |
340 | } |
341 | |
e0b90236 |
342 | function upload_new () { |
343 | // Serialize the upload form, get the file and attach it to the request, |
344 | // POST the lot and handle the response. |
345 | var newfile = $('#new_file').get(0).files[0]; |
346 | var reader = new FileReader(); |
347 | reader.onload = function( evt ) { |
2ece58b3 |
348 | var data = new FormData(); |
349 | $.each( $('#new_tradition').serializeArray(), function( i, o ) { |
350 | data.append( o.name, o.value ); |
351 | }); |
352 | data.append( 'file', newfile ); |
e0b90236 |
353 | var upload_url = _get_url([ 'newtradition' ]); |
2ece58b3 |
354 | post_xhr2( upload_url, data, function( ret ) { |
e0b90236 |
355 | if( ret.id ) { |
356 | $('#upload-collation-dialog').dialog('close'); |
357 | refreshDirectory(); |
358 | loadTradition( ret.id, ret.name, 1 ); |
359 | } else if( ret.error ) { |
360 | $('#upload_status').empty().append( |
361 | $('<span>').attr('class', 'error').append( ret.error ) ); |
362 | } |
2ece58b3 |
363 | }, 'json' ); |
e0b90236 |
364 | }; |
365 | reader.onerror = function( evt ) { |
366 | var err_resp = 'File read error'; |
367 | if( e.name == 'NotFoundError' ) { |
368 | err_resp = 'File not found'; |
369 | } else if ( e.name == 'NotReadableError' ) { |
370 | err_resp == 'File unreadable - is it yours?'; |
371 | } else if ( e.name == 'EncodingError' ) { |
372 | err_resp == 'File cannot be encoded - is it too long?'; |
373 | } else if ( e.name == 'SecurityError' ) { |
374 | err_resp == 'File read security error'; |
375 | } |
376 | // Fake a jqXHR object that we can pass to our generic error handler. |
377 | var jqxhr = { responseText: '{error:"' + err_resp + '"}' }; |
378 | display_error( jqxhr, $('#upload_status') ); |
379 | $('#upload_button').button('disable'); |
380 | } |
381 | |
382 | reader.readAsBinaryString( newfile ); |
3f9d7ae5 |
383 | } |
384 | |
385 | // Utility function to neatly construct an application URL |
386 | function _get_url( els ) { |
387 | return basepath + els.join('/'); |
388 | } |
389 | |
bd3ccd15 |
390 | |
3f9d7ae5 |
391 | $(document).ready( function() { |
50778a5d |
392 | // See if we have the browser functionality we need |
393 | // TODO Also think of a test for SVG readiness |
7c25980f |
394 | if( !!window.FileReader && !!window.File ) { |
50778a5d |
395 | $('#compatibility_check').empty(); |
396 | } |
b63f3a77 |
397 | |
398 | // hide dialog not yet in use |
399 | $('#root_tree_dialog').hide(); |
50778a5d |
400 | |
3f9d7ae5 |
401 | // call out to load the directory div |
402 | $('#textinfo_container').hide(); |
403 | $('#textinfo_waitbox').hide(); |
404 | refreshDirectory(); |
405 | |
406 | // Set up the textinfo edit dialog |
407 | $('#textinfo-edit-dialog').dialog({ |
408 | autoOpen: false, |
409 | height: 200, |
410 | width: 300, |
411 | modal: true, |
412 | buttons: { |
413 | Save: function (evt) { |
414 | $("#edit_textinfo_status").empty(); |
415 | $(evt.target).button("disable"); |
416 | var requrl = _get_url([ "textinfo", selectedTextID ]); |
417 | var reqparam = $('#edit_textinfo').serialize(); |
418 | $.post( requrl, reqparam, function (data) { |
419 | // Reload the selected text fields |
420 | selectedTextInfo = data; |
421 | load_textinfo(); |
422 | // Reenable the button and close the form |
423 | $(evt.target).button("enable"); |
424 | $('#textinfo-edit-dialog').dialog('close'); |
425 | }, 'json' ); |
426 | }, |
427 | Cancel: function() { |
428 | $('#textinfo-edit-dialog').dialog('close'); |
429 | } |
430 | }, |
431 | open: function() { |
432 | $("#edit_textinfo_status").empty(); |
433 | // Populate the form fields with the current values |
434 | // edit_(name, language, public, owner) |
435 | $.each([ 'name', 'language', 'owner' ], function( idx, k ) { |
436 | var fname = '#edit_' + k; |
437 | // Special case: language Default is basically language null |
438 | if( k == 'language' && selectedTextInfo[k] == 'Default' ) { |
439 | $(fname).val( "" ); |
440 | } else { |
441 | $(fname).val( selectedTextInfo[k] ); |
442 | } |
443 | }); |
444 | if( selectedTextInfo['public'] == true ) { |
445 | $('#edit_public').attr('checked','true'); |
446 | } else { |
447 | $('#edit_public').removeAttr('checked'); |
448 | } |
449 | }, |
450 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
451 | $(event.target).parent().find('.ui-button').button("enable"); |
452 | if( ajaxSettings.url.indexOf( 'textinfo' ) > -1 |
453 | && ajaxSettings.type == 'POST' ) { |
454 | display_error( jqXHR, $("#edit_textinfo_status") ); |
455 | } |
456 | }); |
457 | |
458 | |
459 | // Set up the stemma editor dialog |
460 | $('#stemma-edit-dialog').dialog({ |
461 | autoOpen: false, |
462 | height: 700, |
463 | width: 600, |
464 | modal: true, |
465 | buttons: { |
466 | Save: function (evt) { |
467 | $("#edit_stemma_status").empty(); |
468 | $(evt.target).button("disable"); |
469 | var stemmaseq = $('#stemmaseq').val(); |
470 | var requrl = _get_url([ "stemma", selectedTextID, stemmaseq ]); |
471 | var reqparam = { 'dot': $('#dot_field').val() }; |
472 | // TODO We need to stash the literal SVG string in stemmata |
473 | // somehow. Implement accept header on server side to decide |
474 | // whether to send application/json or application/xml? |
475 | $.post( requrl, reqparam, function (data) { |
476 | // We received a stemma SVG string in return. |
477 | // Update the current stemma sequence number |
478 | selectedStemmaID = data.stemmaid; |
be536c89 |
479 | delete data.stemmaid; |
480 | // Stash the answer in the appropriate spot in our stemma array |
481 | stemmata[selectedStemmaID] = data; |
3f9d7ae5 |
482 | // Display the new stemma |
63378fe0 |
483 | load_stemma( selectedStemmaID, true ); |
3f9d7ae5 |
484 | // Reenable the button and close the form |
485 | $(evt.target).button("enable"); |
486 | $('#stemma-edit-dialog').dialog('close'); |
487 | }, 'json' ); |
488 | }, |
489 | Cancel: function() { |
490 | $('#stemma-edit-dialog').dialog('close'); |
491 | } |
492 | }, |
493 | open: function(evt) { |
494 | $("#edit_stemma_status").empty(); |
495 | var stemmaseq = $('#stemmaseq').val(); |
496 | if( stemmaseq == 'n' ) { |
497 | // If we are creating a new stemma, populate the textarea with a |
498 | // bare digraph. |
499 | $(evt.target).dialog('option', 'title', 'Add a new stemma') |
db234220 |
500 | $('#dot_field').val( "digraph \"NAME STEMMA HERE\" {\n\n}" ); |
3f9d7ae5 |
501 | } else { |
502 | // If we are editing a stemma, grab its stemmadot and populate the |
503 | // textarea with that. |
504 | $(evt.target).dialog('option', 'title', 'Edit selected stemma') |
505 | $('#dot_field').val( 'Loading, please wait...' ); |
506 | var doturl = _get_url([ "stemmadot", selectedTextID, stemmaseq ]); |
507 | $.getJSON( doturl, function (data) { |
508 | // Re-insert the line breaks |
509 | var dotstring = data.dot.replace(/\|n/gm, "\n"); |
510 | $('#dot_field').val( dotstring ); |
511 | }); |
512 | } |
513 | }, |
514 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
515 | $(event.target).parent().find('.ui-button').button("enable"); |
516 | if( ajaxSettings.url.indexOf( 'stemma' ) > -1 |
517 | && ajaxSettings.type == 'POST' ) { |
518 | display_error( jqXHR, $("#edit_stemma_status") ); |
519 | } |
520 | }); |
db234220 |
521 | |
522 | $('#stemweb-ui-dialog').dialog({ |
523 | autoOpen: false, |
70744367 |
524 | height: 160, |
525 | width: 225, |
db234220 |
526 | modal: true, |
527 | buttons: { |
528 | Run: function (evt) { |
529 | $("#stemweb_run_status").empty(); |
530 | $(evt.target).button("disable"); |
70744367 |
531 | var requrl = _get_url([ "stemweb", "request" ]); |
532 | var reqparam = $('#call_stemweb').serialize(); |
db234220 |
533 | // TODO We need to stash the literal SVG string in stemmata |
534 | // somehow. Implement accept header on server side to decide |
535 | // whether to send application/json or application/xml? |
70744367 |
536 | $.getJSON( requrl, reqparam, function (data) { |
537 | // Job ID is in data.jobid. TODO do something with it. |
e883f11b |
538 | selectedTextInfo.stemweb_jobid = data.jobid; |
db234220 |
539 | $(evt.target).button("enable"); |
e883f11b |
540 | $('#stemweb-ui-dialog').dialog('close'); |
2c514a6f |
541 | // Reload the current stemma to rejigger the buttons |
542 | load_stemma( selectedStemmaID, true ); |
db234220 |
543 | }, 'json' ); |
544 | }, |
545 | Cancel: function() { |
546 | $('#stemweb-ui-dialog').dialog('close'); |
547 | } |
548 | }, |
549 | create: function(evt) { |
550 | // Call out to Stemweb to get the algorithm options, with which we |
551 | // populate the form. |
552 | var algorithmTypes = {}; |
553 | var algorithmArgs = {}; |
554 | $.each( stemwebAlgorithms, function( i, o ) { |
db234220 |
555 | if( o.model === 'algorithms.algorithm' ) { |
70744367 |
556 | // it's an algorithm. |
db234220 |
557 | algorithmTypes[ o.pk ] = o.fields; |
70744367 |
558 | } else if( o.model == 'algorithms.algorithmarg' && o.fields.external ) { |
559 | // it's an option for an algorithm that we should display. |
db234220 |
560 | algorithmArgs[ o.pk ] = o.fields; |
561 | } |
562 | }); |
563 | $.each( algorithmTypes, function( pk, fields ) { |
564 | var algopt = $('<option>').attr( 'value', pk ).append( fields.name ); |
565 | $('#stemweb_algorithm').append( algopt ); |
566 | }); |
70744367 |
567 | // Set up the relevant options for whichever algorithm is chosen. |
568 | // "key" -> form name, option ID "stemweb_$key_opt" |
569 | // "name" -> form label |
570 | $('#stemweb_algorithm').change( function() { |
571 | var pk = $(this).val(); |
572 | $('#stemweb_runtime_options').empty(); |
573 | $.each( algorithmTypes[pk].args, function( i, apk ) { |
574 | var argInfo = algorithmArgs[apk]; |
575 | if( argInfo ) { |
576 | // Make the element ID |
577 | var optId = 'stemweb_' + argInfo.key + '_opt'; |
578 | // Make the label |
579 | var optLabel = $('<label>').attr( 'for', optId ) |
580 | .append( argInfo.name + ": " ); |
581 | var optCtrl; |
582 | var argType = argInfo.value; |
583 | if( argType === 'positive_integer' ) { |
584 | // Make it an input field of smallish size. |
585 | optCtrl = $('<input>').attr( 'size', 4 ); |
586 | } else if ( argType === 'boolean' ) { |
587 | // Make it a checkbox. |
588 | optCtrl = $('<checkbox>'); |
589 | } |
590 | // Add the name and element ID |
ed0ce314 |
591 | optCtrl.attr( 'name', argInfo.key ).attr( 'id', optId ); |
70744367 |
592 | // Append the label and the option itself to the form. |
593 | $('#stemweb_runtime_options').append( optLabel ) |
594 | .append( optCtrl ).append( $('<br>') ); |
595 | } |
596 | }); |
597 | }); |
598 | // Prime the initial options |
599 | $('#stemweb_algorithm').change(); |
db234220 |
600 | }, |
601 | open: function(evt) { |
70744367 |
602 | $('#stemweb_run_status').empty(); |
b8f3a8c8 |
603 | $('#stemweb_tradition').attr('value', selectedTextID ); |
db234220 |
604 | }, |
605 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
606 | $(event.target).parent().find('.ui-button').button("enable"); |
ed0ce314 |
607 | if( ajaxSettings.url.indexOf( 'stemweb/request' ) > -1 ) { |
db234220 |
608 | display_error( jqXHR, $("#stemweb_run_status") ); |
609 | } |
610 | }); |
3f9d7ae5 |
611 | |
612 | $('#upload-collation-dialog').dialog({ |
613 | autoOpen: false, |
e0b90236 |
614 | height: 360, |
3f9d7ae5 |
615 | width: 480, |
616 | modal: true, |
617 | buttons: { |
3f9d7ae5 |
618 | upload: { |
619 | text: 'Upload', |
620 | id: 'upload_button', |
621 | click: function() { |
622 | $('#upload_status').empty(); |
e0b90236 |
623 | $('#upload_button').button("disable"); |
624 | upload_new(); |
3f9d7ae5 |
625 | } |
626 | }, |
ab0d1218 |
627 | pick_file: { |
628 | text: 'Pick File', |
629 | id: 'pick_file_button', |
630 | click: function() { |
631 | $('#new_file').click(); |
632 | } |
633 | }, |
3f9d7ae5 |
634 | Cancel: function() { |
635 | $('#upload-collation-dialog').dialog('close'); |
636 | } |
637 | }, |
e0b90236 |
638 | open: function() { |
639 | // Set the upload button to its correct state based on |
640 | // whether a file is loaded |
641 | file_selected( $('#new_file').get(0) ); |
2ece58b3 |
642 | $('#upload_status').empty(); |
3f9d7ae5 |
643 | } |
e0b90236 |
644 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
645 | // Reset button state |
646 | file_selected( $('#new_file').get(0) ); |
647 | // Display error message if applicable |
648 | if( ajaxSettings.url.indexOf( 'newtradition' ) > -1 |
649 | && ajaxSettings.type == 'POST' ) { |
650 | display_error( jqXHR, $("#upload_status") ); |
651 | } |
652 | });; |
3f9d7ae5 |
653 | |
654 | $('#stemma_graph').mousedown( function(evt) { |
655 | evt.stopPropagation(); |
656 | $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] ); |
657 | $('body').mousemove( function(evt) { |
658 | mouse_scale = 1; // for now, was: mouse_scale = svg_root_element.getScreenCTM().a; |
659 | dx = (evt.clientX - $('#stemma_graph').data( 'mousedown_xy' )[0]) / mouse_scale; |
660 | dy = (evt.clientY - $('#stemma_graph').data( 'mousedown_xy' )[1]) / mouse_scale; |
661 | $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] ); |
662 | var svg_root = $('#stemma_graph svg').svg().svg('get').root(); |
663 | var g = $('g.graph', svg_root).get(0); |
664 | current_translate = g.getAttribute( 'transform' ).split(/translate\(/)[1].split(')',1)[0].split(' '); |
665 | new_transform = g.getAttribute( 'transform' ).replace( /translate\([^\)]*\)/, 'translate(' + (parseFloat(current_translate[0]) + dx) + ' ' + (parseFloat(current_translate[1]) + dy) + ')' ); |
666 | g.setAttribute( 'transform', new_transform ); |
667 | evt.returnValue = false; |
668 | evt.preventDefault(); |
669 | return false; |
670 | }); |
671 | $('body').mouseup( function(evt) { |
672 | $('body').unbind('mousemove'); |
673 | $('body').unbind('mouseup'); |
674 | }); |
675 | }); |
676 | |
677 | $('#stemma_graph').mousewheel(function (event, delta) { |
678 | event.returnValue = false; |
679 | event.preventDefault(); |
680 | if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta; |
681 | if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail; |
682 | if( delta < -9 ) { delta = -9 }; |
683 | var z = 1 + delta/10; |
684 | z = delta > 0 ? 1 : -1; |
685 | var svg_root = $('#stemma_graph svg').svg().svg('get').root(); |
686 | var g = $('g.graph', svg_root).get(0); |
687 | if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 1000))) { |
688 | var scaleLevel = z/10; |
689 | current_scale = parseFloat( g.getAttribute( 'transform' ).split(/scale\(/)[1].split(')',1)[0].split(' ')[0] ); |
690 | new_transform = g.getAttribute( 'transform' ).replace( /scale\([^\)]*\)/, 'scale(' + (current_scale + scaleLevel) + ')' ); |
691 | g.setAttribute( 'transform', new_transform ); |
692 | } |
693 | }); |
694 | |
695 | }); |