2016-04-09 12:42:15 -04:00
|
|
|
"use strict";
|
|
|
|
|
2016-04-09 14:24:16 -04:00
|
|
|
function fileList(settings) {
|
|
|
|
|
2016-04-09 12:42:15 -04:00
|
|
|
terminal.setEcho(false);
|
|
|
|
terminal.clear();
|
2016-04-09 14:24:16 -04:00
|
|
|
terminal.print(settings.title);
|
2016-04-09 12:42:15 -04:00
|
|
|
if (core.user.credentials.permissions.authenticated) {
|
|
|
|
terminal.print({command: "new"});
|
|
|
|
}
|
|
|
|
|
2016-04-09 14:24:16 -04:00
|
|
|
let prefix = settings.prefix || "";
|
|
|
|
|
2016-04-09 12:42:15 -04:00
|
|
|
let makeSaveCallback = function(oldName, oldValue) {
|
|
|
|
return function(newName, newValue) {
|
|
|
|
return database.set(prefix + newName, newValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let backCallback = function() {
|
2016-04-09 14:24:16 -04:00
|
|
|
terminal.setHash("");
|
|
|
|
return fileList(settings);
|
2016-04-09 12:42:15 -04:00
|
|
|
}
|
|
|
|
|
2016-04-09 14:24:16 -04:00
|
|
|
let hashChange = function(event) {
|
|
|
|
var name = event.hash.substring(1);
|
|
|
|
if (name.length) {
|
|
|
|
database.get(prefix + name).then(function(value) {
|
|
|
|
settings.edit({
|
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
save: makeSaveCallback(name, value),
|
|
|
|
back: backCallback,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
core.register("hashChange", hashChange);
|
|
|
|
|
2016-04-09 12:42:15 -04:00
|
|
|
return database.getAll().then(function(entries) {
|
|
|
|
terminal.readLine().then(function(input) {
|
|
|
|
if (input == "new") {
|
2016-04-09 14:24:16 -04:00
|
|
|
terminal.setHash(name);
|
|
|
|
settings.edit({
|
|
|
|
name: "untitled",
|
|
|
|
value: "",
|
|
|
|
save: makeSaveCallback("untitled", ""),
|
|
|
|
back: backCallback
|
|
|
|
});
|
2016-04-09 12:42:15 -04:00
|
|
|
} else if (input.substring(0, "open:".length) == "open:") {
|
2016-04-09 14:24:16 -04:00
|
|
|
let name = input.substring("open:".length + prefix.length);
|
|
|
|
terminal.setHash(name);
|
|
|
|
database.get(prefix + name).then(function(contents) {
|
|
|
|
settings.edit({
|
|
|
|
name: name,
|
|
|
|
value: contents,
|
|
|
|
save: makeSaveCallback(name, contents),
|
|
|
|
back: backCallback
|
|
|
|
});
|
2016-04-09 12:42:15 -04:00
|
|
|
});
|
|
|
|
} else if (input == "home") {
|
2016-04-09 14:24:16 -04:00
|
|
|
filelist(settings);
|
2016-04-09 12:42:15 -04:00
|
|
|
} else if (input.substring(0, "delete:".length) == "delete:") {
|
|
|
|
terminal.clear();
|
2016-04-09 14:24:16 -04:00
|
|
|
var name = input.substring(7);
|
|
|
|
terminal.print("Are you sure you want to delete '", name.substring(prefix.length), "'?");
|
|
|
|
terminal.print({command: "confirmDelete:" + name, value: "delete it"});
|
2016-04-09 12:42:15 -04:00
|
|
|
terminal.print({command: "home", value: "cancel"});
|
|
|
|
terminal.readLine().then(function(input) {
|
|
|
|
if (input == "home") {
|
|
|
|
backCallback();
|
|
|
|
} else if (input.substring(0, "confirmDelete:".length) == "confirmDelete:") {
|
|
|
|
var title = input.substring("confirmDelete:".length);
|
|
|
|
return database.remove(title).then(backCallback);
|
|
|
|
} else {
|
|
|
|
backCallback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (var i = 0; i < entries.length; i++) {
|
|
|
|
if (entries[i].substring(0, prefix.length) == prefix) {
|
|
|
|
if (core.user.credentials.permissions.authenticated) {
|
|
|
|
terminal.print(
|
|
|
|
"* ",
|
|
|
|
{style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i].substring(prefix.length)}},
|
|
|
|
" (",
|
|
|
|
{command: "delete:" + entries[i], value: "x"},
|
|
|
|
")");
|
|
|
|
} else {
|
|
|
|
terminal.print(
|
|
|
|
"* ",
|
|
|
|
{style: "font-weight: bold", value: {command: "open:" + entries[i], value: entries[i].substring(prefix.length)}});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-09 14:24:16 -04:00
|
|
|
function testEdit(event) {
|
2016-04-09 12:42:15 -04:00
|
|
|
terminal.clear();
|
2016-04-09 14:24:16 -04:00
|
|
|
terminal.print("testEdit ", event.name, " ", event.value);
|
2016-04-09 12:42:15 -04:00
|
|
|
terminal.print({command: "++"});
|
|
|
|
terminal.print({command: "--"});
|
|
|
|
terminal.print({command: "back"});
|
|
|
|
terminal.readLine().then(function(command) {
|
|
|
|
if (command == "back") {
|
|
|
|
terminal.print("calling back");
|
2016-04-09 14:24:16 -04:00
|
|
|
event.back();
|
2016-04-09 12:42:15 -04:00
|
|
|
} else if (command == "++") {
|
2016-04-09 14:24:16 -04:00
|
|
|
event.save(event.name, (parseInt(event.value || "0") + 1).toString()).then(event.back);
|
2016-04-09 12:42:15 -04:00
|
|
|
} else if (command == "--") {
|
2016-04-09 14:24:16 -04:00
|
|
|
event.save(event.name, (parseInt(event.value || "0") - 1).toString()).then(event.back);
|
2016-04-09 12:42:15 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imports.terminal) {
|
2016-04-09 14:24:16 -04:00
|
|
|
fileList({
|
|
|
|
title: "Test File List",
|
|
|
|
prefix: "fileList_",
|
|
|
|
edit: testEdit,
|
|
|
|
});
|
2016-04-09 12:42:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.fileList = fileList;
|