123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /**
- * Created by logicp on 5/28/17.
- */
- (function($, Drupal, drupalSettings) {
- Drupal.behaviors.heartbeat = {
- attach: function (context, settings) {
- if (drupalSettings.friendData != null) {
- var divs = document.querySelectorAll('.flag-friendship a.use-ajax');
- for (let i = 0; i < divs.length; i++) {
- let anchor = divs[i];
- var userId = anchor.href.substring(anchor.href.indexOf('friendship') + 11, anchor.href.indexOf('?destination'));
- JSON.parse(drupalSettings.friendData).forEach(function (friendship) {
- if (friendship.uid_target === userId && friendship.uid == drupalSettings.user.uid && friendship.status == 0) {
- anchor.innerHTML = 'Friendship Pending';
- }
- });
- }
- }
- feedElement = document.querySelector('.heartbeat-stream');
- if (drupalSettings.feedUpdate == true) {
- updateFeed();
- }
- Drupal.AjaxCommands.prototype.selectFeed = function(ajax, response, status) {
- $.ajax({
- type:'POST',
- url:'/heartbeat/render_feed/' + response.feed,
- success: function(response) {
- feedElement = document.querySelector('.heartbeat-stream');
- if (feedElement != null) {
- feedElement.innerHTML = response;
- } else {
- feedBlock = document.getElementById('block-heartbeatblock');
- insertNode = document.createElement('div');
- insertNode.innerHTML = response;
- feedBlock.appendChild(insertNode);
- }
- }
- });
- };
- Drupal.AjaxCommands.prototype.updateFeed = function(ajax, response, status) {
- if (response.update) {
- $.ajax({
- type: 'POST',
- url:'/heartbeat/update_feed/' + response.timestamp,
- success: function(response) {
- }
- });
- }
- };
- listenImages();
- listenCommentPost();
- Drupal.AjaxCommands.prototype.myfavouritemethodintheworld = function(ajax, response, status) {
- console.dir(response);
- if (response.cid) {
- console.log('this shit is getting called again');
- let parentComment = document.getElementById('heartbeat-comment-' + response.cid);
- let text = parentComment.querySelector('.form-textarea');
- text.addEventListener('keydown', function (e) {
- console.dir(e);
- if (e.keyCode === 13) {
- let submitBtn = parentComment.querySelector('.form-submit');
- submitBtn.click();
- }
- });
- }
- }
- }
- };
- function updateFeed() {
- $.ajax({
- type: 'POST',
- url: '/heartbeat/form/heartbeat_update_feed',
- success: function (response) {
- }
- })
- }
- function listenImages() {
- let cboxOptions = {
- width: '95%',
- height: '95%',
- maxWidth: '960px',
- maxHeight: '960px',
- };
- $('.heartbeat-content').find('img').each(function() {
- $(this).colorbox({href: $(this).attr('src'), cboxOptions});
- });
- }
- function listenCommentPost() {
- //TODO is drupal data selector enough? I doubt it.
- let comments = document.querySelectorAll('[data-drupal-selector]');
- for (let i = 0; i < comments.length; i++) {
- let comment = comments[i];
- // console.dir(comment);
- comment.addEventListener('click', function() {
- getParent(comment);
- })
- }
- }
- function getParent(node) {
- console.dir(node);
- if (node.classList.contains('heartbeat-comment')) {
- let id = node.id.substr(node.id.indexOf('-') + 1);
- $.ajax({
- type: 'POST',
- url:'/heartbeat/commentupdate/' + id,
- success: function(response) {
- }
- });
- } else {
- getParent(node.parentNode);
- }
- }
- function getScrollXY() {
- var scrOfX = 0, scrOfY = 0;
- if( typeof( window.pageYOffset ) == 'number' ) {
- //Netscape compliant
- scrOfY = window.pageYOffset;
- scrOfX = window.pageXOffset;
- } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
- //DOM compliant
- scrOfY = document.body.scrollTop;
- scrOfX = document.body.scrollLeft;
- } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
- //IE6 standards compliant mode
- scrOfY = document.documentElement.scrollTop;
- scrOfX = document.documentElement.scrollLeft;
- }
- return [ scrOfX, scrOfY ];
- }
- //taken from http://james.padolsey.com/javascript/get-document-height-cross-browser/
- function getDocHeight() {
- var D = document;
- return Math.max(
- D.body.scrollHeight, D.documentElement.scrollHeight,
- D.body.offsetHeight, D.documentElement.offsetHeight,
- D.body.clientHeight, D.documentElement.clientHeight
- );
- }
- document.addEventListener("scroll", function (event) {
- if (getDocHeight() == getScrollXY()[1] + window.innerHeight) {
- let streams = document.querySelectorAll('.heartbeat-stream');
- let stream = streams.length > 1 ? streams[streams.length - 1] : streams[0];
- if (stream !== null) {
- console.dir(stream);
- let lastHeartbeat = stream.lastElementChild;
- if (lastHeartbeat !== null) {
- let hid = lastHeartbeat.id.substring(lastHeartbeat.id.indexOf('-') + 1);
- $.ajax({
- type: 'POST',
- url: '/heartbeat/update_feed/' + hid,
- success: function (response) {
- feedBlock = document.getElementById('block-heartbeatblock');
- insertNode = document.createElement('div');
- insertNode.innerHTML = response;
- feedBlock.appendChild(insertNode);
- }
- });
- }
- }
- }
- });
- })(jQuery, Drupal, drupalSettings);
|