/
sl-gallery.js
204 lines (167 loc) · 5.52 KB
/
sl-gallery.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
194
195
196
197
198
199
200
201
202
203
204
/**
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
import './sl-gallery-image.js';
import './sl-gallery-slideshow.js';
/**
* Silverlinkz Photo Gallery
*
* A web-component based hash-route photo gallery.
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class SLGallery extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
overflow: hidden;
--sl-gallery-columns: 4;
--sl-gallery-gutter: 8px;
--sl-gallery-item-height: 75%;
}
:host([hidden]) {
display: none;
}
.image-grid {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
-ms-flex-wrap: wrap;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
padding-top: var(--sl-gallery-gutter, 0px);
padding-left: var(--sl-gallery-gutter, 0px);
box-sizing: border-box;
/* @override, hide gutter around grid */
padding: 0;
margin: 0 calc(-1 * var(--sl-gallery-gutter, 0px))
calc(-1 * var(--sl-gallery-gutter, 0px)) 0;
}
</style>
<div class="image-grid" on-click="_handleGridClicked">
<slot></slot>
</div>
<!-- Workaround for not being able to set "prefix" without a binding -->
<span style="display: none;">{{prefix}}</span>
`;
}
static get is() { return 'sl-gallery'; }
static get properties() {
return {
active: {
type: Boolean,
value: true,
observer: '_activeChanged',
},
prefix: {
type: String,
value: 'i',
},
_activeIndex: {
type: Number,
value: null,
observer: '_activeIndexChanged',
},
_images: {
type: Array,
observer: '_imagesChanged',
},
};
}
constructor() {
super();
// Create slideshow element, keep handle as this.slideshow.
// It is appended to body to escape the stacking context
// until we have widespread support for the `dialog` element.
this.slideshow = document.createElement('sl-gallery-slideshow');
// Create reference to local scope in slideshow
this.slideshow.gallery = this;
// Hide slideshow before appending to prevent potential flash of content
this.slideshow.hidden = true;
// Append slideshow to root custom-element
document.body.appendChild(this.slideshow);
}
connectedCallback() {
super.connectedCallback();
// Initialize hash route and listen for changes
this._updateRoute = this._updateRoute.bind(this);
window.addEventListener('hashchange', this._updateRoute);
this._updateRoute();
// Create observer to watch for new nodes added from light dom
this._observer = new FlattenedNodesObserver(this, (info) => {
const blacklist = ['dom-repeat', 'dom-if'];
this._images = [...info.target.children]
.filter(el => blacklist.indexOf(el.nodeName.toLowerCase()) === -1);
});
}
disconnectedCallback() {
super.disconnectedCallback();
// Remove listeners
window.removeEventListener('hashchange', this._updateRoute);
// Disconnect flattened nodes observer
this._observer.disconnect();
}
_updateRoute() {
const hash = window.location.hash;
let routeIndex = null;
if (hash && hash.substr(1, 1) === '/') {
const [, prefix, rawIndex] = hash.split('/');
if (prefix && prefix === this.prefix) {
const index = parseInt(rawIndex);
if (!isNaN(index)) {
routeIndex = index;
}
}
}
this._activeIndex = routeIndex;
}
_resetRoute() {
window.history.pushState(null, document.title,
window.location.pathname + window.location.search);
this._updateRoute();
}
_handleGridClicked(event) {
if (event.target.index !== undefined) {
window.location.hash = `/${this.prefix}/${event.target.index}`;
}
}
_imagesChanged(images) {
// Give each image an index based on position in array
images.forEach((image, i, arr) => {
image.index = i;
image.hasPreviousImage = i > 0;
image.hasNextImage = i < (arr.length - 1);
image.previousImage = image.hasPreviousImage ? arr[i - 1] : undefined;
image.nextImage = image.hasNextImage ? arr[i + 1] : undefined;
});
// Manually call index changed when images change to update routes
this._activeIndexChanged(this._activeIndex);
}
_activeChanged(active) {
// Reflect active state to slideshow
this.slideshow.active = active;
}
_activeIndexChanged(index) {
if (index !== null && this._images && this._images[index]) {
this.slideshow.activeImage = this._images[index];
} else {
this.slideshow.activeImage = undefined;
}
}
}
window.customElements.define(SLGallery.is, SLGallery);