|
1 | 1 | /* global _, wu_initialize_selectizer, wu_selector */ |
2 | 2 | (function($) { |
3 | 3 |
|
4 | | - $(document).ready(function() { |
| 4 | + $(document).ready(function() { |
5 | 5 |
|
6 | | - window.wu_initialize_selectizer = function() { |
| 6 | + window.wu_initialize_selectizer = function() { |
7 | 7 |
|
8 | | - jQuery('[data-selectize]').selectize(); |
| 8 | + $.each($('[data-selectize]'), function(index, item) { |
9 | 9 |
|
10 | | - $.each($('[data-selectize-categories]'), function(index, item) { |
| 10 | + const $el = jQuery($(item)); |
| 11 | + const optionCount = $el.find('option').length; |
| 12 | + const settings = {}; |
11 | 13 |
|
12 | | - jQuery($(item)).selectize({ |
13 | | - maxItems: $(item).data('max-items') || 10, |
14 | | - create(input) { |
| 14 | + // Override maxOptions when the select has many options (e.g. TLD lists) |
| 15 | + if (optionCount > 1000) { |
| 16 | + settings.maxOptions = optionCount + 100; |
| 17 | + } |
15 | 18 |
|
16 | | - return { |
17 | | - value: input, |
18 | | - text: input, |
19 | | - }; |
| 19 | + $el.selectize(settings); |
20 | 20 |
|
21 | | - }, |
22 | | - }); |
| 21 | + }); |
23 | 22 |
|
24 | | - }); |
| 23 | + $.each($('[data-selectize-categories]'), function(index, item) { |
25 | 24 |
|
26 | | - $.each($('[data-model]'), function(index, item) { |
| 25 | + jQuery($(item)).selectize({ |
| 26 | + maxItems: $(item).data('max-items') || 10, |
| 27 | + create(input) { |
27 | 28 |
|
28 | | - wu_selector({ |
29 | | - el: item, |
30 | | - valueField: $(item).data('value-field'), |
31 | | - labelField: $(item).data('label-field'), |
32 | | - searchField: $(item).data('search-field'), |
33 | | - maxItems: $(item).data('max-items'), |
34 | | - selected: $(item).data('selected'), |
35 | | - options: [], |
36 | | - data: { |
37 | | - action: 'wu_search', |
38 | | - model: $(item).data('model'), |
39 | | - number: 10, |
40 | | - exclude: $(item).data('exclude'), |
41 | | - include: $(item).data('include'), |
42 | | - }, |
43 | | - }); |
| 29 | + return { |
| 30 | + value: input, |
| 31 | + text: input, |
| 32 | + }; |
44 | 33 |
|
45 | | - }); |
| 34 | + }, |
| 35 | + }); |
46 | 36 |
|
47 | | - }; |
| 37 | + }); |
48 | 38 |
|
49 | | - wu_initialize_selectizer(); |
| 39 | + $.each($('[data-model]'), function(index, item) { |
50 | 40 |
|
51 | | - jQuery('body').on('wubox:load', function() { |
| 41 | + wu_selector({ |
| 42 | + el: item, |
| 43 | + valueField: $(item).data('value-field'), |
| 44 | + labelField: $(item).data('label-field'), |
| 45 | + searchField: $(item).data('search-field'), |
| 46 | + maxItems: $(item).data('max-items'), |
| 47 | + selected: $(item).data('selected'), |
| 48 | + options: [], |
| 49 | + data: { |
| 50 | + action: 'wu_search', |
| 51 | + model: $(item).data('model'), |
| 52 | + number: 10, |
| 53 | + exclude: $(item).data('exclude'), |
| 54 | + include: $(item).data('include'), |
| 55 | + }, |
| 56 | + }); |
52 | 57 |
|
53 | | - wu_initialize_selectizer(); |
| 58 | + }); |
54 | 59 |
|
55 | | - }); |
| 60 | + }; |
56 | 61 |
|
57 | | - }); |
| 62 | + wu_initialize_selectizer(); |
58 | 63 |
|
59 | | - window.wu_selector = function wu_selector(options) { |
| 64 | + jQuery('body').on('wubox:load', function() { |
60 | 65 |
|
61 | | - options = _.defaults(options, { |
62 | | - options: [], |
63 | | - maxItems: 1, |
64 | | - templateName: false, |
65 | | - create: false, |
66 | | - }); |
| 66 | + wu_initialize_selectizer(); |
67 | 67 |
|
68 | | - if (jQuery(options.el).data('init')) { |
| 68 | + }); |
69 | 69 |
|
70 | | - return; |
| 70 | + }); |
71 | 71 |
|
72 | | - } // end if; |
| 72 | + window.wu_selector = function wu_selector(options) { |
73 | 73 |
|
74 | | - jQuery(options.el).data('__options', options); |
| 74 | + options = _.defaults(options, { |
| 75 | + options: [], |
| 76 | + maxItems: 1, |
| 77 | + templateName: false, |
| 78 | + create: false, |
| 79 | + }); |
75 | 80 |
|
76 | | - const select = jQuery(options.el).selectize({ |
77 | | - valueField: options.valueField, |
78 | | - labelField: options.labelField, |
79 | | - searchField: ['text', 'name', 'display_name', 'domain', 'path', 'title', 'desc', 'code', 'post_title', 'reference_code'], |
80 | | - options: options.options, |
81 | | - maxItems: options.maxItems, |
82 | | - create: options.create, |
83 | | - render: { |
84 | | - option(option) { |
| 81 | + if (jQuery(options.el).data('init')) { |
85 | 82 |
|
86 | | - const templateName = options.templateName ? options.templateName : options.data.model; |
| 83 | + return; |
87 | 84 |
|
88 | | - const template_html = jQuery('#wu-template-' + templateName).length |
89 | | - ? jQuery('#wu-template-' + templateName).html() |
90 | | - : jQuery('#wu-template-default').html(); |
| 85 | + } // end if; |
91 | 86 |
|
92 | | - const template = _.template(template_html, { |
93 | | - interpolate: /\{\{(.+?)\}\}/g, |
94 | | - }); |
| 87 | + jQuery(options.el).data('__options', options); |
95 | 88 |
|
96 | | - const render = template(option); |
| 89 | + const select = jQuery(options.el).selectize({ |
| 90 | + valueField: options.valueField, |
| 91 | + labelField: options.labelField, |
| 92 | + searchField: [ 'text', 'name', 'display_name', 'domain', 'path', 'title', 'desc', 'code', 'post_title', 'reference_code' ], |
| 93 | + options: options.options, |
| 94 | + maxItems: options.maxItems, |
| 95 | + create: options.create, |
| 96 | + render: { |
| 97 | + option(option) { |
97 | 98 |
|
98 | | - return render; |
| 99 | + const templateName = options.templateName ? options.templateName : options.data.model; |
99 | 100 |
|
100 | | - }, |
101 | | - }, |
102 | | - load(query, callback) { |
| 101 | + const template_html = jQuery('#wu-template-' + templateName).length |
| 102 | + ? jQuery('#wu-template-' + templateName).html() |
| 103 | + : jQuery('#wu-template-default').html(); |
103 | 104 |
|
104 | | - if (! query.length) { |
| 105 | + const template = _.template(template_html, { |
| 106 | + interpolate: /\{\{(.+?)\}\}/g, |
| 107 | + }); |
105 | 108 |
|
106 | | - return callback(); |
| 109 | + const render = template(option); |
107 | 110 |
|
108 | | - } // end if; |
| 111 | + return render; |
109 | 112 |
|
110 | | - const __options = jQuery(options.el).data('__options'); |
| 113 | + }, |
| 114 | + }, |
| 115 | + load(query, callback) { |
111 | 116 |
|
112 | | - jQuery.ajax({ |
113 | | - // eslint-disable-next-line no-undef |
114 | | - url: wu_selectizer.ajaxurl, |
115 | | - type: 'POST', |
116 | | - data: { |
117 | | - ...__options.data, |
118 | | - query: { |
119 | | - search: '*' + query + '*', |
120 | | - }, |
121 | | - }, |
122 | | - error() { |
| 117 | + if (! query.length) { |
123 | 118 |
|
124 | | - callback(); |
| 119 | + return callback(); |
125 | 120 |
|
126 | | - }, |
127 | | - success(res) { |
| 121 | + } // end if; |
128 | 122 |
|
129 | | - selectize.savedItems = res; |
| 123 | + const __options = jQuery(options.el).data('__options'); |
130 | 124 |
|
131 | | - callback(res); |
| 125 | + jQuery.ajax({ |
| 126 | + // eslint-disable-next-line no-undef |
| 127 | + url: wu_selectizer.ajaxurl, |
| 128 | + type: 'POST', |
| 129 | + data: { |
| 130 | + ...__options.data, |
| 131 | + query: { |
| 132 | + search: '*' + query + '*', |
| 133 | + }, |
| 134 | + }, |
| 135 | + error() { |
132 | 136 |
|
133 | | - }, |
134 | | - }); |
| 137 | + callback(); |
135 | 138 |
|
136 | | - }, |
137 | | - }); |
| 139 | + }, |
| 140 | + success(res) { |
138 | 141 |
|
139 | | - jQuery(options.el).attr('data-init', 1); |
| 142 | + selectize.savedItems = res; |
140 | 143 |
|
141 | | - const selectize = select[0].selectize; |
| 144 | + callback(res); |
142 | 145 |
|
143 | | - /* |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + jQuery(options.el).attr('data-init', 1); |
| 153 | + |
| 154 | + const selectize = select[ 0 ].selectize; |
| 155 | + |
| 156 | + /* |
144 | 157 | * Makes sure this is reactive for vue |
145 | 158 | */ |
146 | | - selectize.on('change', function(value) { |
| 159 | + selectize.on('change', function(value) { |
147 | 160 |
|
148 | | - const input = jQuery(select[0]); |
| 161 | + const input = jQuery(select[ 0 ]); |
149 | 162 |
|
150 | | - const vue_app = input.parents('[data-wu-app]').data('wu-app'); |
| 163 | + const vue_app = input.parents('[data-wu-app]').data('wu-app'); |
151 | 164 |
|
152 | | - if (vue_app && typeof window['wu_' + vue_app] !== 'undefined') { |
| 165 | + if (vue_app && typeof window[ 'wu_' + vue_app ] !== 'undefined') { |
153 | 166 |
|
154 | | - window['wu_' + vue_app][input.attr('name')] = value; |
| 167 | + window[ 'wu_' + vue_app ][ input.attr('name') ] = value; |
155 | 168 |
|
156 | | - } // end if; |
| 169 | + } // end if; |
157 | 170 |
|
158 | | - }); |
| 171 | + }); |
159 | 172 |
|
160 | | - selectize.on('item_add', function(value) { |
| 173 | + selectize.on('item_add', function(value) { |
161 | 174 |
|
162 | | - let active_item = { |
163 | | - url: null, |
164 | | - }; |
| 175 | + let active_item = { |
| 176 | + url: null, |
| 177 | + }; |
165 | 178 |
|
166 | | - jQuery.each(selectize.savedItems, function(index, item) { |
| 179 | + jQuery.each(selectize.savedItems, function(index, item) { |
167 | 180 |
|
168 | | - if (item.setting_id === value) { |
| 181 | + if (item.setting_id === value) { |
169 | 182 |
|
170 | | - active_item = item; |
| 183 | + active_item = item; |
171 | 184 |
|
172 | | - } // end if; |
| 185 | + } // end if; |
173 | 186 |
|
174 | | - }); |
| 187 | + }); |
175 | 188 |
|
176 | | - if (active_item.url) { |
| 189 | + if (active_item.url) { |
177 | 190 |
|
178 | | - window.location.href = active_item.url; |
| 191 | + window.location.href = active_item.url; |
179 | 192 |
|
180 | | - } // end if; |
| 193 | + } // end if; |
181 | 194 |
|
182 | | - }); |
| 195 | + }); |
183 | 196 |
|
184 | | - if (options.selected) { |
| 197 | + if (options.selected) { |
185 | 198 |
|
186 | | - selectize.options = []; |
| 199 | + selectize.options = []; |
187 | 200 |
|
188 | | - selectize.clearOptions(); |
| 201 | + selectize.clearOptions(); |
189 | 202 |
|
190 | | - const selected_values = _.isArray(options.selected) ? options.selected : [options.selected]; |
| 203 | + const selected_values = _.isArray(options.selected) ? options.selected : [ options.selected ]; |
191 | 204 |
|
192 | | - selectize.addOption(selected_values); |
| 205 | + selectize.addOption(selected_values); |
193 | 206 |
|
194 | | - const selected = _.isArray(options.selected) ? _.pluck(options.selected, options.valueField) : options.selected[options.valueField]; |
| 207 | + const selected = _.isArray(options.selected) ? _.pluck(options.selected, options.valueField) : options.selected[ options.valueField ]; |
195 | 208 |
|
196 | | - selectize.setValue(selected, false); |
| 209 | + selectize.setValue(selected, false); |
197 | 210 |
|
198 | | - } // end if; |
| 211 | + } // end if; |
199 | 212 |
|
200 | | - }; |
| 213 | + }; |
201 | 214 |
|
202 | 215 | }(jQuery)); |
0 commit comments