PageSourceSearch

https://www.onegreenplanet.org/wp-content/themes/kadence-child/js/custom.js?ver=1.0.2

js onegreenplanet.org collected 2026-10-01 09:06:59 UTC 17,883 bytes, 444 lines download raw bytes

1(function($) {
2    // ============================================
3    // INITIALIZATION FUNCTIONS (Reusable)
4    // ============================================
5    
6    // Initialize empty paragraph and list item handling
7    function initEmptyElements(container) {
8        var $scope = container ? jQuery(container) : jQuery('body');
9        $scope.find("p").each(function(){  
10            if (jQuery(this).text().trim() === "") {
11                jQuery(this).addClass("empty-p");
12            }
13         });  
14         $scope.find("li").each(function(){  
15            if (jQuery(this).text().trim() === "") {
16                jQuery(this).addClass("empty-li");
17            }
18         });
19    }
20    
21    // Initialize Amazon affiliate links
22    function initAmazonLinks(container) {
23        var $scope = container ? jQuery(container) : jQuery('body');
24        $scope.find('a').each(function() {
25            var $link = jQuery(this);
26            var _href = $link.attr('href');
27            if (_href && _href.indexOf('amazon.com') !== -1 && _href.indexOf('?tag=') === -1) {
28                var separator = _href.indexOf('?') > -1 ? '&' : '?';
29                $link.attr('href', _href + separator + 'tag=onegrepla-20');
30            }
31        });
32    }
33    
34    // Initialize recipe show more functionality
35    function initRecipeShowMore(container) {
36        var $scope = container ? jQuery(container) : jQuery('body');
37        
38        $scope.find('.bio p').each(function() {
39            var $bioP = jQuery(this);
40            if (!$bioP.hasClass('more-processed')) {
41                $bioP.addClass('more-processed');
42            }
43        });
44        
45        // Process bio paragraphs
46        var $bioContainer = $scope.find('.bio');
47        if ($bioContainer.length && !$bioContainer.hasClass('bio-processed')) {
48            $bioContainer.addClass('bio-processed');
49            var $p = $bioContainer.find('p');
50            if ($p.length > 0) {
51                $p.first().html(function(){
52                    return $p.map(function(){
53                        return jQuery(this).html();
54                    }).get().join(' ');
55                }).end().filter(':gt(0)').remove();
56                $bioContainer.find("p:first").addClass("more");
57                $bioContainer.find('br').remove();
58            }
59        }
60        
61        $scope.find('.menu-bar ul li.menu-item').find('br').remove();
62      
63        var showChar = 150;
64        var ellipsestext = "...";
65        var moretext = "Load More";
66        var lesstext = "Load Less";
67        
68        $scope.find('p.more').each(function() {
69            var $moreP = jQuery(this);
70            if ($moreP.hasClass('morelink-processed')) {
71                return; // Already processed
72            }
73            $moreP.addClass('morelink-processed');
74            
75            var content = $moreP.html();
76            if(content && content.length > showChar) {
77                var c = content.substr(0, showChar);
78                var h = content.substr(showChar, content.length - showChar);
79                var html = c + '<span class="moreellipses">' 
80                + ellipsestext+ ' </span><span class="morecontent"><span>' 
81                + h + '</span>  <a href="" class="morelink">' 
82                + moretext + '</a></span>';
83                $moreP.html(html);
84            }
85            if(content && content.length <= showChar){
86                $scope.find(".other_recipes").addClass("marginTop0");
87            }
88        });
89    }
90
91    // Post load scroll animation
92    jQuery(document.body).on('post-load', function() {
93        console.log("Post next loaded");        
94        jQuery('html, body').animate({
95            scrollTop: jQuery(window).scrollTop() + 100
96        }, 300);
97    });
98
99    // Initialize on document ready
100    jQuery(document).ready(function(){
101        initEmptyElements();
102        initAmazonLinks();
103        initRecipeShowMore();
104    });
105    
106    // Initialize on post-load event (for dynamically loaded content)
107    jQuery(document).on('post-load', function(e) {
108        // Get the newly loaded content container
109        var $newContent = jQuery('.infinite-wrap').last();
110        if ($newContent.length) {
111            initEmptyElements($newContent);
112            initAmazonLinks($newContent);
113            initRecipeShowMore($newContent);
114            initList1LoadMore($newContent);
115            initList2LoadMore($newContent);
116        } else {
117            // Fallback: initialize everything if no specific container
118            initEmptyElements();
119            initAmazonLinks();
120            initRecipeShowMore();
121            initList1LoadMore();
122            initList2LoadMore();
123        }
124    });
125    
126    // Fallback: Use MutationObserver to detect new content (in case post-load event isn't triggered)
127    // This ensures scripts work even if the event system isn't working
128    if (typeof MutationObserver !== 'undefined') {
129        var observer = new MutationObserver(function(mutations) {
130            mutations.forEach(function(mutation) {
131                if (mutation.addedNodes.length) {
132                    jQuery(mutation.addedNodes).each(function() {
133                        var $node = jQuery(this);
134                        // Check if this is an infinite-wrap or contains one
135                        var $infiniteWrap = $node.hasClass('infinite-wrap') ? $node : $node.find('.infinite-wrap');
136                        if ($infiniteWrap.length === 0 && $node.closest('.infinite-wrap').length) {
137                            $infiniteWrap = $node.closest('.infinite-wrap');
138                        }
139                        
140                        if ($infiniteWrap.length) {
141                            $infiniteWrap.each(function() {
142                                var $wrap = jQuery(this);
143                                if (!$wrap.hasClass('custom-js-initialized')) {
144                                    $wrap.addClass('custom-js-initialized');
145                                    // Small delay to ensure DOM is fully ready
146                                    setTimeout(function() {
147                                        initEmptyElements($wrap);
148                                        initAmazonLinks($wrap);
149                                        initRecipeShowMore($wrap);
150                                        initList1LoadMore($wrap);
151                                        initList2LoadMore($wrap);
152                                    }, 100);
153                                }
154                            });
155                        }
156                    });
157                }
158            });
159        });
160        
161        // Start observing the main content container
162        jQuery(document).ready(function() {
163            var $mainContent = jQuery('#main-content');
164            if ($mainContent.length) {
165                observer.observe($mainContent[0], {
166                    childList: true,
167                    subtree: true
168                });
169            }
170        });
171    }
172
173    // Load More for Breakers (list1)
174    function initList1LoadMore(container) {
175        var $scope = container ? jQuery(container) : jQuery('body');
176        
177        $scope.find(".list1").each(function(){
178            var $list = jQuery(this);
179            if ($list.hasClass('list1-initialized')) {
180                return; // Already initialized
181            }
182            $list.addClass('list1-initialized');
183            
184            var list = $list.children('li');      
185            var numToShow = 3;
186            list.hide();
187            list.slice(0, numToShow).show();
188        });
189        
190        goingviralposts($scope);
191    }
192    
193    function goingviralposts(container) {
194        var $scope = container ? jQuery(container) : jQuery('body');
195        
196        $scope.find(".load").closest(".loader").siblings().find('.list1').each(function(){
197            var $list = jQuery(this);
198            var list = $list.children('li');             
199            var numInList = list.length;              
200            var on_load = jQuery(this).parent().siblings().find('.load').attr('id');
201            var on_close = jQuery(this).parent().siblings().find('.closebtn').attr('id'); 
202            var on_view_more = jQuery(this).parent().siblings().find('.view-more').attr('id');
203            
204            if (!on_load) return;
205            
206            // Remove existing handlers to prevent duplicates
207            jQuery('#'+on_load).off('click.list1');
208            jQuery('#'+on_close).off('click.list1');
209            
210            // Use namespace for event handlers
211            jQuery('#'+on_load).on('click.list1', function() {
212                var $clickedLoad = jQuery(this);
213                var $targetList = $clickedLoad.parent().parent().siblings().closest(".infinite-wrap").find(".list1");
214                if ($targetList.length === 0) {
215                    $targetList = $clickedLoad.closest('.infinite-wrap').find(".list1");
216                }
217                if ($targetList.length === 0) {
218                    $targetList = $list;
219                }
220                
221                $targetList.addClass("loaded-section");
222                list.slice(3, numInList).fadeIn(200);
223                var nowShowing = list.filter(':visible').length;
224                if (nowShowing >= numInList) {
225                    jQuery('#'+on_load).css("display","none");
226                    if (on_view_more) jQuery('#'+on_view_more).css("display","inline-block");
227                    if (on_close) jQuery('#'+on_close).css("display","inline-block");
228                }
229            });
230            
231            if (on_close) {
232                jQuery('#'+on_close).on('click.list1', function() {
233                    var $clickedClose = jQuery(this);
234                    var $targetList = $clickedClose.parent().parent().siblings().closest(".infinite-wrap").find(".list1");
235                    if ($targetList.length === 0) {
236                        $targetList = $clickedClose.closest('.infinite-wrap').find(".list1");
237                    }
238                    if ($targetList.length === 0) {
239                        $targetList = $list;
240                    }
241                    
242                    $targetList.removeClass("loaded-section");
243                    list.slice(3, numInList).fadeOut(200);
244                    jQuery('#'+on_close).css("display","none");
245                    if (on_view_more) jQuery('#'+on_view_more).css("display","none");
246                    jQuery('#'+on_load).css("display","inline-block");
247                });
248            }
249        });
250    }
251    
252    jQuery(document).ready(function() {
253        if(!(jQuery("#main-content").parent().hasClass("infinite-wrap"))){
254            initList1LoadMore();
255        }
256    });
257    
258    jQuery(document).on("post-load", function(e) {
259        var $newContent = jQuery(".infinite-wrap").last();
260        if ($newContent.length) {
261            $newContent.find(".list1").each(function(){       
262                if (!jQuery(this).hasClass("loaded-section")){
263                    var list = jQuery(this).children('li');      
264                    var numToShow = 3;
265                    list.hide();
266                    list.slice(0, numToShow).show();
267                }                  
268            });
269            initList1LoadMore($newContent);
270        }
271    });
272
273    // Loadmore for 6-12 Item script (list2 - monthly top stories)
274    function initList2LoadMore(container) {
275        var $scope = container ? jQuery(container) : jQuery('body');
276        
277        $scope.find(".list2").each(function(){
278            var $list = jQuery(this);
279            if ($list.hasClass('list2-initialized')) {
280                return; // Already initialized
281            }
282            $list.addClass('list2-initialized');
283            
284            var list = $list.children('li');      
285            var numToShow = 6;
286            list.hide();
287            list.slice(0, numToShow).show();
288        });
289        
290        monthlytopstories($scope);
291    }
292    
293    function monthlytopstories(container) {
294        var $scope = container ? jQuery(container) : jQuery('body');
295        
296        $scope.find(".load").closest(".loader").siblings().find('.list2').each(function(){
297            var $list = jQuery(this);
298            var list = $list.children('li');             
299            var numInList = list.length;              
300            var on_load = jQuery(this).parent().siblings().find('.load').attr('id');
301            var on_close = jQuery(this).parent().siblings().find('.closebtn').attr('id'); 
302            var on_view_more = jQuery(this).parent().siblings().find('.view-more').attr('id');
303            
304            if (!on_load) return;
305            
306            // Remove existing handlers to prevent duplicates
307            jQuery('#'+on_load).off('click.list2');
308            jQuery('#'+on_close).off('click.list2');
309            
310            // Use namespace for event handlers
311            jQuery('#'+on_load).on('click.list2', function() {
312                var $clickedLoad = jQuery(this);
313                var $targetList = $clickedLoad.parent().parent().siblings().closest(".infinite-wrap").find(".list2");
314                if ($targetList.length === 0) {
315                    $targetList = $clickedLoad.closest('.infinite-wrap').find(".list2");
316                }
317                if ($targetList.length === 0) {
318                    $targetList = $list;
319                }
320                
321                $targetList.addClass("loaded-section");
322                list.slice(6, numInList).fadeIn(200);
323                var nowShowing = list.filter(':visible').length;
324                if (nowShowing >= numInList) {
325                    jQuery('#'+on_load).css("display","none");
326                    if (on_view_more) jQuery('#'+on_view_more).css("display","inline-block");
327                    if (on_close) jQuery('#'+on_close).css("display","inline-block");
328                }
329            });
330            
331            if (on_close) {
332                jQuery('#'+on_close).on('click.list2', function() {
333                    var $clickedClose = jQuery(this);
334                    var $targetList = $clickedClose.parent().parent().siblings().closest(".infinite-wrap").find(".list2");
335                    if ($targetList.length === 0) {
336                        $targetList = $clickedClose.closest('.infinite-wrap').find(".list2");
337                    }
338                    if ($targetList.length === 0) {
339                        $targetList = $list;
340                    }
341                    
342                    $targetList.removeClass("loaded-section");
343                    list.slice(6, numInList).fadeOut(200);
344                    jQuery('#'+on_close).css("display","none");
345                    if (on_view_more) jQuery('#'+on_view_more).css("display","none");
346                    jQuery('#'+on_load).css("display","inline-block");
347                });
348            }
349        });
350    }
351    
352    jQuery(document).ready(function() {
353        if(!(jQuery("#main-content").parent().hasClass("infinite-wrap"))){
354            initList2LoadMore();
355        }
356    });
357    
358    jQuery(document).on('post-load', function() {
359        var $newContent = jQuery(".infinite-wrap").last();
360        if ($newContent.length) {
361            $newContent.find(".list2").each(function(){       
362                if (!jQuery(this).hasClass("loaded-section")){
363                    var list = jQuery(this).children('li');      
364                    var numToShow = 6;
365                    list.hide();
366                    list.slice(0, numToShow).show();
367                }                  
368            });
369            initList2LoadMore($newContent);
370        }
371    });
372
373    // Recipe show more - use delegated event handler for dynamically added content
374    jQuery(document).on('click', '.more .morelink', function(e){
375        e.preventDefault();
376        var moretext = "Load More";
377        var lesstext = "Load Less";
378        
379        if(jQuery(this).hasClass("less")) {
380          jQuery(this).removeClass("less");
381          jQuery(this).html(moretext);
382        } else {
383          jQuery(this).addClass("less");
384          jQuery(this).html(lesstext);
385        }
386        jQuery(this).parent().prev().toggle();
387        jQuery(this).prev().toggle();
388        return false;
389    });
390
391    // Mobile responsive dropdown - use delegated event handler
392    jQuery(document).on('click', '.navbar a.dropdown-toggle', function(e) {
393        if (screen.width <= 768) {
394            var $el = jQuery(this);
395            var $parent = jQuery(this).offsetParent(".dropdown-menu");
396            jQuery(this).parent("li").toggleClass('open');
397
398            if(!$parent.parent().hasClass('nav')) {
399                $el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4});
400            }
401
402            jQuery('.nav li.open').not(jQuery(this).parents("li")).removeClass("open");
403
404            return false;
405        }
406    });
407
408    // Scroll to top when arrow up clicked
409    jQuery(window).scroll(function() {
410        var height = jQuery(window).scrollTop();
411        if (height > 100) {
412            jQuery('#back2Top').fadeIn();
413        } else {
414            jQuery('#back2Top').fadeOut();
415        }
416    });
417
418    jQuery(document).ready(function() {
419        jQuery("#back2Top").click(function(event) {
420            event.preventDefault();
421            jQuery("html, body").animate({ scrollTop: 0 }, "slow");
422            return false;
423        });
424
425    });
426
427    // Mobile responsive script
428    function openNav() {         
429        jQuery(".off-canvas-wrapper").addClass("is-open");  
430        jQuery(".off-canvas-wrapper").css("width", "100%");
431    }
432    function closeNav() {
433       jQuery(".off-canvas-wrapper").removeClass("is-open");
434       jQuery(".off-canvas-wrapper").css("width", "0%");
435    }
436
437    // Make functions globally available
438    window.openNav = openNav;
439    window.closeNav = closeNav;
440
441    
442
443})(jQuery);
444

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.