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/17-:class.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

//类名定义 :class="{xx: yy}" :class="xx"
2018-08-04 16:26:50 +08:00
Anot.directive('class', {
init: function(binding) {
binding.expr = binding.expr.replace(/\n/g, ' ').replace(/\s+/g, ' ')
2018-08-04 16:26:50 +08:00
if (binding.type === 'hover' || binding.type === 'active') {
var expr = new Function('return ' + binding.expr)()
2018-08-04 16:26:50 +08:00
//确保只绑定一次
if (!binding.hasBindEvent) {
var elem = binding.element
var $elem = Anot(elem)
var activate = 'mouseenter' //在移出移入时切换类名
var abandon = 'mouseleave'
if (binding.type === 'active') {
//在聚焦失焦中切换类名
elem.tabIndex = elem.tabIndex || -1
activate = 'mousedown'
abandon = 'mouseup'
var fn0 = $elem.bind('mouseleave', function() {
$elem.removeClass(expr)
2018-08-04 16:26:50 +08:00
})
}
}
var fn1 = $elem.bind(activate, function() {
$elem.addClass(expr)
2018-08-04 16:26:50 +08:00
})
var fn2 = $elem.bind(abandon, function() {
$elem.removeClass(expr)
2018-08-04 16:26:50 +08:00
})
binding.rollback = function() {
$elem.unbind('mouseleave', fn0)
$elem.unbind(activate, fn1)
$elem.unbind(abandon, fn2)
}
binding.hasBindEvent = true
}
},
update: function(val) {
if (this.type !== 'class') {
return
}
var obj = val
if (!obj || this.param)
return log(
'class指令语法错误 %c %s="%s"',
'color:#f00',
this.name,
this.expr
)
if (typeof obj === 'string') {
obj = {}
obj[val] = true
}
if (!Anot.isPlainObject(obj)) {
obj = obj.$model
}
for (var i in obj) {
this.element.classList.toggle(i, !!obj[i])
2018-08-04 16:26:50 +08:00
}
}
})
'hover,active'.replace(rword, function(name) {
directives[name] = directives['class']
})