Browse Source

Changing reply link listen behaviour
changing rendermessage methods to use new date format
changing feedloading method (heartbeatstreamservices) evoked by anonymous users

logicp 7 years ago
parent
commit
211ca4af2c

+ 39 - 37
js/heartbeat.js

@@ -12,6 +12,41 @@
     }
   }
 
+  /******** Load Login Block **********
+   ******** append to document ********
+   ******** Hover in middle of screen */
+
+  function loginModal() {
+
+    $('#heartbeat-loader').show(225);
+
+    $.ajax({
+      type: 'GET',
+      url: '/user/modal/login',
+      success: function (response) {
+        mainContainer = document.getElementById('main');
+        loginBlock = document.createElement('div');
+        loginBlock.innerHTML = response;
+        loginBlock.className = 'kekistan-login-block';
+        loginBlock.id = 'kekistan-login-block';
+        closeBtn = document.createElement('div');
+        closeBtn.className = 'kekistan-login-block-close';
+        closeBtn.innerHTML = '✖';
+        loginBlock.appendChild(closeBtn);
+        mainContainer.appendChild(loginBlock);
+
+        closeBtn.addEventListener('click', function () {
+          loginBlock.innerHTML = '';
+          mainContainer.removeChild(loginBlock);
+        });
+
+      },
+      complete: function () {
+        $('#heartbeat-loader').hide(225);
+      }
+    });
+  }
+
   function toggleCommentElements(node) {
 
     console.dir(node);
@@ -22,7 +57,7 @@
       node.className += ' comment-form-hidden';
     }
   }
-  
+
   const commentListen = function(e) {
 
     if (drupalSettings.user.uid > 0) {
@@ -166,6 +201,8 @@
       hideCommentForms();
       commentFormListeners();
       flagListeners();
+      listenVideos();
+      listenWindowScroll();
     });
 
     let config = {attributes: true, childList: true, characterData: true};
@@ -175,7 +212,7 @@
     let flagObserver = new MutationObserver(function(mutations) {
       console.dir(mutations);
       if (mutations[0].target !== null && mutations[0].target.children !== null && mutations[0].target.children.length > 0 && mutations[0].target.children[0].classList.contains('flag')) {
-        flagListeners(mutations[0].target.children[0]);
+        flagListeners();
       }
     });
 
@@ -371,40 +408,5 @@
         console.debug('FlagListen called with no flags available');
       }
     }
-
-    /******** Load Login Block **********
-     ******** append to document ********
-     ******** Hover in middle of screen */
-
-    function loginModal() {
-
-      $('#heartbeat-loader').show(225);
-
-      $.ajax({
-        type: 'GET',
-        url: '/user/modal/login',
-        success: function (response) {
-          mainContainer = document.getElementById('main');
-          loginBlock = document.createElement('div');
-          loginBlock.innerHTML = response;
-          loginBlock.className = 'kekistan-login-block';
-          loginBlock.id = 'kekistan-login-block';
-          closeBtn = document.createElement('div');
-          closeBtn.className = 'kekistan-login-block-close';
-          closeBtn.innerHTML = '✖';
-          loginBlock.appendChild(closeBtn);
-          mainContainer.appendChild(loginBlock);
-
-          closeBtn.addEventListener('click', function () {
-            loginBlock.innerHTML = '';
-            mainContainer.removeChild(loginBlock);
-          });
-
-        },
-        complete: function () {
-          $('#heartbeat-loader').hide(225);
-        }
-      });
-    }
   })
 })(jQuery, Drupal, drupalSettings);

+ 1 - 1
src/Form/HeartbeatSubCommentForm.php

@@ -74,7 +74,7 @@ class HeartbeatSubCommentForm extends FormBase {
           $response = new AjaxResponse();
           $response->addCommand(new AppendCommand(
               '#heartbeat-comment-' . $config->get('cid') . ' .sub-comment',
-              '<div class="heartbeat-subcomment" id="sub-comment-' . $comment->id() . '"><span class="comment-owner"><span class="comment-username">' . \Drupal::currentUser()->getAccountName() . '</span>' . render($userview) . '<span class"comment-ago">1 sec ago</span></span><span class="comment-body">' . $commentBody . '</span><span class="sub-comment"><a href="/heartbeat/subcommentrequest/' . $cid . '" class="button button-action use-ajax">Reply</a></span></div>')
+              '<div class="heartbeat-subcomment" id="sub-comment-' . $comment->id() . '"><span class="comment-owner"><span class="comment-username">' . \Drupal::currentUser()->getAccountName() . '</span>' . render($userview) . '<span class"comment-ago">1 sec ago</span></span><span class="comment-body">' . $commentBody . '</span></div>')
           );
           return $response;
         }

+ 5 - 1
src/HeartbeatStreamServices.php

@@ -114,11 +114,14 @@ class HeartbeatStreamServices {
 
     foreach ($this->getAllStreams() as $stream) {
       foreach ($stream->getTypes() as $type) {
-        if (strlen($type->getValue()[0]['target_id']) > 1) {
+        $type = $type->getValue();
+        $type = key($type) === 'target_id' ? $type : $type->getValue()[0];
+        if (strlen($type['target_id']) > 1) {
           $types[] = $type;
         }
       }
     }
+    $cleanTypes = array_column($types, 'target_id');
     return $this->entityTypeManager->getStorage(
       'heartbeat')->loadMultiple(
         $this->entityQuery->get(
@@ -126,6 +129,7 @@ class HeartbeatStreamServices {
           ->condition('status', 1)
           ->condition('type', array_column($types, 'target_id'), 'IN')
           ->sort('created', 'DESC')
+          ->range(0,25)
 
         ->execute());
   }

+ 24 - 19
src/Plugin/Block/HeartbeatBlock.php

@@ -120,13 +120,14 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
    */
   public function build() {
 
+    $messages = array();
+
     if (\Drupal::currentUser()->id() > 0) {
       $myConfig = \Drupal::service('config.factory')->getEditable('heartbeat_feed.settings');
       $friendData = \Drupal::config('heartbeat_friendship.settings')->get('data');
 
       $feed = $myConfig->get('message');
       $uids = null;
-      $messages = array();
 
       $query = Database::getConnection()->select('heartbeat_friendship', 'hf')
         ->fields('hf', ['uid', 'uid_target']);
@@ -158,21 +159,25 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
           $this->renderMessage($messages, $heartbeat);
         }
       }
-
-      return [
-        '#theme' => 'heartbeat_stream',
-        '#messages' => $messages,
-        '#attached' => array(
-          'library' => 'heartbeat/heartbeat',
-          'drupalSettings' => [
-            'activeFeed' => 'jigga',
-            'friendData' => $friendData,
-          ]
-        ),
-        '#cache' => array('max-age' => 0)
-      ];
+    } else {
+      foreach ($this->heartbeatStreamServices->loadAllStreams() as $heartbeat) {
+        $this->renderMessage($messages, $heartbeat);
+      }
     }
-    return null;
+
+    return [
+      '#theme' => 'heartbeat_stream',
+      '#messages' => $messages,
+      '#attached' => array(
+        'library' => 'heartbeat/heartbeat',
+        'drupalSettings' => [
+          'activeFeed' => 'jigga',
+          'friendData' => $friendData,
+        ]
+      ),
+      '#cache' => array('max-age' => 0)
+    ];
+
   }
 
     private function renderMessage(array &$messages, $heartbeat) {
@@ -186,7 +191,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
           $timeago = 'Yesterday at ' . $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_time');
           break;
         case ($diff >= 172800):
-          $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_medium');
+          $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_short');
           break;
       }
 
@@ -250,12 +255,12 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
                 $timeago = 'Yesterday at ' . $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_time');
                 break;
               case ($subDiff >= 172800):
-                $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+                $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
                 break;
             }
 
             $subCommentOwner = user_view($subComment->getOwner(), 'comment');
-            $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+            $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
             $subComments[] = [
               'id' => $subCid,
               'body' => $subComment->get('comment_body')->value,
@@ -278,7 +283,7 @@ class HeartbeatBlock extends BlockBase implements ContainerFactoryPluginInterfac
             $cTimeago = 'Yesterday at ' . $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_time');
             break;
           case ($commentTimeDiff >= 172800):
-            $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_medium');
+            $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_short');
             break;
         }
 

+ 11 - 10
src/Plugin/Block/HeartbeatHashBlock.php

@@ -181,7 +181,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
         $timeago = 'Yesterday at ' . $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_time');
         break;
       case ($diff >= 172800):
-        $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_medium');
+        $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_short');
         break;
     }
 
@@ -245,19 +245,19 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
               $timeago = 'Yesterday at ' . $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_time');
               break;
             case ($subDiff >= 172800):
-              $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+              $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
               break;
           }
 
           $subCommentOwner = user_view($subComment->getOwner(), 'comment');
-          $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+          $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
           $subComments[] = [
             'id' => $subCid,
             'body' => $subComment->get('comment_body')->value,
             'username' => $subComment->getAuthorName(),
             'owner' => $subCommentOwner,
             'timeAgo' => $subCommentTime,
-            'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $subComment, $this->flagService)
+            'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $subComment, $this->flagService)
           ];
 
         }
@@ -273,7 +273,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
           $cTimeago = 'Yesterday at ' . $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_time');
           break;
         case ($commentTimeDiff >= 172800):
-          $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_medium');
+          $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_short');
           break;
       }
 
@@ -283,7 +283,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
         'username' => $comment->getAuthorName(),
         'owner' => $commentOwner,
         'timeAgo' => $cTimeago,
-        'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $comment, $this->flagService),
+        'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $comment, $this->flagService),
         'reply' => $commentLink,
         'subComments' => $subComments
       ];
@@ -291,7 +291,7 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
     }
 
     $form = \Drupal::service('form_builder')->getForm('\Drupal\heartbeat\Form\HeartbeatCommentForm', $heartbeat);
-
+    $commentCount = count($comments);
     $messages[] = array('heartbeat' => $heartbeat->getMessage()->getValue()[0]['value'],
       'userPicture' => $rendered,
       'userId' => $user->id(),
@@ -300,9 +300,10 @@ class HeartbeatHashBlock extends BlockBase implements ContainerFactoryPluginInte
       'userName' => $user->getAccountName(),
       'user' => $userView,
       'commentForm' => $form,
-      'comments' => $comments,
-      'likeFlag' => Heartbeat::flagAjaxMarkup('heartbeat_like', $heartbeat, $this->flagService),
-      'unlikeFlag' => Heartbeat::flagAjaxMarkup('jihad_flag', $heartbeat, $this->flagService)
+      'comments' => array_reverse($comments),
+      'commentCount' => $commentCount > 0 ? $commentCount : '',
+      'likeFlag' => Heartbeat::flagAjaxBuilder('heartbeat_like', $heartbeat, $this->flagService),
+      'unlikeFlag' => Heartbeat::flagAjaxBuilder('jihad_flag', $heartbeat, $this->flagService)
     );
   }
 }

+ 11 - 10
src/Plugin/Block/HeartbeatMoreBlock.php

@@ -176,7 +176,7 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
         $timeago = 'Yesterday at ' . $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_time');
         break;
       case ($diff >= 172800):
-        $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_medium');
+        $timeago = $this->dateFormatter->format($heartbeat->getCreatedTime(), 'heartbeat_short');
         break;
     }
 
@@ -240,19 +240,19 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
               $timeago = 'Yesterday at ' . $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_time');
               break;
             case ($subDiff >= 172800):
-              $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+              $timeago = $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
               break;
           }
 
           $subCommentOwner = user_view($subComment->getOwner(), 'comment');
-          $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_medium');
+          $subCommentTime = $this->timestamp - $subComment->getCreatedTime() < 172800 ? $this->dateFormatter->formatInterval(REQUEST_TIME - $subComment->getCreatedTime()) . ' ago': $this->dateFormatter->format($subComment->getCreatedTime(), 'heartbeat_short');
           $subComments[] = [
             'id' => $subCid,
             'body' => $subComment->get('comment_body')->value,
             'username' => $subComment->getAuthorName(),
             'owner' => $subCommentOwner,
             'timeAgo' => $subCommentTime,
-            'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $subComment, $this->flagService)
+            'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $subComment, $this->flagService)
           ];
 
         }
@@ -268,7 +268,7 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
           $cTimeago = 'Yesterday at ' . $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_time');
           break;
         case ($commentTimeDiff >= 172800):
-          $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_medium');
+          $cTimeago = $this->dateFormatter->format($comment->getCreatedTime(), 'heartbeat_short');
           break;
       }
 
@@ -278,7 +278,7 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
         'username' => $comment->getAuthorName(),
         'owner' => $commentOwner,
         'timeAgo' => $cTimeago,
-        'commentLike' => Heartbeat::flagAjaxMarkup('heartbeat_like_comment', $comment, $this->flagService),
+        'commentLike' => Heartbeat::flagAjaxBuilder('heartbeat_like_comment', $comment, $this->flagService),
         'reply' => $commentLink,
         'subComments' => $subComments
       ];
@@ -286,7 +286,7 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
     }
 
     $form = \Drupal::service('form_builder')->getForm('\Drupal\heartbeat\Form\HeartbeatCommentForm', $heartbeat);
-
+    $commentCount = count($comments);
     $messages[] = array('heartbeat' => $heartbeat->getMessage()->getValue()[0]['value'],
       'userPicture' => $rendered,
       'userId' => $user->id(),
@@ -295,9 +295,10 @@ class HeartbeatMoreBlock extends BlockBase implements ContainerFactoryPluginInte
       'userName' => $user->getAccountName(),
       'user' => $userView,
       'commentForm' => $form,
-      'comments' => $comments,
-      'likeFlag' => Heartbeat::flagAjaxMarkup('heartbeat_like', $heartbeat, $this->flagService),
-      'unlikeFlag' => Heartbeat::flagAjaxMarkup('jihad_flag', $heartbeat, $this->flagService)
+      'comments' => array_reverse($comments),
+      'commentCount' => $commentCount > 0 ? $commentCount : '',
+      'likeFlag' => Heartbeat::flagAjaxBuilder('heartbeat_like', $heartbeat, $this->flagService),
+      'unlikeFlag' => Heartbeat::flagAjaxBuilder('jihad_flag', $heartbeat, $this->flagService)
     );
   }
 }