c9c492a8b876eae897c48ec5905fab342f9b557b
1 // © 2013 Benoît Pin MINES ParisTech
4 var MAX_PREVIEW
= 2; // à virer
7 // nombre maximun d'image chargées en local
9 var isThumbnail
= /.*\/getThumbnail$/;
11 DDImageUploader = function(dropbox
, uploadUrl
) {
12 DDFileUploaderBase
.apply(this, [dropbox
, uploadUrl
]);
14 this.existingSlides
= this.indexExistingSlides();
16 this.progressBarMaxSize
= 200; // pixels
17 this.thumbnailSize
= 180;
18 this.previewQueue
= [];
19 this._previewQueueRunning
= false;
20 this.previewsLoaded
= 0;
23 copyPrototype(DDImageUploader
, DDFileUploaderBase
);
25 DDImageUploader
.prototype.indexExistingSlides = function() {
26 var images
= this.dropbox
.getElementsByTagName('img');
29 for (i
=0 ; i
< images
.length
; i
++) {
30 if (isThumbnail
.test(images
[i
].src
)) {
31 index
[images
[i
].src
] = images
[i
]; }
36 // Methods about upload.
37 DDImageUploader
.prototype.handleFiles = function(files
) {
39 for (i
= 0; i
< files
.length
; i
++) {
41 slide
= this.createSlide(file
);
42 this.previewQueuePush(slide
);
43 this.uploadQueuePush(slide
);
47 DDImageUploader
.prototype.beforeUpload = function(slide
) {
48 this.uploadedSlide
= slide
;
49 this.previewImg
= slide
.img
;
50 this.progressBar
= slide
.progressBar
;
53 DDImageUploader
.prototype.progressHandlerCB = function(progress
) {
54 this.updateProgressBar(progress
);
55 var currentOpacity
= this.previewImg
.style
.opacity
;
56 this.previewImg
.style
.opacity
= Math
.max(currentOpacity
, progress
);
59 // Methods about preview queue.
60 DDImageUploader
.prototype.previewQueuePush = function(slide
) {
61 this.previewQueue
.push(slide
);
62 if (!this._previewQueueRunning
) {
63 this.startPreviewQueue();
67 DDImageUploader
.prototype.startPreviewQueue = function() {
68 this._previewQueueRunning
= true;
69 this.previewQueueLoadNext();
72 DDImageUploader
.prototype.previewQueueLoadNext = function() {
73 if (this.previewQueue
.length
&& this.previewsLoaded
< MAX_PREVIEW
) {
74 var slide
= this.previewQueue
.shift();
75 this.previewUploadedImage(slide
);
76 this.previewsLoaded
++;
79 this._previewQueueRunning
= false;
84 DDImageUploader
.prototype.createSlide = function(file
) {
85 var slide
= document
.createElement('span');
88 var a
= document
.createElement('a');
90 a
.className
= 'slide';
92 var img
= document
.createElement('img');
93 img
.className
= 'hidden';
94 var size
= this.thumbnailSize
;
96 img
.onload = function(evt
) {
97 if (img
.width
> img
.height
) { // landscape
98 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
102 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
105 img
.style
.marginLeft
= Math
.floor((self
.slideSize
- img
.width
) / 2) + 'px';
106 img
.style
.marginTop
= Math
.floor((self
.slideSize
- img
.height
) / 2) + 'px';
107 img
.style
.opacity
= 0.2;
108 img
.className
= undefined;
113 var label
= document
.createElement('span');
115 label
.className
= 'label';
116 label
.innerHTML
= file
.name
;
118 var progressBar
= document
.createElement('span');
119 progressBar
.className
= 'upload-progress';
120 slide
.progressBar
= progressBar
;
122 slide
.appendChild(a
);
123 slide
.appendChild(progressBar
);
124 slide
.appendChild(label
);
125 this.dropbox
.appendChild(slide
);
130 DDImageUploader
.prototype.updateProgressBar = function(progress
) {
131 // 0 <= progress <= 1
132 var size
= this.progressBarMaxSize
* progress
;
133 size
= Math
.round(size
);
134 this.progressBar
.style
.width
= size
+ 'px';
137 DDImageUploader
.prototype.previewUploadedImage = function(slide
) {
138 var reader
= new FileReader();
139 var size
= this.thumbnailSize
;
142 reader
.onload = function(evt
) {
143 slide
.img
.src
= evt
.target
.result
;
144 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
146 reader
.readAsDataURL(slide
.file
);