2013-09-16 18:16:06 +00:00
|
|
|
define([
|
2013-09-24 19:16:19 +00:00
|
|
|
"sessions",
|
2014-02-20 17:00:29 +00:00
|
|
|
"editor",
|
2013-12-04 17:24:04 +00:00
|
|
|
"storage/file",
|
|
|
|
"ui/dialog",
|
2013-09-16 18:16:06 +00:00
|
|
|
"command",
|
2014-01-21 06:10:06 +00:00
|
|
|
"storage/settingsProvider",
|
2014-01-12 09:12:14 +00:00
|
|
|
"util/manos",
|
|
|
|
"ui/projectManager"
|
2014-02-20 17:00:29 +00:00
|
|
|
], function(sessions, editor, File, dialog, command, Settings, M, projectManager) {
|
2014-02-27 06:34:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
FileManager splits out the session code that specifically deals with I/O.
|
|
|
|
Pretty much the whole file is just bindings to various commands.
|
|
|
|
|
|
|
|
Now that session.js is refactored, this could probably move into a submodule,
|
|
|
|
except that it gets loaded explicitly on startup.
|
|
|
|
*/
|
2013-09-16 18:16:06 +00:00
|
|
|
|
|
|
|
var openFromLaunchData = function() {
|
2013-10-28 19:58:24 +00:00
|
|
|
if (window.launchData) {
|
|
|
|
window.launchData.forEach(function(file) {
|
2013-10-28 19:12:08 +00:00
|
|
|
var f = new File(file.entry);
|
2013-12-23 18:27:51 +00:00
|
|
|
f.read().then(function(contents) {
|
2013-09-16 18:16:06 +00:00
|
|
|
sessions.addFile(contents, f);
|
2013-12-23 18:27:51 +00:00
|
|
|
}, dialog);
|
2013-09-16 18:16:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-02-27 06:34:02 +00:00
|
|
|
|
|
|
|
command.on("session:open-dragdrop", function(items) {
|
2014-01-12 09:12:14 +00:00
|
|
|
[].forEach.call(items, function(entry){
|
|
|
|
//only process files
|
|
|
|
if (entry.kind !== "file") return;
|
|
|
|
entry = entry.webkitGetAsEntry();
|
|
|
|
|
|
|
|
//files get opened in a tab
|
|
|
|
if (entry.isFile) {
|
|
|
|
var f = new File(entry);
|
|
|
|
return f.read().then(function(data) {
|
|
|
|
sessions.addFile(data, f);
|
|
|
|
}, dialog);
|
|
|
|
//directories get added to project
|
|
|
|
} else if (entry.isDirectory) {
|
|
|
|
projectManager.insertDirectory(entry);
|
|
|
|
}
|
|
|
|
});
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
2014-01-12 09:26:37 +00:00
|
|
|
|
|
|
|
document.body.on("dragover", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.on("drop", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if (e.dataTransfer.types.indexOf("Files") === -1) return;
|
2014-01-12 09:34:47 +00:00
|
|
|
command.fire("session:open-dragdrop", e.dataTransfer.items);
|
2014-01-12 09:26:37 +00:00
|
|
|
});
|
2014-01-12 09:34:47 +00:00
|
|
|
|
2013-12-17 01:35:02 +00:00
|
|
|
command.on("session:new-file", function(content) { return sessions.addFile(content) });
|
2014-02-27 06:34:02 +00:00
|
|
|
|
|
|
|
command.on("session:open-file", function(c) {
|
|
|
|
//have to call chooseEntry manually to support multiple files
|
|
|
|
var args = {
|
|
|
|
type: "openWritableFile"
|
|
|
|
};
|
|
|
|
if (chrome.version >= 30) {
|
|
|
|
args.acceptsMultiple = true;
|
|
|
|
}
|
|
|
|
chrome.fileSystem.chooseEntry(args, function(files) {
|
|
|
|
//annoying array function test, since it's not apparently a real array
|
|
|
|
if (!files.slice) {
|
|
|
|
files = [ files ];
|
|
|
|
}
|
|
|
|
files.map(function(entry) {
|
|
|
|
var f = new File(entry);
|
|
|
|
return f.read().then(function(data) {
|
|
|
|
sessions.addFile(data, f);
|
|
|
|
}, dialog);
|
|
|
|
});
|
|
|
|
Promise.all(files).then(c);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-22 06:27:36 +00:00
|
|
|
command.on("session:save-file", function(c) {
|
|
|
|
sessions.getCurrent()
|
|
|
|
.save(c)
|
|
|
|
.then(function() {
|
|
|
|
command.fire("session:syntax");
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-17 01:35:02 +00:00
|
|
|
command.on("session:save-file-as", function(c) {
|
2013-11-06 16:46:20 +00:00
|
|
|
var tab = sessions.getCurrent();
|
2013-12-23 19:20:04 +00:00
|
|
|
tab.save(true).then(function() {
|
2014-01-21 06:10:06 +00:00
|
|
|
var mode = tab.detectSyntax();
|
2014-01-21 02:03:18 +00:00
|
|
|
sessions.renderTabs();
|
|
|
|
command.fire("session:syntax", mode);
|
2013-12-17 01:35:02 +00:00
|
|
|
if (c) c();
|
|
|
|
});
|
2013-11-06 16:46:20 +00:00
|
|
|
});
|
2013-09-24 02:22:03 +00:00
|
|
|
|
2013-12-17 01:35:02 +00:00
|
|
|
command.on("session:revert-file", function(c) {
|
2013-09-16 18:58:37 +00:00
|
|
|
var tab = sessions.getCurrent();
|
|
|
|
if (!tab.file) return;
|
2013-12-23 18:27:51 +00:00
|
|
|
tab.file.read().then(function(data) {
|
2013-09-16 18:58:37 +00:00
|
|
|
tab.setValue(data);
|
|
|
|
tab.modified = false;
|
|
|
|
sessions.renderTabs();
|
2013-12-24 00:15:03 +00:00
|
|
|
c();
|
2013-09-16 18:58:37 +00:00
|
|
|
});
|
|
|
|
});
|
2014-02-27 06:34:02 +00:00
|
|
|
|
|
|
|
command.on("session:retain-tabs", function() {
|
2014-02-22 04:46:28 +00:00
|
|
|
var tabs = sessions.getAllTabs();
|
|
|
|
var keep = [];
|
|
|
|
tabs.forEach(function(tab, i) {
|
|
|
|
if (!tab.file || tab.file.virtual) return;
|
|
|
|
keep[i] = tab.file.retain();
|
|
|
|
});
|
|
|
|
keep = keep.filter(function(m) { return m });
|
|
|
|
if (keep.length) {
|
|
|
|
chrome.storage.local.set({ retained: keep });
|
|
|
|
}
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
2014-02-22 04:46:28 +00:00
|
|
|
|
2013-09-21 00:39:04 +00:00
|
|
|
command.on("session:check-file", function() {
|
|
|
|
var tab = sessions.getCurrent();
|
2013-09-21 19:21:25 +00:00
|
|
|
if (!tab.file || tab.file.virtual) return;
|
2013-09-21 00:39:04 +00:00
|
|
|
tab.file.entry.file(function(entry) {
|
|
|
|
if (tab.modifiedAt && entry.lastModifiedDate > tab.modifiedAt) {
|
|
|
|
if (tab.modified) {
|
|
|
|
dialog(
|
|
|
|
"This file has been modified since the last time it was saved. Would you like to reload?",
|
|
|
|
[{label: "Reload", value: true}, {label: "Cancel", value: false, focus: true}],
|
|
|
|
function(confirmed) {
|
|
|
|
if (confirmed) {
|
|
|
|
command.fire("session:revert-file");
|
|
|
|
} else {
|
|
|
|
tab.modifiedAt = new Date();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
command.fire("session:revert-file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-09-16 18:16:06 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("session:open-settings-file", function(name, c) {
|
2013-09-16 18:16:06 +00:00
|
|
|
Settings.load(name, function() {
|
|
|
|
var data = Settings.getAsString(name);
|
|
|
|
var file = Settings.getAsFile(name);
|
|
|
|
sessions.addFile(data, file);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-09-16 18:16:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
//defaults don't get loaded as files, just as content
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("session:open-settings-defaults", function(name, c) {
|
2013-09-16 18:16:06 +00:00
|
|
|
sessions.addDefaultsFile(name);
|
2013-12-24 00:15:03 +00:00
|
|
|
if (c) c();
|
2013-09-16 18:16:06 +00:00
|
|
|
});
|
|
|
|
|
2014-02-20 17:00:29 +00:00
|
|
|
command.on("session:insert-from-file", function(c) {
|
|
|
|
var f = new File();
|
|
|
|
f.open().then(f.read.bind(f)).then(function(text) {
|
|
|
|
editor.execCommand("insertstring", text);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-16 18:16:06 +00:00
|
|
|
command.on("session:open-launch", openFromLaunchData);
|
|
|
|
|
2014-01-12 23:35:30 +00:00
|
|
|
var init = function(complete) {
|
2013-09-16 18:16:06 +00:00
|
|
|
openFromLaunchData();
|
2014-01-21 06:10:06 +00:00
|
|
|
Settings.pull("user").then(function(data) {
|
|
|
|
if (data.user.disableTabRestore) complete("fileManager");
|
|
|
|
chrome.storage.local.get("retained", function(data) {
|
|
|
|
var failures = [];
|
2014-01-27 19:11:58 +00:00
|
|
|
if (!data.retained || !data.retained.length) return complete("fileManager");
|
2014-01-21 06:10:06 +00:00
|
|
|
//try to restore items in order
|
2014-01-27 19:11:58 +00:00
|
|
|
M.map(
|
|
|
|
data.retained,
|
|
|
|
function(id, i, c) {
|
|
|
|
var file = new File();
|
|
|
|
file
|
|
|
|
.restore(id)
|
|
|
|
.then(file.read.bind(file))
|
|
|
|
.then(function(data) {
|
|
|
|
c({
|
|
|
|
value: data,
|
|
|
|
file: file
|
2014-01-21 06:10:06 +00:00
|
|
|
});
|
2014-01-27 19:11:58 +00:00
|
|
|
}, function(err) {
|
|
|
|
failures.push(id);
|
|
|
|
c(null);
|
2013-12-23 18:27:51 +00:00
|
|
|
});
|
2014-01-27 19:11:58 +00:00
|
|
|
},
|
|
|
|
function(restored) {
|
|
|
|
restored = restored.filter(function(d) { return d });
|
|
|
|
for (var i = 0; i < restored.length; i++) {
|
|
|
|
var tab = restored[i];
|
|
|
|
sessions.addFile(tab.value, tab.file);
|
2013-09-16 18:16:06 +00:00
|
|
|
}
|
2014-01-27 19:11:58 +00:00
|
|
|
complete("fileManager");
|
|
|
|
if (!failures.length) return;
|
|
|
|
chrome.storage.local.get("retained", function(data) {
|
|
|
|
if (!data.retained) return;
|
|
|
|
chrome.storage.local.set({
|
|
|
|
retained: data.retained.filter(function(d) { return failures.indexOf(d) == -1 })
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2014-01-21 06:10:06 +00:00
|
|
|
});
|
2013-09-16 18:16:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-24 02:28:52 +00:00
|
|
|
var reset = function() {
|
|
|
|
var tabs = sessions.getAllTabs();
|
2014-01-21 06:10:06 +00:00
|
|
|
var virtuals = [];
|
2013-09-24 02:28:52 +00:00
|
|
|
tabs.forEach(function(tab) {
|
|
|
|
if (tab.file && tab.file.virtual) {
|
2014-01-21 06:10:06 +00:00
|
|
|
var v = tab.file.read().then(function(data) {
|
|
|
|
tab.setValue(data);
|
2013-09-24 02:28:52 +00:00
|
|
|
tab.modified = false;
|
|
|
|
});
|
2014-01-21 06:10:06 +00:00
|
|
|
virtuals.push(v);
|
2013-09-24 02:28:52 +00:00
|
|
|
}
|
|
|
|
});
|
2014-01-21 06:10:06 +00:00
|
|
|
Promise.all(virtuals).then(function() {
|
|
|
|
setTimeout(sessions.renderTabs, 10);
|
|
|
|
});
|
2013-09-24 02:28:52 +00:00
|
|
|
};
|
|
|
|
|
2013-09-16 18:16:06 +00:00
|
|
|
command.on("init:startup", init);
|
2013-09-24 02:28:52 +00:00
|
|
|
command.on("init:restart", reset);
|
2013-09-16 18:16:06 +00:00
|
|
|
|
|
|
|
});
|