UNPKG

1.79 kBJavaScriptView Raw
1// InputPassword 密码框
2var InputPassword = function InputPassword(container, params) {
3 /* -----------------------
4 Model
5 ----------------------- */
6 var defaults = {
7 revealClass: 'input-icon-reveal',
8 inputClass: 'input-text'
9 };
10 params = params || {};
11 for (var def in defaults) {
12 if (params[def] === undefined) {
13 params[def] = defaults[def];
14 }
15 }
16 var s = this;
17 s.params = params;
18
19 // Container
20 s.container = typeof container === 'string' ? document.querySelector(container) : container;
21 if (!s.container) {
22 console.log('SeedsUI Error:未找到InputPre的DOM对象,请检查传入参数是否正确');
23 return;
24 }
25
26 // Reveal
27 s.reveal = s.container.querySelector('.' + s.params.revealClass);
28 if (!s.container) {
29 console.log('SeedsUI Error:未找到InputPre的pre,请检查传入参数是否正确');
30 return;
31 }
32
33 // Input
34 s.input = s.container.querySelector('.' + s.params.inputClass);
35 if (!s.input) {
36 console.log('SeedsUI Error:未找到InputPre的input,请检查传入参数是否正确');
37 return;
38 }
39
40 /* -----------------------
41 Touch Events
42 ----------------------- */
43 s.events = function (detach) {
44 var touchTarget = s.reveal;
45 var action = detach ? 'removeEventListener' : 'addEventListener';
46 touchTarget[action]('click', s.revealAutoSize, false);
47 };
48 // attach、dettach事件
49 s.attach = function () {
50 s.events();
51 };
52 s.detach = function () {
53 s.events(true);
54 };
55 s.attach();
56 /* -----------------------
57 Method
58 ----------------------- */
59 s.onReveal = function () {
60 if (s.reveal.classList.contains('active')) {
61 s.reveal.classList.remove('active');
62 s.input.type = 'password';
63 } else {
64 s.reveal.classList.add('active');
65 s.input.type = 'text';
66 }
67 s.input.focus();
68 };
69};
70
71export default InputPassword;
\No newline at end of file