-
Notifications
You must be signed in to change notification settings - Fork 352
/
component.js
184 lines (151 loc) · 4.77 KB
/
component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* global ResizeSensor */
import Component from '@ember/component';
import { or, readOnly } from '@ember/object/computed';
import { next, schedule, scheduleOnce } from '@ember/runloop';
/**
Renders a custom loading indicator beneath the table body. Can be used to
implement an infinite scroll pattern. Provides optional centering to keep the
user-provided block centered horizontally in the scroll viewport.
```hbs
<EmberTable as |t|>
<t.head @columns={{this.columns}} />
<t.body @rows={{this.rows}} />
<t.loadingMore
@isLoading={{this.isLoadingMore}}
@canLoadMore={{this.canLoadMore}}
@center={{true}}>
{{!-- custom spinner --}}
<img class="spinner" src="spinner.gif"/>
</t.loadingMore>
</EmberTable>
```
@class <EmberTableLoadingMore />
@public
*/
export default Component.extend({
classNames: ['ember-table-loading-more'],
attributeBindings: ['dataTestEmberTableLoadingMore:data-test-ember-table-loading-more'],
dataTestEmberTableLoadingMore: true,
unwrappedApi: or('api.api', 'api'),
scrollElement: readOnly('unwrappedApi.columnTree.container'),
/**
* Boolean flag specifying if additional rows are being loaded. If true,
* indicator block will be visible below the table body.
*
* @argument isLoading
* @type boolean
*/
isLoading: false,
/**
* Boolean flag specifying if there are more rows yet to load. If false, the
* indicator block will be removed from the DOM.
*
* @argument canLoadMore
* @type boolean
*/
canLoadMore: true,
/**
* Boolean flag specifying if the indicator block should be centered
* horizontally in the scroll viewport.
*
* @argument center
* @type boolean
*/
center: true,
init() {
this._super(...arguments);
this._updateTransform = () => scheduleOnce('afterRender', this, 'updateTransform');
},
didReceiveAttrs() {
this._super(...arguments);
// ignore the first run of `didReceiveAttrs` before element exists
if (!this.element) {
return;
}
// no observers here, no sir.
let canLoadMore = this.get('canLoadMore');
if (canLoadMore !== this._canLoadMore) {
scheduleOnce('afterRender', this, 'canLoadMoreChanged');
this._canLoadMore = canLoadMore;
}
let isLoading = this.get('isLoading');
if (isLoading !== this._isLoading) {
scheduleOnce('afterRender', this, 'isLoadingChanged');
this._isLoading = isLoading;
}
let center = this.get('center');
if (center !== this._center) {
scheduleOnce('afterRender', this, 'centerChanged');
this._center = center;
}
},
didInsertElement() {
this._super(...arguments);
this.canLoadMoreChanged();
this.isLoadingChanged();
this.centerChanged();
},
willDestroyElement() {
if (this.get('center')) {
this.removeListeners();
}
this._super(...arguments);
},
canLoadMoreChanged() {
if (this.get('canLoadMore')) {
this.setIncludedInLayout(true);
} else {
// Delay removal to minimize impact on scroll position. Usually any new
// rows have been rendered by now, but sometimes they are not, and
// removing this element from the DOM will cause a small scrollback.
next(() => schedule('afterRender', this, 'setIncludedInLayout', false));
}
},
isLoadingChanged() {
this.setVisible(this.get('isLoading'));
},
centerChanged() {
this.updateTransform();
if (this.get('center')) {
this.addListeners();
} else {
this.removeListeners();
}
},
addListeners() {
let scrollElement = this.get('scrollElement');
scrollElement.addEventListener('scroll', this._updateTransform);
this._scrollElementResizeSensor = new ResizeSensor(scrollElement, this._updateTransform);
},
removeListeners() {
this.get('scrollElement').removeEventListener('scroll', this._updateTransform);
if (this._scrollElementResizeSensor) {
this._scrollElementResizeSensor.detach();
}
},
updateTransform() {
let scrollElement = this.get('scrollElement');
// this method can be triggered by the resize sensor before the element
// (and `scrollElement`) exists
if (!scrollElement) {
return;
}
let translateX = 0;
if (this.get('center')) {
// keep indicator centered in viewport, even if user scrolls left or right
translateX = Math.round(
scrollElement.scrollLeft + (scrollElement.clientWidth - this.element.clientWidth) / 2
);
}
this.setTranslateX(translateX);
},
setIncludedInLayout(value) {
this.element.style.display = value ? '' : 'none';
},
setVisible(value) {
this.element.style.visibility = value ? '' : 'hidden';
},
setTranslateX(value) {
this.element.style.transform = value === 0 ? '' : `translateX(${value}px)`;
},
});