PluginProbe
WP Directory Kit / 1.1.8
WP Directory Kit v1.1.8
1.5.9 1.5.7 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 All 37 releases
wpdirectorykit / public / js / wdk-select2.js

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

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