-
Notifications
You must be signed in to change notification settings - Fork 0
/
licensePlateSelector.js
329 lines (315 loc) · 11.3 KB
/
licensePlateSelector.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* 基于jquery 移动端H5 车牌选择器
* author: pxsgdsb
* gitee:
* github:
*/
; (function ($) {
function LicensePlateSelector() {
// 输入框元素
this.input_dom = `<ul class="plate_input_box">
<li class="territory_key" data-type="territory_key"></li>
<li style="margin-right:.8rem;"></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li data-end="end"></li>
<li data-cls="new_energy" data-end="end" class="new_energy">
<span>新能源</span>
</li>
</ul>`
// 键盘元素
this.keyboard_dom = `
<!--地域键盘 -->
<div class="territory_keyboard">
<div class="top">
<span>关闭</span>
</div>
<ul class="keys">
<li>京</li>
<li>津</li>
<li>沪</li>
<li>渝</li>
<li>蒙</li>
<li>新</li>
<li>藏</li>
<li>宁</li>
<li>桂</li>
<li>黑</li>
<li>吉</li>
<li>辽</li>
<li>晋</li>
<li>冀</li>
<li>青</li>
<li>鲁</li>
<li>豫</li>
<li>苏</li>
<li>皖</li>
<li>浙</li>
<li>闽</li>
<li>赣</li>
<li>湘</li>
<li>鄂</li>
<li>粤</li>
<li>琼</li>
<li>甘</li>
<li>陕</li>
<li>贵</li>
<li>云</li>
<li>川</li>
<li>使</li>
</ul>
</div>
<!--字母键盘 -->
<div class="alphabet_keyboard">
<div class="top">
<span>关闭</span>
</div>
<ul class="keys">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>0</li>
<li>Q</li>
<li>W</li>
<li>E</li>
<li>R</li>
<li>T</li>
<li>Y</li>
<li>U</li>
<li>P</li>
<li>A</li>
<li>S</li>
<li>D</li>
<li>F</li>
<li>G</li>
<li>H</li>
<li>J</li>
<li>K</li>
<li>L</li>
<li>Z</li>
<li>X</li>
<li>C</li>
<li>V</li>
<li>B</li>
<li>N</li>
<li>M</li>
<li>警</li>
<li>港</li>
<li>澳</li>
<li>学</li>
<li>使</li>
<li>挂</li>
<li class="del">删除</li>
</ul>
</div>`
this.elemDom;
}
/**
* 初始化 车牌选择器
* @param {string} config.elem 元素
* @param {string} config.value 默认填充车牌
* @param {number} config.activeIndex 默认选中下标 (从0开始)
* @param {function} inputCallBack 输入事件回调
* @param {function} deleteCallBack 键盘删除事件回调
* @param {function} closeKeyCallBack 关闭键盘事件回调
*/
LicensePlateSelector.prototype.init = function (config) {
config = {
elem: config.elem,
value: config.value || "",
activeIndex: config.activeIndex || false,
inputCallBack: config.inputCallBack || false,
deleteCallBack: config.deleteCallBack || false,
closeKeyCallBack: config.closeKeyCallBack || false,
}
this.elemDom = $(config.elem);
this.elemDom.append(this.input_dom);
this.elemDom.append(this.keyboard_dom);
this.watchKeyboardEvents(function(val){
// 键盘输入回调
if(config.inputCallBack){
config.inputCallBack(val);
}
},function(){
// 键盘删除事件回调
if(config.deleteCallBack){
config.deleteCallBack();
}
},function(){
// 关闭键盘事件回调
if(config.closeKeyCallBack){
config.closeKeyCallBack();
}
}) // 监听键盘事件
// 输入默认车牌
if (config.value) {
this.elemDom.find(".plate_input_box li").each(function (index) {
if (config.value[index]) {
$(this).text(config.value[index])
}
})
}
// 选中默认下标
if(config.activeIndex){
this.elemDom.find(".plate_input_box li").eq(config.activeIndex).click();
}
};
/**
* 获取车牌
*/
LicensePlateSelector.prototype.getValue = function () {
let plate_number = ""
this.elemDom.find(".plate_input_box li").each(function () {
if ($(this).find("span").length == 0) {
plate_number += $(this).text()
}
})
return plate_number;
}
/**
* 设置车牌
*/
LicensePlateSelector.prototype.setValue = function (plate_number) {
this.elemDom.find(".plate_input_box li").each(function (index) {
if (index == 7) {
if(plate_number[index]){
$(this).removeClass("new_energy").attr("data-cls","")
$(this).html(plate_number[index])
}
}else{
$(this).text(plate_number[index] || '')
}
})
}
/**
* 清空车牌
*/
LicensePlateSelector.prototype.clearValue = function () {
this.elemDom.find(".plate_input_box li").each(function (index) {
if(index==7){
$(this).html("<span>新能源</span>").attr("data-cls","new_energy").attr("class","new_energy")
}else{
$(this).text("")
}
})
}
/**
* 监听键盘输入
* @param {function} inputCallBack 输入事件回调
* @param {function} deleteCallBack 键盘删除事件回调
* @param {function} closeKeyCallBack 关闭键盘事件回调
*/
LicensePlateSelector.prototype.watchKeyboardEvents = function(inputCallBack,deleteCallBack,closeKeyCallBack) {
let _this = this
// 输入框点击
_this.elemDom.find(".plate_input_box li").click(function (event) {
// 显示边框
$(".plate_input_this").removeClass("plate_input_this");
$(this).addClass("plate_input_this")
// 弹出键盘
// 关闭别的键盘
$(".territory_keyboard").css("display","none")
$(".alphabet_keyboard").css("display","none")
if ($(this).attr("data-type") && $(this).attr("data-type") == "territory_key") {
if (_this.elemDom.find(".territory_keyboard").css("display") == "none") {
_this.elemDom.find(".alphabet_keyboard").animate({ bottom: "-50rem" }).hide()
_this.elemDom.find(".territory_keyboard").show().animate({ bottom: 0 })
}
} else {
if (_this.elemDom.find(".alphabet_keyboard").css("display") == "none") {
_this.elemDom.find(".territory_keyboard").animate({ bottom: "-50rem" }).hide()
_this.elemDom.find(".alphabet_keyboard").show().animate({ bottom: 0 })
}
}
// 点击新能源
if ($(this).attr("data-cls") == "new_energy") {
$(this).empty().removeClass("new_energy").attr("data-cls", "")
}
event.stopPropagation(); // 阻止事件冒泡
})
// 地域键盘输入事件
_this.elemDom.find(".territory_keyboard .keys li").click(function (event) {
let val = $(this).text()
_this.elemDom.find(".plate_input_this").text(val)
_this.elemDom.find(".plate_input_this").next().click()
if(inputCallBack){
inputCallBack(val)
}
event.stopPropagation(); // 阻止事件冒泡
})
// 关闭地域键盘
_this.elemDom.find(".territory_keyboard .top span").click(function () {
_this.elemDom.find(".territory_keyboard").animate({ bottom: "-50rem" }).hide()
if(closeKeyCallBack){
closeKeyCallBack()
}
})
// 字母键盘输入事件
_this.elemDom.find(".alphabet_keyboard .keys li").click(function (event) {
// 点击删除
if ($(this).attr("class") == "del") {
let plate_input_this = _this.elemDom.find(".plate_input_this")
if(plate_input_this.text()){
_this.elemDom.find(".plate_input_this").text("")
}else{
_this.elemDom.find(".plate_input_this").prev().text("").click()
}
let new_energy = _this.elemDom.find(".plate_input_box li").eq(7);
if(!new_energy.hasClass("plate_input_this") && !new_energy.attr("data-cls")){
new_energy.attr("data-cls","new_energy").addClass("new_energy").append("<span>新能源</span>")
}
if(deleteCallBack){
deleteCallBack()
}
event.stopPropagation(); // 阻止事件冒泡
return
}
// 输入
let val = $(this).text()
_this.elemDom.find(".plate_input_this").text(val)
if (_this.elemDom.find(".plate_input_this").attr("data-end") == "end") {
_this.elemDom.find(".alphabet_keyboard").animate({ bottom: "-50rem" }).hide()
if(closeKeyCallBack){
closeKeyCallBack()
}
} else {
_this.elemDom.find(".plate_input_this").next().click()
}
if(inputCallBack){
inputCallBack(val)
}
event.stopPropagation(); // 阻止事件冒泡
})
// 关闭字母键盘
_this.elemDom.find(".alphabet_keyboard .top span").click(function () {
_this.elemDom.find(".alphabet_keyboard").animate({ bottom: "-50rem" }).hide()
if(closeKeyCallBack){
closeKeyCallBack()
}
})
$("body").click(function () {
_this.elemDom.find(".territory_keyboard").animate({ bottom: "-50rem" }).hide()
_this.elemDom.find(".alphabet_keyboard").animate({ bottom: "-50rem" }).hide()
if(closeKeyCallBack){
closeKeyCallBack()
}
})
_this.elemDom.find(".territory_keyboard").click(function (event) {
event.stopPropagation();
})
_this.elemDom.find(".alphabet_keyboard").click(function (event) {
event.stopPropagation();
})
}
if (!window.LicensePlateSelector) {
window.LicensePlateSelector = LicensePlateSelector;
}
})(jQuery);