Upload js. Implémentation en cours.
[Plinn.git] / skins / fileupload.js
index d862ad6..d1da59e 100644 (file)
@@ -1,54 +1,75 @@
-function dragenter(evt) {
+// © 2013 Benoît Pin MINES ParisTech
+var DDFileUploader;
+
+(function(){
+
+DDFileUploader = function(dropbox, uploadUrl) {
+       this.dropbox = dropbox;
+       this.uploadUrl = uploadUrl;
+       var self = this;
+       addListener(dropbox, 'dragenter', function(evt){self.dragenter(evt);});
+       addListener(dropbox, 'dragover', function(evt){self.dragover(evt);});
+       addListener(dropbox, 'drop', function(evt){self.drop(evt);});
+};
+
+DDFileUploader.prototype.dragenter = function(evt) {
        disableDefault(evt);
        disablePropagation(evt);
-}
+};
 
-function dragover(evt) {
+DDFileUploader.prototype.dragover = function(evt) {
        disableDefault(evt);
        disablePropagation(evt);
        evt = getEventObject(evt);
        var dt = evt.dataTransfer;
        dt.dropEffect = 'copy';
-}
+};
 
 
-function drop(evt) {
+DDFileUploader.prototype.drop = function(evt) {
        disableDefault(evt);
        disablePropagation(evt);
        getEventObject(evt);
        var dt = evt.dataTransfer;
        dt.dropEffect = 'copy';
-       handleFiles(dt.files);
-}
+       this.handleFiles(dt.files);
+};
 
-function handleFiles(files) {
-       for (var i = 0; i < files.length; i++) {
-               var file = files[i];
+DDFileUploader.prototype.handleFiles = function(files) {
+       var file, i;
+       for (i = 0; i < files.length; i++) {
+               file = files[i];
                console.log(file.type);
-               // console.log(file);
-               // var imageType = /image.*/;
-               //      
-               // if (!file.type.match(imageType)) {
-               //   continue;
-               // }
-               //      
-               // var img = document.createElement("img");
-               // img.classList.add("obj");
-               // img.file = file;
-               // preview.appendChild(img);
-               //      
-               // var reader = new FileReader();
-               // reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
-               // reader.readAsDataURL(file);
+               this.upload(file);
        }
-}
+};
+
 
-function init() {
-       var dropbox = document.getElementById('dropbox');
-       addListener(dropbox, 'dragenter', dragenter);
-       addListener(dropbox, 'dragover', dragover);
-       addListener(dropbox, 'drop', drop);
-}
+DDFileUploader.prototype.upload = function(file) {
+       var reader = new FileReader();
+       var xhr = new XMLHttpRequest();
+       var percentage;
+       var self = this;
+       // this.xhr.upload.addEventListener("progress", function(e) {
+       //      if (e.lengthComputable) {
+       //              var percentage = Math.round((e.loaded * 100) / e.total);
+       //              self.ctrl.update(percentage);
+       //      }
+       // }, false);
 
+       // xhr.upload.addEventListener("load", function(e){
+       //   self.ctrl.update(100);
+       //   var canvas = self.ctrl.ctx.canvas;
+       //   canvas.parentNode.removeChild(canvas);
+       //  }, false);
+       console.log(file);
+       xhr.open("PUT", this.uploadUrl + '/' + file.name);
+       xhr.setRequestHeader("Content-Type", file.type);
+       // xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
+       reader.onload = function(evt) {
+               xhr.sendAsBinary(evt.target.result);
+       };
+       reader.readAsBinaryString(file);
+};
 
-addListener(window, 'load', init);
\ No newline at end of file
+}());