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
;
50 this.scrollToSlide(slide
);
53 DDImageUploader
.prototype.scrollToSlide = function(slide
) {
54 var slideHeight
= slide
.offsetHeight
;
55 var slideOffsetTop
= slide
.getElementsByClassName('slide')[0].offsetTop
;
56 var to
= slideOffsetTop
- getWindowHeight() + slideHeight
;
60 DDImageUploader
.prototype.uploadCompleteHandlerCB = function(req
) {
61 var slide
= this.uploadedSlide
;
62 this.uploadedSlide
.removeChild(slide
.label
);
63 this.uploadedSlide
.removeChild(slide
.progressBar
);
64 var fragment
= getCopyOfNode(req
.responseXML
.documentElement
.firstChild
);
65 var img
= fragment
.getElementsByTagName('img')[0];
66 if (req
.status
=== 200) {
68 var existing
= this.existingSlides
[img
.src
];
70 existing
.src
= existing
.src
+ '?' + Math
.random().toString();
73 slide
.img
.parentNode
.removeChild(slide
.img
);
74 slide
.img
= undefined;
75 slide
.parentNode
.removeChild(slide
);
77 else if(req
.status
=== 201) {
79 img
.onload = function(evt
) {
80 // accelerate GC before replacing
82 slide
.img
.parentNode
.removeChild(slide
.img
);
83 slide
.img
= undefined;
84 slide
.parentNode
.replaceChild(fragment
, slide
);
87 this.previewsLoaded
--;
88 this.previewQueueLoadNext();
91 DDImageUploader
.prototype.progressHandlerCB = function(progress
) {
92 this.updateProgressBar(progress
);
93 var currentOpacity
= this.previewImg
.style
.opacity
;
94 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
97 // Methods about preview queue.
98 DDImageUploader
.prototype.previewQueuePush = function(slide
) {
99 this.previewQueue
.push(slide
);
100 if (!this._previewQueueRunning
) {
101 this.startPreviewQueue();
105 DDImageUploader
.prototype.startPreviewQueue = function() {
106 this._previewQueueRunning
= true;
107 this.previewQueueLoadNext();
110 DDImageUploader
.prototype.previewQueueLoadNext = function() {
111 if (this.previewQueue
.length
&& this.previewsLoaded
< MAX_PREVIEW
) {
112 var slide
= this.previewQueue
.shift();
113 this.previewUploadedImage(slide
);
114 this.previewsLoaded
++;
117 this._previewQueueRunning
= false;
122 DDImageUploader
.prototype.createSlide = function(file
) {
123 var slide
= document
.createElement('span');
126 var a
= document
.createElement('a');
128 a
.className
= 'slide';
130 var img
= document
.createElement('img');
131 img
.className
= 'hidden';
132 var size
= this.thumbnailSize
;
134 img
.onload = function(evt
) {
135 if (img
.width
> img
.height
) { // landscape
136 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
140 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
143 img
.style
.marginLeft
= Math
.floor((self
.slideSize
- img
.width
) / 2) + 'px';
144 img
.style
.marginTop
= Math
.floor((self
.slideSize
- img
.height
) / 2) + 'px';
145 img
.style
.opacity
= 0.2;
146 img
.className
= undefined;
151 var label
= document
.createElement('span');
153 label
.className
= 'label';
154 label
.innerHTML
= file
.name
;
156 var progressBar
= document
.createElement('span');
157 progressBar
.className
= 'upload-progress';
158 slide
.progressBar
= progressBar
;
160 slide
.appendChild(a
);
161 slide
.appendChild(progressBar
);
162 slide
.appendChild(label
);
163 this.dropbox
.appendChild(slide
);
168 DDImageUploader
.prototype.updateProgressBar = function(progress
) {
169 // 0 <= progress <= 1
170 var size
= this.progressBarMaxSize
* progress
;
171 size
= Math
.round(size
);
172 this.progressBar
.style
.width
= size
+ 'px';
175 DDImageUploader
.prototype.previewUploadedImage = function(slide
) {
176 var reader
= new FileReader();
177 var size
= this.thumbnailSize
;
180 reader
.onload = function(evt
) {
181 slide
.img
.src
= evt
.target
.result
;
182 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
184 reader
.readAsDataURL(slide
.file
);