fix logic bug for admin textinfo edit
[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;
57e3a008 38 $('#run_stexaminer').show();
65a0c9c6 39 } else {
57e3a008 40 selectedStemmaID = -1;
65a0c9c6 41 $('#open_stemma_edit').hide();
42 $('#run_stexaminer').hide();
65a0c9c6 43 }
57e3a008 44 load_stemma( selectedStemmaID, basepath );
98a45925 45 // Set up the relationship mapper button
46 $('#run_relater').attr( 'action', basepath + "/relation/" + textid );
47 });
48}
49
75354c3a 50function load_textinfo() {
51 $('#textinfo_waitbox').hide();
52 $('#textinfo_load_status').empty();
53 $('#textinfo_container').show();
54 $('.texttitle').empty().append( selectedTextInfo.name );
55 // Witnesses
56 $('#witness_num').empty().append( selectedTextInfo.witnesses.size );
57 $('#witness_list').empty().append( selectedTextInfo.witnesses.join( ', ' ) );
58 // Who the owner is
59 $('#owner_id').empty().append('no one');
60 if( selectedTextInfo.owner ) {
61 $('#owner_id').empty().append( selectedTextInfo.owner );
62 }
63 // Whether or not it is public
64 $('#not_public').empty();
65 if( selectedTextInfo['public'] == false ) {
66 $('#not_public').append('NOT ');
67 }
68 // What language setting it has, if any
69 $('#marked_language').empty().append('no language set');
70 if( selectedTextInfo.language && selectedTextInfo.language != 'Default' ) {
71 $('#marked_language').empty().append( selectedTextInfo.language );
72 }
73}
74
65a0c9c6 75function show_stemmapager () {
bf81fb57 76 $('.pager_left_button').unbind('click').addClass( 'greyed_out' );
77 $('.pager_right_button').unbind('click').addClass( 'greyed_out' );
65a0c9c6 78 if( selectedStemmaID > 0 ) {
79 $('.pager_left_button').click( function () {
80 load_stemma( selectedStemmaID - 1 );
bf81fb57 81 }).removeClass( 'greyed_out' );
65a0c9c6 82 }
83 if( selectedStemmaID + 1 < stemmata.length ) {
84 $('.pager_right_button').click( function () {
85 load_stemma( selectedStemmaID + 1 );
bf81fb57 86 }).removeClass( 'greyed_out' );
65a0c9c6 87 }
88}
89
90
75354c3a 91function load_stemma( idx ) {
65a0c9c6 92 // Load the stemma at idx
93 selectedStemmaID = idx;
57e3a008 94 show_stemmapager();
98a45925 95 if( idx > -1 ) {
98a45925 96 $('#stemma_graph').empty();
97 $('#stemma_graph').append( stemmata[idx] );
98 // Stexaminer submit action
99 var stexpath = basepath + "/stexaminer/" + selectedTextID + "/" + idx;
100 $('#run_stexaminer').attr( 'action', stexpath );
40803b80 101 setTimeout( 'start_element_height = $("#stemma_graph .node")[0].getBBox().height;', 500 );
98a45925 102 }
5ba6c2b4 103}
75354c3a 104
105function display_error( jqXHR, el ) {
ce1c5863 106 var errmsg;
107 if( jqXHR.responseText == "" ) {
108 errmsg = "perhaps the server went down?"
75354c3a 109 } else {
ce1c5863 110 var errobj;
111 try {
112 errobj = jQuery.parseJSON( jqXHR.responseText );
113 errmsg = errobj.error;
114 } catch ( parse_err ) {
115 errmsg = "something went wrong on the server."
116 }
75354c3a 117 }
ce1c5863 118 var msghtml = $('<span>').attr('class', 'error').text( "An error occurred: " + errmsg );
75354c3a 119 $(el).empty().append( msghtml ).show();
120}