Files
parcoursup/node_modules/@riotjs/dom-bindings/src/bindings/if.js
lalBi94 7bc56c09b5 $
2023-03-05 13:23:23 +01:00

66 lines
1.4 KiB
JavaScript

import {insertBefore, removeChild} from '@riotjs/util/dom'
/**
* Binding responsible for the `if` directive
*/
export const IfBinding = {
// dynamic binding properties
// node: null,
// evaluate: null,
// isTemplateTag: false,
// placeholder: null,
// template: null,
// API methods
mount(scope, parentScope) {
return this.update(scope, parentScope)
},
update(scope, parentScope) {
const value = !!this.evaluate(scope)
const mustMount = !this.value && value
const mustUnmount = this.value && !value
const mount = () => {
const pristine = this.node.cloneNode()
insertBefore(pristine, this.placeholder)
this.template = this.template.clone()
this.template.mount(pristine, scope, parentScope)
}
switch (true) {
case mustMount:
mount()
break
case mustUnmount:
this.unmount(scope)
break
default:
if (value) this.template.update(scope, parentScope)
}
this.value = value
return this
},
unmount(scope, parentScope) {
this.template.unmount(scope, parentScope, true)
return this
}
}
export default function create(node, { evaluate, template }) {
const placeholder = document.createTextNode('')
insertBefore(placeholder, node)
removeChild(node)
return {
...IfBinding,
node,
evaluate,
placeholder,
template: template.createDOM(node)
}
}