Skip to content

Commit c592d7b

Browse files
committed
del _createSubscribers prop from api.factory.js
1 parent c7ee34d commit c592d7b

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/com/react-dynamic-tabs/utils/api/api.factory.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,40 @@ const api = function (getDeps, param = { options: {} }) {
1414
userProxy: getUserProxy(this)
1515
});
1616
this.pub_sub = pub_sub_Instance;
17-
this._createSubscribers();
17+
this._alterOnChangeCallback()._subscribeCallbacksOptions()._subscribeSelectedTabsHistory();
1818
};
1919
api.prototype = Object.create(BaseApi.prototype);
2020
api.prototype.constructor = api;
21-
api.prototype._createSubscribers = function () {
22-
const op = this.getOptions(), api = this.userProxy, _pp = this._panelProxy;
23-
this.pub_sub
24-
.subscribe('onLoad', () => { op.onLoad.call(api); })
25-
.subscribe('onDestroy', () => { op.onDestroy.call(api); })
26-
.subscribe('beforeSwitchTab', (id) => { _pp.addRenderedPanel(id); })
27-
.subscribe('onChange', ({ closedTabsId }) => { closedTabsId.map(id => { _pp.removeRenderedPanel(id); }); })
28-
.subscribe('onChange', ({ isSwitched, oldState }) => { isSwitched && this.activedTabsHistory.add(oldState.selectedTabID); })
29-
.subscribe('onChange', ({ newState, oldState, closedTabsId, openedTabsId, isSwitched }) => {
30-
const currentSelectedTabId = newState.selectedTabID
31-
, perviousSelectedTabId = oldState.selectedTabID;
32-
openedTabsId.length && op.onOpen.call(api, openedTabsId);
33-
closedTabsId.length && op.onClose.call(api, closedTabsId);
34-
isSwitched && op.onSelect.call(api, { currentSelectedTabId, perviousSelectedTabId });
35-
op.onChange.call(api, { currentData: newState, perviousData: oldState });
21+
api.prototype._alterOnChangeCallback = function () {
22+
const op = this.getOptions();
23+
op.onChange = op.onChange || (() => { });
24+
const oldOnchange = op.onChange;
25+
op.onChange = function ({ newState, oldState, closedTabsId, openedTabsId, isSwitched }) {
26+
oldOnchange.call(this, { currentData: newState, perviousData: oldState });
27+
openedTabsId.length && this.trigger('onOpen', openedTabsId, this);
28+
closedTabsId.length && this.trigger('onClose', closedTabsId, this);
29+
isSwitched && this.trigger('onSelect', {
30+
currentSelectedTabId: newState.selectedTabID,
31+
perviousSelectedTabId: oldState.selectedTabID
32+
}, this);
33+
};
34+
return this;
35+
};
36+
api.prototype._subscribeSelectedTabsHistory = function () {
37+
this.pub_sub.subscribe('onChange', ({ isSwitched, oldState }) => {
38+
isSwitched && this.activedTabsHistory.add(oldState.selectedTabID);
39+
});
40+
return this;
41+
};
42+
api.prototype._subscribeCallbacksOptions = function () {
43+
const op = this.getOptions();
44+
Object.keys(this.pub_sub.publishers).map(publisher => {
45+
this.pub_sub.subscribe(publisher, param => {
46+
if (op.hasOwnProperty(publisher) && typeof op[publisher] === 'function')
47+
op[publisher].call(this, param);
3648
});
49+
});
50+
return this;
3751
};
3852
api.prototype.getCopyPerviousData = function () { return this.helper.getCopyState(this.perviousState); };
3953
api.prototype.getOptions = function () { return this.optionManager.getMutableOptions(); };

src/com/react-dynamic-tabs/utils/api/pub_sub.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const Pub_Sub = function (subscribers) {
44
, onChange: []
55
, onLoad: []
66
, onDestroy: []
7+
, onOpen: []
8+
, onClose: []
9+
, onSelect: []
710
};
811
};
912
Pub_Sub.prototype.unSubscribe = function (publisherName, fn) {
@@ -21,10 +24,10 @@ Pub_Sub.prototype.oneSubscribe = function (publisherName, fn) {
2124
};
2225
return this.subscribe(publisherName, _fn);
2326
};
24-
Pub_Sub.prototype.trigger = function (publisherName, param) {
27+
Pub_Sub.prototype.trigger = function (publisherName, param, context) {
2528
const _subscribers = [...this.publishers[publisherName]];
2629
_subscribers.map(subscriber => {
27-
subscriber(param);
30+
context ? subscriber.call(context, param) : subscriber(param);
2831
});
2932
return this;
3033
};

0 commit comments

Comments
 (0)