| 1 |
import MaskedPattern from './pattern.js'; |
| 2 |
import IMask from '../core/holder.js'; |
| 3 |
import ChangeDetails from '../core/change-details.js'; |
| 4 |
import { DIRECTION } from '../core/utils.js'; |
| 5 |
import ContinuousTailDetails from '../core/continuous-tail-details.js'; |
| 6 |
import './base.js'; |
| 7 |
import './factory.js'; |
| 8 |
import './pattern/chunk-tail-details.js'; |
| 9 |
import './pattern/cursor.js'; |
| 10 |
import './pattern/fixed-definition.js'; |
| 11 |
import './pattern/input-definition.js'; |
| 12 |
import './regexp.js'; |
| 13 |
|
| 14 |
/** Pattern which validates enum values */ |
| 15 |
class MaskedEnum extends MaskedPattern { |
| 16 |
constructor(opts) { |
| 17 |
super({ |
| 18 |
...MaskedEnum.DEFAULTS, |
| 19 |
...opts |
| 20 |
}); // mask will be created in _update |
| 21 |
} |
| 22 |
updateOptions(opts) { |
| 23 |
super.updateOptions(opts); |
| 24 |
} |
| 25 |
_update(opts) { |
| 26 |
const { |
| 27 |
enum: enum_, |
| 28 |
...eopts |
| 29 |
} = opts; |
| 30 |
if (enum_) { |
| 31 |
const lengths = enum_.map(e => e.length); |
| 32 |
const requiredLength = Math.min(...lengths); |
| 33 |
const optionalLength = Math.max(...lengths) - requiredLength; |
| 34 |
eopts.mask = '*'.repeat(requiredLength); |
| 35 |
if (optionalLength) eopts.mask += '[' + '*'.repeat(optionalLength) + ']'; |
| 36 |
this.enum = enum_; |
| 37 |
} |
| 38 |
super._update(eopts); |
| 39 |
} |
| 40 |
_appendCharRaw(ch, flags) { |
| 41 |
if (flags === void 0) { |
| 42 |
flags = {}; |
| 43 |
} |
| 44 |
const matchFrom = Math.min(this.nearestInputPos(0, DIRECTION.FORCE_RIGHT), this.value.length); |
| 45 |
const matches = this.enum.filter(e => this.matchValue(e, this.unmaskedValue + ch, matchFrom)); |
| 46 |
if (matches.length) { |
| 47 |
if (matches.length === 1) { |
| 48 |
this._forEachBlocksInRange(0, this.value.length, (b, bi) => { |
| 49 |
const mch = matches[0][bi]; |
| 50 |
if (bi >= this.value.length || mch === b.value) return; |
| 51 |
b.reset(); |
| 52 |
b._appendChar(mch, flags); |
| 53 |
}); |
| 54 |
} |
| 55 |
const d = super._appendCharRaw(matches[0][this.value.length], flags); |
| 56 |
if (matches.length === 1) { |
| 57 |
matches[0].slice(this.unmaskedValue.length).split('').forEach(mch => d.aggregate(super._appendCharRaw(mch))); |
| 58 |
} |
| 59 |
return d; |
| 60 |
} |
| 61 |
return new ChangeDetails({ |
| 62 |
skip: !this.isComplete |
| 63 |
}); |
| 64 |
} |
| 65 |
extractTail(fromPos, toPos) { |
| 66 |
if (fromPos === void 0) { |
| 67 |
fromPos = 0; |
| 68 |
} |
| 69 |
if (toPos === void 0) { |
| 70 |
toPos = this.displayValue.length; |
| 71 |
} |
| 72 |
// just drop tail |
| 73 |
return new ContinuousTailDetails('', fromPos); |
| 74 |
} |
| 75 |
remove(fromPos, toPos) { |
| 76 |
if (fromPos === void 0) { |
| 77 |
fromPos = 0; |
| 78 |
} |
| 79 |
if (toPos === void 0) { |
| 80 |
toPos = this.displayValue.length; |
| 81 |
} |
| 82 |
if (fromPos === toPos) return new ChangeDetails(); |
| 83 |
const matchFrom = Math.min(super.nearestInputPos(0, DIRECTION.FORCE_RIGHT), this.value.length); |
| 84 |
let pos; |
| 85 |
for (pos = fromPos; pos >= 0; --pos) { |
| 86 |
const matches = this.enum.filter(e => this.matchValue(e, this.value.slice(matchFrom, pos), matchFrom)); |
| 87 |
if (matches.length > 1) break; |
| 88 |
} |
| 89 |
const details = super.remove(pos, toPos); |
| 90 |
details.tailShift += pos - fromPos; |
| 91 |
return details; |
| 92 |
} |
| 93 |
get isComplete() { |
| 94 |
return this.enum.indexOf(this.value) >= 0; |
| 95 |
} |
| 96 |
} |
| 97 |
/** Match enum value */ |
| 98 |
MaskedEnum.DEFAULTS = { |
| 99 |
...MaskedPattern.DEFAULTS, |
| 100 |
matchValue: (estr, istr, matchFrom) => estr.indexOf(istr, matchFrom) === matchFrom |
| 101 |
}; |
| 102 |
IMask.MaskedEnum = MaskedEnum; |
| 103 |
|
| 104 |
export { MaskedEnum as default }; |
| 105 |
|