2013-09-01 18:09:09 +00:00
|
|
|
define([
|
2014-02-23 20:28:41 +00:00
|
|
|
"sessions/state",
|
2014-02-23 22:21:59 +00:00
|
|
|
"sessions/addRemove",
|
|
|
|
"sessions/switching",
|
|
|
|
"sessions/binding",
|
2013-09-01 18:09:09 +00:00
|
|
|
"editor",
|
|
|
|
"command",
|
2014-01-21 06:10:06 +00:00
|
|
|
"storage/settingsProvider",
|
2013-09-04 19:45:51 +00:00
|
|
|
"aceBindings"
|
2013-09-24 18:03:39 +00:00
|
|
|
],
|
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-02-23 20:28:41 +00:00
|
|
|
|
2014-01-21 02:10:35 +00:00
|
|
|
*/
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-13 18:13:00 +00:00
|
|
|
var syntax = document.find(".syntax");
|
2013-12-11 02:20:01 +00:00
|
|
|
|
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() {
|
2013-09-01 18:09:09 +00:00
|
|
|
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 = "";
|
2014-02-23 20:28:41 +00:00
|
|
|
state.tabs.forEach(function(tab, i) {
|
2014-01-21 02:03:18 +00:00
|
|
|
var element = tab.render(i);
|
2013-08-22 06:35:14 +00:00
|
|
|
if (tab === current) {
|
2014-01-12 02:22:32 +00:00
|
|
|
element.addClass("active");
|
2013-08-22 06:35:14 +00:00
|
|
|
}
|
2014-01-12 02:22:32 +00:00
|
|
|
tabContainer.append(element);
|
2013-08-21 00:35:25 +00:00
|
|
|
});
|
2013-10-17 00:06:32 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
//wait for render before triggering the enter animation
|
2014-01-12 02:22:32 +00:00
|
|
|
tabContainer.findAll(".enter").forEach(function(element) { element.removeClass("enter") });
|
2013-10-17 00:06:32 +00:00
|
|
|
});
|
2014-02-22 04:46:28 +00:00
|
|
|
command.fire("session:retain-tabs");
|
2013-09-04 22:01:32 +00:00
|
|
|
};
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-10-07 06:21:04 +00:00
|
|
|
command.on("session:render", renderTabs);
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
var init = function() {
|
2014-01-21 06:10:06 +00:00
|
|
|
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("");
|
2013-10-07 06:03:14 +00:00
|
|
|
renderTabs();
|
2014-02-23 22:21:59 +00:00
|
|
|
bindEvents();
|
2013-09-16 18:16:06 +00:00
|
|
|
reset();
|
2014-01-12 23:35:30 +00:00
|
|
|
return "sessions";
|
2013-09-02 01:26:22 +00:00
|
|
|
};
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
var reset = function() {
|
2014-02-23 20:28:41 +00:00
|
|
|
state.tabs.forEach(function(tab) {
|
2014-01-21 06:10:06 +00:00
|
|
|
tab.detectSyntax();
|
|
|
|
});
|
2013-09-02 01:26:22 +00:00
|
|
|
};
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-02 01:26:22 +00:00
|
|
|
command.on("init:startup", init);
|
|
|
|
command.on("init:restart", reset);
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-11 03:34:28 +00:00
|
|
|
var locationMemory = null;
|
2013-12-11 02:20:01 +00:00
|
|
|
|
2013-09-03 15:47:59 +00:00
|
|
|
return {
|
2014-02-23 22:21:59 +00:00
|
|
|
addFile: addRemove.add,
|
2013-09-16 18:16:06 +00:00
|
|
|
addDefaultsFile: function(name) {
|
2014-02-23 22:21:59 +00:00
|
|
|
var tab = addRemove.add(Settings.getAsString(name, true));
|
2014-01-21 06:10:06 +00:00
|
|
|
tab.syntaxMode = "javascript";
|
|
|
|
tab.detectSyntax();
|
|
|
|
tab.fileName = name + ".json";
|
|
|
|
renderTabs();
|
2013-09-16 18:16:06 +00:00
|
|
|
},
|
2013-09-21 19:21:25 +00:00
|
|
|
getAllTabs: function() {
|
2014-02-23 20:28:41 +00:00
|
|
|
return state.tabs;
|
2013-09-21 19:21:25 +00:00
|
|
|
},
|
2013-09-10 16:19:14 +00:00
|
|
|
getCurrent: function() {
|
|
|
|
return editor.getSession();
|
|
|
|
},
|
|
|
|
getTabByIndex: function(index) {
|
2014-02-23 20:28:41 +00:00
|
|
|
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++) {
|
2014-02-23 20:28:41 +00:00
|
|
|
if (state.tabs[i].fileName == name) {
|
|
|
|
return state.tabs[i];
|
2013-09-10 16:19:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
getFilenames: function() {
|
2014-02-23 20:28:41 +00:00
|
|
|
return state.tabs.map(function(t) { return t.fileName });
|
2013-09-11 03:34:28 +00:00
|
|
|
},
|
2014-02-23 22:21:59 +00:00
|
|
|
setCurrent: switching.raise,
|
|
|
|
raiseBlurred: switching.raiseBlurred,
|
2013-09-11 03:34:28 +00:00
|
|
|
saveLocation: function() {
|
|
|
|
var session = editor.getSession();
|
|
|
|
var position = editor.getCursorPosition();
|
|
|
|
locationMemory = {
|
|
|
|
tab: session,
|
|
|
|
cursor: position
|
2013-09-11 06:15:04 +00:00
|
|
|
};
|
2013-09-11 03:34:28 +00:00
|
|
|
},
|
|
|
|
restoreLocation: function() {
|
2013-09-11 06:15:04 +00:00
|
|
|
if (!locationMemory) return;
|
2014-02-23 22:21:59 +00:00
|
|
|
switching.raise(locationMemory.tab);
|
2013-09-11 03:34:28 +00:00
|
|
|
editor.moveCursorToPosition(locationMemory.cursor);
|
2013-09-16 18:58:37 +00:00
|
|
|
},
|
|
|
|
renderTabs: renderTabs
|
2013-09-03 15:47:59 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 00:53:03 +00:00
|
|
|
});
|