PageSourceSearch

https://www.velaw.com/wp-content/themes/vinson-and-elkins/resource…/wpgb-custom/wpgb-custom.js?id=1790209500

js velaw.com collected 2026-09-24 17:18:56 UTC 21,362 bytes, 636 lines download raw bytes

1(function ($) {
2
3    window.wpgb = null;
4
5    let prefilterApplied = false;
6    let urlParamsApplied = false;
7
8    /**
9     * Extract value-to-label map from facet HTML
10     * WPGB stores facet HTML in facet.html property containing inputs with values and label spans
11     */
12    function extractLabelsFromFacetHtml(facetHtml) {
13        const labelMap = {};
14        if (!facetHtml) return labelMap;
15
16        // Create a temporary element to parse the HTML
17        const $temp = $('<div>').html(facetHtml);
18
19        // Handle radio/checkbox facets: wpgb-radio or wpgb-checkbox with hidden input and label span
20        $temp.find('.wpgb-radio, .wpgb-checkbox, .wpgb-button').each(function () {
21            const $input = $(this).find('input[type="hidden"]');
22            const $label = $(this).find('.wpgb-radio-label, .wpgb-checkbox-label, .wpgb-button-label');
23            if ($input.length && $label.length) {
24                const inputVal = $input.val();
25                const labelText = $label.text().trim();
26                if (inputVal && labelText) {
27                    labelMap[inputVal] = labelText;
28                }
29            }
30        });
31
32        // Handle select/dropdown facets
33        $temp.find('select option').each(function () {
34            const optVal = $(this).val();
35            const optText = $(this).text().trim();
36            if (optVal && optText) {
37                labelMap[optVal] = optText;
38            }
39        });
40
41        return labelMap;
42    }
43
44    /**
45     * Push active facets to GTM dataLayer
46     * Called on facet change, prefilter apply, and keyword updates
47     */
48    function pushActiveFacetsToDataLayer() {
49        if (!window.wpgb || !window.wpgb.facets || !window.wpgb.facets.params) {
50            return;
51        }
52
53        const activeFacets = {};
54        wpgb.facets.params.forEach(function (value, key) {
55            if (value && value.trim() !== '') {
56                // Remove leading underscore to get the actual facet slug
57                const facetSlug = key.startsWith('_') ? key.substring(1) : key;
58                const facetArray = wpgb.facets.getFacet(facetSlug);
59                let label = value; // Default to value if label not found
60
61                // Get the facet object (getFacet returns an array)
62                const facet = Array.isArray(facetArray) && facetArray.length > 0 ? facetArray[0] : null;
63
64                // Extract labels from facet HTML - this is where WPGB stores value→label mapping
65                if (facet && facet.html) {
66                    const labelMap = extractLabelsFromFacetHtml(facet.html);
67
68                    // Handle comma-separated values (multiple selections)
69                    if (value.includes(',')) {
70                        const values = value.split(',');
71                        const labels = values.map(function (v) {
72                            return labelMap[v.trim()] || v.trim();
73                        });
74                        label = labels.join(', ');
75                    } else if (labelMap[value]) {
76                        label = labelMap[value];
77                    }
78                }
79
80                activeFacets[key] = label;
81            }
82        });
83
84        if (Object.keys(activeFacets).length > 0) {
85            // Get page slug from URL path (e.g., /insights/ → insights, /people/ → people)
86            const pathParts = window.location.pathname.split('/').filter(function (part) {
87                return part !== '';
88            });
89            const pageSlug = pathParts.length > 0 ? pathParts[0] : 'page';
90            const eventName = pageSlug + '_filters';
91
92            const pushData = {
93                'event': eventName,
94                'filters': activeFacets
95            };
96
97            window.dataLayer = window.dataLayer || [];
98            window.dataLayer.push(pushData);
99        }
100    }
101
102    /**
103     * Parse URL parameters that start with underscore (_)
104     * These are WPGB facet parameters in format: _facet_slug=value
105     * Returns an object with facet slugs as keys and arrays of values
106     */
107    function getUrlFacetParams() {
108        const params = {};
109        const urlParams = new URLSearchParams(window.location.search);
110
111        urlParams.forEach(function (value, key) {
112            if (key.startsWith('_')) {
113                const facetSlug = key.substring(1); // Remove the leading underscore
114                if (facetSlug) {
115                    // Handle multiple values (comma-separated or multiple same-key params)
116                    if (params[facetSlug]) {
117                        params[facetSlug].push(value);
118                    } else {
119                        params[facetSlug] = [value];
120                    }
121                }
122            }
123        });
124
125        return params;
126    }
127
128    window.WP_Grid_Builder && WP_Grid_Builder.on('init', function (wpgb) {
129
130        window.wpgb = wpgb;
131
132        wpgb.facets.on('loaded', function (facets) {
133
134            /**
135             * Apply prefilter from URL parameters (fallback when WPGB history is disabled)
136             * Parses all URL params starting with _ and applies them as facet filters
137             * Only runs once on initial load
138             */
139            if (!urlParamsApplied) {
140                const urlFacetParams = getUrlFacetParams();
141                const hasUrlParams = Object.keys(urlFacetParams).length > 0;
142
143                if (hasUrlParams) {
144                    urlParamsApplied = true;
145
146                    // Apply each URL parameter as a facet filter
147                    Object.keys(urlFacetParams).forEach(function (facetSlug) {
148                        wpgb.facets.setParams(facetSlug, urlFacetParams[facetSlug]);
149
150                        // Update corresponding input fields if they exist
151                        if (facetSlug === 'search_keyword') {
152                            $('input[name="fake-search-keyword"]').val(urlFacetParams[facetSlug][0] || '');
153                        } else if (facetSlug === 'insight_keyword') {
154                            $('input[name="live_search_insights"]').val(urlFacetParams[facetSlug][0] || '');
155                        } else if (facetSlug === 'news_keyword') {
156                            $('input[name="live_search_news"]').val(urlFacetParams[facetSlug][0] || '');
157                        }
158                    });
159
160                    wpgb.facets.refresh();
161                    return; // Exit early, will re-enter on refresh
162                }
163            }
164
165            /**
166             * Apply prefilter from POST data (header search form)
167             * Uses mobile or desktop keyword based on viewport width
168             * Only runs once on initial load
169             */
170            if (!prefilterApplied && typeof wpgbPrefilter !== 'undefined') {
171                const isMobile = window.innerWidth <= 768;
172                const keyword = isMobile
173                    ? (wpgbPrefilter.mobile_search_keyword || wpgbPrefilter.search_keyword)
174                    : (wpgbPrefilter.search_keyword || wpgbPrefilter.mobile_search_keyword);
175                const contentType = wpgbPrefilter.search_content_type || '';
176
177                if (keyword || contentType) {
178                    prefilterApplied = true;
179
180                    if (keyword) {
181                        wpgb.facets.setParams('search_keyword', [keyword]);
182                        // Update the fake search input field with the keyword
183                        $('input[name="fake-search-keyword"]').val(keyword);
184                    }
185
186                    if (contentType) {
187                        wpgb.facets.setParams('search_content_type', [contentType]);
188                    }
189
190                    wpgb.facets.refresh();
191                    return; // Exit early, will re-enter on refresh
192                }
193            }
194
195            showHideActiveFilters();
196            articlesListRebuild();
197
198            if (isArchiveBios()) {
199                let
200                    filtersContainer = $('.filters-container-int'),
201                    moveAZFilter = $('.wpgb-facet-3 select.mobile-az-index'),
202                    existingAZFilter = $('.filters-container-int select.mobile-az-index')
203                ;
204
205                // Remove existing A-Z filter to avoid duplication
206                if (existingAZFilter.length > 0) {
207                    existingAZFilter.remove();
208                }
209
210                // Move A-Z filter to the top of filters container and assign change event
211                if (filtersContainer.length > 0 && moveAZFilter.length > 0) {
212                    filtersContainer.prepend(moveAZFilter);
213                    moveAZFilter.on('change', function () {
214                        const selectedValue = $(this).val();
215                        wpgb.facets.setParams('bio_letter', [selectedValue]);
216                        wpgb.facets.refresh();
217                        closeFilterCategories();
218                    });
219                }
220
221                showHideBioResults();
222            }
223
224        });
225
226        wpgb.facets.on('refresh', function () {
227
228            pushActiveFacetsToDataLayer();
229
230            /**
231             * VE Archive - Update Headings based on Topic Filter
232             */
233            updateVEArchiveHeadings();
234
235            /**
236             * Main Search - Top Navigation - Update Search Keyword Input
237             */
238            let
239                $globalSearchInput = $('input[name="_search_keyword"]'),
240                $searchKeyword = wpgb.facets.getParam('search_keyword')
241            ;
242
243            if ($globalSearchInput.length) {
244                $globalSearchInput.each(function () {
245                  //  $(this).val($searchKeyword);
246                });
247            }
248
249            // console.log('refresh: $searchKeyword', $searchKeyword, 'top search input length:', $globalSearchInput.length);
250
251            /**
252             * Global Search - Update Search Keyword Input
253             */
254            if (isGlobalSearchArchive()) {
255
256                const
257                    filterParams = wpgb.facets.params.toString(),
258                    $fakeSearchInput = $('input[name="fake-search-keyword"]'),
259                    $fakeSearchInputInsights = $('input[name="live_search_insights"]'),
260                    $fakeSearchInputNews = $('input[name="live_search_news"]')
261                ;
262
263                // console.log('wpgb.facets.params.toString():', filterParams);
264
265                if ($fakeSearchInput.length) {
266
267                    $fakeSearchInput.val(filterParams !== '' ? (wpgb.facets.getParam('search_keyword') || '') : '');
268
269                    // console.log('refresh: $fakeSearchInput.val()', $fakeSearchInput.val(), 'facets param:', wpgb.facets.getParam('search_keyword'));
270
271                } else if ($fakeSearchInputInsights.length) {
272
273                    $fakeSearchInputInsights.val(filterParams !== '' ? (wpgb.facets.getParam('insight_keyword') || '') : '');
274
275                } else if ($fakeSearchInputNews.length) {
276
277                    $fakeSearchInputNews.val(filterParams !== '' ? (wpgb.facets.getParam('news_keyword') || '') : '');
278                }
279            }
280        });
281
282        wpgb.facets.on('change', function (slug, values) {
283
284            // console.log('Facet changed:', 'slug: ', slug, 'values: ', values);
285
286            /**
287             *  If on Global Search Archive page, scroll to results bar after facet change
288             */
289            if (isGlobalSearchArchive()) {
290                niceScrollToElement($('.search-results-bar'));
291            }
292
293            if (isArchiveBios()) {
294                closeFilterCategories();
295                showHideBioResults();
296            }
297
298        });
299
300
301    });
302    /** END WP_Grid_Builder.on('init') */
303
304    $(document).ready(function () {
305
306        /**
307         * Archive Bios - Handle Filters Reveal Button
308         */
309        $('#filters-reveal-button').on('click', function (e) {
310            e.preventDefault();
311
312            const filtersContainer = $('.filters-container');
313
314            if (filtersContainer.length > 0) {
315                filtersContainer.slideToggle('slow');
316                $(this).toggleClass('open');
317            }
318
319            return false;
320        });
321
322        /**
323         * Archive Global Search - Handle fake search form - update WPGB facet on Enter key
324         */
325        $('input[name="fake-search-keyword"]').on('keyup', function (e) {
326            e.preventDefault();
327
328            const
329                searchValue = this.value,
330                relevanssiContainerId = $(this).attr('aria-owns')
331            ;
332
333            wpgb.facets.setParams('search_keyword', [searchValue]);
334
335            clearTimeout($.data(this, 'timer'));
336
337            const wait = setTimeout(function () {
338                wpgb.facets.refresh();
339            }, 500);
340
341            $(this).data('timer', wait);
342
343            if (relevanssiContainerId) {
344                $('#' + relevanssiContainerId).fadeIn("slow");
345            }
346
347            // on enter key press, scroll to results bar
348            if (e.key === 'Enter' || e.keyCode === 13) {
349                niceScrollToElement($('.search-results-bar'));
350
351                if (relevanssiContainerId) {
352                    $('#' + relevanssiContainerId).fadeOut("slow");
353                }
354
355                $('input:focus').blur();
356            }
357
358            return false;
359        });
360
361        /**
362         * Archive Global Search - Handle fake search form submit button - update WPGB facet on click
363         */
364        $(document).on('click', '.wpgb-facet-22 input[name="search_keyword"]', function (e) {
365            e.preventDefault();
366            e.stopPropagation();
367
368            const
369                searchInput = $('input[name="fake-search-keyword"]'),
370                searchValue = searchInput.val(),
371                relevanssiContainerId = searchInput.attr('aria-owns')
372            ;
373
374            if (window.wpgb && window.wpgb.facets) {
375                wpgb.facets.setParams('search_keyword', [searchValue]);
376                wpgb.facets.refresh();
377            }
378
379            if (relevanssiContainerId) {
380                $('#' + relevanssiContainerId).fadeOut("slow");
381            }
382
383            niceScrollToElement($('.search-results-bar'));
384
385            $('input:focus').blur();
386
387            return false;
388        });
389
390        /**
391         *
392         * Archive Insights - Handle AJAX Live Search input - update WPGB facet on keyup with delay
393         *
394         */
395        $('input[name="live_search_insights"]').on('keyup', function (e) {
396            e.preventDefault();
397
398            const
399                searchValue = this.value,
400                relevanssiContainerId = $(this).attr('aria-owns')
401            ;
402
403            wpgb.facets.setParams('insight_keyword', [searchValue]);
404
405            clearTimeout($.data(this, 'timer'));
406
407            const wait = setTimeout(function () {
408                wpgb.facets.refresh();
409            }, 500);
410
411            $(this).data('timer', wait);
412
413            if (relevanssiContainerId) {
414                $('#' + relevanssiContainerId).fadeIn("slow");
415            }
416
417            // on enter key press, scroll to results bar
418            if (e.key === 'Enter' || e.keyCode === 13) {
419                niceScrollToElement($('.search-results-bar'));
420
421                if (relevanssiContainerId) {
422                    $('#' + relevanssiContainerId).fadeOut("slow");
423                }
424            }
425
426            return false;
427        });
428        /**
429         * Archive Insights - Handle AJAX Live Search submit button - update WPGB facet on click
430         */
431        $(document).on('click', '.wpgb-facet-19 input[name="insight_keyword"]', function (e) {
432            e.preventDefault();
433            e.stopPropagation();
434
435            const
436                searchInput = $('input[name="live_search_insights"]'),
437                searchValue = searchInput.val(),
438                relevanssiContainerId = searchInput.attr('aria-owns')
439            ;
440
441            if (window.wpgb && window.wpgb.facets) {
442                wpgb.facets.setParams('insight_keyword', [searchValue]);
443                wpgb.facets.refresh();
444            }
445
446            if (relevanssiContainerId) {
447                $('#' + relevanssiContainerId).fadeOut("slow");
448            }
449
450            niceScrollToElement($('.search-results-bar'));
451
452            return false;
453        });
454
455
456        /**
457         *
458         * Archive News - Handle AJAX Live Search input - update WPGB facet on keyup with delay
459         *
460         */
461        $('input[name="live_search_news"]').on('keyup', function (e) {
462            e.preventDefault();
463
464            const
465                searchValue = this.value,
466                relevanssiContainerId = $(this).attr('aria-owns')
467            ;
468
469            wpgb.facets.setParams('news_keyword', [searchValue]);
470
471            clearTimeout($.data(this, 'timer'));
472
473            const wait = setTimeout(function () {
474                wpgb.facets.refresh();
475            }, 500);
476
477            $(this).data('timer', wait);
478
479            if (relevanssiContainerId) {
480                $('#' + relevanssiContainerId).fadeIn("slow");
481            }
482
483            if (e.key === 'Enter' || e.keyCode === 13) {
484                // niceScrollToElement($('.search-results-bar'));
485
486                if (relevanssiContainerId) {
487                    $('#' + relevanssiContainerId).fadeOut("slow");
488                }
489            }
490
491            return false;
492        });
493        /**
494         * Archive News - Handle AJAX Live Search submit button - update WPGB facet on click
495         */
496        $(document).on('click', '.wpgb-facet-32 input[name="news_keyword"]', function (e) {
497            e.preventDefault();
498            e.stopPropagation();
499
500            const
501                searchInput = $('input[name="live_search_news"]'),
502                searchValue = searchInput.val(),
503                relevanssiContainerId = searchInput.attr('aria-owns')
504            ;
505
506            if (window.wpgb && window.wpgb.facets) {
507                wpgb.facets.setParams('news_keyword', [searchValue]);
508                wpgb.facets.refresh();
509            }
510
511            if (relevanssiContainerId) {
512                $('#' + relevanssiContainerId).fadeOut("slow");
513            }
514
515            // niceScrollToElement($('.search-results-bar'));
516
517            return false;
518        });
519
520
521    });
522
523    function isArchiveBios() {
524        return $('.archive-bio').length > 0;
525    }
526
527    function showHideActiveFilters() {
528
529        if ($('.active-filters fieldset').length > 0) {
530            $('.active-filters').slideDown(150);
531        } else {
532            $('.active-filters').slideUp(150);
533        }
534    }
535
536    function articlesListRebuild() {
537        setTimeout(function () {
538            $(document).trigger('articlesList:rebuild');
539        }, 500);
540    }
541
542    /**
543     * Global Search Archive Check
544     * @returns {boolean}
545     */
546    function isGlobalSearchArchive() {
547        return $('.fake-global-search-form').length > 0;
548    }
549
550    function niceScrollToElement(element) {
551        let
552            siteHeaderHeight = $('.site-header').outerHeight() || 0,
553            adminBarHeight = $('#wpadminbar').outerHeight() || 0
554        ;
555
556        $('html, body').animate({
557            scrollTop: element.offset().top - (siteHeaderHeight - adminBarHeight) + 90
558        }, 800);
559    }
560
561    /**
562     * People Archive - Close Filter Categories if Open
563     */
564    function closeFilterCategories() {
565        let
566            filterCategoriesButton = $('#filters-reveal-button'),
567            filtersContainer = $('.filters-container')
568        ;
569
570        if (filterCategoriesButton.length > 0 && filtersContainer.length && filtersContainer.is(':visible')) {
571            filterCategoriesButton.trigger('click');
572        }
573    }
574
575    /**
576     * People Archive - Show/Hide Results Container based on Filters
577     */
578    function showHideBioResults() {
579        let
580            resultsContainer = $('.bio-results-with-pagination'),
581            filterParams = wpgb.facets.params.toString()
582        ;
583
584        if (isArchiveBios() && resultsContainer.length > 0) {
585
586            if (window.wpgb && window.wpgb.facets) {
587                let
588                    filterParams = wpgb.facets.params.toString()
589                ;
590
591                if (filterParams !== '') {
592
593                    if (resultsContainer.is(':hidden'))
594                        resultsContainer.slideDown('slow');
595
596                } else {
597
598                    if (resultsContainer.is(':visible'))
599                        resultsContainer.slideUp('slow');
600                }
601            }
602
603        }
604    }
605
606    function isVEArchive() {
607        return $('.archive-ve').length > 0;
608    }
609
610    function updateVEArchiveHeadings() {
611
612        if (isVEArchive()) {
613            let
614                currentTopicValue = wpgb.facets.getParam('ve_topic') || 'all',
615                archiveContainer = $('.archive-ve')
616            ;
617
618            archiveContainer.find('.wp-block-columns.is-style-heading').hide();
619
620            if (currentTopicValue === 'all') {
621
622            } else {
623
624                let
625                    topicHeadingElement = archiveContainer.find('#heading-' + currentTopicValue[0])
626                ;
627
628                if (topicHeadingElement) {
629                    topicHeadingElement.fadeIn("slow");
630                }
631            }
632        }
633    }
634
635
636})(jQuery);

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.