hook up pager buttons to stemma loading; zap occasional error in upload success function
[scpubgit/stemmaweb.git] / root / js / componentload.js
CommitLineData
98a45925 1function loadTradition( textid, textname, editable ) {
2 selectedTextID = textid;
3 // First insert the placeholder image and register an error handler
04469f3e 4 $('#textinfo_load_status').empty();
5f0eda3f 5 $('#stemma_graph').empty();
98a45925 6 $('#textinfo_waitbox').show();
75354c3a 7 $('#textinfo_container').hide().ajaxError(
8 function(event, jqXHR, ajaxSettings, thrownError) {
9 if( ajaxSettings.url.indexOf( 'textinfo' ) > -1 && ajaxSettings.type == 'GET' ) {
10 $('#textinfo_waitbox').hide();
11 $('#textinfo_container').show();
12 display_error( jqXHR, $("#textinfo_load_status") );
98a45925 13 }
75354c3a 14 });
15
16 // Hide the functionality that is irrelevant
17 if( editable ) {
ce1c5863 18 $('#open_stemma_add').show();
19 $('#open_stemma_edit').show();
20 $('#open_textinfo_edit').show();
75354c3a 21 } else {
ce1c5863 22 $('#open_stemma_add').hide();
23 $('#open_stemma_edit').hide();
24 $('#open_textinfo_edit').hide();
75354c3a 25 }
26
62723740 27 // Then get and load the actual content.
98a45925 28 // TODO: scale #stemma_graph both horizontally and vertically
f6a8db89 29 // TODO: load svgs from SVG.Jquery (to make scaling react in Safari)
98a45925 30 $.getJSON( basepath + "/textinfo/" + textid, function (textdata) {
31 // Add the scalar data
75354c3a 32 selectedTextInfo = textdata;
33 load_textinfo();
34 // Add the stemma(ta) and set up the stexaminer button
98a45925 35 stemmata = textdata.stemmata;
36 if( stemmata.length ) {
37 selectedStemmaID = 0;
38 load_stemma( selectedStemmaID, basepath );
65a0c9c6 39 } else {
40 $('#open_stemma_edit').hide();
41 $('#run_stexaminer').hide();
42 show_stemmapager(); // to grey out both buttons
43 }
98a45925 44 // Set up the relationship mapper button
45 $('#run_relater').attr( 'action', basepath + "/relation/" + textid );
46 });
47}
48
75354c3a 49function load_textinfo() {
50 $('#textinfo_waitbox').hide();
51 $('#textinfo_load_status').empty();
52 $('#textinfo_container').show();
53 $('.texttitle').empty().append( selectedTextInfo.name );
54 // Witnesses
55 $('#witness_num').empty().append( selectedTextInfo.witnesses.size );
56 $('#witness_list').empty().append( selectedTextInfo.witnesses.join( ', ' ) );
57 // Who the owner is
58 $('#owner_id').empty().append('no one');
59 if( selectedTextInfo.owner ) {
60 $('#owner_id').empty().append( selectedTextInfo.owner );
61 }
62 // Whether or not it is public
63 $('#not_public').empty();
64 if( selectedTextInfo['public'] == false ) {
65 $('#not_public').append('NOT ');
66 }
67 // What language setting it has, if any
68 $('#marked_language').empty().append('no language set');
69 if( selectedTextInfo.language && selectedTextInfo.language != 'Default' ) {
70 $('#marked_language').empty().append( selectedTextInfo.language );
71 }
72}
73
65a0c9c6 74function show_stemmapager () {
75 $('.pager_left_button').unbind('click').css( 'opacity', '0.5' );
76 $('.pager_right_button').unbind('click').css( 'opacity', '0.5' );
77 if( selectedStemmaID > 0 ) {
78 $('.pager_left_button').click( function () {
79 load_stemma( selectedStemmaID - 1 );
80 }).css( 'opacity' , '1.0' );
81 }
82 if( selectedStemmaID + 1 < stemmata.length ) {
83 $('.pager_right_button').click( function () {
84 load_stemma( selectedStemmaID + 1 );
85 }).css( 'opacity' , '1.0' );
86 }
87}
88
89
75354c3a 90function load_stemma( idx ) {
65a0c9c6 91 // Load the stemma at idx
92 selectedStemmaID = idx;
98a45925 93 if( idx > -1 ) {
98a45925 94 $('#stemma_graph').empty();
95 $('#stemma_graph').append( stemmata[idx] );
65a0c9c6 96 show_stemmapager();
98a45925 97 // Stexaminer submit action
98 var stexpath = basepath + "/stexaminer/" + selectedTextID + "/" + idx;
99 $('#run_stexaminer').attr( 'action', stexpath );
40803b80 100 setTimeout( 'start_element_height = $("#stemma_graph .node")[0].getBBox().height;', 500 );
98a45925 101 }
5ba6c2b4 102}
75354c3a 103
104function display_error( jqXHR, el ) {
ce1c5863 105 var errmsg;
106 if( jqXHR.responseText == "" ) {
107 errmsg = "perhaps the server went down?"
75354c3a 108 } else {
ce1c5863 109 var errobj;
110 try {
111 errobj = jQuery.parseJSON( jqXHR.responseText );
112 errmsg = errobj.error;
113 } catch ( parse_err ) {
114 errmsg = "something went wrong on the server."
115 }
75354c3a 116 }
ce1c5863 117 var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg );
75354c3a 118 $(el).empty().append( msghtml ).show();
119}