+ slide = this.createSlide(file);
+ this.previewQueuePush(slide);
+ this.uploadQueuePush(slide);
+ }
+};
+
+DDFileUploader.prototype.upload = function(slide) {
+ var reader = new FileReader();
+ var req = new XMLHttpRequest();
+ var file = slide.file;
+ this.uploadedSlide = slide;
+ this.previewImg = slide.img;
+ this.progressBar = slide.progressBar;
+ var self = this;
+
+ addListener(req.upload, 'progress', function(evt){self.progressHandler(evt);});
+ addListener(req, 'readystatechange',
+ function(evt) {
+ if (req.readyState === 4) {
+ self.uploadCompleteHandler(req);
+ }
+ });
+
+ req.open("PUT", this.uploadUrl);
+ req.setRequestHeader("Content-Type", file.type);
+ req.setRequestHeader("X-File-Name", file.name);
+ addListener(reader, 'load',
+ function(evt){
+ try {
+ req.sendAsBinary(evt.target.result);
+ }
+ catch(e){}
+ });
+ reader.readAsBinaryString(file);
+};
+
+DDFileUploader.prototype.uploadCompleteHandler = function(req) {
+ var slide = this.uploadedSlide;
+ this.uploadedSlide.removeChild(slide.label);
+ this.uploadedSlide.removeChild(slide.progressBar);
+ var fragment = getCopyOfNode(req.responseXML.documentElement.firstChild);
+ var img = fragment.getElementsByTagName('img')[0];
+ img.onload = function(evt) {
+ var preview = slide.getElementsByTagName('img')[0];
+ preview.src = undefined;
+ slide.parentNode.replaceChild(fragment, slide);
+ };
+ this.previewsLoaded--;
+ this.previewQueueLoadNext();
+ this.uploadQueueLoadNext();
+};
+
+DDFileUploader.prototype.progressHandler = function(evt) {
+ if (evt.lengthComputable) {
+ var progress = evt.loaded / evt.total;
+ this.updateProgressBar(progress);
+ var currentOpacity = this.previewImg.style.opacity;
+ this.previewImg.style.opacity = Math.max(currentOpacity, progress);
+ }
+};
+
+// Method about queues
+
+DDFileUploader.prototype.previewQueuePush = function(slide) {
+ this.previewQueue.push(slide);
+ if (!this._previewQueueRunning) {
+ this.startPreviewQueue();
+ }
+};
+
+DDFileUploader.prototype.startPreviewQueue = function() {
+ this._previewQueueRunning = true;
+ this.previewQueueLoadNext();
+};
+
+DDFileUploader.prototype.previewQueueLoadNext = function() {
+ if (this.previewQueue.length && this.previewsLoaded < MAX_PREVIEW) {
+ var slide = this.previewQueue.shift();
+ this.previewUploadedImage(slide);
+ this.previewsLoaded++;
+ }
+ else {
+ this._previewQueueRunning = false;
+ }
+};
+
+DDFileUploader.prototype.uploadQueuePush = function(slide) {
+ this.uploadQueue.push(slide);
+ if (!this._uploadQueueRunning) {
+ this.startUploadQueue();
+ }
+};
+
+DDFileUploader.prototype.startUploadQueue = function() {
+ this._uploadQueueRunning = true;
+ this.uploadQueueLoadNext();
+};
+
+
+DDFileUploader.prototype.uploadQueueLoadNext = function() {
+ var slide = this.uploadQueue.shift();
+ if (slide) {
+ this.upload(slide);
+ }
+ else {
+ this._uploadQueueRunning = false;