-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathlist-item-checkbox-mixin.js
193 lines (168 loc) · 5.36 KB
/
list-item-checkbox-mixin.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
185
186
187
188
189
190
191
192
193
import '../selection/selection-input.js';
import { css, html, nothing } from 'lit';
import { getUniqueId } from '../../helpers/uniqueId.js';
import { SelectionInfo } from '../selection/selection-mixin.js';
import { SkeletonMixin } from '../skeleton/skeleton-mixin.js';
export const ListItemCheckboxMixin = superclass => class extends SkeletonMixin(superclass) {
static get properties() {
return {
/**
* **Selection:** Disables selection
* @type {boolean}
*/
selectionDisabled: { type: Boolean, attribute: 'selection-disabled', reflect: true },
/**
* **Selection:** Value to identify item if selectable
* @type {string}
*/
key: { type: String, reflect: true },
/**
* **Selection:** Indicates an input should be rendered for selecting the item
* @type {boolean}
*/
selectable: { type: Boolean },
/**
* **Selection:** Whether the item is selected
* @type {boolean}
*/
selected: { type: Boolean, reflect: true },
/**
* Private. The selection info (set by the selection component).
* @ignore
*/
selectionInfo: { type: Object, attribute: false },
_hoveringSelection: { type: Boolean, attribute: '_hovering-selection', reflect: true }
};
}
static get styles() {
const styles = [ css`
.d2l-checkbox-action {
cursor: pointer;
display: block;
height: 100%;
}
:host([selection-disabled]) .d2l-checkbox-action {
cursor: default;
}
` ];
super.styles && styles.unshift(super.styles);
return styles;
}
constructor() {
super();
this.selectable = false;
this.selected = false;
this.selectionDisabled = false;
this.selectionInfo = new SelectionInfo();
this._checkboxId = getUniqueId();
}
get selectionInfo() {
return this._selectionInfo;
}
set selectionInfo(val) {
const oldVal = this._selectionInfo;
if (oldVal !== val) {
this._selectionInfo = val;
this.setSelected(this._selectionInfo.state === SelectionInfo.states.all);
this.requestUpdate('selectionInfo', oldVal);
}
}
connectedCallback() {
super.connectedCallback();
if (this.selectable) {
if (!this.key) console.warn('ListItemCheckboxMixin requires a key.');
}
if (!this.key) this.setSelected(undefined, true);
}
updated(changedProperties) {
super.updated(changedProperties);
if (!this._selectionProvider || !changedProperties.has('selectionInfo')) return;
this.selected = (this.selectionInfo.state === SelectionInfo.states.all);
}
willUpdate(changedProperties) {
super.willUpdate(changedProperties);
if (changedProperties.has('selectionDisabled') && this.selectionDisabled === true) this._hoveringSelection = false;
}
setSelected(selected, suppressEvent = false) {
if (this.selected === selected || (this.selected === undefined && !selected)) return;
this.selected = selected;
if (!suppressEvent) this._dispatchSelected(selected);
}
async _dispatchSelected(value) {
/* wait for internal state to be updated in case of action-click case so that a consumer
calling getSelectionInfo will get the correct state */
await this.updateComplete;
/** Dispatched when the component item is selected */
this.dispatchEvent(new CustomEvent('d2l-list-item-selected', {
detail: { key: this.key, selected: value },
composed: true,
bubbles: true
}));
}
_onCheckboxActionClick(event) {
event.preventDefault();
if (this.selectionDisabled) return;
this.setSelected(!this.selected);
const checkbox = this.shadowRoot && this.shadowRoot.querySelector(`#${this._checkboxId}`);
if (checkbox) checkbox.focus();
}
_onCheckboxChange(event) {
this.setSelected(event.target.selected);
if (this._selectionProvider) {
if (this.selected && this.selectionInfo.state !== SelectionInfo.states.all || !this.selected && this.selectionInfo.state === SelectionInfo.states.all) {
this._selectionProvider.setSelectionForAll(this.selected);
}
}
}
_onMouseEnterSelection() {
this._hoveringSelection = !this.selectionDisabled;
}
_onMouseLeaveSelection() {
this._hoveringSelection = false;
}
_onNestedSlotChangeCheckboxMixin() {
this._updateNestedSelectionProvider();
}
_onSelectionProviderConnected(e) {
e.stopPropagation();
this._updateNestedSelectionProvider();
}
_renderCheckbox() {
return this.selectable ? html`
<d2l-selection-input
@d2l-selection-change="${this._onCheckboxChange}"
?disabled="${this.selectionDisabled}"
.hovering="${this._hoveringSelection}"
id="${this._checkboxId}"
?_indeterminate="${this.selectionInfo.state === SelectionInfo.states.some}"
key="${this.key}"
label="${this.label}"
?selected="${this.selected}"
?skeleton="${this.skeleton}">
</d2l-selection-input>
` : nothing;
}
_renderCheckboxAction(inner) {
return this.selectable ? html`
<div class="d2l-checkbox-action"
@click="${this._onCheckboxActionClick}"
@mouseenter="${this._onMouseEnterSelection}"
@mouseleave="${this._onMouseLeaveSelection}">
${inner}
</div>
` : nothing;
}
_updateNestedSelectionProvider() {
if (!this.selectable) return;
const nestedList = this._getNestedList();
if (this._selectionProvider === nestedList) return;
if (this._selectionProvider && this._selectionProvider !== nestedList) {
this._selectionProvider.unsubscribeObserver(this);
this._selectionProvider = null;
}
if (nestedList) {
this._selectionProvider = nestedList;
this._selectionProvider.subscribeObserver(this);
}
}
};