-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.ts
More file actions
149 lines (133 loc) · 3.73 KB
/
index.ts
File metadata and controls
149 lines (133 loc) · 3.73 KB
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
import { html, LitElement, unsafeCSS } from 'lit'
import { state } from 'lit/decorators.js'
import { createRef, ref, type Ref } from 'lit/directives/ref.js'
import {
AllSet,
ContributionRequired,
InstallRequired,
} from './components/index.js'
import { NO_OP_CONTROLLER } from './controller.js'
import type { Actions, Controller, Screen } from './controller.js'
import styles from './styles.css?raw'
import styleTokens from './vars.css?raw'
const COMPONENTS = {
'wm-offerwall-install-required': InstallRequired,
'wm-offerwall-all-set': AllSet,
'wm-offerwall-contribution-required': ContributionRequired,
}
const ALLOWED_SCREENS: Screen[] = [
'install-required',
'contribution-required',
'all-set',
]
export class OfferwallModal extends LitElement {
static styles = [unsafeCSS(styleTokens), unsafeCSS(styles)]
constructor() {
super()
}
#controller: Controller = NO_OP_CONTROLLER
setController(controller: Controller): Actions {
if (this.#controller !== NO_OP_CONTROLLER) {
throw new Error('Controller already set')
}
this.#controller = controller
return {
setScreen: (screen: Screen) => this.#setScreen(screen),
}
}
@state() _screen: Screen = 'install-required'
#setScreen(screen: Screen) {
if (!ALLOWED_SCREENS.includes(screen)) {
throw new Error('Invalid screen')
}
this._screen = screen
this._loading = false
}
// TODO: unused for now.
@state() _loading = false
setLoading(loading: boolean) {
this._loading = loading
}
connectedCallback(): void {
super.connectedCallback()
for (const [name, elConstructor] of Object.entries(COMPONENTS)) {
if (!customElements.get(name)) {
customElements.define(name, elConstructor)
}
}
}
firstUpdated() {
this.#openDialog()
}
render() {
const isPreviewMode = !!this.#controller.isPreviewMode
return html`
<dialog
${ref(this.#dialogRef)}
@cancel=${this.#onDialogCancel}
?data-preview=${isPreviewMode}
>
${this.#renderScreen(this._screen)}
</dialog>
`
}
#renderScreen(screen: Screen) {
switch (screen) {
case 'install-required':
return html`
<wm-offerwall-install-required
@close=${this.#onInstallScreenClose}
@click-extension-link=${this.#onExtensionLinkClick}
></wm-offerwall-install-required>
`
case 'all-set':
return html`
<wm-offerwall-all-set
@all-set-done=${this.#onAllSetDone}
></wm-offerwall-all-set>
`
case 'contribution-required':
return html`
<wm-offerwall-contribution-required></wm-offerwall-contribution-required>
`
}
}
#onDialogCancel = (ev: Event) => {
if (this._screen === 'install-required') {
return this.#onInstallScreenClose(ev)
}
if (this._screen === 'all-set') {
return this.#onAllSetDone(ev)
}
ev.preventDefault()
}
#onInstallScreenClose = (ev: MouseEvent | Event) => {
this.#controller.onModalClose(ev)
if (ev.defaultPrevented) return
this.#closeDialog()
}
#onExtensionLinkClick = (ev: Event) => {
this.#controller.onExtensionLinkClick(ev)
if (ev.defaultPrevented) return
}
#onAllSetDone = (ev: Event) => {
this.#controller.onDone(ev)
if (ev.defaultPrevented) return
this.#closeDialog()
}
#dialogRef: Ref<HTMLDialogElement> = createRef()
#openDialog() {
if (this.#controller.isPreviewMode) {
const dialog = this.#dialogRef.value!
dialog.inert = true
dialog.show()
dialog.inert = false
return
}
this.#dialogRef.value!.showModal()
}
#closeDialog = () => {
this.#dialogRef.value!.close()
this.#dialogRef.value!.remove()
}
}