Use GroupedUserList for reaction details instead of own implementation
authorJoshua Rüsweg <josh@bastelstu.be>
Thu, 27 Sep 2018 20:28:51 +0000 (22:28 +0200)
committerJoshua Rüsweg <josh@bastelstu.be>
Thu, 27 Sep 2018 20:29:01 +0000 (22:29 +0200)
See #2508

com.woltlab.wcf/templates/reactionTabbedUserList.tpl [deleted file]
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Reaction/CountButtons.js
wcfsetup/install/files/lib/data/reaction/ReactionAction.class.php

diff --git a/com.woltlab.wcf/templates/reactionTabbedUserList.tpl b/com.woltlab.wcf/templates/reactionTabbedUserList.tpl
deleted file mode 100644 (file)
index a76d4bb..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<nav class="tabMenu">
-       <ul>
-               {foreach from=$reactions item=reaction}
-                       <li data-reaction-type-id="{$reaction->reactionTypeID}"{if $reactionTypeID == $reaction->reactionTypeID} class="active ui-state-active"{/if}><a>{@$reaction->renderIcon()} {$reaction->getTitle()}</a></li>
-               {/foreach}
-       </ul>
-</nav>
-
-{if !$reactionUserList|empty}
-       <ol class="containerList jsGroupedUserList section">
-               {foreach from=$reactionUserList item=reaction}
-                       {assign var=user value=$reaction->getUserProfile()}
-                       {include file='userListItem'}
-               {/foreach}
-       </ol>
-       
-       <div class="paginationBottom jsPagination"></div>
-{else}
-       <p class="info">{lang}wcf.reactions.summary.noReactions{/lang}</p>
-{/if}
-
-<script data-relocate="true">
-       $(function() {
-               WCF.Language.addObject({
-                       'wcf.user.button.follow': '{lang}wcf.user.button.follow{/lang}',
-                       'wcf.user.button.ignore': '{lang}wcf.user.button.ignore{/lang}',
-                       'wcf.user.button.unfollow': '{lang}wcf.user.button.unfollow{/lang}',
-                       'wcf.user.button.unignore': '{lang}wcf.user.button.unignore{/lang}'
-               });
-               
-               new WCF.User.Action.Follow($('.jsGroupedUserList > li'));
-               new WCF.User.Action.Ignore($('.jsGroupedUserList > li'));
-       });
-</script>
\ No newline at end of file
index c8673d98c176f074467b56363aa081ebf8d259b8..d22ef57b1fc273e2d443d2f043ef4824260ffe69 100644 (file)
@@ -38,9 +38,7 @@ define(
                                }
                                
                                this._containers = new Dictionary();
-                               this._details = new ObjectMap();
                                this._objectType = objectType;
-                               this._cache = new Dictionary();
                                
                                this._options = Core.extend({
                                        // selectors
@@ -90,15 +88,6 @@ define(
                                }
                        },
                        
-                       /**
-                        * Invalidates the cache for the object with the given objectId. 
-                        * 
-                        * @param       {int}   objectId
-                        */
-                       invalidateCache: function(objectId) {
-                               this._cache.delete(objectId);
-                       },
-                       
                        /**
                         * Update the count buttons with the given data. 
                         * 
@@ -150,8 +139,6 @@ define(
                                if (triggerChange) {
                                        DomChangeListener.trigger();
                                }
-                               
-                               this.invalidateCache(objectId);
                        },
                        
                        /**
@@ -183,20 +170,17 @@ define(
                         * @param       {int}            objectId
                         */
                        _initReactionCountButton: function(element, objectId) {
-                               element.addEventListener(WCF_CLICK_EVENT, this._showReactionOverlay.bind(this, elData(element, 'reaction-type-id'), objectId));
+                               element.addEventListener(WCF_CLICK_EVENT, this._showReactionOverlay.bind(this, objectId));
                        },
                        
                        /**
-                        * Shows the reaction overly for a specific object and reaction type
+                        * Shows the reaction overly for a specific object. 
                         *
-                        * @param       {int}        reactionTypeId
                         * @param       {int}        objectId
                         */
-                       _showReactionOverlay: function(reactionTypeId, objectId) {
+                       _showReactionOverlay: function(objectId) {
                                this._currentObjectId = objectId;
-                               this._currentPageNo = 1;
-                               this._currentReactionTypeId = reactionTypeId;
-                               this._showPage();
+                               this._showOverlay();
                        },
                        
                        /**
@@ -204,70 +188,19 @@ define(
                         *
                         * @param       {int}        pageNo
                         */
-                       _showPage: function(pageNo) {
-                               if (pageNo !== undefined) {
-                                       this._currentPageNo = pageNo;
-                               }
+                       _showOverlay: function() {
+                               this._options.parameters.data.containerID = this._objectType + '-' + this._currentObjectId;
+                               this._options.parameters.data.objectID = this._currentObjectId;
+                               this._options.parameters.data.objectType = this._objectType;
                                
-                               if (this._cache.has(this._currentObjectId) && this._cache.get(this._currentObjectId).has(this._currentReactionTypeId)) {
-                                       // validate pageNo
-                                       if (this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('pageCount') !== 0 && (this._currentPageNo < 1 || this._currentPageNo > this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('pageCount'))) {
-                                               throw new RangeError("pageNo must be between 1 and " + this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('pageCount') + " (" + this._currentPageNo + " given).");
-                                       }
-                               }
-                               else {
-                                       // init objectId cache
-                                       if (!this._cache.has(this._currentObjectId)) {
-                                               this._cache.set(this._currentObjectId, new Dictionary());
-                                       }
-                                       
-                                       // init reaction type id cache for objectId
-                                       this._cache.get(this._currentObjectId).set(this._currentReactionTypeId, new Dictionary());
-                               }
-                               
-                               if (this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).has(this._currentPageNo)) {
-                                       var dialog = UiDialog.open(this, this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get(this._currentPageNo));
-                                       UiDialog.setTitle('userReactionOverlay-' + this._objectType, this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('title'));
-                                       
-                                       if (this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('pageCount') > 1) {
-                                               var element = elBySel('.jsPagination', dialog.content);
-                                               if (element !== null) {
-                                                       new UiPagination(element, {
-                                                               activePage: this._currentPageNo,
-                                                               maxPage: this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).get('pageCount'),
-                                                               callbackSwitch: this._showPage.bind(this)
-                                                       });
-                                               }
-                                       }
-                                       
-                                       // init tab menu
-                                       var tabMenu = elBySel('.tabMenu ul', dialog.content);
-                                       var elements = elBySelAll('li', tabMenu);
-                                       for (var i = 0, length = elements.length; i < length; i++) {
-                                               elements[i].addEventListener(WCF_CLICK_EVENT, this._showReactionOverlay.bind(this, elData(elements[i], 'reaction-type-id'), this._currentObjectId));
-                                       }
-                               }
-                               else {
-                                       this._options.parameters.pageNo = this._currentPageNo;
-                                       this._options.parameters.reactionTypeID = this._currentReactionTypeId;
-                                       this._options.parameters.data.containerID = this._currentReactionTypeId;
-                                       this._options.parameters.data.objectID = this._currentObjectId;
-                                       this._options.parameters.data.objectType = this._objectType;
-                                       
-                                       Ajax.api(this, {
-                                               parameters: this._options.parameters
-                                       });
-                               }
+                               Ajax.api(this, {
+                                       parameters: this._options.parameters
+                               });
                        },
                        
                        _ajaxSuccess: function(data) {
-                               if (data.returnValues.pageCount !== undefined) {
-                                       this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).set('pageCount', ~~data.returnValues.pageCount);
-                               }
-                               
-                               this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).set(this._currentPageNo, data.returnValues.template);
-                               this._cache.get(this._currentObjectId).get(this._currentReactionTypeId).set('title', data.returnValues.title);
-                               this._showPage();
+                               UiDialog.open(this, data.returnValues.template);
+                               UiDialog.setTitle('userReactionOverlay-' + this._objectType, data.returnValues.title);
                        },
                        
                        _ajaxSetup: function() {
index ab27badd98bf2175552010e689e4240ecfa8b677..ad1e37fb470e48f8a177828b1830106030c0b840 100644 (file)
@@ -19,6 +19,7 @@ use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\UserInputException;
 use wcf\system\reaction\ReactionHandler;
 use wcf\system\user\activity\point\UserActivityPointHandler;
+use wcf\system\user\GroupedUserList;
 use wcf\system\WCF;
 
 /**
@@ -78,15 +79,6 @@ class ReactionAction extends AbstractDatabaseObjectAction {
                if (!WCF::getSession()->getPermission('user.like.canViewLike')) {
                        throw new PermissionDeniedException();
                }
-               
-               $this->readInteger('reactionTypeID');
-               $this->readInteger('pageNo');
-               
-               $this->reactionType = ReactionTypeCache::getInstance()->getReactionTypeByID($this->parameters['reactionTypeID']); 
-               
-               if ($this->reactionType === null) {
-                       throw new IllegalLinkException();
-               }
        }
        
        /**
@@ -98,24 +90,33 @@ class ReactionAction extends AbstractDatabaseObjectAction {
                $likeList = new ViewableLikeList();
                $likeList->getConditionBuilder()->add('objectID = ?', [$this->parameters['data']['objectID']]);
                $likeList->getConditionBuilder()->add('objectTypeID = ?', [$this->objectType->objectTypeID]);
-               $likeList->getConditionBuilder()->add('reactionTypeID = ?', [$this->reactionType->reactionTypeID]);
-               $likeList->sqlLimit = 25;
-               $likeList->sqlOffset = ($this->parameters['pageNo'] - 1) * 10;
                $likeList->sqlOrderBy = 'time DESC';
-               $pageCount = ceil($likeList->countObjects() / 10);
                $likeList->readObjects();
                
-               WCF::getTPL()->assign([
-                       'reactionUserList' => $likeList->getObjects(),
-                       'reactions' => ReactionTypeCache::getInstance()->getEnabledReactionTypes(), 
-                       'reactionTypeID' => $this->reactionType->reactionTypeID
-               ]);
+               $data = [];
+               foreach ($likeList as $item) {
+                       if ($item->getReactionType()->isDisabled) continue; 
+                       
+                       // we cast the reactionTypeID to a string, so that we can sort the array
+                       if (!isset($data[(string)$item->getReactionType()->reactionTypeID])) {
+                               $data[(string)$item->getReactionType()->reactionTypeID] = new GroupedUserList($item->getReactionType()->getTitle());
+                       }
+                       
+                       $data[(string)$item->getReactionType()->reactionTypeID]->addUserIDs([$item->userID]);
+               }
+               
+               GroupedUserList::loadUsers();
+               
+               uksort($data, function ($a, $b) {
+                       $sortOrderA = ReactionTypeCache::getInstance()->getReactionTypeByID($a)->showOrder;
+                       $sortOrderB = ReactionTypeCache::getInstance()->getReactionTypeByID($b)->showOrder;
+                       
+                       return ($sortOrderA < $sortOrderB) ? -1 : 1;
+                       
+               });
                
                return [
-                       'reactionTypeID' => $this->reactionType->reactionTypeID,
-                       'pageNo' => $this->parameters['pageNo'],
-                       'pageCount' => $pageCount,
-                       'template' => WCF::getTPL()->fetch('reactionTabbedUserList'),
+                       'template' => WCF::getTPL()->fetch('groupedUserList', 'wcf', ['groupedUsers' => $data]),
                        'title' => WCF::getLanguage()->get('wcf.reactions.summary.title')
                ];
        }