nfp_sites/filadelfia_archive/app/input.js

144 lines
4 KiB
JavaScript
Raw Normal View History

2023-11-14 07:27:04 +00:00
const m = require('mithril')
2023-11-20 07:12:08 +00:00
const api = require('./api')
2023-11-15 04:43:05 +00:00
const tempus = require('@eonasdan/tempus-dominus')
2023-11-14 07:27:04 +00:00
2023-11-20 07:12:08 +00:00
const tempusLocalization = {
locale: 'is',
startOfTheWeek: 0,
hourCycle: 'h23',
dateFormats: {
LTS: 'H:mm:ss',
LT: 'H:mm',
L: 'dd.MM.yyyy',
LL: 'd [de] MMMM [de] yyyy',
LLL: 'd [de] MMMM [de] yyyy H:mm',
LLLL: 'dddd, d [de] MMMM [de] yyyy H:mm',
},
}
2023-11-14 07:27:04 +00:00
const Input = {
oninit: function(vnode) {
2023-11-15 04:43:05 +00:00
this.tempus = null
this.subscription = null
2023-11-20 07:12:08 +00:00
this.input = null
2023-11-29 19:19:41 +00:00
this.preview = null
2023-11-15 04:43:05 +00:00
},
onremove: function(vnode) {
2023-11-20 07:12:08 +00:00
if (this.subscription) this.subscription.unsubscribe()
2023-11-29 19:19:41 +00:00
if (this.tempus) {
this.tempus.dispose()
this.tempus = null
}
if (this.preview) {
this.preview.clear()
}
},
onupdate: function(vnode) {
if (this.tempus && vnode.attrs.form[vnode.attrs.formKey]) {
if (vnode.attrs.form[vnode.attrs.formKey].getTime() !== this.tempus.viewDate?.getTime()) {
this.tempus.dates.setValue(new tempus.DateTime(vnode.attrs.form[vnode.attrs.formKey]))
}
}
},
2023-11-29 19:19:41 +00:00
imageChanged: function(vnode, e) {
let file = e.currentTarget.files?.[0] || null
this.updateValue(vnode, file)
2023-11-29 19:19:41 +00:00
if (this.preview) {
this.preview.clear()
this.preview = null
}
if (!file) return
if (file.type.startsWith('image')) {
this.preview = {
file: file,
preview: URL.createObjectURL(file),
clear: function() {
URL.revokeObjectURL(this.preview)
},
}
}
2023-11-15 04:43:05 +00:00
},
updateValue: function(vnode, value) {
vnode.attrs.form[vnode.attrs.formKey] = value
if (typeof(vnode.attrs.oninput) === 'function') {
vnode.attrs.oninput(vnode.attrs.form[vnode.attrs.formKey])
}
return false
},
2023-11-15 04:43:05 +00:00
getInput: function(vnode) {
switch (vnode.attrs.utility) {
2023-11-20 07:12:08 +00:00
case 'file':
return m('div.form-row', [
m('input', {
type: 'text',
disabled: api.loading,
value: vnode.attrs.form[vnode.attrs.formKey]?.name || '',
}),
m('button.fal', { class: vnode.attrs.button || 'file' }),
m('input.cover', {
type: 'file',
accept: vnode.attrs.accept,
disabled: api.loading,
oninput: (e) => this.updateValue(vnode, e.currentTarget.files?.[0] || null),
2023-11-20 07:12:08 +00:00
}),
])
2023-11-15 04:43:05 +00:00
case 'datetime':
2023-11-20 07:12:08 +00:00
return m('div.form-row', [
m('input', {
type: 'text',
disabled: api.loading,
oncreate: (e) => {
this.tempus = new tempus.TempusDominus(e.dom, {
localization: tempusLocalization,
})
this.tempus.dates.setValue(new tempus.DateTime(vnode.attrs.form[vnode.attrs.formKey]))
2023-11-20 07:12:08 +00:00
this.subscription = this.tempus.subscribe(tempus.Namespace.events.change, (e) => {
this.updateValue(vnode, e.date)
2023-11-20 07:12:08 +00:00
});
},
}),
m('button.fal.fa-calendar', {
onclick: () => { this.tempus.toggle(); return false },
})
])
2023-11-29 19:19:41 +00:00
case 'image':
let imageLink = this.preview && this.preview.preview || vnode.attrs.form[vnode.attrs.formKey]
return m('div.form-row.image-banner', {
style: {
'background-image': typeof imageLink === 'string' ? 'url("' + (imageLink) + '")' : null,
},
}, [
m('input.cover', {
type: 'file',
accept: vnode.attrs.accept,
disabled: api.loading,
onchange: this.imageChanged.bind(this, vnode),
}),
])
2023-11-15 04:43:05 +00:00
default:
return m('input', {
2023-11-20 07:12:08 +00:00
disabled: api.loading,
2023-11-15 04:43:05 +00:00
type: vnode.attrs.type || 'text',
value: vnode.attrs.form[vnode.attrs.formKey],
oninput: (e) => this.updateValue(vnode, e.currentTarget.value),
2023-11-15 04:43:05 +00:00
})
}
2023-11-14 07:27:04 +00:00
},
view: function(vnode) {
return [
vnode.attrs.label ? m('label', vnode.attrs.label) : null,
2023-11-15 04:43:05 +00:00
this.getInput(vnode),
2023-11-14 07:27:04 +00:00
]
},
}
module.exports = Input