1 // © 2013 Benoît Pin MINES ParisTech
6 // nombre maximun d'image chargées en local
8 var isThumbnail
= /.*\/getThumbnail$/;
10 DDImageUploader = function(dropbox
, uploadUrl
, options
) {
11 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
13 this.existingSlides
= this.indexExistingSlides();
14 this.slideSize
= options
.slideSize
|| 222; // pixels
15 this.progressBarMaxSize
= this.slideSize
- 22 || 200;
16 this.thumbnailSize
= options
.thumbnailSize
|| 180;
17 this.previewQueue
= [];
18 this._previewQueueRunning
= false;
19 this.previewsLoaded
= 0;
22 copyPrototype(DDImageUploader
, DDFileUploaderBase
);
24 DDImageUploader
.prototype.indexExistingSlides = function() {
25 var images
= this.dropbox
.getElementsByTagName('img');
28 for (i
=0 ; i
< images
.length
; i
++) {
29 if (isThumbnail
.test(images
[i
].src
)) {
30 index
[images
[i
].src
] = images
[i
]; }
35 // Methods about upload.
36 DDImageUploader
.prototype.handleFiles = function(files
) {
38 for (i
= 0; i
< files
.length
; i
++) {
40 slide
= this.createSlide(file
);
41 this.previewQueuePush(slide
);
42 this.uploadQueuePush(slide
);
46 DDImageUploader
.prototype.beforeUpload = function(slide
) {
47 this.uploadedSlide
= slide
;
48 this.previewImg
= slide
.img
;
49 this.progressBar
= slide
.progressBar
;
52 DDImageUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
53 var slide
= this.uploadedSlide
;
54 this.uploadedSlide
.removeChild(slide
.label
);
55 this.uploadedSlide
.removeChild(slide
.progressBar
);
56 var fragment
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
57 var img
= fragment
.getElementsByTagName('img')[0];
58 if (req
.status
=== 200) {
60 var existing
= this.existingSlides
[img
.src
];
62 existing
.src
= existing
.src
+ '?' + Math
.random().toString();
65 slide
.img
.parentNode
.removeChild(slide
.img
);
66 slide
.img
= undefined;
67 slide
.parentNode
.removeChild(slide
);
69 else if(req
.status
=== 201) {
71 img
.onload = function(evt
) {
72 // accelerate GC before replacing
74 slide
.img
.parentNode
.removeChild(slide
.img
);
75 slide
.img
= undefined;
76 slide
.parentNode
.replaceChild(fragment
, slide
);
79 this.previewsLoaded
--;
80 this.previewQueueLoadNext();
83 DDImageUploader
.prototype.progressHandlerCB = function(progress
) {
84 this.updateProgressBar(progress
);
85 var currentOpacity
= this.previewImg
.style
.opacity
;
86 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
89 // Methods about preview queue.
90 DDImageUploader
.prototype.previewQueuePush = function(slide
) {
91 this.previewQueue
.push(slide
);
92 if (!this._previewQueueRunning
) {
93 this.startPreviewQueue();
97 DDImageUploader
.prototype.startPreviewQueue = function() {
98 this._previewQueueRunning
= true;
99 this.previewQueueLoadNext();
102 DDImageUploader
.prototype.previewQueueLoadNext = function() {
103 if (this.previewQueue
.length
&& this.previewsLoaded
< MAX_PREVIEW
) {
104 var slide
= this.previewQueue
.shift();
105 this.previewUploadedImage(slide
);
106 this.previewsLoaded
++;
109 this._previewQueueRunning
= false;
114 DDImageUploader
.prototype.createSlide = function(file
) {
115 var slide
= document
.createElement('span');
118 var a
= document
.createElement('a');
120 a
.className
= 'slide';
122 var img
= document
.createElement('img');
123 img
.className
= 'hidden';
124 var size
= this.thumbnailSize
;
126 img
.onload = function(evt
) {
127 if (img
.width
> img
.height
) { // landscape
128 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
132 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
135 img
.style
.marginLeft
= Math
.floor((self
.slideSize
- img
.width
) / 2) + 'px';
136 img
.style
.marginTop
= Math
.floor((self
.slideSize
- img
.height
) / 2) + 'px';
137 img
.style
.opacity
= 0.2;
138 img
.className
= undefined;
143 var label
= document
.createElement('span');
145 label
.className
= 'label';
146 label
.innerHTML
= file
.name
;
148 var progressBar
= document
.createElement('span');
149 progressBar
.className
= 'upload-progress';
150 slide
.progressBar
= progressBar
;
152 slide
.appendChild(a
);
153 slide
.appendChild(progressBar
);
154 slide
.appendChild(label
);
155 this.dropbox
.appendChild(slide
);
160 DDImageUploader
.prototype.updateProgressBar = function(progress
) {
161 // 0 <= progress <= 1
162 var size
= this.progressBarMaxSize
* progress
;
163 size
= Math
.round(size
);
164 this.progressBar
.style
.width
= size
+ 'px';
167 DDImageUploader
.prototype.previewUploadedImage = function(slide
) {
168 var reader
= new FileReader();
169 var size
= this.thumbnailSize
;
172 reader
.onload = function(evt
) {
173 slide
.img
.src
= evt
.target
.result
;
174 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
176 reader
.readAsDataURL(slide
.file
);