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