-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
225 lines (193 loc) · 6.29 KB
/
main.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
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
216
217
218
219
220
221
222
223
224
225
import type from "can-type"
import StacheElement from "can-stache-element"
import route from "can-route"
import "can-stache-route-helpers"
import view from "./app.stache"
import { ssgDefineElement, ssgEnd, prepareRouting } from "./client-helpers/ssg-helpers.js"
import "./styles.css"
import "./components/root/root"
prepareRouting(route)
route.register("{page}", { page: "home" })
route.register("{page}/", { page: "home" }) // To support trailing `/`
route.register("tasks/{taskId}", { page: "tasks" })
route.register("tasks/{taskId}/", { page: "tasks" }) // To support trailing `/`
route.register("progressive-loading/{loadId}", { page: "progressive-loading" })
route.register("progressive-loading/{loadId}/", { page: "progressive-loading" }) // To support trailing `/`
route.start()
class MyRoutingApp extends StacheElement {
static view = `
{{ this.componentToShow }}
<div>
<span>Routes: </span>
<a href="{{ routeUrl(page='home') }}">Home</a>
<a href="{{ routeUrl(page='tasks') }}">Tasks</a>
<a href="{{ routeUrl(page='unknown') }}">404</a>
<a href="{{ routeUrl(page='progressive-loading') }}">Progressive Loading</a>
</div>
<p>The current page is {{ this.routeData.page }}.</p>
`
static props = {
routeData: {
get default() {
return route.data
},
},
}
get componentToShow() {
console.log("route.data.page", this.routeData.page)
switch (this.routeData.page) {
case "home":
const home = document.createElement("h2")
home.innerHTML = "Home"
return home
case "tasks":
const tasks = document.createElement("h2")
tasks.innerHTML = "Tasks"
return tasks
case "progressive-loading":
return document.createElement("progressive-root")
default:
const page404 = document.createElement("h2")
page404.innerHTML = "Page Missing"
return page404
}
}
}
ssgDefineElement("my-routing-app", MyRoutingApp)
// class ValueFromInput extends StacheElement {
// static view = `
// <input value:from="count"/>
// `
// static props = {
// count: {
// // Makes count increase by 1 every
// // second.
// value(prop) {
// let count = prop.resolve(20)
// let timer = setInterval(() => {
// prop.resolve(++count)
// }, 1000)
// // Return a cleanup function
// // that is called when count
// // is longer used.
// return () => {
// clearTimeout(timer)
// }
// },
// },
// }
// }
// ssgDefineElement("my-value-from-input", ValueFromInput)
class ValueToInput extends StacheElement {
static view = `
<input value:to="this.count"/> Count: {{ this.count }}
`
static props = {
count: type.convert(Number),
}
}
ssgDefineElement("my-value-to-input", ValueToInput)
function xhrGet(url) {
return new Promise((res) => {
const req = new XMLHttpRequest()
req.onload = () => {
if (req.readyState === req.DONE) {
if (req.status === 200) {
res(JSON.parse(req.responseText))
}
}
}
req.open("GET", url)
req.send()
})
}
class MyStacheElement extends StacheElement {
static view = view
static props = {
message: "Stache is cool",
response: "",
}
connected() {
if (this.INERT_PRERENDERED) {
return
}
setTimeout(() => (this.style.color = "red"), 1500)
xhrGet("https://dummyjson.com/products?limit=10&skip=10&select=title,price").then((jsonData) => {
this.response = jsonData.products[0].title
})
}
}
ssgDefineElement("my-stache-element", MyStacheElement)
// Extend Component to define a custom element
class MyCounter extends StacheElement {
// TODO: https://canjs.com/doc/guides/html.html#Stachetemplatesandbindings
// {{# is(this.count, 1) }} Count is 1 {{ else }} Count is not 1 {{/ if }}
// should end with {{/ is }}
// {{# is(this.count, 1) }} Count is 1 {{ else }} Count is not 1 {{/ is }}
static view = `
<div>Count: <span>{{ this.count }}</span><button on:click='this.increment()'>+1</button></div>
<div>Count using if: {{# if(this.count) }} Count not 0 {{ else }} Count is 0 {{/ if }}</div>
<div>Count using is: {{# is(this.count, 1) }} Count is 1 {{ else }} Count is not 1 {{/ is }}</div>
<div>Count using for: {{# for(item of this.items) }} {{ item.name }} {{/ for }}</div>
`
static props = {
count: 0,
// TODO: example for using `for` isn't working as expected
items: {
get default() {
return [{ name: "some-item" }, { name: "some-next-item" }]
},
},
}
increment() {
this.count++
}
}
ssgDefineElement("my-counter", MyCounter)
class MyApp extends StacheElement {
// <my-value-from-input></my-value-from-input><br>
static view = `
<h1>Hello {{ this.name }}! {{this.response2}}</h1>
<my-stache-element></my-stache-element><br>
<my-value-to-input></my-value-to-input><br>
<my-routing-app></my-routing-app><br>
`
static props = {
name: "world",
response2: "",
}
/**
* as long as you call super.connectedCallback() there's no error in cleanup
*/
// connectedCallback() {
// super.connectedCallback()
// console.log('MyApp - connectedCallback')
// console.log(this.innerHTML)
// this.name = "connectedCallback"
// console.log(this.innerHTML)
// }
constructor() {
super()
// standard lifecycle hooks need to do this so re-attachement/hydration works smoothly
if (this.INERT_PRERENDERED) {
return
}
console.log("constructing can-app")
this.style.color = "lime"
setTimeout(() => (this.style.color = "#552255"), 1000)
}
connected() {
// non-standard lifecycle hooks won't run if this.INERT_PRERENDERED is true
this.style.backgroundColor = "white"
setTimeout(() => (this.style.backgroundColor = "violet"), 1000)
console.log("MyApp - connected")
this.name = "canjs"
this.appendChild(document.createElement("my-counter"))
xhrGet("https://dummyjson.com/products?limit=2&skip=10&select=title,price").then((jsonData) => {
this.response2 = jsonData.products[1].title
})
}
}
ssgDefineElement("can-app", MyApp)
ssgEnd()
console.log("href", window.location.href)