forked from Brightspace/polymer-siren-behaviors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siren-entity-loading.js
117 lines (100 loc) · 2.89 KB
/
siren-entity-loading.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
import '@polymer/polymer/polymer-legacy.js';
import './entity-loading-behavior.js';
import 'fastdom/fastdom.js';
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.innerHTML = `<dom-module id="siren-entity-loading">
<template strip-whitespace="">
<style>
:host {
display: block;
position: relative;
min-height: var(--siren-entity-loading-min-height, 3rem);
display: flex;
flex-direction: column;
transition: min-height 400ms ease-out;
}
div {
display: flex;
flex-direction: row;
justify-content: space-between;
border: 0;
opacity: 0;
max-height: 0;
transition: opacity 400ms ease-out, max-height 400ms ease-out;
}
div.hidden {
display: none;
}
div.show {
max-height: none;
opacity: 1;
height: 100%;
}
.loading {
display: block;
justify-content: center;
position: absolute;
width: 100%;
}
</style>
<div class="loading show"><slot name="loading"></slot></div>
<div class="error hidden"><slot name="error"></slot></div>
<div class="fetched hidden"><slot></slot></div>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
Polymer({
is: 'siren-entity-loading',
properties: {
maxHeight: Number
},
behaviors: [
D2L.PolymerBehaviors.Siren.EntityLoadingBehavior
],
observers: [
'_transition(loading, error, fetched)'
],
_transition: function(loading, error, fetched) {
var self = this;
this.async(function() {
var maxHeight = self.maxHeight || 10;
self._transitionElement(self.$$('.loading'), loading ? maxHeight : null, 'loading', true);
self._transitionElement(self.$$('.error'), error ? maxHeight : null, 'error');
self._transitionElement(self.$$('.fetched'), fetched ? maxHeight : null, 'fetched');
}, 1);
},
_transitionElement: function(element, maxHeightRem, name, hideOnEnd) {
var self = this;
function onTransitionEnd() {
// remove "max-height" from the element's inline styles, so it can return to its initial value
fastdom.mutate(function() {
element.style.maxHeight = null;
if (hideOnEnd && !element.classList.contains('show')) {
element.classList.add('hidden');
self.style.minHeight = 0;
}
if (element.classList.contains('show') && name) {
element.dispatchEvent(new CustomEvent('siren-entity-loading-' + name, {
bubbles: true,
composed: true
}));
}
});
}
fastdom.mutate(function() {
if (hideOnEnd) {
self.style.minHeight = null;
}
element.classList.remove('hidden');
if (maxHeightRem) {
element.style.maxHeight = maxHeightRem + 'rem';
element.classList.add('show');
} else if (element.classList.contains('show')) {
element.style.maxHeight = '0px';
element.classList.remove('show');
}
setTimeout(onTransitionEnd, 400);
});
}
});