123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- /**
- * Created by logicp on 5/28/17.
- */
- (function($, Drupal, drupalSettings) {
- drupalSettings.requestInProgress = false;
- const flagListen = function(e) {
- let stringWithHid = e.srcElement.href.substring(0, e.srcElement.href.indexOf('?destination'));
- let hid = stringWithHid.substring(stringWithHid.lastIndexOf('/') + 1);
- let flagId = stringWithHid.substring(stringWithHid.lastIndexOf('/flag/'), stringWithHid.lastIndexOf(hid) - 1);
- flagId = flagId.substring(flagId.lastIndexOf('/') + 1);
- $.ajax({
- type: 'POST',
- url:'/heartbeat/userflaggings',
- data: {
- entity_id: hid,
- entity_type: 'heartbeat',
- flag_id: flagId,
- uid: drupalSettings.user.uid
- },
- success: function(response) {
- console.dir(response);
- // let heartbeatLikeFlag = document.querySelector('.flag-heartbeat_like-1111 a.use-ajax');
- // heartbeatLikeFlag.click();
- }
- });
- };
- const addWindowScrollListener = function() {
- if (drupalSettings.filterMode == false &&
- (getScrollXY()[1] + window.innerHeight) / getDocHeight() > 0.99 &&
- !drupalSettings.requestInProgress) {
- drupalSettings.requestInProgress = true;
- let streams = document.querySelectorAll('.heartbeat-stream');
- let stream = streams.length > 1 ? streams[streams.length - 1] : streams[0];
- if (stream !== null) {
- let lastHeartbeat = stream.lastElementChild;
- if (lastHeartbeat !== null) {
- let hid = lastHeartbeat.id.substring(lastHeartbeat.id.indexOf('-') + 1);
- if (drupalSettings.lastHid !== hid) {
- drupalSettings.lastHid = hid;
- $('#heartbeat-loader').show(225);
- $.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)
- Drupal.attachBehaviors()
- },
- complete: function () {
- $('#heartbeat-loader').hide(225);
- drupalSettings.requestInProgress = false;
- }
- });
- }
- }
- }
- }
- };
- function updateFeed() {
- $.ajax({
- type: 'POST',
- url: '/heartbeat/form/heartbeat_update_feed',
- success: function (response) {
- }
- })
- }
- function listenImages() {
- let cboxOptions = { maxWidth: '960px', maxHeight: '960px' };
- $('.heartbeat-content').find('img').each(function () {
- let parentClass = $(this).parent().prop('className');
- let phid = parentClass.substring(parentClass.indexOf('hid') + 4);
- $(this).colorbox({rel: phid, href: $(this).attr('src'), cboxOptions});
- });
- }
- function listenCommentPost() {
- let comments = document.querySelectorAll('[data-drupal-selector]');
- for (let i = 0; i < comments.length; i++) {
- let comment = comments[i];
- comment.addEventListener('click', function () {
- getParent(comment);
- })
- }
- }
- function commentFormListeners() {
- let cFormButtons = document.querySelectorAll('.heartbeat-comment-button');
- for (let b = 0; b < cFormButtons.length; b++) {
- cFormButtons[b].removeEventListener('click', commentListen);
- cFormButtons[b].addEventListener('click', commentListen);
- }
- }
- function flagListeners() {
- let flags = arguments[0] == undefined ? document.querySelectorAll('.flag .use-ajax') : arguments[0];
- if (flags.constructor === NodeList) {
- for (let f = 0; f < flags.length; f++) {
- flags[f].removeEventListener('click', flagListen);
- flags[f].addEventListener('click', flagListen);
- }
- } else if (flags.constructor === Array && (flags[0].nodeType !== null && flags[0].nodeType > 0)) {
- flags.forEach(function(node) {
- node.removeEventListener('click', flagListen);
- node.addEventListener('click', flagListen);
- });
- } else if (flags.nodeName !== null && flags.nodeName !== undefined) {
- flags.removeEventListener('click', flagListen);
- flags.addEventListener('click', flagListen);
- } else {
- console.debug('FlagListen called with no flags available');
- }
- }
- function getParent(node) {
- if (node != null && node != undefined && node.classList != undefined && 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 {
- if (node != null && node.nodeName !== 'body') {
- getParent(node.parentNode);
- }
- }
- }
- function getScrollXY() {
- let scrOfX = 0, scrOfY = 0;
- if (typeof( window.pageYOffset ) == 'number') {
- scrOfY = window.pageYOffset;
- scrOfX = window.pageXOffset;
- } else if (document.body && ( document.body.scrollLeft || document.body.scrollTop )) {
- scrOfY = document.body.scrollTop;
- scrOfX = document.body.scrollLeft;
- } else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
- 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() {
- let D = document;
- return Math.max(
- D.body.scrollHeight, D.documentElement.scrollHeight,
- D.body.offsetHeight, D.documentElement.offsetHeight,
- D.body.clientHeight, D.documentElement.clientHeight
- );
- }
- if (Drupal.AjaxCommands) {
- if (drupalSettings.user.uid > 0 && !drupalSettings.path.currentPathIsAdmin) {
- 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.attachBehaviors();
- }
- });
- };
- Drupal.AjaxCommands.prototype.updateFeed = function (ajax, response, status) {
- if (response.update) {
- $.ajax({
- type: 'POST',
- url: '/heartbeat/update_feed/' + response.timestamp,
- success: function (response) {
- }
- });
- }
- };
- 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 hideCommentForms() {
- let forms = document.querySelectorAll('.heartbeat-comment-form .js-form-type-textarea, .heartbeat-comment-form .form-submit');
- for (let f = 0; f < forms.length; f++) {
- console.log('adding hidden class to comment form');
- if (!forms[f].classList.contains('comment-form-hidden')) {
- forms[f].className += ' comment-form-hidden';
- }
- }
- }
- /******** Load Login Block **********
- ******** append to document ********
- ******** Hover in middle of screen */
- function loginModal() {
- $('#heartbeat-loader').show(225);
- $.ajax({
- type: 'GET',
- url: '/user/modal/login',
- success: function (response) {
- mainContainer = document.getElementById('main');
- loginBlock = document.createElement('div');
- loginBlock.innerHTML = response;
- loginBlock.className = 'heartbeat-login-block';
- loginBlock.id = 'heartbeat-login-block';
- closeBtn = document.createElement('div');
- closeBtn.className = 'heartbeat-login-block-close';
- closeBtn.innerHTML = '✖';
- loginBlock.appendChild(closeBtn);
- mainContainer.appendChild(loginBlock);
- closeBtn.addEventListener('click', function () {
- loginBlock.innerHTML = '';
- mainContainer.removeChild(loginBlock);
- });
- },
- complete: function () {
- $('#heartbeat-loader').hide(225);
- Drupal.attachBehaviors()
- }
- });
- }
- function toggleCommentElements(node) {
- if (node.classList.contains('comment-form-hidden')) {
- node.classList.remove('comment-form-hidden');
- } else {
- node.className += ' comment-form-hidden';
- }
- }
- const commentListen = function(e) {
- if (drupalSettings.user.uid > 0) {
- let commentBlock = e.srcElement.parentNode.parentNode.querySelector('.heartbeat-comments');
- if (!commentBlock.classList.contains('heartbeat-comments-visible')) {
- commentBlock.className += ' heartbeat-comments-visible';
- } else {
- commentBlock.classList.remove('heartbeat-comments-visible');
- }
- let childs = e.srcElement.parentNode.querySelectorAll('.form-submit, .js-form-type-textarea');
- for (let c = 0; c < childs.length; c++) {
- toggleCommentElements(childs[c]);
- }
- } else {
- loginModal();
- }
- };
- Drupal.behaviors.heartbeat = {
- attach: function (context, settings) {
- // Drupal.AjaxCommands.prototype.viewsScrollTop = null;
- // console.dir(Drupal.AjaxCommands.prototype);
- if (drupalSettings.friendData != null) {
- let divs = document.querySelectorAll('.flag-friendship a.use-ajax');
- for (let i = 0; i < divs.length; i++) {
- let anchor = divs[i];
- let 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';
- }
- });
- }
- }
- listenImages();
- listenCommentPost();
- commentFormListeners();
- feedElement = document.querySelector('.heartbeat-stream');
- flagListeners();
- // if (drupalSettings.feedUpdate == true) {
- // updateFeed();
- // }
- }
- };
- $(document).ready(function() {
- const loader = document.createElement('div');
- loader.id = 'heartbeat-loader';
- const body = document.getElementsByTagName('body')[0];
- body.appendChild(loader);
- // flagListeners();
- //
- // let stream = document.getElementById('block-heartbeatblock');
- //
- // if (stream !== null) {
- // let observer = new MutationObserver(function (mutations) {
- // console.log('observer observes a change');
- // // Drupal.attachBehaviors();
- // // listenImages();
- // hideCommentForms();
- // // commentFormListeners();
- // // flagListeners();
- // // listenVideos();
- // });
- //
- // let config = {attributes: true, childList: true, characterData: true};
- // observer.observe(stream, config);
- // }
- //
- // let flagObserver = new MutationObserver(function(mutations) {
- // console.log('observer observes a change');
- // if (mutations[0].target !== null && mutations[0].target.children !== null && mutations[0].target.children.length > 0 && mutations[0].target.children[0].classList.contains('flag')) {
- // flagListeners();
- // }
- // });
- //
- // let flagObserveConfig = {subTree: true, childList: true};
- //
- // let flags = Array.from(document.querySelectorAll('.heartbeat-like, .heartbeat-unlike'));
- //
- // flags.forEach(function(flag) {
- // flagObserver.observe(flag, flagObserveConfig);
- // });
- document.removeEventListener('scroll', addWindowScrollListener);
- document.addEventListener('scroll', addWindowScrollListener);
- $(document).on('cbox_open', function () {
- $("#colorbox").swipe({
- //Generic swipe handler for all directions
- swipeLeft: function (event, direction, distance, duration, fingerCount) {
- $.colorbox.prev();
- },
- swipeRight: function (event, direction, distance, duration, fingerCount) {
- $.colorbox.next();
- },
- //Default is 75px, set to 0 for demo so any distance triggers swipe
- threshold: 25
- });
- let cboxCloseBtn = $('#cboxClose');
- cboxCloseBtn.on('click touchstart', function () {
- $.colorbox.close();
- });
- cboxCloseBtn.on('keyup', function (e) {
- if (e.keyCode == 27) {
- $.colorbox().close();
- }
- });
- return true;
- }
- );
- })
- })(jQuery, Drupal, drupalSettings);
|