heartbeat.js 13 KB

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