html2apk 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ (function () {
2
+ "use strict";
3
+
4
+ var lastPayload = "";
5
+ var scheduled = false;
6
+ var observer = null;
7
+
8
+ function clamp(value) {
9
+ var number = Number(value);
10
+ if (!Number.isFinite(number)) {
11
+ return 0;
12
+ }
13
+ return Math.max(0, Math.min(255, Math.round(number)));
14
+ }
15
+
16
+ function toHex(red, green, blue) {
17
+ return "#" + [red, green, blue].map(function (part) {
18
+ return clamp(part).toString(16).padStart(2, "0");
19
+ }).join("");
20
+ }
21
+
22
+ function parseColor(value) {
23
+ var color = String(value || "").trim();
24
+ var match;
25
+ var parts;
26
+
27
+ if (!color || color === "transparent") {
28
+ return null;
29
+ }
30
+
31
+ if (/^#[0-9a-f]{3}$/i.test(color)) {
32
+ return "#" + color.slice(1).split("").map(function (part) {
33
+ return part + part;
34
+ }).join("").toLowerCase();
35
+ }
36
+
37
+ if (/^#[0-9a-f]{6}$/i.test(color)) {
38
+ return color.toLowerCase();
39
+ }
40
+
41
+ match = color.match(/^rgba?\((.+)\)$/i);
42
+ if (!match) {
43
+ return null;
44
+ }
45
+
46
+ parts = match[1].split(",").map(function (part) {
47
+ return part.trim();
48
+ });
49
+ if (parts.length < 3 || (parts.length > 3 && Number(parts[3]) === 0)) {
50
+ return null;
51
+ }
52
+
53
+ return toHex(parts[0], parts[1], parts[2]);
54
+ }
55
+
56
+ function fallbackColor() {
57
+ return parseColor(window.getComputedStyle(document.body).backgroundColor)
58
+ || parseColor(window.getComputedStyle(document.documentElement).backgroundColor)
59
+ || "#126fff";
60
+ }
61
+
62
+ function colorAt(x, y) {
63
+ var maxX = Math.max(0, window.innerWidth - 1);
64
+ var maxY = Math.max(0, window.innerHeight - 1);
65
+ var element = document.elementFromPoint(Math.max(0, Math.min(maxX, x)), Math.max(0, Math.min(maxY, y)));
66
+
67
+ while (element && element.nodeType === 1) {
68
+ var color = parseColor(window.getComputedStyle(element).backgroundColor);
69
+ if (color) {
70
+ return color;
71
+ }
72
+ element = element.parentElement;
73
+ }
74
+
75
+ return fallbackColor();
76
+ }
77
+
78
+ function isLight(hex) {
79
+ var value = String(hex || "").replace("#", "");
80
+ var red = parseInt(value.slice(0, 2), 16) / 255;
81
+ var green = parseInt(value.slice(2, 4), 16) / 255;
82
+ var blue = parseInt(value.slice(4, 6), 16) / 255;
83
+ var luminance = (0.2126 * red) + (0.7152 * green) + (0.0722 * blue);
84
+ return luminance > 0.62;
85
+ }
86
+
87
+ function applyTheme() {
88
+ var setSystemBarsColor = window.setSystemBarsColor || window.definirCorTema;
89
+ var statusBarColor = colorAt(window.innerWidth / 2, 8);
90
+ var navigationBarColor = colorAt(window.innerWidth / 2, window.innerHeight - 8);
91
+ var payload = {
92
+ statusBarColor: statusBarColor,
93
+ navigationBarColor: navigationBarColor,
94
+ color: statusBarColor,
95
+ darkIcons: isLight(statusBarColor),
96
+ darkNavigationIcons: isLight(navigationBarColor),
97
+ source: "auto"
98
+ };
99
+ var nextPayload = JSON.stringify(payload);
100
+
101
+ scheduled = false;
102
+ if (nextPayload === lastPayload || typeof setSystemBarsColor !== "function") {
103
+ return;
104
+ }
105
+
106
+ lastPayload = nextPayload;
107
+ setSystemBarsColor(payload).catch(function () {});
108
+ }
109
+
110
+ function scheduleThemeUpdate() {
111
+ if (scheduled) {
112
+ return;
113
+ }
114
+
115
+ scheduled = true;
116
+ window.requestAnimationFrame(applyTheme);
117
+ }
118
+
119
+ function startAutoTheme() {
120
+ scheduleThemeUpdate();
121
+ window.addEventListener("scroll", scheduleThemeUpdate, { passive: true });
122
+ window.addEventListener("resize", scheduleThemeUpdate);
123
+ document.addEventListener("visibilitychange", scheduleThemeUpdate);
124
+
125
+ if (window.MutationObserver && !observer) {
126
+ observer = new MutationObserver(scheduleThemeUpdate);
127
+ observer.observe(document.documentElement, {
128
+ attributes: true,
129
+ childList: true,
130
+ subtree: true
131
+ });
132
+ }
133
+
134
+ window.setInterval(scheduleThemeUpdate, 1200);
135
+ }
136
+
137
+ if (document.readyState === "loading") {
138
+ document.addEventListener("DOMContentLoaded", scheduleThemeUpdate);
139
+ } else {
140
+ scheduleThemeUpdate();
141
+ }
142
+
143
+ document.addEventListener("deviceready", startAutoTheme, false);
144
+ }());
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+
3
+ (function () {
4
+ var appId = __HTML2APK_ONESIGNAL_APP_ID__;
5
+ var ready = false;
6
+ var initError = null;
7
+
8
+ function oneSignal() {
9
+ return (window.plugins && window.plugins.OneSignal) || window.OneSignal || null;
10
+ }
11
+
12
+ function requireOneSignal() {
13
+ var sdk = oneSignal();
14
+ if (!sdk) {
15
+ throw new Error("OneSignal is not available. Check whether onesignal-cordova-plugin was installed.");
16
+ }
17
+ return sdk;
18
+ }
19
+
20
+ function dispatch(type, detail) {
21
+ window.dispatchEvent(new CustomEvent(type, { detail: detail || {} }));
22
+ }
23
+
24
+ function asPromise(action) {
25
+ return new Promise(function (resolve, reject) {
26
+ try {
27
+ resolve(action(requireOneSignal()));
28
+ } catch (error) {
29
+ reject(error);
30
+ }
31
+ });
32
+ }
33
+
34
+ function status() {
35
+ return {
36
+ enabled: Boolean(appId),
37
+ habilitado: Boolean(appId),
38
+ ready: ready,
39
+ pronto: ready,
40
+ appId: appId,
41
+ error: initError,
42
+ erro: initError
43
+ };
44
+ }
45
+
46
+ function initialize() {
47
+ if (!appId || ready) {
48
+ return;
49
+ }
50
+
51
+ try {
52
+ var sdk = requireOneSignal();
53
+ sdk.initialize(appId);
54
+ ready = true;
55
+
56
+ if (sdk.Notifications && sdk.Notifications.addEventListener) {
57
+ sdk.Notifications.addEventListener("click", function (event) {
58
+ dispatch("html2apk:push-click", event);
59
+ dispatch("html2apk:onesignal-click", event);
60
+ });
61
+ }
62
+
63
+ dispatch("html2apk:onesignal-ready", status());
64
+ } catch (error) {
65
+ initError = error && error.message ? error.message : String(error);
66
+ dispatch("html2apk:onesignal-error", status());
67
+ }
68
+ }
69
+
70
+ function requestPushPermission(fallbackToSettings) {
71
+ return asPromise(function (sdk) {
72
+ if (!sdk.Notifications || !sdk.Notifications.requestPermission) {
73
+ throw new Error("OneSignal notification permission API is not available.");
74
+ }
75
+ return sdk.Notifications.requestPermission(Boolean(fallbackToSettings));
76
+ });
77
+ }
78
+
79
+ function onPushClick(listener) {
80
+ if (typeof listener !== "function") {
81
+ throw new TypeError("listener must be a function");
82
+ }
83
+
84
+ var sdk = requireOneSignal();
85
+ if (!sdk.Notifications || !sdk.Notifications.addEventListener) {
86
+ throw new Error("OneSignal click listener API is not available.");
87
+ }
88
+
89
+ sdk.Notifications.addEventListener("click", listener);
90
+ return function unsubscribe() {
91
+ if (sdk.Notifications.removeEventListener) {
92
+ sdk.Notifications.removeEventListener("click", listener);
93
+ }
94
+ };
95
+ }
96
+
97
+ function loginPushUser(externalId) {
98
+ return asPromise(function (sdk) {
99
+ if (!externalId) {
100
+ throw new Error("External user id is required.");
101
+ }
102
+ sdk.login(String(externalId));
103
+ return { externalId: String(externalId), loggedIn: true, logado: true };
104
+ });
105
+ }
106
+
107
+ function logoutPushUser() {
108
+ return asPromise(function (sdk) {
109
+ if (sdk.logout) {
110
+ sdk.logout();
111
+ }
112
+ return { loggedOut: true, deslogado: true };
113
+ });
114
+ }
115
+
116
+ function addPushTag(key, value) {
117
+ return asPromise(function (sdk) {
118
+ if (!sdk.User || !sdk.User.addTag) {
119
+ throw new Error("OneSignal user tag API is not available.");
120
+ }
121
+ sdk.User.addTag(String(key), String(value));
122
+ return { key: String(key), value: String(value), saved: true, salvo: true };
123
+ });
124
+ }
125
+
126
+ function addPushTags(tags) {
127
+ return asPromise(function (sdk) {
128
+ if (!tags || typeof tags !== "object" || Array.isArray(tags)) {
129
+ throw new TypeError("tags must be an object.");
130
+ }
131
+ if (!sdk.User || !sdk.User.addTags) {
132
+ throw new Error("OneSignal user tag API is not available.");
133
+ }
134
+ sdk.User.addTags(tags);
135
+ return { tags: tags, saved: true, salvo: true };
136
+ });
137
+ }
138
+
139
+ window.statusOneSignal = status;
140
+ window.oneSignalStatus = status;
141
+ window.solicitarPermissaoPush = requestPushPermission;
142
+ window.requestPushPermission = requestPushPermission;
143
+ window.aoClicarPush = onPushClick;
144
+ window.onPushClick = onPushClick;
145
+ window.identificarUsuarioPush = loginPushUser;
146
+ window.loginPushUser = loginPushUser;
147
+ window.sairUsuarioPush = logoutPushUser;
148
+ window.logoutPushUser = logoutPushUser;
149
+ window.adicionarTagPush = addPushTag;
150
+ window.addPushTag = addPushTag;
151
+ window.adicionarTagsPush = addPushTags;
152
+ window.addPushTags = addPushTags;
153
+
154
+ document.addEventListener("deviceready", initialize, false);
155
+ })();