heartbeat.js 13 KB

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