Repository URL to install this package:
|
Version:
4.0.116 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const tslib_1 = require("tslib");
const mobx_1 = require("xmobx/mobx");
const exotic_1 = require("exotic");
class State {
/**
* @note
* 1. now we can get the props to create the state
* 2. we can pass in the state
* 3. or we can default the state
*/
constructor(props = exotic_1.EMPTY_OBJ) {
this.value = 1;
this.minValue = 1;
this.adaptive = false;
this.delta = 4;
this.size = 20;
this.sizes = [10, 20, 50, 100, 200];
this.total = 100;
if (props.value) {
this.setValue(props.value);
}
if (props.minValue) {
this.setMinValue(props.minValue);
}
if (props.adaptive) {
this.setAdaptive(props.adaptive);
}
if (props.delta) {
this.setDelta(props.delta);
}
if (props.size) {
this.setSize(props.size);
}
if (props.sizes) {
this.setSizes(props.sizes);
}
if (props.total) {
this.setTotal(props.total);
}
}
setValue(value) {
this.value = parseInt(value, 10);
}
setMinValue(value) {
this.minValue = parseInt(value, 10);
}
nextPage(event) {
if (this.value < this.maxValue) {
this.value++;
}
}
previousPage(event) {
if (this.value > this.minValue) {
this.value--;
}
}
/**
* can do the same with delta
* so all can be configured without changing this code
* externally configurable 1st
*/
setAdaptive(adaptive) {
this.adaptive = adaptive === true || adaptive === 'true';
}
setDelta(delta) {
this.delta = parseInt(delta, 10);
}
setSize(size) {
this.size = parseInt(size, 10);
this.value = this.minValue;
}
setSizes(sizes) {
this.sizes = sizes;
}
setTotal(total) {
this.total = parseInt(total, 10);
}
/**
* this is so we can
*/
get start() {
const {
value,
maxValue,
minValue,
delta
} = this;
if (!this.adaptive) {
return minValue;
}
const offset = value - delta;
if (maxValue - value < delta) {
return maxValue - delta * 2;
} else {
return offset > 0 ? value - delta : minValue;
}
}
get options() {
const fromIndexToPlusStart = (x, index) => this.start + index || 0;
return Array.from({
length: this.length
}, fromIndexToPlusStart);
}
get length() {
return this.adaptive ? 1 + this.delta * 2 : this.maxValue - this.minValue + 1;
}
get maxValue() {
return Math.ceil(this.total / this.size);
}
get isFirst() {
return this.value === this.minValue;
}
get isLast() {
return this.value === this.maxValue;
}
}
tslib_1.__decorate([mobx_1.observable], State.prototype, "value", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "minValue", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "adaptive", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "delta", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "size", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "sizes", void 0);
tslib_1.__decorate([mobx_1.observable], State.prototype, "total", void 0);
tslib_1.__decorate([mobx_1.action], State.prototype, "setValue", null);
tslib_1.__decorate([mobx_1.action], State.prototype, "setMinValue", null);
tslib_1.__decorate([mobx_1.action.bound], State.prototype, "nextPage", null);
tslib_1.__decorate([mobx_1.action.bound], State.prototype, "previousPage", null);
tslib_1.__decorate([mobx_1.action], State.prototype, "setAdaptive", null);
tslib_1.__decorate([mobx_1.action], State.prototype, "setDelta", null);
tslib_1.__decorate([mobx_1.action], State.prototype, "setSize", null);
tslib_1.__decorate([mobx_1.action], State.prototype, "setTotal", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "start", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "options", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "length", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "maxValue", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "isFirst", null);
tslib_1.__decorate([mobx_1.computed], State.prototype, "isLast", null);
exports.State = State;
exports.default = State; //# sourceMappingURL=state.js.map