annotate core/modules/tracker/js/tracker-history.js @ 19:fa3358dc1485 tip

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