Caret/js/fileManager.js
Thomas Wilburn 5ff71e0a5c Added a revert option
Fixes #34
2013-09-16 11:58:37 -07:00

107 lines
No EOL
2.9 KiB
JavaScript

define([
"sessions",
"file",
"dialog",
"command",
"settings!" //not excited, it just runs as a RequireJS plugin
], function(sessions, File, dialog, command, Settings) {
var openFile = function() {
var f = new File();
f.open(function(err) {
if (err) {
return dialog(err);
}
f.read(function(err, data) {
if (err) {
dialog(err);
return;
}
sessions.addFile(data, f);
});
});
};
var openFromLaunchData = function() {
if (window.launchData && window.launchData.items) {
window.launchData.items.forEach(function(file) {
var f = new File();
f.entry = file.entry;
f.read(function(err, contents) {
if (err) {
dialog(err);
return;
}
sessions.addFile(contents, f);
});
});
}
};
command.on("session:new-file", function() { sessions.addFile() });
command.on("session:open-file", openFile);
command.on("session:save-file", function() { sessions.getCurrent().save() });
command.on("session:save-file-as", function() { sessions.getCurrent().save(true) });
command.on("session:revert-file", function() {
var tab = sessions.getCurrent();
if (!tab.file) return;
tab.file.read(function(err, data) {
if (err) return;
tab.setValue(data);
tab.modified = false;
sessions.renderTabs();
});
});
command.on("session:open-settings-file", function(name) {
Settings.load(name, function() {
var data = Settings.getAsString(name);
var file = Settings.getAsFile(name);
sessions.addFile(data, file);
});
});
//defaults don't get loaded as files, just as content
command.on("session:open-settings-defaults", function(name) {
sessions.addDefaultsFile(name);
});
command.on("session:open-launch", openFromLaunchData);
var init = function() {
openFromLaunchData();
chrome.storage.local.get("retained", function(data) {
var failures = [];
if (data.retained && data.retained.length) {
data.retained.forEach(function(id) {
var file = new File();
file.restore(id, function(err, f) {
if (err) {
//add failures to be removed asynchronously
failures.push(id);
return;
}
file.read(function(err, contents) {
if (err) return;
sessions.addFile(contents, file);
});
return id;
});
});
}
//after a reasonable delay, filter failures out of retention
setTimeout(function() {
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 })
});
});
}, 500);
});
};
command.on("init:startup", init);
});