Commit c3e2adf4 authored by Benjamin Fischer's avatar Benjamin Fischer
Browse files

FileHandler2: added

parent b4f3cd25
/**
* filehandler
* =================
*/
define([
"jquery",
"jclass",
"vispa/utils",
], function(
$,
JClass,
Utils
) {
// some globals
// the "manager"
var Manager = JClass._extend({
init: function () {
this.clsBib = {};
this.handlersBase = {};
this.handlersOpen = {};
},
makeMenu: function(info) {
var menu = this._filterMenu(this.handlersBase, info);
// need the open with menu?
if (menu.open) {
var openMenu = this._filterMenu(this.handlersOpen, info);
if (Object.keys(openMenu).length > 1) {
menu.openWith = {
label: "Open with ...",
iconClass: "fa-file",
position: 5,
items: openMenu,
};
}
}
// add divider
menu.divider1 = {divider: true, position: 10};
menu.divider2 = {divider: true, position: 100};
return menu;
},
action: function(what, info) {
var handler = this.handlersBase[what];
if (!handler)
throw Error("so such file handler: "+what);
else
return handler.attempt(info);
},
addOpen: function() {
var args = [].slice.call(arguments);
return this.add.apply(this, ["open"].concat(args));
},
addExt: function(cls, extension) {
cls = (this.clsBib[cls] || cls)._extend(extension);
var args = [].slice.call(arguments, 2);
return this.add.apply(this, [cls].concat(args));
},
add: function(cls) {
cls = (this.clsBib[cls] || cls);
return new (cls.bind.apply(cls, arguments));
},
_filterMenu: function(handlers, info) {
var ret = {};
for (var i in handlers) {
var h = handlers[i];
var m = h.check(info) && h.menu(info);
if (m)
ret[i] = m;
}
return ret;
},
_bestOpen: function(info, _handlers) {
var b, m = _handlers || this._filterMenu(this.handlersOpen, info);
for(var h in m)
if (!b || m[h].position < b.position)
b = m[h];
return b;
},
});
var M = new Manager();
// base classes
M.clsBib.base = JClass._extend({
init: function(name) {
Utils.setROP(this, "name", name);
if (this._peers[name])
throw Error("this filehandler is already defined: "+name);
else
this._peers[name] = this;
},
_peers: M.handlersBase,
attempt: function(info, ev) {
if (ev instanceof Event)
info = $.extend({}, info, {event: ev});
return this.check(info) ? this.run(info) || true : false;
},
// to be overwritten
menuTmpl: undefined,
check: function(info) {
return info.view && info.pathBase;
},
run: null,
menu: function(info) {
var r = this.attempt.bind(this, info);
return $.extend({}, this.menuTmpl, {
callback: function() {
r();
},
});
},
});
M.clsBib.single = M.clsBib.base._extend({
fullPath: function (info) {
return info.pathBase + "/" + info.data.name;
},
ext: function (info) {
if (!info.data) return;
if (info.data.type == "d")
return false;
var f = info.data.name.split(".");
if (f[0] == "")
f = f.slice(1);
var l = f.length;
if (l < 2)
return "";
if (l > 2 && f[l-1] && f[l-2].toLowerCase() == "tar")
return f.slice(-2).join(".");
return f.pop();
},
check: function check(info) {
return check._super.call(this, info) && info.data && info.data.name;
},
});
M.clsBib.open = M.clsBib.single._extend({
init: function init(name, menu, saf, fext, fextNot) {
init._super.call(this, name);
this.menuTmplF = menu;
this.saf = saf;
this.fext = fext;
if (fextNot === undefined && !this._testExt(false, this.fext))
fextNot = false;
this.fextNot = fextNot;
},
_peers: M.handlersOpen,
_testExt: function (ext, value) {
return value instanceof Array ? ~value.indexOf(ext) : (value == ext);
},
check: function check(info) {
var ext = this.ext(info);
return check._super.call(this, info) &&
(this.fext == undefined || this._testExt(ext, this.fext)) &&
!this._testExt(ext, this.fextNot);
},
menu: function menu(info) {
var m = {};
var e = this.ext(info);
for (var k in this.menuTmplF) {
var v = this.menuTmplF[k];
m[k] = $.isFunction(v) ? v.call(this, e, info) : v;
}
return $.extend(m, menu._super.call(this, info));
},
run: function (info) {
return info.view.spawnInstance.apply(info.view, this.saf.call(this, this.fullPath(info), info));
},
});
M.clsBib.multi = M.clsBib.base._extend({
items: function(info) {
var r = info.items;
if (!r)
return [];
if (this.filter)
return r.filter(this.filter);
else
return r.slice();
},
fullPaths: function (info, processing, _items) {
return (_items || this.items(info)).map(function (it) {
if (processing)
it.processing = true;
return info.pathBase + "/" + it.name;
});
},
// to be overwritten as needed
check: function check(info) {
return check._super.call(this, info) && this.items(info).length;
},
filter: null,
});
// position counter
var pos = 11;
// now create the default handler
M.addExt("single", {
menu: function menu(info) {
var bo = M._bestOpen(info);
return $.extend({
label: "Open",
iconClass: bo && bo.iconClass || "fa-file",
disabled: !bo,
position: 1,
}, menu._super.call(this, info));
},
run: function (info) {
var bo = M._bestOpen(info);
return bo ? bo.callback() : false;
},
}, "open");
// clipboard stuff
var ClipboardHandler = M.clsBib.multi._extend({
init: function init(name) {
init._super.call(this, name);
this.menuTmpl = {
label: Utils.capitalize(name),
iconClass: "fa-" + name,
position: pos++,
};
},
run: function (info) {
vispa.clipboard = {
wsId: info.view.workspace.id,
pathBase: info.pathBase,
names: this.fullPaths(info),
action: this.name,
};
}
});
new ClipboardHandler("copy");
new ClipboardHandler("cut");
M.addExt("base", {
menuTmpl: {
label: "Paste",
iconClass: "fa-paste",
position: pos++,
},
check: function check(info) {
var cb = vispa.clipboard;
return check._super.call(this, info) && cb && cb.wsId == info.wsId &&
~["copy", "cut"].indexOf(cb.action);
},
run: function (info) {
var cut = (vispa.clipboard.action == "cut");
info.view.POST("/ajax/fs/paste", {
path: info.pathBase,
paths: JSON.stringify(vispa.clipboard.names),
cut: cut,
}, function () {
if (cut)
vispa.clipboard = null;
});
},
}, "paste");
// other file ops
M.addExt("single", {
menuTmpl: {
label: "Rename",
iconClass: "fa-pencil",
position: pos++,
},
run: function run(info) {
if (info.newName) {
info.data.processing = true;
info.view.POST("/ajax/fs/rename", {
"path": info.pathBase,
"name": info.data.name,
"new_name": info.newName,
});
} else if (info._f2br) {
info.data.renameing = true;
} else {
info.view.prompt("", function (newName) {
if (newName === null || newName === info.data.name) return;
run($.extend({}, info, {newName: newName}));
}, {
value: info.data.name,
placeholder: "new file name",
title: "Rename file",
okLabel: "Rename",
});
}
},
}, "rename");
M.addExt("multi", {
menuTmpl: {
label: "Remove",
iconClass: "fa-times",
position: pos++,
},
run: function run(info) {
if (info.confimed || (info.event && info.event.shiftKey)) {
info.view.POST("/ajax/fs/remove", {
"path": JSON.stringify(this.fullPaths(info, true)),
});
} else {
var it = this.items(info);
var cb = run.bind(this, $.extend(true, {}, info, {confimed: true}));
info.view.confirm(
"Do you really want to delete " + it.length + " items?",
function (ok) {
if (ok) cb();
}, {
focusButton: "yes",
}
);
}
},
}, "remove");
M.addExt("multi", {
menuTmpl: {
label: "Download",
iconClass: "fa-download",
position: pos++,
},
run: function (info) {
var it = this.items(info);
var dl = function (path, del) {
$("<iframe />")
.attr({
src: [
info.view.fileURL(path),
"&download=1",
"&deleteoncomplete=" + String(del)
].join("&"),
style: "display:none;"
}).appendTo("body");
};
if (it.length > 1 || it[0].type=="d") {
info.view.POST("/ajax/fs/compress", {
paths: JSON.stringify(this.fullPaths(info, false, it)),
path: "",
name: [
"vispa",
it.length > 1 ?
info.pathBase.split("/").pop() :
it[0].name,
(new Date()).toISOString().slice(0, 19)
].join("_"),
isTmp: true,
}, function (err, archive) {
if (err) return;
dl(archive, true);
});
} else {
dl(info.pathBase + "/" + it[0].name, false);
}
},
}, "download");
M.addExt("multi", {
menuTmpl: {
label: "Compress",
iconClass: "fa-compress",
position: pos++,
},
run: function run(info) {
if (info.zipName) {
info.view.POST("/ajax/fs/compress", {
paths: JSON.stringify(this.fullPaths(info)),
path: info.pathBase,
name: info.zipName,
isTmp: false,
});
} else {
var self = this;
info.view.prompt(
"Enter the name for the zip-file.",
function (zipName) {
if (zipName)
run.call(self, $.extend({}, info, {zipName: zipName}));
}
);
}
}
}, "compress");
M.addExt("single", {
fext: [
"zip",
// "tar",
// "tgz", "tar.gz",
// "tbz2", "tar.bz2",
],
check: function check(info) {
return false; // TODO: implement this!
return check._super.call(this, info) && ~this.fext.indexOf(this.ext(info));
},
run: function (info) {
throw Error("NOT IMPLEMENTED");
},
}, "extract");
pos = 101;
// TODO: info
return M;
});
......@@ -16,6 +16,7 @@ define([
"vispa/ui/uinode",
"vispa/ui/split",
"vispa/filehandler",
"vispa/filehandler2",
"vispa/prefs/manager",
"vispa/messenger",
"vispa/session",
......@@ -51,6 +52,7 @@ define([
UINode,
Split,
FileHandler,
FileHandler2,
PrefsManager,
Messenger,
SessionManager,
......@@ -131,6 +133,7 @@ define([
this.extManager = Extension._members;
this.jsondata = JSONData._members;
this.fileHandler = new FileHandler();
this.fileHandling = FileHandler2;
// modules
this.preferences = new PrefsManager(); // actually not a module but a vuejs
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment