heartbeat.js 13 KB

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