heartbeat.original 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Created by logicp on 5/28/17.
  3. */
  4. (function($, Drupal, drupalSettings) {
  5. Drupal.behaviors.heartbeat = {
  6. attach: function (context, settings) {
  7. if (drupalSettings.friendData != null) {
  8. var divs = document.querySelectorAll('.flag-friendship a.use-ajax');
  9. for (let i = 0; i < divs.length; i++) {
  10. let anchor = divs[i];
  11. var userId = anchor.href.substring(anchor.href.indexOf('friendship') + 11, anchor.href.indexOf('?destination'));
  12. JSON.parse(drupalSettings.friendData).forEach(function (friendship) {
  13. if (friendship.uid_target === userId && friendship.uid == drupalSettings.user.uid && friendship.status == 0) {
  14. anchor.innerHTML = 'Friendship Pending';
  15. }
  16. });
  17. }
  18. }
  19. feedElement = document.querySelector('.heartbeat-stream');
  20. if (drupalSettings.feedUpdate == true) {
  21. updateFeed();
  22. }
  23. Drupal.AjaxCommands.prototype.selectFeed = function(ajax, response, status) {
  24. $.ajax({
  25. type:'POST',
  26. url:'/heartbeat/render_feed/' + response.feed,
  27. success: function(response) {
  28. feedElement = document.querySelector('.heartbeat-stream');
  29. if (feedElement != null) {
  30. feedElement.innerHTML = response;
  31. } else {
  32. feedBlock = document.getElementById('block-heartbeatblock');
  33. insertNode = document.createElement('div');
  34. insertNode.innerHTML = response;
  35. feedBlock.appendChild(insertNode);
  36. }
  37. }
  38. });
  39. };
  40. Drupal.AjaxCommands.prototype.updateFeed = function(ajax, response, status) {
  41. if (response.update) {
  42. $.ajax({
  43. type: 'POST',
  44. url:'/heartbeat/update_feed/' + response.timestamp,
  45. success: function(response) {
  46. }
  47. });
  48. }
  49. };
  50. listenImages();
  51. listenCommentPost();
  52. Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function(ajax, response, status) {
  53. console.dir(response);
  54. if (response.cid) {
  55. console.log('this shit is getting called again');
  56. let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
  57. let text = parentComment.querySelector('.form-textarea');
  58. text.addEventListener('keydown', function (e) {
  59. console.dir(e);
  60. if (e.keyCode === 13) {
  61. let submitBtn = parentComment.querySelector('.form-submit');
  62. submitBtn.click();
  63. }
  64. });
  65. }
  66. }
  67. }
  68. };
  69. function updateFeed() {
  70. $.ajax({
  71. type: 'POST',
  72. url: '/heartbeat/form/heartbeat_update_feed',
  73. success: function (response) {
  74. }
  75. })
  76. }
  77. function listenImages() {
  78. let cboxOptions = {
  79. width: '95%',
  80. height: '95%',
  81. maxWidth: '960px',
  82. maxHeight: '960px',
  83. };
  84. $('.heartbeat-content').find('img').each(function() {
  85. $(this).colorbox({href: $(this).attr('src'), cboxOptions});
  86. });
  87. }
  88. function listenCommentPost() {
  89. //TODO is drupal data selector enough? I doubt it.
  90. let comments = document.querySelectorAll('[data-drupal-selector]');
  91. for (let i = 0; i < comments.length; i++) {
  92. let comment = comments[i];
  93. // console.dir(comment);
  94. comment.addEventListener('click', function() {
  95. getParent(comment);
  96. })
  97. }
  98. }
  99. function getParent(node) {
  100. console.dir(node);
  101. if (node.classList.contains('heartbeat-comment')) {
  102. let id = node.id.substr(node.id.indexOf('-') + 1);
  103. $.ajax({
  104. type: 'POST',
  105. url:'/heartbeat/commentupdate/' + id,
  106. success: function(response) {
  107. }
  108. });
  109. } else {
  110. getParent(node.parentNode);
  111. }
  112. }
  113. function getScrollXY() {
  114. var scrOfX = 0, scrOfY = 0;
  115. if( typeof( window.pageYOffset ) == 'number' ) {
  116. //Netscape compliant
  117. scrOfY = window.pageYOffset;
  118. scrOfX = window.pageXOffset;
  119. } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  120. //DOM compliant
  121. scrOfY = document.body.scrollTop;
  122. scrOfX = document.body.scrollLeft;
  123. } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  124. //IE6 standards compliant mode
  125. scrOfY = document.documentElement.scrollTop;
  126. scrOfX = document.documentElement.scrollLeft;
  127. }
  128. return [ scrOfX, scrOfY ];
  129. }
  130. //taken from http://james.padolsey.com/javascript/get-document-height-cross-browser/
  131. function getDocHeight() {
  132. var D = document;
  133. return Math.max(
  134. D.body.scrollHeight, D.documentElement.scrollHeight,
  135. D.body.offsetHeight, D.documentElement.offsetHeight,
  136. D.body.clientHeight, D.documentElement.clientHeight
  137. );
  138. }
  139. document.addEventListener("scroll", function (event) {
  140. if (getDocHeight() == getScrollXY()[1] + window.innerHeight) {
  141. let streams = document.querySelectorAll('.heartbeat-stream');
  142. let stream = streams.length > 1 ? streams[streams.length - 1] : streams[0];
  143. if (stream !== null) {
  144. console.dir(stream);
  145. let lastHeartbeat = stream.lastElementChild;
  146. if (lastHeartbeat !== null) {
  147. let hid = lastHeartbeat.id.substring(lastHeartbeat.id.indexOf('-') + 1);
  148. $.ajax({
  149. type: 'POST',
  150. url: '/heartbeat/update_feed/' + hid,
  151. success: function (response) {
  152. feedBlock = document.getElementById('block-heartbeatblock');
  153. insertNode = document.createElement('div');
  154. insertNode.innerHTML = response;
  155. feedBlock.appendChild(insertNode);
  156. }
  157. });
  158. }
  159. }
  160. }
  161. });
  162. })(jQuery, Drupal, drupalSettings);