-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (86 loc) · 2.7 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
const init = (config) => {
if (!config.appId) {
console.warn("no app id");
}
const vm = new Vue({
data() {
return {
ready: false,
visible: false,
unreadCount: 0
};
}
});
const intercom = { _vm: vm };
Object.defineProperties(
intercom,
mapInstanceToProps(vm, ["ready", "visible", "unreadCount"])
);
window._support["account"] = config.appId;
window._support["ui"] = {};
window._support["ui"]["contactMode"] = config.contactMode ? config.contactMode : "traditional";
window._support['ui']['faces'] = config.faces ? config.faces: [];
window._support["ui"]["enableKb"] = "true";
window._support["ui"]["styles"] = {
widgetColor: config.color
};
window._support["ui"]["widget"] = {
img: config.img,
size: config.size,
label: config.label ? config.label : false,
position: config.position
};
window._support["apps"] = {
faq: { enabled: true },
recentConversations: {},
orders: {}
};
};
let installed = false;
let Vue;
init.install = function install(_Vue, config) {
assert(!Vue, "already installed.");
Vue = _Vue;
Vue.mixin({
mounted() {
callIf(!installed, () => {
const loaded = () => init.loadScript(() => {
setTimeout(() => {
init(config);
window.Reamaze.reload();
Object.defineProperty(Vue.prototype, "$reamaze", {
get: () => window.Reamaze
});
}, 100);
});
if (document.readyState === "complete") {
loaded();
} else if (window.attachEvent) {
window.attachEvent("onload", loaded);
} else {
window.addEventListener("load", loaded, false);
}
installed = true;
});
}
});
};
init.loadScript = function loadScript(done) {
const script = document.createElement("script");
script.async = true;
script.src = "https://cdn.reamaze.com/assets/reamaze.js";
const firstScript = document.getElementsByTagName("script")[0];
firstScript.parentNode.insertBefore(script, firstScript);
script.onload = done;
};
const callIf = (a, f) => a && f();
const assert = (condition, msg) =>
callIf(!condition, () => {
throw new Error(`[vue-reamaze] ${msg}`);
});
const mapInstanceToProps = (vm, props) => {
const o = {};
props.forEach(p => (o[p] = { get: () => vm[p] }));
return o;
};
export default init;