react-dyn-tabs
Version:
React dynamic tabs with full API
60 lines • 1.83 kB
JavaScript
import helper from '../helper.js';
export default helper.module(function () {
this._publishers = {
onChange: [],
onLoad: [],
onDestroy: [],
onOpen: [],
onClose: [],
onSelect: [],
onInit: [],
_onFlushEffects: [],
_onReady: [],
onFirstSelect: []
};
}, {
//unSubscribe
off: function off(publisherName, fn) {
if (typeof fn === 'function' && Object.prototype.hasOwnProperty.call(this._publishers, publisherName)) {
var _index = this._publishers[publisherName].indexOf(fn);
_index >= 0 && this._publishers[publisherName].splice(_index, 1);
}
return this;
},
//subscribe
on: function on(publisherName, fn) {
if (typeof fn === 'function' && Object.prototype.hasOwnProperty.call(this._publishers, publisherName)) {
// check if it has not existed
if (this._publishers[publisherName].indexOf(fn) === -1) {
this._publishers[publisherName].push(fn);
}
}
return this;
},
//oneSubscribe
one: function one(publisherName, fn) {
if (typeof fn === 'function' && Object.prototype.hasOwnProperty.call(this._publishers, publisherName)) {
var _fn = function _fn() {
fn.apply(this, arguments);
this.off(publisherName, _fn);
};
return this.on(publisherName, _fn);
}
return this;
}
}, {
trigger: function trigger(publisherName, context, generateParamsCallback) {
if (generateParamsCallback === void 0) {
generateParamsCallback = function generateParamsCallback() {
return [];
};
}
context = context || null;
var result = [];
var _subscribers = [].concat(this._publishers[publisherName]);
_subscribers.forEach(function (subscriber) {
result.push(subscriber.apply(context, generateParamsCallback()));
});
return result;
}
});