-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (93 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import $, { extend, isFunction, noop } from 'jquery'
const defaults = {
ele: '.checkbox',
hoverCls: 'hover',
activeCls: 'active',
checkedCls: 'checked',
disabledCls: 'disabled',
callback: noop
}
const ACTIONS = {
DEFAULT: 0x00,
ENTER: 0x01,
LEAVE: 0x05,
PRESS: 0x02,
DISABLED: 0x03,
CHECKED: 0x04
}
class CheckBox {
constructor (options) {
this.options = extend(defaults, options)
const { ele } = this.options
this.$ele = $(ele)
if (this.$ele[0]._initialized) return
this.$ele[0]._initialized = true
this.bindEvents()
}
checkState (state) {
const { hoverCls, activeCls, checkedCls, disabledCls } = this.options
switch (state) {
case ACTIONS.ENTER:
this.$ele.addClass(hoverCls).removeClass([activeCls, disabledCls].join(' '))
break
case ACTIONS.LEAVE:
this.$ele.removeClass([hoverCls, hoverCls].join(' '))
break
case ACTIONS.PRESS:
this.$ele.addClass(activeCls).removeClass([hoverCls, disabledCls].join(' '))
break
case ACTIONS.CHECKED:
this.$ele.addClass(checkedCls).removeClass([activeCls, hoverCls, disabledCls].join(' '))
break
case ACTIONS.DISABLED:
this.$ele.addClass(disabledCls).removeClass([hoverCls, activeCls].join(' '))
break
default:
this.$ele.removeClass([hoverCls, activeCls, disabledCls, checkedCls].join(' '))
}
}
bindEvents () {
this.$ele.on('mousedown', () => {
if (this.isDisabled()) return
this.checkState(ACTIONS.PRESS)
})
this.$ele.on('mouseenter', () => {
if (this.isDisabled()) return
this.checkState(ACTIONS.ENTER)
})
this.$ele.on('mouseleave', () => {
if (this.isDisabled()) return
this.checkState(ACTIONS.LEAVE)
})
this.$ele.on('click', () => {
if (this.isDisabled()) return
this.state = !this.state
if (this.state) {
this.checkState(ACTIONS.CHECKED)
} else {
this.checkState(ACTIONS.DEFAULT)
}
})
}
isDisabled () {
return this.$ele.hasClass(this.options.disabledCls)
}
}
$.fn.checkBox = function $checkBox (options = {}) {
return this.each(function () {
return new CheckBox(
isFunction(options) ? {
...options,
callback: options,
ele: this
} : {
...options,
ele: this
}
)
})
}
// Initialize
export default $(() => {
return $('.checkbox').checkBox()
})