Caret/js/sessions.js

159 lines
4.3 KiB
JavaScript
Raw Normal View History

2013-08-23 23:03:46 +00:00
define(["editor", "command", "file", "json!config/ace.json"], function(editor, command, File, cfg) {
2013-08-20 00:53:03 +00:00
/*
- test loading a session from an external file
- test loading two sessions, switching between them
- get tabs up and running
- get/set/display theme per tab
- expose API for creating/get/set/dropSession
- retain file handles after shutdown
*/
2013-08-21 00:35:25 +00:00
var tabs = [];
2013-08-22 06:35:14 +00:00
var Session = ace.require("ace/edit_session").EditSession;
2013-08-21 00:35:25 +00:00
var renderTabs = function() {
var tabContainer = document.querySelector(".tabs");
var contents = "";
2013-08-22 06:35:14 +00:00
var current = editor.getSession();
tabContainer.innerHTML = "";
2013-08-21 00:35:25 +00:00
tabs.forEach(function(tab, index) {
2013-08-22 06:35:14 +00:00
var span = document.createElement("span");
span.setAttribute("command", "session:raise-tab");
span.setAttribute("argument", index);
if (tab === current) {
span.className = "active";
}
span.innerHTML = tab.fileName + (tab.modified ? " *" : "");
var close = document.createElement("a");
close.innerHTML = "×";
close.className = "close";
close.setAttribute("command", "session:close-tab");
close.setAttribute("argument", index);
span.append(close);
tabContainer.append(span);
2013-08-21 00:35:25 +00:00
});
}
2013-09-01 04:21:21 +00:00
var setTabSyntax = function(tab) {
tab.setTabSize(2);
if (tab.file) {
var extension = tab.file.entry.name.split(".").pop();
console.log(extension);
}
};
var saveFile = function(as) {
if (session.modified || as) {
var content = this.getValue();
if (!this.file) {
var file = this.file = new File();
file.open("save", function() {
file.write(content);
session.file = file;
session.fileName = file.entry.name;
renderTabs();
});
} else {
this.file.write(content);
}
this.modified = false;
renderTabs();
}
};
2013-08-21 00:47:07 +00:00
var addTab = function(contents, file) {
2013-08-22 06:35:14 +00:00
contents = contents || "";
var current = editor.getSession();
var session;
//reuse tab if opening a file into an empty tab
if (file && !current.file && !current.modified) {
2013-08-22 06:35:14 +00:00
session = current;
session.setValue(contents);
} else {
session = new Session(contents);
tabs.push(session);
editor.setSession(session);
}
session.fileName = file ? file.entry.name : "untitled.txt";
2013-08-21 00:47:07 +00:00
session.file = file;
2013-09-01 04:21:21 +00:00
setTabSyntax(session);
session.save = saveFile;
2013-08-22 06:35:14 +00:00
session.modified = false;
session.once("change", function() {
session.modified = true;
renderTabs();
});
editor.focus();
2013-08-21 00:35:25 +00:00
renderTabs();
};
2013-08-21 00:47:07 +00:00
var removeTab = function(index) {
2013-08-31 20:23:41 +00:00
if (!index) {
index = tabs.indexOf(editor.getSession());
}
2013-08-22 06:35:14 +00:00
tabs = tabs.filter(function(tab, i) {
if (i == index) {
//tab.save();
return false;
}
return true;
2013-08-31 20:23:41 +00:00
});
2013-08-22 06:35:14 +00:00
if (tabs.length == 0) {
return addTab();
}
2013-08-31 20:23:41 +00:00
return raiseTab(index - 1);
2013-08-21 00:47:07 +00:00
};
2013-08-21 00:35:25 +00:00
var raiseTab = function(index) {
var tab = tabs[index];
editor.setSession(tab);
2013-08-22 06:35:14 +00:00
renderTabs();
editor.focus();
2013-08-21 00:47:07 +00:00
};
2013-08-21 00:35:25 +00:00
2013-09-01 04:21:21 +00:00
var switchTab = function(shift) {
shift = shift || 1;
var current = editor.getSession();
var currentIndex = tabs.indexOf(current);
var shifted = (currentIndex + shift) % tabs.length;
raiseTab(shifted);
}
2013-08-21 00:35:25 +00:00
var openFile = function() {
var f = new File();
f.open(function(file) {
f.read(function(err, data) {
2013-08-22 06:35:14 +00:00
addTab(data, file);
2013-08-21 00:35:25 +00:00
});
});
2013-09-01 04:21:21 +00:00
};
2013-08-21 00:35:25 +00:00
2013-08-22 06:35:14 +00:00
addTab("");
renderTabs();
2013-08-23 23:03:46 +00:00
var syntax = document.querySelector(".syntax");
cfg.modes.forEach(function(mode) {
var option = document.createElement("option");
option.innerHTML = mode.label;
option.value = mode.name;
syntax.append(option);
});
syntax.value = "javascript";
command.on("session:syntax", function(mode) {
editor.getSession().setMode("ace/mode/" + mode);
});
2013-08-22 06:35:14 +00:00
command.on("session:new-file", function() { addTab() });
2013-08-21 00:35:25 +00:00
command.on("session:open-file", openFile);
command.on("session:raise-tab", raiseTab);
2013-08-22 06:35:14 +00:00
command.on("session:save-file", function() { editor.getSession().save() });
command.on("session:save-file-as", function() { editor.getSession().save(true) });
command.on("session:close-tab", removeTab);
2013-09-01 04:21:21 +00:00
command.on("session:change-tab", switchTab);
2013-08-20 00:53:03 +00:00
});