1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { __extends } from "tslib";
|
17 | import classNames from "classnames";
|
18 | import * as React from "react";
|
19 | import { Boundary, Classes, DISPLAYNAME_PREFIX } from "../../common";
|
20 | import { OVERFLOW_LIST_OBSERVE_PARENTS_CHANGED } from "../../common/errors";
|
21 | import { shallowCompareKeys } from "../../common/utils";
|
22 | import { ResizeSensor } from "../resize-sensor/resizeSensor";
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var OverflowList = (function (_super) {
|
29 | __extends(OverflowList, _super);
|
30 | function OverflowList() {
|
31 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
32 | _this.state = {
|
33 | chopSize: _this.defaultChopSize(),
|
34 | lastChopSize: null,
|
35 | lastOverflowCount: 0,
|
36 | overflow: [],
|
37 | repartitioning: false,
|
38 | visible: _this.props.items,
|
39 | };
|
40 | _this.spacer = null;
|
41 | _this.resize = function () {
|
42 | _this.repartition();
|
43 | };
|
44 | return _this;
|
45 | }
|
46 | OverflowList.ofType = function () {
|
47 | return OverflowList;
|
48 | };
|
49 | OverflowList.prototype.componentDidMount = function () {
|
50 | this.repartition();
|
51 | };
|
52 | OverflowList.prototype.shouldComponentUpdate = function (nextProps, nextState) {
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | return this.props !== nextProps || !(this.state !== nextState && shallowCompareKeys(this.state, nextState));
|
60 | };
|
61 | OverflowList.prototype.componentDidUpdate = function (prevProps, prevState) {
|
62 | var _a, _b;
|
63 | if (prevProps.observeParents !== this.props.observeParents) {
|
64 | console.warn(OVERFLOW_LIST_OBSERVE_PARENTS_CHANGED);
|
65 | }
|
66 | if (prevProps.collapseFrom !== this.props.collapseFrom ||
|
67 | prevProps.items !== this.props.items ||
|
68 | prevProps.minVisibleItems !== this.props.minVisibleItems ||
|
69 | prevProps.overflowRenderer !== this.props.overflowRenderer ||
|
70 | prevProps.alwaysRenderOverflow !== this.props.alwaysRenderOverflow ||
|
71 | prevProps.visibleItemRenderer !== this.props.visibleItemRenderer) {
|
72 |
|
73 | this.setState({
|
74 | chopSize: this.defaultChopSize(),
|
75 | lastChopSize: null,
|
76 | lastOverflowCount: 0,
|
77 | overflow: [],
|
78 | repartitioning: true,
|
79 | visible: this.props.items,
|
80 | });
|
81 | }
|
82 | var _c = this.state, repartitioning = _c.repartitioning, overflow = _c.overflow, lastOverflowCount = _c.lastOverflowCount;
|
83 | if (
|
84 |
|
85 | repartitioning === false &&
|
86 | prevState.repartitioning === true) {
|
87 |
|
88 | if (overflow.length !== lastOverflowCount) {
|
89 | (_b = (_a = this.props).onOverflow) === null || _b === void 0 ? void 0 : _b.call(_a, overflow.slice());
|
90 | }
|
91 | }
|
92 | else if (!shallowCompareKeys(prevState, this.state)) {
|
93 | this.repartition();
|
94 | }
|
95 | };
|
96 | OverflowList.prototype.render = function () {
|
97 | var _this = this;
|
98 | var _a = this.props, className = _a.className, collapseFrom = _a.collapseFrom, observeParents = _a.observeParents, style = _a.style, _b = _a.tagName, tagName = _b === void 0 ? "div" : _b, visibleItemRenderer = _a.visibleItemRenderer;
|
99 | var overflow = this.maybeRenderOverflow();
|
100 | var list = React.createElement(tagName, {
|
101 | className: classNames(Classes.OVERFLOW_LIST, className),
|
102 | style: style,
|
103 | }, collapseFrom === Boundary.START ? overflow : null, this.state.visible.map(visibleItemRenderer), collapseFrom === Boundary.END ? overflow : null, React.createElement("div", { className: Classes.OVERFLOW_LIST_SPACER, ref: function (ref) { return (_this.spacer = ref); } }));
|
104 | return (React.createElement(ResizeSensor, { onResize: this.resize, observeParents: observeParents }, list));
|
105 | };
|
106 | OverflowList.prototype.maybeRenderOverflow = function () {
|
107 | var overflow = this.state.overflow;
|
108 | if (overflow.length === 0 && !this.props.alwaysRenderOverflow) {
|
109 | return null;
|
110 | }
|
111 | return this.props.overflowRenderer(overflow.slice());
|
112 | };
|
113 | OverflowList.prototype.repartition = function () {
|
114 | var _this = this;
|
115 | var _a;
|
116 | if (this.spacer == null) {
|
117 | return;
|
118 | }
|
119 |
|
120 | var partitionExhausted = this.state.lastChopSize === 1;
|
121 | var minVisible = (_a = this.props.minVisibleItems) !== null && _a !== void 0 ? _a : 0;
|
122 |
|
123 | var shouldShrink = this.spacer.offsetWidth < 0.9 && this.state.visible.length > minVisible;
|
124 |
|
125 | var shouldGrow = (this.spacer.offsetWidth >= 1 || this.state.visible.length < minVisible) &&
|
126 | this.state.overflow.length > 0 &&
|
127 | !partitionExhausted;
|
128 | if (shouldShrink || shouldGrow) {
|
129 | this.setState(function (state) {
|
130 | var visible;
|
131 | var overflow;
|
132 | if (_this.props.collapseFrom === Boundary.END) {
|
133 | var result = shiftElements(state.visible, state.overflow, _this.state.chopSize * (shouldShrink ? 1 : -1));
|
134 | visible = result[0];
|
135 | overflow = result[1];
|
136 | }
|
137 | else {
|
138 | var result = shiftElements(state.overflow, state.visible, _this.state.chopSize * (shouldShrink ? -1 : 1));
|
139 | overflow = result[0];
|
140 | visible = result[1];
|
141 | }
|
142 | return {
|
143 | chopSize: halve(state.chopSize),
|
144 | lastChopSize: state.chopSize,
|
145 |
|
146 | lastOverflowCount: _this.isFirstPartitionCycle(state.chopSize)
|
147 | ? state.overflow.length
|
148 | : state.lastOverflowCount,
|
149 | overflow: overflow,
|
150 | repartitioning: true,
|
151 | visible: visible,
|
152 | };
|
153 | });
|
154 | }
|
155 | else {
|
156 |
|
157 | this.setState({
|
158 | chopSize: this.defaultChopSize(),
|
159 | lastChopSize: null,
|
160 | repartitioning: false,
|
161 | });
|
162 | }
|
163 | };
|
164 | OverflowList.prototype.defaultChopSize = function () {
|
165 | return halve(this.props.items.length);
|
166 | };
|
167 | OverflowList.prototype.isFirstPartitionCycle = function (currentChopSize) {
|
168 | return currentChopSize === this.defaultChopSize();
|
169 | };
|
170 | OverflowList.displayName = "".concat(DISPLAYNAME_PREFIX, ".OverflowList");
|
171 | OverflowList.defaultProps = {
|
172 | alwaysRenderOverflow: false,
|
173 | collapseFrom: Boundary.START,
|
174 | minVisibleItems: 0,
|
175 | };
|
176 | return OverflowList;
|
177 | }(React.Component));
|
178 | export { OverflowList };
|
179 | function halve(num) {
|
180 | return Math.ceil(num / 2);
|
181 | }
|
182 | function shiftElements(leftArray, rightArray, num) {
|
183 |
|
184 | var allElements = leftArray.concat(rightArray);
|
185 | var newLeftLength = leftArray.length - num;
|
186 | if (newLeftLength <= 0) {
|
187 | return [[], allElements];
|
188 | }
|
189 | else if (newLeftLength >= allElements.length) {
|
190 | return [allElements, []];
|
191 | }
|
192 | var sliceIndex = allElements.length - newLeftLength;
|
193 | return [allElements.slice(0, -sliceIndex), allElements.slice(-sliceIndex)];
|
194 | }
|
195 |
|
\ | No newline at end of file |