-
Notifications
You must be signed in to change notification settings - Fork 25
/
list-item-expand-collapse-mixin.js
171 lines (156 loc) · 4.97 KB
/
list-item-expand-collapse-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
import '../button/button-icon.js';
import '../loading-spinner/loading-spinner.js';
import { css, html, nothing } from 'lit';
import { EventSubscriberController } from '../../controllers/subscriber/subscriberControllers.js';
import { SkeletonMixin } from '../skeleton/skeleton-mixin.js';
const dragIntervalDelay = 100;
const dragHoverDropTime = 1000;
export const ListItemExpandCollapseMixin = superclass => class extends SkeletonMixin(superclass) {
static get properties() {
return {
/**
* Whether to show the expand collapse toggle
* @type {boolean}
*/
expandable: { type: Boolean },
/**
* Default state for expand collapse toggle - if not set, collapsed will be the default state
* @type {boolean}
*/
expanded: { type: Boolean, reflect: true },
_siblingHasNestedItems: { state: true },
_renderExpandCollapseSlot: { type: Boolean, reflect: true, attribute: '_render-expand-collapse-slot' },
_showNestedLoadingSpinner: { state: true }
};
}
static get styles() {
const styles = [ css`
:host {
--d2l-expand-collapse-slot-transition-duration: 0.3s;
}
.d2l-list-expand-collapse {
line-height: 0;
width: 0;
}
:host([dir="rtl"][_render-expand-collapse-slot]) .d2l-list-expand-collapse {
padding: 0.55rem 0 0 0.3rem;
}
.d2l-list-expand-collapse d2l-button-icon {
--d2l-button-icon-min-height: 1.2rem;
--d2l-button-icon-min-width: 1.2rem;
}
.d2l-list-expand-collapse:hover d2l-button-icon {
background-color: var(--d2l-button-icon-background-color-hover);
border-radius: var(--d2l-button-icon-border-radius);
}
:host([_render-expand-collapse-slot]) .d2l-list-expand-collapse {
padding: 0.55rem 0.3rem 0 0;
width: 1.2rem;
}
.d2l-list-expand-collapse-action {
cursor: pointer;
display: block;
height: 100%;
}
.d2l-list-nested-loading {
display: flex;
justify-content: center;
padding: 0.4rem;
}
@media (prefers-reduced-motion: no-preference) {
.d2l-list-expand-collapse {
transition: width var(--d2l-expand-collapse-slot-transition-duration) cubic-bezier(0, 0.7, 0.5, 1);
}
}
` ];
super.styles && styles.unshift(super.styles);
return styles;
}
constructor() {
super();
this._siblingHasNestedItems = false;
this._renderExpandCollapseSlot = false;
this._showNestedLoadingSpinner = false;
this._parentChildUpdateSubscription = new EventSubscriberController(this, 'list-child-status');
}
connectedCallback() {
super.connectedCallback();
// mixin requires key for events
if (!this.key && this.expandable) {
console.warn('ListItemExpandCollapseMixin requires a key.');
this.expandable = false;
}
}
updated(changedProperties) {
if (changedProperties.has('_siblingHasNestedItems') || changedProperties.has('expandable')) {
this._renderExpandCollapseSlot = this.expandable || this._siblingHasNestedItems;
}
if (changedProperties.has('_draggingOver') && this._draggingOver && this.dropNested && !this.expanded && this.expandable) {
let elapsedHoverTime = 0;
let dragIntervalId = null;
const watchDraggingOver = () => {
if (elapsedHoverTime >= dragHoverDropTime) {
if (this._draggingOver) {
this._toggleExpandCollapse();
}
clearInterval(dragIntervalId);
} else {
if (!this._draggingOver) {
clearInterval(dragIntervalId);
} else {
elapsedHoverTime += dragIntervalDelay;
}
}
};
// check if they are still hovered over same item every 100ms
dragIntervalId = setInterval(watchDraggingOver, dragIntervalDelay);
}
if (changedProperties.has('expanded') || changedProperties.has('_hasNestedList')) {
this._showNestedLoadingSpinner = this.expanded && !this._hasNestedList;
}
}
updateSiblingHasChildren(siblingHasNestedItems) {
this._siblingHasNestedItems = siblingHasNestedItems;
}
_renderExpandCollapse() {
if (!this.expandable) {
return nothing;
}
return html`
<d2l-button-icon
class="d2l-skeletize"
icon="${this.expanded ? 'tier1:arrow-collapse-small' : 'tier1:arrow-expand-small' }"
aria-expanded="${this.expanded ? 'true' : 'false'}"
text="${this.label}"
@click="${this._toggleExpandCollapse}"></d2l-button-icon>`;
}
_renderExpandCollapseAction() {
if (this.selectable || !this.expandable || this.noPrimaryAction) {
return nothing;
}
return html`<div class="d2l-list-expand-collapse-action" @click="${this._toggleExpandCollapse}"></div>`;
}
_renderNestedLoadingSpinner() {
if (!this.expandable || !this._showNestedLoadingSpinner) {
return nothing;
}
return html`
<div class="d2l-list-nested-loading">
<d2l-loading-spinner size="40"></d2l-loading-spinner>
</div>`;
}
_toggleExpandCollapse(e = null) {
if (e) {
e.stopPropagation();
}
if (!this.expandable) {
return;
}
this.expanded = !this.expanded;
/** Dispatched whenever the list item expand state is toggled. */
this.dispatchEvent(new CustomEvent('d2l-list-item-expand-collapse-toggled', {
composed: true,
bubbles: true
}));
}
};