forked from GerroDen/vue2-product-fruits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (112 loc) · 3.06 KB
/
index.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
/**
* Content free component to add [Product Fruits](https://docs.productfruits.com/) and change user information after login.
* Also requires a call of `pageChanged()` when routing changed.
*/
export default {
name: "ProductFruits",
props: {
projectCode: {type: String, required: true},
language: {type: String, required: true},
username: {type: String, required: true},
email: String,
firstname: String,
lastname: String,
signUpAt: String,
role: String,
/** @type {Record<string, string | number | boolean>} */
props: Object,
},
mounted() {
const {
projectCode,
language,
} = this.$props;
if (!projectCode || !language || !isDOMReady()) {
console.info("PF - dom is not ready, projectCode is not set or language is not set");
return;
}
if (!window.productFruits) {
this.setUserConfig();
window.pfDisableUrlChangeDetection = true;
((w, d, u, c) => {
const headEl = d.getElementsByTagName("head")[0];
const scriptEl = d.createElement("script");
scriptEl.async = 1;
scriptEl.src = `${u}?c=${c}`;
this.scriptElement = scriptEl;
headEl.appendChild(scriptEl);
})(window, document, "https://app.productfruits.com/static/script.js", projectCode, language);
}
if (window.productFruitsUnmounted && window.productFruitsInit) {
window.productFruitsInit();
delete window.productFruitsUnmounted;
}
console.debug(`product fruits mounted with ${JSON.stringify(this.$props)}`);
},
updated() {
if (!isDOMReady()) return;
this.setUserConfig();
console.debug(`product fruits updated with ${JSON.stringify(this.$props)}`);
},
beforeDestroy() {
if (!isDOMReady() || !window.productFruits || !window.productFruits.services) return;
window.productFruits.services.destroy();
delete window.productFruits;
delete window.productFruitsUser;
window.productFruitsUnmounted = true;
this.scriptElement && this.scriptElement.remove();
console.debug("product fruits unmounted");
},
methods: {
setUserConfig() {
const {
projectCode,
language,
username,
email,
firstname,
lastname,
signUpAt,
role,
props,
} = this.$props;
if (!window.productFruits || !window.productFruits.identifyUser) {
window.productFruitsUser = {
username,
email,
firstname,
lastname,
signUpAt,
role,
props,
};
} else {
window.productFruits.identifyUser({
username,
email,
firstname,
lastname,
signUpAt,
role,
props,
});
}
window.productFruits = window.productFruits || {};
const fireLanguageChangedEvent = window.productFruits.language && window.productFruits.language !== language;
window.productFruits.language = language;
window.productFruits.code = projectCode;
if (fireLanguageChangedEvent) {
document.dispatchEvent(new CustomEvent("pf:language_changed"));
}
},
},
render(createElement) {
return createElement(null, `product fruits ${JSON.stringify(this.$props)}`);
},
};
/**
* @return {boolean}
*/
function isDOMReady() {
return window && window.document && window.document.createElement;
}