2019-08-08 16:59:37 +08:00
|
|
|
/**
|
|
|
|
* 公共方法库
|
|
|
|
* @author yutent<yutent@doui.cc>
|
|
|
|
* @date 2019/08/08 10:47:55
|
|
|
|
*/
|
|
|
|
|
|
|
|
function noop() {}
|
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
export default {
|
|
|
|
/**
|
|
|
|
* 异步回调
|
|
|
|
*/
|
|
|
|
nextTick: (function() {
|
|
|
|
let queue = []
|
|
|
|
function callback() {
|
|
|
|
let n = queue.length
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
queue[i]()
|
|
|
|
}
|
|
|
|
queue = queue.slice(n)
|
2019-08-08 16:59:37 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
let node = document.createTextNode('<!-- -->')
|
|
|
|
new MutationObserver(callback).observe(node, { characterData: true })
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
let bool = false
|
|
|
|
return function(fn) {
|
|
|
|
queue.push(fn)
|
|
|
|
bool = !bool
|
|
|
|
node.data = bool
|
|
|
|
}
|
|
|
|
})(),
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
/**
|
|
|
|
* 对象/数组遍历
|
|
|
|
* 支持跳出
|
|
|
|
*/
|
|
|
|
each(obj, fn) {
|
|
|
|
if (obj) {
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
for (let i = 0, it; (it = obj[i++]); ) {
|
|
|
|
if (fn(it, i - 1) === false) {
|
|
|
|
break
|
|
|
|
}
|
2019-08-08 16:59:37 +08:00
|
|
|
}
|
2019-12-19 01:04:15 +08:00
|
|
|
} else {
|
|
|
|
for (let i in obj) {
|
|
|
|
if (obj.hasOwnProperty(i) && fn(obj[i], i) === false) {
|
|
|
|
break
|
|
|
|
}
|
2019-08-08 16:59:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 01:04:15 +08:00
|
|
|
},
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
/**
|
|
|
|
* 事件绑定
|
|
|
|
*/
|
|
|
|
bind(dom, type, fn = noop, phase = false) {
|
|
|
|
let events = type.split(',')
|
|
|
|
this.each(events, function(t) {
|
|
|
|
t = t.trim()
|
|
|
|
dom.addEventListener(t, fn, phase)
|
|
|
|
})
|
|
|
|
return fn
|
|
|
|
},
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
/**
|
|
|
|
* 事件绑定(默认不冒泡)
|
|
|
|
*/
|
|
|
|
catch(dom, type, fn, phase) {
|
|
|
|
function fn2(ev) {
|
|
|
|
ev.stopPropagation()
|
|
|
|
fn && fn(ev)
|
|
|
|
}
|
|
|
|
return this.bind(dom, type, fn2, phase)
|
|
|
|
},
|
2019-08-08 16:59:37 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
/**
|
|
|
|
* 解除事件绑定
|
|
|
|
*/
|
|
|
|
unbind(dom, type, fn = noop, phase = false) {
|
|
|
|
let events = type.split(',')
|
|
|
|
this.each(events, function(t) {
|
|
|
|
t = t.trim()
|
|
|
|
dom.removeEventListener(t, fn, phase)
|
|
|
|
})
|
|
|
|
},
|
2019-08-19 14:15:18 +08:00
|
|
|
|
2019-12-19 01:04:15 +08:00
|
|
|
// 指定节点外点击(最高不能超过body层)
|
|
|
|
outside(dom, fn = noop) {
|
|
|
|
return this.bind(document, 'mousedown', ev => {
|
|
|
|
if (ev) {
|
|
|
|
if (ev.path) {
|
|
|
|
var path = ev.path.concat()
|
|
|
|
while (path.length > 3) {
|
|
|
|
if (path.shift() === dom) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var target = ev.explicitOriginalTarget || ev.target
|
|
|
|
if (
|
|
|
|
dom === target ||
|
|
|
|
dom.contains(target) ||
|
|
|
|
(dom.root && dom.root.contains(target))
|
|
|
|
) {
|
2019-08-28 21:11:17 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 14:15:18 +08:00
|
|
|
}
|
2019-12-19 01:04:15 +08:00
|
|
|
fn(ev)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
clearOutside(fn = noop) {
|
|
|
|
this.unbind(document, 'mousedown', fn)
|
|
|
|
}
|
2019-08-19 14:15:18 +08:00
|
|
|
}
|