2016-06-26 11:53:19 +00:00
|
|
|
define(function(require, exports, module) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
main.consumes = [
|
2017-04-19 16:53:30 +00:00
|
|
|
"Plugin", "c9", "ui", "menus", "tree", "info", "vfs", "preferences", "settings", "util"
|
2016-06-26 11:53:19 +00:00
|
|
|
];
|
|
|
|
main.provides = ["download"];
|
|
|
|
return main;
|
|
|
|
|
|
|
|
function main(options, imports, register) {
|
|
|
|
var Plugin = imports.Plugin;
|
|
|
|
var ui = imports.ui;
|
|
|
|
var c9 = imports.c9;
|
2017-04-19 16:53:30 +00:00
|
|
|
var util = imports.util;
|
2016-06-26 11:53:19 +00:00
|
|
|
var menus = imports.menus;
|
|
|
|
var tree = imports.tree;
|
|
|
|
var vfs = imports.vfs;
|
|
|
|
var info = imports.info;
|
|
|
|
var prefs = imports.preferences;
|
|
|
|
var settings = imports.settings;
|
|
|
|
|
|
|
|
var SETTING_NAME = "downloadFilesAs";
|
|
|
|
var SETTING_PATH = "user/general/@" + SETTING_NAME;
|
|
|
|
|
|
|
|
/***** Initialization *****/
|
|
|
|
|
|
|
|
var plugin = new Plugin("Ajax.org", main.consumes);
|
|
|
|
|
|
|
|
var loaded = false;
|
2017-01-30 11:32:54 +00:00
|
|
|
function load() {
|
2016-06-26 11:53:19 +00:00
|
|
|
if (loaded) return false;
|
|
|
|
loaded = true;
|
|
|
|
|
|
|
|
menus.addItemByPath("File/Download Project", new ui.item({
|
|
|
|
onclick: downloadProject
|
|
|
|
}), 1300, plugin);
|
|
|
|
|
|
|
|
// Context Menu
|
|
|
|
tree.getElement("mnuCtxTree", function(mnuCtxTree) {
|
|
|
|
menus.addItemToMenu(mnuCtxTree, new ui.item({
|
|
|
|
match: "folder|project",
|
2017-01-30 11:32:54 +00:00
|
|
|
isAvailable: function() {
|
2016-06-26 11:53:19 +00:00
|
|
|
return tree.selectedNode;
|
|
|
|
},
|
|
|
|
caption: "Download",
|
|
|
|
onclick: download
|
|
|
|
}), 140, plugin);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Preferences
|
|
|
|
prefs.add({
|
2017-01-30 11:32:54 +00:00
|
|
|
"General": {
|
|
|
|
"Tree & Navigate": {
|
|
|
|
"Download Files As": {
|
2016-06-26 11:53:19 +00:00
|
|
|
type: "dropdown",
|
|
|
|
path: SETTING_PATH,
|
|
|
|
items: [
|
2017-01-30 11:32:54 +00:00
|
|
|
{ caption: "auto", value: "auto" },
|
|
|
|
{ caption: "tar.gz", value: "tar.gz" },
|
|
|
|
{ caption: "zip", value: "zip" }
|
2016-06-26 11:53:19 +00:00
|
|
|
],
|
|
|
|
position: 5000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, plugin);
|
|
|
|
|
|
|
|
settings.on("read", function() {
|
|
|
|
settings.setDefaults("user/general", [[SETTING_NAME, "auto"]]);
|
|
|
|
}, plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
function download() {
|
|
|
|
if (!c9.has(c9.STORAGE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var node = tree.selectedNode;
|
|
|
|
if (!node) return;
|
|
|
|
|
|
|
|
var paths = tree.selectedNodes.map(function(node) {
|
2017-04-19 16:53:30 +00:00
|
|
|
return util.normalizePath(node.path);
|
2016-06-26 11:53:19 +00:00
|
|
|
});
|
|
|
|
if (node.isFolder && node.path == "/")
|
|
|
|
downloadProject();
|
|
|
|
else if (paths.length > 1)
|
|
|
|
downloadPaths(paths);
|
|
|
|
else if (node.isFolder)
|
2017-04-19 16:53:30 +00:00
|
|
|
downloadFolder(paths[0]);
|
2016-06-26 11:53:19 +00:00
|
|
|
else
|
2017-04-19 16:53:30 +00:00
|
|
|
downloadFile(paths[0]);
|
2016-06-26 11:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function downloadProject() {
|
|
|
|
vfs.download("/", makeArchiveFilename(info.getWorkspace().name));
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadPaths(paths) {
|
|
|
|
var lastPart = paths[0].match(/([^\/]*)\/?$/)[1];
|
|
|
|
var filename = lastPart ? (lastPart + "[+" + (paths.length - 1) + "]") : info.getWorkspace().name;
|
|
|
|
vfs.download(paths, makeArchiveFilename(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadFolder(path) {
|
|
|
|
var withTrailingSlash = path.replace(/\/*$/, "/");
|
|
|
|
var parts = withTrailingSlash.split("/");
|
|
|
|
var folderName = parts[parts.length - 2];
|
|
|
|
vfs.download(withTrailingSlash, makeArchiveFilename(folderName));
|
|
|
|
}
|
|
|
|
|
|
|
|
function downloadFile(path) {
|
|
|
|
vfs.download(path.replace(/\/*$/, ""), null, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeArchiveFilename(filename) {
|
|
|
|
return filename + getArchiveFileExtension();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getArchiveFileExtension() {
|
|
|
|
var downloadFilesAs = settings.get(SETTING_PATH);
|
|
|
|
if (downloadFilesAs === 'auto' || !downloadFilesAs) {
|
|
|
|
downloadFilesAs = /Win/.test(navigator.platform) ? 'zip' : 'tar.gz';
|
|
|
|
}
|
|
|
|
return '.' + downloadFilesAs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***** Lifecycle *****/
|
|
|
|
|
2017-01-30 11:32:54 +00:00
|
|
|
plugin.on("load", function() {
|
2016-06-26 11:53:19 +00:00
|
|
|
load();
|
|
|
|
});
|
|
|
|
|
|
|
|
/***** Register and define API *****/
|
|
|
|
|
|
|
|
plugin.freezePublicAPI({
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
register(null, {
|
|
|
|
download: plugin
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|