Skip to content

Commit

Permalink
First code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Rendez committed Sep 29, 2011
1 parent 8cd4204 commit d08c2de
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 81 deletions.
202 changes: 122 additions & 80 deletions client/ext/dragdrop/dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,110 +12,152 @@ var util = require("core/util");
var fs = require("ext/filesystem/filesystem");
var tree = require("ext/tree/tree");

var MAX_UPLOAD_SIZE = 52428800;
var MAX_OPENFILE_SIZE = 2097152;

module.exports = ext.register("ext/dragdrop/dragdrop", {
dev : "Ajax.org",
name : "Dragdrop",
alone : true,
type : ext.GENERAL,
deps : [tree],

init : function(){
nodes: [],

init: function() {
if (typeof window.FileReader == "undefined")
return;

var holders = [trFiles.$ext, tabEditors.$ext],
_self = this;
var _self = this;
var holders = [trFiles.$ext, tabEditors.$ext];
var dropbox = document.createElement("div");

apf.setStyleClass(dropbox, "draganddrop");
dropbox.innerHtml = "Drop files here to upload";

holders.forEach(function(holder) {
holder.ondragover = function() {
apf.setStyleClass(this, "dragdrop_hover");
return false;
};
holder.ondragend = function() {
apf.setStyleClass(this, null, ["dragdrop_hover"]);
return false;
};
holder.ondrop = function(e) {
apf.setStyleClass(this, null, ["dragdrop_hover"]);
e.preventDefault();
_self.onDrop(e);
return false;
};
dropbox = holder.dropbox = dropbox.cloneNode(false);
holder.insertBefore(dropbox);

holder.addEventListener("dragenter", dragEnter, false);
dropbox.addEventListener("dragleave", dragLeave, false);
dropbox.addEventListener("drop", dragDrop, false);

["dragexit", "dragover"].forEach(function(e) {
dropbox.addEventListener(e, noopHandler, false);
});
});

function dragLeave(e) {
apf.stopEvent(e);
apf.setStyleClass(this, null, ["over"]);
}

function dragEnter(e) {
apf.stopEvent(e);
apf.setStyleClass(this.dropbox, "over");
}

function dragDrop(e) {
dragLeave.call(this, e);
_self.onDrop(e);
}

function noopHandler(e) {
apf.stopEvent(e);
}
},

onDrop: function(e) {
// check total filesize of dropped files
/** Check total filesize of dropped files */
for (var size = 0, i = 0, l = e.dataTransfer.files.length; i < l; ++i)
size += e.dataTransfer.files[i].size;

var m50 = 52428800,
m2 = 2097152;
if (size > m50) {
return util.alert("Could not save document", "An error occurred while saving this document",
"The file(s) you dropped exceeds the maximum of 50MB and could therefore not be uploaded.");
if (size > MAX_UPLOAD_SIZE) {
return util.alert(
"Could not save document", "An error occurred while saving this document",
"The file(s) you dropped exceeds the maximum of 50MB and could therefore not be uploaded."
);
}

var files = e.dataTransfer.files;
if (files.length < 1)
return false;

apf.asyncForEach(e.dataTransfer.files, function(file, nextFile) {
var _self = this;

apf.asyncForEach(files, function(file, next) {
/** Processing ... */
var reader = new FileReader();
reader.onload = function(e) {
var node = trFiles.selected;
if (!node)
node = trFiles.xmlRoot.selectSingleNode("folder");
if (node.getAttribute("type") != "folder")
node = node.parentNode;
var path = node.getAttribute("path"),
filename = file.name,
index = 0;
/** Init the reader event handlers */
reader.onloadend = _self.onLoadEnd.bind(_self, file, next);
/** Begin the read operation */
reader.readAsBinaryString(file);
}, function() {});
},

var test = function(exists) {
if (exists) {
filename = file.name + "." + index++;
fs.exists(path + "/" + filename, test);
}
else {
// lock == false, binary = [object]
fs.webdav.write(path + "/" + file.name, e.target.result, false, {
filename : file.name,
filedataname: file.name,
filesize : file.size,
multipart : true
}, function(data, state, extra){
if (state != apf.SUCCESS) {
util.alert(
"Could not save document",
"An error occurred while saving this document",
"Please see if your internet connection is available and try again. "
+ (state == apf.TIMEOUT
? "The connection timed out."
: "The error reported was " + extra.message));
return nextFile();
}

fs.webdav.exec("readdir", [path], function(data) {
// @todo: in case of error, show nice alert dialog
if (data instanceof Error)
return nextFile();

var strXml = data.match(new RegExp(("(<file path='" + path +
"/" + filename + "'.*?>)").replace(/\//g, "\\/")))[1];
onLoadEnd: function(file, next, e) {
var node = trFiles.selected;
if (!node)
node = trFiles.xmlRoot.selectSingleNode("folder");

if (node.getAttribute("type") != "folder")
node = node.parentNode;

var path = node.getAttribute("path");
var filename = file.name;
var index = 0;

var oXml = apf.xmldb.appendChild(node, apf.getXml(strXml));

trFiles.select(oXml);
if (file.size < m2)
ide.dispatchEvent("openfile", {doc: ide.createDocument(oXml)});
return nextFile();
});
});
}
};
function check(exists) {
if (exists) {
filename = file.name + "." + index++;
fs.exists(path + "/" + filename, check);
} else
send();
}

function send() {
// lock == false, binary = [object]
fs.webdav.write(path + "/" + file.name, e.target.result, false, {
filename: file.name,
filedataname: file.name,
filesize: file.size,
multipart: true
}, complete);
}

function complete(data, state, extra) {
if (state != apf.SUCCESS) {
util.alert(
"Could not save document",
"An error occurred while saving this document",
"Please see if your internet connection is available and try again. "
+ (state == apf.TIMEOUT
? "The connection timed out."
: "The error reported was " + extra.message));
return next();
}

fs.webdav.exec("readdir", [path], function(data) {
if (data instanceof Error) {
// @todo: in case of error, show nice alert dialog
return next();
}

fs.exists(path + "/" + file.name, test);
};
reader.readAsBinaryString(file);
}, function() {});
var strXml = data.match(new RegExp(("(<file path='" + path +
"/" + filename + "'.*?>)").replace(/\//g, "\\/")))[1];

var oXml = apf.xmldb.appendChild(node, apf.getXml(strXml));

return false;
trFiles.select(oXml);
if (file.size < MAX_OPENFILE_SIZE)
ide.dispatchEvent("openfile", {doc: ide.createDocument(oXml)});

return next();
});
}

fs.exists(path + "/" + file.name, check);
},

enable : function(){
Expand All @@ -125,7 +167,7 @@ module.exports = ext.register("ext/dragdrop/dragdrop", {
},

destroy : function(){
//Remove all events
//@todo Remove all events
}
});

Expand Down
25 changes: 24 additions & 1 deletion client/style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@ BODY {
color : #666;
}

.relative{
.draganddrop {
position: absolute;
z-index: 99999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: yellow;
opacity: 0;
visibility: hidden;
color: #000;
-webkit-transition: opacity 0.25s ease-out;
-moz-transition: opacity 0.25s ease-out;
-ms-transition: opacity 0.25s ease-out;
-o-transition: opacity 0.25s ease-out;
transition: opacity 0.25s ease-out;
}

.draganddrop.over {
opacity: .35;
visibility: visible;
}

.relative {
position : relative;
}

Expand Down

0 comments on commit d08c2de

Please sign in to comment.