heartbeat.js 11 KB

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