antos-frontend/src/core/BaseDialog.coffee

308 lines
10 KiB
CoffeeScript
Raw Normal View History

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: () ->
evt = new _GUI.BaseEvent("exit")
@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
@handler = undefined
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 = 'dia-window' apptitle='#{@title}' width='#{@conf.width}' height='#{@conf.height}'>
2018-01-30 19:06:30 +01:00
<afx-vbox>"
2018-02-13 04:59:08 +01:00
html += "<#{v.tag} #{v.att} style = 'margin-left:5px; margin-right:5px;' data-id = 'content#{k}'></#{v.tag}>" for k,v of @conf.tags
2018-02-01 19:36:09 +01:00
html += "<div data-height = '35' style=' text-align:right;padding-top:3px;'>"
2017-08-27 23:40:02 +02:00
html += "<afx-button data-id = 'bt#{k}' text = '#{v.label}' style='margin-right:5px;'></afx-button>" for k,v of @conf.buttons
2018-01-30 19:06:30 +01:00
html += "</div></afx-vbox></afx-app-window>"
2017-08-24 01:53:13 +02:00
#render the html
_GUI.htmlToScheme html, @, @host
main: () ->
@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", att: "data-height = '20'" },
{ tag: "input", att: "type = 'text'" }
],
2018-01-25 00:49:02 +01:00
width: 200,
2018-02-01 19:36:09 +01:00
height: 100,
2018-01-25 00:49:02 +01:00
resizable: false,
buttons: [
{
label: "0k",
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 ""
d.handler txt if d.handler
d.quit()
},
{
label: "Cancel",
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-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,
height: 220,
2017-08-25 00:18:35 +02:00
resizable: false,
2017-08-24 01:53:13 +02:00
buttons: [
{
label: 'Ok',
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
d.handler date if d.handler
d.quit()
else
d.notify "Please select a date"
},
{
label: 'Cancel',
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-02-01 19:36:09 +01:00
tags: [{ tag: 'afx-color-picker' }],
2017-08-25 00:18:35 +02:00
width: 313,
height: 220,
resizable: false,
buttons: [
{
label: 'Ok',
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
d.handler c if d.handler
d.quit()
else
d.notify "Please select a color"
},
{
label: 'Cancel',
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() } ],
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", {
2018-02-16 18:38:14 +01:00
tags: [{ tag: "afx-label", att: "style = 'padding-left:10px;'" }],
2018-01-25 19:15:41 +01:00
width: 300,
height: 100,
resizable: true,
buttons: [
{
label: "Yes", onclick: (d) ->
d.handler true if d.handler
d.quit()
},
{
label: "No", onclick: (d) ->
2018-01-28 02:01:21 +01:00
d.handler false if d.handler
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
d.handler it if d.handler
d.quit()
},
{ label: "Cancel", onclick: (d) -> d.quit() }
],
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-02-01 19:36:09 +01:00
@render "os:///resources/schemes/about.html"
2017-08-26 16:50:13 +02:00
main: () ->
mt = @meta()
@scheme.set "apptitle", "About: #{mt.name}"
(@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-02-01 19:36:09 +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 = @
@scheme.set "apptitle", "#{@title}"
fileview.set "fetch", (e, f) ->
return unless e.child
e.child.path.asFileHandler().read (d) ->
return me.error "Resource not found #{e.child.path}" if d.error
f d.result
location.set "onlistselect", (e) ->
return unless e and e.data.path
e.data.path.asFileHandler().read (d) ->
if(d.error)
return me.error "Resource not found #{e.data.path}"
fileview.set "path", e.data.path
fileview.set "data", d.result
location.set "items", ( i for i in @systemsetting.VFS.mountpoints when i.type isnt "app" )
location.set "selected", 0 unless location.get "selected"
2018-01-28 16:33:49 +01:00
fileview.set "onfileselect", (f) ->
($ 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"
2018-02-18 21:11:49 +01:00
return me.notify "Please select a file" unless f
if me.data and me.data.mimes
#verify the mime
m = false
for v in me.data.mimes
if f.mime.match (new RegExp v, "g")
m = true
break
return me.notify "Only #{me.data.mimes.join(",")} could be selected" unless m
2018-01-28 16:33:49 +01:00
d = f.path
d = f.path.asFileHandler().parent() if f.type is "file"
me.handler d, ($ filename).val() if me.handler
#sel = if me.data and me.data.selection then me.data.selection else "file"
#me.handler f, ($ filename).val() if me.handler 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()
2018-01-28 16:33:49 +01:00
($ filename).css("display", "block").val @data.file.basename or "Untitled" if @data and @data.file
2018-01-28 02:01:21 +01:00
this.OS.register "FileDiaLog", FileDiaLog