var internalDirectives = { style: { update (value) { if (typeof value === 'string') { this.el.style.cssText = value } elseif (Array.isArray(value)) { this.handleObject(value.reduce(extend, {})) } else { this.handleObject(value || {}) } }, handleObject (value) { // cache object styles so that only changed props // are actually updated. var cache = this.cache || (this.cache = {}) var name, val for (name in cache) { if (!(name in value)) { this.handleSingle(name, null) delete cache[name] } } for (name in value) { val = value[name] if (val !== cache[name]) { cache[name] = val this.handleSingle(name, val) } } }, handleSingle (prop, value) { // cast possible numbers/booleans into strings if (value != null) value += '' if (value) { this.el.style[prop] = value } else { this.el.style[prop] = '' } } } }
functionnormalize (value) { const res = [] if (isArray(value)) { for (var i = 0, l = value.length; i < l; i++) { const key = value[i] if (key) { if (typeof key === 'string') { res.push(key) } else { for (var k in key) { if (key[k]) res.push(k) } } } } } elseif (isObject(value)) { for (var key in value) { if (value[key]) res.push(key) } } return res }
functionaddClass (el, cls) { if (el.classList) { el.classList.add(cls) } else { var cur = ' ' + getClass(el) + ' ' if (cur.indexOf(' ' + cls + ' ') < 0) { setClass(el, (cur + cls).trim()) } } }
functionremoveClass (el, cls) { if (el.classList) { el.classList.remove(cls) } else { var cur = ' ' + getClass(el) + ' ' var tar = ' ' + cls + ' ' while (cur.indexOf(tar) >= 0) { cur = cur.replace(tar, ' ') } setClass(el, cur.trim()) } if (!el.className) { el.removeAttribute('class') } }
functionapply (el, key, fn) { key = key.trim() if (key.indexOf(' ') === -1) { fn(el, key) return } // The key contains one or more space characters. // Since a class name doesn't accept such characters, we // treat it as multiple classes. var keys = key.split(/\s+/) for (var i = 0, l = keys.length; i < l; i++) { fn(el, keys[i]) } }