-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfrontend.js
224 lines (189 loc) · 6.09 KB
/
frontend.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
/* eslint-disable no-undef */
// get the sticky element
const stickyDetector = document.querySelector( '#navbar-sticky-detector' );
const stickyElement = document.querySelector( '.navbar-main' );
const observer = new IntersectionObserver(
( [ e ] ) => {
stickyElement.classList.toggle( 'stuck', ! e.isIntersecting );
},
{
rootMargin: '0px',
threshold: [ 0.1 ],
}
);
if ( stickyDetector ) {
observer.observe( stickyDetector );
}
jQuery( function () {
jQuery( '.show-leaflet' ).on( 'click', function () {
const lat = jQuery( '.show-leaflet' ).data( 'lat' );
const lon = jQuery( '.show-leaflet' ).data( 'lon' );
const zoom = jQuery( '.show-leaflet' ).data( 'zoom' );
showLeaflet( lat, lon, zoom );
} );
jQuery( '#privacy_policy_url' ).attr(
'href',
sunflower.privacy_policy_url
);
jQuery( '.show-search' ).on( 'click', function () {
jQuery( '.topmenu .search input' ).toggleClass( 'active' );
jQuery( '.topmenu .search input' ).trigger( 'focus' );
} );
jQuery( '.show-contrast' ).on( 'click', function () {
jQuery( 'html' ).toggleClass( 'theme--contrast' );
jQuery( 'html' ).toggleClass( 'theme--default' );
localStorage.setItem(
'theme--contrast',
jQuery( 'html' ).hasClass( 'theme--contrast' )
);
} );
if ( localStorage.getItem( 'theme--contrast' ) === 'true' ) {
jQuery( 'html' ).addClass( 'theme--contrast' );
jQuery( 'html' ).removeClass( 'theme--default' );
}
adjustMetaboxHeight();
addRssReadMore();
// mailto unscrambler
jQuery( '[data-unscramble]' ).on( 'click', function () {
const text = jQuery( this )
.data( 'unscramble' )
.split( '' )
.reverse()
.join( '' );
window.location.href = 'MAILTO:' + text;
return false;
} );
jQuery( '.wp-block-gallery figure' ).each( function () {
const caption = jQuery( 'figcaption', this ).text();
jQuery( 'a', this )
.first()
.attr( 'data-lightbox', 'sunflower-gallery' );
jQuery( 'a', this ).first().attr( 'data-title', caption );
} );
lightbox.option( {
albumLabel: 'Bild %1 von %2',
} );
} );
function getIcon() {
return L.icon( {
iconUrl: sunflower.maps_marker,
iconSize: [ 25, 41 ], // size of the icon
shadowSize: [ 0, 0 ], // size of the shadow
iconAnchor: [ 12, 41 ], // point of the icon which will correspond to marker's location
shadowAnchor: [ 0, 0 ], // the same for the shadow
popupAnchor: [ 0, -41 ], // point from which the popup should open relative to the iconAnchor
} );
}
function showLeaflet( lat, lon, zoom ) {
const leaflet = L.map( 'leaflet' ).setView( [ lat, lon ], zoom );
L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution:
'© <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>',
} ).addTo( leaflet );
L.marker( [ lat, lon ], { icon: getIcon() } ).addTo( leaflet );
}
jQuery( '.show-leaflet-all' ).on( 'click', function showLeafletAll() {
const leaflet = L.map( 'leaflet' ).setView(
[ map.center.lat, map.center.lon ],
map.center.zoom
);
L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution:
'© <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>',
} ).addTo( leaflet );
map.marker.forEach( ( marker ) =>
L.marker( [ marker.lat, marker.lon ], { icon: getIcon() } )
.addTo( leaflet )
.bindPopup( marker.content )
);
} );
jQuery( '#sunflower-contact-form' ).on( 'submit', function ( e ) {
e.preventDefault();
jQuery
.ajax( {
url: sunflower.ajaxurl,
method: 'POST',
data: {
action: 'sunflower_contact_form',
_wpnonce: jQuery( '#_wpnonce' ).val(),
message: jQuery( '#message' ).val(),
name: jQuery( '#name' ).val(),
mail: jQuery( '#mail' ).val(),
mailTo: jQuery( '#mail-to' ).val(),
phone: jQuery( '#phone' ).val(),
title: jQuery( '#contact-form-title' ).html(),
captcha: jQuery( '#captcha' ).val(),
sendCopy: jQuery( '#send-copy' ).val(),
},
} )
.done( function ( response ) {
response = JSON.parse( response );
if ( response.code === 500 ) {
jQuery( '#sunflower-contact-form' ).append(
'<div class="bg-danger p-4 text-white">' +
response.text +
'</div>'
);
return;
}
jQuery( '#sunflower-contact-form' ).html( response.text );
} );
return false;
} );
function adjustMetaboxHeight() {
if ( ! jQuery( '.metabox' ).length ) {
return;
}
const tooBig =
jQuery( '.metabox' ).outerHeight() -
jQuery( '.entry-header' ).outerHeight();
if ( tooBig <= 0 ) {
return;
}
jQuery( '.entry-content' ).prepend( '<div class="metabox-spacer"></div>' );
jQuery( '.metabox-spacer' ).height( tooBig + 'px' );
}
/* add read more link to rss block items */
function addRssReadMore() {
// loop over every title with link and add it as "read more" link
jQuery( '.wp-block-rss .wp-block-rss__item' ).each( function () {
const titlelink = jQuery( '.wp-block-rss__item-title a', this );
const mydiv = jQuery( '<div class="d-flex flex-row-reverse">' );
const moreLink = jQuery( '<a />', {
href: titlelink.attr( 'href' ),
class: 'continue-reading',
rel: 'bookmark',
text: sunflower.texts.readmore,
} );
jQuery( this ).append( mydiv.append( moreLink ) );
// convert excerpt into link to have same behaviour as latest-posts
const excerpt = jQuery( '.wp-block-rss__item-excerpt', this ).text();
const newExcerpt = jQuery( '<div>', {
class: 'wp-block-rss__item-excerpt entry-content',
} );
jQuery( '.wp-block-rss__item-excerpt', this ).replaceWith(
newExcerpt.append( titlelink.clone().text( excerpt ) )
);
} );
}
// make parent item of dropdowm menu clickable which is not intended by Bootstrap
jQuery( '.dropdown .dropdown-toggle' ).on( 'click', function () {
if ( jQuery( '.dropdown:hover' ).length !== 0 ) {
window.location = jQuery( this ).attr( 'href' );
}
return false;
} );
jQuery( function () {
jQuery( '.navbar-toggler' ).click( function () {
if ( jQuery( '.navbar-toggler' ).hasClass( 'collapsed' ) ) {
window.setTimeout( () => {
jQuery( 'body' ).removeClass( 'navbar-open' );
}, 100 );
} else {
jQuery( 'body' ).addClass( 'navbar-open' );
}
} );
} );
/* eslint-enable no-undef */