Skip to content

Commit 8f0ee76

Browse files
committed
create helper.assingAll() and
helper.setNoneEnumProps()
1 parent 3f0fc6a commit 8f0ee76

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ const { throwMissingParam: missingParamEr, throwInvalidParam: invalidParamEr } =
33
export const apiConstructor = function (getDeps, param = { options: {} }) {
44
param.options = param.options || {};
55
const { optionManagerIns, helper, activedTabsHistoryIns } = getDeps.call(this, param.options);
6-
this.optionManager = optionManagerIns;
7-
this.helper = helper;
8-
this.activedTabsHistory = activedTabsHistoryIns;
6+
helper.setNoneEnumProps(this, { helper, optionManager: optionManagerIns, activedTabsHistory: activedTabsHistoryIns });
97
this._setUserProxy()._alterOnChangeCallback()._subscribeCallbacksOptions()._subscribeSelectedTabsHistory();
108
};
11-
export const apiMethods = {
9+
const _apiProps = {
1210
_setUserProxy: function () {
1311
const userProxy = {};
1412
for (var prop in this)
15-
if (prop[0] !== '_') {
13+
if (prop[0] !== '_' && prop !== 'constructor') {
1614
const propValue = this[prop];
1715
if (typeof propValue === 'function') {
1816
userProxy[prop] = propValue.bind(this);
@@ -69,14 +67,6 @@ export const apiMethods = {
6967
},
7068
isSelected: function (id = missingParamEr('isSelected')) { return this._state.selectedTabID == id; },
7169
isOpen: function (id = missingParamEr('isOpen')) { return this._state.openTabIDs.indexOf(id) >= 0; },
72-
eventHandlerFactory: function ({ e, id }) {
73-
const { beforeClose, beforeSelect } = this.getOptions(), el = e.target, parentEl = el.parentElement
74-
, { close, tab } = this.getSetting().cssClasses;
75-
if (el.className.includes(close) && parentEl && parentEl.lastChild && (parentEl.lastChild == el) && parentEl.className.includes(tab))
76-
beforeClose.call(this.userProxy, e, id) && this.close(id);
77-
else
78-
beforeSelect.call(this.userProxy, e, id) && this.select(id);
79-
},
8070
_getOnChangePromise: function () {
8171
return new (Promise)(resolve => { this.one('onChange', () => { resolve.call(this.userProxy); }); });
8272
},
@@ -138,3 +128,14 @@ export const apiMethods = {
138128
return result;
139129
}
140130
};
131+
Helper.setNoneEnumProps(_apiProps, {
132+
eventHandlerFactory: function ({ e, id }) {
133+
const { beforeClose, beforeSelect } = this.getOptions(), el = e.target, parentEl = el.parentElement
134+
, { close, tab } = this.getSetting().cssClasses;
135+
if (el.className.includes(close) && parentEl && parentEl.lastChild && (parentEl.lastChild == el) && parentEl.className.includes(tab))
136+
beforeClose.call(this.userProxy, e, id) && this.close(id);
137+
else
138+
beforeSelect.call(this.userProxy, e, id) && this.select(id);
139+
}
140+
});
141+
export const apiProps = _apiProps;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiMethods, apiConstructor } from './api.factory';
1+
import { apiProps, apiConstructor } from './api.factory';
22
import OptionManager from './optionManager/optionManager.js';
33
import helper from '../helper';
44
import ActivedTabsHistory from './activedTabsHistory';
@@ -12,7 +12,6 @@ const getDeps = function (options) {
1212
Pub_Sub.call(this);
1313
return { activedTabsHistoryIns, optionManagerIns, helper };
1414
};
15-
apiConstructor.prototype = Object.create(Object.assign({}, BaseApi.prototype, Tabs.prototype, Pub_Sub.prototype, apiMethods));
16-
apiConstructor.prototype.constructor = apiConstructor;
15+
helper.assingAll(apiConstructor.prototype, BaseApi.prototype, Tabs.prototype, Pub_Sub.prototype, apiProps).constructor = apiConstructor;
1716
export default apiConstructor.bind(null, getDeps);
1817

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import actions from '../stateManagement/actions';
2+
import Helper from '../helper.js';
23
function BaseApi(helper) {
3-
this.forceUpdateState = {};
44
this._helper = helper;
55
this._state = {};
6-
this.stateRef = {};
76
this._perviousState = {};
87
this._dispatch = () => { };
8+
helper.setNoneEnumProps(this, {
9+
forceUpdateState: {},
10+
stateRef: {}
11+
});
912
}
10-
BaseApi.prototype.updateReducer = function (state, dispatch) {
11-
this.stateRef = state;
12-
this._perviousState = this._helper.getCopyState(this._state);
13-
this._state = this._helper.getCopyState(state);
14-
this._perviousState = this._perviousState.hasOwnProperty('openTabIDs') ? this._perviousState :
15-
this._helper.getCopyState(this._state);
16-
this._dispatch = dispatch;
17-
};
1813
BaseApi.prototype._select = function (tabId) { this._dispatch({ type: actions.active, tabId }); };
1914
BaseApi.prototype._close = function (tabId) { this._dispatch({ type: actions.close, tabId }); };
2015
BaseApi.prototype._open = function (tabId) { this._dispatch({ type: actions.open, tabId }); };
2116
BaseApi.prototype._refresh = function () {
2217
this.forceUpdateState = {};
2318
this._dispatch({ type: actions.refresh });
2419
};
20+
Helper.setNoneEnumProps(BaseApi.prototype, {
21+
updateReducer: function (state, dispatch) {
22+
this.stateRef = state;
23+
this._perviousState = this._helper.getCopyState(this._state);
24+
this._state = this._helper.getCopyState(state);
25+
this._perviousState = this._perviousState.hasOwnProperty('openTabIDs') ? this._perviousState :
26+
this._helper.getCopyState(this._state);
27+
this._dispatch = dispatch;
28+
}
29+
});
2530
export default BaseApi;

src/com/react-dynamic-tabs/utils/helper.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ helper.getCopyState = function (state) {
88
newState.openTabIDs = [...newState.openTabIDs];
99
return newState;
1010
};
11+
helper.assingAll = function (targetObj, ...sourcObjs) {
12+
// copy all enumerable and none-enumerable properties into target
13+
sourcObjs.map(sourcObj => {
14+
Object.getOwnPropertyNames(sourcObj).map(prop => {
15+
targetObj[prop] = sourcObj[prop];
16+
});
17+
});
18+
return targetObj;
19+
};
20+
helper.setNoneEnumProps = function (obj, props) {
21+
const noneEnum = {};
22+
Object.keys(props).map(prop => {
23+
noneEnum[prop] = {
24+
writable: true,
25+
value: props[prop]
26+
};
27+
});
28+
return Object.defineProperties(obj, noneEnum);
29+
};
1130
helper.getArraysDiff = function (arr1, arr2) {
1231
const arr1Copy = [...arr1], arr2Copy = [...arr2];
1332
arr1.map(item => {

0 commit comments

Comments
 (0)