2013-12-23 18:27:33 +00:00
|
|
|
chrome.version = window.navigator.appVersion.match(/Chrome\/(\d+)/)[1] * 1 || 0;
|
|
|
|
|
2013-09-13 22:57:57 +00:00
|
|
|
require([
|
2013-12-04 17:24:04 +00:00
|
|
|
"command",
|
2014-08-18 05:07:42 +00:00
|
|
|
"editor",
|
2014-01-21 06:14:46 +00:00
|
|
|
"storage/settingsProvider",
|
2013-12-04 17:24:04 +00:00
|
|
|
"ui/dialog",
|
2014-04-04 16:42:31 +00:00
|
|
|
"sessions",
|
|
|
|
"util/manos",
|
2014-06-30 01:16:50 +00:00
|
|
|
"util/i18n",
|
2013-12-04 17:24:04 +00:00
|
|
|
"ui/projectManager",
|
|
|
|
"ui/keys",
|
|
|
|
"fileManager",
|
|
|
|
"ui/menus",
|
|
|
|
"ui/palette",
|
2014-04-04 06:45:19 +00:00
|
|
|
"ui/cli",
|
2015-03-19 03:41:38 +00:00
|
|
|
"ui/theme",
|
2013-12-04 18:52:56 +00:00
|
|
|
"api",
|
|
|
|
"storage/syncfile"
|
2014-08-18 05:07:42 +00:00
|
|
|
], function(command, editor, Settings, dialog, sessions, M, i18n) {
|
2014-06-30 01:16:50 +00:00
|
|
|
|
|
|
|
//translate inline strings
|
|
|
|
i18n.page();
|
2013-08-20 00:53:03 +00:00
|
|
|
|
2013-08-23 23:03:46 +00:00
|
|
|
var frame = chrome.app.window.current();
|
|
|
|
|
2013-09-13 22:57:57 +00:00
|
|
|
var setTheme = function() {
|
2014-01-21 06:14:46 +00:00
|
|
|
Settings.pull("user").then(function(data) {
|
|
|
|
var themes = {
|
|
|
|
"dark": "css/caret-dark.css",
|
|
|
|
"light": "css/caret.css"
|
|
|
|
};
|
|
|
|
var theme = data.user.uiTheme || "light";
|
|
|
|
var url = themes[theme] || themes.dark;
|
|
|
|
document.find("#theme").setAttribute("href", url);
|
|
|
|
});
|
2013-09-13 22:57:57 +00:00
|
|
|
}
|
2014-02-27 06:34:02 +00:00
|
|
|
setTheme();
|
2014-01-12 23:35:30 +00:00
|
|
|
|
2014-02-27 06:34:02 +00:00
|
|
|
//these are modules that must be loaded before init:complete
|
2014-01-12 23:35:30 +00:00
|
|
|
var loadedModules = {
|
2014-06-30 01:16:50 +00:00
|
|
|
"editor": false,
|
|
|
|
"fileManager": false,
|
2014-01-12 23:35:30 +00:00
|
|
|
"sessions": false
|
|
|
|
};
|
2013-09-13 22:57:57 +00:00
|
|
|
|
2013-09-02 01:28:31 +00:00
|
|
|
//the settings manager may also fire init:restart to re-init components after startup
|
2014-01-12 23:35:30 +00:00
|
|
|
command.fire("init:startup", function(mod) {
|
|
|
|
//ignore callback in non-essential modules
|
|
|
|
if (typeof loadedModules[mod] == "undefined") return;
|
|
|
|
loadedModules[mod] = true;
|
|
|
|
for (var key in loadedModules) {
|
|
|
|
if (!loadedModules[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//all specified modules are loaded, app is ready for init:complete
|
|
|
|
command.fire("init:complete");
|
|
|
|
});
|
2013-09-13 22:57:57 +00:00
|
|
|
command.on("init:restart", setTheme);
|
2013-09-02 01:26:22 +00:00
|
|
|
|
2014-02-27 06:34:02 +00:00
|
|
|
//code to enable update checking
|
2013-10-28 21:02:11 +00:00
|
|
|
var updateID = "caret:update";
|
|
|
|
|
2013-11-06 20:18:50 +00:00
|
|
|
var checkUpdates = function(isManual) {
|
|
|
|
chrome.runtime.requestUpdateCheck(function(status, details) {
|
|
|
|
if (status == "update_available") {
|
2013-12-16 18:44:11 +00:00
|
|
|
chrome.runtime.onUpdateAvailable.addListener(function() {
|
2014-05-03 22:55:38 +00:00
|
|
|
chrome.notifications.clear(updateID, function() {
|
|
|
|
chrome.notifications.create(updateID, {
|
|
|
|
type: "basic",
|
|
|
|
iconUrl: "icon-128.png",
|
|
|
|
title: "Caret: Update Available",
|
|
|
|
message: "An update to Caret version " + details.version + " is available. Would you like to update and restart now?",
|
|
|
|
buttons: [ { title: "Yes, update and restart" }, { title: "No thanks" }]
|
|
|
|
}, function(id) { updateID = id });
|
|
|
|
});
|
2013-12-16 18:44:11 +00:00
|
|
|
});
|
2013-11-06 20:18:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-21 06:14:46 +00:00
|
|
|
Settings.pull("user").then(function(cfg) {
|
|
|
|
if (cfg.user.promptForUpdates !== false) checkUpdates();
|
|
|
|
});
|
2013-11-06 20:18:50 +00:00
|
|
|
command.on("app:check-for-updates", checkUpdates);
|
2013-10-28 21:02:11 +00:00
|
|
|
|
|
|
|
chrome.notifications.onButtonClicked.addListener(function(id, index) {
|
|
|
|
if (id != updateID) return;
|
|
|
|
if (index == 0) {
|
|
|
|
chrome.runtime.reload();
|
2013-09-16 17:22:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-27 06:34:02 +00:00
|
|
|
command.on("app:exit", function() {
|
2014-04-04 16:42:31 +00:00
|
|
|
var cancelled = false;
|
|
|
|
var tabs = sessions.getAllTabs();
|
|
|
|
M.serial(tabs, function(tab, c) {
|
2014-04-23 17:15:14 +00:00
|
|
|
if (tab.modified && (!tab.file || !tab.file.virtual)) {
|
2014-04-04 16:42:31 +00:00
|
|
|
return dialog(
|
2014-07-07 18:32:05 +00:00
|
|
|
i18n.get("dialogUnsaved", tab.fileName),
|
2014-04-04 16:42:31 +00:00
|
|
|
[
|
2014-07-07 18:32:05 +00:00
|
|
|
{ label: i18n.get("dialogSave"), value: "save", shortcut: "s" },
|
|
|
|
{ label: i18n.get("dialogDiscard"), value: "discard", shortcut: "n" },
|
|
|
|
{ label: i18n.get("dialogCancel"), value: "cancel", shortcut: "c" }
|
2014-04-04 16:42:31 +00:00
|
|
|
],
|
|
|
|
function(value) {
|
|
|
|
if (!value || value == "cancel") {
|
|
|
|
cancelled = true;
|
|
|
|
}
|
|
|
|
if (value == "save") {
|
2014-08-31 05:41:33 +00:00
|
|
|
return tab.save().then(c);
|
2014-04-04 16:42:31 +00:00
|
|
|
}
|
2014-08-31 05:41:33 +00:00
|
|
|
c(!cancelled);
|
2014-04-04 16:42:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
c();
|
|
|
|
}, function() {
|
|
|
|
if (!cancelled) frame.close();
|
|
|
|
})
|
2014-02-27 06:34:02 +00:00
|
|
|
});
|
|
|
|
|
2014-04-03 22:06:30 +00:00
|
|
|
command.on("app:minimize", function() {
|
|
|
|
frame.minimize();
|
2014-08-21 15:13:07 +00:00
|
|
|
editor.focus();
|
2014-04-03 22:06:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
command.on("app:maximize", function() {
|
2014-04-04 20:13:25 +00:00
|
|
|
frame.isMaximized() || frame.isFullscreen() ? frame.restore() : frame.maximize();
|
2014-08-18 05:07:42 +00:00
|
|
|
editor.focus();
|
2014-04-04 16:13:54 +00:00
|
|
|
});
|
2014-04-03 22:06:30 +00:00
|
|
|
|
2014-03-25 02:29:56 +00:00
|
|
|
command.on("app:restart", function() {
|
|
|
|
chrome.runtime.reload();
|
2014-04-04 16:13:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//handle immersive fullscreen
|
2014-04-04 20:21:27 +00:00
|
|
|
var onFullscreen = function() {
|
2014-04-04 20:13:25 +00:00
|
|
|
Settings.pull("user").then(function(data) {
|
|
|
|
if (data.user.immersiveFullscreen) {
|
2014-04-04 16:13:54 +00:00
|
|
|
document.find("body").addClass("immersive");
|
2014-04-06 16:00:48 +00:00
|
|
|
editor.resize();
|
2014-04-04 16:13:54 +00:00
|
|
|
}
|
2014-04-04 16:16:35 +00:00
|
|
|
});
|
2014-04-04 20:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frame.onFullscreened.addListener(onFullscreen);
|
|
|
|
if (frame.isFullscreen()) {
|
|
|
|
onFullscreen();
|
|
|
|
}
|
2014-04-04 16:13:54 +00:00
|
|
|
|
|
|
|
frame.onRestored.addListener(function() {
|
|
|
|
document.find("body").removeClass("immersive");
|
|
|
|
});
|
2014-03-25 02:29:56 +00:00
|
|
|
|
2014-04-24 19:15:51 +00:00
|
|
|
//It's nice to be able to launch the debugger from a command stroke
|
|
|
|
command.on("app:debug", function() {
|
|
|
|
debugger;
|
|
|
|
});
|
|
|
|
|
|
|
|
command.on("app:browse", function(url) {
|
|
|
|
window.open(url, "target=_blank");
|
|
|
|
});
|
|
|
|
|
2014-04-10 23:48:01 +00:00
|
|
|
//kill middle clicks if not handled
|
|
|
|
|
|
|
|
document.body.on("click", function(e) {
|
|
|
|
if (e.button == 1) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-04 08:11:07 +00:00
|
|
|
});
|