PageSourceSearch

https://ecard.uzleti.hu/bundles/tetranzselect2entity/js/select2entity.js

js uzleti.hu collected 2026-10-08 07:10:44 UTC 6,380 bytes, 148 lines download raw bytes

1(function ($) {
2    $.fn.select2entity = function (options) {
3        this.each(function () {
4            let 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            let $s2 = $(this),
9                limit = $s2.data('page-limit') || 0,
10                scroll = $s2.data('scroll'),
11                prefix = Date.now(),
12                query_parameters = $s2.data('query-parameters'),
13                render_html = $s2.data('render-html'),
14                cache = [];
15
16            let reqParams = $s2.data('req_params');
17            if (reqParams) {
18                $.each(reqParams, function (key, value) {
19                    $('*[name="' + value + '"]').on('change', function () {
20                        $s2.val(null);
21                        $s2.trigger('change');
22                    });
23                });
24            }
25
26            // Deep-merge the options
27            let mergedOptions = $.extend(true, {
28                // Tags support
29                createTag: function (data) {
30                    if ($s2.data('tags') && data.term.length > 0) {
31                        let text = data.term + $s2.data('tags-text');
32                        return { id: $s2.data('new-tag-prefix') + data.term, text: text };
33                    }
34                },
35                ajax: {
36                    url: $s2.data('ajax--url') || null,
37                    transport: function (params, success, failure) {
38                        // is caching enabled?
39                        if ($s2.data('ajax--cache')) {
40                            // try to make the key unique to make it less likely for a page+q to match a real query
41                            var key = prefix + ' page:' + (params.data.page || 1) + ' ' + params.data.q,
42                                cacheTimeout = $s2.data('ajax--cacheTimeout');
43                            // no cache entry for 'term' or the cache has timed out?
44                            if (typeof cache[key] === 'undefined' || (cacheTimeout && Date.now() >= cache[key].time)) {
45                                return $.ajax(params).fail(failure).done(function (data) {
46                                    cache[key] = {
47                                        data: data,
48                                        time: cacheTimeout ? Date.now() + cacheTimeout : null
49                                    };
50                                    success(data);
51                                });
52                            } else {
53                                // return cached data with no ajax request
54                                success(cache[key].data);
55                            }
56                        } else {
57                            // no caching enabled. just do the ajax request
58                            if (request) {
59                                request.abort();
60                            }
61                            request = $.ajax(params).fail(failure).done(success).always(function () {
62                                request = undefined;
63                            });
64
65                            return request;
66                        }
67                    },
68                    data: function (params) {
69                        let ret = {
70                            'q': params.term,
71                            'field_name': $s2.data('name'),
72                            'class_type': $s2.data('classtype')
73                        };
74
75                        let reqParams = $s2.data('req_params');
76                        if (reqParams) {
77                            $.each(reqParams, function (key, value) {
78                                ret[key] = $('*[name="' + value + '"]').val();
79                            });
80                        }
81
82                        // only send the 'page' parameter if scrolling is enabled
83                        if (scroll) {
84                            ret['page'] = params.page || 1;
85                        }
86
87                        if (Array.isArray(query_parameters) ||
88                            typeof (query_parameters) === 'object') {
89                            for (var key in query_parameters) {
90                                // prevent overriding required parameters
91                                if (!ret[key]) {
92                                    ret[key] = query_parameters[key];
93                                }
94                            }
95                        }
96
97                        return ret;
98                    },
99                    processResults: function (data, params) {
100                        let results, more = false,
101                            response = {};
102                        params.page = params.page || 1;
103
104                        if ($.isArray(data)) {
105                            results = data;
106                        } else if (typeof data === 'object') {
107                            // assume remote result was proper object
108                            results = data.results;
109                            more = data.more;
110                        } else {
111                            // failsafe
112                            results = [];
113                        }
114
115                        if (scroll) {
116                            response.pagination = { more: more };
117                        }
118                        response.results = results;
119
120                        return response;
121                    }
122                }
123            }, options || {});
124            if (render_html) {
125                mergedOptions = $.extend({
126                    escapeMarkup: function (text) {
127                        return text;
128                    },
129                    templateResult: function (option) {
130                        return option.html ? option.html : option.text;
131                    },
132                    templateSelection: function (option) {
133                        return option.text;
134                    }
135                }, mergedOptions);
136            }
137
138            $s2.select2(mergedOptions);
139        });
140        return this;
141    };
142})(jQuery);
143
144(function ($) {
145    $(document).ready(function () {
146        $('.select2entity[data-autostart="true"]').select2entity();
147    });
148})(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.