react-dyn-tabs
Version:
React dynamic tabs with full API
314 lines • 10.6 kB
JavaScript
import Helper from '../helper.js';
var missingParamEr = Helper.throwMissingParam,
isArray = Helper.isArray,
thorwInvalidParam = Helper.thorwInvalidParam;
export var apiConstructor = function apiConstructor(getDeps, param, modules, Components) {
var _this = this;
if (param === void 0) {
param = {
options: {}
};
}
if (modules === void 0) {
modules = [];
}
var _getDeps$call = getDeps.call(this, param.options),
optionsManager = _getDeps$call.optionsManager,
helper = _getDeps$call.helper,
activedTabsHistory = _getDeps$call.activedTabsHistory;
helper.setNoneEnumProps(this, {
optionsManager: optionsManager,
helper: helper,
activedTabsHistory: activedTabsHistory,
userProxy: {}
});
this._setUserProxy()._subscribeOnReadyEvent()._createReadyFunction()._subscribeSelectedTabsHistory()._subscribeCallbacksOptions();
modules.forEach(function (module) {
return module(_this, Components);
});
};
var _apiProps = {
_setUserProxy: function _setUserProxy() {
var _this2 = this;
var userProxy = {};
var _loop = function _loop(prop) {
if (prop[0] !== '_' && prop !== 'constructor') {
(function (that) {
var propValue = that[prop];
if (typeof propValue === 'function') {
userProxy[prop] = function () {
var result = propValue.apply(that, arguments);
return result === that ? userProxy : result;
};
} else {
userProxy[prop] = propValue;
}
})(_this2);
}
};
for (var prop in this) {
_loop(prop);
}
this.userProxy = userProxy;
return this;
},
_subscribeOnReadyEvent: function _subscribeOnReadyEvent() {
var _this3 = this;
this.one('_onReady', function () {
_this3._isReady = true;
});
return this;
},
_createReadyFunction: function _createReadyFunction() {
var _this4 = this;
var ready = function ready(fn) {
if (_this4._isReady === true) {
fn.call(_this4.userProxy, _this4.userProxy);
} else {
_this4.one('_onReady', function () {
fn.call(_this4.userProxy, _this4.userProxy);
});
}
};
ready = ready.bind(this);
this.helper.setNoneEnumProps(this, {
ready: ready
});
return this;
},
_subscribeSelectedTabsHistory: function _subscribeSelectedTabsHistory() {
var _this5 = this;
this.on('onChange', function (_ref) {
var currentData = _ref.currentData,
previousData = _ref.previousData,
closedTabIDs = _ref.closedTabIDs;
for (var i = 0, l = closedTabIDs.length; i < l; i++) {
_this5.activedTabsHistory.remove(closedTabIDs[i]);
}
var isSwitched = previousData.selectedTabID !== currentData.selectedTabID;
if (isSwitched && _this5.isOpen(previousData.selectedTabID) && !_this5.isSelected(previousData.selectedTabID)) _this5.activedTabsHistory.add(previousData.selectedTabID);
});
return this;
},
_subscribeCallbacksOptions: function _subscribeCallbacksOptions() {
var _this6 = this;
var op = this.optionsManager.options;
Object.keys(this._publishers).forEach(function (eventName) {
if (eventName[0] !== '_') _this6.on(eventName, function () {
op[eventName].apply(this, arguments);
});
});
return this;
},
getOption: function getOption(name) {
return this.optionsManager.getOption(name);
},
setOption: function setOption(name, value) {
this.optionsManager.setOption(name, value);
return this;
},
getPreviousData: function getPreviousData() {
return this.helper.getCopyState(this.previousState);
},
getCopyPerviousData: function getCopyPerviousData() {
return this.getPreviousData();
},
getData: function getData() {
return this.helper.getCopyState(this.stateRef);
},
getCopyData: function getCopyData() {
return this.getData();
},
isSelected: function isSelected(id) {
if (id === void 0) {
id = missingParamEr('isSelected');
}
return this.stateRef.selectedTabID == id;
},
isOpen: function isOpen(id) {
if (id === void 0) {
id = missingParamEr('isOpen');
}
return this.stateRef.openTabIDs.indexOf(id) >= 0;
},
_getFlushEffectsPromise: function _getFlushEffectsPromise() {
var _this7 = this;
return new Promise(function (resolve) {
_this7.one('_onFlushEffects', function () {
resolve.apply(this, arguments);
});
});
},
select: function select(id) {
if (id === void 0) {
id = missingParamEr('select');
}
if (id) id = id + ''; //make sure id is string
var result = this._getFlushEffectsPromise();
this._select(id);
return result;
},
_getPreSelectedTabId: function _getPreSelectedTabId() {
var selectedTabHistory = [].concat(this.activedTabsHistory.tabsId);
var tabID = '';
while (!tabID && selectedTabHistory.length) {
var _tabID = selectedTabHistory.pop();
if (_tabID) {
var _tabData = this.getTab(_tabID);
if (_tabData && !_tabData.disable && this.isOpen(_tabID) && !this.isSelected(_tabID)) tabID = _tabID;
}
}
return tabID;
},
_getPreSiblingTabId: function _getPreSiblingTabId() {
var _this8 = this;
var _this$stateRef = this.stateRef,
selectedTabID = _this$stateRef.selectedTabID,
openTabIDs = _this$stateRef.openTabIDs;
var isRightToLeft = true;
var arr = openTabIDs.slice(0, openTabIDs.indexOf(selectedTabID));
return this.helper.filterArrayUntilFirstValue(arr, function (id) {
return !_this8.getTab(id).disable;
}, isRightToLeft);
},
_getNextSiblingTabId: function _getNextSiblingTabId() {
var _this9 = this;
var _this$stateRef2 = this.stateRef,
selectedTabID = _this$stateRef2.selectedTabID,
openTabIDs = _this$stateRef2.openTabIDs;
var isRightToLeft = false;
var arr = openTabIDs.slice(openTabIDs.indexOf(selectedTabID) + 1);
return this.helper.filterArrayUntilFirstValue(arr, function (id) {
return !_this9.getTab(id).disable;
}, isRightToLeft);
},
_findTabIdForSwitching: function _findTabIdForSwitching() {
var tabId = '';
tabId = this._getPreSelectedTabId();
tabId = tabId || this._getPreSiblingTabId();
tabId = tabId || this._getNextSiblingTabId();
return tabId || '';
},
setTab: function setTab(id, newData) {
if (newData === void 0) {
newData = {};
}
this.optionsManager.validateObjectiveTabData(newData).validatePanelComponent(newData);
this._setTab(id, newData);
return this;
},
open: function open(tabObj) {
if (tabObj === void 0) {
tabObj = missingParamEr('open');
}
var newTabObj = this.optionsManager.validateTabData(tabObj);
var result = this._getFlushEffectsPromise();
this._addTab(newTabObj);
this._open(newTabObj.id);
return result;
},
sort: function sort(tabIDs) {
if (tabIDs === void 0) {
tabIDs = missingParamEr('sort');
}
if (!isArray(tabIDs)) {
thorwInvalidParam('sort');
}
var result = this._getFlushEffectsPromise();
this._sort(tabIDs);
return result;
},
__close: function __close(id) {
var result = this._getFlushEffectsPromise();
this._close(id);
this._removeTab(id);
return result;
},
close: function close(id, switching) {
if (id === void 0) {
id = missingParamEr('close');
}
if (switching === void 0) {
switching = true;
}
if (id) id = id + ''; //make sure id is string
if (switching && this.isOpen(id) && this.isSelected(id)) {
var _openTabsId = [].concat(this.stateRef.openTabIDs);
_openTabsId.splice(_openTabsId.indexOf(id), 1);
this.select(this._findTabIdForSwitching());
return this.__close(id);
} else return this.__close(id);
},
refresh: function refresh() {
var result = this._getFlushEffectsPromise();
this._refresh();
return result;
}
};
Helper.setNoneEnumProps(_apiProps, {
onChange: function onChange(_ref2) {
var _this10 = this;
var newState = _ref2.newState,
oldState = _ref2.oldState,
closedTabIDs = _ref2.closedTabIDs,
openedTabIDs = _ref2.openedTabIDs,
isSwitched = _ref2.isSwitched;
if (isSwitched || openedTabIDs.length || closedTabIDs.length) {
this.trigger('onChange', this.userProxy, function () {
return [{
currentData: _this10.helper.getCopyState(newState),
previousData: _this10.helper.getCopyState(oldState),
closedTabIDs: closedTabIDs.slice(),
openedTabIDs: openedTabIDs.slice()
}];
});
openedTabIDs.length && this.trigger('onOpen', this.userProxy, function () {
return [openedTabIDs.slice()];
});
closedTabIDs.length && this.trigger('onClose', this.userProxy, function () {
return [closedTabIDs.slice()];
});
if (isSwitched) {
if (newState.selectedTabID && this.activedTabsHistory.tabsId.indexOf(newState.selectedTabID) === -1) {
this.trigger('onFirstSelect', this.userProxy, function () {
return [{
currentSelectedTabId: newState.selectedTabID,
previousSelectedTabId: oldState.selectedTabID
}];
});
}
this.trigger('onSelect', this.userProxy, function () {
return [{
currentSelectedTabId: newState.selectedTabID,
previousSelectedTabId: oldState.selectedTabID
}];
});
}
}
return this;
},
eventHandlerFactory: function eventHandlerFactory(_ref3) {
var e = _ref3.e,
id = _ref3.id;
var el = e.target,
parentEl = el.parentElement,
_this$optionsManager$ = this.optionsManager.setting,
closeClass = _this$optionsManager$.closeClass,
tabClass = _this$optionsManager$.tabClass;
if (el.className.includes(closeClass) && parentEl && parentEl.lastChild && parentEl.lastChild == el && parentEl.className.includes(tabClass)) {
this.getOption('beforeClose').call(this.userProxy, e, id) !== false && this.close(id, true);
} else {
this.getOption('beforeSelect').call(this.userProxy, e, id) !== false && this.select(id);
}
},
getSetting: function getSetting(settingName) {
var st = this.optionsManager.setting;
if (Object.prototype.hasOwnProperty.call(st, settingName)) {
if (typeof st[settingName] === 'function') {
return st[settingName].apply(st, Array.prototype.slice.call(arguments, 1));
}
return st[settingName];
}
}
});
export var apiProps = _apiProps;