heartbeat.js 13 KB

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