Prechádzať zdrojové kódy

Ticket #4369 - New PUSH server integration: Usage in Comments counters.

AntonLV 3 rokov pred
rodič
commit
9f6bf6c993

+ 10 - 10
inc/classes/BxDolCmts.php

@@ -1584,13 +1584,6 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
         bx_alert($this->_sSystem, 'commentPost', $iObjId, $iPerformerId, $aAlertParams);
         bx_alert('comment', 'added', $iCmtId, $iPerformerId, $aAlertParams);
 
-        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
-            $oSockets->sendEvent($this->getSocketName(), $iObjId, 'comment_added', json_encode([
-                'id' => $iCmtId, 
-                'author_id' => $iPerformerId, 
-                'anchor' => $this->getItemAnchor($iCmtId)
-            ]));
-
         $aAuditParams = $this->_prepareAuditParams($iCmtId, array('comment_author_id' => $aCmt['cmt_author_id'], 'comment_text' => $aCmt['cmt_text']));
         bx_audit($iObjId, $this->_aSystem['module'], '_sys_audit_action_add_comment', $aAuditParams);
 
@@ -1604,13 +1597,20 @@ class BxDolCmts extends BxDolFactory implements iBxDolReplaceable, iBxDolContent
         }
 
         $iCount = (int)$this->getCommentsCountAll(0, true);
-
-        return [
+        $aResult = [
             'id' => $iCmtId, 
             'parent_id' => $iCmtPrntId,
             'count' => $iCount,
-            'countf' => $iCount > 0 ? $this->getCounter() : ''
+            'countf' => $iCount > 0 ? $this->getCounter(['show_script' => false]) : ''
         ];
+
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
+            $oSockets->sendEvent($this->getSocketName(), $iObjId, 'comment_added', json_encode(array_merge($aResult, [
+                'author_id' => $iPerformerId, 
+                'anchor' => $this->getItemAnchor($iCmtId)
+            ])));
+
+        return $aResult;
     }
 
     public function onEditAfter($iCmtId, $aDp = [])

+ 40 - 23
inc/js/classes/BxDolCmts.js

@@ -119,23 +119,7 @@ BxDolCmts.prototype.cmtAfterPostSubmit = function (oCmtForm, oData)
                     oParent.remove();
 
                 //--- Update counter
-                if(oData && oData.count != undefined && parseInt(oData.count) > 0) {
-                    if(oData.countf != undefined && oData.countf.length != 0) {
-                        var oCounter = $this._getCounter(oCmtForm);
-                        if(oCounter && oCounter.length > 0)
-                            oCounter.html(oData.countf);
-                    }
-
-                    var oCounter = $this._getCounter(oCmtForm, true);
-                    if(oCounter && oCounter.length > 0)
-                        oCounter.html(oData.count);
-
-                    var sClassHidden = 'sys-ac-hidden';
-                    if(!oCounter.is('.' + sClassHidden))
-                        oCounter = oCounter.parents('.' + sClassHidden + ':first');
-
-                    oCounter.toggleClass(sClassHidden);
-                }
+                $this.cmtUpdateCounter(oCmtForm, oData);
             }
 
             return;
@@ -209,6 +193,33 @@ BxDolCmts.prototype.cmtAfterEditSubmit = function (oCmtForm, oData, onComplete)
         fContinue();
 };
 
+BxDolCmts.prototype.cmtUpdateCounter = function (oElement, oData)
+{
+    if(oData && (oData.count == undefined || parseInt(oData.count) == 0))
+        return;
+
+    if(oData.countf != undefined && oData.countf.length != 0) {
+        var oCounter = this._getCounter(oElement);
+        if(oCounter && oCounter.length > 0)
+            oCounter.replaceWith(oData.countf);
+    }
+
+    var oCounter = this._getCounter(oElement, true);
+    if(oCounter && oCounter.length > 0)
+        oCounter.html(oData.count);
+
+    var sClassHidden = 'sys-ac-hidden';
+    if(!oCounter.is('.' + sClassHidden))
+        oCounter = oCounter.parents('.' + sClassHidden + ':first');
+
+    oCounter.toggleClass(sClassHidden);
+};
+
+BxDolCmts.prototype.cmtUpdateCounterAs = function (oData)
+{
+    this.cmtUpdateCounter(null, JSON.parse(oData));
+};
+
 BxDolCmts.prototype.cmtPin = function(oLink, iCmtId, iWay, bHideMenu) {
     var $this = this;
 
@@ -760,7 +771,11 @@ BxDolCmts.prototype.showLiveUpdateForSocket = function(sData)
     if(this._iAuthorId == oData.author_id)
         return;
 
-    var oElement = $(this._sRootId + ' .' + this._sSP + '-lu-button:hidden').clone();
+    var sSelector = this._sRootId + ' .' + this._sSP + '-lu-button';
+    if($(sSelector + ':visible').length != 0)
+        return;
+
+    var oElement = $(sSelector + ':hidden').clone();
     if(!oElement || oElement.length == 0)
         return;
 
@@ -1014,12 +1029,14 @@ BxDolCmts.prototype._getCounter = function(oElement, bText)
     var oCounter = null;
     var sSelector = '.' + this._sSP + '-counter' + (bText ? '-text' : '');
 
-    if($(oElement).hasClass(this._sSP))
-        oCounter = $(oElement).find(sSelector);
-    else 
-        oCounter = $(oElement).parents('.' + this._sSP + ':first').find(sSelector);
+    if(oElement) {
+        if($(oElement).hasClass(this._sSP))
+            oCounter = $(oElement).find(sSelector);
+        else 
+            oCounter = $(oElement).parents('.' + this._sSP + ':first').find(sSelector);
+    }
 
-    if(!oCounter.length && this._aHtmlIds['counter'] != undefined) {
+    if((!oCounter || !oCounter.length) && this._aHtmlIds['counter'] != undefined) {
         oCounter = $('#' + this._aHtmlIds['counter']);
         if(!oCounter.is(sSelector))
             oCounter = oCounter.find(sSelector);

+ 2 - 1
template/comment_counter.html

@@ -6,4 +6,5 @@
         </bx_if:show_icon>
         <div class="sys-action-counter-text __style_prefix__-counter-text ml-1.5 first:ml-0">__content__</div>
     </a>
-</div>
+</div>
+__script__

+ 21 - 4
template/scripts/BxBaseCmts.php

@@ -137,13 +137,24 @@ class BxBaseCmts extends BxDolCmts
         return $this->_oTemplate->_wrapInTagJsCode("if(window['" . $this->_sJsObjName . "'] == undefined) var " . $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode($aParams) . "); " . $this->_sJsObjName . ".cmtInit();");
     }
     
-    public function getJsScriptSocket($aBp = [], $aDp = [])
+    public function getJsScriptSocket($sType, $aBp = [], $aDp = [])
     {
         $oSockets = BxDolSockets::getInstance();
         if(!$oSockets->isEnable())
             return '';
 
-        $sCode = $oSockets->getSubscribeJsCode($this->getSocketName(), $this->_iId, 'comment_added', $this->getJsObjectName() . '.showLiveUpdateForSocket(data)');
+        $sJsMethod = '';
+        switch($sType) {
+            case 'counter':
+                $sJsMethod = 'cmtUpdateCounterAs';
+                break;
+
+            case 'live_update':
+                $sJsMethod = 'showLiveUpdateForSocket';
+                break;
+        }
+
+        $sCode = $oSockets->getSubscribeJsCode($this->getSocketName(), $this->_iId, 'comment_added', $this->getJsObjectName() . '.' . $sJsMethod . '(data)');
 
         return $this->_oTemplate->_wrapInTagJsCode($sCode);
     }
@@ -229,7 +240,7 @@ class BxBaseCmts extends BxDolCmts
         $sPostFormBottom = $this->getFormBoxPost($aBp, array_merge($aDp, ['type' => $this->_sDisplayType, 'position' => BX_CMT_PFP_BOTTOM]));
         $sJsContent = $this->getJsScript($aBp, $aDp);
         if($bSockets)
-            $sJsContent .= $this->getJsScriptSocket($aBp, $aDp);
+            $sJsContent .= $this->getJsScriptSocket('live_update', $aBp, $aDp);
 
         $sBlockTitle = _t($this->_aT['block_comments_title'], $this->getCommentsCountAll(0, true));
         $sBlockMenu = $this->_getControlsBox();
@@ -831,6 +842,7 @@ class BxBaseCmts extends BxDolCmts
         $bShowDoCommentAsButton = !$bShowDoCommentAsButtonSmall && isset($aParams['show_do_comment_as_button']) && $aParams['show_do_comment_as_button'] == true;
         $bShowEmpty = isset($aParams['show_counter_empty']) && $aParams['show_counter_empty'] == true;
         $bShowReversed = isset($aParams['show_counter_reversed']) && $aParams['show_counter_reversed'] == true;
+        $bShowScript = !isset($aParams['show_script']) || (bool)$aParams['show_script'] === true;
         $bRecalculateCounter = isset($aParams['recalculate_counter']) && $aParams['recalculate_counter'] == true;
 
         $sClass = 'sys-action-counter';
@@ -876,6 +888,10 @@ class BxBaseCmts extends BxDolCmts
         if(!empty($aParams['overwrite_counter_link_onclick']))
             $sOnclick = $aParams['overwrite_counter_link_onclick'];
 
+        $sJsContent = $this->getJsScript();
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnable())
+            $sJsContent .= $this->getJsScriptSocket('counter');
+
         return $this->_oTemplate->parseHtmlByContent($this->_getTmplCounter(), [
             'style_prefix' => $this->_sStylePrefix,
             'id' => $this->_aHtmlIds['counter'],
@@ -895,7 +911,8 @@ class BxBaseCmts extends BxDolCmts
                     'style_prefix' => $this->_sStylePrefix,
                     'icon' => !empty($aParams['custom_icon']) ? $aParams['custom_icon'] : $this->_oTemplate->getImageAuto('comment|' . $this->_sStylePrefix . '-counter-icon')
                 ]
-            ]
+            ],
+            'script' => $sJsContent
         ]);
     }