annotate core/modules/history/js/history.es6.js @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * JavaScript API for the History module, with client-side caching.
Chris@0 4 *
Chris@0 5 * May only be loaded for authenticated users, with the History module enabled.
Chris@0 6 */
Chris@0 7
Chris@0 8 (function ($, Drupal, drupalSettings, storage) {
Chris@0 9 const currentUserID = parseInt(drupalSettings.user.uid, 10);
Chris@0 10
Chris@0 11 // Any comment that is older than 30 days is automatically considered read,
Chris@0 12 // so for these we don't need to perform a request at all!
Chris@0 13 const thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - 30 * 24 * 60 * 60;
Chris@0 14
Chris@0 15 // Use the data embedded in the page, if available.
Chris@0 16 let embeddedLastReadTimestamps = false;
Chris@0 17 if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
Chris@0 18 embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
Chris@0 19 }
Chris@0 20
Chris@0 21 /**
Chris@0 22 * @namespace
Chris@0 23 */
Chris@0 24 Drupal.history = {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Fetch "last read" timestamps for the given nodes.
Chris@0 28 *
Chris@0 29 * @param {Array} nodeIDs
Chris@0 30 * An array of node IDs.
Chris@0 31 * @param {function} callback
Chris@0 32 * A callback that is called after the requested timestamps were fetched.
Chris@0 33 */
Chris@0 34 fetchTimestamps(nodeIDs, callback) {
Chris@0 35 // Use the data embedded in the page, if available.
Chris@0 36 if (embeddedLastReadTimestamps) {
Chris@0 37 callback();
Chris@0 38 return;
Chris@0 39 }
Chris@0 40
Chris@0 41 $.ajax({
Chris@0 42 url: Drupal.url('history/get_node_read_timestamps'),
Chris@0 43 type: 'POST',
Chris@0 44 data: { 'node_ids[]': nodeIDs },
Chris@0 45 dataType: 'json',
Chris@0 46 success(results) {
Chris@0 47 for (const nodeID in results) {
Chris@0 48 if (results.hasOwnProperty(nodeID)) {
Chris@0 49 storage.setItem(`Drupal.history.${currentUserID}.${nodeID}`, results[nodeID]);
Chris@0 50 }
Chris@0 51 }
Chris@0 52 callback();
Chris@0 53 },
Chris@0 54 });
Chris@0 55 },
Chris@0 56
Chris@0 57 /**
Chris@0 58 * Get the last read timestamp for the given node.
Chris@0 59 *
Chris@0 60 * @param {number|string} nodeID
Chris@0 61 * A node ID.
Chris@0 62 *
Chris@0 63 * @return {number}
Chris@0 64 * A UNIX timestamp.
Chris@0 65 */
Chris@0 66 getLastRead(nodeID) {
Chris@0 67 // Use the data embedded in the page, if available.
Chris@0 68 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 69 return parseInt(embeddedLastReadTimestamps[nodeID], 10);
Chris@0 70 }
Chris@0 71 return parseInt(storage.getItem(`Drupal.history.${currentUserID}.${nodeID}`) || 0, 10);
Chris@0 72 },
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Marks a node as read, store the last read timestamp client-side.
Chris@0 76 *
Chris@0 77 * @param {number|string} nodeID
Chris@0 78 * A node ID.
Chris@0 79 */
Chris@0 80 markAsRead(nodeID) {
Chris@0 81 $.ajax({
Chris@0 82 url: Drupal.url(`history/${nodeID}/read`),
Chris@0 83 type: 'POST',
Chris@0 84 dataType: 'json',
Chris@0 85 success(timestamp) {
Chris@0 86 // If the data is embedded in the page, don't store on the client
Chris@0 87 // side.
Chris@0 88 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 89 return;
Chris@0 90 }
Chris@0 91
Chris@0 92 storage.setItem(`Drupal.history.${currentUserID}.${nodeID}`, timestamp);
Chris@0 93 },
Chris@0 94 });
Chris@0 95 },
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Determines whether a server check is necessary.
Chris@0 99 *
Chris@0 100 * Any content that is >30 days old never gets a "new" or "updated"
Chris@0 101 * indicator. Any content that was published before the oldest known reading
Chris@0 102 * also never gets a "new" or "updated" indicator, because it must've been
Chris@0 103 * read already.
Chris@0 104 *
Chris@0 105 * @param {number|string} nodeID
Chris@0 106 * A node ID.
Chris@0 107 * @param {number} contentTimestamp
Chris@0 108 * The time at which some content (e.g. a comment) was published.
Chris@0 109 *
Chris@0 110 * @return {bool}
Chris@0 111 * Whether a server check is necessary for the given node and its
Chris@0 112 * timestamp.
Chris@0 113 */
Chris@0 114 needsServerCheck(nodeID, contentTimestamp) {
Chris@0 115 // First check if the content is older than 30 days, then we can bail
Chris@0 116 // early.
Chris@0 117 if (contentTimestamp < thirtyDaysAgo) {
Chris@0 118 return false;
Chris@0 119 }
Chris@0 120
Chris@0 121 // Use the data embedded in the page, if available.
Chris@0 122 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
Chris@0 123 return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
Chris@0 124 }
Chris@0 125
Chris@0 126 const minLastReadTimestamp = parseInt(storage.getItem(`Drupal.history.${currentUserID}.${nodeID}`) || 0, 10);
Chris@0 127 return contentTimestamp > minLastReadTimestamp;
Chris@0 128 },
Chris@0 129 };
Chris@0 130 }(jQuery, Drupal, drupalSettings, window.localStorage));