-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpubcidModule.js
116 lines (99 loc) · 3.56 KB
/
pubcidModule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import PubcidHandler from './pubcidHandler';
import {uuid4} from './utils';
import log from 'loglevel'
/**
* Create a window level PublisherCommonId object that sets and returns pubcid.
* @param {Window} win window object
* @param {Document} doc document object
* @param {object} options Additional options.
*/
export function setupPubcid(win, doc, options = {}) {
const name = 'PublisherCommonId';
const delegate = win[name] || {};
delegate.que = delegate.que || [];
/**
* Process one of the queued commands. This is also mapped to PublisherCommonId.que.push.
* @param {Object[] | function} args An array or a function. If it's an array, then the first item is the method
* name, followed by parameters.
*/
function processQueue(args) {
if (typeof args !== 'function') {
const params = [].slice.call(args); // convert to a proper array
const method = params.shift(); // separate method name from the rest
if (typeof delegate[method] === 'function') {
log.debug(`Processing command: ${method}`)
delegate[method].apply(delegate, params);
}
else {
log.warn(`Skipped unrecognized command: ${method}`);
}
} else {
log.debug('Processing anonymous function');
args();
}
}
const _handler = new PubcidHandler(options);
/* --- synchronous methods --- */
/**
* Return the pubcid currently stored. This does not check consent again
* and rely on the check that occurs during module initialization.
* @returns {string} pubcid as a string
*/
delegate.getId = function() {
return _handler.readPubcid() || '';
};
/**
* Create or refresh pubcid in cookie/local storage if there is consent,
* otherwise delete the pubcid.
*/
delegate.init = function() {
_handler.updatePubcidWithConsent();
};
/**
* Create and store a pubcid in cookie/local storage. The caller is responsible for
* checking consent.
*/
delegate.createId = function() {
_handler.createPubcid();
};
/**
* Delete pubcid from cookie/local storage
*/
delegate.deleteId = function() {
_handler.deletePubcid();
};
/**
* Generate a pubcid without storing it. Since it's not stored, each call returns a
* different value.
* @returns {string} pubcid as a string
*/
delegate.generateId = function() {
return uuid4();
};
/* --- asynchronous methods --- */
/**
* Create or refresh pubcid in cookie/local storage if there is consent and
* pass the pubcid to the callback. If there is no consent, then pubcid is deleted
* and a null is passed to the callback.
* @param {function} callback
*/
delegate.updateIdWithConsent = function(callback) {
_handler.updatePubcidWithConsent(callback);
};
/**
* Return the pubcid via the callback if there is consent. Otherwise
* pass null to the callback.
* @param {function} callback
*/
delegate.getIdWithConsent = function(callback) {
_handler.readPubcidWithConsent(callback);
};
/* --- Queue and global object initialization --- */
if (options.autoinit === undefined || options.autoinit)
delegate.init();
delegate.que.forEach((args) => {
processQueue(args);
});
delegate.que.push = processQueue; // Intercept array push
win[name] = delegate; // Assign delegate to window.PublisherCommonId
}