|
1 |
| -const Api = function ({getElManagementIns, btnRef, ctx, setHiddenTabIDs}) { |
| 1 | +const Api = function ({getResizerIns, btnRef, ctx, setHiddenTabIDs}) { |
2 | 2 | this.api = ctx;
|
3 | 3 | this.tablistEl = null;
|
4 |
| - this.getElManagementIns = getElManagementIns; |
| 4 | + this.getResizerIns = getResizerIns; |
5 | 5 | this.tablistContainerEl = null;
|
6 | 6 | this.tabs = null;
|
7 | 7 | this.tabsCount = null;
|
8 | 8 | this.btnRef = btnRef;
|
9 | 9 | this._setHiddenTabIDs = setHiddenTabIDs;
|
10 | 10 | };
|
11 | 11 | Object.assign(Api.prototype, {
|
12 |
| - setEls: function () { |
| 12 | + setResizer: function () { |
13 | 13 | this.tablistEl = this.btnRef.current.previousSibling;
|
14 | 14 | this.tablistContainerEl = this.tablistEl.parentElement.parentElement;
|
15 | 15 | this.tablistViewEl = this.tablistContainerEl.parentElement;
|
16 |
| - this.tablistEl.style.overflow = 'visible'; |
17 |
| - this.tablistContainerEl.style.overflow = 'hidden'; |
| 16 | + debugger; |
| 17 | + this._resizer = this.getResizerIns({ |
| 18 | + containerElement: this.tablistContainerEl, |
| 19 | + buttonElement: this.btnRef.current, |
| 20 | + }); |
18 | 21 | },
|
19 | 22 | installResizer: function (resizeDetectorIns) {
|
20 | 23 | resizeDetectorIns.debncListenTo(this.tablistViewEl, () => this.resize());
|
21 | 24 | },
|
22 | 25 | uninstallResizer: function (resizeDetectorIns) {
|
23 | 26 | if (this.tablistViewEl && resizeDetectorIns) resizeDetectorIns.uninstall(this.tablistViewEl);
|
24 | 27 | },
|
25 |
| - showBtn: function () { |
26 |
| - this.btnRef.current.style.opacity = 1; |
27 |
| - this.btnRef.current.style.position = 'relative'; |
28 |
| - this.btnRef.current.style.pointerEvents = 'all'; |
29 |
| - }, |
30 |
| - hideBtn: function () { |
31 |
| - this.btnRef.current.style.opacity = 0; |
32 |
| - this.btnRef.current.style.position = 'absolute'; |
33 |
| - this.btnRef.current.style.pointerEvents = 'none'; |
34 |
| - }, |
35 |
| - checkOverflow: function (lastTab) { |
36 |
| - return this.els.getDistance(lastTab).value < 0; |
37 |
| - }, |
38 |
| - showAll: function () { |
39 |
| - this.tablistContainerEl.style.display = 'none'; |
40 |
| - for (let i = 0, tabs = this.tablistEl.children, tabsCount = tabs.length; i < tabsCount; i++) { |
41 |
| - tabs[i].style.display = 'flex'; |
42 |
| - } |
43 |
| - this.hideBtn(); |
44 |
| - this.tablistContainerEl.style.display = 'flex'; |
45 |
| - }, |
46 |
| - hideTabs: function (firstHiddenTabIndex, selectedTabInfo, includeSelectedTab) { |
47 |
| - const {openTabIDs} = this.api.getData(); |
48 |
| - const hiddenTabIDs = []; |
49 |
| - this.tablistContainerEl.style.display = 'none'; |
50 |
| - const {index: selectedTabIndex} = selectedTabInfo; |
51 |
| - for (let i = firstHiddenTabIndex, tabsCount = this.tabsCount; i < tabsCount; i++) { |
52 |
| - if (includeSelectedTab || i !== selectedTabIndex) { |
53 |
| - this.tabs[i].style.display = 'none'; |
54 |
| - hiddenTabIDs.push(openTabIDs[i]); |
55 |
| - } |
56 |
| - } |
57 |
| - this.showBtn(); |
58 |
| - this.tablistContainerEl.style.display = 'flex'; |
59 |
| - this._setHiddenTabIDs(hiddenTabIDs.toString()); |
60 |
| - }, |
61 |
| - getSelectedTabInfo: function (tabs, data) { |
62 |
| - const {openTabIDs, selectedTabID} = data; |
63 |
| - const index = openTabIDs.indexOf(selectedTabID); |
64 |
| - const el = index >= 0 ? tabs[index] : null; |
65 |
| - const overflow = el |
66 |
| - ? this.els.getDistance(el).sub(this.els.getEl(this.btnRef.current).getFullSize()).value <= 0 |
67 |
| - : false; |
68 |
| - const overflowFullSize = overflow ? this.els.getEl(el).getFullSize() : 0; |
69 |
| - return {index, overflowFullSize}; |
70 |
| - }, |
71 |
| - validateTabsCount: function (data) { |
72 |
| - const openTabsCount = data.openTabIDs.length; |
73 |
| - if (!openTabsCount) { |
74 |
| - return false; |
75 |
| - } |
| 28 | + _validateTabsCount: function (data) { |
76 | 29 | this.tabs = this.tablistEl.children;
|
77 | 30 | this.tabsCount = this.tabs.length;
|
78 |
| - if (openTabsCount !== this.tabsCount) { |
| 31 | + if (data.openTabIDs.length !== this.tabsCount) { |
79 | 32 | throw new Error("tablist children's length is not equal to openTabIDs length");
|
80 | 33 | }
|
81 | 34 | return true;
|
82 | 35 | },
|
83 | 36 | resize: function () {
|
84 | 37 | const ins = this.api;
|
85 | 38 | const data = ins.getData();
|
86 |
| - if (this.validateTabsCount(data) === false) { |
| 39 | + if (this._validateTabsCount(data) === false) { |
87 | 40 | return this._setHiddenTabIDs('');
|
88 | 41 | }
|
89 |
| - this.showAll(); //showAll should be called regardless of overflow |
90 |
| - this.els = this.getElManagementIns({ |
91 |
| - baseEl: this.tablistContainerEl, |
92 |
| - isVertical: ins.getOption('isVertical'), |
93 |
| - dir: ins.getOption('direction'), |
94 |
| - }); |
95 |
| - const _lastTab = this.tabs[this.tabsCount - 1]; |
96 |
| - if (this.checkOverflow(_lastTab) === false) { |
| 42 | + const {openTabIDs, selectedTabID} = data; |
| 43 | + const selectedTabIndex = openTabIDs.indexOf(selectedTabID); |
| 44 | + const hiddenTabs = this._resizer.resize(selectedTabIndex, ins.getOption('direction'), ins.getOption('isVertical')); |
| 45 | + if (!hiddenTabs.length) { |
97 | 46 | return this._setHiddenTabIDs('');
|
98 | 47 | }
|
99 |
| - const selectedTabInfo = this.getSelectedTabInfo(this.tabs, data); |
100 |
| - this.validateSliderMinSize(selectedTabInfo) |
101 |
| - ? this.hideTabs( |
102 |
| - this.findFirstHiddenTabIndexFactory( |
103 |
| - selectedTabInfo, |
104 |
| - this.getSearchBoundries(selectedTabInfo), |
105 |
| - this.getOrder(_lastTab), |
106 |
| - ), |
107 |
| - selectedTabInfo, |
108 |
| - ) |
109 |
| - : this.hideTabs(0, selectedTabInfo, true); |
110 |
| - }, |
111 |
| - validateSliderMinSize: function (selectedTabInfo) { |
112 |
| - //the slider's size should greater than size of selected tab + more button |
113 |
| - return selectedTabInfo.overflowFullSize + this.els.getEl(this.btnRef.current).getFullSize() >= |
114 |
| - this.els.getEl(this.tablistContainerEl).getSize() |
115 |
| - ? false |
116 |
| - : true; |
117 |
| - }, |
118 |
| - getOrder: function (lastTab) { |
119 |
| - return Math.abs(this.els.getDistance(lastTab).value) > this.els.getEl(this.tablistContainerEl).getSize() |
120 |
| - ? 'asc' |
121 |
| - : 'desc'; |
122 |
| - }, |
123 |
| - getSearchBoundries: function (selectedTabInfo) { |
124 |
| - const {overflowFullSize, index: pivotIndex} = selectedTabInfo; |
125 |
| - //if selected tab is not existed |
126 |
| - if (pivotIndex < 0) { |
127 |
| - return [0, this.tabsCount - 2]; |
128 |
| - } |
129 |
| - const isSelectedTabOverflow = overflowFullSize > 0; |
130 |
| - return isSelectedTabOverflow ? [0, pivotIndex - 1] : [pivotIndex + 1, this.tabsCount - 2]; |
131 |
| - }, |
132 |
| - getTabDis: function (selectedTabInfo, el) { |
133 |
| - return this.els |
134 |
| - .getDistance(el) |
135 |
| - .sub(selectedTabInfo.overflowFullSize) |
136 |
| - .sub(this.els.getEl(this.btnRef.current).getFullSize()); |
137 |
| - }, |
138 |
| - findFirstHiddenTabIndexDSCE: function (selectedTabInfo, start, stop) { |
139 |
| - let value = this.tabsCount - 1; |
140 |
| - for (let i = stop; i >= start; i--) { |
141 |
| - if (this.getTabDis(selectedTabInfo, this.tabs[i]).value <= 0) { |
142 |
| - value = i; |
143 |
| - } else { |
144 |
| - break; |
145 |
| - } |
146 |
| - } |
147 |
| - return value; |
148 |
| - }, |
149 |
| - findFirstHiddenTabIndexASC: function (selectedTabInfo, start, stop) { |
150 |
| - for (let i = start; i <= stop; i++) { |
151 |
| - if (this.getTabDis(selectedTabInfo, this.tabs[i]).value <= 0) { |
152 |
| - return i; |
153 |
| - } |
154 |
| - } |
155 |
| - return this.tabsCount - 1; |
156 |
| - }, |
157 |
| - findFirstHiddenTabIndexFactory: function (selectedTabInfo, [start, stop], order) { |
158 |
| - return order === 'asc' |
159 |
| - ? this.findFirstHiddenTabIndexASC(selectedTabInfo, start, stop) |
160 |
| - : this.findFirstHiddenTabIndexDSCE(selectedTabInfo, start, stop); |
| 48 | + this._setHiddenTabIDs(hiddenTabs.map(({index}) => openTabIDs[index]).toString()); |
161 | 49 | },
|
162 | 50 | });
|
163 | 51 | export default Api;
|
0 commit comments