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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
odoo.define("website_event_track.website_event_pwa_widget", function (require) {
"use strict";
/*
* The "deferredPrompt" Promise will resolve only if the "beforeinstallprompt" event
* has been triggered. It allows to register this listener as soon as possible
* to avoid missed-events (as the browser can trigger it very early in the page lifecycle).
*/
var deferredPrompt = new Promise(function (resolve, reject) {
if (!("serviceWorker" in navigator)) {
return reject();
}
window.addEventListener("beforeinstallprompt", function (ev) {
ev.preventDefault();
resolve(ev);
});
});
var config = require("web.config");
var publicWidget = require("web.public.widget");
var utils = require("web.utils");
var PWAInstallBanner = publicWidget.Widget.extend({
xmlDependencies: ["/website_event_track/static/src/xml/website_event_pwa.xml"],
template: "pwa_install_banner",
events: {
"click .o_btn_install": "_onClickInstall",
"click .o_btn_close": "_onClickClose",
},
/**
* @private
*/
_onClickClose: function () {
this.trigger_up("prompt_close_bar");
},
/**
* @private
*/
_onClickInstall: function () {
this.trigger_up("prompt_install");
},
});
publicWidget.registry.WebsiteEventPWAWidget = publicWidget.Widget.extend({
selector: "#wrapwrap.event",
custom_events: {
prompt_install: "_onPromptInstall",
prompt_close_bar: "_onPromptCloseBar",
},
/**
*
* @override
*/
start: function () {
var self = this;
return this._super.apply(this, arguments)
.then(this._registerServiceWorker.bind(this))
.then(function () {
// Don't wait for the prompt's Promise as it may never resolve.
deferredPrompt.then(self._showInstallBanner.bind(self)).catch(function () {
console.log("ServiceWorker not supported");
});
})
.then(this._prefetch.bind(this));
},
/**
*
* @override
*/
destroy: function () {
this._super.apply(this, arguments);
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Returns the PWA's scope
*
* Note: this method performs a matching to handle URLs with the language prefix.
* Typically this prefix is in the form of "en" or "en_US" but it can also be
* any string using the customization options in the Website's settings.
* @private
* @returns {String}
*/
_getScope: function () {
var matches = window.location.pathname.match(/^(\/(?:event|[^/]+\/event))\/?/);
if (matches && matches[1]) {
return matches[1];
}
return "/event";
},
/**
* @private
*/
_hideInstallBanner: function () {
this.installBanner ? this.installBanner.destroy() : undefined;
$(".o_livechat_button").css("bottom", "0");
},
/**
* Parse the current page for first-level children pages and ask the ServiceWorker
* to already fetch them to populate the cache.
* @private
*/
_prefetch: function () {
if (!("serviceWorker" in navigator)) {
return;
}
var assetsUrls = Array.from(document.querySelectorAll('link[rel="stylesheet"], script[src]')).map(function (el) {
return el.href || el.src;
});
navigator.serviceWorker.ready.then(function (registration) {
registration.active.postMessage({
action: "prefetch-assets",
urls: assetsUrls,
});
}).catch(function (error) {
console.error("Service worker ready failed, error:", error);
});
var currentPageUrl = window.location.href;
var childrenPagesUrls = Array.from(document.querySelectorAll('a[href^="' + this._getScope() + '/"]')).map(function (el) {
return el.href;
});
navigator.serviceWorker.ready.then(function (registration) {
registration.active.postMessage({
action: "prefetch-pages",
urls: childrenPagesUrls.concat(currentPageUrl),
});
}).catch(function (error) {
console.error("Service worker ready failed, error:", error);
});
},
/**
*
* @private
*/
_registerServiceWorker: function () {
if (!("serviceWorker" in navigator)) {
return;
}
var scope = this._getScope();
return navigator.serviceWorker
.register(scope + "/service-worker.js", { scope: scope })
.then(function (registration) {
console.info("Registration successful, scope is:", registration.scope);
})
.catch(function (error) {
console.error("Service worker registration failed, error:", error);
});
},
/**
* @private
*/
_showInstallBanner: function () {
if (!config.device.isMobile) {
return;
}
var self = this;
this.installBanner = new PWAInstallBanner(this);
this.installBanner.appendTo(this.$el).then(function () {
// If Livechat available, It should be placed above the PWA banner.
var height = self.$(".o_pwa_install_banner").outerHeight(true);
$(".o_livechat_button").css("bottom", height + "px");
});
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param ev {Event}
*/
_onPromptCloseBar: function (ev) {
ev.stopPropagation();
this._hideInstallBanner();
},
/**
* @private
* @param ev {Event}
*/
_onPromptInstall: function (ev) {
ev.stopPropagation();
this._hideInstallBanner();
deferredPrompt.then(function (prompt) {
prompt.prompt();
prompt.userChoice.then(function (choiceResult) {
if (choiceResult.outcome === "accepted") {
console.log("User accepted the install prompt");
} else {
console.log("User dismissed the install prompt");
}
});
})
.catch(function () {
console.log("ServiceWorker not supported");
});
},
});
return {
PWAInstallBanner: PWAInstallBanner,
WebsiteEventPWAWidget: publicWidget.registry.WebsiteEventPWAWidget,
};
});
|