Caret/js/sessions/switching.js
2014-02-23 14:21:11 -08:00

82 lines
No EOL
2 KiB
JavaScript

define([
"sessions/state",
"editor",
"command"
], function(state, editor, command) {
var stackOffset = 0;
var raiseTab = function(tab) {
editor.setSession(tab);
command.fire("session:syntax");
command.fire("session:render");
editor.focus();
command.fire("session:check-file");
};
var raiseBlurred = function(tab) {
editor.setSession(tab);
syntax.value = tab.syntaxMode || "plain_text";
command.fire("session:render");
command.fire("session:check-file");
};
var resetStack = function(tab) {
var raised = tab || state.stack[stackOffset];
state.stack = state.stack.filter(function(t) { return t != raised });
state.stack.unshift(raised);
}
var watchCtrl = function(e) {
if (e.keyCode == 17) {
resetStack();
document.body.off("keyup", watchCtrl);
ctrl = false;
}
};
var ctrl = false;
var switchTab = function(arg, c) {
arg = arg || 1;
if (!ctrl) {
ctrl = true;
stackOffset = 0;
document.body.on("keyup", watchCtrl);
}
stackOffset = (stackOffset + arg) % state.stack.length;
if (stackOffset < 0) stackOffset = state.stack.length + stackOffset;
raiseTab(state.stack[stackOffset]);
if (c) c();
};
var switchTabLinear = function(shift, c) {
shift = shift || 1;
var current = editor.getSession();
var currentIndex = state.tabs.indexOf(current);
var shifted = (currentIndex + shift) % state.tabs.length;
if (shifted < 0) {
shifted = state.tabs.length + shifted;
}
var tab = state.tabs[shifted];
raiseTab(tab);
resetStack(tab);
if (c) c();
};
command.on("session:raise-tab", function(index) {
var tab = state.tabs[index];
raiseTab(tab);
resetStack(tab);
});
command.on("session:change-tab", switchTab);
command.on("session:change-tab-linear", switchTabLinear);
return {
raise: raiseTab,
raiseBlurred: raiseBlurred,
switchTab: switchTab,
switchLinear: switchTabLinear
};
});