Ajout script d'installation.
[Plinn.git] / Products / Plinn / skins / ajax_scripts / fileupload.js
1 // © 2013 Benoît Pin MINES ParisTech
2 var DDFileUploaderBase;
3
4 (function(){
5
6 DDFileUploaderBase = function(dropbox, uploadUrl) {
7 this.dropbox = dropbox;
8 this.uploadUrl = uploadUrl;
9 this.uploadQueue = [];
10 this._uploadQueueRunning = false;
11 var self = this;
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);});
15 };
16
17 // Drag and drop
18 DDFileUploaderBase.prototype.dragenter = function(evt) {
19 disableDefault(evt);
20 disablePropagation(evt);
21 };
22
23 DDFileUploaderBase.prototype.dragover = function(evt) {
24 disableDefault(evt);
25 disablePropagation(evt);
26 evt = getEventObject(evt);
27 var dt = evt.dataTransfer;
28 dt.dropEffect = 'copy';
29 };
30
31 DDFileUploaderBase.prototype.drop = function(evt) {
32 disableDefault(evt);
33 disablePropagation(evt);
34 getEventObject(evt);
35 var dt = evt.dataTransfer;
36 dt.dropEffect = 'copy';
37 this.handleFiles(dt.files);
38 };
39
40 // Methods about upload
41 DDFileUploaderBase.prototype.handleFiles = function(files) {
42 // To be implemented by descendant.
43 };
44
45
46
47 DDFileUploaderBase.prototype.beforeUpload = function(item) {
48 // To be implemented by decendant.
49 };
50
51
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();
57 var file = item.file;
58
59 var self = this;
60
61 addListener(req.upload, 'progress', function(evt){self.progressHandler(evt);});
62 addListener(req, 'readystatechange',
63 function(evt) {
64 if (req.readyState === 4) {
65 self.uploadCompleteHandler(req);
66 }
67 });
68
69 req.open("PUT", this.uploadUrl);
70 req.setRequestHeader("Content-Type", file.type);
71 req.setRequestHeader("X-File-Name", encodeURI(file.name));
72 addListener(reader, 'load',
73 function(evt){
74 try {
75 req.sendAsBinary(evt.target.result);
76 }
77 catch(e){}
78 });
79 reader.readAsBinaryString(file);
80 };
81
82
83 DDFileUploaderBase.prototype.uploadCompleteHandlerCB = function(req) {
84 // To be implemented by descendant.
85 };
86
87 DDFileUploaderBase.prototype.uploadCompleteHandler = function(req) {
88 this.uploadCompleteHandlerCB(req);
89 this.uploadQueueLoadNext();
90 };
91
92 DDFileUploaderBase.prototype.progressHandlerCB = function(progress) {
93 // To be implemented by descendant.
94 // 0 <= progress <= 1
95 };
96
97 DDFileUploaderBase.prototype.progressHandler = function(evt) {
98 if (evt.lengthComputable) {
99 var progress = evt.loaded / evt.total;
100 this.progressHandlerCB(progress);
101 }
102 };
103
104 // Methods about queue
105 DDFileUploaderBase.prototype.uploadQueuePush = function(item) {
106 this.uploadQueue.push(item);
107 if (!this._uploadQueueRunning) {
108 this.startUploadQueue();
109 }
110 };
111
112 DDFileUploaderBase.prototype.startUploadQueue = function() {
113 this._uploadQueueRunning = true;
114 this.uploadQueueLoadNext();
115 };
116
117 DDFileUploaderBase.prototype.uploadQueueLoadNext = function() {
118 var item = this.uploadQueue.shift();
119 if (item) {
120 this.upload(item);
121 }
122 else {
123 this._uploadQueueRunning = false;
124 }
125 };
126
127 }());