c4b798244835d823239eb7343d57048df7c414c3
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
);
48 // Methods about preview queue.
49 DDImageUploader
.prototype.previewQueuePush = function(slide
) {
50 this.previewQueue
.push(slide
);
51 if (!this._previewQueueRunning
) {
52 this.startPreviewQueue();
56 DDImageUploader
.prototype.startPreviewQueue = function() {
57 this._previewQueueRunning
= true;
58 this.previewQueueLoadNext();
61 DDImageUploader
.prototype.previewQueueLoadNext = function() {
62 if (this.previewQueue
.length
&& this.previewsLoaded
< MAX_PREVIEW
) {
63 var slide
= this.previewQueue
.shift();
64 this.previewUploadedImage(slide
);
65 this.previewsLoaded
++;
68 this._previewQueueRunning
= false;
73 DDImageUploader
.prototype.createSlide = function(file
) {
74 var slide
= document
.createElement('span');
77 var a
= document
.createElement('a');
79 a
.className
= 'slide';
81 var img
= document
.createElement('img');
82 img
.className
= 'hidden';
83 var size
= this.thumbnailSize
;
85 img
.onload = function(evt
) {
86 if (img
.width
> img
.height
) { // landscape
87 img
.height
= Math
.round(size
* img
.height
/ img
.width
);
91 img
.width
= Math
.round(size
* img
.width
/ img
.height
);
94 img
.style
.marginLeft
= Math
.floor((self
.slideSize
- img
.width
) / 2) + 'px';
95 img
.style
.marginTop
= Math
.floor((self
.slideSize
- img
.height
) / 2) + 'px';
96 img
.style
.opacity
= 0.2;
97 img
.className
= undefined;
102 var label
= document
.createElement('span');
104 label
.className
= 'label';
105 label
.innerHTML
= file
.name
;
107 var progressBar
= document
.createElement('span');
108 progressBar
.className
= 'upload-progress';
109 slide
.progressBar
= progressBar
;
111 slide
.appendChild(a
);
112 slide
.appendChild(progressBar
);
113 slide
.appendChild(label
);
114 this.dropbox
.appendChild(slide
);
119 DDImageUploader
.prototype.updateProgressBar = function(progress
) {
120 // 0 <= progress <= 1
121 var size
= this.progressBarMaxSize
* progress
;
122 size
= Math
.round(size
);
123 this.progressBar
.style
.width
= size
+ 'px';
126 DDImageUploader
.prototype.previewUploadedImage = function(slide
) {
127 var reader
= new FileReader();
128 var size
= this.thumbnailSize
;
131 reader
.onload = function(evt
) {
132 slide
.img
.src
= evt
.target
.result
;
133 setTimeout(function(){self
.previewQueueLoadNext();}, 500);
135 reader
.readAsDataURL(slide
.file
);