Enhance dialog api

This commit is contained in:
Xuan Sang LE 2020-05-23 20:07:32 +02:00
parent 9f184fc19b
commit 0c93ff7c9c
12 changed files with 116 additions and 56 deletions

View File

@ -54,18 +54,21 @@ class BaseDialog extends SubWindow
this.OS.GUI.BaseDialog = BaseDialog this.OS.GUI.BaseDialog = BaseDialog
class BasicDialog extends BaseDialog class BasicDialog extends BaseDialog
constructor: ( name, target) -> constructor: ( name, @markup) ->
super name super name
if target
if typeof target is "string"
Ant.OS.GUI.htmlToScheme target, @, @host
else # a file handle
@render target.path
else if Ant.OS.GUI.subwindows[name] and Ant.OS.GUI.subwindows[name].scheme
scheme = Ant.OS.GUI.subwindows[name].scheme
Ant.OS.GUI.htmlToScheme scheme, @, @host
init: () -> init: () ->
if @markup
if typeof @markup is "string"
Ant.OS.GUI.htmlToScheme @markup, @, @host
else # a file handle
@render @markup.path
else if Ant.OS.GUI.subwindows[@name] and Ant.OS.GUI.subwindows[@name].scheme
scheme = Ant.OS.GUI.subwindows[@name].scheme
Ant.OS.GUI.htmlToScheme scheme, @, @host
main: () ->
@scheme.set "apptitle", @data.title if @data and @data.title @scheme.set "apptitle", @data.title if @data and @data.title
@scheme.set "resizable", false @scheme.set "resizable", false
@scheme.set "minimizable", false @scheme.set "minimizable", false
@ -76,8 +79,8 @@ class PromptDialog extends BasicDialog
constructor: () -> constructor: () ->
super "PromptDialog" super "PromptDialog"
init: () -> main: () ->
super.init() super.main()
$input = $(@find "txtInput") $input = $(@find "txtInput")
@find("lbl").set "text", @data.label if @data and @data.label @find("lbl").set "text", @data.label if @data and @data.label
$input.val @data.value if @data and @data.value $input.val @data.value if @data and @data.value
@ -122,10 +125,10 @@ this.OS.register "PromptDialog", PromptDialog
class TextDialog extends this.OS.GUI.BasicDialog class TextDialog extends this.OS.GUI.BasicDialog
constructor: () -> constructor: () ->
super "TextDialog", TextDialog.scheme super "TextDialog"
init: () -> main: () ->
super.init() super.main()
$input = $(@find "txtInput") $input = $(@find "txtInput")
$input.val @data.value if @data and @data.value $input.val @data.value if @data and @data.value
@ -166,8 +169,8 @@ class CalendarDialog extends BasicDialog
constructor: () -> constructor: () ->
super "CalendarDialog" super "CalendarDialog"
init: () -> main: () ->
super.init() super.main()
(@find "btnOk").set "onbtclick", (e) => (@find "btnOk").set "onbtclick", (e) =>
date = (@find "cal").get "selectedDate" date = (@find "cal").get "selectedDate"
return @notify __("Please select a day") unless date return @notify __("Please select a day") unless date
@ -205,8 +208,8 @@ class ColorPickerDialog extends BasicDialog
constructor: () -> constructor: () ->
super "ColorPickerDialog" super "ColorPickerDialog"
init: () -> main: () ->
super.init() super.main()
(@find "btnOk").set "onbtclick", (e) => (@find "btnOk").set "onbtclick", (e) =>
color = (@find "cpicker").get "selectedColor" color = (@find "cpicker").get "selectedColor"
return @notify __("Please select color") unless color return @notify __("Please select color") unless color
@ -244,8 +247,8 @@ class InfoDialog extends BasicDialog
constructor: () -> constructor: () ->
super "InfoDialog" super "InfoDialog"
init: () -> main: () ->
super.init() super.main()
rows = [] rows = []
delete @data.title if @data and @data.title delete @data.title if @data and @data.title
rows.push [ { text: k }, { text: v } ] for k, v of @data rows.push [ { text: k }, { text: v } ] for k, v of @data
@ -282,8 +285,8 @@ class YesNoDialog extends BasicDialog
constructor: () -> constructor: () ->
super "YesNoDialog" super "YesNoDialog"
init: () -> main: () ->
super.init() super.main()
@find("lbl").set "*", @data if @data @find("lbl").set "*", @data if @data
(@find "btnYes").set "onbtclick", (e) => (@find "btnYes").set "onbtclick", (e) =>
@handle(true) if @handle @handle(true) if @handle
@ -318,8 +321,8 @@ class SelectionDialog extends BasicDialog
constructor: () -> constructor: () ->
super "SelectionDialog" super "SelectionDialog"
init: () -> main: () ->
super.init() super.main()
(@find "list").set "data", @data.data if @data and @data.data (@find "list").set "data", @data.data if @data and @data.data
fn = (e) => fn = (e) =>
data = (@find "list").get "selectedItem" data = (@find "list").get "selectedItem"
@ -357,7 +360,8 @@ this.OS.register "SelectionDialog", SelectionDialog
class AboutDialog extends BasicDialog class AboutDialog extends BasicDialog
constructor: () -> constructor: () ->
super "AboutDialog" super "AboutDialog"
init: () -> main: () ->
super.main()
mt = @meta() mt = @meta()
@scheme.set "apptitle", __("About: {0}", mt.name) @scheme.set "apptitle", __("About: {0}", mt.name)
(@find "mylabel").set "*", { (@find "mylabel").set "*", {
@ -403,8 +407,8 @@ class FileDialog extends BasicDialog
constructor: () -> constructor: () ->
super "FileDialog" super "FileDialog"
init: () -> main: () ->
super.init() super.main()
fileview = @find "fileview" fileview = @find "fileview"
location = @find "location" location = @find "location"
filename = @find "filename" filename = @find "filename"

View File

@ -25,6 +25,7 @@ class BaseModel
@on "exit", () => @quit() @on "exit", () => @quit()
@host = @_gui.workspace @host = @_gui.workspace
@dialog = undefined @dialog = undefined
render: (p) -> render: (p) ->
Ant.OS.GUI.loadScheme p, @, @host Ant.OS.GUI.loadScheme p, @, @host

View File

@ -362,8 +362,8 @@ Ant.OS.API =
setting: (f) -> setting: (f) ->
Ant.OS.API.handle.setting f Ant.OS.API.handle.setting f
apigateway: (d, ws, c) -> apigateway: (d, ws) ->
return Ant.OS.API.handle.apigateway d, ws, c return Ant.OS.API.handle.apigateway d, ws
search: (text) -> search: (text) ->
r = [] r = []

View File

@ -20,22 +20,41 @@ class DB
constructor: (@table) -> constructor: (@table) ->
save: (d) -> save: (d) ->
Ant.OS.API.handle.dbquery "save", { table: @table, data: d } return new Promise (resolve, reject) =>
Ant.OS.API.handle.dbquery "save", { table: @table, data: d }
.then (r) ->
return reject(Ant.OS.API.throwe(r.error)) if r.error
resolve(r.result)
.catch (e) -> reject __e e
delete: (c) -> delete: (c) ->
rq = { table: @table } return new Promise (resolve, reject) =>
return new Promise (resolve, reject) -> rq = { table: @table }
reject(Ant.OS.API.throwe("OS.DB: unkown condition")) unless c and c isnt "" reject(Ant.OS.API.throwe("OS.DB: unkown condition")) unless c and c isnt ""
if isNaN c if isNaN c
rq.cond = c rq.cond = c
else else
rq.id = c rq.id = c
Ant.OS.API.handle.dbquery "delete", rq Ant.OS.API.handle.dbquery "delete", rq
.then (r) ->
return reject(Ant.OS.API.throwe(r.error)) if r.error
resolve(r.result)
.catch (e) -> reject __e e
get: (id) -> get: (id) ->
Ant.OS.API.handle.dbquery "get", { table: @table, id: id } new Promise (resolve, reject) =>
Ant.OS.API.handle.dbquery "get", { table: @table, id: id }
.then (r) ->
return reject(Ant.OS.API.throwe(r.error)) if r.error
resolve(r.result)
.catch (e) -> reject __e e
find: (cond) -> find: (cond) ->
Ant.OS.API.handle.dbquery "select", { table: @table, cond: cond } new Promise (resolve, reject) =>
Ant.OS.API.handle.dbquery "select", { table: @table, cond: cond }
.then (r) ->
return reject(Ant.OS.API.throwe(r.error)) if r.error
resolve(r.result)
.catch (e) -> reject __e e
Ant.OS.API.DB = DB Ant.OS.API.DB = DB

View File

@ -422,9 +422,6 @@ Ant.OS.GUI =
refreshDesktop: () -> refreshDesktop: () ->
($ Ant.OS.GUI.workspace)[0].fetch() ($ Ant.OS.GUI.workspace)[0].fetch()
mkdialog: (conf) ->
return new Ant.OS.GUI.BasicDialog conf.name, conf.layout
login: () -> login: () ->
scheme = $.parseHTML Ant.OS.GUI.schemes.login scheme = $.parseHTML Ant.OS.GUI.schemes.login

View File

@ -210,13 +210,14 @@ class MenuTag extends Ant.OS.GUI.BaseTag
push: (item, flag) -> push: (item, flag) ->
tag = @get "contentag" tag = @get "contentag"
tag = item.tag if item.tag tag = item.tag if item.tag
items = @get "items"
el = $("<#{tag}>") el = $("<#{tag}>")
if flag if flag
$(@refs.container).prepend el[0] $(@refs.container).prepend el[0]
@get("items").unshift item @get("items").unshift item if not items.includes item
else else
el.appendTo @refs.container el.appendTo @refs.container
@get("items").push item @get("items").push item if not items.includes item
el[0].uify undefined el[0].uify undefined
el[0].set "parent", @get("parent") el[0].set "parent", @get("parent")
el[0].set "root", if @get("parent") then @get("parent").get("root") else @ el[0].set "root", if @get("parent") then @get("parent").get("root") else @

View File

@ -104,6 +104,13 @@ class SystemPanelTag extends Ant.OS.GUI.BaseTag
list = [] list = []
list.push v for k, v of Ant.OS.setting.system.packages when (v and v.app) list.push v for k, v of Ant.OS.setting.system.packages when (v and v.app)
list.push v for k, v of Ant.OS.setting.system.menu list.push v for k, v of Ant.OS.setting.system.menu
list.sort (a, b) ->
if a.text < b.text
-1
else if a.text > b.text
1
else
0
@refs.applist.set "data", list @refs.applist.set "data", list
toggle: (flag) -> toggle: (flag) ->

View File

@ -15,18 +15,31 @@ class TreeViewItemPrototype extends Ant.OS.GUI.BaseTag
update: (p) -> update: (p) ->
return unless p return unless p
return unless p is @get("treepath") switch p
@set "open", true when "expand"
@set "open", true
when "collapse"
@set "open", false
else
return unless p is @get("treepath")
@set "open", true
__data__: (v) -> __data__: (v) ->
return unless v return unless v
@set "nodes", v.nodes if v.nodes @set "nodes", v.nodes if v.nodes
@set "open", v.open @set "open", v.open
@set "treepath", v.path if v.path @set "treepath", v.path if v.path
@set "selected", v.selected
__selected__: (v) -> __selected__: (v) ->
return unless @opts.data
$(@refs.wrapper).removeClass() $(@refs.wrapper).removeClass()
return $(@refs.wrapper).addClass("afx_tree_item_selected") if v @opts.data.selected = v
if v
@get("treeroot").unselect()
# set selectedItem but not trigger the update
@get("treeroot").set "selectedItem", @root, true
return $(@refs.wrapper).addClass("afx_tree_item_selected")
__open__: (v) -> __open__: (v) ->
return unless @is_folder() return unless @is_folder()
@ -164,14 +177,28 @@ class TreeViewTag extends Ant.OS.GUI.BaseTag
@setopt "fetch", undefined @setopt "fetch", undefined
@setopt "dragndrop", false @setopt "dragndrop", false
@setopt "treepath", @aid() @setopt "treepath", @aid()
@root.is_left = () => @is_left() @root.is_leaf = () => @is_leaf()
@root.expandAll = () => @expandAll()
@root.collapseAll = () => @collapseAll()
@root.unselect = () => @unselect()
@indexcounter = 0 @indexcounter = 0
unselect: () ->
@get("selectedItem").set "selected", false if @get("selectedItem")
__selectedItem: (v) -> __selectedItem: (v) ->
return unless v return unless v
@get("selectedItem").set "selected", false if @get("selectedItem") return if v is @get("selectedItem")
v.set "selected", true v.set "selected", true
expandAll: () ->
return if @is_leaf()
@root.update "expand"
collapseAll: () ->
return if @is_leaf()
@root.update "collapse"
itemclick: (e, flag) -> itemclick: (e, flag) ->
return unless e and e.item return unless e and e.item
@ -188,7 +215,7 @@ class TreeViewTag extends Ant.OS.GUI.BaseTag
is_root: () -> is_root: () ->
return @get("treeroot") is undefined return @get("treeroot") is undefined
is_left: () -> is_leaf: () ->
data = @get "data" data = @get "data"
return true unless data return true unless data
return if data.nodes then false else true return if data.nodes then false else true
@ -232,7 +259,7 @@ class TreeViewTag extends Ant.OS.GUI.BaseTag
el = $(e.target).closest("afx-tree-view") el = $(e.target).closest("afx-tree-view")
return if el.length is 0 return if el.length is 0
el = el[0] el = el[0]
el = el.get("parent") if el.is_left() el = el.get("parent") if el.is_leaf()
return if el is @dnd.from or el is @dnd.from.get("parent") return if el is @dnd.from or el is @dnd.from.get("parent")
@dnd.to = el @dnd.to = el
@__("ondragndrop") { id: @aid(), data: @dnd } @__("ondragndrop") { id: @aid(), data: @dnd }

View File

@ -47,13 +47,13 @@ class Ant.OS.GUI.BaseTag
value = v value = v
@set name, value @set name, value
set: (opt, value) -> set: (opt, value, flag) ->
if opt is "*" if opt is "*"
@set k, v for k, v of value @set k, v for k, v of value
else else
@["__#{opt}"](value) if @["__#{opt}"] @["__#{opt}"](value) if @["__#{opt}"] and not flag
@opts[opt] = value @opts[opt] = value
@["__#{opt}__"](value) if @["__#{opt}__"] @["__#{opt}__"](value) if @["__#{opt}__"] and not flag
@ @
aid: () -> aid: () ->

View File

@ -2,7 +2,8 @@ class CommandPalette extends this.OS.GUI.BasicDialog
constructor: () -> constructor: () ->
super "CommandPalete", CommandPalette.scheme super "CommandPalete", CommandPalette.scheme
init: () -> main: () ->
super.main()
offset = $(".afx-window-content", @parent.scheme).offset() offset = $(".afx-window-content", @parent.scheme).offset()
pw = @parent.scheme.get("width") / 5 pw = @parent.scheme.get("width") / 5
@scheme.set "width", 3 * pw @scheme.set "width", 3 * pw

View File

@ -19,7 +19,9 @@
class RepositoryDialog extends this.OS.GUI.subwindows.SelectionDialog class RepositoryDialog extends this.OS.GUI.subwindows.SelectionDialog
constructor: () -> constructor: () ->
super() super()
main: () -> main: () ->
super.main()
@list = @find "list" @list = @find "list"
$((@find "btnOk")).hide() $((@find "btnOk")).hide()
@list.set "buttons", [ @list.set "buttons", [

View File

@ -20,7 +20,8 @@ class VFSSettingDialog extends this.OS.GUI.BasicDialog
constructor: () -> constructor: () ->
super "VFSSettingDialog", VFSSettingDialog.scheme super "VFSSettingDialog", VFSSettingDialog.scheme
init: () -> main: () ->
super.main()
$(@find("txtPath")).click (e) => $(@find("txtPath")).click (e) =>
@openDialog("FileDialog", { @openDialog("FileDialog", {
title: "__(Select a directory)", title: "__(Select a directory)",