antosdk-apps/Booklet/build/debug/main.js

807 lines
20 KiB
JavaScript
Raw Normal View History

2019-11-24 20:33:14 +01:00
(function() {
2019-11-29 22:07:31 +01:00
var Booklet, BookletBook, BookletChapter, BookletEntry, BookletFile, BookletFolder, BookletSection, NS;
2019-11-24 20:33:14 +01:00
Booklet = class Booklet extends this.OS.GUI.BaseApplication {
constructor(args) {
super("Booklet", args);
}
main() {
2019-11-26 22:35:25 +01:00
var me;
me = this;
this.tree = this.find("toc-ui");
this.currentToc = void 0;
2019-11-29 22:07:31 +01:00
this.dirty = false;
2019-11-28 23:11:46 +01:00
this.emux = false;
2019-11-28 18:35:55 +01:00
this.on("treeselect", function(e) {
if ((me.currentToc === e) || (e === void 0) || (e.treepath === 0)) {
2019-11-28 23:11:46 +01:00
return me.reloadEditor();
2019-11-28 18:35:55 +01:00
}
2019-11-29 22:07:31 +01:00
e.treepath = e.path;
return me.load(e).then(function() {
return me.open(e);
}).catch(function(msg) {
e.loaded = true;
me.open(e);
return me.error(__("Error when loading '{0}': {1}", e.name, msg));
});
2019-11-26 22:35:25 +01:00
});
2019-11-24 20:33:14 +01:00
this.initEditor();
2019-11-26 22:35:25 +01:00
this.resizeContent();
2019-11-28 18:35:55 +01:00
this.tree.contextmenuHandler = function(e, m) {
2019-11-26 22:35:25 +01:00
var menus;
menus = me.contextMenu();
if (!menus) {
return;
}
m.set("items", menus);
m.set("onmenuselect", function(evt) {
return me[evt.item.data.dataid]();
});
return m.show(e);
};
2019-11-28 18:35:55 +01:00
return this.editor.codemirror.on("change", function() {
2019-11-28 23:11:46 +01:00
if (me.emux) {
return;
}
2019-11-28 18:35:55 +01:00
if (!me.currentToc) {
return;
}
2019-11-29 22:07:31 +01:00
me.currentToc.descFile.dirty = true;
return me.dirty = true;
2019-11-28 18:35:55 +01:00
});
2019-11-26 22:35:25 +01:00
}
newChapter() {
2019-11-28 18:35:55 +01:00
var ch;
2019-11-29 22:07:31 +01:00
if (!(this.currentToc && this.currentToc.type === "Book")) {
2019-11-28 18:35:55 +01:00
return this.error(__("No book selected"));
}
ch = new BookletChapter(this.book);
2019-11-28 23:11:46 +01:00
this.displayToc();
return ch.treepath = ch.path;
2019-11-28 18:35:55 +01:00
}
newSection() {
var sec;
2019-11-29 22:07:31 +01:00
if (!(this.currentToc && this.currentToc.type === "Chapter")) {
2019-11-28 18:35:55 +01:00
return this.error(__("No chapter selected"));
}
sec = new BookletSection(this.currentToc);
2019-11-28 23:11:46 +01:00
this.displayToc();
return sec.treepath = sec.path;
2019-11-28 18:35:55 +01:00
}
newFile() {
var file;
2019-11-29 22:07:31 +01:00
if (!(this.currentToc && this.currentToc.type === "Section")) {
2019-11-28 18:35:55 +01:00
return this.error(__("No section selected"));
}
file = new BookletFile(this.currentToc);
2019-11-28 23:11:46 +01:00
this.displayToc();
return file.treepath = file.path;
}
delete() {
var fn, me;
me = this;
if (!this.currentToc) {
return this.error(__("No entrie select"));
}
fn = function() {
me.currentToc = void 0;
me.displayToc();
return me.reloadEditor();
};
return this.currentToc.remove().then(function() {
me.notify(__("Entrie deleted"));
return fn();
}).catch(function(e) {
me.error(e);
return fn();
});
2019-11-26 22:35:25 +01:00
}
2019-11-29 22:07:31 +01:00
load(entry) {
var me;
me = this;
return new Promise(function(r, e) {
if (entry.loaded) {
return r();
}
return entry.descFile.meta(function(d) {
if (d.error) {
return e(d.error);
}
return entry.descFile.read(function(data) {
entry.descFile.cache = data;
entry.loaded = true;
entry.descFile.dirty = false;
return r();
});
});
});
}
2019-11-26 22:35:25 +01:00
contextMenu() {
if (!this.currentToc) {
return void 0;
}
switch (this.currentToc.type) {
2019-11-29 22:07:31 +01:00
case "Book":
2019-11-26 22:35:25 +01:00
return [
{
text: __("New chapter"),
dataid: "newChapter"
},
{
text: __("Delete book"),
2019-11-28 23:11:46 +01:00
dataid: "delete"
2019-11-26 22:35:25 +01:00
}
];
2019-11-29 22:07:31 +01:00
case "Chapter":
2019-11-26 22:35:25 +01:00
return [
{
text: __("New section"),
dataid: "newSection"
},
{
text: __("Delete chapter"),
2019-11-28 23:11:46 +01:00
dataid: "delete"
2019-11-26 22:35:25 +01:00
}
];
2019-11-29 22:07:31 +01:00
case "Section":
2019-11-26 22:35:25 +01:00
return [
{
text: __("New file"),
dataid: "newFile"
},
{
text: __("Delete section"),
2019-11-28 23:11:46 +01:00
dataid: "delete"
}
];
2019-11-29 22:07:31 +01:00
case "File":
2019-11-28 23:11:46 +01:00
return [
{
text: __("Delete file"),
dataid: "delete"
2019-11-26 22:35:25 +01:00
}
];
}
return void 0;
2019-11-24 20:33:14 +01:00
}
initEditor() {
var markarea, me;
markarea = this.find("markarea");
this.container = this.find("mycontainer");
this.previewOn = false;
this.editormux = false;
me = this;
this.editor = new SimpleMDE({
element: markarea,
autofocus: true,
tabSize: 4,
indentWithTabs: true,
toolbar: [
"bold",
"italic",
"heading",
"|",
"quote",
"code",
"unordered-list",
"ordered-list",
"|",
"link",
"image",
"table",
"horizontal-rule",
"|",
{
name: "preview",
className: "fa fa-eye no-disable",
action: function(e) {
me.previewOn = !me.previewOn;
return SimpleMDE.togglePreview(e);
}
}
]
});
//if(self.previewOn) toggle the highlight
//{
// var container = self._scheme.find(self,"Text")
// .$element.getElementsByClassName("editor-preview");
// if(container.length == 0) return;
// var codes = container[0].getElementsByTagName('pre');
// codes.forEach(function(el){
// hljs.highlightBlock(el);
// });
// //console.log(code);
//}
this.on("hboxchange", function(e) {
return me.resizeContent();
});
this.bindKey("ALT-N", function() {
return me.actionFile(`${me.name}-New`);
});
2019-11-28 18:35:55 +01:00
this.bindKey("ALT-O", function() {
2019-11-24 20:33:14 +01:00
return me.actionFile(`${me.name}-Open`);
});
2019-11-28 18:35:55 +01:00
return this.bindKey("CTRL-S", function() {
return me.actionFile(`${me.name}-Save`);
});
2019-11-24 20:33:14 +01:00
}
2019-11-26 22:35:25 +01:00
reloadEditor() {
2019-11-28 18:35:55 +01:00
if (this.currentToc === void 0) {
this.editor.value("");
return this.scheme.set("apptitle", this.name);
}
this.editor.value(this.currentToc.descFile.cache || "");
return this.scheme.set("apptitle", `Booklet - ${this.currentToc.descFile.path}`);
2019-11-26 22:35:25 +01:00
}
saveContext() {
2019-11-28 18:35:55 +01:00
if (!this.currentToc) {
return;
}
return this.currentToc.descFile.cache = this.editor.value();
2019-11-24 20:33:14 +01:00
}
resizeContent() {
var cheight, children, statusbar, titlebar, toolbar;
children = ($(this.container)).children();
titlebar = (($(this.scheme)).find(".afx-window-top"))[0];
toolbar = children[1];
statusbar = children[4];
cheight = ($(this.scheme)).height() - ($(titlebar)).height() - ($(toolbar)).height() - ($(statusbar)).height() - 40;
return ($(children[2])).css("height", cheight + "px");
}
menu() {
var me, menu;
me = this;
menu = [
{
text: "__(File)",
child: [
{
text: "__(New booklet)",
dataid: `${this.name}-New`,
shortcut: "A-N"
},
{
text: "__(Open a booklet)",
dataid: `${this.name}-Open`,
shortcut: "A-O"
2019-11-28 18:35:55 +01:00
},
{
text: "__(Save a booklet)",
dataid: `${this.name}-Save`,
shortcut: "C-S"
2019-11-24 20:33:14 +01:00
}
],
onmenuselect: function(e) {
return me.actionFile(e.item.data.dataid);
}
}
];
return menu;
}
actionFile(e) {
2019-11-26 22:35:25 +01:00
var me;
me = this;
switch (e) {
case `${this.name}-Open`:
2019-11-29 22:07:31 +01:00
return this.checkForDirty(function() {
return me.openDialog("FileDiaLog", function(d, f) {
me.book = new BookletBook(d);
return me.book.read(d).then(function() {
me.book.treepath = me.book.path;
me.tree.set("selectedItem", void 0);
me.displayToc();
return me.notify(__("Book loaded"));
}).catch(function(msg) {
return me.error(__("Cannot load book: {0}", msg));
});
}, __("Open file"), {
mimes: ['dir']
});
2019-11-26 22:35:25 +01:00
});
case `${this.name}-New`:
return this.openDialog("FileDiaLog", function(d, f) {
2019-11-28 18:35:55 +01:00
return me.newAt(`${d}/${f}`);
2019-11-26 22:35:25 +01:00
}, __("Open file"), {
2019-11-28 18:35:55 +01:00
mimes: ['dir'],
file: {
basename: __("BookName")
}
2019-11-26 22:35:25 +01:00
});
2019-11-28 18:35:55 +01:00
case `${this.name}-Save`:
2019-11-28 23:11:46 +01:00
if (!me.book) {
return;
}
if (me.currentToc) {
me.saveContext();
}
me.displayToc();
return me.book.save().then(function() {
2019-11-29 22:07:31 +01:00
me.dirty = false;
2019-11-28 23:11:46 +01:00
return me.notify(__("Book saved"));
}).catch(function(e) {
return me.error(__("Can't save the book : {0}", e));
});
2019-11-26 22:35:25 +01:00
}
}
2019-11-29 22:07:31 +01:00
checkForDirty(f) {
if (!this.dirty) {
return f();
}
return this._gui.openDialog("YesNoDialog", function(d) {
console.log(d);
if (d) {
return f();
}
}, __("Continue ?"), {
text: __("Book is unsaved, you want to continue ?")
});
}
2019-11-28 18:35:55 +01:00
open(toc) {
2019-11-29 22:07:31 +01:00
var me;
me = this;
me.emux = true;
me.saveContext();
me.currentToc = toc;
me.reloadEditor();
me.displayToc();
return me.emux = false;
2019-11-28 18:35:55 +01:00
}
2019-11-26 22:35:25 +01:00
2019-11-29 22:07:31 +01:00
openBook(metaFile) {}
2019-11-26 22:35:25 +01:00
newAt(folder) {
2019-11-28 18:35:55 +01:00
this.tree.set("selectedItem", false);
2019-11-29 22:07:31 +01:00
this.book = new BookletBook(folder);
2019-11-28 18:35:55 +01:00
this.book.treepath = this.book.path;
this.currentToc = void 0;
this.reloadEditor();
2019-11-26 22:35:25 +01:00
return this.displayToc();
}
displayToc() {
2019-11-28 18:35:55 +01:00
this.book.toc();
return this.tree.set("data", this.book);
2019-11-24 20:33:14 +01:00
}
};
Booklet.dependencies = ["mde/simplemde.min"];
this.OS.register("Booklet", Booklet);
BookletEntry = class BookletEntry {
2019-11-29 22:07:31 +01:00
constructor(name) {
this.name = name;
this.loaded = true;
2019-11-24 20:33:14 +01:00
}
save() {}
remove() {}
toc() {}
2019-11-28 18:35:55 +01:00
updateName() {
var t;
if (!this.descFile.dirty) {
return this.name;
}
2019-11-28 23:11:46 +01:00
t = (new RegExp("^\s*#+(.*)\n", "g")).exec(this.descFile.cache);
2019-11-28 18:35:55 +01:00
if (!(t && t.length === 2)) {
return this.name;
2019-11-26 22:35:25 +01:00
}
2019-11-29 22:07:31 +01:00
if (this.hasMeta && this.metaFile) {
this.metaFile.dirty = true;
}
if (this.parent && this.parent.hasMeta && this.parent.metaFile) {
this.parent.metaFile.dirty = true;
}
2019-11-28 18:35:55 +01:00
return this.name = t[1].trim();
2019-11-26 22:35:25 +01:00
}
2019-11-28 23:11:46 +01:00
remove() {
var me;
me = this;
return new Promise(function(r, e) {
var f;
f = me.path.asFileHandler();
return f.meta(function(d) {
if (d.error) {
if (!me.parent) {
return r();
}
return me.parent.removeChild(me).then(function() {
return r();
}).catch(function(msg) {
return e(msg);
});
} else {
return f.remove(function(ret) {
if (ret.error) {
return e(ret.error);
}
if (!me.parent) {
return r();
}
return me.parent.removeChild(me).then(function() {
return r();
}).catch(function(msg) {
return e(msg);
});
});
}
});
});
}
2019-11-24 20:33:14 +01:00
};
BookletFolder = class BookletFolder extends BookletEntry {
2019-11-28 18:35:55 +01:00
constructor(type, path1, hasMeta) {
2019-11-29 22:07:31 +01:00
super("Untitle");
2019-11-28 18:35:55 +01:00
this.type = type;
this.path = path1;
this.hasMeta = hasMeta;
2019-11-29 22:07:31 +01:00
this.init();
}
init() {
2019-11-28 23:11:46 +01:00
this.cnt = 0;
2019-11-28 18:35:55 +01:00
this.nodes = [];
if (this.hasMeta) {
this.metaFile = `${this.path}/meta.json`.asFileHandler();
}
2019-11-29 22:07:31 +01:00
return this.descFile = `${this.path}/INTRO.md`.asFileHandler();
2019-11-24 20:33:14 +01:00
}
2019-11-28 18:35:55 +01:00
add(chap) {
chap.parent = this;
2019-11-28 23:11:46 +01:00
this.nodes.push(chap);
if (this.hasMeta && this.metaFile) {
this.metaFile.dirty = true;
}
if (chap.metaFile && chap.hasMeta) {
chap.metaFile.dirty = true;
}
return this.cnt = this.cnt + 1;
}
removeChild(child) {
var me;
me = this;
//v.treepath = v.path for v in @nodes if @nodes
return new Promise(function(r, e) {
me.nodes.splice(me.nodes.indexOf(child), 1);
if (!(me.hasMeta && me.metaFile)) {
//if me.nodes.includes child
return r();
}
me.metaFile.dirty = true;
return me.updateMeta().then(function() {
return r();
}).catch(function(msg) {
return e(msg);
});
});
2019-11-24 20:33:14 +01:00
}
2019-11-29 22:07:31 +01:00
read(folderPath) {
var me;
me = this;
return new Promise(function(r, e) {
me.path = folderPath;
me.init();
me.loaded = false;
return me.metaFile.meta(function(d) {
if (d.error) {
return e(d.error);
}
return me.metaFile.read(function(data) {
var fn, i, j, len, list, ref, v;
// load all child
me.name = data.name;
list = [];
ref = data.entries;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
list[i] = v;
}
fn = function(l) {
var el, obj;
if (l.length === 0) {
me.cnt = data.cnt;
return r();
}
el = (l.splice(0, 1))[0];
console.log("create", el.type);
obj = new NS[el.type](me);
obj.name = el.name;
return obj.read(el.path).then(function() {
return fn(l);
}).catch(function(msg) {
return fn(l);
});
};
return fn(list);
}, "json");
});
});
}
2019-11-24 20:33:14 +01:00
size() {
2019-11-28 18:35:55 +01:00
return this.nodes.length;
2019-11-24 20:33:14 +01:00
}
2019-11-28 18:35:55 +01:00
mkdir() {
var me;
2019-11-26 22:35:25 +01:00
me = this;
2019-11-28 18:35:55 +01:00
return new Promise(function(r, e) {
var dir;
dir = me.path.asFileHandler();
return dir.meta(function(d) {
var bname;
if (!d.error) {
return r();
}
bname = dir.basename;
dir = dir.parent().asFileHandler();
return dir.mk(bname, function(result) {
if (result.error) {
e(__("Error when create directory: {0}", result.error));
2019-11-26 22:35:25 +01:00
}
2019-11-28 18:35:55 +01:00
return r();
2019-11-26 22:35:25 +01:00
});
});
2019-11-28 18:35:55 +01:00
});
2019-11-26 22:35:25 +01:00
}
2019-11-28 18:35:55 +01:00
mkdirs() {
var me;
me = this;
return new Promise(function(r, e) {
var i, j, len, list, ref, v;
list = [];
2019-11-29 22:07:31 +01:00
if (me.type !== 'Section') {
2019-11-28 18:35:55 +01:00
ref = me.nodes;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
2019-11-24 20:33:14 +01:00
v = ref[i];
2019-11-28 18:35:55 +01:00
list[i] = v;
2019-11-24 20:33:14 +01:00
}
2019-11-28 18:35:55 +01:00
}
return me.mkdir().then(function() {
var fn;
fn = function(l) {
var el;
if (l.length === 0) {
return r();
}
el = (l.splice(0, 1))[0];
return el.mkdirs().then(function() {
return fn(l);
2019-11-28 23:11:46 +01:00
}).catch(function(msg) {
return e(msg);
2019-11-28 18:35:55 +01:00
});
};
return fn(list);
2019-11-28 23:11:46 +01:00
}).catch(function(msg) {
return e(msg);
});
});
}
updateMeta() {
var me;
me = this;
return new Promise(function(r, e) {
2019-11-29 22:07:31 +01:00
var data, entries, i, j, len, ref, v;
2019-11-28 23:11:46 +01:00
if (!me.metaFile.dirty) {
return r();
}
entries = [];
ref = me.nodes;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
2019-11-29 22:07:31 +01:00
entries[i] = {
name: v.name,
path: v.path,
type: v.type
};
2019-11-28 23:11:46 +01:00
}
2019-11-29 22:07:31 +01:00
data = {
name: me.name,
entries: entries,
cnt: me.cnt,
meta: me.hasMeta
};
me.metaFile.cache = data;
2019-11-28 23:11:46 +01:00
return me.metaFile.write("object", function(d) {
if (d.error) {
return e(d.error);
}
me.metaFile.dirty = false;
console.log("saved " + me.metaFile.path);
return r();
});
});
}
update() {
var me;
me = this;
return new Promise(function(r, e) {
return me.updateMeta().then(function() {
if (!me.descFile.dirty) {
return r();
}
return me.descFile.write("text/plain", function(d) {
if (d.error) {
return e(d.error);
}
me.descFile.dirty = false;
console.log("saved " + me.descFile.path);
return r();
});
}).catch(function(msg) {
return e(msg);
2019-11-28 18:35:55 +01:00
});
});
2019-11-24 20:33:14 +01:00
}
2019-11-28 23:11:46 +01:00
updateAll() {
var me;
me = this;
return new Promise(function(r, e) {
var i, j, len, list, ref, v;
list = [];
ref = me.nodes;
for (i = j = 0, len = ref.length; j < len; i = ++j) {
v = ref[i];
list[i] = v;
}
return me.update().then(function() {
var fn;
fn = function(l) {
var el;
if (l.length === 0) {
return r();
}
el = (l.splice(0, 1))[0];
return el.updateAll().then(function() {
return fn(l);
}).catch(function(msg) {
return e(msg);
});
};
return fn(list);
}).catch(function(msg) {
return e(msg);
});
2019-11-28 18:35:55 +01:00
});
2019-11-24 20:33:14 +01:00
}
toc() {
2019-11-28 18:35:55 +01:00
var j, len, ref, v;
this.updateName();
ref = this.nodes;
for (j = 0, len = ref.length; j < len; j++) {
v = ref[j];
v.toc();
2019-11-26 22:35:25 +01:00
}
2019-11-28 18:35:55 +01:00
return this;
2019-11-26 22:35:25 +01:00
}
2019-11-24 20:33:14 +01:00
};
2019-11-29 22:07:31 +01:00
BookletBook = class BookletBook extends BookletFolder {
2019-11-28 18:35:55 +01:00
constructor(path) {
2019-11-29 22:07:31 +01:00
super('Book', path, true);
2019-11-24 20:33:14 +01:00
}
2019-11-28 23:11:46 +01:00
save() {
var me;
me = this;
return new Promise(function(r, e) {
return me.mkdirs().then(function() {
return me.updateAll().then(function() {
return r();
}).catch(function(msg) {
return e(msg);
});
}).catch(function(msg) {
return e(msg);
});
});
}
2019-11-28 18:35:55 +01:00
};
2019-11-24 20:33:14 +01:00
2019-11-28 18:35:55 +01:00
BookletChapter = class BookletChapter extends BookletFolder {
constructor(book) {
var path;
2019-11-28 23:11:46 +01:00
path = `${book.path}/c_${book.cnt}`;
2019-11-29 22:07:31 +01:00
super('Chapter', path, true);
2019-11-28 18:35:55 +01:00
book.add(this);
2019-11-24 20:33:14 +01:00
}
2019-11-28 18:35:55 +01:00
};
2019-11-26 22:35:25 +01:00
2019-11-28 18:35:55 +01:00
BookletSection = class BookletSection extends BookletFolder {
constructor(chapter) {
var path;
2019-11-28 23:11:46 +01:00
path = `${chapter.path}/s_${chapter.cnt}`;
2019-11-29 22:07:31 +01:00
super("Section", path, true);
2019-11-28 18:35:55 +01:00
chapter.add(this);
2019-11-24 20:33:14 +01:00
}
};
BookletFile = class BookletFile extends BookletEntry {
constructor(section) {
2019-11-29 22:07:31 +01:00
super("Untitle file");
2019-11-24 20:33:14 +01:00
this.section = section;
2019-11-28 23:11:46 +01:00
this.hasMeta = false;
2019-11-29 22:07:31 +01:00
this.type = "File";
2019-11-28 23:11:46 +01:00
this.path = `${this.section.path}/f_${this.section.cnt}.md`;
2019-11-26 22:35:25 +01:00
this.descFile = this.path.asFileHandler();
2019-11-28 23:11:46 +01:00
this.section.add(this);
2019-11-24 20:33:14 +01:00
}
2019-11-28 23:11:46 +01:00
updateAll() {
var me;
2019-11-26 22:35:25 +01:00
me = this;
2019-11-28 23:11:46 +01:00
return new Promise(function(r, e) {
if (!me.descFile.dirty) {
return r();
}
return me.descFile.write("text/plain", function(d) {
if (d.error) {
return e(d.error);
2019-11-28 18:35:55 +01:00
}
2019-11-28 23:11:46 +01:00
me.descFile.dirty = false;
console.log("saved" + me.descFile.path);
return r();
2019-11-28 18:35:55 +01:00
});
2019-11-28 23:11:46 +01:00
});
2019-11-26 22:35:25 +01:00
}
2019-11-24 20:33:14 +01:00
2019-11-29 22:07:31 +01:00
read(p) {
var me;
me = this;
return new Promise(function(r, e) {
me.loaded = false;
me.treepath = p;
return r();
});
}
2019-11-24 20:33:14 +01:00
toc() {
2019-11-28 18:35:55 +01:00
this.updateName();
return this;
2019-11-24 20:33:14 +01:00
}
};
2019-11-29 22:07:31 +01:00
NS = {
Book: BookletBook,
Chapter: BookletChapter,
Section: BookletSection,
File: BookletFile
};
2019-11-24 20:33:14 +01:00
}).call(this);