Caret/js/fileManager.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

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: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);
});