-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (137 loc) · 3.64 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import $, { extend, isFunction, noop } from 'jquery'
const defaults = {
ele: '.J_InputNumber',
input: '.J_NumberInput',
plusBtn: '.J_NumberPlus',
minusBtn: '.J_NumberMinus',
disabledCls: 'disabled',
float: false,
max: 99999999,
min: 0,
step: 0,
currentValue: 0,
callback: noop
}
const ACTIONS = {
PLUS: 1,
CHANGE: 0,
MINUS: -1
}
class InputNumber {
constructor (options) {
this.options = extend(defaults, options)
const { ele, plusBtn, input, minusBtn, currentValue } = this.options
this.$ele = $(ele)
this.$input = this.$ele.children(input)
this.$plusBtn = this.$ele.children(plusBtn)
this.$minusBtn = this.$ele.children(minusBtn)
this.currentValue = +this.$input.val() || currentValue
this.checkState()
this.bindEvents()
}
checkState (nextValue) {
nextValue = nextValue !== undefined ? nextValue : this.currentValue
const { min, max, disabledCls } = this.options
const gtMax = nextValue >= max
const ltMin = nextValue <= min
const disabledPlus = this.$plusBtn.hasClass(disabledCls)
const disabledMinus = this.$minusBtn.hasClass(disabledCls)
if (gtMax) {
if (!disabledPlus) {
this.$plusBtn.addClass(disabledCls)
}
} else {
if (disabledPlus) {
this.$plusBtn.removeClass(disabledCls)
}
}
if (ltMin) {
if (!disabledMinus) {
this.$minusBtn.addClass(disabledCls)
}
} else {
if (disabledMinus) {
this.$minusBtn.removeClass(disabledCls)
}
}
}
bindEvents () {
const self = this
const { float } = this.options
self.$plusBtn.click(function () {
let value = self.getNextVal(ACTIONS.PLUS)
self.$input.val(value)
self.$input.attr('value', value)
})
self.$minusBtn.click(function () {
let value = self.getNextVal(ACTIONS.MINUS)
self.$input.val(value)
self.$input.attr('value', value)
})
self.$input.on('change input', function (e) {
let nextValue = e.target.value
let isNumber = /^\d*$/.test(nextValue)
if (isNumber) {
if (!float) {
if (!Number.isInteger(nextValue)) {
nextValue = parseInt(nextValue)
}
}
} else {
nextValue = +nextValue.replace(/[^0-9]/g, '')
}
let value = self.getNextVal(ACTIONS.CHANGE, nextValue)
self.$input.val(value)
self.$input.attr('value', value)
})
}
getNextVal (action, nextValue) {
nextValue = nextValue !== undefined ? nextValue : this.currentValue
const { min, max, step } = this.options
if (Number.isNaN(nextValue)) {
return 0
}
if (action === ACTIONS.PLUS) {
nextValue = step ? nextValue - step : ++nextValue
} else if (action === ACTIONS.MINUS) {
nextValue = step ? nextValue - step : --nextValue
}
const gtMax = nextValue > max
const ltMin = nextValue <= min
if (gtMax) {
nextValue = max
}
if (ltMin) {
nextValue = min
}
this.execute(nextValue)
return nextValue
}
execute (nextValue) {
if (this.currentValue !== nextValue) {
this.currentValue = nextValue
this.checkState(nextValue)
this.options.callback(nextValue)
}
}
}
$.fn.inputNumber = function $inputNumber (options = {}) {
return this.each(function () {
return new InputNumber(
isFunction(options) ? {
...options,
callback: options,
ele: this
} : {
...options,
ele: this
}
)
})
}
// Initialize .J_InputNumber with callback function
export default $(() => {
return $('.J_InputNumber').inputNumber(function callback (value) {
console.log(value)
})
})