-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
179 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
]) | ||
])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters