| 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 |
maximumSelectionLength: +($(this).attr('data-maxselectlimit')) || 10, |
| 142 |
templateResult: wdk_select_ajax_formatRepo, |
| 143 |
templateSelection: wdk_select_ajax_templateSelection |
| 144 |
}); |
| 145 |
|
| 146 |
$select.data('select2').$dropdown.addClass('select_multi_dropdown'); |
| 147 |
}); |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
function wdk_select_ajax_formatRepo (repo) { |
| 152 |
|
| 153 |
if (repo.loading) { |
| 154 |
return repo.text; |
| 155 |
} |
| 156 |
|
| 157 |
var $container = $( |
| 158 |
"<span>"+repo.text+"</span>" |
| 159 |
); |
| 160 |
|
| 161 |
return $container; |
| 162 |
|
| 163 |
} |
| 164 |
function wdk_select_ajax_templateSelection (repo) { |
| 165 |
|
| 166 |
if (repo.loading) { |
| 167 |
return repo.text; |
| 168 |
} |
| 169 |
|
| 170 |
var $container = $( |
| 171 |
"<span>"+repo.text+"</span>" |
| 172 |
); |
| 173 |
|
| 174 |
return $container; |
| 175 |
|
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|