antosdk-apps/Booklet/coffees/common.coffee

112 lines
3.0 KiB
CoffeeScript
Raw Normal View History

2019-11-24 20:33:14 +01:00
class BookletEntry
2019-11-26 22:35:25 +01:00
constructor: () ->
2019-11-28 18:35:55 +01:00
@name = "Untitled"
2019-11-24 20:33:14 +01:00
save: () ->
remove: () ->
toc: () ->
2019-11-28 18:35:55 +01:00
2019-11-26 22:35:25 +01:00
2019-11-28 18:35:55 +01:00
updateName:() ->
return @name unless @descFile.dirty
t = (new RegExp "^\s*#+(.*)[\n,$]", "g").exec @descFile.cache
return @name unless t and t.length is 2
@name = t[1].trim()
2019-11-24 20:33:14 +01:00
class BookletFolder extends BookletEntry
2019-11-28 18:35:55 +01:00
constructor: (@type, @path, @hasMeta) ->
2019-11-26 22:35:25 +01:00
super()
2019-11-28 18:35:55 +01:00
@nodes = []
@metaFile = "#{@path}/meta.json".asFileHandler() if @hasMeta
@descFile = "#{@path}/#{@type}.md".asFileHandler()
2019-11-24 20:33:14 +01:00
2019-11-28 18:35:55 +01:00
add: (chap) ->
chap.parent = @
@nodes.push chap
2019-11-24 20:33:14 +01:00
size: () ->
2019-11-28 18:35:55 +01:00
return @nodes.length
2019-11-26 20:23:28 +01:00
2019-11-28 18:35:55 +01:00
mkdir: () ->
2019-11-26 20:23:28 +01:00
me = @
2019-11-28 18:35:55 +01:00
console.log "making:" + me.path
return new Promise (r, e) ->
dir = me.path.asFileHandler()
dir.meta (d) ->
return r() unless d.error
bname = dir.basename
dir = dir.parent().asFileHandler()
dir.mk bname, (result) ->
e __("Error when create directory: {0}", result.error) if result.error
r()
mkdirs: () ->
me = @
return new Promise (r, e) ->
list = []
list[i] = v for v, i in me.nodes if me.hasMeta
console.log list
me.mkdir().then () ->
fn = (l) ->
return r() if l.length is 0
el = (l.splice 0, 1)[0]
el.mkdirs().then () ->
fn l
return fn list
save:(handle) ->
@mkdirs().then ()->
handle.notify __("All directories are created")
.catch (msg) ->
handle.error msg
2019-11-24 20:33:14 +01:00
toc: () ->
2019-11-28 18:35:55 +01:00
@updateName()
v.toc() for v in @nodes
@
2019-11-24 20:33:14 +01:00
2019-11-28 18:35:55 +01:00
remove: (apif) ->
2019-11-24 20:33:14 +01:00
2019-11-28 18:35:55 +01:00
class Book extends BookletFolder
constructor: (path) ->
super 'book', path, true
2019-11-26 22:35:25 +01:00
2019-11-28 18:35:55 +01:00
class BookletChapter extends BookletFolder
constructor: (book) ->
path = "#{book.path}/c_#{book.size()}"
super 'chapter', path, true
book.add @
2019-11-26 22:35:25 +01:00
2019-11-24 20:33:14 +01:00
class BookletSection extends BookletFolder
2019-11-28 18:35:55 +01:00
constructor: (chapter) ->
path = "#{chapter.path}/s_#{chapter.size()}"
super "section", path, false
chapter.add @
2019-11-24 20:33:14 +01:00
class BookletFile extends BookletEntry
constructor: (@section) ->
2019-11-26 22:35:25 +01:00
super()
2019-11-28 18:35:55 +01:00
@section.add @
@path = "#{@section.path}/f_#{@section.size()}.md"
2019-11-26 22:35:25 +01:00
@descFile = @path.asFileHandler()
2019-11-24 20:33:14 +01:00
2019-11-26 22:35:25 +01:00
save: (handle) ->
v.save @descFile for v in @sections
me = @
2019-11-28 18:35:55 +01:00
if @descFile.dirty
@descFile.write "text/plain", (r) ->
handle.error __("Fail to save file {0}: {1}", me.descFile.path, r.error) if r.error
@descFile.dirty = false
handle.notify __("Book saved")
2019-11-24 20:33:14 +01:00
toc: () ->
2019-11-28 18:35:55 +01:00
@updateName()
@