Skip to content

Commit

Permalink
datalist more or less working
Browse files Browse the repository at this point in the history
  • Loading branch information
marcodpt committed Jan 15, 2024
1 parent c153aa5 commit c22a502
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 25 deletions.
2 changes: 1 addition & 1 deletion new/e.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const h = (tagName, attributes, children) => {
const v = attributes[key]
const k = camelToKebab(key)
if (typeof v == 'function') {
e.addEventListener(k, v)
e.addEventListener(k.substr(0, 2) == 'on' ? k.substr(2) : k, v)
} else if (v != null && v !== false) {
e.setAttribute(k, v)
}
Expand Down
17 changes: 9 additions & 8 deletions new/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ const router = build({
pagination: {
page: p,
pages: ps,
first: goto({
change: _page => location.replace(goto({_page})),
first: 'javascript:location.replace("'+goto({
_page: 1
}),
previous: goto({
})+'")',
previous: 'javascript:location.replace("'+goto({
_page: p - 1
}),
next: goto({
})+'")',
next: 'javascript:location.replace("'+goto({
_page: p + 1
}),
last: goto({
})+'")',
last: 'javascript:location.replace("'+goto({
_page: ps
})
})+'")'
},
rows: users.slice((p - 1) * 10, p * 10),
totals: null
Expand Down
8 changes: 4 additions & 4 deletions new/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ const validator = schema => data => {
} = schema
var error = ''
if (
schema.enum instanceof Array && schema.enum.indexOf(data) < 0
) {
error = l.enum(schema.enum)
} else if (
(type == 'null' && data !== null) ||
(type == 'boolean' && data !== false && data !== true) ||
(type == 'object' && (
Expand All @@ -130,10 +134,6 @@ const validator = schema => data => {
(type == 'integer' && (typeof data != 'number' || data % 1 !== 0))
) {
error = l.type(type)
} else if (
schema.enum instanceof Array && schema.enum.indexOf(data) < 0
) {
error = l.enum(schema.enum)
} else if (typeof data == 'string') {
if (minLength != null && data.length < minLength) {
error = l.minLength(minLength)
Expand Down
59 changes: 59 additions & 0 deletions new/navbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import e from './e.js'

export default ({
title,
sidebar
}) => {
return e(({div, nav, h5, a, small, ul, li, i}) => div({}, [
nav({
class: 'navbar navbar-expand-lg bg-light navbar-light'
}, [
div({
class: 'container-fluid'
}, [
ul({
class: 'navbar-nav'
}, [
li({
class: 'nav-item'
}, [
a({
class: 'nav-link',
dataBsToggle: 'offcanvas',
href: '#sidebar',
role: 'button',
ariaControls: 'sidebar'
}, [
i({class: link.menu})
])
])
])
])
]),
div({
class: 'offcanvas offcanvas-start',
tabindex: '-1',
id: 'sidebar'
}, [
div({
class: 'offcanvas-header'
}, [
h5({
class: 'offcanvas-title'
}, [
a({
href: '#/',
class: 'text-reset text-decoration-none',
onclick: hide
}, text(title)),
small({
class: 'text-secondary'
}, text(current))
])
]),
div({
class: 'offcanvas-body'
}, sidebar)
])
]))
}
29 changes: 26 additions & 3 deletions new/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {link, icon, linkify, iconify, interpolate, lang} from './lib.js'
import back from './tags/back.js'
import spinner from './tags/spinner.js'
import output from './tags/output.js'
import input from './tags/input.js'

export default ({
title,
Expand All @@ -17,6 +18,18 @@ export default ({
const K = Object.keys(P)
const totals = null
const rows = []
const pager = {
type: 'integer',
title: 'pagination',
enum: [],
noValid: true,
update: (v, err) => {
const d = pager.default
if (!err && d != null && v != d && pager.change) {
pager.change(v)
}
}
}

const tbl = e(({
table, thead, tbody, tr, th, td, div, a, i, text
Expand Down Expand Up @@ -67,6 +80,7 @@ export default ({
i({
class: iconify(icon)
}),
text(' '),
text(title)
])
]))))
Expand Down Expand Up @@ -107,7 +121,7 @@ export default ({
div({
class: 'col-auto'
}, [
text(l.loading)
input(pager)
]),
div({
class: 'col-auto'
Expand Down Expand Up @@ -173,7 +187,7 @@ export default ({
var x = null

if (pagination) {
const {page, pages, first, previous, next, last} = pagination
const {page, pages, change, first, previous, next, last} = pagination
x = tbl.querySelector('.app-pagination').querySelectorAll('.col-auto')
if (page <= 1 || !first) {
x[0].querySelector('a').classList.add('disabled')
Expand All @@ -189,7 +203,16 @@ export default ({
x[1].querySelector('a').classList.remove('disabled')
x[1].querySelector('a').setAttribute('href', previous)
}
x[2].textContent = l.pagination(page, pages)
pager.default = page
pager.change = change
x[2].querySelector('input').dispatchEvent(
new CustomEvent('app.input.pagination.data', {
detail: Array(pages).fill().map((v, i) => ({
value: i + 1,
label: l.pagination(i + 1, pages)
}))
})
)
if (page >= pages || !next) {
x[3].querySelector('a').classList.add('disabled')
x[3].querySelector('a').removeAttribute('href')
Expand Down
86 changes: 78 additions & 8 deletions new/tags/input.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,105 @@
import e from '../e.js'
import {lang, validator, parser} from '../lib.js'

export default ({title, description, css, update, ...schema}) => {
const validate = validator(schema)
export default s => {
const {title, description, css, update, noValid, ...schema} = s
const l = lang()
var validate = validator(schema)
const parse = parser(schema)
var oldValue = null
const onfocus = !schema.enum ? null : () => {
oldValue = target.value
target.value = ''
}
const getOpt = () => Array.from(wrapper.querySelectorAll('option'))
.reduce((p, o, i) => p < 0 && o.value == target.value ? i : p, -1)
const onblur = !schema.enum ? null : () => {
const i = getOpt()
if (oldValue != null && i < 0) {
target.value = oldValue
}
}
const change = () => {
const v = parse(target.value)
var value = target.value
if (schema.enum) {
const i = getOpt()
value = schema.enum[i]
}

const v = parse(value)
const err = validate(v)
target.classList.remove(`is-${err ? '' : 'in'}valid`)
target.classList.add(`is-${err ? 'in' : ''}valid`)
target.classList.remove(`is-invalid`)
target.classList.remove(`is-valid`)
if ((err || !noValid) &&
!(schema.enum && oldValue == null && !schema.enum.length)
) {
target.classList.add(`is-${err ? 'in' : ''}valid`)
}
wrapper.querySelector('.invalid-feedback').textContent = err
if (typeof update == 'function') {
update(v, err)
}
}
const {type, ui} = schema
var step = null
if (['integer', 'number'].indexOf(type) >= 0) {
if (/^num\.[1-9][0-9]*$/.test(ui)) {
const precision = parseInt(ui.substr(4))
step = 1 / (10 ** precision)
} else {
step = 1
}
}
const target = e(({input}) => input({
class: 'form-control',
name: title,
placeholder: description,
type: schema.enum ? null :
step ? 'number' :
ui == 'date' ? 'date' : null,
step,
placeholder: schema.enum && !schema.enum.length ? l.loading : description,
value: schema.default,
input: change
oninput: change,
onfocus,
onblur,
min: schema.minimum,
max: schema.maximum,
disabled: schema.enum && !schema.enum.length,
list: schema.enum ? `app.data.${title}` : null
}))
const wrapper = e(({div}) => div({
const wrapper = e(({div, datalist, option}) => div({
class: css
}, [
target,
schema.enum ? datalist({
id: `app.data.${title}`
}, schema.enum.map(o => option({value: o}))) : null,
div({
class: 'invalid-feedback'
})
]))
change()

if (schema.enum) {
target.addEventListener(`app.input.${title}.data`, ev => {
schema.enum = ev.detail.map(({value}) => value)
validate = validator(schema)
target.disabled = false
wrapper.querySelector('datalist')
.replaceWith(e(({datalist, option}) => datalist({
id: `app.data.${title}`
}, ev.detail.map(({label}) => option({value: label})))))
if (s.default != null) {
const v = ev.detail.reduce((v, {value, label}) =>
v == null && value == s.default ? label : v
, null)
if (v != null) {
target.value = v
change()
}
}
})
}

return wrapper
}
3 changes: 2 additions & 1 deletion schema/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default {
title: 'Balance ($)',
default: 0,
minimum: 1000,
maximum: 4000
maximum: 4000,
ui: 'num.2'
}
},
links: [
Expand Down

0 comments on commit c22a502

Please sign in to comment.