2016-12-25 15:41:57 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Codex.Editor Transport Module
|
|
|
|
*
|
|
|
|
* @author Codex Team
|
2017-01-10 19:22:40 +01:00
|
|
|
* @version 1.0
|
2016-12-25 15:41:57 +01:00
|
|
|
*/
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
module.exports = (function (transport) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-13 18:54:18 +01:00
|
|
|
let editor = codex.editor;
|
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
transport.input = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Object} arguments - keep plugin settings and defined callbacks
|
|
|
|
*/
|
|
|
|
transport.arguments = null;
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
transport.prepare = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var input = document.createElement('INPUT');
|
|
|
|
|
|
|
|
input.type = 'file';
|
2017-02-13 18:54:18 +01:00
|
|
|
editor.listeners.add(input, 'change', editor.transport.fileSelected);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.transport.input = input;
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Clear input when files is uploaded */
|
2017-02-01 18:25:59 +01:00
|
|
|
transport.clearInput = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
/** Remove old input */
|
|
|
|
this.input = null;
|
|
|
|
|
|
|
|
/** Prepare new one */
|
|
|
|
this.prepare();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for file selection
|
2017-02-01 18:25:59 +01:00
|
|
|
* @param {Event} event
|
2016-12-07 19:25:31 +01:00
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
transport.fileSelected = function () {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var input = this,
|
|
|
|
files = input.files,
|
2017-02-01 18:25:59 +01:00
|
|
|
formdData = new FormData();
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
formdData.append('files', files[0], files[0].name);
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
editor.transport.ajax({
|
2016-12-07 19:25:31 +01:00
|
|
|
data : formdData,
|
2017-02-01 18:25:59 +01:00
|
|
|
beforeSend : editor.transport.arguments.beforeSend,
|
|
|
|
success : editor.transport.arguments.success,
|
|
|
|
error : editor.transport.arguments.error
|
2016-12-07 19:25:31 +01:00
|
|
|
});
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use plugin callbacks
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
transport.selectAndUpload = function (args) {
|
|
|
|
|
|
|
|
this.arguments = args;
|
|
|
|
this.input.click();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ajax requests module
|
2017-02-01 18:25:59 +01:00
|
|
|
* @todo use core.ajax
|
2016-12-07 19:25:31 +01:00
|
|
|
*/
|
2017-02-01 18:25:59 +01:00
|
|
|
transport.ajax = function (params) {
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest(),
|
2017-02-01 18:25:59 +01:00
|
|
|
beforeSend = typeof params.beforeSend == 'function' ? params.beforeSend : function () {},
|
|
|
|
success = typeof params.success == 'function' ? params.success : function () {},
|
|
|
|
error = typeof params.error == 'function' ? params.error : function () {};
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
beforeSend();
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
xhr.open('POST', editor.settings.uploadImagesUrl, true);
|
2016-12-07 19:25:31 +01:00
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
2016-12-07 19:25:31 +01:00
|
|
|
|
|
|
|
xhr.onload = function () {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
if (xhr.status === 200) {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
success(xhr.responseText);
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
} else {
|
2017-02-01 18:25:59 +01:00
|
|
|
|
|
|
|
editor.core.log('request error: %o', xhr);
|
2016-12-07 19:25:31 +01:00
|
|
|
error();
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
}
|
2017-02-01 18:25:59 +01:00
|
|
|
|
2016-12-07 19:25:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(params.data);
|
|
|
|
this.clearInput();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
return transport;
|
|
|
|
|
2017-02-01 18:25:59 +01:00
|
|
|
})({});
|