-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.js
More file actions
131 lines (106 loc) · 2.82 KB
/
manager.js
File metadata and controls
131 lines (106 loc) · 2.82 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
window.datalayer = [];
const DB_NAME = "TY_OBSERVER_DB";
const DB_VERSION = 1;
const DB_STORE_BOUTIQUES = "boutiqueItems";
const DB_STORE_PRODUCTS = "productItems";
const IDB_EXIST = "idb-exist";
/*
DOMObserver class
*/
function DOMObserver(elemToObserve, options, cb) {
this.observer = null;
this.options = options || {};
this.elemToObserve = elemToObserve || [];
this.cb = cb;
}
DOMObserver.prototype.init = function () {
if (this.__isWorkerSupported()) {
this.__start();
this.__addElems();
}
};
DOMObserver.prototype.__isWorkerSupported = function () {
return window.Worker && window.IntersectionObserver;
};
DOMObserver.prototype.__start = function () {
const self = this;
this.observer = new window.IntersectionObserver((entries, observer)=>{
const messages = [];
entries.forEach((entry)=>{
if(entry.isIntersecting && entry.intersectionRatio > this.options.threshold) {
let message = {
id: parseInt(entry.target.id),
class: entry.target.classList.toString(),
storeName: entry.target.dataset.type
};
console.log(message);
messages.push(message);
}
});
self.cb(messages);
}, self.options);
};
DOMObserver.prototype.__addElems = function () {
this.elemToObserve.forEach((elem) => {
this.observer.observe(elem);
});
};
/*
* Manager Class
*/
function Manager(config) {
this.config = config;
this.worker = null;
this.domobserver = null;
}
Manager.prototype.init = function () {
const self = this;
this.createWorker(()=>{
// first handle coming messages listener event
this.worker.onmessage = function (message) {
const item = message.data;
window.datalayer.push(item); // change
};
// then init dom observer
self.initDOMObserver((message)=>{
// pass the postmessage event as callback to
// domobserver while starting
self.worker.postMessage(message);
});
});
};
Manager.prototype.initDOMObserver = function (cb) {
this.domobserver = new DOMObserver(
this.config.domObserverConfig.elemToObserve,
this.config.domObserverConfig.options,
cb
);
this.domobserver.init();
console.log("dom observer init");
};
Manager.prototype.createWorker = function (cb) {
this.worker = new Worker(this.config.workerFile);
console.log("worker created");
cb();
};
const manager = new Manager({
workerFile: "worker.js",
domObserverConfig: {
elemToObserve: Array.from(document.getElementsByClassName("block")),
options: {
root: null,
rootMargin: '0px',
threshold: 0.6
}
}
});
if (!sessionStorage.getItem(IDB_EXIST)) {
const request = indexedDB.open(DB_NAME);
request.onsuccess = function(e) {
indexedDB.deleteDatabase(DB_NAME);
manager.init();
sessionStorage.setItem(IDB_EXIST, true);
};
} else {
manager.init();
}