editor.js/modules/transport.js
khaydarov c3ee7560f1 cover restoring, versioning, sanitize and new initialization structure (#105)
* cover restoring fixed

* upd

* fetch fixed in safari

* updated

* plugins

* plugins ready

* code improved

* fixed bug with backspace

* improved architecture

* Versioning (#102)

* start versioning

* codex.version

* eslint settings

* versioning improved

* cover restoring and fetch function fixed (#101)

* cover restoring fixed

* upd

* fetch fixed in safari

* updated

* plugins

* plugins ready

* code improved

* fixed bug with backspace

* improved architecture

* new sanitize method (#103)

* new sanitize method

Need to fix caret position

* removed console logs

* version updated

* eslint style

* caret position

* big fixed on sanitize method

* sanitize improved, using observers

* sanitize: using html-janitor

* fixes

* last fixes, code improved after review

* updated

* new bundle

* webpack config improved

* upd

* upd

* upd

* upd

* clear from conflicts

* upd

* upd
2017-01-10 21:22:40 +03:00

106 lines
2.4 KiB
JavaScript

/**
*
* Codex.Editor Transport Module
*
* @author Codex Team
* @version 1.0
*/
var transport = (function(transport){
transport.input = null;
/**
* @property {Object} arguments - keep plugin settings and defined callbacks
*/
transport.arguments = null;
transport.prepare = function(){
var input = document.createElement('INPUT');
input.type = 'file';
input.addEventListener('change', codex.transport.fileSelected);
codex.transport.input = input;
};
/** Clear input when files is uploaded */
transport.clearInput = function() {
/** Remove old input */
this.input = null;
/** Prepare new one */
this.prepare();
};
/**
* Callback for file selection
*/
transport.fileSelected = function(event){
var input = this,
files = input.files,
filesLength = files.length,
formdData = new FormData(),
file,
i;
formdData.append('files', files[0], files[0].name);
codex.transport.ajax({
data : formdData,
beforeSend : codex.transport.arguments.beforeSend,
success : codex.transport.arguments.success,
error : codex.transport.arguments.error
});
};
/**
* Use plugin callbacks
* @protected
*/
transport.selectAndUpload = function (args) {
this.arguments = args;
this.input.click();
};
/**
* Ajax requests module
*/
transport.ajax = function(params){
var xhr = new XMLHttpRequest(),
beforeSend = typeof params.beforeSend == 'function' ? params.beforeSend : function(){},
success = typeof params.success == 'function' ? params.success : function(){},
error = typeof params.error == 'function' ? params.error : function(){};
beforeSend();
xhr.open('POST', codex.settings.uploadImagesUrl, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.onload = function () {
if (xhr.status === 200) {
success(xhr.responseText);
} else {
console.log("request error: %o", xhr);
error();
}
};
xhr.send(params.data);
this.clearInput();
};
return transport;
})({});
module.exports = transport;