react-dayo
Version:
A Queue component for notification etc
136 lines • 5.32 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var core_dayo_1 = require("core-dayo");
var queue_1 = __importDefault(require("../components/queue"));
var box_1 = __importDefault(require("../components/box"));
exports.defaultOptions = {
to: 'top',
position: 'center',
maxLength: 5,
};
/**
* To create the `Dayo` component and also`dispatch` function to pass a seed into `Dayo`
*/
exports.createDayo = function (userOptions) {
if (userOptions === void 0) { userOptions = {}; }
var options = __assign({}, exports.defaultOptions, userOptions);
var dispatcher = new core_dayo_1.Dispatcher();
/**
* Interface
*/
var Dayo = /** @class */ (function (_super) {
__extends(Dayo, _super);
function Dayo() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.dispatcher = dispatcher;
_this.operator = new core_dayo_1.DayoOperator(_this);
_this.selector = new core_dayo_1.DayoSelector(_this);
_this.state = {
queue: [],
};
/**
* To be called when the cycle step is updated
*
* if the step of current is 'enter' then addind it into the queue,
* or else just re-rendering with new state
*
*/
_this.handleUpdateSeed = function (seedOnCycle) {
if (seedOnCycle.cycle.isEnter()) {
_this.operator.addSeed(seedOnCycle);
_this.operator.skipOverflowSeeds({
maxLength: _this.getOption('maxLength'),
});
}
return _this.operator.rewriteQueueItem(seedOnCycle);
};
/**
* To be called when the cycle step is ended for removing a ended seed from the queue
*/
_this.handleDoneSeed = function (seed) {
var queue = _this.state.queue;
var nextQueue = queue.filter(function (item) {
return item.id !== seed.id;
});
_this.setQueue(nextQueue);
};
return _this;
}
Dayo.prototype.getQueue = function () {
return this.state.queue;
};
Dayo.prototype.setQueue = function (queue) {
this.setState({ queue: queue });
};
/**
* To just return `option`
*/
Dayo.prototype.getOption = function (key) {
return (this.props[key] ||
options[key]);
};
/**
* To be called when rendered in the dom
*/
Dayo.prototype.componentDidMount = function () {
dispatcher.on(core_dayo_1.Event.UpdateSeed, this.handleUpdateSeed);
dispatcher.on(core_dayo_1.Event.DoneSeed, this.handleDoneSeed);
};
/**
* Advance to the next cycle step at the css animation ended
*/
Dayo.prototype.onTransitionEnd = function (seedOnCycle) {
return function () {
if (seedOnCycle.cycle.isEntering()) {
seedOnCycle.cycle.proceed();
return;
}
if (seedOnCycle.cycle.isExiting()) {
seedOnCycle.cycle.proceed();
}
};
};
/**
* To just render component
*/
Dayo.prototype.render = function () {
var _this = this;
return (react_1.default.createElement(queue_1.default, __assign({}, this.selector.getQueueComponentProps()), this.state.queue.map(function (seedOnCycle) {
return (react_1.default.createElement(box_1.default, __assign({ key: seedOnCycle.id }, _this.selector.getBoxComponentProps(seedOnCycle)),
react_1.default.createElement("div", null, seedOnCycle.message)));
})));
};
Dayo.defaultProps = options;
return Dayo;
}(react_1.default.Component));
return [Dayo, dispatcher.dispatch];
};
//# sourceMappingURL=dayo.js.map