Caret/js/sessions.js

128 lines
3.2 KiB
JavaScript
Raw Normal View History

define([
"sessions/state",
2014-02-23 22:21:59 +00:00
"sessions/addRemove",
"sessions/switching",
"sessions/binding",
"editor",
"command",
"storage/settingsProvider",
"aceBindings"
],
2014-02-23 23:48:51 +00:00
function(state, addRemove, switching, bindEvents, editor, command, Settings) {
2014-01-21 02:10:35 +00:00
/*
2014-02-23 22:21:59 +00:00
The sessions module handles tracking tabs, their contents, and their bindings.
It shares the sessions:* command namespace with the fileManager module.
2014-01-21 02:10:35 +00:00
*/
var syntax = document.find(".syntax");
2014-02-23 23:48:51 +00:00
command.on("session:syntax", function(mode) {
var session = editor.getSession();
if (mode) {
session.setMode("ace/mode/" + mode);
session.syntaxMode = mode;
} else {
mode = session.syntaxMode;
}
syntax.value = mode;
});
2013-08-21 00:35:25 +00:00
var renderTabs = function() {
var tabContainer = document.find(".tabs");
2013-08-21 00:35:25 +00:00
var contents = "";
2013-08-22 06:35:14 +00:00
var current = editor.getSession();
tabContainer.innerHTML = "";
state.tabs.forEach(function(tab, i) {
var element = tab.render(i);
2013-08-22 06:35:14 +00:00
if (tab === current) {
element.addClass("active");
2013-08-22 06:35:14 +00:00
}
tabContainer.append(element);
2013-08-21 00:35:25 +00:00
});
setTimeout(function() {
//wait for render before triggering the enter animation
tabContainer.findAll(".enter").forEach(function(element) { element.removeClass("enter") });
});
command.fire("session:retain-tabs");
};
command.on("session:render", renderTabs);
var init = function() {
Settings.pull("ace").then(function(data) {
data.ace.modes.forEach(function(mode) {
var option = document.createElement("option");
option.innerHTML = mode.label;
option.value = mode.name;
syntax.append(option);
});
})
2014-02-23 22:21:59 +00:00
if (!state.tabs.length) addRemove.add("");
renderTabs();
2014-02-23 22:21:59 +00:00
bindEvents();
reset();
return "sessions";
};
var reset = function() {
state.tabs.forEach(function(tab) {
tab.detectSyntax();
});
};
command.on("init:startup", init);
command.on("init:restart", reset);
var locationMemory = null;
return {
2014-02-23 22:21:59 +00:00
addFile: addRemove.add,
addDefaultsFile: function(name) {
2014-02-23 22:21:59 +00:00
var tab = addRemove.add(Settings.getAsString(name, true));
tab.syntaxMode = "javascript";
tab.detectSyntax();
tab.fileName = name + ".json";
renderTabs();
},
getAllTabs: function() {
return state.tabs;
},
2013-09-10 16:19:14 +00:00
getCurrent: function() {
return editor.getSession();
},
getTabByIndex: function(index) {
return state.tabs[index];
2013-09-10 16:19:14 +00:00
},
getTabByName: function(name) {
2014-02-23 20:30:30 +00:00
for (var i = 0; i < state.tabs.length; i++) {
if (state.tabs[i].fileName == name) {
return state.tabs[i];
2013-09-10 16:19:14 +00:00
}
}
return null;
},
getFilenames: function() {
return state.tabs.map(function(t) { return t.fileName });
},
2014-02-23 22:21:59 +00:00
setCurrent: switching.raise,
raiseBlurred: switching.raiseBlurred,
saveLocation: function() {
var session = editor.getSession();
var position = editor.getCursorPosition();
locationMemory = {
tab: session,
cursor: position
};
},
restoreLocation: function() {
if (!locationMemory) return;
2014-02-23 22:21:59 +00:00
switching.raise(locationMemory.tab);
editor.moveCursorToPosition(locationMemory.cursor);
2013-09-16 18:58:37 +00:00
},
renderTabs: renderTabs
}
2013-08-20 00:53:03 +00:00
});