From aee1dfd837562207e8f31d21cbad9c83ff57e86e Mon Sep 17 00:00:00 2001 From: lavigor Date: Sun, 22 Jul 2018 02:06:18 +0300 Subject: [PATCH] [ticket/13713] Introduce priorities for groups PHPBB3-13713 --- phpBB/assets/javascript/editor.js | 36 +++++++++---------- phpBB/phpbb/mention/source/base_group.php | 10 ++++++ phpBB/phpbb/mention/source/base_user.php | 6 ++-- .../phpbb/mention/source/source_interface.php | 8 +++++ 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/phpBB/assets/javascript/editor.js b/phpBB/assets/javascript/editor.js index b6382304bc..f1931f0bdf 100644 --- a/phpBB/assets/javascript/editor.js +++ b/phpBB/assets/javascript/editor.js @@ -493,7 +493,7 @@ function getCaretPosition(txtarea) { sorter: function(query, items, searchKey) { let i; let len; - let highestPriority = 0; + let highestPriorities = {u: 0, g: 0}; let _unsorted = {u: {}, g: {}}; let _exactMatch = []; let _results = []; @@ -517,8 +517,7 @@ function getCaretPosition(txtarea) { } // If the item hasn't been added yet - add it - // Group names do not have priorities and are also handled here - if (!_unsorted[item.type][item.id] || item.type === 'g') { + if (!_unsorted[item.type][item.id]) { _unsorted[item.type][item.id] = item; continue; } @@ -527,26 +526,23 @@ function getCaretPosition(txtarea) { _unsorted[item.type][item.id].priority += parseFloat(item.priority); // Calculate the highest priority - we'll give it to group names - highestPriority = Math.max(highestPriority, _unsorted[item.type][item.id].priority); + highestPriorities[item.type] = Math.max(highestPriorities[item.type], _unsorted[item.type][item.id].priority); } - // Push user names to the result array - if (_unsorted['u']) { - $.each(_unsorted['u'], function(name, value) { - _results.push(value); - }); - } + // All types of names should come at the same level of importance, + // otherwise they will be unlikely to be shown + // That's why we normalize priorities and push names to a single results array + $.each(['u', 'g'], function(key, type) { + if (_unsorted[type]) { + $.each(_unsorted[type], function(name, value) { + // Normalize priority + value.priority /= highestPriorities[type]; - // Push group names to the result array and give them the highest priority - // They will be sorted together with the usernames on top of the list - if (_unsorted['g']) { - $.each(_unsorted['g'], function(name, value) { - // Groups should come at the same level of importance - // as users, otherwise they will be unlikely to be shown - value.priority = highestPriority; - _results.push(value); - }); - } + // Add item to all results + _results.push(value); + }); + } + }); // Sort names by priorities - higher values come first _results = _results.sort(function(a, b) { diff --git a/phpBB/phpbb/mention/source/base_group.php b/phpBB/phpbb/mention/source/base_group.php index 607fb91cc9..a31071a964 100644 --- a/phpBB/phpbb/mention/source/base_group.php +++ b/phpBB/phpbb/mention/source/base_group.php @@ -107,6 +107,15 @@ abstract class base_group implements source_interface */ abstract protected function query($keyword, $topic_id); + /** + * {@inheritdoc} + */ + public function get_priority($row) + { + // By default every result from the source increases the priority by a fixed value + return 1; + } + /** * {@inheritdoc} */ @@ -141,6 +150,7 @@ abstract class base_group implements source_interface 'img' => phpbb_get_group_avatar($groups[$group_id]), ], 'rank' => $group_rank['title'], + 'priority' => $this->get_priority($groups[$group_id]), ]); } } diff --git a/phpBB/phpbb/mention/source/base_user.php b/phpBB/phpbb/mention/source/base_user.php index c47bb313c7..cb6ae1eaca 100644 --- a/phpBB/phpbb/mention/source/base_user.php +++ b/phpBB/phpbb/mention/source/base_user.php @@ -60,13 +60,11 @@ abstract class base_user implements source_interface abstract protected function query($keyword, $topic_id); /** - * Returns the priority of the currently selected name - * - * @param array $row Array of fetched user data - * @return int Priority (defaults to 1) + * {@inheritdoc} */ public function get_priority($row) { + // By default every result from the source increases the priority by a fixed value return 1; } diff --git a/phpBB/phpbb/mention/source/source_interface.php b/phpBB/phpbb/mention/source/source_interface.php index 7c7da7369f..731aedc763 100644 --- a/phpBB/phpbb/mention/source/source_interface.php +++ b/phpBB/phpbb/mention/source/source_interface.php @@ -24,4 +24,12 @@ interface source_interface * @param int $topic_id Current topic ID */ public function get(array &$names, $keyword, $topic_id); + + /** + * Returns the priority of the currently selected name + * + * @param array $row Array of fetched data for the name type (e.g. user row) + * @return int Priority (defaults to 1) + */ + public function get_priority($row); }