Move into nested docroot

This commit is contained in:
Rob Davies 2017-02-13 15:31:17 +00:00
parent 83a0d3a149
commit c8b70abde9
13405 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,134 @@
/**
* @file
* JavaScript API for the History module, with client-side caching.
*
* May only be loaded for authenticated users, with the History module enabled.
*/
(function ($, Drupal, drupalSettings, storage) {
'use strict';
var currentUserID = parseInt(drupalSettings.user.uid, 10);
// Any comment that is older than 30 days is automatically considered read,
// so for these we don't need to perform a request at all!
var thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - 30 * 24 * 60 * 60;
// Use the data embedded in the page, if available.
var embeddedLastReadTimestamps = false;
if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
}
/**
* @namespace
*/
Drupal.history = {
/**
* Fetch "last read" timestamps for the given nodes.
*
* @param {Array} nodeIDs
* An array of node IDs.
* @param {function} callback
* A callback that is called after the requested timestamps were fetched.
*/
fetchTimestamps: function (nodeIDs, callback) {
// Use the data embedded in the page, if available.
if (embeddedLastReadTimestamps) {
callback();
return;
}
$.ajax({
url: Drupal.url('history/get_node_read_timestamps'),
type: 'POST',
data: {'node_ids[]': nodeIDs},
dataType: 'json',
success: function (results) {
for (var nodeID in results) {
if (results.hasOwnProperty(nodeID)) {
storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, results[nodeID]);
}
}
callback();
}
});
},
/**
* Get the last read timestamp for the given node.
*
* @param {number|string} nodeID
* A node ID.
*
* @return {number}
* A UNIX timestamp.
*/
getLastRead: function (nodeID) {
// Use the data embedded in the page, if available.
if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
return parseInt(embeddedLastReadTimestamps[nodeID], 10);
}
return parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
},
/**
* Marks a node as read, store the last read timestamp client-side.
*
* @param {number|string} nodeID
* A node ID.
*/
markAsRead: function (nodeID) {
$.ajax({
url: Drupal.url('history/' + nodeID + '/read'),
type: 'POST',
dataType: 'json',
success: function (timestamp) {
// If the data is embedded in the page, don't store on the client
// side.
if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
return;
}
storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, timestamp);
}
});
},
/**
* Determines whether a server check is necessary.
*
* Any content that is >30 days old never gets a "new" or "updated"
* indicator. Any content that was published before the oldest known reading
* also never gets a "new" or "updated" indicator, because it must've been
* read already.
*
* @param {number|string} nodeID
* A node ID.
* @param {number} contentTimestamp
* The time at which some content (e.g. a comment) was published.
*
* @return {bool}
* Whether a server check is necessary for the given node and its
* timestamp.
*/
needsServerCheck: function (nodeID, contentTimestamp) {
// First check if the content is older than 30 days, then we can bail
// early.
if (contentTimestamp < thirtyDaysAgo) {
return false;
}
// Use the data embedded in the page, if available.
if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
}
var minLastReadTimestamp = parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
return contentTimestamp > minLastReadTimestamp;
}
};
})(jQuery, Drupal, drupalSettings, window.localStorage);

View file

@ -0,0 +1,23 @@
/**
* @file
* Marks the nodes listed in drupalSettings.history.nodesToMarkAsRead as read.
*
* Uses the History module JavaScript API.
*
* @see Drupal.history
*/
(function (window, Drupal, drupalSettings) {
'use strict';
// When the window's "load" event is triggered, mark all enumerated nodes as
// read. This still allows for Drupal behaviors (which are triggered on the
// "DOMContentReady" event) to add "new" and "updated" indicators.
window.addEventListener('load', function () {
if (drupalSettings.history && drupalSettings.history.nodesToMarkAsRead) {
Object.keys(drupalSettings.history.nodesToMarkAsRead).forEach(Drupal.history.markAsRead);
}
});
})(window, Drupal, drupalSettings);