Files
front/dist/assets/vendor/imask/esm/masked/dynamic.js
2021-12-28 13:34:18 +01:00

357 lines
10 KiB
JavaScript

import { d as _inherits, e as _createSuper, a as _classCallCheck, _ as _createClass, g as _get, h as _getPrototypeOf, i as _set, b as _objectWithoutProperties } from '../_rollupPluginBabelHelpers-a0b34764.js';
import ChangeDetails from '../core/change-details.js';
import createMask from './factory.js';
import Masked from './base.js';
import IMask from '../core/holder.js';
import '../core/utils.js';
import '../core/continuous-tail-details.js';
var _excluded = ["compiledMasks", "currentMaskRef", "currentMask"];
/** Dynamic mask for choosing apropriate mask in run-time */
var MaskedDynamic = /*#__PURE__*/function (_Masked) {
_inherits(MaskedDynamic, _Masked);
var _super = _createSuper(MaskedDynamic);
/** Currently chosen mask */
/** Compliled {@link Masked} options */
/** Chooses {@link Masked} depending on input value */
/**
@param {Object} opts
*/
function MaskedDynamic(opts) {
var _this;
_classCallCheck(this, MaskedDynamic);
_this = _super.call(this, Object.assign({}, MaskedDynamic.DEFAULTS, opts));
_this.currentMask = null;
return _this;
}
/**
@override
*/
_createClass(MaskedDynamic, [{
key: "_update",
value: function _update(opts) {
_get(_getPrototypeOf(MaskedDynamic.prototype), "_update", this).call(this, opts);
if ('mask' in opts) {
// mask could be totally dynamic with only `dispatch` option
this.compiledMasks = Array.isArray(opts.mask) ? opts.mask.map(function (m) {
return createMask(m);
}) : [];
}
}
/**
@override
*/
}, {
key: "_appendCharRaw",
value: function _appendCharRaw(ch) {
var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var details = this._applyDispatch(ch, flags);
if (this.currentMask) {
details.aggregate(this.currentMask._appendChar(ch, flags));
}
return details;
}
}, {
key: "_applyDispatch",
value: function _applyDispatch() {
var appended = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var prevValueBeforeTail = flags.tail && flags._beforeTailState != null ? flags._beforeTailState._value : this.value;
var inputValue = this.rawInputValue;
var insertValue = flags.tail && flags._beforeTailState != null ? // $FlowFixMe - tired to fight with type system
flags._beforeTailState._rawInputValue : inputValue;
var tailValue = inputValue.slice(insertValue.length);
var prevMask = this.currentMask;
var details = new ChangeDetails();
var prevMaskState = prevMask && prevMask.state; // clone flags to prevent overwriting `_beforeTailState`
this.currentMask = this.doDispatch(appended, Object.assign({}, flags)); // restore state after dispatch
if (this.currentMask) {
if (this.currentMask !== prevMask) {
// if mask changed reapply input
this.currentMask.reset();
if (insertValue) {
// $FlowFixMe - it's ok, we don't change current mask above
var d = this.currentMask.append(insertValue, {
raw: true
});
details.tailShift = d.inserted.length - prevValueBeforeTail.length;
}
if (tailValue) {
// $FlowFixMe - it's ok, we don't change current mask above
details.tailShift += this.currentMask.append(tailValue, {
raw: true,
tail: true
}).tailShift;
}
} else {
// Dispatch can do something bad with state, so
// restore prev mask state
this.currentMask.state = prevMaskState;
}
}
return details;
}
}, {
key: "_appendPlaceholder",
value: function _appendPlaceholder() {
var details = this._applyDispatch.apply(this, arguments);
if (this.currentMask) {
details.aggregate(this.currentMask._appendPlaceholder());
}
return details;
}
/**
@override
*/
}, {
key: "doDispatch",
value: function doDispatch(appended) {
var flags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return this.dispatch(appended, this, flags);
}
/**
@override
*/
}, {
key: "doValidate",
value: function doValidate() {
var _get2, _this$currentMask;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_get2 = _get(_getPrototypeOf(MaskedDynamic.prototype), "doValidate", this)).call.apply(_get2, [this].concat(args)) && (!this.currentMask || (_this$currentMask = this.currentMask).doValidate.apply(_this$currentMask, args));
}
/**
@override
*/
}, {
key: "reset",
value: function reset() {
if (this.currentMask) this.currentMask.reset();
this.compiledMasks.forEach(function (m) {
return m.reset();
});
}
/**
@override
*/
}, {
key: "value",
get: function get() {
return this.currentMask ? this.currentMask.value : '';
},
set: function set(value) {
_set(_getPrototypeOf(MaskedDynamic.prototype), "value", value, this, true);
}
/**
@override
*/
}, {
key: "unmaskedValue",
get: function get() {
return this.currentMask ? this.currentMask.unmaskedValue : '';
},
set: function set(unmaskedValue) {
_set(_getPrototypeOf(MaskedDynamic.prototype), "unmaskedValue", unmaskedValue, this, true);
}
/**
@override
*/
}, {
key: "typedValue",
get: function get() {
return this.currentMask ? this.currentMask.typedValue : '';
} // probably typedValue should not be used with dynamic
,
set: function set(value) {
var unmaskedValue = String(value); // double check it
if (this.currentMask) {
this.currentMask.typedValue = value;
unmaskedValue = this.currentMask.unmaskedValue;
}
this.unmaskedValue = unmaskedValue;
}
/**
@override
*/
}, {
key: "isComplete",
get: function get() {
return !!this.currentMask && this.currentMask.isComplete;
}
/**
@override
*/
}, {
key: "remove",
value: function remove() {
var details = new ChangeDetails();
if (this.currentMask) {
var _this$currentMask2;
details.aggregate((_this$currentMask2 = this.currentMask).remove.apply(_this$currentMask2, arguments)) // update with dispatch
.aggregate(this._applyDispatch());
}
return details;
}
/**
@override
*/
}, {
key: "state",
get: function get() {
return Object.assign({}, _get(_getPrototypeOf(MaskedDynamic.prototype), "state", this), {
_rawInputValue: this.rawInputValue,
compiledMasks: this.compiledMasks.map(function (m) {
return m.state;
}),
currentMaskRef: this.currentMask,
currentMask: this.currentMask && this.currentMask.state
});
},
set: function set(state) {
var compiledMasks = state.compiledMasks,
currentMaskRef = state.currentMaskRef,
currentMask = state.currentMask,
maskedState = _objectWithoutProperties(state, _excluded);
this.compiledMasks.forEach(function (m, mi) {
return m.state = compiledMasks[mi];
});
if (currentMaskRef != null) {
this.currentMask = currentMaskRef;
this.currentMask.state = currentMask;
}
_set(_getPrototypeOf(MaskedDynamic.prototype), "state", maskedState, this, true);
}
/**
@override
*/
}, {
key: "extractInput",
value: function extractInput() {
var _this$currentMask3;
return this.currentMask ? (_this$currentMask3 = this.currentMask).extractInput.apply(_this$currentMask3, arguments) : '';
}
/**
@override
*/
}, {
key: "extractTail",
value: function extractTail() {
var _this$currentMask4, _get3;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return this.currentMask ? (_this$currentMask4 = this.currentMask).extractTail.apply(_this$currentMask4, args) : (_get3 = _get(_getPrototypeOf(MaskedDynamic.prototype), "extractTail", this)).call.apply(_get3, [this].concat(args));
}
/**
@override
*/
}, {
key: "doCommit",
value: function doCommit() {
if (this.currentMask) this.currentMask.doCommit();
_get(_getPrototypeOf(MaskedDynamic.prototype), "doCommit", this).call(this);
}
/**
@override
*/
}, {
key: "nearestInputPos",
value: function nearestInputPos() {
var _this$currentMask5, _get4;
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return this.currentMask ? (_this$currentMask5 = this.currentMask).nearestInputPos.apply(_this$currentMask5, args) : (_get4 = _get(_getPrototypeOf(MaskedDynamic.prototype), "nearestInputPos", this)).call.apply(_get4, [this].concat(args));
}
}, {
key: "overwrite",
get: function get() {
return this.currentMask ? this.currentMask.overwrite : _get(_getPrototypeOf(MaskedDynamic.prototype), "overwrite", this);
},
set: function set(overwrite) {
console.warn('"overwrite" option is not available in dynamic mask, use this option in siblings');
}
}]);
return MaskedDynamic;
}(Masked);
MaskedDynamic.DEFAULTS = {
dispatch: function dispatch(appended, masked, flags) {
if (!masked.compiledMasks.length) return;
var inputValue = masked.rawInputValue; // simulate input
var inputs = masked.compiledMasks.map(function (m, index) {
m.reset();
m.append(inputValue, {
raw: true
});
m.append(appended, flags);
var weight = m.rawInputValue.length;
return {
weight: weight,
index: index
};
}); // pop masks with longer values first
inputs.sort(function (i1, i2) {
return i2.weight - i1.weight;
});
return masked.compiledMasks[inputs[0].index];
}
};
IMask.MaskedDynamic = MaskedDynamic;
export { MaskedDynamic as default };