Wiki source code of Suggest Widget

Version 82.1 by Marius Dumitru Florea on 2019/06/21

Hide last authors
Sergiu Dumitriu 49.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
Marius Dumitru Florea 67.1 2 {{toc start="2"/}}
Sergiu Dumitriu 49.1 3 {{/box}}
Sergiu Dumitriu 48.1 4
Marius Dumitru Florea 67.1 5 The suggest widget is implemented using [[Selectize.js>>http://selectize.github.io/selectize.js/]] with its Bootstrap skin and is bundled by default in XWiki platform.
ElenaOanaTabaranu 1.1 6
Marius Dumitru Florea 67.1 7 == Predefined Suggesters ==
ElenaOanaTabaranu 1.1 8
Marius Dumitru Florea 67.1 9 A couple of suggesters are available by default in XWiki. Each suggester provides:
ElenaOanaTabaranu 14.1 10
Marius Dumitru Florea 67.1 11 * dedicated helper Velocity macros (to import required resources and to output the required HTML)
12 * a jQuery plugin to activate the suggest behavior on any input or select element
13 * a CSS class that activates the suggest behavior automatically
ElenaOanaTabaranu 14.1 14
Marius Dumitru Florea 67.1 15 The dedicated Velocity macros are in fact loading a JavaScript code that calls the dedicated jQuery plugin on all the elements (input or select) that are marked with the dedicated CSS class. Here's the list of available suggesters:
Sergiu Dumitriu 49.1 16
Marius Dumitru Florea 67.1 17 |=Data|=Velocity Macros|=jQuery Plugin|=CSS Class
18 |Pages|pagePicker, pagePicker_import|suggestPages|suggest-pages
19 |Attachments|attachmentPicker, attachmentPicker_import|suggestAttachments|suggest-attachments
20 |Users|userPicker, userPicker_input, userPicker_import|suggestUsers|suggest-users
21 |Groups|groupPicker|suggestGroups|suggest-groups
22 |Property Values|xpropertySuggestInputDisplayer|suggestPropertyValues|suggest-propertyValues
Sergiu Dumitriu 49.1 23
Marius Dumitru Florea 67.1 24 === Usage ===
ElenaOanaTabaranu 14.1 25
Marius Dumitru Florea 67.1 26 There are 3 ways in which you can activate a predefined suggester:
27
28 * from Velocity (by calling the dedicated Velocity macro)
29 * from JavaScript (by calling the dedicated jQyery plugin)
30 * from HTML (by marking form fields with the dedicated CSS class)
31
32 Use the one that suits you the best.
33
34 ==== From Velocity ====
35
36 You can do this by putting the following code in the content of a wiki page. Here we're calling the dedicated ##pagePicker## Velocity macro. Note that you can pass configuration options. Most of these settings are translated into HTML attributes (of the generated suggest input element) read by the dedicated jQuery plugin. You can use the ##data-xwiki-selectize## parameter to pass configuration options to the [[Selectize.js>>https://github.com/selectize/selectize.js/blob/master/docs/usage.md#configuration]] widget. Each predefined suggester might also have custom parameters you can set, check their documentation below.
37
38 {{code language="none"}}
39 {{velocity}}
40 {{html}}
41 #set ($pagePickerParams = {
42 'name': 'targetPages',
43 'value': ['Path.To.Alice', 'Path.To.Bob'],
44 'multiple': 'multiple'
45 })
46 #pagePicker($pagePickerParams)
47 {{/html}}
48 {{/velocity}}
Sergiu Dumitriu 48.1 49 {{/code}}
ElenaOanaTabaranu 1.1 50
Marius Dumitru Florea 67.1 51 ==== From JavaScript ====
ElenaOanaTabaranu 1.1 52
Marius Dumitru Florea 67.1 53 You can do this from a JavaScript Skin extension. First you need to configure the path to the suggester you want to use and then you need to call the dedicated jQuery plugin (##suggestAttachments##) from a require block. Note that you can pass configuration options when activating the suggester. These configuration options are either generic (for the [[Selectize>>https://github.com/selectize/selectize.js/blob/master/docs/usage.md#configuration]] widget) or specific to each suggester (see below).
54
55 {{code language="js"}}
56 require.config({
57 paths: {
58 'xwiki-suggestAttachments': "$xwiki.getSkinFile('uicomponents/suggest/suggestAttachments.js', true)" +
59 "?v=$escapetool.url($xwiki.version)"
60 }
61 });
62
63 require(['jquery', 'xwiki-suggestAttachments'], function($) {
64 $('input[name="logo"]').suggestAttachments({
65 accept: 'image/,.mp4,.pdf',
66 uploadAllowed: true
67 });
68 });
Sergiu Dumitriu 48.1 69 {{/code}}
ElenaOanaTabaranu 1.1 70
Marius Dumitru Florea 67.1 71 Of course, you still need to load your skin extension code (from the content of a wiki page):
ElenaOanaTabaranu 17.1 72
Marius Dumitru Florea 67.1 73 {{code language="none"}}
74 {{velocity}}
75 {{html}}
76 #set ($discard = $xwiki.jsx.use('Path.To.Your.JSX'))
77 <input type="text" name="logo" value="Sandbox.WebHome@XWikiLogo.png"/>
78 {{/html}}
79 {{/velocity}}
80 {{/code}}
ElenaOanaTabaranu 17.1 81
Marius Dumitru Florea 67.1 82 ==== From HTML ====
ElenaOanaTabaranu 17.1 83
Marius Dumitru Florea 67.1 84 Put the following code in the content of a wiki page. First you need to import the resources required by the suggester and then you need to mark the form fields you wish to enhance (using the dedicated CSS class, ##suggest-users## here). Note that you can use the ##data-xwiki-selectize## attribute to pass configuration options in JSON format to the [[Selectize.js>>https://github.com/selectize/selectize.js/blob/master/docs/usage.md#configuration]] widget. Each predefined suggester might also have custom attributes you can set, check their documentation below.
ElenaOanaTabaranu 18.1 85
Marius Dumitru Florea 67.1 86 {{code language="none"}}
87 {{velocity output="false"}}
88 #userPicker_import
89 {{/velocity}}
ElenaOanaTabaranu 1.1 90
Marius Dumitru Florea 67.1 91 {{html}}
92 <input type="text" name="manager" value="XWiki.mflorea" class="suggest-users"/>
93 {{/html}}
94 {{/code}}
Sergiu Dumitriu 49.1 95
Marius Dumitru Florea 67.1 96 === Suggest Pages ===
ElenaOanaTabaranu 30.1 97
Marius Dumitru Florea 67.1 98 This suggester displays the page rendered title and the page location. The value that is saved in the end is the page reference. The hidden page user preference is taken into account.
99
100 {{code language="none"}}
101 {{velocity}}
102 {{html}}
103 #set ($pagePickerParams = {
104 ...
105 })
106 #pagePicker($pagePickerParams)
107 {{/html}}
108 {{/velocity}}
Sergiu Dumitriu 48.1 109 {{/code}}
ElenaOanaTabaranu 14.1 110
Marius Dumitru Florea 80.1 111 {{image reference="pagePicker.png"/}}
ElenaOanaTabaranu 14.1 112
Marius Dumitru Florea 67.1 113 Custom configuration parameters:
114
115 |=Name|=Description|=Default Value
116 |data-suggest-space|Restrict the suggestions to a specific location|None
117
118 === Suggest Attachments ===
119
120 This suggester displays the attachment (file) name and the attachment location. The suggestion icon is either a preview, if the attachment is an image or an icon that depends on the attachment media type. The value that is saved in the end is the attachment reference. The hidden page user preference is taken into account (e.g. attachments from hidden pages are not displayed by default).
121
122 {{code language="none"}}
123 {{velocity}}
124 {{html}}
125 #set ($attachmentPickerParams = {
126 ...
127 })
128 #attachmentPicker($attachmentPickerParams)
129 {{/html}}
130 {{/velocity}}
Sergiu Dumitriu 48.1 131 {{/code}}
132
Marius Dumitru Florea 67.1 133 {{image reference="attachmentPicker.png"/}}
Sergiu Dumitriu 48.1 134
Marius Dumitru Florea 67.1 135 It supports local file upload, either by selecting the files from the file browser or by dropping them on the suggest input. It also supports drag and drop of attachment links from the Attachment tab at the bottom of the wiki page.
136
137 Custom configuration parameters:
138
139 |=Name|=Description|=Default Value
140 |data-document-reference|The document where the selected values are saved and where new files are being uploaded. Stored attachment references will be relative this document.|Current page
141 |data-search-scope|(((Where to look for attachments. The following is supported:
142 * "wiki:wikiName" look for attachments in the specified wiki
143 * "space:spaceReference": look for attachments in the specified space
144 * "document:documentReference" look for attachments in the specified document)))|Current wiki
145 |data-upload-allowed|Whether to allow the user to upload files|false
146 |data-accept|(((Indicates the type of files that can be selected or uploaded. The value is a comma separated list of:
147 * file name extensions (e.g. .png,.pdf)
148 * complete or partial media types (e.g. image/,video/mpeg)
149
150 If nothing is specified then no restriction is applied.)))|None
151
152 === Suggest Users ===
153
154 This suggester displays the user avatar and their name. The value that is saved in the end is the user reference (the reference of the user profile page).
155
156 {{code language="none"}}
157 {{velocity}}
158 {{html}}
159 #set ($userPickerParams = {
160 ...
161 })
162 #userPicker($userPickerParams)
163 {{/html}}
164 {{/velocity}}
Sergiu Dumitriu 48.1 165 {{/code}}
ElenaOanaTabaranu 17.1 166
Marius Dumitru Florea 67.1 167 {{image reference="userPicker.png"/}}
Sergiu Dumitriu 49.1 168
Marius Dumitru Florea 67.1 169 Custom configuration parameters:
MensoHeus 45.1 170
Marius Dumitru Florea 67.1 171 |=Name|=Description|=Default Value
172 |data-userScope|Where to retrieve the user suggestions from. Supported values are: LOCAL_ONLY, GLOBAL_ONLY, LOCAL_AND_GLOBAL|User scope of the current wiki
MensoHeus 45.1 173
Marius Dumitru Florea 67.1 174 === Suggest Groups ===
175
176 The suggester displays the group logo and its name. The group logo is the first image attachment found on the group page. The value that is saved in the end is the group reference (the reference of the group page).
177
178 {{code language="none"}}
179 {{velocity}}
180 {{html}}
181 #set ($groupPickerParams = {
182 ...
183 })
184 #groupPicker($groupPickerParams)
185 {{/html}}
186 {{/velocity}}
Sergiu Dumitriu 48.1 187 {{/code}}
MensoHeus 45.1 188
Marius Dumitru Florea 67.1 189 {{image reference="groupPicker.png"/}}
ElenaOanaTabaranu 18.1 190
Marius Dumitru Florea 67.1 191 Custom configuration parameters:
ElenaOanaTabaranu 18.1 192
Marius Dumitru Florea 67.1 193 |=Name|=Description|=Default Value
194 |data-userScope|Where to retrieve the group suggestions from. Supported values are: LOCAL_ONLY, GLOBAL_ONLY, LOCAL_AND_GLOBAL. Note that GLOBAL_ONLY has the same effect as LOCAL_AND_GLOBAL because a wiki with only global users can have local groups|User scope of the current wiki
Oana Florea 38.1 195
Marius Dumitru Florea 67.1 196 === Suggest Property Values ===
Sergiu Dumitriu 49.1 197
Marius Dumitru Florea 67.1 198 This suggester displays the label assocaited with a property value and saves the raw property value. The following property types are supported by this suggester: Static List and Database List. Note that these property types have default custom displayers associated that load the suggest widget when the "Use suggest" meta property is on. So normally the only thing you need to do is to display the property.
Sergiu Dumitriu 49.1 199
Marius Dumitru Florea 67.1 200 {{code language="none"}}
201 {{velocity}}
202 $doc.display('myDatabaseListProperty')
203 {{/velocity}}
204 {{/code}}
Oana Florea 38.1 205
Marius Dumitru Florea 67.1 206 {{image reference="propertyValuePicker.png"/}}
207
Marius Dumitru Florea 81.1 208 If you don't have a property to display then you can also enable this suggester by calling the generic ##suggestInput## macro like this:
209
210 {{code language="none"}}
211 {{velocity}}
212 {{html}}
213 #set ($discard = $xwiki.jsfx.use('uicomponents/suggest/suggestPropertyValues.js',
214 {'forceSkinAction': true, 'language': $xcontext.locale}))
215 #set ($suggestParams = {
216 'class': 'suggest-propertyValues',
217 'placeholder': 'Select the movie director',
218 'data-className': 'Help.Applications.Movies.Code.MoviesClass',
219 'data-propertyName': 'databaseList1'
220 })
221 #suggestInput($suggestParams)
222 {{/html}}
223 {{/velocity}}
224 {{/code}}
225
Marius Dumitru Florea 82.1 226 If you need tag suggestions then you can use this configuration:
227
228 {{code language="none"}}
229 #set ($suggestParams = {
230 'class': 'suggest-propertyValues',
231 'multiple': 'multiple',
232 'data-className': 'XWiki.TagClass',
233 'data-propertyName': 'tags'
234 })
235 {{/code}}
236
Marius Dumitru Florea 67.1 237 Custom configuration parameters:
238
239 |=Name|=Description|=Default Value
240 |data-className|The class where the property is defined|None
241 |data-propertyName|The property whose values are to be retrieved as suggestions|None
242 |data-freeText|Whether free text is allowed or forbidden. Possible values are: allowed, forbidden|None
243
244 == Custom Suggesters ==
245
246 === Suggest from Static Data ===
247
248 Let's see how you can use the suggest widget with static data (i.e. data that is available when the web page is loaded).
249
250 {{html wiki="true"}}
251 <div>
252 <ul class="nav nav-tabs" role="tablist">
253 <li role="presentation" class="active">
254 <a href="#staticDataV1" aria-controls="staticDataV1" role="tab" data-toggle="tab">V1: The Basics</a>
255 </li>
256 <li role="presentation">
257 <a href="#staticDataV2" aria-controls="staticDataV2" role="tab" data-toggle="tab">V2: Separate Data</a>
258 </li>
259 <li role="presentation">
260 <a href="#staticDataV3" aria-controls="staticDataV3" role="tab" data-toggle="tab">V3: Helper Macros</a>
261 </li>
262 <li role="presentation">
263 <a href="#staticDataV4" aria-controls="staticDataV4" role="tab" data-toggle="tab">V4: Reusable Code</a>
264 </li>
265 </ul>
266
267 <div class="tab-content">
268 <div role="tabpanel" class="tab-pane active" id="staticDataV1">
269 <p>The following code:</p>
270
271 * loads the resources (JavaScript and CSS) needed by the suggest widget
272 * outputs the HTML (select element) needed by the suggest widget
273
274 <p>Note that the select element is marked with the ##xwiki-selectize## CSS class which activates the suggest widget.</p>
275
276 {{code language="none"}}
277 {{velocity}}
278 #set ($discard = $xwiki.linkx.use($services.webjars.url('selectize.js', 'css/selectize.bootstrap3.css'),
279 {'type': 'text/css', 'rel': 'stylesheet'}))
280 #set ($discard = $xwiki.ssfx.use('uicomponents/suggest/xwiki.selectize.css', true))
281 #set ($discard = $xwiki.jsfx.use('uicomponents/suggest/xwiki.selectize.js', true))
282 {{/velocity}}
283
284 {{html}}
285 <select class="xwiki-selectize">
286 <option value="">Select the country</option>
287 <option value="fr">France</option>
288 <option value="de">Germany</option>
289 <option value="ro">Romania</option>
290 </select>
291 {{/html}}
Sergiu Dumitriu 49.1 292 {{/code}}
293
Marius Dumitru Florea 67.1 294 </div>
Oana Florea 38.1 295
Marius Dumitru Florea 67.1 296 <div role="tabpanel" class="tab-pane" id="staticDataV2">
297 <p>In this version we separate the code that generates the data used to provide suggestions. The data is usually retrieved from the database.</p>
298
299 {{code language="none"}}
300 {{velocity}}
301 {{html}}
302 #set ($discard = $xwiki.linkx.use($services.webjars.url('selectize.js', 'css/selectize.bootstrap3.css'),
303 {'type': 'text/css', 'rel': 'stylesheet'}))
304 #set ($discard = $xwiki.ssfx.use('uicomponents/suggest/xwiki.selectize.css', true))
305 #set ($discard = $xwiki.jsfx.use('uicomponents/suggest/xwiki.selectize.js', true))
306 #set ($countries = [
307 {'name': 'France', 'code': 'fr'},
308 {'name': 'Germany', 'code': 'de'},
309 {'name': 'Romania', 'code': 'ro'}
310 ])
311 ## See https://github.com/selectize/selectize.js/blob/master/docs/usage.md#configuration
312 #set ($selectizeSettings = {})
313 <select class="xwiki-selectize" data-xwiki-selectize="$escapetool.xml($jsontool.serialize($selectizeSettings))">
314 <option value="">Select the country</option>
315 #foreach ($country in $countries)
316 <option value="$!escapetool.xml($country.code)">$!escapetool.xml($country.name)</option>
317 #end
318 </select>
319 {{/html}}
320 {{/velocity}}
Sergiu Dumitriu 48.1 321 {{/code}}
Oana Florea 38.1 322
Marius Dumitru Florea 67.1 323 </div>
324
325 <div role="tabpanel" class="tab-pane" id="staticDataV3">
326 <p>In this version we use some helper Velocity macros: ##picker_import## and ##suggestInput##. Notice the way we specify the selected value and the placeholder by passing parameters to the ##suggestInput## macro.</p>
327
328 {{code language="none"}}
329 {{velocity output="false"}}
330 #set ($countries = [
331 {'name': 'France', 'code': 'fr'},
332 {'name': 'Germany', 'code': 'de'},
333 {'name': 'Romania', 'code': 'ro'}
334 ])
335
336 #macro (countryPicker_displayOptions $selectedValues)
337 #foreach ($country in $countries)
338 <option value="$!escapetool.xml($country.code)"#if ($selectedValues.contains($country.code)) selected="selected"#end
339 >$!escapetool.xml($country.name)</option>
340 #end
341 #end
342 {{/velocity}}
343
344 {{velocity}}
345 {{html}}
346 #picker_import
347 #set ($discard = $xwiki.jsfx.use('uicomponents/suggest/xwiki.selectize.js', true))
348 ## See https://github.com/selectize/selectize.js/blob/master/docs/usage.md#configuration
349 #set ($selectizeSettings = {})
350 #set ($suggestInputAttributes = {
351 'name': 'country',
352 'class': 'xwiki-selectize',
353 'data-xwiki-selectize': $selectizeSettings,
354 'value': 'ro',
355 'placeholder': 'Select the country'
356 })
357 #suggestInput($suggestInputAttributes 'countryPicker_displayOptions')
358 {{/html}}
359 {{/velocity}}
Sergiu Dumitriu 48.1 360 {{/code}}
Oana Florea 38.1 361
Marius Dumitru Florea 67.1 362 </div>
Buzila Vlad 52.1 363
Marius Dumitru Florea 67.1 364 <div role="tabpanel" class="tab-pane" id="staticDataV4">
365 <p>In this last version we make the code reusable by creating the ##countryPicker## and ##countryPicker_import## Velocity macros.</p>
Buzila Vlad 52.1 366
Marius Dumitru Florea 67.1 367 {{code language="none"}}
368 {{velocity output="false"}}
369 #set ($countries = [
370 {'name': 'France', 'code': 'fr'},
371 {'name': 'Germany', 'code': 'de'},
372 {'name': 'Romania', 'code': 'ro'}
373 ])
Buzila Vlad 52.1 374
Marius Dumitru Florea 67.1 375 #macro (countryPicker_displayOptions $selectedValues)
376 #foreach ($country in $countries)
377 <option value="$!escapetool.xml($country.code)"#if ($selectedValues.contains($country.code)) selected="selected"#end
378 >$!escapetool.xml($country.name)</option>
379 #end
380 #end
Buzila Vlad 52.1 381
Marius Dumitru Florea 67.1 382 #macro (countryPicker_import)
383 #picker_import
384 #set ($discard = $xwiki.jsfx.use('uicomponents/suggest/xwiki.selectize.js', true))
385 #end
Buzila Vlad 52.1 386
Marius Dumitru Florea 67.1 387 #macro (countryPicker $parameters)
388 #countryPicker_import
389 #if ("$!parameters" == "")
390 #set ($parameters = {})
391 #end
392 #set ($discard = $parameters.put('class', "$!parameters.get('class') xwiki-selectize suggest-countries"))
393 #if (!$parameters.containsKey('placeholder'))
394 #set ($parameters.placeholder = 'Select the country')
395 #end
396 #suggestInput($parameters 'countryPicker_displayOptions')
397 #end
398 {{/velocity}}
Buzila Vlad 52.1 399
Marius Dumitru Florea 67.1 400 {{velocity}}
401 {{html}}
402 #set ($countryPickerParams = {
403 'name': 'country',
404 'value': 'ro'
405 })
406 #countryPicker($countryPickerParams)
407 {{/html}}
408 {{/velocity}}
Buzila Vlad 52.1 409 {{/code}}
410
Marius Dumitru Florea 67.1 411 </div>
412 </div>
413 </div>
414 {{/html}}
Buzila Vlad 52.1 415
Marius Dumitru Florea 67.1 416 === Suggest from Remote Data ===
Buzila Vlad 52.1 417
Marius Dumitru Florea 67.1 418 Most of the time you will want to retrive suggestions when the user types rather than loading all the suggestions on page load. Let's see how you can do this.
419
420 {{html wiki="true"}}
421 <div>
422 <ul class="nav nav-tabs" role="tablist">
423 <li role="presentation" class="active">
424 <a href="#remoteDataV1" aria-controls="remoteDataV1" role="tab" data-toggle="tab">V1: The Basics</a>
425 </li>
426 <li role="presentation">
427 <a href="#remoteDataV2" aria-controls="remoteDataV2" role="tab" data-toggle="tab">V2: Reusable Code</a>
428 </li>
429 </ul>
430
431 <div class="tab-content">
432 <div role="tabpanel" class="tab-pane active" id="remoteDataV1">
433 <p>In order to retrieve suggestions as you type you need to write some JavaScript code that makes an asynchronous HTTP request to retrive the data. You can put this code in a JavaScript Skin extension object. Then you need to load this JavaScript code on the page where you want to activate the suggest widget.</p>
434
435 {{code language="js"}}
436 require.config({
437 paths: {
438 'xwiki-selectize': "$xwiki.getSkinFile('uicomponents/suggest/xwiki.selectize.js', true)" +
439 "?v=$escapetool.url($xwiki.version)"
440 }
441 });
442
443 require(['jquery', 'xwiki-selectize'], function($) {
444 var settings = {
445 load: function(typedText, callback) {
446 $.getJSON('your/service/url', {
447 text: typedText
448 }).done(callback).fail(callback);
449 },
450 loadSelected: function(selectedValue, callback) {
451 $.getJSON('your/service/url', {
452 text: typedText,
453 exactMatch: true
454 }).done(callback).fail(callback);
Buzila Vlad 54.1 455 }
Marius Dumitru Florea 67.1 456 };
Buzila Vlad 52.1 457
Marius Dumitru Florea 67.1 458 $('.suggest-countries').xwikiSelectize(settings);
459 });
Buzila Vlad 52.1 460 {{/code}}
461
Marius Dumitru Florea 67.1 462 {{code language="none"}}
463 {{velocity}}
464 #set ($discard = $xwiki.linkx.use($services.webjars.url('selectize.js', 'css/selectize.bootstrap3.css'),
465 {'type': 'text/css', 'rel': 'stylesheet'}))
466 #set ($discard = $xwiki.ssfx.use('uicomponents/suggest/xwiki.selectize.css', true))
467 #set ($discard = $xwiki.jsx.use('Path.To.Your.JSX'))
468 {{/velocity}}
ElenaOanaTabaranu 1.1 469
Marius Dumitru Florea 67.1 470 {{html}}
471 <select class="suggest-countries">
472 <option value="">Select the country</option>
473 </select>
474 {{/html}}
475 {{/code}}
ElenaOanaTabaranu 1.1 476
Marius Dumitru Florea 67.1 477 </div>
Adel Atallah 58.1 478
Marius Dumitru Florea 67.1 479 <div role="tabpanel" class="tab-pane" id="remoteDataV2">
480 <p>In this last version we make the code reusable by creating a jQuery plugin and some helper Velocity macros.</p>
Adel Atallah 58.1 481
Marius Dumitru Florea 67.1 482 {{code language="js"}}
483 require.config({
484 paths: {
485 'xwiki-selectize': "$xwiki.getSkinFile('uicomponents/suggest/xwiki.selectize.js', true)" +
486 "?v=$escapetool.url($xwiki.version)"
487 }
488 });
Adel Atallah 58.1 489
Marius Dumitru Florea 67.1 490 define('xwiki-suggestCountries', ['jquery', 'xwiki-selectize'], function($) {
491 var getSettings = function(select) {
492 return {
493 load: function(typedText, callback) {
494 $.getJSON('your/service/url', {
495 text: typedText
496 }).done(callback).fail(callback);
497 },
498 loadSelected: function(selectedValue, callback) {
499 $.getJSON('your/service/url', {
500 text: typedText,
501 exactMatch: true
502 }).done(callback).fail(callback);
503 }
504 };
505 };
Adel Atallah 60.1 506
Marius Dumitru Florea 67.1 507 $.fn.suggestCountries = function(settings) {
508 return this.each(function() {
509 $(this).xwikiSelectize($.extend(getSettings($(this)), settings));
510 });
511 };
512 });
Adel Atallah 58.1 513
Marius Dumitru Florea 67.1 514 require(['jquery', 'xwiki-suggestCountries', 'xwiki-events-bridge'], function($) {
515 var init = function(event, data) {
516 var container = $((data && data.elements) || document);
517 container.find('.suggest-countries').suggestCountries();
518 };
Adel Atallah 58.1 519
Marius Dumitru Florea 67.1 520 $(document).on('xwiki:dom:loaded xwiki:dom:updated', init);
521 XWiki.domIsLoaded && init();
522 });
523 {{/code}}
Adel Atallah 66.1 524
Marius Dumitru Florea 67.1 525 {{code language="none"}}
526 {{velocity output="false"}}
527 #macro (countryPicker_import)
528 #picker_import
529 #set ($discard = $xwiki.jsx.use('Path.To.Your.JSX'))
530 #end
ElenaOanaTabaranu 1.1 531
Marius Dumitru Florea 67.1 532 #macro (countryPicker $parameters)
533 #countryPicker_import
534 #if ("$!parameters" == "")
535 #set ($parameters = {})
536 #end
537 #set ($discard = $parameters.put('class', "$!parameters.get('class') suggest-countries"))
538 #if (!$parameters.containsKey('placeholder'))
539 #set ($parameters.placeholder = 'Select the country')
540 #end
541 #suggestInput($parameters)
542 #end
543 {{/velocity}}
Oana Florea 36.1 544
Marius Dumitru Florea 67.1 545 {{velocity}}
546 {{html}}
547 #set ($countryPickerParams = {
548 'name': 'country',
549 'value': 'ro'
550 })
551 #countryPicker($countryPickerParams)
552 {{/html}}
553 {{/velocity}}
554 {{/code}}
Sergiu Dumitriu 48.1 555
Marius Dumitru Florea 67.1 556 </div>
557 </div>
558 </div>
559 {{/html}}
560
561 The remote data should have the following JSON format:
562
563 {{code language="js"}}
564 [
565 {
566 'label': '...',
567 'value': '...',
568 'url': '...',
569 'icon': {
570 'iconSetName': '...',
571 'iconSetType': 'IMAGE or FONT',
572 'cssClass': 'for font icons',
573 'url': 'for image icons'
574 },
575 'hint': '...'
576 },
577 ...
578 ]
579 {{/code}}

Get Connected