-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpager-load-more.js
161 lines (142 loc) · 4.82 KB
/
pager-load-more.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
import '../colors/colors.js';
import '../loading-spinner/loading-spinner.js';
import { css, html, LitElement, nothing } from 'lit';
import { buttonStyles } from '../button/button-styles.js';
import { FocusMixin } from '../../mixins/focus/focus-mixin.js';
import { formatNumber } from '@brightspace-ui/intl/lib/number.js';
import { getFirstFocusableDescendant } from '../../helpers/focus.js';
import { getSeparator } from '@brightspace-ui/intl/lib/list.js';
import { labelStyles } from '../typography/styles.js';
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js';
import { offscreenStyles } from '../offscreen/offscreen.js';
import { PageableSubscriberMixin } from './pageable-subscriber-mixin.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
const nativeFocus = document.createElement('div').focus;
/**
* A pager component for load-more paging.
* @fires d2l-pager-load-more - Dispatched when the user clicks the load-more button. Consumers must call the provided "complete" method once items have been loaded.
* @fires d2l-pager-load-more-loaded - Dispatched after more items have been loaded.
*/
class LoadMore extends PageableSubscriberMixin(FocusMixin(LocalizeCoreElement(RtlMixin(LitElement)))) {
static get properties() {
return {
/**
* Whether there are more items that can be loaded.
* @type {boolean}
*/
hasMore: { type: Boolean, attribute: 'has-more', reflect: true },
/**
* The number of additional items to load.
* @type {number}
*/
pageSize: { type: Number, attribute: 'page-size', reflect: true },
_loading: { state: true }
};
}
static get styles() {
return [ buttonStyles, labelStyles, offscreenStyles, css`
:host {
display: block;
}
:host(:not([has-more])),
:host([hidden]) {
display: none;
}
button {
align-items: center;
background-color: var(--d2l-color-regolith);
border: 1px solid var(--d2l-color-sylvite);
display: flex;
font-family: inherit;
gap: 0.5rem;
justify-content: center;
width: 100%;
}
button:hover {
background-color: var(--d2l-color-sylvite);
border-color: var(--d2l-color-gypsum);
}
.action {
color: var(--d2l-color-celestine);
}
.separator {
border-right: 1px solid var(--d2l-color-mica);
height: 0.8rem;
}
.info {
color: var(--d2l-color-galena);
font-weight: 400;
}
`];
}
constructor() {
super();
this.hasMore = false;
/** @ignore */
this._loading = false;
}
static get focusElementSelector() {
return 'button';
}
render() {
if (!this.hasMore || !this._pageableInfo) return nothing;
const { itemCount, itemShowingCount } = this._pageableInfo;
return html`
${this._loading ? html`
<span class="d2l-offscreen" role="alert">${this.localize('components.pager-load-more.status-loading')}</span>
` : nothing}
<button class="d2l-label-text" @click="${this._handleClick}" type="button">
${this._loading ? html`
<d2l-loading-spinner size="24"></d2l-loading-spinner>
` : html`
<span class="action">${isNaN(this.pageSize) ? this.localize('components.pager-load-more.action') : this.localize('components.pager-load-more.action-with-page-size', { count: formatNumber(this.pageSize) })}</span>
${itemCount !== null ? html`
<span class="d2l-offscreen">${getSeparator({ nonBreaking: true })}</span>
<span class="separator"></span>
<span class="info">${this.localize('components.pageable.info-with-total', { countFormatted: formatNumber(itemShowingCount), totalCount: itemCount, totalCountFormatted: formatNumber(itemCount) })}</span>
` : nothing}
`}
</button>
`;
}
async _handleClick() {
if (this._loading) return;
const pageable = this._getPageableRegistries()[0];
if (!pageable) return;
const lastItemIndex = pageable._getLastItemIndex();
await new Promise(resolve => {
this._loading = true;
this.dispatchEvent(new CustomEvent('d2l-pager-load-more', {
bubbles: false,
composed: false,
detail: { complete: resolve }
}));
});
this._loading = false;
// wait a frame for async sub-components to render
await new Promise(requestAnimationFrame);
const item = pageable._getItemByIndex(lastItemIndex + 1);
let itemToFocus;
if (item) {
if (item.updateComplete) await item.updateComplete;
if (item.focus !== nativeFocus) {
itemToFocus = item;
} else {
const firstFocusable = getFirstFocusableDescendant(item);
if (firstFocusable) {
itemToFocus = firstFocusable;
} else if (item.focus === nativeFocus) {
item.tabIndex = -1;
itemToFocus = item;
}
}
}
if (itemToFocus) {
await new Promise(requestAnimationFrame);
itemToFocus.focus();
}
await new Promise(requestAnimationFrame);
this.dispatchEvent(new CustomEvent('d2l-pager-load-more-loaded'));
}
}
customElements.define('d2l-pager-load-more', LoadMore);