Wiki source code of Class Picker (Velocity Macro)

Last modified by Eleni Cojocariu on 2026/11/25 11:29

Show last authors
1 The ##classPicker## Velocity macro outputs a form field for picking one or more [[XWiki Classes>>doc:documentation.xs.dev.data-model.x-classes.WebHome]]. It renders a ##select## element that the suggest widget turns into a searchable drop-down list. The class and object editors use it to switch to another class and to choose the class of a new object.
2
3 == Signature ==
4
5 {{code language="none"}}
6 #classPicker($parameters)
7 {{/code}}
8
9 ##$parameters## is a map. It is optional: an empty or missing map gives a single-selection picker with no name. The macro adds entries to the map you pass, so pass a new map on each call.
10
11 == Parameters ==
12
13 The drop-down list is a [[Tom Select>>https://tom-select.js.org/docs/]] widget.
14
15 Most entries become HTML attributes of the ##select## element. A value that is not a String is serialized as JSON.
16
17 |=Parameter|=Type|=Default|=What it does
18 |##name##|String|None|Name of the form field. The form submits the selected classes under this name.
19 |##id##|String|None|HTML ##id## of the ##select## element.
20 |##value##|String or List of Strings|None|Classes selected when the field is displayed. Each value is a class page reference, resolved relative to the current page. When ##multiple## is set, a String value is split with ##separators##.
21 |##multiple##|String|None|Set to ##multiple## to allow several classes. The macro then also outputs a hidden empty field with the same ##name##, so that removing every class submits an empty value.
22 |##separators##|String|##~|,##|Characters that split a String ##value## when ##multiple## is set. Not output as an attribute.
23 |##placeholder##|String|None|Text shown while no class is selected. The macro outputs it as an empty first option, not as an attribute.
24 |##title##|String|"Pick a class." or, with ##multiple##, "Pick classes."|Tooltip of the field. The defaults are the translations of ##core.widgets.suggest.classPicker.single.title## and ##core.widgets.suggest.classPicker.multi.title##.
25 |##class##|String|Empty|Extra CSS classes. The macro always appends ##xwiki-selectize suggest-classes##.
26 |##data-xwiki-selectize##|Map|##{'maxOptions': null}##|Tom Select settings for the drop-down list. The macro sets ##maxOptions## to ##null## when you don't, so the list shows every class instead of the first 50. Callers in XWiki also use ##takeInputWidth##, ##copyClassesToDropdown##, ##dropdownAlignment## and ##dropdownParent##.
27 |##size##|Number|##1##|Always ##1##: the macro overwrites any value you pass.
28 |Any other key|String or JSON-serializable value|None|Output as an attribute of the ##select## element, for example ##disabled## or a ##data-*## attribute.
29
30 == Displayed Classes ==
31
32 The drop-down list shows every class of the current wiki that the current user can view, plus the classes of ##value##. Classes are grouped by location: each group is labeled with the space path of its classes, such as ##XWiki## or ##Help / Applications / Movies / Code##. Each option shows the plain title of the class page. Typing in the field filters the list.
33
34 {{image reference="class-picker-drop-down-list.png" size="large" alt="The class picker with its drop-down list open, showing classes grouped under their location, such as Help / Applications / Movies / Code"/}}
35
36 == Submitted Value ==
37
38 The field is a standard ##select## element, so the form submits it like any other field. The value of each selected class is its local page reference, without the wiki: ##Help.Applications.Movies.Code.MoviesClass##, for instance. With ##multiple##, the form submits one ##name## parameter per selected class, plus the empty one from the hidden field. The field also fires a ##change## event when the selection changes.
39
40 == Required Resources ==
41
42 The macro loads its resources itself, through ##~#classPicker_import##. Call ##~#classPicker_import## alone when you write the ##select## element yourself.
43
44 * ##~#classPicker_import## loads ##uicomponents/suggest/xwiki.selectize.js## through the ##skin## action, because the file contains Velocity code.
45 * That script defines the ##xwiki-selectize## RequireJS module. The module loads Tom Select and two stylesheets: ##tom-select.bootstrap4.min.css## and ##uicomponents/suggest/xwiki.selectize.css##.
46 * The script activates every element with the ##xwiki-selectize## CSS class when the page loads and on each ##xwiki:dom:updated## event. Fire this event after you insert a picker with JavaScript.
47 * The ##suggest-classes## styles (a 400px high drop-down list, an inline field) live in ##js/xwiki/editors/dataeditors.css##. Only the class and object editors load this file, so load it with ##$xwiki.ssfx.use## if you want the same look.
48
49 == Example ==
50
51 The object editor displays its "Select a Class" field with the following parameters. Wrap them in the ##velocity## and ##html## macros to use them in a wiki page:
52
53 {{code language="none"}}
54 {{velocity}}
55 {{html}}
56 #set ($classPickerParameters = {
57 'id': 'classname',
58 'name': 'classname',
59 'placeholder': $services.localization.render('core.editors.object.add.selectClass'),
60 'data-xwiki-selectize': {
61 'copyClassesToDropdown': true,
62 'takeInputWidth': true
63 }
64 })
65 #classPicker($classPickerParameters)
66 {{/html}}
67 {{/velocity}}
68 {{/code}}
69
70 The macro outputs the following HTML, shortened to one group and one option, with the attribute values unescaped for readability:
71
72 {{code language="html"}}
73 <select id='classname' name='classname'
74 data-xwiki-selectize='{"copyClassesToDropdown":true,"takeInputWidth":true,"maxOptions":null}'
75 class=' xwiki-selectize suggest-classes' title='Pick a class.' size='1'>
76 <option value="">Select a Class</option>
77 <optgroup label="Help / Applications / Movies / Code">
78 <option value="Help.Applications.Movies.Code.MoviesClass"
79 data-data='{"url":"\/xwiki\/bin\/view\/Help\/Applications\/Movies\/Code\/MoviesClass"}'>
80 Movies Class
81 </option>
82 </optgroup>
83 </select>
84 {{/code}}

Get Connected