-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxxwv_gallery_handling.js
231 lines (226 loc) · 5.71 KB
/
maxxwv_gallery_handling.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// maxxwv.com JavaScript Document
/* jshint esversion: 6 */
/* global mwv */
import _ from 'lodash';
import { stringify } from 'query-string';
class Gallery{
constructor(args){
this.galleryDiv = args.galleryDiv;
this.descrDiv = args.descrDiv;
this._$ = args.$;
this.duration = parseInt(args.duration) || 1000;
if(!this.galleryDiv instanceof this._$){
this.galleryDiv = this._$(this.galleryDiv);
}
if(!this.descrDiv instanceof this._$){
this.descrDiv = this._$(this.descrDiv);
}
}
/**
* Make all the links triggers for gallery loading.
* By doing it this way, I'm making sure that if the user has
* javascript turned off, they'll still get to the gallery
* page and be able to bookmark it if they want to.
* @param Array elems Array of jQuery link objects
* @return void
*/
activateLinks(elems){
_.each(elems, link => {
link.onclick = e => {
e.preventDefault();
this._currentClick = e.target;
this.changeGallery(e.target.parentElement.dataset.albumId);
};
});
}
/**
* Run the entire object to change out the gallery dynamically
* @param string albumID Numerical DB index of the album in question
* @return void
*/
changeGallery(albumID){
this.addOverlay()
.then(() => this.removeDescription())
.then(() => this.slideUpGallery())
.then(() => this.loadAlbumGalleries(albumID))
.then((data) => this.populateGallery(data))
.then((slug) => this.updateURL(slug))
.then(() => this.revealDescription())
.then(() => this.slideDownGallery())
.then(() => this.removeOverlay());
}
/**
* Add a fixed overlay div so the user knows something's happening
*/
addOverlay(){
return new Promise((resolve) => {
let ovrly = document.createElement('div'),
spin = document.createElement('div');
ovrly.classList.add('overlay');
ovrly.appendChild(spin);
spin.classList.add('fa', 'fa-spinner', 'fa-pulse');
ovrly.id = 'gallery-overlay';
document.body.appendChild(ovrly);
resolve(true);
});
}
/**
* Remove the overlay div
*/
removeOverlay(){
document.body.removeChild(document.getElementById('gallery-overlay'));
}
/**
* Remove the main description, assuming it exists
* @return Promise.resolve
*/
removeDescription(){
return new Promise((resolve) => {
this.descrDiv.slideUp(
this.duration,
() => {
this.descrDiv.html('');
resolve(true);
}
);
});
}
/**
* Slide down the description div for the album
* @return void
*/
revealDescription(){
return new Promise((resolve) => {
this.descrDiv.slideDown(
this.duration,
() => {
resolve(true);
}
);
});
}
/**
* Remove the current gallery
* @return Promise.resolve
*/
slideUpGallery(){
return new Promise((resolve) => {
this.galleryDiv.slideUp(
this.duration,
() => {
this.galleryDiv.html('');
resolve(true);
}
);
});
}
/**
* Slide down the gallery div
* @return void
*/
slideDownGallery(){
return new Promise((resolve) => {
this.galleryDiv.slideDown(
this.duration,
() => {
resolve(true);
}
);
});
}
/**
* Affect the history state for bookmarks
* @param string slug Link to activate
* @return Promise
*/
updateURL(slug){
return new Promise((resolve) => {
let crGall = document.querySelector('.album-link.selected');
crGall.classList.remove('selected');
this._currentClick.classList.add('selected');
window.history.pushState({}, slug, '/design/' + slug);
resolve(true);
});
}
/**
* Get the selected album galleries via AJAX and return the data
* @param int albumID ID of the selected album
* @throws type
* @return void
*/
loadAlbumGalleries(albumID){
return fetch(
mwv.ajax_url, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
},
method: 'post',
credentials: 'same-origin',
body: stringify({action: 'get_galleries_by_album', album: albumID}),
}).then(resp => {
if(!resp.ok){
throw new Error(resp);
}
return resp.json();
}).then(data => {
return data;
}).catch(err => {
console.log(err.message);
});
}
/**
* Use the returned data to populate the gallery div
* @param object data JSON-decoded data object from server
* @return Promise
*/
populateGallery(data){
return new Promise((resolve, reject) => {
if(!data.success){
reject('No galleries found');
}
let galleryNode = document.createElement('div'),
slug = '';
galleryNode.classList.add('flex-wrap');
this.galleryDiv[0].appendChild(galleryNode);
_.each(data.gallery, g => {
this.descrDiv.html(g.description);
if(g.description && this.descrDiv.hasClass('empty')){
this.descrDiv.removeClass('empty');
}else{
if(!this.descrDiv.hasClass('empty')){
this.descrDiv.addClass('empty');
}
}
_.each(g.images, img => {
let wrpNode = document.createElement('div');
wrpNode.classList.add('gallery-wrap');
wrpNode.dataset.title = img.title;
let anchorNode = document.createElement('a');
anchorNode.href = img.sizes.full.url;
anchorNode.dataset.lightbox = g.title;
anchorNode.dataset.title = img.description.replace(/(<([^>]+)>)/ig,"");
anchorNode.classList.add('lazyload');
let imgNode = new Image(img.sizes.thumbnail.width, img.sizes.thumbnail.height);
imgNode.src = img.sizes.thumbnail.url;
imgNode.title = img.title;
imgNode.alt = img.alt;
anchorNode.appendChild(imgNode);
wrpNode.appendChild(anchorNode);
galleryNode.appendChild(wrpNode);
});
slug = this._currentClick.innerHTML;
});
resolve(slug);
});
}
}
(($) => {
'use strict';
let gallery = new Gallery({
galleryDiv: $('#gallery'),
descrDiv: $('#gallery-description'),
$: $
});
gallery.activateLinks($('.album-link'));
var ll = new LazyLoad();
}) (jQuery);