Skip to content

XWIKI-22540: Slow loading time when changing the icons theme to Silk in the Icon picker #3537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
96d740e
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Oct 2, 2024
2392ffa
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Oct 3, 2024
fd1505e
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Oct 4, 2024
b50e558
Merge branch 'master' into XWIKI-22540
Sereza7 Nov 27, 2024
cdc937f
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Dec 9, 2024
0053841
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Dec 9, 2024
53b88e0
Merge branch 'xwiki:master' into XWIKI-22540
Sereza7 Jan 10, 2025
3642613
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 10, 2025
1546dc7
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
ec97807
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
4622902
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
127d585
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
6663d3c
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
a0087f8
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
8a7c415
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
4b1f7e1
XWIKI-22540: Slow loading time when changing the icons theme to Silk …
Sereza7 Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
#set ($discard = $map.put('currentIconTheme', $services.icon.currentIconSetName))
#jsonResponse($map)
###########################
## DATA: ICON_AMOUNT
## DATA: ICON COUNT
###########################
#elseif ($request.action == 'data_iconamount')
#elseif ($request.action == 'data_icon_count')
#set ($iconTheme = $request.iconTheme)
#set ($xwikiIcons = $collectiontool.sort($services.icon.getIconNames($iconTheme)))
#set ($iconNamePrefix = $request.query.toLowerCase())
#set ($total = 0)
#foreach ($xwikiIcon in $xwikiIcons)
#if ("$!iconNamePrefix" == '' || $xwikiIcon.startsWith($iconNamePrefix))
#if ("$!iconNamePrefix" == '' || $stringtool.startsWithIgnoreCase($xwikiIcon, $iconNamePrefix))
#set ($total = $total + 1)
#end
#end
Expand All @@ -68,21 +68,27 @@
#set ($xwikiIcons = $collectiontool.sort($services.icon.getIconNames($iconTheme)))
#set ($iconNamePrefix = $request.query.toLowerCase())
#set ($offset = $numbertool.toNumber($request.offset).intValue())
#set ($number = $numbertool.toNumber($request.number).intValue())
#if (!$offset || $offset < 0)
#set($offset = 0)
#set ($limit = $numbertool.toNumber($request.limit).intValue())
#if (!$offset || $offset &lt; 0)
#set ($offset = 0)
#end
#foreach ($xwikiIcon in $xwikiIcons)
#if ("$!iconNamePrefix" == '' || $xwikiIcon.startsWith($iconNamePrefix))
#if (($foreach.count >= $offset) && (!$number || $foreach.count < $offset + $number))
#if ("$!iconNamePrefix" == '' || $stringtool.startsWithIgnoreCase($xwikiIcon, $iconNamePrefix))
## We compare $foreach.count - 1 to our values since foreach.count is 1 based index (!= standard 0 based index)
## We want to make sure we retrieve only the right values.
#if (($foreach.count - 1 &gt;= $offset) &amp;&amp; (!$limit || $foreach.count - 1 &lt; $offset + $limit))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if (($foreach.count - 1 &gt;= $offset) &amp;&amp; (!$limit || $foreach.count - 1 &lt; $offset + $limit))
#if ($foreach.index &gt;= $offset &amp;&amp; (!$limit || $foreach.index &lt; $offset + $limit))

$foreach.index is also available, and it's 0-based, see https://github.com/search?q=repo%3Axwiki%2Fxwiki-platform+%22foreach.index%22+language%3AXML&type=code&l=XML .

#set ($icon = {
'name': $xwikiIcon,
'render': $services.icon.renderHTML($xwikiIcon, $iconTheme)
'render': $services.icon.renderHTML($xwikiIcon, $iconTheme),
'queryString': $iconNamePrefix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used client-side?

})
#if ($request.metadata == 'true')
#set ($icon.metadata = $services.icon.getMetaData($xwikiIcon, $iconTheme))
#end
#set ($discard = $icons.add($icon))
#elseif($limit &amp;&amp; ($foreach.count - 1 &gt;= $offset + $limit))
## We already got all the icons we wanted, we can exit the foreach loop.
#break
#end
#end
#end
Expand Down Expand Up @@ -259,12 +265,13 @@ require(['jquery', 'xwiki-icon-picker'], function($) {
*/
var displayList = function(iconList) {
// Filter the icons we display based on the value of the input field.
let selectedIconName = '';
let selectedIconName = currentInput[0].value;
if (currentInput.data('xwikiIconPickerSettings') !== '') {
// We need to remove the prefix for comparison to UI elements.
selectedIconName = currentInput[0].value.replace(currentInput.data('xwikiIconPickerSettings').prefix, '');
}
// For each icon
iconList.forEach(icon => {
iconList.forEach(icon =&gt; {
// Display the icon
if (selectedIconName === '' || icon.name.includes(selectedIconName)) {
var displayer = $(document.createElement('div'));
Expand All @@ -286,29 +293,45 @@ require(['jquery', 'xwiki-icon-picker'], function($) {
}

/**
* Load the icon list (get the JSON from the server) and display it afterwards
* Load part of the icon list (get the JSON from the server) and display it afterwards
*/
var loadIconList = function(iconTheme) {
let pageSize = 20;
let maxItems = 0;
$.getJSON(getResourceURL('data_iconamount', 'iconTheme=' + iconTheme) , function(dataIconAmount) {maxItems =
dataIconAmount});
$.getJSON(getResourceURL('data_icons', 'iconTheme=' + iconTheme + '&number=' + pageSize.toString()),
function(dataIcons) {
// Put the result in the icons map
icons[iconTheme] = dataIcons;
// Display the list
displayList(icons[iconTheme]);
// Hide the spinner
spinner.hide();
});
// Load more items if the user scrolled at the end of the current list and everything is not loaded yet.
iconListSection.off('scroll');
iconListSection.on('scroll', event => {
if ( iconListSection[0].scrollTop + iconListSection[0].clientHeight >= iconListSection[0].scrollHeight) {
console.log('I am on the bottom!');
var pageSize = 20;
var maxItems = -1;
Comment on lines +299 to +300
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var pageSize = 20;
var maxItems = -1;
const pageSize = 20;
let maxItems = -1;

// Recursive function helper. This calls the page to retrieve the icon information, by chunk of pageSize.
// After receiving the icon info, it displays them and calls itself again if the colortheme is not fully loaded.
var partialLoadIcon = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var partialLoadIcon = function () {
function partialLoadIcon() {

let itemCount = icons[iconTheme].length;
if( maxItems === -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if( maxItems === -1) {
if (maxItems === -1) {

// The first call will retrieve the total count of items we'll need to get.
$.getJSON(getResourceURL('data_icon_count', 'iconTheme=' + iconTheme) , function(dataIconCount) {
maxItems = dataIconCount
});
}
});
$.getJSON(getResourceURL('data_icons', 'iconTheme=' + iconTheme
+ '&amp;offset=' + itemCount.toString()
+ '&amp;limit=' + pageSize.toString()),
function(dataIcons) {
// Put the result in the icons map
icons[iconTheme].push.apply(icons[iconTheme], dataIcons);
// Display the list of new icons.
// Since this recursive calls can take a while on poor network, we want to make sure we only display the newly
// retrieved icons if the iconTheme input still has their value.
if (iconTheme === currentIconTheme) {
displayList(dataIcons);
}
// Whatever the value of the current icon theme, we finish loading this one so that we have a correct state
// if the user ever comes back to it.
if (itemCount + pageSize &lt; maxItems) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no guarantee that maxItems was updated by the first request when this code is called. You have two HTTP requests sent one after another, but there's no guarantee that the response will come in the order the requests were made. It will happen most of the time, but when it doesn't the icon theme will remain partially loaded. I think you should wait for the icon count to be retrieved before starting to fetch the icons (e.g. make partialLoadIcon async and use await), or you could also always include the total count in the response that returns the icons.

partialLoadIcon(maxItems);
} else if (iconTheme === currentIconTheme) {
// All icons have been loaded and the user didn't change the icon theme in the meantime.
// We can finalize the rendering and hide the spinner.
spinner.hide();
}
});
};
partialLoadIcon();
}

/**
Expand Down Expand Up @@ -356,6 +379,8 @@ require(['jquery', 'xwiki-icon-picker'], function($) {
currentIconTheme = iconThemeSelector.val();
// Remove all the displayed icons
$('.xwikiIconPickerIcon').remove();
// And reset the state of the loading spinner.
spinner.hide();
// Display the new ones
if (icons[currentIconTheme].length == 0) {
// if the icon theme has not already been loaded, load it
Expand Down