2013-09-16 18:16:06 +00:00
|
|
|
define([
|
2013-09-24 19:16:19 +00:00
|
|
|
"sessions",
|
2013-12-04 17:24:04 +00:00
|
|
|
"storage/file",
|
|
|
|
"ui/dialog",
|
2013-09-16 18:16:06 +00:00
|
|
|
"command",
|
2013-10-08 15:31:13 +00:00
|
|
|
"settings!", //not excited, it just runs as a RequireJS plugin,
|
2014-01-12 09:12:14 +00:00
|
|
|
"util/manos",
|
|
|
|
"ui/projectManager"
|
|
|
|
], function(sessions, File, dialog, command, Settings, M, projectManager) {
|
2013-09-16 18:16:06 +00:00
|
|
|
|
2013-12-24 00:15:03 +00:00
|
|
|
var openFile = function(c) {
|
2013-10-28 18:59:14 +00:00
|
|
|
//have to call chooseEntry manually to support multiple files
|
2013-11-11 21:31:41 +00:00
|
|
|
var args = {
|
|
|
|
type: "openWritableFile"
|
|
|
|
};
|
|
|
|
if (chrome.version >= 30) {
|
|
|
|
args.acceptsMultiple = true;
|
|
|
|
}
|
|
|
|
chrome.fileSystem.chooseEntry(args, function(files) {
|
2013-10-28 18:59:14 +00:00
|
|
|
//annoying array function test, since it's not apparently a real array
|
|
|
|
if (!files.slice) {
|
|
|
|
files = [ files ];
|
|
|
|
};
|
2013-12-24 00:15:03 +00:00
|
|
|
files.map(function(entry) {
|
2013-10-28 19:12:08 +00:00
|
|
|
var f = new File(entry);
|
2013-12-24 00:15:03 +00:00
|
|
|
return f.read().then(function(data) {
|
2013-10-28 18:59:14 +00:00
|
|
|
sessions.addFile(data, f);
|
2013-12-23 18:27:51 +00:00
|
|
|
}, dialog);
|
2013-09-16 18:16:06 +00:00
|
|
|
});
|
2013-12-24 00:15:03 +00:00
|
|
|
Promise.all(files).then(c);
|
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-01-12 09:12:14 +00:00
|
|
|
|
|
|
|
var openFromDropEvent = function(items) {
|
|
|
|
[].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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-09-16 18:16:06 +00:00
|
|
|
|
2014-01-12 09:12:14 +00:00
|
|
|
command.on("session:open-dragdrop", openFromDropEvent);
|
2013-12-17 01:35:02 +00:00
|
|
|
command.on("session:new-file", function(content) { return sessions.addFile(content) });
|
2013-09-16 18:16:06 +00:00
|
|
|
command.on("session:open-file", openFile);
|
2013-12-24 00:15:03 +00:00
|
|
|
command.on("session:save-file", function(c) { sessions.getCurrent().save(c) });
|
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() {
|
2013-11-06 16:46:20 +00:00
|
|
|
sessions.setSyntax(tab);
|
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
|
|
|
});
|
|
|
|
});
|
2013-09-24 02:22:03 +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
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2013-10-08 15:31:13 +00:00
|
|
|
//try to restore items in order
|
2013-11-06 15:37:56 +00:00
|
|
|
M.map(
|
|
|
|
data.retained,
|
|
|
|
function(id, i, c) {
|
|
|
|
var file = new File();
|
2013-12-23 18:27:51 +00:00
|
|
|
file
|
|
|
|
.restore(id)
|
|
|
|
.then(file.read.bind(file))
|
|
|
|
.then(function(data) {
|
2013-11-06 15:37:56 +00:00
|
|
|
c({
|
|
|
|
value: data,
|
|
|
|
file: file
|
2013-12-23 18:27:51 +00:00
|
|
|
})
|
|
|
|
}, function(err) {
|
|
|
|
failures.push(id);
|
|
|
|
});
|
2013-11-06 15:37:56 +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
|
|
|
}
|
2013-11-06 15:37:56 +00:00
|
|
|
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 })
|
|
|
|
});
|
2013-10-08 15:31:13 +00:00
|
|
|
});
|
2013-11-06 15:37:56 +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();
|
|
|
|
tabs.forEach(function(tab) {
|
|
|
|
if (tab.file && tab.file.virtual) {
|
|
|
|
var setting = tab.fileName.replace(".json", "");
|
|
|
|
Settings.load(setting, function() {
|
|
|
|
var value = Settings.getAsString(setting);
|
|
|
|
tab.setValue(value);
|
|
|
|
tab.modified = false;
|
|
|
|
sessions.renderTabs();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
});
|