PluginProbe
WP Directory Kit / 1.3.3
WP Directory Kit v1.3.3
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / public / js / wdk-select2.js

wdk-select2.js in WP Directory Kit 1.3.3, at public/js/wdk-select2.js

196 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function($){
2 wdk_select_init();
3 /* elementor popup */
4 jQuery( document ).on( 'elementor/popup/show', () => {
5 $('.select2-hidden-accessible', '.elementor-popup-modal').each(function () {
6 $(this).removeClass('select2-hidden-accessible').next('.select2').remove();
7 });
8
9 wdk_select_init('.elementor-popup-modal');
10 } );
11 });
12
13
14 const wdk_select_init = ($wrapper = 'body') => {
15 var $ = jQuery;
16
17 /* Start select2
18 * Site https://select2.org/
19 */
20 if (typeof $.fn.select2 == 'function') {
21 //Set Dropdown with SearchBox via dropdownAdapter option (https://stackoverflow.com/questions/35799854/how-to-add-selectall-using-dropdownadapter-on-select2-v4)
22 var Utils = $.fn.select2.amd.require('select2/utils');
23 var Dropdown = $.fn.select2.amd.require('select2/dropdown');
24 var DropdownSearch = $.fn.select2.amd.require('select2/dropdown/search');
25 var CloseOnSelect = $.fn.select2.amd.require('select2/dropdown/closeOnSelect');
26 var AttachBody = $.fn.select2.amd.require('select2/dropdown/attachBody');
27
28 var dropdownAdapter = Utils.Decorate(Utils.Decorate(Utils.Decorate(Dropdown, DropdownSearch), CloseOnSelect), AttachBody);
29
30 $('.select_ajax', $wrapper).each(function () {
31 var self = $(this);
32 let ajax_url = $(this).attr('data-ajax');
33 var data = {
34 "action": 'wdk_public_action',
35 "page": 'wdk_frontendajax',
36 "function": 'select_2_ajax',
37 };
38
39 if(self.hasClass('select2-hidden-accessible')) return true;
40
41 var $select = $(this).select2({
42 language: {
43 errorLoading: function () {
44 return wdk_select2_script_parameters.text.errorLoading;
45 },
46 loadingMore: function () {
47 return wdk_select2_script_parameters.text.loadingMore;
48 },
49 noResults: function () {
50 return wdk_select2_script_parameters.text.noResults;
51 },
52 searching: function () {
53 return wdk_select2_script_parameters.text.searching;
54 },
55 removeAllItems: function () {
56 return wdk_select2_script_parameters.text.removeAllItems;
57 }
58 },
59 placeholder: $(this).attr('data-placeholder') || '',
60 maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10,
61 dropdownAdapter: dropdownAdapter,
62 minimumResultsForSearch: 1,
63 ajax: {
64 url: ajax_url,
65 dataType: 'json',
66 data: data,
67 type: "POST",
68 quietMillis: 10,
69
70 data: function (term, page) { // page is the one-based page number tracked by Select2
71 return {
72 q: term, //search term
73 "page_result": term.page || 1,
74 "action": 'wdk_public_action',
75 "page": 'wdk_frontendajax',
76 "function": 'select_2_ajax',
77 "table": $(this).attr('data-table') || '',
78 };
79 },
80 results: function (data, page) {
81 var more = (page * 30) < data.total_count; // whether or not there are more results available
82
83 // notice we return the value of more so Select2 knows if more results can be loaded
84 return { results: data.items, more: more };
85 }
86 },
87 templateResult: wdk_select_ajax_formatRepo,
88 templateSelection: wdk_select_ajax_templateSelection
89 });
90 $select.data('select2').$dropdown.addClass('select_multi_dropdown_tree');
91 }).on('select2:open', function (e) {
92 // Do something
93 $('.select_ajax').find('option[value=""]').remove();
94 });
95
96
97 $('.select_ajax_user', $wrapper).each(function () {
98 var self = $(this);
99 let ajax_url = $(this).attr('data-ajax');
100 var data = {
101 "action": 'wdk_public_action',
102 "page": 'wdk_frontendajax',
103 "function": 'select_2_ajax_user',
104 };
105
106 if(self.hasClass('select2-hidden-accessible')) return true;
107
108 var $select = $(this).select2({
109 multiple: true,
110 placeholder: $(this).attr('data-placeholder') || '',
111 maximumSelectionLength: 10,
112 dropdownAdapter: dropdownAdapter,
113 minimumResultsForSearch: 1,
114 ajax: {
115 url: ajax_url,
116 dataType: 'json',
117 data: data,
118 type: "POST",
119 quietMillis: 10,
120
121 data: function (term, page) { // page is the one-based page number tracked by Select2
122 return {
123 q: term, //search term
124 "page_result": term.page || 1,
125 "action": 'wdk_public_action',
126 "page": 'wdk_frontendajax',
127 "function": 'select_2_ajax_user',
128 };
129 },
130 results: function (data, page) {
131 var more = (page * 30) < data.total_count; // whether or not there are more results available
132
133 // notice we return the value of more so Select2 knows if more results can be loaded
134 return { results: data.items, more: more };
135 }
136 },
137 templateResult: wdk_select_ajax_formatRepo,
138 templateSelection: wdk_select_ajax_templateSelection
139 }).on('select2:opening select2:closing', function (event) {
140 //Disable original search (https://select2.org/searching#multi-select)
141 var searchfield = $(this).parent().find('.select2-search__field');
142 searchfield.prop('disabled', true);
143 });
144
145 $select.data('select2').$dropdown.addClass('select_multi_dropdown_users');
146 });
147
148 $('.select_multi', $wrapper).each(function () {
149 var self = $(this);
150 if(self.hasClass('select2-hidden-accessible')) return true;
151
152 var $select = $(this).select2({
153 closeOnSelect:false,
154 placeholder: $(this).attr('data-placeholder') || '',
155 maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10,
156 dropdownAdapter: dropdownAdapter,
157 minimumResultsForSearch: 1,
158 maximumSelectionLength: +($(this).attr('data-maxselectlimit')) || 10,
159 templateResult: wdk_select_ajax_formatRepo,
160 templateSelection: wdk_select_ajax_templateSelection
161 });
162
163 $select.data('select2').$dropdown.addClass('select_multi_dropdown');
164 });
165
166 }
167
168 function wdk_select_ajax_formatRepo (repo) {
169
170 if (repo.loading) {
171 return repo.text;
172 }
173
174 var $container = $(
175 "<span>"+repo.text+"</span>"
176 );
177
178 return $container;
179
180 }
181 function wdk_select_ajax_templateSelection (repo) {
182
183 if (repo.loading) {
184 return repo.text;
185 }
186
187 var $container = $(
188 "<span>"+repo.text+"</span>"
189 );
190
191 return $container;
192
193 }
194 }
195
196