forked from cory/tildefriends
Used ui.fileList in wiki.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3194 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
@ -1,39 +1,69 @@
|
||||
"use strict";
|
||||
|
||||
function fileList(title, prefix, editCallback) {
|
||||
function fileList(settings) {
|
||||
|
||||
terminal.setEcho(false);
|
||||
terminal.clear();
|
||||
terminal.print(title);
|
||||
terminal.print(settings.title);
|
||||
if (core.user.credentials.permissions.authenticated) {
|
||||
terminal.print({command: "new"});
|
||||
}
|
||||
|
||||
let prefix = settings.prefix || "";
|
||||
|
||||
let makeSaveCallback = function(oldName, oldValue) {
|
||||
return function(newName, newValue) {
|
||||
print(newName, " ", newValue);
|
||||
return database.set(prefix + newName, newValue);
|
||||
}
|
||||
}
|
||||
let backCallback = function() {
|
||||
return fileList(title, prefix, editCallback);
|
||||
terminal.setHash("");
|
||||
return fileList(settings);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return database.getAll().then(function(entries) {
|
||||
terminal.readLine().then(function(input) {
|
||||
if (input == "new") {
|
||||
editCallback("untitled", "", makeSaveCallback("untitled", ""), backCallback);
|
||||
terminal.setHash(name);
|
||||
settings.edit({
|
||||
name: "untitled",
|
||||
value: "",
|
||||
save: makeSaveCallback("untitled", ""),
|
||||
back: backCallback
|
||||
});
|
||||
} else if (input.substring(0, "open:".length) == "open:") {
|
||||
var title = input.substring("open:".length + prefix.length);
|
||||
database.get(prefix + title).then(function(contents) {
|
||||
editCallback(title, contents, makeSaveCallback(title, contents), backCallback);
|
||||
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
|
||||
});
|
||||
});
|
||||
} else if (input == "home") {
|
||||
filelist(title, prefix, editCallback);
|
||||
filelist(settings);
|
||||
} else if (input.substring(0, "delete:".length) == "delete:") {
|
||||
terminal.clear();
|
||||
var title = input.substring(7);
|
||||
terminal.print("Are you sure you want to delete '", title.substring(prefix.length), "'?");
|
||||
terminal.print({command: "confirmDelete:" + title, value: "delete it"});
|
||||
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"});
|
||||
terminal.print({command: "home", value: "cancel"});
|
||||
terminal.readLine().then(function(input) {
|
||||
if (input == "home") {
|
||||
@ -66,26 +96,30 @@ function fileList(title, prefix, editCallback) {
|
||||
});
|
||||
}
|
||||
|
||||
function testEdit(name, value, save, back) {
|
||||
function testEdit(event) {
|
||||
terminal.clear();
|
||||
terminal.print("testEdit ", name, " ", value);
|
||||
terminal.print("testEdit ", event.name, " ", event.value);
|
||||
terminal.print({command: "++"});
|
||||
terminal.print({command: "--"});
|
||||
terminal.print({command: "back"});
|
||||
terminal.readLine().then(function(command) {
|
||||
if (command == "back") {
|
||||
terminal.print("calling back");
|
||||
back();
|
||||
event.back();
|
||||
} else if (command == "++") {
|
||||
save(name, (parseInt(value || "0") + 1).toString()).then(back);
|
||||
event.save(event.name, (parseInt(event.value || "0") + 1).toString()).then(event.back);
|
||||
} else if (command == "--") {
|
||||
save(name, (parseInt(value || "0") - 1).toString()).then(back);
|
||||
event.save(event.name, (parseInt(event.value || "0") - 1).toString()).then(event.back);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (imports.terminal) {
|
||||
fileList("Test File List", "fileList_", testEdit);
|
||||
fileList({
|
||||
title: "Test File List",
|
||||
prefix: "fileList_",
|
||||
edit: testEdit,
|
||||
});
|
||||
}
|
||||
|
||||
exports.fileList = fileList;
|
Reference in New Issue
Block a user