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
|
|
|
|
)
|
2019-07-16 11:45:23 +08:00
|
|
|
// 是否直接替换当前容器
|
|
|
|
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
|
|
|
|
var obj = {}
|
|
|
|
|
|
|
|
val = toJson(val)
|
|
|
|
|
|
|
|
if (this.param) {
|
2019-07-16 11:45:23 +08:00
|
|
|
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)) {
|
2019-07-16 11:45:23 +08:00
|
|
|
obj[this.param] = val.toISOString()
|
2018-08-04 16:26:50 +08:00
|
|
|
} else {
|
|
|
|
obj[this.param] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj[this.param] = val
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-16 11:45:23 +08:00
|
|
|
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') {
|
2019-07-16 11:45:23 +08:00
|
|
|
elem.style.cssText = obj[i]
|
2018-08-04 16:26:50 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (i === 'href' || i === 'src') {
|
|
|
|
elem[i] = obj[i]
|
|
|
|
} else {
|
2019-07-20 15:50:47 +08:00
|
|
|
// 修正这些值的显示
|
2018-08-04 16:26:50 +08:00
|
|
|
if (obj[i] === false || obj[i] === null || obj[i] === undefined) {
|
|
|
|
obj[i] = ''
|
|
|
|
}
|
|
|
|
|
2019-07-20 15:50:47 +08:00
|
|
|
if (
|
|
|
|
typeof elem[i] === 'boolean' ||
|
|
|
|
typeof elem[boolMap[i]] === 'boolean'
|
|
|
|
) {
|
|
|
|
var k = i
|
|
|
|
if (boolMap[i] && k !== boolMap[i]) {
|
|
|
|
k = boolMap[i]
|
|
|
|
}
|
2018-08-04 16:26:50 +08:00
|
|
|
//布尔属性必须使用el.xxx = true|false方式设值
|
2019-07-16 11:45:23 +08:00
|
|
|
obj[i] = !!obj[i]
|
2019-07-20 15:50:47 +08:00
|
|
|
elem[k] = obj[i]
|
2018-08-04 16:26:50 +08:00
|
|
|
|
|
|
|
if (!obj[i]) {
|
2019-07-20 15:50:47 +08:00
|
|
|
elem.removeAttribute(k)
|
2018-08-04 16:26:50 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 15:50:47 +08:00
|
|
|
//SVG只能使用setAttribute(xxx, yyy), HTML的固有属性必须elem.xxx = yyy
|
2019-07-16 11:45:23 +08:00
|
|
|
var isInnate = rsvg.test(elem) ? false : i in elem.cloneNode(false)
|
2018-08-04 16:26:50 +08:00
|
|
|
if (isInnate) {
|
2019-07-16 11:45:23 +08:00
|
|
|
elem[i] = obj[i]
|
2018-08-04 16:26:50 +08:00
|
|
|
} else {
|
|
|
|
if (typeof obj[i] === 'object') {
|
|
|
|
obj[i] = Date.isDate(obj[i])
|
2019-07-16 11:45:23 +08:00
|
|
|
? obj[i].toISOString()
|
2018-08-04 16:26:50 +08:00
|
|
|
: JSON.stringify(obj[i])
|
|
|
|
}
|
2019-07-16 11:45:23 +08:00
|
|
|
elem.setAttribute(i, obj[i])
|
2018-08-04 16:26:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|