From aeb36f4241187d74818d889d69ec1d15349c0cd3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 14 Apr 2014 09:06:44 -0400 Subject: [PATCH] feature(listeners) load files directly to server, without reading them --- lib/client/dom.js | 10 ++++++---- lib/client/listeners.js | 38 +++++++++++++++++++++----------------- lib/client/rest.js | 5 +++-- lib/util.js | 15 +++++++++++++++ 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/lib/client/dom.js b/lib/client/dom.js index ae018b44..dfa1b93d 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -38,9 +38,10 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; var classes, isSVG, name = 'loading', id = ID + name, - element = Images[id]; + element = Images[id], + parent = element && element.parentElement; - if (!element) { + if (!element || !parent) { classes = Classes + ' ' + name; isSVG = DOM.isSVG(); @@ -61,9 +62,10 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; var classes, name = 'error', id = ID + name, - element = Images[id]; + element = Images[id], + parent = element && element.parentElement; - if (!element) { + if (!element || !parent) { classes = Classes + ' ' + name; Images[id] = diff --git a/lib/client/listeners.js b/lib/client/listeners.js index 9c5dee49..65cf43a6 100644 --- a/lib/client/listeners.js +++ b/lib/client/listeners.js @@ -303,34 +303,38 @@ var Util, DOM, CloudCmd; DOM.toggleClass(panels[i], 'selected-panel'); }, onDrop = function (event) { - var reader, file, files, + var file, files, + func = CloudCmd.refresh, dir = Info.dirPath, - load = function(file, event) { + load = function(file, callback) { var Images = DOM.Images, + isFunc = Util.isFunction(file), name = file.name, - path = dir + name, - data = event.target.result; + path = dir + name; - Images.showLoad({top: true}); - Images.setProgress(0, name); - - DOM.RESTful.write(path, data, CloudCmd.refresh); + if (isFunc) { + callback = file; + Util.exec(callback); + } else { + Images.showLoad({top: true}); + Images.setProgress(0, name); + + DOM.RESTful.write(path, file, callback); + } }; preventDefault(event); toggle(); - files = event.dataTransfer.files; - - if (files.length) { - n = files.length; - + files = event.dataTransfer.files; + n = files.length; + if (n) { for (i = 0; i < n; i++) { - reader = new FileReader(); - file = files[i]; - Events.add('load', Util.bind(load, file), reader); - reader.readAsArrayBuffer(file); + file = files[i]; + func = Util.bind(load, file, func); } + + func(); } }; diff --git a/lib/client/rest.js b/lib/client/rest.js index 2a09f38d..fe8d32bc 100644 --- a/lib/client/rest.js +++ b/lib/client/rest.js @@ -121,12 +121,13 @@ var Util, DOM, CloudFunc, CloudCmd; CloudCmd.getConfig(function(config) { var data, isString = Util.isString(p.data), - isArrayBuffer = Util.isArrayBuffer(p.data); + isArrayBuffer = Util.isArrayBuffer(p.data), + isFile = Util.isFile(p.data); if (Util.isString(p.url)) p.url = decodeURI(p.url); - if (p.data && !isString && !isArrayBuffer) + if (p.data && !isString && !isArrayBuffer && !isFile) data = Util.stringifyJSON(p.data); else data = p.data; diff --git a/lib/util.js b/lib/util.js index c31da62f..b3a711db 100644 --- a/lib/util.js +++ b/lib/util.js @@ -687,6 +687,21 @@ return Util.isType(variable, 'undefined'); }; + /** + * functions check is variable is File + * @param variable + */ + this.isFile = function(variable) { + var FILE = '[object File]', + name, is; + + name = Util.execIfExist(variable, 'toString'); + + is = name === FILE; + + return is; + }; + /** * functions check is variable is pType * @param variable