PageSourceSearch

https://nbcf.org.au/wp-content/themes/nbcf/functions/components/archive/assets/js/archive.js?ver=7.0.5

js nbcf.org.au collected 2026-10-02 03:58:48 UTC 12,445 bytes, 337 lines download raw bytes

1if (typeof Archive === 'undefined') {
2
3    var Archive = {
4
5        /**
6         * Initialise the archive template functionality
7         *
8         */
9        init: function () {
10
11            if (!jQuery('[data-archive="form"]').length) {
12
13                return;
14            }
15
16            Archive.initForm();
17            Archive.initPagination();
18            Archive.initWindowPopState();
19            Archive.initHistory();
20        },
21
22        /**
23         * Initialise the form functionality
24         *
25         */
26        initForm: function () {
27
28            /**
29             * Form submission
30             *
31             */
32            jQuery('[data-archive="form"]').on('submit', Archive.handleSubmit);
33
34            /**
35             * Filter input changes
36             *
37             */
38            jQuery('[data-archive="form"] input, [data-archive="form"] select').on('change', Archive.handleInputChange);
39        },
40
41        handleInputChange: function() {
42            // let selected_category = jQuery('[name="category"] option:selected').text();
43            // jQuery('[data-archive="active-filters"] strong').text(selected_category);
44            jQuery('[data-archive="form"]').submit();
45        },
46
47        handleSubmit: function(event) {
48            event.preventDefault();
49
50            // Get query arguments
51            let query_args = jQuery('[data-archive="form"]').data('archive_query_args');
52
53            // Get filters
54            // some pages have conflicting mobile and desktop inputs, so there
55            // may be an empty param which overrides the one with a value - 
56            // replace manually.
57            let filters = jQuery(this).serialize().replace(/category=&/, '');
58
59            Archive.getHTMLUpdates(query_args, filters);
60
61            return false;
62        },
63
64        /**
65         * Handle history.back() when the current page was pushed manually (from history.pushState)
66         */
67        initWindowPopState: function() {
68            window.onpopstate = function(event) {
69                if (event.state && event.state.filters) {
70                    var jQueryinputs = jQuery('[data-archive="form"] input, [data-archive="form"] select');
71
72                    jQueryinputs.off('change', Archive.handleInputChange);
73
74                    var filters = event.state.filters;
75                    var filterKeys = Object.keys(filters);
76
77                    for (var i = 0; i < filterKeys.length; i++) {
78                        var key = filterKeys[i];
79                        var value = filters[filterKeys[i]];
80                        if (Object.prototype.toString.call(value) === '[object Array]') {
81                            value = value.join()
82                        }
83                        var jQueryfield =jQuery('[data-archive="form"] [name="' + key + '"]');
84
85                        var jQuerytype = jQueryfield.prop('type');
86                        if (jQuerytype === 'radio') {
87                            jQueryfield.each(function () {
88                                jQuery(this).prop('checked', jQuery(this).val() === value);
89                            });
90                        } else if (jQuerytype === 'select' || jQuerytype === 'select-one') {
91                            var jQueryselect = jQuery('select[name="' + key + '"]');
92                            jQueryselect.find('option').each(function () {
93                                jQuery(this).prop('selected', jQuery(this).val() === value);
94                            });
95                            jQueryselect.val(value).trigger('render');
96                        } else {
97                            jQueryfield.val(value);
98                        }
99
100                    }
101                    var jQueryform = jQuery('[data-archive="form"]');
102                    // Get query arguments
103                    var query_args = jQueryform.data('archive_query_args');
104
105                    if (event.state.data) {
106                        var data = event.state.data;
107                        jQuery('[data-archive="items"]').html(data.items_html);
108
109                        // Add pagination to the page
110                        jQuery('[data-archive="pagination"]').html(data.pagination_html);
111
112                        // Update item counts
113                        jQuery('[data-archive="page-count"]').html(data.page_count);
114                        jQuery('[data-archive="total-count"]').html(data.total_count);
115                    } else {
116                        Archive.getHTMLUpdates(query_args, jQueryform.serialize(), true);
117                    }
118                    jQueryinputs.on('change', Archive.handleInputChange);
119                } else {
120                    Archive.resetForm();
121                }
122            };
123        },
124
125        resetForm: function() {
126            var jQueryinputs = jQuery('[data-archive="form"] input, [data-archive="form"] select');
127            jQueryinputs.off('change', Archive.handleInputChange);
128            jQuery('[data-archive="form"]').find('[name]').each(function() {
129                var jQuerytype = jQuery(this).prop('type');
130                if (jQuerytype === 'radio') {
131                    jQuery(this).each(function () {
132                        jQuery(this).prop('checked', jQuery(this).val() === '');
133                    });
134                } else if (jQuerytype === 'select' || jQuerytype === 'select-one') {
135                    jQuery(this).find('option').each(function () {
136                        jQuery(this).prop('selected', jQuery(this).val() === '');
137                    });
138                    jQuery(this).val('').trigger('render');
139                } else {
140                    jQuery(this).val('');
141                }
142            });
143            var jQueryform = jQuery('[data-archive="form"]');
144            // Get query arguments
145            var query_args = jQueryform.data('archive_query_args');
146
147            Archive.getHTMLUpdates(query_args, jQueryform.serialize(), true);
148            jQueryinputs.on('change', Archive.handleInputChange);
149        },
150
151        /**
152         * Get the HTML updates for the archive items
153         *
154         * @param query_args
155         * @param filters
156         * @param ignore_history
157         */
158        getHTMLUpdates: function (query_args, filters, ignore_history) {
159
160            jQuery('[data-archive="items"]').addClass('loading');
161
162            let post_type = jQuery('[data-archive="form"]').attr('data-archive_post_type');
163
164            let data = {
165                action: 'get-' + post_type + '-items',
166                query_args: query_args,
167                filters: filters
168            };
169
170            jQuery.ajax({
171                url: js_vars.ajax_url,
172                data: data,
173                method: 'post',
174                dataType: 'json'
175            }).done(function (data, status, jqXHR) {
176
177                jQuery('[data-archive="items"]').removeClass('loading');
178
179                if (data.status == 'SUCCESS') {
180
181                    if (!ignore_history) {
182                        // Update URL to match current item listings state
183                        window.history.pushState({
184                            filters: data.filters,
185                            data,
186                        }, '', data.url);
187                    }
188
189                    // Add items to the page
190                    jQuery('[data-archive="items"]').html(data.items_html);
191
192                    if (data.total_count > 0) {
193                        jQuery('[data-archive="no-results"').hide();
194                    } else {
195                        jQuery('[data-archive="no-results"').show();
196                    }
197
198                    // Add pagination to the page
199                    jQuery('[data-archive="pagination"]').html(data.pagination_html);
200
201                    // Update item counts
202                    jQuery('[data-archive="page-count"]').html(data.page_count);
203                    jQuery('[data-archive="total-count"]').html(data.total_count);
204
205                    Archive.initPagination();
206
207                    jQuery("html, body").animate({ scrollTop:
208                            jQuery('#skip-to-form').offset().top - jQuery(".header-main-spacer").outerHeight()
209                        }, 1000, function(){
210                            console.log('sending focus');
211                            jQuery('#scroll-to-form').focus()
212                        }
213                    );
214                }
215                else {
216
217
218                }
219            });
220        },
221
222        /**
223         * Initialise the archive page pagination
224         *
225         */
226        initPagination: function () {
227
228            /**
229             * Click event trigger for pagination links
230             *
231             */
232            jQuery('[data-archive="pagination"] a').on('click', function (event) {
233
234                event.preventDefault();
235
236                // Get query arguments
237                let query_args = jQuery('[data-archive="form"]').data('archive_query_args');
238
239                if (typeof query_args !== 'object') {
240
241                    let query_args = jQuery.parseJSON(query_args);
242                }
243
244                // Get filter data
245                let filters = jQuery(this).data('archive_filters');
246
247                if (typeof filters !== 'object') {
248
249                    filters = jQuery.parseJSON(filters);
250                }
251
252                Archive.getHTMLUpdates(query_args, filters);
253
254                return false;
255            });
256        },
257
258        /**
259         * Initialise history object with state so that history.back() loads
260         * items from state object, rather than submitting ajax request
261         */
262        initHistory: function() {
263            var filter_string = jQuery('[data-archive="form"]').attr('data-archive_filter');
264            var filters = JSON.parse(filter_string);
265
266            var data = {};
267
268            if (jQuery('[data-archive="items"]').length) {
269                data.items_html = jQuery('[data-archive="items"]').prop('innerHTML');
270            }
271
272            if (jQuery('[data-archive="pagination"]').length) {
273                data.pagination_html = jQuery('[data-archive="pagination"]').prop('innerHTML');
274            }
275
276            if (jQuery('[data-archive="page_count"]').length) {
277                data.page_count = jQuery('[data-archive="page_count"]').prop('innerHTML');
278            }
279
280            if (jQuery('[data-archive="total_count"]').length) {
281                data.total_count = jQuery('[data-archive="total_count"]').prop('innerHTML');
282            }
283
284            var state = {
285                filters: filters,
286                data: data,
287            };
288            window.history.replaceState(state, '', window.location.href);
289        },
290    };
291
292    jQuery(function () {
293        // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
294        if (!Object.keys) {
295            Object.keys = (function() {
296                'use strict';
297                var hasOwnProperty = Object.prototype.hasOwnProperty,
298                    hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
299                    dontEnums = [
300                        'toString',
301                        'toLocaleString',
302                        'valueOf',
303                        'hasOwnProperty',
304                        'isPrototypeOf',
305                        'propertyIsEnumerable',
306                        'constructor'
307                    ],
308                    dontEnumsLength = dontEnums.length;
309
310                return function(obj) {
311                    if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
312                        throw new TypeError('Object.keys called on non-object');
313                    }
314
315                    var result = [], prop, i;
316
317                    for (prop in obj) {
318                        if (hasOwnProperty.call(obj, prop)) {
319                            result.push(prop);
320                        }
321                    }
322
323                    if (hasDontEnumBug) {
324                        for (i = 0; i < dontEnumsLength; i++) {
325                            if (hasOwnProperty.call(obj, dontEnums[i])) {
326                                result.push(dontEnums[i]);
327                            }
328                        }
329                    }
330                    return result;
331                };
332            }());
333        }
334        Archive.init();
335    });
336}
337

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.