Skip to content

Commit bb8703e

Browse files
author
Kryazhev Alexey
committed
add pipe type, refactor
1 parent 03a7b68 commit bb8703e

File tree

7 files changed

+174
-29
lines changed

7 files changed

+174
-29
lines changed

dist/imask.js

Lines changed: 89 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/imask.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/imask.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/imask.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/imask.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BaseMask from './masks/base';
44
import RegExpMask from './masks/regexp';
55
import FuncMask from './masks/func';
66
import PatternMask from './masks/pattern';
7+
import PipeMask from './masks/pipe';
78

89

910
export default
@@ -20,6 +21,7 @@ IMask.MaskFactory = function (el, opts) {
2021
if (mask instanceof BaseMask) return mask;
2122
if (mask instanceof RegExp) return new RegExpMask(el, opts);
2223
if (mask instanceof Function) return new FuncMask(el, opts);
24+
if (mask instanceof Array) return new PipeMask(el, opts);
2325
if (isString(mask)) return new PatternMask(el, opts);
2426
return new BaseMask(el, opts);
2527
}

src/masks/base.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ class BaseMask {
4141
set rawValue (str) {
4242
this.processInput(str, {
4343
cursorPos: str.length,
44+
oldValue: this.rawValue,
4445
oldSelection: {
4546
start: 0,
4647
end: this.rawValue.length
47-
},
48+
}
4849
});
4950
}
5051

@@ -95,12 +96,16 @@ class BaseMask {
9596

9697

9798
get selectionStart () {
98-
return this.el.selectionStart;
99+
return this._cursorChanging ?
100+
this._changingCursorPos :
101+
102+
this.el.selectionStart;
99103
}
100104

101105
get cursorPos () {
102106
return this._cursorChanging ?
103107
this._changingCursorPos :
108+
104109
this.el.selectionEnd;
105110
}
106111

@@ -133,14 +138,17 @@ class BaseMask {
133138

134139
if (this.el.value !== value) this.el.value = value;
135140
if (this.cursorPos != cursorPos && cursorPos != null) {
136-
// also queue change cursor for some browsers
137-
if (this._cursorChanging) clearTimeout(this._cursorChanging);
138-
this._changingCursorPos = cursorPos;
139-
this._cursorChanging = setTimeout(() => {
140-
this.cursorPos = this._changingCursorPos;
141-
delete this._cursorChanging;
142-
}, 10);
143141
this.cursorPos = cursorPos;
142+
143+
// also queue change cursor for mobile browsers
144+
if (this._cursorChanging) clearTimeout(this._cursorChanging);
145+
if (this.cursorPos != cursorPos) {
146+
this._changingCursorPos = cursorPos;
147+
this._cursorChanging = setTimeout(() => {
148+
this.cursorPos = this._changingCursorPos;
149+
delete this._cursorChanging;
150+
}, 10);
151+
}
144152
}
145153
this.saveSelection();
146154

@@ -152,10 +160,6 @@ class BaseMask {
152160
}
153161

154162
_onInput (ev) {
155-
if (this._cursorChanging) {
156-
ev.preventDefault();
157-
return;
158-
}
159163
this.processInput(this.el.value);
160164
}
161165

src/masks/pipe.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import BaseMask from './base';
2+
import {extendDetailsAdjustments} from '../utils';
3+
4+
5+
export default
6+
class PipeMask extends BaseMask {
7+
constructor (el, opts) {
8+
super(el, opts);
9+
10+
this.multipass = opts.multipass;
11+
12+
this._compiledMasks = this.mask.map(m => IMask.MaskFactory(el, m));
13+
}
14+
15+
resolve (str, details) {
16+
var res = this._pipe(str, details);
17+
if (!this.multipass) return res;
18+
19+
var cursorPos = details.cursorPos;
20+
21+
var stepRes;
22+
var tempRes = res;
23+
24+
while (stepRes !== tempRes) {
25+
stepRes = tempRes;
26+
tempRes = this._pipe(stepRes, {
27+
cursorPos: stepRes.length,
28+
oldValue: stepRes,
29+
oldSelection: {
30+
start: 0,
31+
end: stepRes.length
32+
}
33+
});
34+
}
35+
36+
details.cursorPos = cursorPos - (res.length - stepRes.length);
37+
38+
return stepRes;
39+
}
40+
41+
_pipe (str, details) {
42+
return this._compiledMasks.reduce((s, m) => {
43+
var d = extendDetailsAdjustments(s, details);
44+
var res = m.resolve(s, d);
45+
details.cursorPos = d.cursorPos;
46+
return res;
47+
}, str);
48+
}
49+
50+
bindEvents () {
51+
super.bindEvents();
52+
this._compiledMasks.forEach(m => {
53+
m.bindEvents();
54+
// disable basemask events for child masks
55+
BaseMask.prototype.unbindEvents.apply(m);
56+
});
57+
}
58+
59+
unbindEvents () {
60+
super.unbindEvents();
61+
this._compiledMasks.forEach(m => m.unbindEvents());
62+
}
63+
}

0 commit comments

Comments
 (0)