From 0c93ff7c9c6d8cc4b8bd4734df63d823b2cb88dd Mon Sep 17 00:00:00 2001 From: Xuan Sang LE Date: Sat, 23 May 2020 20:07:32 +0200 Subject: [PATCH] Enhance dialog api --- src/core/BaseDialog.coffee | 58 ++++++++++--------- src/core/BaseModel.coffee | 1 + src/core/api.coffee | 4 +- src/core/db.coffee | 39 +++++++++---- src/core/gui.coffee | 3 - src/core/tags/MenuTag.coffee | 5 +- src/core/tags/SystemPanelTag.coffee | 7 +++ src/core/tags/TreeViewTag.coffee | 41 ++++++++++--- src/core/tags/tag.coffee | 6 +- .../CodePad/coffees/CommandPalette.coffee | 3 +- src/packages/MarketPlace/dialog.coffee | 2 + src/packages/Setting/VFSHandle.coffee | 3 +- 12 files changed, 116 insertions(+), 56 deletions(-) diff --git a/src/core/BaseDialog.coffee b/src/core/BaseDialog.coffee index d96f125..358e875 100644 --- a/src/core/BaseDialog.coffee +++ b/src/core/BaseDialog.coffee @@ -54,18 +54,21 @@ class BaseDialog extends SubWindow this.OS.GUI.BaseDialog = BaseDialog class BasicDialog extends BaseDialog - constructor: ( name, target) -> + constructor: ( name, @markup) -> 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: () -> + 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 "resizable", false @scheme.set "minimizable", false @@ -76,8 +79,8 @@ class PromptDialog extends BasicDialog constructor: () -> super "PromptDialog" - init: () -> - super.init() + main: () -> + super.main() $input = $(@find "txtInput") @find("lbl").set "text", @data.label if @data and @data.label $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 constructor: () -> - super "TextDialog", TextDialog.scheme + super "TextDialog" - init: () -> - super.init() + main: () -> + super.main() $input = $(@find "txtInput") $input.val @data.value if @data and @data.value @@ -166,8 +169,8 @@ class CalendarDialog extends BasicDialog constructor: () -> super "CalendarDialog" - init: () -> - super.init() + main: () -> + super.main() (@find "btnOk").set "onbtclick", (e) => date = (@find "cal").get "selectedDate" return @notify __("Please select a day") unless date @@ -205,8 +208,8 @@ class ColorPickerDialog extends BasicDialog constructor: () -> super "ColorPickerDialog" - init: () -> - super.init() + main: () -> + super.main() (@find "btnOk").set "onbtclick", (e) => color = (@find "cpicker").get "selectedColor" return @notify __("Please select color") unless color @@ -244,8 +247,8 @@ class InfoDialog extends BasicDialog constructor: () -> super "InfoDialog" - init: () -> - super.init() + main: () -> + super.main() rows = [] delete @data.title if @data and @data.title rows.push [ { text: k }, { text: v } ] for k, v of @data @@ -282,8 +285,8 @@ class YesNoDialog extends BasicDialog constructor: () -> super "YesNoDialog" - init: () -> - super.init() + main: () -> + super.main() @find("lbl").set "*", @data if @data (@find "btnYes").set "onbtclick", (e) => @handle(true) if @handle @@ -318,8 +321,8 @@ class SelectionDialog extends BasicDialog constructor: () -> super "SelectionDialog" - init: () -> - super.init() + main: () -> + super.main() (@find "list").set "data", @data.data if @data and @data.data fn = (e) => data = (@find "list").get "selectedItem" @@ -357,7 +360,8 @@ this.OS.register "SelectionDialog", SelectionDialog class AboutDialog extends BasicDialog constructor: () -> super "AboutDialog" - init: () -> + main: () -> + super.main() mt = @meta() @scheme.set "apptitle", __("About: {0}", mt.name) (@find "mylabel").set "*", { @@ -403,8 +407,8 @@ class FileDialog extends BasicDialog constructor: () -> super "FileDialog" - init: () -> - super.init() + main: () -> + super.main() fileview = @find "fileview" location = @find "location" filename = @find "filename" diff --git a/src/core/BaseModel.coffee b/src/core/BaseModel.coffee index 8cd4a9e..dd71c13 100644 --- a/src/core/BaseModel.coffee +++ b/src/core/BaseModel.coffee @@ -25,6 +25,7 @@ class BaseModel @on "exit", () => @quit() @host = @_gui.workspace @dialog = undefined + render: (p) -> Ant.OS.GUI.loadScheme p, @, @host diff --git a/src/core/api.coffee b/src/core/api.coffee index a433dd9..30f4320 100644 --- a/src/core/api.coffee +++ b/src/core/api.coffee @@ -362,8 +362,8 @@ Ant.OS.API = setting: (f) -> Ant.OS.API.handle.setting f - apigateway: (d, ws, c) -> - return Ant.OS.API.handle.apigateway d, ws, c + apigateway: (d, ws) -> + return Ant.OS.API.handle.apigateway d, ws search: (text) -> r = [] diff --git a/src/core/db.coffee b/src/core/db.coffee index 54be9d6..576ac25 100644 --- a/src/core/db.coffee +++ b/src/core/db.coffee @@ -20,22 +20,41 @@ class DB constructor: (@table) -> 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) -> - 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 "" - if isNaN c - rq.cond = c - else - rq.id = c - Ant.OS.API.handle.dbquery "delete", rq + if isNaN c + rq.cond = c + else + rq.id = c + 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) -> - 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) -> - 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 \ No newline at end of file diff --git a/src/core/gui.coffee b/src/core/gui.coffee index d8bc3a4..e0250d3 100644 --- a/src/core/gui.coffee +++ b/src/core/gui.coffee @@ -422,9 +422,6 @@ Ant.OS.GUI = refreshDesktop: () -> ($ Ant.OS.GUI.workspace)[0].fetch() - - mkdialog: (conf) -> - return new Ant.OS.GUI.BasicDialog conf.name, conf.layout login: () -> scheme = $.parseHTML Ant.OS.GUI.schemes.login diff --git a/src/core/tags/MenuTag.coffee b/src/core/tags/MenuTag.coffee index 43cbed0..24e4cf1 100644 --- a/src/core/tags/MenuTag.coffee +++ b/src/core/tags/MenuTag.coffee @@ -210,13 +210,14 @@ class MenuTag extends Ant.OS.GUI.BaseTag push: (item, flag) -> tag = @get "contentag" tag = item.tag if item.tag + items = @get "items" el = $("<#{tag}>") if flag $(@refs.container).prepend el[0] - @get("items").unshift item + @get("items").unshift item if not items.includes item else el.appendTo @refs.container - @get("items").push item + @get("items").push item if not items.includes item el[0].uify undefined el[0].set "parent", @get("parent") el[0].set "root", if @get("parent") then @get("parent").get("root") else @ diff --git a/src/core/tags/SystemPanelTag.coffee b/src/core/tags/SystemPanelTag.coffee index c4400aa..29b99ed 100644 --- a/src/core/tags/SystemPanelTag.coffee +++ b/src/core/tags/SystemPanelTag.coffee @@ -104,6 +104,13 @@ class SystemPanelTag extends Ant.OS.GUI.BaseTag 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.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 toggle: (flag) -> diff --git a/src/core/tags/TreeViewTag.coffee b/src/core/tags/TreeViewTag.coffee index 212d2b0..4548b5d 100644 --- a/src/core/tags/TreeViewTag.coffee +++ b/src/core/tags/TreeViewTag.coffee @@ -15,18 +15,31 @@ class TreeViewItemPrototype extends Ant.OS.GUI.BaseTag update: (p) -> return unless p - return unless p is @get("treepath") - @set "open", true + switch p + when "expand" + @set "open", true + when "collapse" + @set "open", false + else + return unless p is @get("treepath") + @set "open", true __data__: (v) -> return unless v @set "nodes", v.nodes if v.nodes @set "open", v.open @set "treepath", v.path if v.path + @set "selected", v.selected __selected__: (v) -> + return unless @opts.data $(@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) -> return unless @is_folder() @@ -164,14 +177,28 @@ class TreeViewTag extends Ant.OS.GUI.BaseTag @setopt "fetch", undefined @setopt "dragndrop", false @setopt "treepath", @aid() - @root.is_left = () => @is_left() + @root.is_leaf = () => @is_leaf() + @root.expandAll = () => @expandAll() + @root.collapseAll = () => @collapseAll() + @root.unselect = () => @unselect() @indexcounter = 0 + + unselect: () -> + @get("selectedItem").set "selected", false if @get("selectedItem") + __selectedItem: (v) -> return unless v - @get("selectedItem").set "selected", false if @get("selectedItem") + return if v is @get("selectedItem") v.set "selected", true + expandAll: () -> + return if @is_leaf() + @root.update "expand" + + collapseAll: () -> + return if @is_leaf() + @root.update "collapse" itemclick: (e, flag) -> return unless e and e.item @@ -188,7 +215,7 @@ class TreeViewTag extends Ant.OS.GUI.BaseTag is_root: () -> return @get("treeroot") is undefined - is_left: () -> + is_leaf: () -> data = @get "data" return true unless data 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") return if el.length is 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") @dnd.to = el @__("ondragndrop") { id: @aid(), data: @dnd } diff --git a/src/core/tags/tag.coffee b/src/core/tags/tag.coffee index c4d4548..a290d44 100644 --- a/src/core/tags/tag.coffee +++ b/src/core/tags/tag.coffee @@ -47,13 +47,13 @@ class Ant.OS.GUI.BaseTag value = v @set name, value - set: (opt, value) -> + set: (opt, value, flag) -> if opt is "*" @set k, v for k, v of value else - @["__#{opt}"](value) if @["__#{opt}"] + @["__#{opt}"](value) if @["__#{opt}"] and not flag @opts[opt] = value - @["__#{opt}__"](value) if @["__#{opt}__"] + @["__#{opt}__"](value) if @["__#{opt}__"] and not flag @ aid: () -> diff --git a/src/packages/CodePad/coffees/CommandPalette.coffee b/src/packages/CodePad/coffees/CommandPalette.coffee index 535f698..4fef62c 100644 --- a/src/packages/CodePad/coffees/CommandPalette.coffee +++ b/src/packages/CodePad/coffees/CommandPalette.coffee @@ -2,7 +2,8 @@ class CommandPalette extends this.OS.GUI.BasicDialog constructor: () -> super "CommandPalete", CommandPalette.scheme - init: () -> + main: () -> + super.main() offset = $(".afx-window-content", @parent.scheme).offset() pw = @parent.scheme.get("width") / 5 @scheme.set "width", 3 * pw diff --git a/src/packages/MarketPlace/dialog.coffee b/src/packages/MarketPlace/dialog.coffee index db62344..f690f17 100644 --- a/src/packages/MarketPlace/dialog.coffee +++ b/src/packages/MarketPlace/dialog.coffee @@ -19,7 +19,9 @@ class RepositoryDialog extends this.OS.GUI.subwindows.SelectionDialog constructor: () -> super() + main: () -> + super.main() @list = @find "list" $((@find "btnOk")).hide() @list.set "buttons", [ diff --git a/src/packages/Setting/VFSHandle.coffee b/src/packages/Setting/VFSHandle.coffee index cd2582e..2f73c50 100644 --- a/src/packages/Setting/VFSHandle.coffee +++ b/src/packages/Setting/VFSHandle.coffee @@ -20,7 +20,8 @@ class VFSSettingDialog extends this.OS.GUI.BasicDialog constructor: () -> super "VFSSettingDialog", VFSSettingDialog.scheme - init: () -> + main: () -> + super.main() $(@find("txtPath")).click (e) => @openDialog("FileDialog", { title: "__(Select a directory)",