This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
anot.js/src/16-:attr.js

132 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-08-04 16:26:50 +08:00
var bools = [
'autofocus,autoplay,async,allowTransparency,checked,controls',
'declare,disabled,defer,defaultChecked,defaultSelected',
'contentEditable,isMap,loop,multiple,noHref,noResize,noShade',
'open,readOnly,selected'
].join(',')
var boolMap = {}
bools.replace(rword, function(name) {
boolMap[name.toLowerCase()] = name
})
var attrDir = Anot.directive('attr', {
init: function(binding) {
//{{aaa}} --> aaa
//{{aaa}}/bbb.html --> (aaa) + "/bbb.html"
binding.expr = normalizeExpr(binding.expr.trim())
if (binding.type === 'include') {
var elem = binding.element
effectBinding(elem, binding)
binding.includeRendered = getBindingCallback(
elem,
'data-rendered',
binding.vmodels
)
binding.includeLoaded = getBindingCallback(
elem,
'data-loaded',
binding.vmodels
)
// 是否直接替换当前容器
var outer = (binding.includeReplace = elem.hasAttribute('replace'))
if (elem.hasAttribute('cache')) {
2018-08-04 16:26:50 +08:00
binding.templateCache = {}
}
binding.start = DOC.createComment(':include')
binding.end = DOC.createComment(':include-end')
if (outer) {
binding.element = binding.end
binding._element = elem
elem.parentNode.insertBefore(binding.start, elem)
elem.parentNode.insertBefore(binding.end, elem.nextSibling)
} else {
elem.insertBefore(binding.start, elem.firstChild)
elem.appendChild(binding.end)
}
}
},
update: function(val) {
var elem = this.element
2020-07-06 14:46:15 +08:00
var obj = Object.create(null)
2020-07-06 14:38:22 +08:00
var isSVG = rsvg.test(elem)
2018-08-04 16:26:50 +08:00
val = toJson(val)
if (this.param) {
if (val && typeof val === 'object') {
2018-08-04 16:26:50 +08:00
if (Array.isArray(val)) {
obj[this.param] = val
} else {
if (Date.isDate(val)) {
obj[this.param] = val.toISOString()
2018-08-04 16:26:50 +08:00
} else {
obj[this.param] = val
}
}
} else {
obj[this.param] = val
}
} else {
if (
!val ||
typeof val !== 'object' ||
Array.isArray(val) ||
Date.isDate(val)
) {
2018-08-04 16:26:50 +08:00
return
}
obj = val
}
for (var i in obj) {
if (i === 'style') {
elem.style.cssText = obj[i]
2018-08-04 16:26:50 +08:00
continue
}
2020-07-06 14:46:15 +08:00
if (i.slice(0, 6) === 'xlink:') {
var k = i
i = i.slice(6)
obj[i] = obj[k]
delete obj[k]
}
2020-07-06 14:38:22 +08:00
// 修正这些值的显示
if (obj[i] === false || obj[i] === null || obj[i] === undefined) {
obj[i] = ''
}
2018-08-04 16:26:50 +08:00
2020-07-06 14:38:22 +08:00
if (
typeof elem[i] === 'boolean' ||
typeof elem[boolMap[i]] === 'boolean'
) {
var k = i
if (boolMap[i] && k !== boolMap[i]) {
k = boolMap[i]
}
//布尔属性必须使用el.xxx = true|false方式设值
obj[i] = !!obj[i]
elem[k] = obj[i]
2018-08-04 16:26:50 +08:00
2020-07-06 14:38:22 +08:00
if (!obj[i]) {
elem.removeAttribute(k)
continue
2018-08-04 16:26:50 +08:00
}
2020-07-06 14:38:22 +08:00
}
2018-08-04 16:26:50 +08:00
2020-07-06 14:38:22 +08:00
//SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy
var isInnate = isSVG ? false : i in elem.cloneNode(false)
if (isInnate) {
elem[i] = obj[i]
} else {
if (typeof obj[i] === 'object') {
obj[i] = Date.isDate(obj[i])
? obj[i].toISOString()
: JSON.stringify(obj[i])
2018-08-04 16:26:50 +08:00
}
2020-07-06 14:38:22 +08:00
elem.setAttribute(i, obj[i])
2018-08-04 16:26:50 +08:00
}
}
}
})