File: development/modules/cookienote/js/cookienote.js
import $ from 'jquery';
import {isNumber, isObject} from './../../../js/utils/type';
import {eventType} from './../../../js/utils/events';
/**
* @module Modules
*/
const MS_ONE_DAY = 86400000;
const STORE_DISABLED = Number.NEGATIVE_INFINITY;
const CONFIGS = {
key: 'ls_CookieNote_storage',
expire: 30, //days
};
// internally used utilities
// =========================
// getStored : () -> Integer
const getStored = () => {
// gives back the last timestamp a visitor has visited the page, means it
// returns a integer value if localStorage is available. otherwise it
// returns negative infinity
var r;
try {
r = localStorage.getItem(CONFIGS.key);
return r == null ? 0 : parseInt(r, 10);
} catch (exc) {
return STORE_DISABLED;
}
}
// setStored : () -> Boolean
const setStored = () => {
// saves a timestamp into the localStorage which represents the last time
// a visitor has accepted the CookieNote. returns 'true' if the
// localStorage is available, returns 'false' otherwise
try {
localStorage.setItem(CONFIGS.key, +new Date());
return true;
} catch (exc) {
return false;
}
}
/**
* The CookieNote module allows to show/hide layers with information about
* the use of cookies on the website(s)
* @class Modules.CookieNote
* @constructor
* @param {DOMNode} rootNode The root node of the widget
* @return {CookieNote} A new instance
*/
class CookieNote {
constructor(rootNode) {
this.$rootNode = this.$root = $(rootNode).css({display: 'block'});
this.$root.on(eventType.CLICK, '[data-action="close"]', CookieNote.accept(this));
}
/*
* Used to remove any CookieNote related DOMNodes if the layer does not
* have to be shown
* @method hide
* @for Modules.CookieNote
* @static
* @param {DOMNode} rootNode The root node to remove
* @return {jQuery} A jQuery collection containing the removed DOM node
*/
static hide (rootNode) {
return $(rootNode).remove();
}
/*
* Used to remove a visible information layer and store the current timestamp
* @method accept
* @for Modules.CookieNote
* @static
* @param {CookieNote} self The layer instance to manage
* @return {function} A procedure as event handler function
*/
static accept (self) {
return function (event) {
event.preventDefault();
CookieNote.hide(self.$root);
setStored();
}
}
/**
* Allows to configure global settings for all CookieNote instances
* @method config
* @for Modules.CookieNote
* @static
* @param {object} options Object of configurable fields
* @param {integer} options.expiresAfter Number of days the layers keep hidden
* @return {Module} The CookieNote module itself
*/
static config (options) {
if (isObject(options)) {
CONFIGS.expire = Math.abs(options.expiresAfter || CONFIGS.expire);
CONFIGS.key = options.storageKey || CONFIGS.key;
}
}
static of (node) {
return new CookieNote({container: node});
}
/**
* Starts the CookieNote module, means it initializes all CookieNote DOM
* nodes at once
* @method startUp
* @for Modules.CookieNote
* @static
* @return {undefined} Nothing
*/
static startUp () {
var now = +new Date(),
last = getStored();
// getStored() returns Number.NEGATIVE_INFINITY if localStorage is not
// available, we use that to indicate a disabled storage and display
// the cookie information
if (last === STORE_DISABLED) {
return CookieNote.of('[data-widget="CookieNote"]');
}
// revisiting user found with elapsed date of expiration, show layer
if (isNumber(last) && now - last > MS_ONE_DAY * CONFIGS.expiresAfter) {
return CookieNote.of('[data-widget="CookieNote"]');
}
// if none of the above conditions passes, simply remove the whole layer
// element from the DOM
CookieNote.hide('[data-widget="CookieNote"]');
}
}
export default CookieNote;