93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
/**************************
|
|
* PermanentStorage *
|
|
* 02-11-16 *
|
|
***************************
|
|
* Designed & Developed by *
|
|
* xdrm-brackets *
|
|
* & *
|
|
* SeekDaSky *
|
|
***************************
|
|
* https://xdrm.io/ *
|
|
* http://seekdasky.ovh/ *
|
|
**************************/
|
|
|
|
var PermanentStorage = function(){};
|
|
|
|
PermanentStorage.prototype = {
|
|
|
|
/* STORES DATA TO REMOTE STORAGE
|
|
*
|
|
* @raw_data<String> Raw data to store
|
|
* @callback<Function> Callback function that'll take the @status as result
|
|
*
|
|
* @return status<Boolean> If no error
|
|
*
|
|
* @note: @callback will be called with `this` bound to `xhr.responseText`
|
|
*
|
|
*/
|
|
store: function(raw_data, callback){
|
|
/* (0) Initialization */
|
|
var fd, xhr;
|
|
|
|
{ /* (1) Checks @raw_data argument */
|
|
raw_data = raw_data || null;
|
|
raw_data = typeof raw_data === 'string' ? raw_data : null;
|
|
|
|
if( !raw_data )
|
|
return false;
|
|
}
|
|
|
|
{ /* (2) Checks @callback argument */
|
|
if( !(callback instanceof Function) )
|
|
return false;
|
|
}
|
|
|
|
{ /* (3) Format data and wrap it into FormData */
|
|
fd = new FormData();
|
|
fd.append('data', raw_data);
|
|
}
|
|
|
|
{ /* (4) Sends data to server */
|
|
xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
|
|
|
|
xhr.addEventListener('readystatechange', function(e){
|
|
!(xhr.readyState-4) && !!~[0,200].indexOf(xhr.status) && callback.bind(xhr.responseText);
|
|
}, false);
|
|
|
|
xhr.open( 'POST', './permanent-storage/', true );
|
|
xhr.send( fd );
|
|
}
|
|
|
|
},
|
|
|
|
/* FETCHES DATA FROM REMOTE STORAGE
|
|
*
|
|
* @callback<Function> Callback function that'll take the @fetched_data as result
|
|
*
|
|
* @return raw_data<String> Fetched data
|
|
* | error<null> NULL is returned if an error occured
|
|
*
|
|
*/
|
|
fetch: function(){
|
|
/* (0) Initialization */
|
|
var xhr;
|
|
|
|
{ /* (1) Checks @callback argument */
|
|
if( !(callback instanceof Function) )
|
|
return false;
|
|
}
|
|
|
|
{ /* (2) Sends data to server */
|
|
xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
|
|
|
|
xhr.addEventListener('readystatechange', function(e){
|
|
!(xhr.readyState-4) && !!~[0,200].indexOf(xhr.status) && callback.bind(xhr.responseText);
|
|
}, false);
|
|
|
|
xhr.open( 'POST', './permanent-storage/', true );
|
|
xhr.send( null );
|
|
}
|
|
|
|
}
|
|
};
|