Skip to content

Instantly share code, notes, and snippets.

@kokizzu
Created February 29, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kokizzu/2ba2d8431a8081af9a6862467603184f to your computer and use it in GitHub Desktop.
Save kokizzu/2ba2d8431a8081af9a6862467603184f to your computer and use it in GitHub Desktop.
Global JS
function toDate( unix ) {
if( !unix ) return '';
if( unix<9876543210 ) unix *= 1000;
return (new Date( unix )).toISOString().substr( 0, 10 );
}
function toTime( unix ) {
if( !unix ) return '';
if( unix<9876543210 ) unix *= 1000;
return (new Date( unix )).toISOString().substr( 0, 19 ).replace( 'T', ' ' );
}
function currDate() {
return (new Date()).toISOString().substr( 0, 10 );
}
function currTime() {
return (new Date()).toISOString().substr( 0, 19 ).replace( 'T', ' ' );
}
function isFunc( f ) {
return 'function'== typeof (f);
}
function hasAjaxError( res ) {
if( !res ) return 'Server response is empty';
if( res.error ) return res.error;
return '';
}
function postAjax( url, data, successF, errorF, completeF ) {
console.log( 'Ajax post', url, data );
return $.ajax( {
type: "POST",
url: url,
data: data
} ).done( function( res, status, xhr ) {
console.log( 'Ajax success', url, data, res, status, xhr );
if( isFunc( successF ) ) successF( res, status );
} ).fail( function( xhr, status, err ) {
console.log( 'Ajax error', url, data, status, err, xhr );
if( isFunc( errorF ) ) errorF( xhr.status + ' ' + xhr.responseText + ': ' + err );
} ).always( function( res_xhr, status, xhr_err ) {
if( isFunc( completeF ) ) completeF( res_xhr, status, xhr_err );
} );
}
function uploadAjax( url, data, successF, errorF, completeF, progressF ) {
console.log( 'Ajax upload', url, data );
return $.ajax( {
type: "POST",
url: url,
data: data,
async: true,
processData: false,
contentType: false,
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if( !progressF ) return xhr;
console.log( xhr );
progressF( 1 );
xhr.upload.addEventListener( 'progress', function( e ) {
var prog = 1 + (e.lengthComputable) ? e.loaded * 90 / e.total : 1;
progressF( Math.round( prog * 100 ) / 100 );
} );
xhr.addEventListener( 'progress', function( e ) {
var prog = 92 + (e.lengthComputable) ? e.loaded * 8 / e.total : 1;
progressF( Math.round( prog * 100 ) / 100 );
} );
return xhr;
}
} ).done( function( res, status, xhr ) {
console.log( 'Ajax success', url, data, res, status, xhr );
if( progressF ) progressF( 100 );
if( isFunc( successF ) ) successF( res, status );
} ).fail( function( xhr, status, err ) {
console.log( 'Ajax error', url, data, status, err, xhr );
if( isFunc( errorF ) ) errorF( xhr.status + ' ' + xhr.responseText + ': ' + err );
} ).always( function( res_xhr, status, xhr_err ) {
if( isFunc( completeF ) ) completeF( res_xhr, status, xhr_err );
} );
}
@kokizzu
Copy link
Author

kokizzu commented Feb 29, 2020

usage example:

        postAjax( '/m/netizen/content_feed', {}, function( res ) {
                if( errorLoadFeed = hasAjaxError( res ) ) return;
                feeds = res.feed;
        }, function( err ) {
                errorLoadFeed = err;
        }, function() {
                tryLoadFeed = false;
        } )
        tryUploadProfPic = true;
        uploadProfpicProgress = 0;
        errorUploadProfPic = '';
        let data = new FormData();
        data.append( 'file', upload_profpic );
        data.append( 'prefix', 'profpic' );
        uploadAjax( '/u/profile/upload_image', data, function( res ) {
               if( errorUploadProfPic = hasAjaxError( res ) ) return;
               profile_pic = res.local_url;
               upload_profpic = '';
        }, function( err ) {
               errorUploadProfPic = err;
        }, function() {
               tryUploadProfPic = false;
        }, function( percent ) {
               uploadProfpicProgress = percent;
        } );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment