X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=root%2Fjs%2Fcomponentload.js;h=243d8f7ca67f34eb3aa9addfef6e0537a745f93a;hb=2ece58b34307ec3f4e45eb466ac13fa88715a275;hp=1cbc2ac05f3da3694352e19da1ec77e2e5fdb82d;hpb=9a3fb14365e95b36c0d91c70961ec89994ba3706;p=scpubgit%2Fstemmaweb.git diff --git a/root/js/componentload.js b/root/js/componentload.js index 1cbc2ac..243d8f7 100644 --- a/root/js/componentload.js +++ b/root/js/componentload.js @@ -193,19 +193,73 @@ function file_selected( e ) { } } +// Implement our own AJAX method that uses the features of XMLHttpRequest2 +// but try to let it have a similar interface to jquery.post +// The data var needs to be a FormData() object. +// The callback will be given a single argument, which is the response data +// of the given type. + +function post_xhr2( url, data, cb, type ) { + if( !type ) { + type = 'json'; + } + var xhr = new XMLHttpRequest(); + // Set the expected response type + if( type === 'data' ) { + xhr.responseType = 'blob'; + } else if( type === 'xml' ) { + xhr.responseType = 'document'; + } + // Post the form + // Gin up an AJAX settings object + $.ajaxSetup({ url: url, type: 'POST' }); + xhr.open( 'POST', url, true ); + // Handle the results + xhr.onload = function( e ) { + // Get the response and parse it + // Call the callback with the response, whatever it was + var xhrs = e.target; + if( xhrs.status > 199 && xhrs.status < 300 ) { // Success + var resp; + if( type === 'json' ) { + resp = $.parseJSON( xhrs.responseText ); + } else if ( type === 'xml' ) { + resp = xhrs.responseXML; + } else if ( type === 'text' ) { + resp = xhrs.responseText; + } else { + resp = xhrs.response; + } + cb( resp ); + } else { + // Trigger the ajaxError... + _trigger_ajaxerror( e ); + } + }; + xhr.onerror = _trigger_ajaxerror; + xhr.onabort = _trigger_ajaxerror; + xhr.send( data ); +} + +function _trigger_ajaxerror( e ) { + var xhr = e.target; + var thrown = xhr.statusText || 'Request error'; + jQuery.event.trigger( 'ajaxError', [ xhr, $.ajaxSettings, thrown ]); +} + function upload_new () { // Serialize the upload form, get the file and attach it to the request, // POST the lot and handle the response. var newfile = $('#new_file').get(0).files[0]; var reader = new FileReader(); reader.onload = function( evt ) { - var formvals = $('#new_tradition').serializeArray(); - var params = { 'file': evt.target.result, 'filename': newfile.name }; - $.each( formvals, function( i, o ) { - params[o.name] = o.value; - }); + var data = new FormData(); + $.each( $('#new_tradition').serializeArray(), function( i, o ) { + data.append( o.name, o.value ); + }); + data.append( 'file', newfile ); var upload_url = _get_url([ 'newtradition' ]); - $.post( upload_url, params, function( ret ) { + post_xhr2( upload_url, data, function( ret ) { if( ret.id ) { $('#upload-collation-dialog').dialog('close'); refreshDirectory(); @@ -214,7 +268,7 @@ function upload_new () { $('#upload_status').empty().append( $('').attr('class', 'error').append( ret.error ) ); } - }); + }, 'json' ); }; reader.onerror = function( evt ) { var err_resp = 'File read error'; @@ -392,6 +446,7 @@ $(document).ready( function() { // Set the upload button to its correct state based on // whether a file is loaded file_selected( $('#new_file').get(0) ); + $('#upload_status').empty(); } }).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) { // Reset button state