antos-frontend/src/core/BaseDialog.coffee

339 lines
12 KiB
CoffeeScript
Raw Normal View History

2018-03-15 11:00:24 +01:00
# 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/.
2018-02-01 19:36:09 +01:00
class SubWindow extends this.OS.GUI.BaseModel
2017-08-24 01:53:13 +02:00
constructor: (name) ->
2017-08-27 23:40:02 +02:00
super name, null
2017-08-24 01:53:13 +02:00
@parent = undefined
@modal = false
2018-02-01 19:36:09 +01:00
2017-08-24 01:53:13 +02:00
quit: () ->
2020-02-20 18:28:03 +01:00
evt = new Ant.OS.GUI.BaseEvent("exit")
2017-08-24 01:53:13 +02:00
@onexit(evt)
if not evt.prevent
delete @.observable
($ @scheme).remove() if @scheme
@dialog.quit() if @dialog
2017-08-26 16:50:13 +02:00
init: () ->
main: () ->
2017-08-24 01:53:13 +02:00
meta: () ->
@parent.meta()
show: () ->
@trigger 'focus'
2018-02-16 18:38:14 +01:00
($ @scheme).css "z-index", window._zindex + 2
2017-08-24 01:53:13 +02:00
hide: () ->
@trigger 'hide'
2018-02-01 19:36:09 +01:00
SubWindow.type = 3
this.OS.GUI.SubWindow = SubWindow
class BaseDialog extends SubWindow
constructor: (name) ->
super name
2020-02-20 18:28:03 +01:00
@handle = undefined
2018-02-01 19:36:09 +01:00
onexit: (e) ->
@parent.dialog = undefined if @parent
2017-08-24 01:53:13 +02:00
this.OS.GUI.BaseDialog = BaseDialog
###
this dialog rende a tag as main content
and a list of buttons, the behaviour of
the button is specified by user. The conf
object is in the follow form
{
tag: <tag_name>,
buttons:[
{
label: 'buton label',
onclick: function(d){...}
}, ...
]
}
###
class BasicDialog extends BaseDialog
2018-01-25 19:15:41 +01:00
constructor: ( name, @conf, @title) ->
2017-08-24 01:53:13 +02:00
super name
2017-08-26 16:50:13 +02:00
init: () ->
2018-01-25 19:15:41 +01:00
@title = @name if not @title
html = "<afx-app-window data-id = '#{@name}' width='#{@conf.width}' height='#{@conf.height}'>"
html += "<afx-hbox><div data-width='7'></div><afx-vbox><div data-height='5'></div>"
html += "<#{v.tag} #{v.att} data-id = 'content#{k}'></#{v.tag}>" for k,v of @conf.tags
2018-03-12 23:20:06 +01:00
html += "<div data-height = '35' style=' text-align:right;padding-top:3px;'>"
html += "<afx-button data-id = 'bt#{k}' text = '#{v.label}' style='margin-left:5px;'></afx-button>" for k,v of @conf.buttons
html += "</div><div data-height='5'></div></afx-vbox><div data-width='7'></div></afx-hbox></afx-app-window>"
2017-08-24 01:53:13 +02:00
#render the html
2020-02-20 18:28:03 +01:00
Ant.OS.GUI.htmlToScheme html, @, @host
2017-08-24 01:53:13 +02:00
main: () ->
2018-03-10 23:16:48 +01:00
@scheme.set "apptitle", @title
2017-08-24 01:53:13 +02:00
@scheme.set "minimizable", false
2017-08-25 00:18:35 +02:00
@scheme.set "resizable", @conf.resizable if @conf.resizable isnt undefined
2017-08-24 01:53:13 +02:00
me = @
f = (_v) -> () -> _v.onclick me
# bind action to button
( (me.find "bt#{k}").set "onbtclick", f(v) ) for k, v of @conf.buttons
2017-08-27 23:40:02 +02:00
@conf.filldata @ if @conf.filldata
2018-01-30 19:06:30 +01:00
@conf.xtra @ if @conf.xtra
2017-08-24 01:53:13 +02:00
this.OS.GUI.BasicDialog = BasicDialog
2018-01-25 00:49:02 +01:00
class PromptDialog extends BasicDialog
constructor: () ->
super "PromptDialog", {
2018-02-01 19:36:09 +01:00
tags: [
{ tag: "afx-label" },
{ tag: "input", att: "type = 'text' data-height='25'" }
2018-02-01 19:36:09 +01:00
],
2018-01-25 00:49:02 +01:00
width: 200,
height: 120,
2018-01-25 00:49:02 +01:00
resizable: false,
buttons: [
{
label: "__(Ok)",
2018-01-25 00:49:02 +01:00
onclick: (d) ->
2018-02-01 19:36:09 +01:00
txt = (d.find "content1").value
2018-01-25 00:49:02 +01:00
return d.quit() if txt is ""
2020-02-20 18:28:03 +01:00
d.handle txt if d.handle
2018-01-25 00:49:02 +01:00
d.quit()
},
{
label: "__(Cancel)",
2018-01-25 00:49:02 +01:00
onclick: (d) -> d.quit()
}
],
filldata: (d) ->
2018-01-25 19:15:41 +01:00
return unless d.data
2018-02-01 19:36:09 +01:00
(d.find "content0").set "text", d.data.label
(d.find "content1").value = d.data.value if d.data.value
2018-10-01 18:40:45 +02:00
$(d.find "content1").attr("type", d.data.type) if d.data.type
2018-01-30 19:06:30 +01:00
xtra: (d) ->
2018-02-13 04:59:08 +01:00
$( d.find "content1" ).keyup (e) ->
2018-01-30 19:06:30 +01:00
(d.find "bt0").trigger() if e.which is 13
2018-01-25 00:49:02 +01:00
}
this.OS.register "PromptDialog", PromptDialog
2017-08-24 01:53:13 +02:00
class CalendarDialog extends BasicDialog
constructor: () ->
super "CalendarDialog", {
2018-02-01 19:36:09 +01:00
tags: [{ tag: 'afx-calendar-view' }],
2017-08-24 01:53:13 +02:00
width: 300,
2018-03-12 23:20:06 +01:00
height: 230,
2017-08-25 00:18:35 +02:00
resizable: false,
2017-08-24 01:53:13 +02:00
buttons: [
{
label: "__(Ok)",
2017-08-24 01:53:13 +02:00
onclick: (d) ->
2018-02-01 19:36:09 +01:00
date = (d.find "content0").get "selectedDate"
2017-08-24 01:53:13 +02:00
if date
2020-02-20 18:28:03 +01:00
d.handle date if d.handle
2017-08-24 01:53:13 +02:00
d.quit()
else
d.notify __("Please select a date")
2017-08-24 01:53:13 +02:00
},
{
label: "__(Cancel)",
2017-08-24 01:53:13 +02:00
onclick: (d) -> d.quit()
}
]
}
this.OS.register "CalendarDialog", CalendarDialog
2017-08-25 00:18:35 +02:00
class ColorPickerDialog extends BasicDialog
constructor: () ->
super "ColorPickerDialog", {
2018-09-14 19:29:13 +02:00
tags: [{ tag: 'afx-color-picker' }, {tag:'div', att: 'data-height="5"' }],
2017-08-25 00:18:35 +02:00
width: 313,
2018-09-14 19:29:13 +02:00
height: 250,
2017-08-25 00:18:35 +02:00
resizable: false,
buttons: [
{
label: "__(Ok)",
2017-08-25 00:18:35 +02:00
onclick: (d) ->
2018-02-01 19:36:09 +01:00
c = (d.find "content0").get "selectedColor"
2017-08-25 00:18:35 +02:00
if c
2020-02-20 18:28:03 +01:00
d.handle c if d.handle
2017-08-25 00:18:35 +02:00
d.quit()
else
d.notify "Please select a color"
},
{
label: "__(Cancel)",
2017-08-25 00:18:35 +02:00
onclick: (d) -> d.quit()
}
]
}
2017-08-26 16:50:13 +02:00
this.OS.register "ColorPickerDialog", ColorPickerDialog
2017-08-27 23:40:02 +02:00
class InfoDialog extends BasicDialog
constructor: () ->
super "InfoDialog", {
2018-02-01 19:36:09 +01:00
tags: [{ tag: 'afx-grid-view' }],
2017-08-27 23:40:02 +02:00
width: 250,
height: 300,
resizable: true,
buttons: [ { label: "__(Cancel)", onclick: (d) -> d.quit() } ],
2017-08-27 23:40:02 +02:00
filldata: (d) ->
return unless d.data
rows = []
2018-02-01 19:36:09 +01:00
rows.push [ { value: k }, { value: v } ] for k, v of d.data
(d.find "content0").set "rows", rows
2017-08-27 23:40:02 +02:00
}
this.OS.register "InfoDialog", InfoDialog
2018-01-25 19:15:41 +01:00
class YesNoDialog extends BasicDialog
constructor: () ->
super "YesNoDialog", {
tags: [{ tag: "afx-label" }],
2018-01-25 19:15:41 +01:00
width: 300,
height: 100,
resizable: true,
buttons: [
{
label: "__(Yes)", onclick: (d) ->
2020-02-20 18:28:03 +01:00
d.handle true if d.handle
2018-01-25 19:15:41 +01:00
d.quit()
},
{
label: "__(No)", onclick: (d) ->
2020-02-20 18:28:03 +01:00
d.handle false if d.handle
2018-01-25 19:15:41 +01:00
d.quit()
}
],
filldata: (d) ->
return unless d.data
2018-02-01 19:36:09 +01:00
l = d.find "content0"
2018-01-25 19:15:41 +01:00
for k, v of d.data
l.set k, v
}
this.OS.register "YesNoDialog", YesNoDialog
2018-01-26 18:57:28 +01:00
class SelectionDialog extends BasicDialog
constructor: () ->
super "SelectionDialog", {
2018-02-01 19:36:09 +01:00
tags: [{ tag: "afx-list-view" }],
2018-01-26 18:57:28 +01:00
width: 250,
height: 300,
resizable: false,
buttons: [
{
label: "__(Ok)", onclick: (d) ->
2018-02-01 19:36:09 +01:00
el = d.find "content0"
2018-01-26 18:57:28 +01:00
it = el.get "selected"
return unless it
2020-02-20 18:28:03 +01:00
d.handle it if d.handle
2018-01-26 18:57:28 +01:00
d.quit()
},
{ label: "__(Cancel)", onclick: (d) -> d.quit() }
2018-01-26 18:57:28 +01:00
],
filldata: (d) ->
return unless d.data
2018-02-01 19:36:09 +01:00
(d.find "content0").set "items", d.data
2018-01-30 19:06:30 +01:00
xtra: (d) ->
2018-02-01 19:36:09 +01:00
( d.find "content0" ).set "onlistdbclick", (e) ->
2018-01-30 19:06:30 +01:00
(d.find "bt0").trigger()
2018-01-26 18:57:28 +01:00
}
this.OS.register "SelectionDialog", SelectionDialog
2017-08-26 16:50:13 +02:00
class AboutDialog extends BaseDialog
constructor: () ->
super "AboutDialog"
init: () ->
2018-03-11 21:31:40 +01:00
@render "os://resources/schemes/about.html"
2017-08-26 16:50:13 +02:00
main: () ->
mt = @meta()
@scheme.set "apptitle", __("About: {0}",mt.name)
2017-08-26 16:50:13 +02:00
(@find "mylabel").set "*", {icon:mt.icon, iconclass:mt.iconclass, text:"#{mt.name}(v#{mt.version})"}
($ @find "mydesc").html mt.description
# grid data for author info
return unless mt.info
rows = []
rows.push [ { value: k }, { value: v } ] for k, v of mt.info
(@find "mygrid").set "rows", rows
2018-01-28 02:01:21 +01:00
this.OS.register "AboutDialog", AboutDialog
class FileDiaLog extends BaseDialog
constructor: () ->
super "FileDiaLog"
init: () ->
2018-03-11 21:31:40 +01:00
@render "os://resources/schemes/filedialog.html"
2018-01-28 02:01:21 +01:00
main: () ->
fileview = @find "fileview"
location = @find "location"
2018-01-28 16:33:49 +01:00
filename = @find "filename"
2018-01-28 02:01:21 +01:00
me = @
2018-03-10 23:16:48 +01:00
@scheme.set "apptitle", @title
2018-01-28 02:01:21 +01:00
fileview.set "fetch", (e, f) ->
return unless e.child
2020-02-20 18:28:03 +01:00
e.child.path.asFileHandle().read (d) ->
return me.error __("Resource not found: {0}", e.child.path) if d.error
2018-01-28 02:01:21 +01:00
f d.result
setroot = (path) ->
2020-02-20 18:28:03 +01:00
path.asFileHandle().read (d) ->
2018-01-28 02:01:21 +01:00
if(d.error)
return me.error __("Resource not found: {0}", path)
fileview.set "path", path
2018-01-28 02:01:21 +01:00
fileview.set "data", d.result
if not @data or not @data.root
location.set "onlistselect", (e) ->
return unless e and e.data.path
setroot e.data.path
location.set "items", ( i for i in @systemsetting.VFS.mountpoints when i.type isnt "app" )
location.set "selected", 0 unless location.get "selected"
else
$(location).hide()
@trigger "calibrate"
setroot @data.root
2018-01-28 16:33:49 +01:00
fileview.set "onfileselect", (f) ->
2018-02-19 16:52:05 +01:00
($ filename).val f.filename if f.type is "file"
2018-01-28 02:01:21 +01:00
(@find "bt-ok").set "onbtclick", (e) ->
f = fileview.get "selectedFile"
return me.notify __("Please select a file/fofler") unless f
return me.notify __("Please select {0} only", me.data.type) if me.data and me.data.type and me.data.type isnt f.type
2018-02-18 21:11:49 +01:00
if me.data and me.data.mimes
#verify the mime
m = false
if f.mime
for v in me.data.mimes
if f.mime.match (new RegExp v, "g")
m = true
break
return me.notify __("Only {0} could be selected", me.data.mimes.join(",")) unless m
2018-01-28 16:33:49 +01:00
d = f.path
2020-02-20 18:28:03 +01:00
d = f.path.asFileHandle().parent() if f.type is "file"
me.handle d, ($ filename).val(), f.path, f if me.handle
2018-01-28 16:33:49 +01:00
#sel = if me.data and me.data.selection then me.data.selection else "file"
2020-02-20 18:28:03 +01:00
#me.handle f, ($ filename).val() if me.handle and ((f.type is sel) or (sel is "*"))
2018-01-28 02:01:21 +01:00
me.quit()
(@find "bt-cancel").set "onbtclick", (e) ->
me.quit()
if @data and @data.file
($ filename).css("display", "block").val @data.file.basename or "Untitled"
@trigger "resize"
fileview.set "showhidden", @data.hidden if @data and @data.hidden
2018-01-28 16:33:49 +01:00
2018-01-28 02:01:21 +01:00
this.OS.register "FileDiaLog", FileDiaLog