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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
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, drupalSettings, storage) {
Chris@0 9 var currentUserID = parseInt(drupalSettings.user.uid, 10);
Chris@0 10
Chris@14 11 var secondsIn30Days = 2592000;
Chris@14 12 var thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - secondsIn30Days;
Chris@0 13
Chris@0 14 var embeddedLastReadTimestamps = false;
Chris@0 15 if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
Chris@0 16 embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
Chris@0 17 }
Chris@0 18
Chris@0 19 Drupal.history = {
Chris@0 20 fetchTimestamps: function fetchTimestamps(nodeIDs, callback) {
Chris@0 21 if (embeddedLastReadTimestamps) {
Chris@0 22 callback();
Chris@0 23 return;
Chris@0 24 }
Chris@0 25
Chris@0 26 $.ajax({
Chris@0 27 url: Drupal.url('history/get_node_read_timestamps'),
Chris@0 28 type: 'POST',
Chris@0 29 data: { 'node_ids[]': nodeIDs },
Chris@0 30 dataType: 'json',
Chris@0 31 success: function success(results) {
Chris@14 32 Object.keys(results || {}).forEach(function (nodeID) {
Chris@14 33 storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, results[nodeID]);
Chris@14 34 });
Chris@0 35 callback();
Chris@0 36 }
Chris@0 37 });
Chris@0 38 },
Chris@0 39 getLastRead: function getLastRead(nodeID) {
Chris@0 40 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 41 return parseInt(embeddedLastReadTimestamps[nodeID], 10);
Chris@0 42 }
Chris@0 43 return parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
Chris@0 44 },
Chris@0 45 markAsRead: function markAsRead(nodeID) {
Chris@0 46 $.ajax({
Chris@0 47 url: Drupal.url('history/' + nodeID + '/read'),
Chris@0 48 type: 'POST',
Chris@0 49 dataType: 'json',
Chris@0 50 success: function success(timestamp) {
Chris@0 51 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 52 return;
Chris@0 53 }
Chris@0 54
Chris@0 55 storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, timestamp);
Chris@0 56 }
Chris@0 57 });
Chris@0 58 },
Chris@0 59 needsServerCheck: function needsServerCheck(nodeID, contentTimestamp) {
Chris@0 60 if (contentTimestamp < thirtyDaysAgo) {
Chris@0 61 return false;
Chris@0 62 }
Chris@0 63
Chris@0 64 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 65 return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
Chris@0 66 }
Chris@0 67
Chris@0 68 var minLastReadTimestamp = parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
Chris@0 69 return contentTimestamp > minLastReadTimestamp;
Chris@0 70 }
Chris@0 71 };
Chris@0 72 })(jQuery, Drupal, drupalSettings, window.localStorage);