rc-tween-one
Version:
tween-one anim component for react
92 lines (78 loc) • 3.05 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var ChildrenPlugin = /*#__PURE__*/(0, _createClass2.default)( // eslint-disable-next-line no-useless-constructor,no-empty-function
function ChildrenPlugin(_vars, _key) {
var _this = this;
(0, _classCallCheck2.default)(this, ChildrenPlugin);
this.start = void 0;
this.startAt = void 0;
this.target = void 0;
this.vars = void 0;
this.key = void 0;
this.getAnimStart = function () {
var target = _this.target,
vars = _this.vars,
startAt = _this.startAt,
key = _this.key;
var formatMoney = vars.formatMoney;
var opts = {
thousand: formatMoney && formatMoney.thousand || ',',
decimal: formatMoney && formatMoney.decimal || '.'
};
var rep = new RegExp("\\".concat(opts.thousand), 'g');
_this.start = startAt[key] || {
value: parseFloat(target.innerHTML.replace(rep, '').replace(opts.decimal, '.')) || 0
};
return _this.start;
};
this.toMoney = function (v, _opts) {
var opts = {
thousand: _opts.thousand || ',',
decimal: _opts.decimal || '.'
};
var negative = parseFloat(v) < 0 ? '-' : '';
var numberArray = v.split('.');
var base = Math.abs(parseInt(numberArray[0], 10)).toString();
var mod = base.length > 3 ? base.length % 3 : 0;
var decimal = numberArray[1];
return "".concat(negative).concat(mod ? "".concat(base.substr(0, mod)).concat(opts.thousand) : '').concat(base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1".concat(opts.thousand))).concat(decimal ? "".concat(opts.decimal).concat(decimal) : '');
};
this.render = function (ratio) {
var _this$vars = _this.vars,
value = _this$vars.value,
floatLength = _this$vars.floatLength,
formatMoney = _this$vars.formatMoney;
var v = (value - _this.start.value) * ratio + _this.start.value;
if (typeof floatLength === 'number') {
if (floatLength) {
v = v.toFixed(floatLength);
var numberArray = v.toString().split('.');
var decimal = numberArray[1] || '';
decimal = decimal.length > floatLength ? decimal.substring(0, floatLength) : decimal;
var l = floatLength - decimal.length;
if (l) {
Array(l).fill(0).forEach(function (num) {
decimal += "".concat(num);
});
}
v = "".concat(numberArray[0], ".").concat(decimal);
} else {
v = Math.round(v);
}
}
v = formatMoney ? _this.toMoney("".concat(v), formatMoney) : v;
return v;
};
this.vars = _vars;
this.key = _key;
});
ChildrenPlugin.key = 'innerHTML';
ChildrenPlugin.className = 'Children';
var _default = ChildrenPlugin;
exports.default = _default;