| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- var utils = require('../utils')
- module.exports = {
- on : require('./on'),
- repeat : require('./repeat'),
- model : require('./model'),
- attr: function (value) {
- this.el.setAttribute(this.arg, value)
- },
- text: function (value) {
- this.el.textContent = utils.toText(value)
- },
- html: function (value) {
- this.el.innerHTML = utils.toText(value)
- },
- style: {
- bind: function () {
- this.arg = convertCSSProperty(this.arg)
- },
- update: function (value) {
- this.el.style[this.arg] = value
- }
- },
- show: function (value) {
- this.el.style.display = value ? '' : 'none'
- },
- visible: function (value) {
- this.el.style.visibility = value ? '' : 'hidden'
- },
- class: function (value) {
- if (this.arg) {
- this.el.classList[value ? 'add' : 'remove'](this.arg)
- } else {
- if (this.lastVal) {
- this.el.classList.remove(this.lastVal)
- }
- this.el.classList.add(value)
- this.lastVal = value
- }
- },
- 'if': {
- bind: function () {
- this.parent = this.el.parentNode
- this.ref = document.createComment('sd-if-' + this.key)
- },
- update: function (value) {
- var attached = !!this.el.parentNode
- if (!this.parent) { // the node was detached when bound
- if (!attached) {
- return
- } else {
- this.parent = this.el.parentNode
- }
- }
- // should always have this.parent if we reach here
- if (!value) {
- if (attached) {
- // insert the reference node
- var next = this.el.nextSibling
- if (next) {
- this.parent.insertBefore(this.ref, next)
- } else {
- this.parent.appendChild(this.ref)
- }
- this.parent.removeChild(this.el)
- }
- } else if (!attached) {
- this.parent.insertBefore(this.el, this.ref)
- this.parent.removeChild(this.ref)
- }
- }
- }
- }
- /**
- * convert hyphen style CSS property to Camel style
- */
- var CONVERT_RE = /-(.)/g
- function convertCSSProperty (prop) {
- if (prop.charAt(0) === '-') prop = prop.slice(1)
- return prop.replace(CONVERT_RE, function (m, char) {
- return char.toUpperCase()
- })
- }
|