mirror of
https://github.com/antos-rde/antosdk-apps.git
synced 2025-07-23 17:29:51 +02:00
alpha version
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
|
||||
class BookletEntry
|
||||
constructor: () ->
|
||||
@name = "Untitled"
|
||||
constructor: (@name) ->
|
||||
@loaded = true
|
||||
|
||||
save: () ->
|
||||
|
||||
@ -13,6 +14,8 @@ class BookletEntry
|
||||
return @name unless @descFile.dirty
|
||||
t = (new RegExp "^\s*#+(.*)\n", "g").exec @descFile.cache
|
||||
return @name unless t and t.length is 2
|
||||
@metaFile.dirty = true if @hasMeta and @metaFile
|
||||
@parent.metaFile.dirty = true if @parent and @parent.hasMeta and @parent.metaFile
|
||||
@name = t[1].trim()
|
||||
|
||||
remove: () ->
|
||||
@ -35,7 +38,10 @@ class BookletEntry
|
||||
|
||||
class BookletFolder extends BookletEntry
|
||||
constructor: (@type, @path, @hasMeta) ->
|
||||
super()
|
||||
super "Untitle"
|
||||
@init()
|
||||
|
||||
init: () ->
|
||||
@cnt = 0
|
||||
@nodes = []
|
||||
@metaFile = "#{@path}/meta.json".asFileHandler() if @hasMeta
|
||||
@ -60,7 +66,34 @@ class BookletFolder extends BookletEntry
|
||||
r()
|
||||
.catch (msg) ->
|
||||
e msg
|
||||
|
||||
read: (folderPath) ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
me.path = folderPath
|
||||
me.init()
|
||||
me.loaded = false
|
||||
me.metaFile.meta (d) ->
|
||||
return e d.error if d.error
|
||||
me.metaFile.read (data) ->
|
||||
# load all child
|
||||
me.name = data.name
|
||||
list = []
|
||||
list[i] = v for v,i in data.entries
|
||||
fn = (l) ->
|
||||
if l.length is 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
|
||||
obj.read(el.path).then () ->
|
||||
fn l
|
||||
.catch (msg) ->
|
||||
fn l
|
||||
|
||||
return fn list
|
||||
, "json"
|
||||
|
||||
size: () ->
|
||||
return @nodes.length
|
||||
@ -81,7 +114,7 @@ class BookletFolder extends BookletEntry
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
list = []
|
||||
list[i] = v for v, i in me.nodes if me.type isnt 'section'
|
||||
list[i] = v for v, i in me.nodes if me.type isnt 'Section'
|
||||
me.mkdir().then () ->
|
||||
fn = (l) ->
|
||||
return r() if l.length is 0
|
||||
@ -98,12 +131,18 @@ class BookletFolder extends BookletEntry
|
||||
return new Promise (r, e) ->
|
||||
return r() unless me.metaFile.dirty
|
||||
entries = []
|
||||
entries[i] = v.path for v,i in me.nodes
|
||||
me.metaFile.cache = entries
|
||||
entries[i] = {name: v.name, path:v.path, type:v.type} for v,i in me.nodes
|
||||
data = {
|
||||
name: me.name,
|
||||
entries: entries,
|
||||
cnt: me.cnt,
|
||||
meta: me.hasMeta
|
||||
}
|
||||
me.metaFile.cache = data
|
||||
me.metaFile.write "object", (d) ->
|
||||
return e d.error if d.error
|
||||
me.metaFile.dirty = false
|
||||
console.log "saved " + me.metaFile.path
|
||||
#console.log "saved " + me.metaFile.path
|
||||
r()
|
||||
|
||||
|
||||
@ -115,7 +154,7 @@ class BookletFolder extends BookletEntry
|
||||
me.descFile.write "text/plain", (d) ->
|
||||
return e d.error if d.error
|
||||
me.descFile.dirty = false
|
||||
console.log "saved " + me.descFile.path
|
||||
#console.log "saved " + me.descFile.path
|
||||
r()
|
||||
.catch (msg) -> e msg
|
||||
|
||||
@ -141,9 +180,9 @@ class BookletFolder extends BookletEntry
|
||||
@
|
||||
|
||||
|
||||
class Book extends BookletFolder
|
||||
class BookletBook extends BookletFolder
|
||||
constructor: (path) ->
|
||||
super 'book', path, true
|
||||
super 'Book', path, true
|
||||
|
||||
save:() ->
|
||||
me = @
|
||||
@ -158,22 +197,22 @@ class Book extends BookletFolder
|
||||
class BookletChapter extends BookletFolder
|
||||
constructor: (book) ->
|
||||
path = "#{book.path}/c_#{book.cnt}"
|
||||
super 'chapter', path, true
|
||||
super 'Chapter', path, true
|
||||
book.add @
|
||||
|
||||
|
||||
class BookletSection extends BookletFolder
|
||||
constructor: (chapter) ->
|
||||
path = "#{chapter.path}/s_#{chapter.cnt}"
|
||||
super "section", path, true
|
||||
super "Section", path, true
|
||||
chapter.add @
|
||||
|
||||
|
||||
class BookletFile extends BookletEntry
|
||||
constructor: (@section) ->
|
||||
super()
|
||||
super "Untitle file"
|
||||
@hasMeta = false
|
||||
@type = "file"
|
||||
@type = "File"
|
||||
@path = "#{@section.path}/f_#{@section.cnt}.md"
|
||||
@descFile = @path.asFileHandler()
|
||||
@section.add @
|
||||
@ -185,9 +224,22 @@ class BookletFile extends BookletEntry
|
||||
me.descFile.write "text/plain", (d) ->
|
||||
return e d.error if d.error
|
||||
me.descFile.dirty = false
|
||||
console.log "saved" + me.descFile.path
|
||||
#console.log "saved" + me.descFile.path
|
||||
r()
|
||||
|
||||
read: (p) ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
me.loaded = false
|
||||
me.treepath = p
|
||||
r()
|
||||
|
||||
toc: () ->
|
||||
@updateName()
|
||||
@
|
||||
@
|
||||
|
||||
NS =
|
||||
Book: BookletBook
|
||||
Chapter: BookletChapter
|
||||
Section: BookletSection
|
||||
File: BookletFile
|
@ -6,10 +6,17 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me = @
|
||||
@tree = @find "toc-ui"
|
||||
@currentToc = undefined
|
||||
@dirty = false
|
||||
@emux = false
|
||||
@on "treeselect", (e) ->
|
||||
return me.reloadEditor() if (me.currentToc is e) or (e is undefined) or (e.treepath is 0)
|
||||
me.open e
|
||||
e.treepath = e.path
|
||||
me.load(e).then ()->
|
||||
me.open e
|
||||
.catch (msg) ->
|
||||
e.loaded = true
|
||||
me.open e
|
||||
me.error __("Error when loading '{0}': {1}", e.name, msg)
|
||||
|
||||
@initEditor()
|
||||
@resizeContent()
|
||||
@ -24,21 +31,22 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
return if me.emux
|
||||
return unless me.currentToc
|
||||
me.currentToc.descFile.dirty = true
|
||||
me.dirty = true
|
||||
|
||||
newChapter: () ->
|
||||
return @error __("No book selected") unless @currentToc and @currentToc.type is "book"
|
||||
return @error __("No book selected") unless @currentToc and @currentToc.type is "Book"
|
||||
ch = new BookletChapter(@book)
|
||||
@displayToc()
|
||||
ch.treepath = ch.path
|
||||
|
||||
newSection: () ->
|
||||
return @error __("No chapter selected") unless @currentToc and @currentToc.type is "chapter"
|
||||
return @error __("No chapter selected") unless @currentToc and @currentToc.type is "Chapter"
|
||||
sec = new BookletSection(@currentToc)
|
||||
@displayToc()
|
||||
sec.treepath = sec.path
|
||||
|
||||
newFile: () ->
|
||||
return @error __("No section selected") unless @currentToc and @currentToc.type is "section"
|
||||
return @error __("No section selected") unless @currentToc and @currentToc.type is "Section"
|
||||
file = new BookletFile(@currentToc)
|
||||
@displayToc()
|
||||
file.treepath = file.path
|
||||
@ -57,25 +65,37 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me.error e
|
||||
fn()
|
||||
|
||||
load: (entry) ->
|
||||
me = @
|
||||
return new Promise (r, e) ->
|
||||
return r() if entry.loaded
|
||||
entry.descFile.meta (d) ->
|
||||
return e d.error if d.error
|
||||
entry.descFile.read (data) ->
|
||||
entry.descFile.cache = data
|
||||
entry.loaded = true
|
||||
entry.descFile.dirty = false
|
||||
r()
|
||||
|
||||
contextMenu: () ->
|
||||
return undefined unless @currentToc
|
||||
switch @currentToc.type
|
||||
when "book"
|
||||
when "Book"
|
||||
return [
|
||||
{ text: __("New chapter"), dataid: "newChapter" },
|
||||
{ text: __("Delete book"), dataid: "delete" }
|
||||
]
|
||||
when "chapter"
|
||||
when "Chapter"
|
||||
return [
|
||||
{ text: __("New section"), dataid: "newSection" },
|
||||
{ text: __("Delete chapter"), dataid: "delete" }
|
||||
]
|
||||
when "section"
|
||||
when "Section"
|
||||
return [
|
||||
{ text: __("New file"), dataid: "newFile" },
|
||||
{ text: __("Delete section"), dataid: "delete" }
|
||||
]
|
||||
when "file"
|
||||
when "File"
|
||||
return [
|
||||
{ text: __("Delete file"), dataid: "delete" }
|
||||
]
|
||||
@ -157,9 +177,17 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me = @
|
||||
switch e
|
||||
when "#{@name}-Open"
|
||||
@openDialog "FileDiaLog", ( d, f ) ->
|
||||
console.log "#{d}/#{f}".asFileHandler()
|
||||
, __("Open file"), { mimes: me.meta().mimes }
|
||||
@checkForDirty () ->
|
||||
me.openDialog "FileDiaLog", ( d, f ) ->
|
||||
me.book = new BookletBook(d)
|
||||
me.book.read(d).then () ->
|
||||
me.book.treepath = me.book.path
|
||||
me.tree.set "selectedItem", undefined
|
||||
me.displayToc()
|
||||
me.notify __("Book loaded")
|
||||
.catch (msg) ->
|
||||
me.error __("Cannot load book: {0}", msg)
|
||||
, __("Open file"), { mimes: ['dir'] }
|
||||
when "#{@name}-New"
|
||||
@openDialog "FileDiaLog", ( d, f ) ->
|
||||
me.newAt "#{d}/#{f}"
|
||||
@ -169,24 +197,34 @@ class Booklet extends this.OS.GUI.BaseApplication
|
||||
me.saveContext() if me.currentToc
|
||||
me.displayToc()
|
||||
me.book.save().then () ->
|
||||
me.dirty = false
|
||||
me.notify __("Book saved")
|
||||
.catch (e) ->
|
||||
me.error __("Can't save the book : {0}", e)
|
||||
|
||||
checkForDirty: (f) ->
|
||||
return f() unless @dirty
|
||||
@_gui.openDialog "YesNoDialog", (d) ->
|
||||
# console.log d
|
||||
if d
|
||||
f()
|
||||
, __("Continue ?"), { text: __("Book is unsaved, you want to continue ?") }
|
||||
|
||||
open: (toc) ->
|
||||
@emux = true
|
||||
@saveContext()
|
||||
@currentToc = toc
|
||||
@reloadEditor()
|
||||
@displayToc()
|
||||
@emux = false
|
||||
me = @
|
||||
me.emux = true
|
||||
me.saveContext()
|
||||
me.currentToc = toc
|
||||
me.reloadEditor()
|
||||
me.displayToc()
|
||||
me.emux = false
|
||||
|
||||
openBook: (metaFile) ->
|
||||
|
||||
|
||||
newAt: (folder) ->
|
||||
@tree.set "selectedItem", false
|
||||
@book = new Book(folder)
|
||||
@book = new BookletBook(folder)
|
||||
@book.treepath = @book.path
|
||||
@currentToc = undefined
|
||||
@reloadEditor()
|
||||
|
Reference in New Issue
Block a user