heartbeat.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Created by logicp on 5/28/17.
  3. */
  4. (function($, Drupal, drupalSettings) {
  5. const commentListen = function(e) {
  6. if (drupalSettings.user.uid > 0) {
  7. let commentBlock = e.srcElement.parentNode.parentNode.querySelector('.heartbeat-comments');
  8. if (!commentBlock.classList.contains('heartbeat-comments-visible')) {
  9. commentBlock.className += ' heartbeat-comments-visible';
  10. } else {
  11. commentBlock.classList.remove('heartbeat-comments-visible');
  12. }
  13. let childs = e.srcElement.parentNode.querySelectorAll('.form-submit, .js-form-type-textarea');
  14. for (let c = 0; c < childs.length; c++) {
  15. toggleCommentElements(childs[c]);
  16. }
  17. } else {
  18. loginModal();
  19. }
  20. };
  21. $(document).ready(function() {
  22. const loader = document.createElement('div');
  23. loader.id = 'heartbeat-loader';
  24. const body = document.getElementsByTagName('body')[0];
  25. body.appendChild(loader);
  26. Drupal.behaviors.heartbeat = {
  27. attach: function (context, settings) {
  28. if (drupalSettings.friendData != null) {
  29. let divs = document.querySelectorAll('.flag-friendship a.use-ajax');
  30. for (let i = 0; i < divs.length; i++) {
  31. let anchor = divs[i];
  32. let userId = anchor.href.substring(anchor.href.indexOf('friendship') + 11, anchor.href.indexOf('?destination'));
  33. JSON.parse(drupalSettings.friendData).forEach(function (friendship) {
  34. if (friendship.uid_target === userId && friendship.uid == drupalSettings.user.uid && friendship.status == 0) {
  35. anchor.innerHTML = 'Friendship Pending';
  36. }
  37. });
  38. }
  39. }
  40. feedElement = document.querySelector('.heartbeat-stream');
  41. if (drupalSettings.feedUpdate == true) {
  42. updateFeed();
  43. }
  44. Drupal.AjaxCommands.prototype.selectFeed = function(ajax, response, status) {
  45. $.ajax({
  46. type:'POST',
  47. url:'/heartbeat/render_feed/' + response.feed,
  48. success: function(response) {
  49. feedElement = document.querySelector('.heartbeat-stream');
  50. if (feedElement != null) {
  51. feedElement.innerHTML = response;
  52. } else {
  53. feedBlock = document.getElementById('block-heartbeatblock');
  54. insertNode = document.createElement('div');
  55. insertNode.innerHTML = response;
  56. feedBlock.appendChild(insertNode);
  57. }
  58. }
  59. });
  60. };
  61. Drupal.AjaxCommands.prototype.updateFeed = function(ajax, response, status) {
  62. if (response.update) {
  63. $.ajax({
  64. type: 'POST',
  65. url:'/heartbeat/update_feed/' + response.timestamp,
  66. success: function(response) {
  67. }
  68. });
  69. }
  70. };
  71. listenImages();
  72. listenCommentPost();
  73. Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function(ajax, response, status) {
  74. console.dir(response);
  75. if (response.cid) {
  76. console.log('this shit is getting called again');
  77. let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
  78. let text = parentComment.querySelector('.form-textarea');
  79. text.addEventListener('keydown', function (e) {
  80. console.dir(e);
  81. if (e.keyCode === 13) {
  82. let submitBtn = parentComment.querySelector('.form-submit');
  83. submitBtn.click();
  84. }
  85. });
  86. }
  87. };
  88. }
  89. };
  90. commentFormListeners();
  91. let stream = document.getElementById('block-heartbeatblock');
  92. let observer = new MutationObserver(function(mutations) {
  93. console.log('observer observes a change');
  94. listenImages();
  95. hideCommentForms();
  96. commentFormListeners();
  97. });
  98. let config = { attributes: true, childList: true, characterData: true };
  99. observer.observe(stream, config);
  100. console.dir(observer);
  101. function updateFeed() {
  102. $.ajax({
  103. type: 'POST',
  104. url: '/heartbeat/form/heartbeat_update_feed',
  105. success: function (response) {
  106. }
  107. })
  108. }
  109. function listenImages() {
  110. let cboxOptions = {
  111. maxWidth: '960px',
  112. maxHeight: '960px',
  113. };
  114. $('.heartbeat-content').find('img').each(function() {
  115. let parentClass = $(this).parent().prop('className');
  116. let phid = parentClass.substring(parentClass.indexOf('hid') + 4);
  117. $(this).colorbox({rel: phid, href: $(this).attr('src'), cboxOptions});
  118. });
  119. }
  120. function listenCommentPost() {
  121. let comments = document.querySelectorAll('[data-drupal-selector]');
  122. for (let i = 0; i < comments.length; i++) {
  123. let comment = comments[i];
  124. comment.addEventListener('click', function() {
  125. getParent(comment);
  126. })
  127. }
  128. }
  129. function getParent(node) {
  130. if (node != null && node != undefined && node.classList != undefined && node.classList.contains('heartbeat-comment')) {
  131. let id = node.id.substr(node.id.indexOf('-') + 1);
  132. $.ajax({
  133. type: 'POST',
  134. url:'/heartbeat/commentupdate/' + id,
  135. success: function(response) {
  136. }
  137. });
  138. } else {
  139. if (node != null && node.nodeName !== 'body') {
  140. getParent(node.parentNode);
  141. }
  142. }
  143. }
  144. function getScrollXY() {
  145. var scrOfX = 0, scrOfY = 0;
  146. if( typeof( window.pageYOffset ) == 'number' ) {
  147. scrOfY = window.pageYOffset;
  148. scrOfX = window.pageXOffset;
  149. } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  150. scrOfY = document.body.scrollTop;
  151. scrOfX = document.body.scrollLeft;
  152. } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  153. scrOfY = document.documentElement.scrollTop;
  154. scrOfX = document.documentElement.scrollLeft;
  155. }
  156. return [ scrOfX, scrOfY ];
  157. }
  158. //taken from http://james.padolsey.com/javascript/get-document-height-cross-browser/
  159. function getDocHeight() {
  160. var D = document;
  161. return Math.max(
  162. D.body.scrollHeight, D.documentElement.scrollHeight,
  163. D.body.offsetHeight, D.documentElement.offsetHeight,
  164. D.body.clientHeight, D.documentElement.clientHeight
  165. );
  166. }
  167. document.addEventListener("scroll", function (event) {
  168. if (drupalSettings.filterMode == false && (getScrollXY()[1] + window.innerHeight) / getDocHeight() > 0.99) {
  169. let streams = document.querySelectorAll('.heartbeat-stream');
  170. let stream = streams.length > 1 ? streams[streams.length - 1] : streams[0];
  171. if (stream !== null) {
  172. console.dir(stream);
  173. let lastHeartbeat = stream.lastElementChild;
  174. if (lastHeartbeat !== null) {
  175. let hid = lastHeartbeat.id.substring(lastHeartbeat.id.indexOf('-') + 1);
  176. if (drupalSettings.lastHid !== hid) {
  177. drupalSettings.lastHid = hid;
  178. $('#heartbeat-loader').show(225);
  179. $.ajax({
  180. type: 'POST',
  181. url: '/heartbeat/update_feed/' + hid,
  182. success: function (response) {
  183. feedBlock = document.getElementById('block-heartbeatblock');
  184. insertNode = document.createElement('div');
  185. insertNode.innerHTML = response;
  186. feedBlock.appendChild(insertNode)
  187. },
  188. complete: function() {
  189. $('#heartbeat-loader').hide(225);
  190. }
  191. });
  192. }
  193. }
  194. }
  195. }
  196. });
  197. $(document).on('cbox_open', function() {
  198. $("#colorbox").swipe( {
  199. //Generic swipe handler for all directions
  200. swipeLeft:function(event, direction, distance, duration, fingerCount) {
  201. $.colorbox.prev();
  202. },
  203. swipeRight:function(event, direction, distance, duration, fingerCount) {
  204. $.colorbox.next();
  205. },
  206. //Default is 75px, set to 0 for demo so any distance triggers swipe
  207. threshold:25
  208. });
  209. let cboxCloseBtn = $('#cboxClose');
  210. cboxCloseBtn.on('click touchstart', function() {
  211. $.colorbox.close();
  212. });
  213. cboxCloseBtn.on('keyup', function(e) {
  214. if (e.keyCode == 27) {
  215. $.colorbox().close();
  216. }
  217. });
  218. return true;
  219. }
  220. );
  221. function commentFormListeners() {
  222. console.log('Comment Form Listeners');
  223. let cFormButtons = document.querySelectorAll('.heartbeat-comment-button');
  224. for (let b = 0; b < cFormButtons.length; b++) {
  225. cFormButtons[b].removeEventListener('click', commentListen);
  226. cFormButtons[b].addEventListener('click', commentListen);
  227. }
  228. }
  229. });
  230. /******** Load Login Block **********
  231. ******** append to document ********
  232. ******** Hover in middle of screen */
  233. function loginModal() {
  234. $('#heartbeat-loader').show(225);
  235. $.ajax({
  236. type: 'GET',
  237. url: '/user/modal/login',
  238. success: function (response) {
  239. mainContainer = document.getElementById('main');
  240. loginBlock = document.createElement('div');
  241. loginBlock.innerHTML = response;
  242. loginBlock.className = 'kekistan-login-block';
  243. loginBlock.id = 'kekistan-login-block';
  244. closeBtn = document.createElement('div');
  245. closeBtn.className = 'kekistan-login-block-close';
  246. closeBtn.innerHTML = '✖';
  247. loginBlock.appendChild(closeBtn);
  248. mainContainer.appendChild(loginBlock);
  249. closeBtn.addEventListener('click', function() {
  250. loginBlock.innerHTML = '';
  251. mainContainer.removeChild(loginBlock);
  252. });
  253. },
  254. complete: function() {
  255. $('#heartbeat-loader').hide(225);
  256. }
  257. });
  258. }
  259. function hideCommentForms() {
  260. let forms = document.querySelectorAll('.heartbeat-comment-form .js-form-type-textarea, .heartbeat-comment-form .form-submit');
  261. for (let f = 0; f < forms.length; f++) {
  262. forms[f].className += ' comment-form-hidden';
  263. }
  264. }
  265. function toggleCommentElements(node) {
  266. console.dir(node);
  267. if (node.classList.contains('comment-form-hidden')) {
  268. console.log('removing comment-form-hidden class from element');
  269. node.classList.remove('comment-form-hidden');
  270. } else {
  271. node.className += ' comment-form-hidden';
  272. }
  273. }
  274. })(jQuery, Drupal, drupalSettings);