annotate core/modules/tracker/js/tracker-history.js @ 2:5311817fb629

Theme updates
author Chris Cannam
date Tue, 10 Jul 2018 13:19:18 +0000
parents c75dbcec494b
children a9cd425dd02b
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal, window) {
Chris@0 9 Drupal.behaviors.trackerHistory = {
Chris@0 10 attach: function attach(context) {
Chris@0 11 var nodeIDs = [];
Chris@0 12 var $nodeNewPlaceholders = $(context).find('[data-history-node-timestamp]').once('history').filter(function () {
Chris@0 13 var nodeTimestamp = parseInt(this.getAttribute('data-history-node-timestamp'), 10);
Chris@0 14 var nodeID = this.getAttribute('data-history-node-id');
Chris@0 15 if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
Chris@0 16 nodeIDs.push(nodeID);
Chris@0 17 return true;
Chris@0 18 }
Chris@0 19
Chris@0 20 return false;
Chris@0 21 });
Chris@0 22
Chris@0 23 var $newRepliesPlaceholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
Chris@0 24 var lastCommentTimestamp = parseInt(this.getAttribute('data-history-node-last-comment-timestamp'), 10);
Chris@0 25 var nodeTimestamp = parseInt(this.previousSibling.previousSibling.getAttribute('data-history-node-timestamp'), 10);
Chris@0 26
Chris@0 27 if (lastCommentTimestamp === nodeTimestamp) {
Chris@0 28 return false;
Chris@0 29 }
Chris@0 30 var nodeID = this.previousSibling.previousSibling.getAttribute('data-history-node-id');
Chris@0 31 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
Chris@0 32 if (nodeIDs.indexOf(nodeID) === -1) {
Chris@0 33 nodeIDs.push(nodeID);
Chris@0 34 }
Chris@0 35 return true;
Chris@0 36 }
Chris@0 37
Chris@0 38 return false;
Chris@0 39 });
Chris@0 40
Chris@0 41 if ($nodeNewPlaceholders.length === 0 && $newRepliesPlaceholders.length === 0) {
Chris@0 42 return;
Chris@0 43 }
Chris@0 44
Chris@0 45 Drupal.history.fetchTimestamps(nodeIDs, function () {
Chris@0 46 processNodeNewIndicators($nodeNewPlaceholders);
Chris@0 47 processNewRepliesIndicators($newRepliesPlaceholders);
Chris@0 48 });
Chris@0 49 }
Chris@0 50 };
Chris@0 51
Chris@0 52 function processNodeNewIndicators($placeholders) {
Chris@0 53 var newNodeString = Drupal.t('new');
Chris@0 54 var updatedNodeString = Drupal.t('updated');
Chris@0 55
Chris@0 56 $placeholders.each(function (index, placeholder) {
Chris@0 57 var timestamp = parseInt(placeholder.getAttribute('data-history-node-timestamp'), 10);
Chris@0 58 var nodeID = placeholder.getAttribute('data-history-node-id');
Chris@0 59 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
Chris@0 60
Chris@0 61 if (timestamp > lastViewTimestamp) {
Chris@0 62 var message = lastViewTimestamp === 0 ? newNodeString : updatedNodeString;
Chris@0 63 $(placeholder).append('<span class="marker">' + message + '</span>');
Chris@0 64 }
Chris@0 65 });
Chris@0 66 }
Chris@0 67
Chris@0 68 function processNewRepliesIndicators($placeholders) {
Chris@0 69 var placeholdersToUpdate = {};
Chris@0 70 $placeholders.each(function (index, placeholder) {
Chris@0 71 var timestamp = parseInt(placeholder.getAttribute('data-history-node-last-comment-timestamp'), 10);
Chris@0 72 var nodeID = placeholder.previousSibling.previousSibling.getAttribute('data-history-node-id');
Chris@0 73 var lastViewTimestamp = Drupal.history.getLastRead(nodeID);
Chris@0 74
Chris@0 75 if (timestamp > lastViewTimestamp) {
Chris@0 76 placeholdersToUpdate[nodeID] = placeholder;
Chris@0 77 }
Chris@0 78 });
Chris@0 79
Chris@0 80 var nodeIDs = Object.keys(placeholdersToUpdate);
Chris@0 81 if (nodeIDs.length === 0) {
Chris@0 82 return;
Chris@0 83 }
Chris@0 84 $.ajax({
Chris@0 85 url: Drupal.url('comments/render_new_comments_node_links'),
Chris@0 86 type: 'POST',
Chris@0 87 data: { 'node_ids[]': nodeIDs },
Chris@0 88 dataType: 'json',
Chris@0 89 success: function success(results) {
Chris@0 90 Object.keys(results || {}).forEach(function (nodeID) {
Chris@0 91 if (placeholdersToUpdate.hasOwnProperty(nodeID)) {
Chris@0 92 var url = results[nodeID].first_new_comment_link;
Chris@0 93 var text = Drupal.formatPlural(results[nodeID].new_comment_count, '1 new', '@count new');
Chris@0 94 $(placeholdersToUpdate[nodeID]).append('<br /><a href="' + url + '">' + text + '</a>');
Chris@0 95 }
Chris@0 96 });
Chris@0 97 }
Chris@0 98 });
Chris@0 99 }
Chris@0 100 })(jQuery, Drupal, window);