Skip to content

Commit f9028d7

Browse files
_add onChange method to api.factory and
remove _subscribeOnChange method from it. _remove beforeSelect and beforeClose from pub_sub.js. _fix undefined context problem in callbacks options.
1 parent 3303955 commit f9028d7

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/useDynamicTabs/useDynamicTabs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function useDynamicTabs(options) {
2424
const oldState = api.getCopyPerviousData()
2525
, [openedTabsId, closedTabsId] = api.helper.getArraysDiff(state.openTabIDs, oldState.openTabIDs)
2626
, isSwitched = oldState.selectedTabID !== state.selectedTabID;
27-
api.trigger('onChange', api.userProxy, { newState: state, oldState, closedTabsId, openedTabsId, isSwitched });
27+
api.onChange({ newState: state, oldState, closedTabsId, openedTabsId, isSwitched });
2828
}, [state]);
2929
if (!_ref.TabListComponent)
3030
_ref.TabListComponent = (props = {}) => {

src/utils/api/api.factory.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { throwMissingParam: missingParamEr, throwInvalidParam: invalidParamEr } =
33
export const apiConstructor = function (getDeps, param = { options: {} }) {
44
const { optionsManager, helper, activedTabsHistory } = getDeps.call(this, param.options);
55
helper.setNoneEnumProps(this, { optionsManager, helper, activedTabsHistory });
6-
this._setUserProxy()._subscribeSelectedTabsHistory()._subscribeCallbacksOptions()._subscribeOnChange();
6+
this._setUserProxy()._subscribeSelectedTabsHistory()._subscribeCallbacksOptions();//._subscribeOnChange();
77
};
88
const _apiProps = {
99
_setUserProxy: function () {
@@ -23,22 +23,11 @@ const _apiProps = {
2323
this.userProxy = userProxy;
2424
return this;
2525
},
26-
_subscribeOnChange: function () {
27-
this.on('onChange', ({ newState, oldState, closedTabsId, openedTabsId, isSwitched }) => {
28-
openedTabsId.length && this.trigger('onOpen', this.userProxy, openedTabsId);
29-
closedTabsId.length && this.trigger('onClose', this.userProxy, closedTabsId);
30-
isSwitched && this.trigger('onSelect', this.userProxy, {
31-
currentSelectedTabId: newState.selectedTabID,
32-
perviousSelectedTabId: oldState.selectedTabID
33-
});
34-
});
35-
return this;
36-
},
3726
_subscribeCallbacksOptions: function () {
3827
const op = this.optionsManager.options;
3928
Object.keys(this._publishers).map(eventName => {
4029
this.on(eventName, function () {
41-
op[eventName].apply(this.userProxy, arguments);
30+
op[eventName].apply(this, arguments);
4231
});
4332
});
4433
return this;
@@ -127,20 +116,29 @@ Helper.setNoneEnumProps(_apiProps, {
127116
}
128117
return this._initialState;
129118
},
119+
onChange: function ({ newState, oldState, closedTabsId, openedTabsId, isSwitched }) {
120+
if (isSwitched || openedTabsId.length || closedTabsId.length) {
121+
this.trigger('onChange', this.userProxy, {
122+
currentData: { ...newState },
123+
perviousData: { ...oldState }
124+
});
125+
openedTabsId.length && this.trigger('onOpen', this.userProxy, openedTabsId);
126+
closedTabsId.length && this.trigger('onClose', this.userProxy, closedTabsId);
127+
isSwitched && this.trigger('onSelect', this.userProxy, {
128+
currentSelectedTabId: newState.selectedTabID,
129+
perviousSelectedTabId: oldState.selectedTabID
130+
});
131+
}
132+
return this;
133+
},
130134
eventHandlerFactory: function ({ e, id }) {
131135
const el = e.target, parentEl = el.parentElement, { closeClass, tabClass } = this.optionsManager.setting;
132136
if (el.className.includes(closeClass) && parentEl && parentEl.lastChild && (parentEl.lastChild == el)
133137
&& parentEl.className.includes(tabClass)) {
134-
// if just on of the beforeClose subscribers return false then it will prevent tab from close
135-
this.trigger('beforeClose', this.userProxy, e, id).includes(false) || this.close(id);
138+
(this.getOption('beforeClose').call(this.userProxy, e, id) !== false) && this.close(id);
136139
}
137140
else {
138-
// if just on of the beforeSelect subscribers return false then it will prevent tab from select
139-
if (!this.trigger('beforeSelect', this.userProxy, e, id).includes(false)) {
140-
this.select(id).then(result => {
141-
}).catch(er => {
142-
});
143-
}
141+
(this.getOption('beforeSelect').call(this.userProxy, e, id) !== false) && this.select(id);
144142
}
145143
}
146144
});

src/utils/api/pub_sub.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const Pub_Sub = function () {
77
, onOpen: []
88
, onClose: []
99
, onSelect: []
10-
, beforeSelect: []
11-
, beforeClose: []
1210
, onInit: []
1311
, onFirstSelect: []
1412
};

0 commit comments

Comments
 (0)