forked from serinth/superapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniapp_framework.js
83 lines (64 loc) · 1.58 KB
/
miniapp_framework.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
import { LitElement, html, css } from 'lit-element';
// Allowed templates below
class NativeElement extends LitElement {
render(){
return html`
<button @click="${this.handleClick}">Native Func Request</button>
`;
}
handleClick(e) {
console.log('--in native--');
_superapp.callphone();
}
}
class SimpleElement extends LitElement {
static get properties() {
return { data: { type: String } };
}
constructor() {
super();
this.id = '';
this.data = 'No Change';
}
static get styles() {
return css`
div { background-color: light-gray; }
* { color: blue; }
`}
render(){
return html`
<button @click="${this.handleClick}">Simple Element</button>
<div>Response:${this.data}</div>
`;
}
handleClick(e) {
console.log('--in simple--');
_superapp.mySDKFunc({target: this, callbackFnName: 'callback'});
}
callback(msg) {
this.data = msg;
}
}
customElements.define('native-element', NativeElement);
customElements.define('simple-element', SimpleElement);
/* Core lib that should allow native functionality and callbacks */
// Observer pattern
document._observer = {
listeners: [],
registerListener: function(callback) {
this.listeners.push(callback);
},
notify: function(msg) {
this.listeners.forEach(cb => cb.target[cb.callbackFnName](msg));
this.listeners = [];
}
};
var _superapp = {
callphone: function() {
SUPA.postMessage('phoneCall');
},
mySDKFunc: function(callback) {
document._observer.registerListener(callback);
SUPA.postMessage('onMyFunc');
}
};