diff --git a/lib/client/cloudcmd.js b/lib/client/cloudcmd.js index 5fd745ba..41b4aba0 100644 --- a/lib/client/cloudcmd.js +++ b/lib/client/cloudcmd.js @@ -33,6 +33,7 @@ var CloudCmd; 'promise-polyfill/Promise', 'format-io/lib/format', 'rendy/lib/rendy', + 'emitify/lib/emitify' ].map(function(name) { return modules + name; }); diff --git a/lib/client/directory.js b/lib/client/directory.js index 3bafcab6..baf424af 100644 --- a/lib/client/directory.js +++ b/lib/client/directory.js @@ -1,7 +1,5 @@ /* global CloudCmd */ /* global DOM */ -/* global CloudFunc */ -/* global Emitify */ (function() { 'use strict'; @@ -58,14 +56,16 @@ switch(type) { case 'file': - upload = uploadFile(full, data, callback); + upload = uploadFile(full, data); break; case 'directory': - upload = uploadDir(full, callback); + upload = uploadDir(full); break; } + upload.on('end', callback); + upload.on('progress', function(count) { var current = percent(i, n), next = percent(i + 1, n), @@ -110,52 +110,11 @@ } function uploadFile(url, data, callback) { - return upload(url, data, callback); + return DOM.load.put(url, data, callback); } function uploadDir(url, callback) { - return upload(url + '?dir', null, callback); - } - - function upload(url, body, callback) { - var emitter = Emitify(), - prefix = CloudCmd.PREFIX, - apiURL = prefix + CloudFunc.apiURL, - api = apiURL + '/fs', - xhr = new XMLHttpRequest(); - - url = encodeURI(url); - url = url.replace('#', '%23'); - - xhr.open('put', api + url, true); - - xhr.upload.onprogress = function(event) { - var percent, count; - - if (event.lengthComputable) { - percent = (event.loaded / event.total) * 100; - count = Math.round(percent); - - emitter.emit('progress', count); - } - - }; - xhr.onreadystatechange = function() { - var error, - over = xhr.readyState === xhr.DONE, - OK = 200; - - if (over) { - if (xhr.status !== OK) - error = Error(xhr.responseText); - - callback(error); - } - }; - - xhr.send(body); - - return emitter; + return DOM.load.put(url + '?dir', null, callback); } })(); diff --git a/lib/client/dom.js b/lib/client/dom.js index acefa659..8102e928 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -389,7 +389,11 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; }; this.uploadFiles = function(dir, files) { - var func = function(name) { + var array, + slice = [].slice, + i = 0, + n = 0, + func = function(name) { return function() { CloudCmd.refresh(null, function() { var current = DOM.getCurrentByName(name); @@ -397,15 +401,41 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; }); }; }, + percent = function(i, n, per) { + var value; + + if (!per) + per = 100; + + value = Math.round(i * per / n); + + return value; + }, + + step = function(n) { + return 100 / n; + }, load = function(file, callback) { - var Images = DOM.Images, + var uploader, + Images = DOM.Images, name = file.name, path = dir + name; - Images.show.load('top'); - Images.setProgress(0, name); + ++i; - DOM.RESTful.write(path, file, callback); + uploader = DOM.load.put(path, file, callback); + uploader.on('progress', function(count) { + var max = step(n), + value = i * percent(count, 100, max); + + Images.show.load('top'); + Images.setProgress(value); + + if (i === n) + Images.setProgress(100); + }); + + uploader.on('end', callback); }; if (!files) { @@ -413,14 +443,11 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; dir = CurrentInfo.dirPath; } - if (files.length) { - func = func(files[0].name); - - [].forEach.call(files, function(file) { - func = Util.exec.with(load, file, func); - }); - - func(); + n = files.length; + array = slice.call(files); + + if (n) { + Util.exec.eachSeries(array, load, func(files[0].name)); } }; diff --git a/lib/client/load.js b/lib/client/load.js index d63e09d2..91e6771a 100644 --- a/lib/client/load.js +++ b/lib/client/load.js @@ -1,4 +1,9 @@ -var Util, DOM; +/* global Util */ +/* global DOM */ +/* global CloudFunc */ +/* global CloudCmd */ +/* global Emitify */ +/* global async */ (function (Util, DOM) { 'use strict'; @@ -162,7 +167,7 @@ var Util, DOM; */ load.ajax = function(params) { var data, - p = params, countProgress, + p = params, isObject = Util.type.object(p.data), isArray = Util.type.array(p.data), isArrayBuf = Util.type(p.data) === 'arraybuffer', @@ -185,21 +190,6 @@ var Util, DOM; else data = p.data; - Events.add('progress', xhr.upload, function(event) { - var percent, count; - - if (event.lengthComputable) { - percent = (event.loaded / event.total) * 100; - count = Math.round(percent); - - if (countProgress) - Images.setProgress(count); - - countProgress = true; - } - - }); - Events.add('readystatechange', xhr, function(event) { var TYPE_JSON, type, data, isContain, notText, xhr = event.target, @@ -228,6 +218,48 @@ var Util, DOM; xhr.send(data); }; + load.put = function(url, body) { + var emitter = Emitify(), + prefix = CloudCmd.PREFIX, + apiURL = prefix + CloudFunc.apiURL, + api = apiURL + '/fs', + xhr = new XMLHttpRequest(); + + url = encodeURI(url); + url = url.replace('#', '%23'); + + xhr.open('put', api + url, true); + + xhr.upload.onprogress = function(event) { + var percent, count; + + if (event.lengthComputable) { + percent = (event.loaded / event.total) * 100; + count = Math.round(percent); + + emitter.emit('progress', count); + } + + }; + xhr.onreadystatechange = function() { + var error, + over = xhr.readyState === xhr.DONE, + OK = 200; + + if (over) + if (xhr.status === OK) { + emitter.emit('end'); + } else { + error = Error(xhr.responseText); + emitter.emit('error'); + } + }; + + xhr.send(body); + + return emitter; + }; + load.ext = function(src, func) { var element, ext = Util.getExt(src);