1 // © 2013 Benoît Pin MINES ParisTech
2 var DDFileUploaderBase
;
6 DDFileUploaderBase = function(dropbox
, uploadUrl
) {
7 this.dropbox
= dropbox
;
8 this.uploadUrl
= uploadUrl
;
10 this._uploadQueueRunning
= false;
12 addListener(dropbox
, 'dragenter', function(evt
){self
.dragenter(evt
);});
13 addListener(dropbox
, 'dragover', function(evt
){self
.dragover(evt
);});
14 addListener(dropbox
, 'drop', function(evt
){self
.drop(evt
);});
18 DDFileUploaderBase
.prototype.dragenter = function(evt
) {
20 disablePropagation(evt
);
23 DDFileUploaderBase
.prototype.dragover = function(evt
) {
25 disablePropagation(evt
);
26 evt
= getEventObject(evt
);
27 var dt
= evt
.dataTransfer
;
28 dt
.dropEffect
= 'copy';
31 DDFileUploaderBase
.prototype.drop = function(evt
) {
33 disablePropagation(evt
);
35 var dt
= evt
.dataTransfer
;
36 dt
.dropEffect
= 'copy';
37 this.handleFiles(dt
.files
);
40 // Methods about upload
41 DDFileUploaderBase
.prototype.handleFiles = function(files
) {
42 // To be implemented by descendant.
47 DDFileUploaderBase
.prototype.beforeUpload = function(item
) {
48 // To be implemented by decendant.
52 DDFileUploaderBase
.prototype.upload = function(item
) {
53 // item.file must be the file to be uploaded
54 this.beforeUpload(item
);
55 var reader
= new FileReader();
56 var req
= new XMLHttpRequest();
61 addListener(req
.upload
, 'progress', function(evt
){self
.progressHandler(evt
);});
62 addListener(req
, 'readystatechange',
64 if (req
.readyState
=== 4) {
65 self
.uploadCompleteHandler(req
);
69 req
.open("PUT", this.uploadUrl
);
70 req
.setRequestHeader("Content-Type", file
.type
);
71 req
.setRequestHeader("X-File-Name", file
.name
);
72 addListener(reader
, 'load',
75 req
.sendAsBinary(evt
.target
.result
);
79 reader
.readAsBinaryString(file
);
82 DDFileUploaderBase
.prototype.uploadCompleteHandler = function(req
) {
83 var slide
= this.uploadedSlide
;
84 this.uploadedSlide
.removeChild(slide
.label
);
85 this.uploadedSlide
.removeChild(slide
.progressBar
);
86 var fragment
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
87 var img
= fragment
.getElementsByTagName('img')[0];
88 if (req
.status
=== 200) {
90 var existing
= this.existingSlides
[img
.src
];
92 existing
.src
= existing
.src
+ '?' + Math
.random().toString();
95 slide
.img
.parentNode
.removeChild(slide
.img
);
96 slide
.img
= undefined;
97 slide
.parentNode
.removeChild(slide
);
99 else if(req
.status
=== 201) {
101 img
.onload = function(evt
) {
102 // accelerate GC before replacing
104 slide
.img
.parentNode
.removeChild(slide
.img
);
105 slide
.img
= undefined;
106 slide
.parentNode
.replaceChild(fragment
, slide
);
109 this.previewsLoaded
--;
110 this.previewQueueLoadNext();
111 this.uploadQueueLoadNext();
114 DDFileUploaderBase
.prototype.progressHandlerCB = function(progress
) {
115 // To be implemented by descendant.
116 // 0 <= progress <= 1
119 DDFileUploaderBase
.prototype.progressHandler = function(evt
) {
120 if (evt
.lengthComputable
) {
121 var progress
= evt
.loaded
/ evt
.total
;
122 this.progressHandlerCB(progress
);
126 // Methods about queue
127 DDFileUploaderBase
.prototype.uploadQueuePush = function(item
) {
128 this.uploadQueue
.push(item
);
129 if (!this._uploadQueueRunning
) {
130 this.startUploadQueue();
134 DDFileUploaderBase
.prototype.startUploadQueue = function() {
135 this._uploadQueueRunning
= true;
136 this.uploadQueueLoadNext();
139 DDFileUploaderBase
.prototype.uploadQueueLoadNext = function() {
140 var item
= this.uploadQueue
.shift();
145 this._uploadQueueRunning
= false;