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(); |
48 | $('#open_stemma_edit').show(); |
49 | $('#open_textinfo_edit').show(); |
75354c3a |
50 | } else { |
ce1c5863 |
51 | $('#open_stemma_add').hide(); |
52 | $('#open_stemma_edit').hide(); |
53 | $('#open_textinfo_edit').hide(); |
75354c3a |
54 | } |
55 | |
62723740 |
56 | // Then get and load the actual content. |
98a45925 |
57 | // TODO: scale #stemma_graph both horizontally and vertically |
f6a8db89 |
58 | // TODO: load svgs from SVG.Jquery (to make scaling react in Safari) |
3f9d7ae5 |
59 | $.getJSON( _get_url([ "textinfo", textid ]), function (textdata) { |
98a45925 |
60 | // Add the scalar data |
75354c3a |
61 | selectedTextInfo = textdata; |
62 | load_textinfo(); |
63 | // Add the stemma(ta) and set up the stexaminer button |
98a45925 |
64 | stemmata = textdata.stemmata; |
65 | if( stemmata.length ) { |
66 | selectedStemmaID = 0; |
57e3a008 |
67 | $('#run_stexaminer').show(); |
65a0c9c6 |
68 | } else { |
57e3a008 |
69 | selectedStemmaID = -1; |
65a0c9c6 |
70 | $('#open_stemma_edit').hide(); |
71 | $('#run_stexaminer').hide(); |
65a0c9c6 |
72 | } |
3f9d7ae5 |
73 | load_stemma( selectedStemmaID ); |
98a45925 |
74 | // Set up the relationship mapper button |
3f9d7ae5 |
75 | $('#run_relater').attr( 'action', _get_url([ "relation", textid ]) ); |
98a45925 |
76 | }); |
77 | } |
78 | |
3f9d7ae5 |
79 | // Load the metadata about a tradition into the appropriate div. |
75354c3a |
80 | function load_textinfo() { |
81 | $('#textinfo_waitbox').hide(); |
82 | $('#textinfo_load_status').empty(); |
83 | $('#textinfo_container').show(); |
84 | $('.texttitle').empty().append( selectedTextInfo.name ); |
85 | // Witnesses |
86 | $('#witness_num').empty().append( selectedTextInfo.witnesses.size ); |
87 | $('#witness_list').empty().append( selectedTextInfo.witnesses.join( ', ' ) ); |
88 | // Who the owner is |
89 | $('#owner_id').empty().append('no one'); |
90 | if( selectedTextInfo.owner ) { |
91 | $('#owner_id').empty().append( selectedTextInfo.owner ); |
92 | } |
93 | // Whether or not it is public |
94 | $('#not_public').empty(); |
95 | if( selectedTextInfo['public'] == false ) { |
96 | $('#not_public').append('NOT '); |
97 | } |
98 | // What language setting it has, if any |
99 | $('#marked_language').empty().append('no language set'); |
100 | if( selectedTextInfo.language && selectedTextInfo.language != 'Default' ) { |
101 | $('#marked_language').empty().append( selectedTextInfo.language ); |
102 | } |
103 | } |
104 | |
3f9d7ae5 |
105 | // Enable / disable the appropriate buttons for paging through the stemma. |
65a0c9c6 |
106 | function show_stemmapager () { |
bf81fb57 |
107 | $('.pager_left_button').unbind('click').addClass( 'greyed_out' ); |
108 | $('.pager_right_button').unbind('click').addClass( 'greyed_out' ); |
65a0c9c6 |
109 | if( selectedStemmaID > 0 ) { |
110 | $('.pager_left_button').click( function () { |
111 | load_stemma( selectedStemmaID - 1 ); |
bf81fb57 |
112 | }).removeClass( 'greyed_out' ); |
65a0c9c6 |
113 | } |
114 | if( selectedStemmaID + 1 < stemmata.length ) { |
115 | $('.pager_right_button').click( function () { |
116 | load_stemma( selectedStemmaID + 1 ); |
bf81fb57 |
117 | }).removeClass( 'greyed_out' ); |
65a0c9c6 |
118 | } |
119 | } |
120 | |
3f9d7ae5 |
121 | // Load a given stemma SVG into the stemmagraph box. |
75354c3a |
122 | function load_stemma( idx ) { |
65a0c9c6 |
123 | // Load the stemma at idx |
124 | selectedStemmaID = idx; |
57e3a008 |
125 | show_stemmapager(); |
98a45925 |
126 | if( idx > -1 ) { |
bd3ccd15 |
127 | loadSVG( stemmata[idx] ); |
98a45925 |
128 | // Stexaminer submit action |
3f9d7ae5 |
129 | var stexpath = _get_url([ "stexaminer", selectedTextID, idx ]); |
98a45925 |
130 | $('#run_stexaminer').attr( 'action', stexpath ); |
40803b80 |
131 | setTimeout( 'start_element_height = $("#stemma_graph .node")[0].getBBox().height;', 500 ); |
98a45925 |
132 | } |
5ba6c2b4 |
133 | } |
75354c3a |
134 | |
bd3ccd15 |
135 | // Load the SVG we are given |
136 | function loadSVG(svgData) { |
137 | var svgElement = $('#stemma_graph'); |
138 | |
139 | $(svgElement).svg('destroy'); |
140 | |
141 | $(svgElement).svg({ |
142 | loadURL: svgData, |
143 | onLoad : function () { |
144 | var theSVG = svgElement.find('svg'); |
145 | var svgoffset = theSVG.offset(); |
146 | // Firefox needs a different offset, stupidly enough |
147 | var browseroffset = 1; |
148 | if( navigator.userAgent.indexOf('Firefox') > -1 ) { |
149 | browseroffset = 3; |
150 | } |
151 | var topoffset = theSVG.position().top - svgElement.position().top - browseroffset; |
152 | // If we are on Safari, we need to get rid of the 'pt' in the width/height |
153 | // specifications |
154 | theSVG.offset({ top: svgoffset.top - topoffset, left: svgoffset.left }); |
155 | } |
156 | }); |
157 | } |
158 | |
3f9d7ae5 |
159 | // General-purpose error-handling function. |
160 | // TODO make sure this gets used throughout, where appropriate. |
75354c3a |
161 | function display_error( jqXHR, el ) { |
ce1c5863 |
162 | var errmsg; |
163 | if( jqXHR.responseText == "" ) { |
164 | errmsg = "perhaps the server went down?" |
75354c3a |
165 | } else { |
ce1c5863 |
166 | var errobj; |
167 | try { |
168 | errobj = jQuery.parseJSON( jqXHR.responseText ); |
169 | errmsg = errobj.error; |
170 | } catch ( parse_err ) { |
171 | errmsg = "something went wrong on the server." |
172 | } |
75354c3a |
173 | } |
ce1c5863 |
174 | var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg ); |
75354c3a |
175 | $(el).empty().append( msghtml ).show(); |
3f9d7ae5 |
176 | } |
177 | |
178 | function start_upload_dialog() { |
179 | if( typeof uploader != 'undefined' ){ uploader.destroy() }; |
180 | $('#upload-collation-dialog').dialog('option', 'attach_uploader')(); |
181 | $('#upload_status').empty(); |
182 | $('#upload_button').button('disable'); |
183 | $('#upload-collation-dialog').dialog('open'); |
184 | } |
185 | |
186 | // Utility function to neatly construct an application URL |
187 | function _get_url( els ) { |
188 | return basepath + els.join('/'); |
189 | } |
190 | |
bd3ccd15 |
191 | |
3f9d7ae5 |
192 | $(document).ready( function() { |
193 | // call out to load the directory div |
194 | $('#textinfo_container').hide(); |
195 | $('#textinfo_waitbox').hide(); |
196 | refreshDirectory(); |
197 | |
198 | // Set up the textinfo edit dialog |
199 | $('#textinfo-edit-dialog').dialog({ |
200 | autoOpen: false, |
201 | height: 200, |
202 | width: 300, |
203 | modal: true, |
204 | buttons: { |
205 | Save: function (evt) { |
206 | $("#edit_textinfo_status").empty(); |
207 | $(evt.target).button("disable"); |
208 | var requrl = _get_url([ "textinfo", selectedTextID ]); |
209 | var reqparam = $('#edit_textinfo').serialize(); |
210 | $.post( requrl, reqparam, function (data) { |
211 | // Reload the selected text fields |
212 | selectedTextInfo = data; |
213 | load_textinfo(); |
214 | // Reenable the button and close the form |
215 | $(evt.target).button("enable"); |
216 | $('#textinfo-edit-dialog').dialog('close'); |
217 | }, 'json' ); |
218 | }, |
219 | Cancel: function() { |
220 | $('#textinfo-edit-dialog').dialog('close'); |
221 | } |
222 | }, |
223 | open: function() { |
224 | $("#edit_textinfo_status").empty(); |
225 | // Populate the form fields with the current values |
226 | // edit_(name, language, public, owner) |
227 | $.each([ 'name', 'language', 'owner' ], function( idx, k ) { |
228 | var fname = '#edit_' + k; |
229 | // Special case: language Default is basically language null |
230 | if( k == 'language' && selectedTextInfo[k] == 'Default' ) { |
231 | $(fname).val( "" ); |
232 | } else { |
233 | $(fname).val( selectedTextInfo[k] ); |
234 | } |
235 | }); |
236 | if( selectedTextInfo['public'] == true ) { |
237 | $('#edit_public').attr('checked','true'); |
238 | } else { |
239 | $('#edit_public').removeAttr('checked'); |
240 | } |
241 | }, |
242 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
243 | $(event.target).parent().find('.ui-button').button("enable"); |
244 | if( ajaxSettings.url.indexOf( 'textinfo' ) > -1 |
245 | && ajaxSettings.type == 'POST' ) { |
246 | display_error( jqXHR, $("#edit_textinfo_status") ); |
247 | } |
248 | }); |
249 | |
250 | |
251 | // Set up the stemma editor dialog |
252 | $('#stemma-edit-dialog').dialog({ |
253 | autoOpen: false, |
254 | height: 700, |
255 | width: 600, |
256 | modal: true, |
257 | buttons: { |
258 | Save: function (evt) { |
259 | $("#edit_stemma_status").empty(); |
260 | $(evt.target).button("disable"); |
261 | var stemmaseq = $('#stemmaseq').val(); |
262 | var requrl = _get_url([ "stemma", selectedTextID, stemmaseq ]); |
263 | var reqparam = { 'dot': $('#dot_field').val() }; |
264 | // TODO We need to stash the literal SVG string in stemmata |
265 | // somehow. Implement accept header on server side to decide |
266 | // whether to send application/json or application/xml? |
267 | $.post( requrl, reqparam, function (data) { |
268 | // We received a stemma SVG string in return. |
269 | // Update the current stemma sequence number |
270 | selectedStemmaID = data.stemmaid; |
271 | // Stash the answer in our SVG array |
272 | stemmata[selectedStemmaID] = data.stemmasvg; |
273 | // Display the new stemma |
274 | load_stemma( selectedStemmaID ); |
275 | // Reenable the button and close the form |
276 | $(evt.target).button("enable"); |
277 | $('#stemma-edit-dialog').dialog('close'); |
278 | }, 'json' ); |
279 | }, |
280 | Cancel: function() { |
281 | $('#stemma-edit-dialog').dialog('close'); |
282 | } |
283 | }, |
284 | open: function(evt) { |
285 | $("#edit_stemma_status").empty(); |
286 | var stemmaseq = $('#stemmaseq').val(); |
287 | if( stemmaseq == 'n' ) { |
288 | // If we are creating a new stemma, populate the textarea with a |
289 | // bare digraph. |
290 | $(evt.target).dialog('option', 'title', 'Add a new stemma') |
291 | $('#dot_field').val( "digraph stemma {\n\n}" ); |
292 | } else { |
293 | // If we are editing a stemma, grab its stemmadot and populate the |
294 | // textarea with that. |
295 | $(evt.target).dialog('option', 'title', 'Edit selected stemma') |
296 | $('#dot_field').val( 'Loading, please wait...' ); |
297 | var doturl = _get_url([ "stemmadot", selectedTextID, stemmaseq ]); |
298 | $.getJSON( doturl, function (data) { |
299 | // Re-insert the line breaks |
300 | var dotstring = data.dot.replace(/\|n/gm, "\n"); |
301 | $('#dot_field').val( dotstring ); |
302 | }); |
303 | } |
304 | }, |
305 | }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { |
306 | $(event.target).parent().find('.ui-button').button("enable"); |
307 | if( ajaxSettings.url.indexOf( 'stemma' ) > -1 |
308 | && ajaxSettings.type == 'POST' ) { |
309 | display_error( jqXHR, $("#edit_stemma_status") ); |
310 | } |
311 | }); |
312 | |
313 | $('#upload-collation-dialog').dialog({ |
314 | autoOpen: false, |
315 | height: 325, |
316 | width: 480, |
317 | modal: true, |
318 | buttons: { |
319 | pick: { |
320 | text: "Pick File", |
321 | id: "pick_uploadfile_button", |
322 | click: function() {} |
323 | }, |
324 | upload: { |
325 | text: 'Upload', |
326 | id: 'upload_button', |
327 | click: function() { |
328 | $('#upload_status').empty(); |
329 | uploader.start(); |
330 | return false; |
331 | } |
332 | }, |
333 | Cancel: function() { |
334 | $('#upload-collation-dialog').dialog('close'); |
335 | } |
336 | }, |
337 | attach_uploader: function() { |
338 | create_uploader( _get_url([ "newtradition" ]) ); |
339 | $('#filelist').empty().html( 'Use the \'Pick\' button to choose a source fileā¦' ); |
340 | } |
341 | }); |
342 | |
343 | $('#stemma_graph').mousedown( function(evt) { |
344 | evt.stopPropagation(); |
345 | $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] ); |
346 | $('body').mousemove( function(evt) { |
347 | mouse_scale = 1; // for now, was: mouse_scale = svg_root_element.getScreenCTM().a; |
348 | dx = (evt.clientX - $('#stemma_graph').data( 'mousedown_xy' )[0]) / mouse_scale; |
349 | dy = (evt.clientY - $('#stemma_graph').data( 'mousedown_xy' )[1]) / mouse_scale; |
350 | $('#stemma_graph').data( 'mousedown_xy', [evt.clientX, evt.clientY] ); |
351 | var svg_root = $('#stemma_graph svg').svg().svg('get').root(); |
352 | var g = $('g.graph', svg_root).get(0); |
353 | current_translate = g.getAttribute( 'transform' ).split(/translate\(/)[1].split(')',1)[0].split(' '); |
354 | new_transform = g.getAttribute( 'transform' ).replace( /translate\([^\)]*\)/, 'translate(' + (parseFloat(current_translate[0]) + dx) + ' ' + (parseFloat(current_translate[1]) + dy) + ')' ); |
355 | g.setAttribute( 'transform', new_transform ); |
356 | evt.returnValue = false; |
357 | evt.preventDefault(); |
358 | return false; |
359 | }); |
360 | $('body').mouseup( function(evt) { |
361 | $('body').unbind('mousemove'); |
362 | $('body').unbind('mouseup'); |
363 | }); |
364 | }); |
365 | |
366 | $('#stemma_graph').mousewheel(function (event, delta) { |
367 | event.returnValue = false; |
368 | event.preventDefault(); |
369 | if (!delta || delta == null || delta == 0) delta = event.originalEvent.wheelDelta; |
370 | if (!delta || delta == null || delta == 0) delta = -1 * event.originalEvent.detail; |
371 | if( delta < -9 ) { delta = -9 }; |
372 | var z = 1 + delta/10; |
373 | z = delta > 0 ? 1 : -1; |
374 | var svg_root = $('#stemma_graph svg').svg().svg('get').root(); |
375 | var g = $('g.graph', svg_root).get(0); |
376 | if (g && ((z<1 && (g.getScreenCTM().a * start_element_height) > 4.0) || (z>=1 && (g.getScreenCTM().a * start_element_height) < 1000))) { |
377 | var scaleLevel = z/10; |
378 | current_scale = parseFloat( g.getAttribute( 'transform' ).split(/scale\(/)[1].split(')',1)[0].split(' ')[0] ); |
379 | new_transform = g.getAttribute( 'transform' ).replace( /scale\([^\)]*\)/, 'scale(' + (current_scale + scaleLevel) + ')' ); |
380 | g.setAttribute( 'transform', new_transform ); |
381 | } |
382 | }); |
383 | |
384 | }); |