-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
362 lines (337 loc) · 12.1 KB
/
javascript.js
File metadata and controls
362 lines (337 loc) · 12.1 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* ==========
Version 3
Blog Banner Styles Plugin for Squarespace
Copyright Will Myers
========== */
(function(){
let $configEl = $('[data-wm-plugin="blog-post"]');
function initBlogBanner() {
let cssFile = 'https://cdn.jsdelivr.net/gh/willmyethewebsiteguy/BlogPostBanner@3/styles.min.css?v2';
if(!document.querySelector('#wm-blog-banner-css')){
addCSSFileToHeader(cssFile);
}
function addCSSFileToHeader(url){
let head = document.getElementsByTagName('head')[0],
link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
head.appendChild(link);
}
//Config Styles
let style =
$configEl.attr("data-post-style") == undefined
? "1"
: $configEl.attr("data-post-style"),
headerTheme =
$configEl.attr("data-theme") == undefined
? "black"
: $configEl.attr("data-theme"),
opacity =
$configEl.attr("data-opacity") == undefined
? "0"
: $configEl.attr("data-opacity"),
imgSrc =
$configEl.attr("data-img-src") == undefined
? "thumbnail"
: $configEl.attr("data-img-src"),
excerpt =
$configEl.attr("data-excerpt") == undefined
? false
: $configEl.attr("data-excerpt"),
baseUrl = location.protocol + "//" + location.host + location.pathname,
$section =
document.querySelector(
"#sections > .page-section.content-collection"
) ||
document.querySelector("main.Main--blog-item") ||
document.querySelector("main.Main--events-item"),
$sectionBackground =
$section.querySelector(".section-background") ||
document.createElement("section"),
$sectionContent =
$section.querySelector(".content-wrapper") ||
$section.querySelector("section.Main-content"),
$title =
$sectionContent.querySelector(".blog-item-top-wrapper") ||
$sectionContent.querySelector(".eventitem-column-meta") ||
document.createElement("div"),
$titleClone = $title.cloneNode(true),
body = document.querySelector("body");
//Get JSON Post Data
let postData;
$.getJSON(baseUrl + "/?format=json-pretty", {_: new Date().getTime()} ,function (data) {
postData = data;
let posX = data.item.mediaFocalPoint.x * 100 + "%",
posY = data.item.mediaFocalPoint.y * 100 + "%",
focalPoint = posX + " " + posY;
body.style.setProperty("--image-focal-point", focalPoint);
if (imgSrc == "seo") {
if (postData.item.seoData && postData.item.seoData.seoImage && postData.item.seoData.seoImage.assetUrl) {
imgSrc = postData.item.seoData.seoImage.assetUrl;
} else {
imgSrc = postData.item.assetUrl;
}
buildImage();
}
if (imgSrc == "thumbnail") {
imgSrc = postData.item.assetUrl;
buildImage();
}
if (excerpt == 'true' || excerpt == true) {
excerpt = postData.item.excerpt;
let excerptHTML = `<div class="excerpt">${excerpt}</div>`;
$title.insertAdjacentHTML('beforeend', excerptHTML);
$titleClone.insertAdjacentHTML('beforeend', excerptHTML);
}
});
//If 7.0 Website
if (window.Static.SQUARESPACE_CONTEXT.templateVersion == "7") {
document.querySelector("body").classList.add("sqs-seven");
$sectionBackground.classList.add("Index-page--has-image");
let main = document.querySelector("main.Main--blog-item");
$titleClone.classList.add("blog-item-top-wrapper", "clone");
//Add in Section
$section.prepend($sectionBackground);
//Add Title Clone Content
try {
let title =
document.querySelector(".BlogItem-title").cloneNode(true) ||
document.querySelector(".eventitem-title").cloneNode(true);
$titleClone.append(title);
} catch (err) {
console.log("No Title");
}
try {
let meta =
document.querySelector(".BlogItem-meta").cloneNode(true) ||
document
.querySelector(".event-meta-date-time-container")
.cloneNode(true);
$titleClone.append(meta);
} catch (err) {
console.log("No Meta");
}
try {
let share =
document.querySelector(".BlogItem-share").cloneNode(true) ||
document
.querySelector(".event-meta event-meta-addtocalendar-container")
.cloneNode(true);
$titleClone.append(share);
} catch (err) {
console.log("No Share");
}
}
body.classList.add("wm-banner-style-" + style);
$section.classList.add("has-banner");
$sectionBackground.classList.add("wm-blog-banner");
$titleClone.classList.add("clone");
if ($titleClone.querySelector('[itemprop="headline"]')) {
$titleClone.querySelector('[itemprop="headline"]')
.removeAttribute('itemprop');
}
if ($titleClone.querySelector('[data-content-field="title"]')) {
$titleClone.querySelector('[data-content-field="title"]')
.removeAttribute('data-content-field');
}
//Build Section Background
let overlay = document.createElement("div"),
sectionBackgroundImg = document.createElement("div"),
sectionBackgroundContent = document.createElement("div");
overlay.classList.add("section-background-overlay");
sectionBackgroundImg.classList.add("section-background-image");
sectionBackgroundContent.classList.add(
"section-background-content",
"blog-item-wrapper"
);
if (imgSrc !== 'thumbnail' && imgSrc !== 'seo') {
buildImage()
}
sectionBackgroundImg.append(overlay);
$sectionBackground.append(sectionBackgroundImg);
$sectionBackground.append(sectionBackgroundContent);
//Add in Title
sectionBackgroundContent.append($titleClone);
//Make Content Background Same Color on 7.1
let backgroundColor = window
.getComputedStyle($sectionBackground)
.getPropertyValue("background-color");
if (window.Static.SQUARESPACE_CONTEXT.templateVersion !== "7") {
body.style.setProperty("--section-background-color", backgroundColor);
}
function buildImage() {
let img = document.createElement("img") ;
img.setAttribute("data-src", imgSrc);
img.src = imgSrc;
sectionBackgroundImg.append(img);
}
//Set Content Width Variable
if (window.Static.SQUARESPACE_CONTEXT.templateVersion !== "7") {
try {
let sectionWidth = findSectionWidth();
body.style.setProperty("--section-content-width", sectionWidth);
} catch (err) {
console.log(err);
}
}
if (window.Static.SQUARESPACE_CONTEXT.templateVersion == "7") {
document.addEventListener('DOMContentLoaded', function() {
loadAllImages();
})
}
//Add Class After Work Is Complete
body.classList.add("tweak-wm-banner");
}
/*======= FUNCTIONS =========*/
/*If In Edit Mode*/
function watchEditMode() {
const targetNode = document.querySelector("body");
const config = {
attributes: true,
childList: false,
subtree: false,
attributeFilter: ["class"],
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(ifInEditMode).observe(
targetNode,
config
);
// Callback function to execute when mutations are observed
function ifInEditMode(mutationList, observer) {
if (targetNode.classList.contains("sqs-layout-editing")) {
$('link[href*="BlogPostBanner"]').attr("disabled", "disabled");
$('link#wm-blog-banner-css').attr("disabled", "disabled");
$(".wm-blog-banner .section-background-image").hide();
$(".wm-blog-banner .section-background-content").hide();
} else {
$('link[href*="BlogPostBanner"]').removeAttr("disabled");
$(".wm-blog-banner .section-background-image").show();
$(".wm-blog-banner .section-background-content").show();
}
}
}
/*Find Section Width in 7.1*/
let findSectionWidth = () => {
let width,
tweakJSONWidth =
window.Static.SQUARESPACE_CONTEXT.tweakJSON["tweak-blog-item-width"];
if (tweakJSONWidth == "Narrow") {
width = "50%";
} else if (tweakJSONWidth == "Medium") {
width = "75%";
} else if (tweakJSONWidth == "Wide") {
width = "100%";
} else if (tweakJSONWidth == "Custom") {
width =
window.Static.SQUARESPACE_CONTEXT.tweakJSON[
"tweak-blog-item-custom-width"
] + "%";
}
return width;
};
/* Get Image Aspect Ratio in 7.1 */
let getImageAspect = () => {
let aspectRatio,
el = document.querySelector(".section-background-image img"),
imgDimen = [];
imgDimen[0] = el.getAttribute("data-image-dimensions").split("x")[0];
imgDimen[1] = el.getAttribute("data-image-dimensions").split("x")[1];
aspectRatio = (imgDimen[1] / imgDimen[0]) * 100 + "%";
return aspectRatio;
};
/* Load Images */
let loadAllImages = () => {
try {
var images = document.querySelectorAll(".wm-blog-banner img[data-src]");
for (var i = 0; i < images.length; i++) {
ImageLoader.load(images[i], { load: true });
}
} catch (err) {
console.log(err)
}
};
/* init 7.1 */
if (window.Static.SQUARESPACE_CONTEXT.templateVersion !== "7") {
let isCollectionPage = !!document.querySelector('body[id*="collection"]');
if (isCollectionPage) return;
let active = !!$configEl.not('[data-no-edit-mode]').length || (window.self == window.top);
if($configEl.length && active){
initBlogBanner();
if (window.self !== window.top){
/*Fix for not working with Flex Animations*/
let bodyCL = document.body.classList
if (bodyCL.contains('tweak-global-animations-animation-type-flex')) {
let s = document.querySelector('.has-banner .content-wrapper .blog-item-top-wrapper');
if (s){
s.style.display = 'flex';
s.style.position = 'absolute';
s.style.zIndex = '1';
s.style.opacity = '0';
window.setTimeout(function(){
s.style.display = ''
s.style.position = 'relative';
s.style.zIndex = '';
s.style.opacity = '1'
}, 3000);
}
}
watchEditMode();
}
}
} else {
window.Squarespace.onInitialize(Y, function(){
$configEl = $('[data-wm-plugin="blog-post"]');
if($configEl.length){
initBlogBanner();
setTimeout(function(){
Y.config.win.Squarespace.initializeLayoutBlocks(Y)
}, 500)
if (window.self !== window.top){
watchEditMode();
}
}
});
}
}());
/*Global Vars*/
//Set Header Height & Bottom Pos
let wM = window.wM || {};
(function(){
let version = 1.0;
if (document.getElementById('header')){
wM.header = wM.header || {};
wM.header.version = typeof wM.header.version == 'undefined' ? version : (wM.header.version < version ? version : wM.header.version);
if (wM.header.version == version){
initHeaderLogic();
}
}
function initHeaderLogic(){
let headerObj = wM.header;
let $header = document.getElementById('header');
let root = document.documentElement;
headerObj.headerElem = $header;
headerObj.setHeaderCSS = function setHeight(){
headerObj.headerBottom = headerObj.headerElem.getBoundingClientRect().bottom;
headerObj.headerBottom = headerObj.headerBottom < 0 ? 0 : headerObj.headerBottom;
headerObj.headerHeight = headerObj.headerElem.getBoundingClientRect().height;
root.style.setProperty('--wM-headerBottom', headerObj.headerBottom + 'px');
root.style.setProperty('--wM-headerHeight', headerObj.headerHeight + 'px');
}
headerObj.setHeaderCSS();
headerObj.headerElem.addEventListener('transitionend', () => {
headerObj.setHeaderCSS();
});
window.addEventListener('scroll', () => {
setTimeout(function(){
headerObj.setHeaderCSS();
}, 150);
});
window.addEventListener('resize', () => {
setTimeout(function(){
headerObj.setHeaderCSS();
}, 150);
});
}
}());