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