-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathselection-summary.js
88 lines (77 loc) · 2.43 KB
/
selection-summary.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
import { css, html, LitElement, nothing } from 'lit';
import { bodyCompactStyles } from '../typography/styles.js';
import { LocalizeCoreElement } from '../../helpers/localize-core-element.js';
import { SelectionInfo } from './selection-mixin.js';
import { SelectionObserverMixin } from './selection-observer-mixin.js';
/**
* A summary showing the current selected count.
* @fires d2l-selection-observer-subscribe - Internal event
*/
class Summary extends LocalizeCoreElement(SelectionObserverMixin(LitElement)) {
static get properties() {
return {
/**
* Text to display if no items are selected
* @type {string}
*/
noSelectionText: { type: String, attribute: 'no-selection-text' }
};
}
static get styles() {
return [bodyCompactStyles, css`
:host {
display: inline-block;
}
:host([hidden]) {
display: none;
}
p {
margin: 0;
}
`];
}
render() {
if (!this._summary) {
return nothing;
}
return html`
<p class="d2l-body-compact">${this._summary}</p>
`;
}
willUpdate(changedProperties) {
if (changedProperties.has('_provider') || changedProperties.has('selectionInfo') || changedProperties.has('noSelectionText')) {
this._updateSelectSummary();
}
}
_updateSelectSummary() {
if (this._provider?.selectionSingle) {
this._summary = null;
return;
}
/* If lazy loading items is supported (ex. d2l-list) then check the keys to determine if the plus sign should be included.
* If lazy loading is not supported (ex. d2l-table-wrapper), then skip this.
*/
let includePlus = false;
if (this._provider && this._provider._getLazyLoadItems) {
const lazyLoadListItems = this._provider._getLazyLoadItems();
if (lazyLoadListItems.size > 0) {
for (const selectedItemKey of this.selectionInfo.keys) {
if (lazyLoadListItems.has(selectedItemKey)) {
includePlus = true;
break;
}
}
}
}
const count = (this._provider && this.selectionInfo.state === SelectionInfo.states.allPages) ?
this._provider.itemCount : this.selectionInfo.keys.length;
if (this.selectionInfo.state === SelectionInfo.states.none && this.noSelectionText) {
this._summary = this.noSelectionText;
} else if (includePlus) {
this._summary = this.localize('components.selection.selected-plus', 'count', count);
} else {
this._summary = this.localize('components.selection.selected', 'count', count);
}
}
}
customElements.define('d2l-selection-summary', Summary);