Repository URL to install this package:
|
Version:
0.9.5 ▾
|
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const mobx_1 = require("mobx");
const deps_1 = require("./deps");
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 = deps_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);
}
setMinValue(value) {
this.minValue = parseInt(value);
}
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);
}
setSize(size) {
this.size = parseInt(size);
this.value = this.minValue;
}
setSizes(sizes) {
this.sizes = sizes;
}
setTotal(total) {
this.total = parseInt(total);
}
/**
* 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;
}
}
__decorate([
mobx_1.observable
], State.prototype, "value", void 0);
__decorate([
mobx_1.observable
], State.prototype, "minValue", void 0);
__decorate([
mobx_1.observable
], State.prototype, "adaptive", void 0);
__decorate([
mobx_1.observable
], State.prototype, "delta", void 0);
__decorate([
mobx_1.observable
], State.prototype, "size", void 0);
__decorate([
mobx_1.observable
], State.prototype, "sizes", void 0);
__decorate([
mobx_1.observable
], State.prototype, "total", void 0);
__decorate([
mobx_1.action
], State.prototype, "setValue", null);
__decorate([
mobx_1.action
], State.prototype, "setMinValue", null);
__decorate([
mobx_1.action.bound
], State.prototype, "nextPage", null);
__decorate([
mobx_1.action.bound
], State.prototype, "previousPage", null);
__decorate([
mobx_1.action
], State.prototype, "setAdaptive", null);
__decorate([
mobx_1.action
], State.prototype, "setDelta", null);
__decorate([
mobx_1.action
], State.prototype, "setSize", null);
__decorate([
mobx_1.action
], State.prototype, "setTotal", null);
__decorate([
mobx_1.computed
], State.prototype, "start", null);
__decorate([
mobx_1.computed
], State.prototype, "options", null);
__decorate([
mobx_1.computed
], State.prototype, "length", null);
__decorate([
mobx_1.computed
], State.prototype, "maxValue", null);
__decorate([
mobx_1.computed
], State.prototype, "isFirst", null);
__decorate([
mobx_1.computed
], State.prototype, "isLast", null);
exports.State = State;
exports.default = State;
//# sourceMappingURL=State.js.map