2015-07-20 13:46:23 +00:00
|
|
|
/**
|
2022-09-09 06:26:42 +00:00
|
|
|
* Attaches behaviors for the Tracker module's History module integration.
|
|
|
|
*
|
|
|
|
* May only be loaded for authenticated users, with the History module enabled.
|
|
|
|
*/
|
2015-07-20 13:46:23 +00:00
|
|
|
(function ($, Drupal, window) {
|
2021-08-10 10:05:02 +00:00
|
|
|
function processNodeNewIndicators(placeholders) {
|
2021-12-18 06:12:16 +00:00
|
|
|
const newNodeString = Drupal.t('new');
|
|
|
|
const updatedNodeString = Drupal.t('updated');
|
2022-09-09 06:26:42 +00:00
|
|
|
|
|
|
|
placeholders.forEach((placeholder) => {
|
|
|
|
const timestamp = parseInt(
|
|
|
|
placeholder.getAttribute('data-history-node-timestamp'),
|
|
|
|
10,
|
|
|
|
);
|
2021-12-18 06:12:16 +00:00
|
|
|
const nodeID = placeholder.getAttribute('data-history-node-id');
|
|
|
|
const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
|
2015-07-20 13:46:23 +00:00
|
|
|
|
|
|
|
if (timestamp > lastViewTimestamp) {
|
2022-09-09 06:26:42 +00:00
|
|
|
const message =
|
|
|
|
lastViewTimestamp === 0 ? newNodeString : updatedNodeString;
|
2021-12-18 06:12:16 +00:00
|
|
|
$(placeholder).append(`<span class="marker">${message}</span>`);
|
2015-07-20 13:46:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-10 10:05:02 +00:00
|
|
|
function processNewRepliesIndicators(placeholders) {
|
2022-09-09 06:26:42 +00:00
|
|
|
// Figure out which placeholders need the "x new" replies links.
|
2021-12-18 06:12:16 +00:00
|
|
|
const placeholdersToUpdate = {};
|
2022-09-09 06:26:42 +00:00
|
|
|
placeholders.forEach((placeholder) => {
|
|
|
|
const timestamp = parseInt(
|
|
|
|
placeholder.getAttribute('data-history-node-last-comment-timestamp'),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
const nodeID = placeholder.previousSibling.previousSibling.getAttribute(
|
|
|
|
'data-history-node-id',
|
|
|
|
);
|
2021-12-18 06:12:16 +00:00
|
|
|
const lastViewTimestamp = Drupal.history.getLastRead(nodeID);
|
2015-07-20 13:46:23 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
// Queue this placeholder's "X new" replies link to be downloaded from the
|
|
|
|
// server.
|
2015-07-20 13:46:23 +00:00
|
|
|
if (timestamp > lastViewTimestamp) {
|
|
|
|
placeholdersToUpdate[nodeID] = placeholder;
|
|
|
|
}
|
|
|
|
});
|
2020-01-30 09:08:38 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
// Perform an AJAX request to retrieve node view timestamps.
|
|
|
|
const nodeIDs = Object.keys(placeholdersToUpdate);
|
2015-07-20 13:46:23 +00:00
|
|
|
if (nodeIDs.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
url: Drupal.url('comments/render_new_comments_node_links'),
|
|
|
|
type: 'POST',
|
2022-09-09 06:26:42 +00:00
|
|
|
data: { 'node_ids[]': nodeIDs },
|
2015-07-20 13:46:23 +00:00
|
|
|
dataType: 'json',
|
2021-12-18 06:12:16 +00:00
|
|
|
success(results) {
|
2022-09-09 06:26:42 +00:00
|
|
|
Object.keys(results || {}).forEach((nodeID) => {
|
2018-01-03 23:25:46 +00:00
|
|
|
if (placeholdersToUpdate.hasOwnProperty(nodeID)) {
|
2021-12-18 06:12:16 +00:00
|
|
|
const url = results[nodeID].first_new_comment_link;
|
2022-09-09 06:26:42 +00:00
|
|
|
const text = Drupal.formatPlural(
|
|
|
|
results[nodeID].new_comment_count,
|
|
|
|
'1 new',
|
|
|
|
'@count new',
|
|
|
|
);
|
|
|
|
$(placeholdersToUpdate[nodeID]).append(
|
|
|
|
`<br /><a href="${url}">${text}</a>`,
|
|
|
|
);
|
2015-07-20 13:46:23 +00:00
|
|
|
}
|
2018-01-03 23:25:46 +00:00
|
|
|
});
|
2022-09-09 06:26:42 +00:00
|
|
|
},
|
2015-07-20 13:46:23 +00:00
|
|
|
});
|
|
|
|
}
|
2018-03-05 09:14:08 +00:00
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
/**
|
|
|
|
* Render "new" and "updated" node indicators, as well as "X new" replies links.
|
|
|
|
*/
|
2018-03-05 09:14:08 +00:00
|
|
|
Drupal.behaviors.trackerHistory = {
|
2021-12-18 06:12:16 +00:00
|
|
|
attach(context) {
|
2022-09-09 06:26:42 +00:00
|
|
|
// Find all "new" comment indicator placeholders newer than 30 days ago that
|
|
|
|
// have not already been read after their last comment timestamp.
|
2021-12-18 06:12:16 +00:00
|
|
|
const nodeIDs = [];
|
2022-09-09 06:26:42 +00:00
|
|
|
const nodeNewPlaceholders = once(
|
|
|
|
'history',
|
|
|
|
'[data-history-node-timestamp]',
|
|
|
|
context,
|
|
|
|
).filter((placeholder) => {
|
|
|
|
const nodeTimestamp = parseInt(
|
|
|
|
placeholder.getAttribute('data-history-node-timestamp'),
|
|
|
|
10,
|
|
|
|
);
|
2021-12-18 06:12:16 +00:00
|
|
|
const nodeID = placeholder.getAttribute('data-history-node-id');
|
2018-03-05 09:14:08 +00:00
|
|
|
if (Drupal.history.needsServerCheck(nodeID, nodeTimestamp)) {
|
|
|
|
nodeIDs.push(nodeID);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
// Find all "new" comment indicator placeholders newer than 30 days ago that
|
|
|
|
// have not already been read after their last comment timestamp.
|
|
|
|
const newRepliesPlaceholders = once(
|
|
|
|
'history',
|
|
|
|
'[data-history-node-last-comment-timestamp]',
|
|
|
|
context,
|
|
|
|
).filter((placeholder) => {
|
|
|
|
const lastCommentTimestamp = parseInt(
|
|
|
|
placeholder.getAttribute('data-history-node-last-comment-timestamp'),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
const nodeTimestamp = parseInt(
|
|
|
|
placeholder.previousSibling.previousSibling.getAttribute(
|
|
|
|
'data-history-node-timestamp',
|
|
|
|
),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
// Discard placeholders that have zero comments.
|
2018-03-05 09:14:08 +00:00
|
|
|
if (lastCommentTimestamp === nodeTimestamp) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-09 06:26:42 +00:00
|
|
|
const nodeID = placeholder.previousSibling.previousSibling.getAttribute(
|
|
|
|
'data-history-node-id',
|
|
|
|
);
|
2018-03-05 09:14:08 +00:00
|
|
|
if (Drupal.history.needsServerCheck(nodeID, lastCommentTimestamp)) {
|
|
|
|
if (nodeIDs.indexOf(nodeID) === -1) {
|
|
|
|
nodeIDs.push(nodeID);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
if (
|
|
|
|
nodeNewPlaceholders.length === 0 &&
|
|
|
|
newRepliesPlaceholders.length === 0
|
|
|
|
) {
|
2018-03-05 09:14:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:26:42 +00:00
|
|
|
// Fetch the node read timestamps from the server.
|
2021-12-18 06:12:16 +00:00
|
|
|
Drupal.history.fetchTimestamps(nodeIDs, () => {
|
2021-08-10 10:05:02 +00:00
|
|
|
processNodeNewIndicators(nodeNewPlaceholders);
|
|
|
|
processNewRepliesIndicators(newRepliesPlaceholders);
|
2018-03-05 09:14:08 +00:00
|
|
|
});
|
2022-09-09 06:26:42 +00:00
|
|
|
},
|
2018-03-05 09:14:08 +00:00
|
|
|
};
|
2022-09-09 06:26:42 +00:00
|
|
|
})(jQuery, Drupal, window);
|