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 transport: function (params, success, failure) { 35 // is caching enabled? 36 if ($s2.data('ajax--cache')) { 37 // try to make the key unique to make it less likely for a page+q to match a real query 38 var key = prefix + ' page:' + (params.data.page || 1) + ' ' + params.data.q, 39 cacheTimeout = $s2.data('ajax--cacheTimeout'); 40 // no cache entry for 'term' or the cache has timed out? 41 if (typeof cache[key] == 'undefined' || (cacheTimeout && Date.now() >= cache[key].time)) { 42 $.ajax(params).fail(failure).done(function (data) { 43 cache[key] = { 44 data: data, 45 time: cacheTimeout ? Date.now() + cacheTimeout : null 46 }; 47 success(data); 48 }); 49 } else { 50 // return cached data with no ajax request 51 success(cache[key].data); 52 } 53 } else { 54 // no caching enabled. just do the ajax request 55 if (request) { 56 request.abort(); 57 } 58 request = $.ajax(params).fail(failure).done(success).always(function () { 59 request = undefined; 60 }); 61 } 62 }, 63 data: function (params) { 64 var ret = { 65 'q': params.term, 66 'field_name': $s2.data('name') 67 }; 68 69 var reqParams = $s2.data('req_params'); 70 if (reqParams) { 71 $.each(reqParams, function (key, value) { 72 ret[key] = $('*[name="'+value+'"]').val() 73 }); 74 } 75 76 // only send the 'page' parameter if scrolling is enabled 77 if (scroll) { 78 ret['page'] = params.page || 1; 79 } 80 81 return ret; 82 }, 83 processResults: function (data, params) { 84 var results, more = false, response = {}; 85 params.page = params.page || 1; 86 87 if ($.isArray(data)) { 88 results = data; 89 } else if (typeof data == 'object') { 90 // assume remote result was proper object 91 results = data.results; 92 more = data.more; 93 } else { 94 // failsafe 95 results = []; 96 } 97 98 if (scroll) { 99 response.pagination = {more: more}; 100 } 101 response.results = results; 102 103 return response; 104 } 105 } 106 }, options || {})); 107 }); 108 return this; 109 }; 110})( jQuery ); 111 112(function( $ ) { 113 $(document).ready(function () { 114 $('.select2entity[data-autostart="true"]').select2entity(); 115 }); 116})( 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.