Browse Source

updating heartbeat.js

logicp 7 years ago
parent
commit
ea365a86b1
1 changed files with 213 additions and 218 deletions
  1. 213 218
      js/heartbeat.js

+ 213 - 218
js/heartbeat.js

@@ -4,6 +4,193 @@
 
 (function($, Drupal, drupalSettings) {
 
+  const flagListen = function(e) {
+
+    let stringWithHid = e.srcElement.href.substring(0, e.srcElement.href.indexOf('?destination'));
+    let hid = stringWithHid.substring(stringWithHid.lastIndexOf('/') + 1);
+    let flagId = stringWithHid.substring(stringWithHid.lastIndexOf('/flag/'), stringWithHid.lastIndexOf(hid) - 1);
+
+    flagId = flagId.substring(flagId.lastIndexOf('/') + 1);
+
+    $.ajax({
+      type: 'POST',
+      url:'/heartbeat/userflaggings',
+      data: {
+        entity_id: hid,
+        entity_type: 'heartbeat',
+        flag_id: flagId,
+        uid: drupalSettings.user.uid
+      },
+      success: function(response) {
+        console.dir(response);
+      }
+    });
+  };
+
+  function updateFeed() {
+    $.ajax({
+      type: 'POST',
+      url: '/heartbeat/form/heartbeat_update_feed',
+      success: function (response) {
+
+      }
+    })
+  }
+
+  function listenImages() {
+    let cboxOptions = { maxWidth: '960px', maxHeight: '960px' };
+    let images = document.querySelectorAll('.heartbeat-content img');
+
+    console.dir(images);
+
+    $('.heartbeat-content').find('img').each(function () {
+      let parentClass = $(this).parent().prop('className');
+      let phid = parentClass.substring(parentClass.indexOf('hid') + 4);
+      $(this).colorbox({rel: phid, href: $(this).attr('src'), cboxOptions});
+    });
+  }
+
+  function listenCommentPost() {
+    let comments = document.querySelectorAll('[data-drupal-selector]');
+
+    for (let i = 0; i < comments.length; i++) {
+      let comment = comments[i];
+      comment.addEventListener('click', function () {
+        getParent(comment);
+      })
+    }
+  }
+
+  function commentFormListeners() {
+    console.log('Comment Form Listeners');
+    let cFormButtons = document.querySelectorAll('.heartbeat-comment-button');
+
+    for (let b = 0; b < cFormButtons.length; b++) {
+      cFormButtons[b].removeEventListener('click', commentListen);
+      cFormButtons[b].addEventListener('click', commentListen);
+    }
+  }
+
+  function flagListeners() {
+
+    if (arguments[0] !== null && arguments.constructor !== Array) {
+      //noinspection JSValidateTypes
+      arguments = [arguments[0]];
+    }
+
+    let flags = arguments[0] == undefined ? document.querySelectorAll('.flag .use-ajax') : arguments[0];
+    console.log("Reloading FLAG LISTENERS");
+
+    if (flags.constructor === NodeList) {
+      for (let f = 0; f < flags.length; f++) {
+        flags[f].removeEventListener('click', flagListen);
+        flags[f].addEventListener('click', flagListen);
+      }
+    } else if (flags.constructor === Array && (flags[0].nodeType !== null && flags[0].nodeType > 0)) {
+      flags.forEach(function(node) {
+        node.removeEventListener('click', flagListen);
+        node.addEventListener('click', flagListen);
+      });
+    } else if (flags.nodeName !== null && flags.nodeName !== undefined) {
+      flags.removeEventListener('click', flagListen);
+      flags.addEventListener('click', flagListen);
+    } else {
+      console.debug('FlagListen called with no flags available');
+    }
+  }
+
+  function getParent(node) {
+    if (node != null && node != undefined && node.classList != undefined && node.classList.contains('heartbeat-comment')) {
+      let id = node.id.substr(node.id.indexOf('-') + 1);
+      $.ajax({
+        type: 'POST',
+        url: '/heartbeat/commentupdate/' + id,
+        success: function (response) {
+        }
+      });
+    } else {
+      if (node != null && node.nodeName !== 'body') {
+        getParent(node.parentNode);
+      }
+    }
+  }
+
+  function getScrollXY() {
+    let scrOfX = 0, scrOfY = 0;
+    if (typeof( window.pageYOffset ) == 'number') {
+
+      scrOfY = window.pageYOffset;
+      scrOfX = window.pageXOffset;
+    } else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
+
+      scrOfY = document.body.scrollTop;
+      scrOfX = document.body.scrollLeft;
+    } else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
+
+      scrOfY = document.documentElement.scrollTop;
+      scrOfX = document.documentElement.scrollLeft;
+    }
+    return [scrOfX, scrOfY];
+  }
+
+  //taken from http://james.padolsey.com/javascript/get-document-height-cross-browser/
+  function getDocHeight() {
+    let D = document;
+    return Math.max(
+      D.body.scrollHeight, D.documentElement.scrollHeight,
+      D.body.offsetHeight, D.documentElement.offsetHeight,
+      D.body.clientHeight, D.documentElement.clientHeight
+    );
+  }
+
+  Drupal.AjaxCommands.prototype.selectFeed = function (ajax, response, status) {
+    $.ajax({
+      type: 'POST',
+      url: '/heartbeat/render_feed/' + response.feed,
+      success: function (response) {
+        feedElement = document.querySelector('.heartbeat-stream');
+
+        if (feedElement != null) {
+          feedElement.innerHTML = response;
+        } else {
+          feedBlock = document.getElementById('block-heartbeatblock');
+          insertNode = document.createElement('div');
+          insertNode.innerHTML = response;
+          feedBlock.appendChild(insertNode);
+        }
+      }
+    });
+  };
+
+  Drupal.AjaxCommands.prototype.updateFeed = function (ajax, response, status) {
+    if (response.update) {
+      $.ajax({
+        type: 'POST',
+        url: '/heartbeat/update_feed/' + response.timestamp,
+        success: function (response) {
+
+        }
+      });
+    }
+  };
+
+  Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function (ajax, response, status) {
+    console.dir(response);
+    if (response.cid) {
+      console.log('this shit is getting called again');
+      let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
+      let text = parentComment.querySelector('.form-textarea');
+
+      text.addEventListener('keydown', function (e) {
+        console.dir(e);
+        if (e.keyCode === 13) {
+          let submitBtn = parentComment.querySelector('.form-submit');
+          submitBtn.click();
+        }
+      });
+    }
+  };
+
   function hideCommentForms() {
     let forms = document.querySelectorAll('.heartbeat-comment-form .js-form-type-textarea, .heartbeat-comment-form .form-submit');
 
@@ -80,117 +267,40 @@
     }
   };
 
+  Drupal.behaviors.heartbeat = {
+    attach: function (context, settings) {
 
-  $(document).ready(function() {
-
+      if (drupalSettings.friendData != null) {
+        let divs = document.querySelectorAll('.flag-friendship a.use-ajax');
 
-    const loader = document.createElement('div');
-    loader.id = 'heartbeat-loader';
-    const body = document.getElementsByTagName('body')[0];
-    body.appendChild(loader);
-
-    Drupal.behaviors.heartbeat = {
-      attach: function (context, settings) {
-
-        if (drupalSettings.friendData != null) {
-          let divs = document.querySelectorAll('.flag-friendship a.use-ajax');
-
-          for (let i = 0; i < divs.length; i++) {
-            let anchor = divs[i];
-            let userId = anchor.href.substring(anchor.href.indexOf('friendship') + 11, anchor.href.indexOf('?destination'));
-            JSON.parse(drupalSettings.friendData).forEach(function (friendship) {
-              if (friendship.uid_target === userId && friendship.uid == drupalSettings.user.uid && friendship.status == 0) {
-                anchor.innerHTML = 'Friendship Pending';
-              }
-            });
-          }
-        }
-
-        feedElement = document.querySelector('.heartbeat-stream');
-
-        if (drupalSettings.feedUpdate == true) {
-          updateFeed();
-        }
-
-        Drupal.AjaxCommands.prototype.selectFeed = function (ajax, response, status) {
-
-          $.ajax({
-            type: 'POST',
-            url: '/heartbeat/render_feed/' + response.feed,
-            success: function (response) {
-              feedElement = document.querySelector('.heartbeat-stream');
-
-              if (feedElement != null) {
-                feedElement.innerHTML = response;
-              } else {
-                feedBlock = document.getElementById('block-heartbeatblock');
-                insertNode = document.createElement('div');
-                insertNode.innerHTML = response;
-                feedBlock.appendChild(insertNode);
-              }
+        for (let i = 0; i < divs.length; i++) {
+          let anchor = divs[i];
+          let userId = anchor.href.substring(anchor.href.indexOf('friendship') + 11, anchor.href.indexOf('?destination'));
+          JSON.parse(drupalSettings.friendData).forEach(function (friendship) {
+            if (friendship.uid_target === userId && friendship.uid == drupalSettings.user.uid && friendship.status == 0) {
+              anchor.innerHTML = 'Friendship Pending';
             }
           });
-        };
-
-        Drupal.AjaxCommands.prototype.updateFeed = function (ajax, response, status) {
-          if (response.update) {
-            $.ajax({
-              type: 'POST',
-              url: '/heartbeat/update_feed/' + response.timestamp,
-              success: function (response) {
+        }
+      }
+      listenImages();
+      listenCommentPost();
+      feedElement = document.querySelector('.heartbeat-stream');
 
-              }
-            });
-          }
-        };
-
-        listenImages();
-        listenCommentPost();
-
-        Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function (ajax, response, status) {
-          console.dir(response);
-          if (response.cid) {
-            console.log('this shit is getting called again');
-            let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
-            let text = parentComment.querySelector('.form-textarea');
-
-            text.addEventListener('keydown', function (e) {
-              console.dir(e);
-              if (e.keyCode === 13) {
-                let submitBtn = parentComment.querySelector('.form-submit');
-                submitBtn.click();
-              }
-            });
-          }
-        };
+      if (drupalSettings.feedUpdate == true) {
+        updateFeed();
       }
-    };
+    }
+  };
 
-    commentFormListeners();
 
-    const flagListen = function(e) {
-      console.dir(e.srcElement);
-      let stringWithHid = e.srcElement.href.substring(0, e.srcElement.href.indexOf('?destination'));
-      let hid = stringWithHid.substring(stringWithHid.lastIndexOf('/') + 1);
-      let flagId = stringWithHid.substring(stringWithHid.lastIndexOf('/flag/'), stringWithHid.lastIndexOf(hid) - 1);
-      flagId = flagId.substring(flagId.lastIndexOf('/') + 1);
-      console.dir(flagId);
-      console.log(hid);
-      $.ajax({
-        type: 'POST',
-        url:'/heartbeat/userflaggings',
-        data: {
-          entity_id: hid,
-          entity_type: 'heartbeat',
-          flag_id: flagId,
-          uid: drupalSettings.user.uid
-        },
-        success: function(response) {
-          console.dir(response);
-        }
-      });
-    };
+  $(document).ready(function() {
+    const loader = document.createElement('div');
+    loader.id = 'heartbeat-loader';
+    const body = document.getElementsByTagName('body')[0];
+    body.appendChild(loader);
 
+    commentFormListeners();
     flagListeners();
 
     let stream = document.getElementById('block-heartbeatblock');
@@ -224,82 +334,7 @@
       flagObserver.observe(flag, flagObserveConfig);
     });
 
-    function updateFeed() {
-      $.ajax({
-        type: 'POST',
-        url: '/heartbeat/form/heartbeat_update_feed',
-        success: function (response) {
-        }
-      })
-    }
 
-    function listenImages() {
-      let cboxOptions = {
-        maxWidth: '960px',
-        maxHeight: '960px',
-      };
-
-      $('.heartbeat-content').find('img').each(function () {
-        let parentClass = $(this).parent().prop('className');
-        let phid = parentClass.substring(parentClass.indexOf('hid') + 4);
-        $(this).colorbox({rel: phid, href: $(this).attr('src'), cboxOptions});
-      });
-    }
-
-    function listenCommentPost() {
-      let comments = document.querySelectorAll('[data-drupal-selector]');
-
-      for (let i = 0; i < comments.length; i++) {
-        let comment = comments[i];
-        comment.addEventListener('click', function () {
-          getParent(comment);
-        })
-      }
-    }
-
-    function getParent(node) {
-      if (node != null && node != undefined && node.classList != undefined && node.classList.contains('heartbeat-comment')) {
-        let id = node.id.substr(node.id.indexOf('-') + 1);
-        $.ajax({
-          type: 'POST',
-          url: '/heartbeat/commentupdate/' + id,
-          success: function (response) {
-          }
-        });
-      } else {
-        if (node != null && node.nodeName !== 'body') {
-          getParent(node.parentNode);
-        }
-      }
-    }
-
-    function getScrollXY() {
-      var scrOfX = 0, scrOfY = 0;
-      if (typeof( window.pageYOffset ) == 'number') {
-
-        scrOfY = window.pageYOffset;
-        scrOfX = window.pageXOffset;
-      } else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
-
-        scrOfY = document.body.scrollTop;
-        scrOfX = document.body.scrollLeft;
-      } else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
-
-        scrOfY = document.documentElement.scrollTop;
-        scrOfX = document.documentElement.scrollLeft;
-      }
-      return [scrOfX, scrOfY];
-    }
-
-    //taken from http://james.padolsey.com/javascript/get-document-height-cross-browser/
-    function getDocHeight() {
-      var D = document;
-      return Math.max(
-        D.body.scrollHeight, D.documentElement.scrollHeight,
-        D.body.offsetHeight, D.documentElement.offsetHeight,
-        D.body.clientHeight, D.documentElement.clientHeight
-      );
-    }
 
     document.addEventListener("scroll", function (event) {
 
@@ -364,49 +399,9 @@
             $.colorbox().close();
           }
         });
-
         return true;
-
       }
     );
-
-    function commentFormListeners() {
-      console.log('Comment Form Listeners');
-      let cFormButtons = document.querySelectorAll('.heartbeat-comment-button');
-
-
-      for (let b = 0; b < cFormButtons.length; b++) {
-        cFormButtons[b].removeEventListener('click', commentListen);
-        cFormButtons[b].addEventListener('click', commentListen);
-      }
-    }
-
-    function flagListeners() {
-
-      if (arguments[0] !== null && arguments.constructor !== Array) {
-        //noinspection JSValidateTypes
-        arguments = [arguments[0]];
-      }
-
-      let flags = arguments[0] == undefined ? document.querySelectorAll('.flag .use-ajax') : arguments[0];
-      console.log("Reloading FLAG LISTENERS");
-
-      if (flags.constructor === NodeList) {
-        for (let f = 0; f < flags.length; f++) {
-          flags[f].removeEventListener('click', flagListen);
-          flags[f].addEventListener('click', flagListen);
-        }
-      } else if (flags.constructor === Array && (flags[0].nodeType !== null && flags[0].nodeType > 0)) {
-        flags.forEach(function(node) {
-          node.removeEventListener('click', flagListen);
-          node.addEventListener('click', flagListen);
-        });
-      } else if (flags.nodeName !== null && flags.nodeName !== undefined) {
-        flags.removeEventListener('click', flagListen);
-        flags.addEventListener('click', flagListen);
-      } else {
-        console.debug('FlagListen called with no flags available');
-      }
-    }
   })
 })(jQuery, Drupal, drupalSettings);
+