annotate core/modules/tracker/js/tracker-history.js @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
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@0 47 Object.keys(results || {}).forEach(function (nodeID) {
Chris@0 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@0 53 });
Chris@0 54 }
Chris@0 55 });
Chris@0 56 }
Chris@4 57
Chris@4 58 Drupal.behaviors.trackerHistory = {
Chris@4 59 attach: function attach(context) {
Chris@4 60 var nodeIDs = [];
Chris@4 61 var $nodeNewPlaceholders = $(context).find('[data-history-node-timestamp]').once('history').filter(function () {
Chris@4 62 var nodeTimestamp = parseInt(this.getAttribute('data-history-node-timestamp'), 10);
Chris@4 63 var nodeID = this.getAttribute('data-history-node-id');
Chris@4 64 if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
Chris@4 65 nodeIDs.push(nodeID);
Chris@4 66 return true;
Chris@4 67 }
Chris@4 68
Chris@4 69 return false;
Chris@4 70 });
Chris@4 71
Chris@4 72 var $newRepliesPlaceholders = $(context).find('[data-history-node-last-comment-timestamp]').once('history').filter(function () {
Chris@4 73 var lastCommentTimestamp = parseInt(this.getAttribute('data-history-node-last-comment-timestamp'), 10);
Chris@4 74 var nodeTimestamp = parseInt(this.previousSibling.previousSibling.getAttribute('data-history-node-timestamp'), 10);
Chris@4 75
Chris@4 76 if (lastCommentTimestamp === nodeTimestamp) {
Chris@4 77 return false;
Chris@4 78 }
Chris@4 79 var nodeID = this.previousSibling.previousSibling.getAttribute('data-history-node-id');
Chris@4 80 if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
Chris@4 81 if (nodeIDs.indexOf(nodeID) === -1) {
Chris@4 82 nodeIDs.push(nodeID);
Chris@4 83 }
Chris@4 84 return true;
Chris@4 85 }
Chris@4 86
Chris@4 87 return false;
Chris@4 88 });
Chris@4 89
Chris@4 90 if ($nodeNewPlaceholders.length === 0 && $newRepliesPlaceholders.length === 0) {
Chris@4 91 return;
Chris@4 92 }
Chris@4 93
Chris@4 94 Drupal.history.fetchTimestamps(nodeIDs, function () {
Chris@4 95 processNodeNewIndicators($nodeNewPlaceholders);
Chris@4 96 processNewRepliesIndicators($newRepliesPlaceholders);
Chris@4 97 });
Chris@4 98 }
Chris@4 99 };
Chris@0 100 })(jQuery, Drupal, window);