b9cb9fc5fb00e0e38e748226fd6b838257aab8eb
[yaffs-website] / web / core / modules / history / js / history.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings, storage) {
9   var currentUserID = parseInt(drupalSettings.user.uid, 10);
10
11   var secondsIn30Days = 2592000;
12   var thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - secondsIn30Days;
13
14   var embeddedLastReadTimestamps = false;
15   if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
16     embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
17   }
18
19   Drupal.history = {
20     fetchTimestamps: function fetchTimestamps(nodeIDs, callback) {
21       if (embeddedLastReadTimestamps) {
22         callback();
23         return;
24       }
25
26       $.ajax({
27         url: Drupal.url('history/get_node_read_timestamps'),
28         type: 'POST',
29         data: { 'node_ids[]': nodeIDs },
30         dataType: 'json',
31         success: function success(results) {
32           Object.keys(results || {}).forEach(function (nodeID) {
33             storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, results[nodeID]);
34           });
35           callback();
36         }
37       });
38     },
39     getLastRead: function getLastRead(nodeID) {
40       if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
41         return parseInt(embeddedLastReadTimestamps[nodeID], 10);
42       }
43       return parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
44     },
45     markAsRead: function markAsRead(nodeID) {
46       $.ajax({
47         url: Drupal.url('history/' + nodeID + '/read'),
48         type: 'POST',
49         dataType: 'json',
50         success: function success(timestamp) {
51           if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
52             return;
53           }
54
55           storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, timestamp);
56         }
57       });
58     },
59     needsServerCheck: function needsServerCheck(nodeID, contentTimestamp) {
60       if (contentTimestamp < thirtyDaysAgo) {
61         return false;
62       }
63
64       if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
65         return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
66       }
67
68       var minLastReadTimestamp = parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
69       return contentTimestamp > minLastReadTimestamp;
70     }
71   };
72 })(jQuery, Drupal, drupalSettings, window.localStorage);