PageSourceSearch

https://www.icastelli.it/bundles/tetranzselect2entity/js/select2entity.js

js icastelli.it collected 2026-10-02 12:24:34 UTC 5,211 bytes, 120 lines download raw bytes

1(function( $ ) {
2    $.fn.select2entity = function (options) {
3        this.each(function () {
4            var request;
5
6            // Keep a reference to the element so we can keep the cache local to this instance and so we can
7            // fetch config settings since select2 doesn't expose its options to the transport method.
8            var $s2 = $(this),
9                limit = $s2.data('page-limit') || 0,
10                scroll = $s2.data('scroll'),
11                prefix = Date.now(),
12                cache = [];
13
14            var reqParams = $s2.data('req_params');
15            if (reqParams) {
16                $.each(reqParams, function (key, value) {
17                    $('*[name="'+value+'"]').on('change', function () {
18                        $s2.val(null);
19                        $s2.trigger('change');
20                    });
21                });
22            }
23
24            // Deep-merge the options
25            $s2.select2($.extend(true, {
26                // Tags support
27                createTag: function (data) {
28                    if ($s2.data('tags') && data.term.length > 0) {
29                        var text = data.term + $s2.data('tags-text');
30                        return {id: $s2.data('new-tag-prefix') + data.term, text: text};
31                    }
32                },
33                ajax: {
34                    url: $s2.data('ajax--url') || null,
35                    transport: function (params, success, failure) {
36                        // is caching enabled?
37                        if ($s2.data('ajax--cache')) {
38                            // try to make the key unique to make it less likely for a page+q to match a real query
39                            var key = prefix + ' page:' + (params.data.page || 1) + ' ' + params.data.q,
40                                cacheTimeout = $s2.data('ajax--cacheTimeout');
41                            // no cache entry for 'term' or the cache has timed out?
42                            if (typeof cache[key] == 'undefined' || (cacheTimeout && Date.now() >= cache[key].time)) {
43                                return $.ajax(params).fail(failure).done(function (data) {
44                                    cache[key] = {
45                                        data: data,
46                                        time: cacheTimeout ? Date.now() + cacheTimeout : null
47                                    };
48                                    success(data);
49                                });
50                            } else {
51                                // return cached data with no ajax request
52                                success(cache[key].data);
53                            }
54                        } else {
55                            // no caching enabled. just do the ajax request
56                            if (request) {
57                                request.abort();
58                            }
59                            request = $.ajax(params).fail(failure).done(success).always(function () {
60                                request = undefined;
61                            });
62                            
63                            return request;
64                        }
65                    },
66                    data: function (params) {
67                        var ret = {
68                            'q': params.term,
69                            'field_name': $s2.data('name'),
70                            'class_type': $s2.data('classtype')
71                        };
72
73                        var reqParams = $s2.data('req_params');
74                        if (reqParams) {
75                            $.each(reqParams, function (key, value) {
76                                ret[key] = $('*[name="'+value+'"]').val()
77                            });
78                        }
79
80                        // only send the 'page' parameter if scrolling is enabled
81                        if (scroll) {
82                            ret['page'] = params.page || 1;
83                        }
84
85                        return ret;
86                    },
87                    processResults: function (data, params) {
88                        var results, more = false, response = {};
89                        params.page = params.page || 1;
90
91                        if ($.isArray(data)) {
92                            results = data;
93                        } else if (typeof data == 'object') {
94                            // assume remote result was proper object
95                            results = data.results;
96                            more = data.more;
97                        } else {
98                            // failsafe
99                            results = [];
100                        }
101
102                        if (scroll) {
103                            response.pagination = {more: more};
104                        }
105                        response.results = results;
106
107                        return response;
108                    }
109                }
110            }, options || {}));
111        });
112        return this;
113    };
114})( jQuery );
115
116(function( $ ) {
117    $(document).ready(function () {
118        $('.select2entity[data-autostart="true"]').select2entity();
119    });
120})( 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.