antosdk-apps/Blogger/main.coffee

491 lines
18 KiB
CoffeeScript
Raw Normal View History

# Copyright 2017-2018 Xuan Sang LE <xsang.le AT gmail DOT com>
# AnTOS Web desktop is is licensed under the GNU General Public
# License v3.0, see the LICENCE file for more information
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
#along with this program. If not, see https://www.gnu.org/licenses/.
2020-06-05 17:45:07 +02:00
class Blogger extends this.OS.application.BaseApplication
constructor: (args) ->
super "Blogger", args
main: () ->
@user = {}
@cvlist = @find "cv-list"
2020-06-05 17:45:07 +02:00
@cvlist.ontreeselect = (d) =>
2020-05-23 20:09:01 +02:00
return unless d
2020-06-05 17:45:07 +02:00
data = d.data.item.data
2020-05-23 20:09:01 +02:00
@CVSectionByCID Number(data.id)
@inputtags = @.find "input-tags"
@bloglist = @find "blog-list"
2020-05-23 20:09:01 +02:00
@seclist = @find "cv-sec-list"
@userdb = new @_api.DB("user")
@cvcatdb = new @_api.DB("cv_cat")
@cvsecdb = new @_api.DB("cv_sections")
@blogdb = new @_api.DB("blogs")
2020-05-23 20:09:01 +02:00
@tabcontainer = @find "tabcontainer"
2020-06-05 17:45:07 +02:00
@tabcontainer.ontabselect = (e) =>
@fetchData e.data.container.aid
2020-05-23 20:09:01 +02:00
2020-06-05 17:45:07 +02:00
(@find "bt-user-save").onbtclick = (e) =>
2020-05-23 20:09:01 +02:00
@saveUser()
2020-06-05 17:45:07 +02:00
(@find "cv-cat-add").onbtclick = (e) =>
2020-06-07 14:49:50 +02:00
fn = (tree) =>
2020-05-23 20:09:01 +02:00
@openDialog(new BloggerCategoryDialog(), {
title: __("Add category"),
tree: tree
}).then (d) =>
c =
name: d.value,
pid: d.p.id,
publish: 1
@cvcatdb.save c
.then (r) =>
@refreshCVCat()
.catch (e) => @error __("Cannot add new category"), e
.catch (e) => @error e.toString(), e
2020-06-07 14:49:50 +02:00
@fetchCVCat()
.then (tree) => fn(tree)
.catch (e) =>
data =
text: "Porfolio",
id:"0",
nodes: []
fn(data)
@error __("Unable to fetch categories"), e
2020-05-23 20:09:01 +02:00
2020-06-05 17:45:07 +02:00
(@find "cv-cat-edit").onbtclick = (e) =>
sel = @cvlist.selectedItem
2020-05-23 20:09:01 +02:00
return unless sel
2020-06-05 17:45:07 +02:00
cat = sel.data
return unless cat
2020-05-23 20:09:01 +02:00
@fetchCVCat().then (tree) =>
@openDialog(new BloggerCategoryDialog(), {
title: __("Edit category"),
tree: tree, cat: cat
}).then (d) =>
c =
id: cat.id,
publish: cat.publish,
pid: d.p.id,
name: d.value
@cvcatdb.save c
.then (r) =>
@refreshCVCat()
.catch (e) =>
@error __("Cannot Edit category"), e
.catch (e) => @error __("Unable to fetch categories"), e
2020-06-05 17:45:07 +02:00
(@find "cv-cat-del").onbtclick = (e) =>
sel = @cvlist.selectedItem
2020-05-23 20:09:01 +02:00
return unless sel
2020-06-05 17:45:07 +02:00
cat = sel.data
return unless cat
2020-05-23 20:09:01 +02:00
@openDialog("YesNoDialog", {
title: __("Delete category") ,
iconclass: "fa fa-question-circle",
text: __("Do you really want to delete: {0}?", cat.name)
}).then (d) =>
return unless d
@deleteCVCat cat
.catch (e) => @error e.toString(), e
2020-06-05 17:45:07 +02:00
(@find "cv-sec-add").onbtclick = (e) =>
sel = @cvlist.selectedItem
2020-05-23 20:09:01 +02:00
return unless sel
2020-06-05 17:45:07 +02:00
cat = sel.data
2020-05-23 20:09:01 +02:00
return @notify __("Please select a category") unless cat and cat.id isnt "0"
@openDialog(new BloggerCVSectionDiaglog(@), {
title: __("New section entry for {0}", cat.name)
}).then (d) =>
d.cid = Number cat.id
d.start = Number d.start
d.end = Number d.end
2020-05-23 20:09:01 +02:00
# d.publish = 1
@cvsecdb.save d
.then (r) =>
@CVSectionByCID Number(cat.id)
.catch (e) => @error __("Cannot save section: {0}", e.toString()), e
2020-06-05 17:45:07 +02:00
(@find "cv-sec-move").onbtclick = (e) =>
sel = (@find "cv-sec-list").selectedItem
2020-05-23 20:09:01 +02:00
return @notify __("Please select a section to move") unless sel
2020-06-05 17:45:07 +02:00
sec = sel.data
2020-05-23 20:09:01 +02:00
@fetchCVCat().then (tree) =>
@openDialog(new BloggerCategoryDialog(),{
title: __("Move to"),
tree: tree,
selonly: true
}).then (d) =>
c =
id: sec.id,
cid: d.p.id
@cvsecdb.save c
.then (r) =>
@CVSectionByCID(sec.cid)
(@find "cv-sec-list").unselect()
.catch (e) => @error __("Cannot move section"), e
2020-06-05 17:45:07 +02:00
(@find "cv-sec-edit").onbtclick = (e) =>
sel = (@find "cv-sec-list").selectedItem
2020-05-23 20:09:01 +02:00
return @notify __("Please select a section to edit") unless sel
2020-06-05 17:45:07 +02:00
sec = sel.data
2020-05-23 20:09:01 +02:00
@openDialog(new BloggerCVSectionDiaglog(@), {
title: __("Modify section entry"),
section: sec
}).then (d) =>
d.cid = Number sec.cid
d.start = Number d.start
d.end = Number d.end
#d.publish = Number sec.publish
2020-05-23 20:09:01 +02:00
@cvsecdb.save d
.then (r) =>
@CVSectionByCID Number(sec.cid)
.catch (e) => return @error __("Cannot save section: {0}", e.toString()), e
2020-06-05 17:45:07 +02:00
@seclist.onitemclose = (e) =>
2020-05-23 20:09:01 +02:00
return unless e
2020-06-05 17:45:07 +02:00
data = e.data.item.data
2020-05-23 20:09:01 +02:00
@openDialog("YesNoDialog", {
iconclass: "fa fa-question-circle",
text: __("Do you really want to delete: {0}?", data.title)
}).then (b) =>
return unless b
@cvsecdb.delete data.id
.then (r) =>
2020-06-05 17:45:07 +02:00
@seclist.delete e.data.item
2020-05-23 20:09:01 +02:00
.catch (e) => @error __("Cannot delete the section: {0}", e.toString()), e
return false
@editor = new SimpleMDE
2020-05-23 20:09:01 +02:00
element: @find "markarea"
2020-05-23 20:19:50 +02:00
autoDownloadFontAwesome: false
autofocus: true
tabSize: 4
indentWithTabs: true
toolbar: [
{
name: __("New"),
className: "fa fa-file",
2020-05-23 20:09:01 +02:00
action: (e) =>
@bloglist.unselect()
@clearEditor()
},
{
name: __("Save"),
className: "fa fa-save",
2020-05-23 20:09:01 +02:00
action: (e) =>
@saveBlog()
}
, "|", "bold", "italic", "heading", "|", "quote", "code",
"unordered-list", "ordered-list", "|", "link",
"image", "table", "horizontal-rule",
{
name: "image",
className: "fa fa-file-image-o",
2020-05-23 20:09:01 +02:00
action: (e) =>
@openDialog("FileDialog", {
title: __("Select image file"),
mimes: ["image/.*"]
}).then (d) =>
d.file.path.asFileHandle().publish()
.then (r) =>
doc = @editor.codemirror.getDoc()
doc.replaceSelection "![](#{@_api.handle.shared}/#{r.result})"
.catch (e) => @error __("Cannot export file for embedding to text"), e
},
{
name:"Youtube",
className: "fa fa-youtube",
2020-05-23 20:09:01 +02:00
action: (e) =>
doc = @editor.codemirror.getDoc()
doc.replaceSelection "[[youtube:]]"
}
"|",
{
name: __("Preview"),
className: "fa fa-eye no-disable",
2020-05-23 20:09:01 +02:00
action: (e) =>
@previewOn = !@previewOn
SimpleMDE.togglePreview e
2020-05-23 20:09:01 +02:00
#/console.log @select ".editor-preview editor-preview-active"
renderMathInElement @find "editor-container"
},
"|",
{
name: __("Send mail"),
className: "fa fa-paper-plane",
2020-05-23 20:09:01 +02:00
action: (e) =>
2020-06-05 17:45:07 +02:00
sel = @bloglist.selectedItem
2020-05-23 20:09:01 +02:00
return @error __("No post selected") unless sel
2020-06-05 17:45:07 +02:00
data = sel.data
2020-05-23 20:09:01 +02:00
@openDialog(new BloggerSendmailDiaglog(@), {
title: __("Send mail"),
content: @editor.value(),
id: data.id
})
.then (d) ->
2020-06-05 17:45:07 +02:00
console.log "Email sent"
}
]
2020-06-05 17:45:07 +02:00
@bloglist.onlistselect = (e) =>
el = @bloglist.selectedItem
2020-05-23 20:09:01 +02:00
return unless el
2020-06-05 17:45:07 +02:00
sel = el.data
return unless sel
2020-05-23 20:09:01 +02:00
@blogdb.get Number(sel.id)
.then (r) =>
2020-06-18 09:52:39 +02:00
@editor.value r.content
2020-05-23 20:09:01 +02:00
@inputtags.value = r.tags
2020-06-05 17:45:07 +02:00
(@find "blog-publish").swon = if Number(r.publish) then true else false
2020-05-23 20:09:01 +02:00
.catch (e) =>
@error __("Cannot fetch the entry content"), e
2020-06-05 17:45:07 +02:00
@bloglist.onitemclose = (e) =>
2020-05-23 20:09:01 +02:00
return unless e
el = e.data.item
2020-06-05 17:45:07 +02:00
data = el.data
2020-05-23 20:09:01 +02:00
@openDialog("YesNoDialog", {
title: __("Delete a post"),
iconclass: "fa fa-question-circle",
text: __("Do you really want to delete this post ?")
}).then (b) =>
return unless b
2020-05-23 20:09:01 +02:00
@blogdb.delete data.id
.then (r) =>
2020-06-05 17:45:07 +02:00
@bloglist.delete el
2020-05-23 20:09:01 +02:00
@bloglist.unselect()
@clearEditor()
return false
2020-05-23 20:09:01 +02:00
@bindKey "CTRL-S", () =>
2020-06-05 17:45:07 +02:00
sel = @tabcontainer.selectedTab
return unless sel and sel.container.aid is "blog-container"
2020-05-23 20:09:01 +02:00
@saveBlog()
@on "vboxchange", () =>
@resizeContent()
@resizeContent()
@loadBlogs()
# @fetchData 0
# USER TAB
fetchData: (idx) ->
switch idx
2020-05-23 20:09:01 +02:00
when "user-container" #user info
2020-05-23 20:09:01 +02:00
@userdb.get null
.then (d) =>
@user = d[0]
inputs = @select "[input-class='user-input']"
($ v).val @user[v.name] for v in inputs
.catch (e) => @error __("Cannot fetch user data"), e
when "cv-container" # category
@refreshCVCat()
else
@loadBlogs()
saveUser:() ->
inputs = @select "[input-class='user-input']"
@user[v.name] = ($ v).val() for v in inputs
return @notify __("Full name must be entered") if not @user.fullname or @user.fullname is ""
#console.log @user
2020-05-23 20:09:01 +02:00
@userdb.save @user
.then (r) =>
return @notify __("User data updated")
.catch (e) => return @error __("Cannot save user data"), e
# PORFOLIO TAB
refreshCVCat: () ->
2020-05-23 20:09:01 +02:00
@fetchCVCat().then (data) =>
2020-06-05 17:45:07 +02:00
@cvlist.data = data
2020-05-23 20:09:01 +02:00
@cvlist.expandAll()
.catch (e) => @error __("Unable to load categories"), e
fetchCVCat: () ->
new Promise (resolve, reject) =>
data =
2020-06-05 17:45:07 +02:00
text: "Porfolio",
2020-05-23 20:09:01 +02:00
id:"0",
nodes: []
cnd =
order:
name: "ASC"
@cvcatdb.find cnd
.then (d) =>
@catListToTree d, data, "0"
resolve data
.catch (e) -> reject __e e
#it = (@cvlist.find "pid", "2")[0]
#@cvlist.set "selectedItem", it
2020-05-23 20:09:01 +02:00
catListToTree: (table, data, id) ->
result = (v for v in table when v.pid is id)
return data.nodes = null if result.length is 0
for v in result
v.nodes = []
2020-06-05 17:45:07 +02:00
v.text = v.name
2020-05-23 20:09:01 +02:00
@catListToTree table, v, v.id
#v.nodes = null if v.nodes.length is 0
data.nodes.push v
deleteCVCat: (cat) ->
me = @
ids = []
func = (c) ->
ids.push c.id
func(v) for v in c.nodes if c.nodes
func(cat)
cond = ({ "=": { cid: v } } for v in ids)
# delete all content
2020-05-23 20:09:01 +02:00
@cvsecdb.delete({ "or": cond }).then (r) =>
cond = ({ "=": { id: v } } for v in ids)
2020-05-23 20:09:01 +02:00
@cvcatdb.delete({ "or": cond }).then (re) =>
@refreshCVCat()
2020-06-05 17:45:07 +02:00
@seclist.data=[]
2020-05-23 20:09:01 +02:00
.catch (e) =>
@error __("Cannot delete the category: {0} [{1}]", cat.name, e.toString()), e
.catch (e) =>
@error __("Cannot delete all content of: {0} [{1}]", cat.name, e.toString()), e
CVSectionByCID: (cid) ->
cond =
exp:
"=":
cid: cid
order:
start: "DESC"
2020-05-23 20:09:01 +02:00
@cvsecdb.find(cond).then (d) =>
items = []
2020-06-05 17:45:07 +02:00
(@find "cv-sec-status").text = __("Found {0} sections", d.length)
2020-05-23 20:09:01 +02:00
for v in d
v.closable = true
v.tag = "afx-blogger-cvsection-item"
v.start = Number(v.start)
v.end = Number(v.end)
2020-05-23 20:09:01 +02:00
v.start = undefined if v.start < 1000
v.end = undefined if v.end < 1000
items.push v
2020-06-05 17:45:07 +02:00
@seclist.data = items
2020-05-23 20:09:01 +02:00
.catch (e) => @error e.toString(), e
# blog
saveBlog: () ->
2020-05-23 20:09:01 +02:00
sel = undefined
2020-06-05 17:45:07 +02:00
selel = @bloglist.selectedItem
sel = selel.data if selel
tags = @inputtags.value
content = @editor.value()
title = (new RegExp "^#+(.*)\n", "g").exec content
return @notify __("Please insert a title in the text: beginning with heading") unless title and title.length is 2
return @notify __("Please enter tags") if tags is ""
d = new Date()
data =
2020-06-17 21:08:15 +02:00
content: content
title: title[1].trim()
tags: tags
ctime: if sel then sel.ctime else d.timestamp()
ctimestr: if sel then sel.ctimestr else d.toString()
utime: d.timestamp()
utimestr: d.toString()
2020-05-23 20:09:01 +02:00
rendered: @process(@editor.options.previewRender(content))
2020-06-05 17:45:07 +02:00
publish: if (@find "blog-publish").swon then 1 else 0
data.id = sel.id if sel
#save the data
2020-05-23 20:09:01 +02:00
@blogdb.save data
.then (r) =>
@loadBlogs()
.catch (e) => @error __("Cannot save blog: {0}", e.toString()), e
process: (text) ->
# find video tag and rendered it
embed = (id) ->
return """
<iframe
class = "embeded-video"
width="560" height="315"
src="https://www.youtube.com/embed/#{id}"
frameborder="0" allow="encrypted-media" allowfullscreen
></iframe>
"""
2020-06-17 18:52:24 +02:00
re = /\[\[youtube:([^\]]*)\]\]/g
replace = []
while (found = re.exec text) isnt null
replace.push found
2020-06-18 00:39:10 +02:00
return text unless replace.length > 0
ret = ""
begin = 0
for it in replace
ret += text.substring begin, it.index
2020-06-17 18:56:24 +02:00
ret += embed(it[1])
begin = it.index + it[0].length
ret += text.substring begin, text.length
#console.log ret
2020-06-17 21:08:15 +02:00
return ret
clearEditor:() ->
@.editor.value ""
@.inputtags.value = ""
2020-06-05 17:45:07 +02:00
(@.find "blog-publish").swon = false
# load blog
loadBlogs: () ->
2020-05-23 20:09:01 +02:00
selidx = -1
2020-06-05 17:45:07 +02:00
el = @bloglist.selectedItem
2020-05-23 20:09:01 +02:00
selidx = $(el).index()
cond =
order:
ctime: "DESC"
fields: [
"id",
"title",
"ctimestr",
"ctime",
"utime",
"utimestr"
]
2020-05-23 20:09:01 +02:00
@blogdb.find cond
.then (r) =>
v.tag = "afx-blogger-post-item" for v in r
2020-06-05 17:45:07 +02:00
@bloglist.data = r
2020-05-23 20:09:01 +02:00
if selidx isnt -1
2020-06-05 17:45:07 +02:00
@bloglist.selected = selidx
2020-05-23 20:09:01 +02:00
else
@clearEditor()
2020-06-05 17:45:07 +02:00
@bloglist.selected = -1
2020-05-23 20:09:01 +02:00
.catch (e) => @error __("No post found: {0}", e.toString()), e
resizeContent: () ->
container = @find "editor-container"
children = ($ container).children()
titlebar = (($ @scheme).find ".afx-window-top")[0]
toolbar = children[1]
statusbar = children[4]
cheight = ($ @scheme).height() - ($ titlebar).height() - ($ toolbar).height() - ($ statusbar).height() - 90
($ children[2]).css("height", cheight + "px")
2020-05-23 20:09:01 +02:00
Blogger.singleton = true
2020-05-23 20:09:01 +02:00
Blogger.dependencies = [
"os://scripts/mde/simplemde.min.js",
"os://scripts/mde/simplemde.min.css"
]
this.OS.register "Blogger", Blogger