Changes for page Icon Picker

Last modified by superadmin on 2025/05/07 14:14

From version 3.1
edited by Thomas Coelho (local)
on 2023/09/25 10:21
Change comment: Install extension [org.xwiki.platform:xwiki-platform-icon-ui/15.7]
To version 7.1
edited by superadmin
on 2025/05/07 14:14
Change comment: Install extension [org.xwiki.platform:xwiki-platform-icon-ui/17.3.0]

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.coelho
1 +XWiki.superadmin
Content
... ... @@ -1,28 +1,28 @@
1 1  {{velocity}}
2 2  ###########################
3 -## GLOBALS
4 -###########################
5 -#set($xwikiIcons = ['home', 'wiki', 'space', 'page', 'check', 'add', 'anchor', 'terminal', 'list', 'branch', 'down', 'up', 'left', 'right', 'arrows', 'repeat', 'undo', 'refresh', 'rotate-left', 'rotate-right', 'switch', 'random', 'attach', 'shopping-cart', 'bell', 'trash', 'bomb', 'book', 'cube', 'cubes', 'briefcase', 'bug', 'building', 'caret-down', 'caret-up', 'caret-right', 'calculator', 'calendar', 'camera', 'remove', 'cross', 'car', 'truck', 'chart-bar', 'chart-organisation', 'cloud', 'clock', 'cog', 'comment', 'comments', 'desktop', 'contrast', 'eject', 'step-forward', 'step-backward', 'fast-forward', 'backward', 'play', 'pause', 'stop', 'gamepad', 'credit-card', 'coffee', 'cut', 'database', 'delete', 'floppy-disk', 'glass', 'drive', 'envelope', 'warning', 'error', 'info', 'eye', 'rss', 'female', 'male', 'film', 'flag', 'search', 'search-plus', 'search-minus', 'folder', 'user', 'group', 'heart', 'question', 'image', 'key', 'keyboard', 'lightbulb', 'link', 'unlink', 'lock', 'unlock', 'money', 'dollar', 'euro', 'gbp', 'yen', 'music', 'file', 'file-white', 'file-pdf', 'file-code', 'file-archive', 'file-word', 'file-excel', 'file-powerpoint', 'file-text', 'paste', 'pencil', 'print', 'shield', 'certificate', 'volume-up', 'volume-down', 'volume-off', 'soccer', 'star', 'table', 'phone', 'font', 'bold', 'italic', 'strikethrough', 'subscript', 'superscript', 'underline', 'align-center', 'align-justify', 'align-left', 'align-right', 'columns', 'indent-left', 'indent-right', 'list-bullets', 'list-numbers', 'sun', 'world', 'wrench'])
6 -###########################
7 7  ## DATA: ICON THEMES
8 8  ###########################
9 -#if($request.action == 'data_iconthemes')
10 - #set($map = {})
11 - #set($discard = $map.put('iconThemes', $services.icon.iconSetNames))
12 - #set($discard = $map.put('currentIconTheme', $services.icon.currentIconSetName))
5 +#if ($request.action == 'data_iconthemes')
6 + #set ($map = {})
7 + #set ($discard = $map.put('iconThemes', $services.icon.iconSetNames))
8 + #set ($discard = $map.put('currentIconTheme', $services.icon.currentIconSetName))
13 13   #jsonResponse($map)
14 14  ###########################
15 15  ## DATA: ICONS
16 16  ###########################
17 -#elseif($request.action == 'data_icons')
18 - #set($icons = [])
19 - #set($iconTheme = $request.iconTheme)
20 - #foreach($xwikiIcon in $xwikiIcons)
21 - #set($icon = {})
22 - #set($discard = $icon.put('name', $xwikiIcon))
23 - #set($discard = $icon.put('render', $services.icon.renderHTML($xwikiIcon, $iconTheme)))
24 - #set($discard = $icon.put('metadata', $services.icon.getMetaData($xwikiIcon, $iconTheme)))
25 - #set($discard = $icons.add($icon))
13 +#elseif ($request.action == 'data_icons')
14 + #set ($icons = [])
15 + #set ($iconTheme = $request.iconTheme)
16 + #set ($xwikiIcons = $collectiontool.sort($services.icon.getIconNames($iconTheme)))
17 + #set ($iconNamePrefix = $request.query.toLowerCase())
18 + #foreach ($xwikiIcon in $xwikiIcons)
19 + #if ("$!iconNamePrefix" == '' || $xwikiIcon.startsWith($iconNamePrefix))
20 + #set ($discard = $icons.add({
21 + 'name': $xwikiIcon,
22 + 'render': $services.icon.renderHTML($xwikiIcon, $iconTheme),
23 + 'metadata': $services.icon.getMetaData($xwikiIcon, $iconTheme)
24 + }))
25 + #end
26 26   #end
27 27   #jsonResponse($icons)
28 28  #else
XWiki.JavaScriptExtension[0]
Code
... ... @@ -55,23 +55,37 @@
55 55   * Display the list of icons
56 56   */
57 57   var displayList = function(iconTheme) {
58 + // Filter the icons we display based on the value of the input field.
59 + let selectedIconName = '';
60 + if (currentInput.data('xwikiIconPickerSettings') !== '') {
61 + selectedIconName = currentInput[0].value.replace(currentInput.data('xwikiIconPickerSettings').prefix, '');
62 + }
58 58   // For each icon
59 59   for (var i=0; i < iconTheme.length; ++i) {
60 60   // Display the icon
61 - var displayer = $(document.createElement('div'));
62 - iconListSection.append(displayer);
63 - displayer.addClass('xwikiIconPickerIcon');
64 - var imageDiv = $(document.createElement('div'));
65 - imageDiv.addClass('xwikiIconPickerIconImage').html(iconTheme[i].render);
66 - displayer.append(imageDiv);
67 - var iconNameDiv = $(document.createElement('div'));
68 - iconNameDiv.addClass('xwikiIconPickerIconName').text(iconTheme[i].name);
69 - displayer.append(iconNameDiv);
70 - // Change the input value when the icon is clicked
71 - displayer.on('click', function(event) {
72 - currentInput.val(currentInput.data('xwikiIconPickerSettings').prefix + $(event.currentTarget).children('.xwikiIconPickerIconName').text() );
73 - closePicker();
74 - });
66 + if (selectedIconName === '' || iconTheme[i].name.includes(selectedIconName)) {
67 + var displayer = $(document.createElement('button'));
68 + displayer.addClass('btn');
69 + iconListSection.append(displayer);
70 + displayer.addClass('xwikiIconPickerIcon');
71 + var imageDiv = $(document.createElement('p'));
72 + imageDiv.addClass('xwikiIconPickerIconImage').html(iconTheme[i].render);
73 + displayer.append(imageDiv);
74 + var iconNameSpan = $(document.createElement('p'));
75 + // Usually, the icon name is just one "word" in snakecase. e.g. arrow_refresh_small
76 + // We add Line Break Opportunity elements in the middle of this word so that it's cut into nice to read
77 + // substrings.
78 + let iconNameWithLineBreakOpportunities = iconTheme[i].name
79 + .replaceAll("-","<wbr/>-")
80 + .replaceAll("_","<wbr/>_");
81 + iconNameSpan.addClass('xwikiIconPickerIconName').html(iconNameWithLineBreakOpportunities);
82 + displayer.append(iconNameSpan);
83 + // Change the input value when the icon is clicked
84 + displayer.on('click', function(event) {
85 + currentInput.val(currentInput.data('xwikiIconPickerSettings').prefix + $(event.currentTarget).children('.xwikiIconPickerIconName').text());
86 + closePicker();
87 + });
88 + }
75 75   }
76 76   }
77 77  
... ... @@ -79,7 +79,7 @@
79 79   * Load the icon list (get the JSON from the server) and display it afterwards
80 80   */
81 81   var loadIconList = function(iconTheme) {
82 - $.getJSON(getResourceURL('data_icons', 'iconTheme='+iconTheme), function(dataIcons) {
96 + $.getJSON(getResourceURL('data_icons', 'iconTheme=' + iconTheme), function(dataIcons) {
83 83   // Put the result in the icons map
84 84   icons[iconTheme] = dataIcons;
85 85   // Display the list
... ... @@ -187,7 +187,7 @@
187 187   }
188 188  
189 189   /**
190 - * Create the piccker
204 + * Create the picker
191 191   */
192 192   var createPicker = function (input) {
193 193   // If the picker already exists
... ... @@ -206,7 +206,7 @@
206 206   xwikiCurrentIconsPicker = $(document.createElement('div'));
207 207   xwikiCurrentIconsPicker.addClass('xwikiIconPickerContainer');
208 208   // Insert the picker in the DOM
209 - $(body).append(xwikiCurrentIconsPicker);
223 + xwikiCurrentIconsPicker.insertAfter(currentInput);
210 210   // Set the position
211 211   setPickerPosition();
212 212   // Add the icon list section
... ... @@ -242,13 +242,32 @@
242 242   loadIconList(currentIconTheme);
243 243   }
244 244   }
245 - // Close the picker when the user press 'escape'.
259 + var lastTimeout = 0;
260 + var reloadIconList = function () {
261 + // Remove all the displayed icons
262 + $('.xwikiIconPickerIcon').remove();
263 + // Display the new ones. We always reload because the filter results are unrelated.
264 + displayList(icons[currentIconTheme]);
265 + };
246 246   currentInput.on('keyup', function(event) {
267 + // Close the picker when the user press 'escape'.
247 247   // See: http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery
248 248   if (event.which == 27) {
249 249   closePicker();
271 + } else {
272 + if (lastTimeout!==0) {
273 + clearTimeout(lastTimeout);
274 + }
275 + lastTimeout = setTimeout(reloadIconList, 500);
250 250   }
251 251   });
278 + iconThemeSelector.on('keyup', function(event) {
279 + // Close the picker when the user press 'escape'.
280 + // See: http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery
281 + if (event.which == 27) {
282 + closePicker();
283 + }
284 + });
252 252   }
253 253  
254 254   /**
XWiki.StyleSheetExtension[0]
Code
... ... @@ -10,22 +10,37 @@
10 10  }
11 11  
12 12  .xwikiIconPickerList {
13 - overflow-y: scroll;
13 + display: grid;
14 + max-width: 100%;
14 14   height: 240px;
16 + overflow-y: scroll;
17 + gap: .3em;
18 + padding: .3em;
19 + /* 4 columns, each a fraction of the whole width */
20 + grid-template-columns: repeat(4, 1fr);
15 15  }
16 16  
23 +/* Avoid stretching on the Loader element. */
24 +.xwikiIconPickerList > img {
25 + object-fit: contain;
26 +}
27 +
17 17  .xwikiIconPickerIcon {
18 - float: left;
19 - height: 80px;
20 - width: 100px;
29 + display: flex;
30 + flex-direction: column;
31 + margin: 0;
32 + padding: 0;
21 21   text-align: center;
22 - border-radius: 4px;
34 + border-radius: 7px;
23 23   vertical-align: middle;
24 - overflow: hidden;
25 - padding: 10px;
26 26   cursor: pointer;
37 + background-color: transparent;
27 27  }
28 28  
40 +.xwikiIconPickerIcon * {
41 + padding: 0;
42 +}
43 +
29 29  .xwikiIconPickerIcon:hover {
30 30   background-color: $theme.highlightColor;
31 31  }
... ... @@ -32,6 +32,7 @@
32 32  
33 33  .xwikiIconPickerIconImage {
34 34   font-size: 24px;
50 + line-height: 1;
35 35   margin: 0;
36 36  }
37 37  
... ... @@ -39,6 +39,12 @@
39 39   width: 24px;
40 40  }
41 41  
58 +.xwikiIconPickerIcon .xwikiIconPickerIconName {
59 + /* Make sure the name wraps onto multiple lines instead of overflowing. */
60 + white-space: pre-wrap;
61 + word-break: break-word;
62 +}
63 +
42 42  .xwikiIconPickerIconThemeSelector {
43 43   width: 100%;
44 44   background-color: $theme.menuBackgroundColor;