| 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 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 38 |
|
| 39 |
}; |
| 40 |
|
| 41 |
if(self.hasClass('select2-hidden-accessible')) return true; |
| 42 |
|
| 43 |
var $select = $(this).select2({ |
| 44 |
language: { |
| 45 |
errorLoading: function () { |
| 46 |
return wdk_select2_script_parameters.text.errorLoading; |
| 47 |
}, |
| 48 |
loadingMore: function () { |
| 49 |
return wdk_select2_script_parameters.text.loadingMore; |
| 50 |
}, |
| 51 |
noResults: function () { |
| 52 |
return wdk_select2_script_parameters.text.noResults; |
| 53 |
}, |
| 54 |
searching: function () { |
| 55 |
return wdk_select2_script_parameters.text.searching; |
| 56 |
}, |
| 57 |
removeAllItems: function () { |
| 58 |
return wdk_select2_script_parameters.text.removeAllItems; |
| 59 |
} |
| 60 |
}, |
| 61 |
placeholder: $(this).attr('data-placeholder') || '', |
| 62 |
maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10, |
| 63 |
dropdownAdapter: dropdownAdapter, |
| 64 |
minimumResultsForSearch: 1, |
| 65 |
ajax: { |
| 66 |
url: ajax_url, |
| 67 |
dataType: 'json', |
| 68 |
data: data, |
| 69 |
type: "POST", |
| 70 |
quietMillis: 10, |
| 71 |
|
| 72 |
data: function (term, page) { // page is the one-based page number tracked by Select2 |
| 73 |
return { |
| 74 |
q: term, //search term |
| 75 |
"page_result": term.page || 1, |
| 76 |
"action": 'wdk_public_action', |
| 77 |
"page": 'wdk_frontendajax', |
| 78 |
"function": 'select_2_ajax', |
| 79 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 80 |
"table": $(this).attr('data-table') || '', |
| 81 |
}; |
| 82 |
}, |
| 83 |
results: function (data, page) { |
| 84 |
var more = (page * 30) < data.total_count; // whether or not there are more results available |
| 85 |
|
| 86 |
// notice we return the value of more so Select2 knows if more results can be loaded |
| 87 |
return { results: data.items, more: more }; |
| 88 |
} |
| 89 |
}, |
| 90 |
templateResult: wdk_select_ajax_formatRepo, |
| 91 |
templateSelection: wdk_select_ajax_templateSelection |
| 92 |
}); |
| 93 |
$select.data('select2').$dropdown.addClass('select_multi_dropdown_tree'); |
| 94 |
}).on('select2:open', function (e) { |
| 95 |
// Do something |
| 96 |
$('.select_ajax').find('option[value=""]').remove(); |
| 97 |
}); |
| 98 |
|
| 99 |
|
| 100 |
$('.select_ajax_user', $wrapper).each(function () { |
| 101 |
var self = $(this); |
| 102 |
let ajax_url = $(this).attr('data-ajax'); |
| 103 |
var data = { |
| 104 |
"action": 'wdk_public_action', |
| 105 |
"page": 'wdk_frontendajax', |
| 106 |
"function": 'select_2_ajax_user', |
| 107 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 108 |
}; |
| 109 |
|
| 110 |
if(self.hasClass('select2-hidden-accessible')) return true; |
| 111 |
|
| 112 |
var $select = $(this).select2({ |
| 113 |
multiple: true, |
| 114 |
placeholder: $(this).attr('data-placeholder') || '', |
| 115 |
maximumSelectionLength: 10, |
| 116 |
dropdownAdapter: dropdownAdapter, |
| 117 |
minimumResultsForSearch: 1, |
| 118 |
ajax: { |
| 119 |
url: ajax_url, |
| 120 |
dataType: 'json', |
| 121 |
data: data, |
| 122 |
type: "POST", |
| 123 |
quietMillis: 10, |
| 124 |
|
| 125 |
data: function (term, page) { // page is the one-based page number tracked by Select2 |
| 126 |
return { |
| 127 |
q: term, //search term |
| 128 |
"page_result": term.page || 1, |
| 129 |
"action": 'wdk_public_action', |
| 130 |
"page": 'wdk_frontendajax', |
| 131 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 132 |
"function": 'select_2_ajax_user', |
| 133 |
}; |
| 134 |
}, |
| 135 |
results: function (data, page) { |
| 136 |
var more = (page * 30) < data.total_count; // whether or not there are more results available |
| 137 |
|
| 138 |
// notice we return the value of more so Select2 knows if more results can be loaded |
| 139 |
return { results: data.items, more: more }; |
| 140 |
} |
| 141 |
}, |
| 142 |
templateResult: wdk_select_ajax_formatRepo, |
| 143 |
templateSelection: wdk_select_ajax_templateSelection |
| 144 |
}).on('select2:opening select2:closing', function (event) { |
| 145 |
//Disable original search (https://select2.org/searching#multi-select) |
| 146 |
var searchfield = $(this).parent().find('.select2-search__field'); |
| 147 |
searchfield.prop('disabled', true); |
| 148 |
}); |
| 149 |
|
| 150 |
$select.data('select2').$dropdown.addClass('select_multi_dropdown_users'); |
| 151 |
}); |
| 152 |
|
| 153 |
$('.select_multi', $wrapper).each(function () { |
| 154 |
var self = $(this); |
| 155 |
if(self.hasClass('select2-hidden-accessible')) return true; |
| 156 |
|
| 157 |
var $select = $(this).select2({ |
| 158 |
closeOnSelect:false, |
| 159 |
placeholder: $(this).attr('data-placeholder') || '', |
| 160 |
maximumSelectionLength: parseInt($(this).attr('data-limit')) || 10, |
| 161 |
dropdownAdapter: dropdownAdapter, |
| 162 |
minimumResultsForSearch: 1, |
| 163 |
maximumSelectionLength: +($(this).attr('data-maxselectlimit')) || 10, |
| 164 |
templateResult: wdk_select_ajax_formatRepo, |
| 165 |
templateSelection: wdk_select_ajax_templateSelection |
| 166 |
}); |
| 167 |
|
| 168 |
$select.data('select2').$dropdown.addClass('select_multi_dropdown'); |
| 169 |
}); |
| 170 |
|
| 171 |
$('.wdk_select2_field_suggestion').each(function () { |
| 172 |
var self = $(this); |
| 173 |
let ajax_url = $(this).attr('data-ajax'); |
| 174 |
var data = { |
| 175 |
"action": 'wdk_public_action', |
| 176 |
"page": 'wdk_frontendajax', |
| 177 |
"function": 'select_2_ajax_field_db_suggestion', |
| 178 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 179 |
"field_id": $(this).attr('data-id'), |
| 180 |
}; |
| 181 |
|
| 182 |
if(self.hasClass('select2-hidden-accessible')) return true; |
| 183 |
|
| 184 |
var $select = $(this).select2({ |
| 185 |
multiple: true, |
| 186 |
placeholder: $(this).attr('data-placeholder') || '', |
| 187 |
maximumSelectionLength: 1, |
| 188 |
//dropdownAdapter: dropdownAdapter, |
| 189 |
minimumResultsForSearch: 1, |
| 190 |
ajax: { |
| 191 |
url: ajax_url, |
| 192 |
dataType: 'json', |
| 193 |
data: data, |
| 194 |
type: "POST", |
| 195 |
quietMillis: 10, |
| 196 |
|
| 197 |
data: function (term, page) { // page is the one-based page number tracked by Select2 |
| 198 |
return { |
| 199 |
q: term, //search term |
| 200 |
"page_result": term.page || 1, |
| 201 |
"action": 'wdk_public_action', |
| 202 |
"page": 'wdk_frontendajax', |
| 203 |
"function": 'select_2_ajax_field_db_suggestion', |
| 204 |
"wdk_secure": wdk_script_parameters.wpApiSettings.wdk_secure_nonce, |
| 205 |
"field_id": $(this).attr('data-id'), |
| 206 |
}; |
| 207 |
}, |
| 208 |
results: function (data, page) { |
| 209 |
var more = (page * 30) < data.total_count; // whether or not there are more results available |
| 210 |
|
| 211 |
// notice we return the value of more so Select2 knows if more results can be loaded |
| 212 |
return { results: data.items, more: more }; |
| 213 |
} |
| 214 |
}, |
| 215 |
templateResult: wdk_select_ajax_formatRepo, |
| 216 |
templateSelection: wdk_select_ajax_templateSelection |
| 217 |
}).on('select2:opening select2:closing', function (event) { |
| 218 |
|
| 219 |
}); |
| 220 |
|
| 221 |
$select.data('select2').$dropdown.addClass('select_multi_dropdown_fields'); |
| 222 |
}); |
| 223 |
|
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
function wdk_select_ajax_formatRepo (repo) { |
| 228 |
|
| 229 |
if (repo.loading) { |
| 230 |
return repo.text; |
| 231 |
} |
| 232 |
|
| 233 |
var $container = $( |
| 234 |
"<span>"+repo.text+"</span>" |
| 235 |
); |
| 236 |
|
| 237 |
return $container; |
| 238 |
|
| 239 |
} |
| 240 |
function wdk_select_ajax_templateSelection (repo) { |
| 241 |
|
| 242 |
if (repo.loading) { |
| 243 |
return repo.text; |
| 244 |
} |
| 245 |
|
| 246 |
var $container = $( |
| 247 |
"<span>"+repo.text+"</span>" |
| 248 |
); |
| 249 |
|
| 250 |
return $container; |
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|