PluginProbe
WP Directory Kit / 1.2.3
WP Directory Kit v1.2.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.2.3, at public/js/wdk-select2.js

178 lines 7.1 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 placeholder: $(this).attr('data-placeholder') || '',
43 maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10,
44 dropdownAdapter: dropdownAdapter,
45 minimumResultsForSearch: 1,
46 ajax: {
47 url: ajax_url,
48 dataType: 'json',
49 data: data,
50 type: "POST",
51 quietMillis: 10,
52
53 data: function (term, page) { // page is the one-based page number tracked by Select2
54 return {
55 q: term, //search term
56 "page_result": term.page || 1,
57 "action": 'wdk_public_action',
58 "page": 'wdk_frontendajax',
59 "function": 'select_2_ajax',
60 "table": $(this).attr('data-table') || '',
61 };
62 },
63 results: function (data, page) {
64 var more = (page * 30) < data.total_count; // whether or not there are more results available
65
66 // notice we return the value of more so Select2 knows if more results can be loaded
67 return { results: data.items, more: more };
68 }
69 },
70 templateResult: wdk_select_ajax_formatRepo,
71 templateSelection: wdk_select_ajax_templateSelection
72 });
73 $select.data('select2').$dropdown.addClass('select_multi_dropdown_tree');
74 }).on('select2:open', function (e) {
75 // Do something
76 $('.select_ajax').find('option[value=""]').remove();
77 });
78
79
80 $('.select_ajax_user', $wrapper).each(function () {
81 var self = $(this);
82 let ajax_url = $(this).attr('data-ajax');
83 var data = {
84 "action": 'wdk_public_action',
85 "page": 'wdk_frontendajax',
86 "function": 'select_2_ajax_user',
87 };
88
89 if(self.hasClass('select2-hidden-accessible')) return true;
90
91 var $select = $(this).select2({
92 multiple: true,
93 placeholder: $(this).attr('data-placeholder') || '',
94 maximumSelectionLength: 10,
95 dropdownAdapter: dropdownAdapter,
96 minimumResultsForSearch: 1,
97 ajax: {
98 url: ajax_url,
99 dataType: 'json',
100 data: data,
101 type: "POST",
102 quietMillis: 10,
103
104 data: function (term, page) { // page is the one-based page number tracked by Select2
105 return {
106 q: term, //search term
107 "page_result": term.page || 1,
108 "action": 'wdk_public_action',
109 "page": 'wdk_frontendajax',
110 "function": 'select_2_ajax_user',
111 };
112 },
113 results: function (data, page) {
114 var more = (page * 30) < data.total_count; // whether or not there are more results available
115
116 // notice we return the value of more so Select2 knows if more results can be loaded
117 return { results: data.items, more: more };
118 }
119 },
120 templateResult: wdk_select_ajax_formatRepo,
121 templateSelection: wdk_select_ajax_templateSelection
122 }).on('select2:opening select2:closing', function (event) {
123 //Disable original search (https://select2.org/searching#multi-select)
124 var searchfield = $(this).parent().find('.select2-search__field');
125 searchfield.prop('disabled', true);
126 });
127
128 $select.data('select2').$dropdown.addClass('select_multi_dropdown_users');
129 });
130
131 $('.select_multi', $wrapper).each(function () {
132 var self = $(this);
133 if(self.hasClass('select2-hidden-accessible')) return true;
134
135 var $select = $(this).select2({
136 closeOnSelect:false,
137 placeholder: $(this).attr('data-placeholder') || '',
138 maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10,
139 dropdownAdapter: dropdownAdapter,
140 minimumResultsForSearch: 1,
141 templateResult: wdk_select_ajax_formatRepo,
142 templateSelection: wdk_select_ajax_templateSelection
143 });
144
145 $select.data('select2').$dropdown.addClass('select_multi_dropdown');
146 });
147
148 }
149
150 function wdk_select_ajax_formatRepo (repo) {
151
152 if (repo.loading) {
153 return repo.text;
154 }
155
156 var $container = $(
157 "<span>"+repo.text+"</span>"
158 );
159
160 return $container;
161
162 }
163 function wdk_select_ajax_templateSelection (repo) {
164
165 if (repo.loading) {
166 return repo.text;
167 }
168
169 var $container = $(
170 "<span>"+repo.text+"</span>"
171 );
172
173 return $container;
174
175 }
176 }
177
178