-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathframe_controller.ts
297 lines (233 loc) · 7.99 KB
/
frame_controller.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { FrameElement, FrameElementDelegate, FrameLoadingStyle } from "../../elements/frame_element"
import { FetchMethod, FetchRequest, FetchRequestDelegate, FetchRequestHeaders } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { AppearanceObserver, AppearanceObserverDelegate } from "../../observers/appearance_observer"
import { parseHTMLDocument } from "../../util"
import { FormSubmission, FormSubmissionDelegate } from "../drive/form_submission"
import { Snapshot } from "../snapshot"
import { ViewDelegate } from "../view"
import { expandURL, Locatable } from "../url"
import { FormInterceptor, FormInterceptorDelegate } from "./form_interceptor"
import { FrameView } from "./frame_view"
import { LinkInterceptor, LinkInterceptorDelegate } from "./link_interceptor"
import { FrameRenderer } from "./frame_renderer"
export class FrameController implements AppearanceObserverDelegate, FetchRequestDelegate, FormInterceptorDelegate, FormSubmissionDelegate, FrameElementDelegate, LinkInterceptorDelegate, ViewDelegate<Snapshot<FrameElement>> {
readonly element: FrameElement
readonly view: FrameView
readonly appearanceObserver: AppearanceObserver
readonly linkInterceptor: LinkInterceptor
readonly formInterceptor: FormInterceptor
loadingURL?: string
formSubmission?: FormSubmission
private resolveVisitPromise = () => {}
constructor(element: FrameElement) {
this.element = element
this.view = new FrameView(this, this.element)
this.appearanceObserver = new AppearanceObserver(this, this.element)
this.linkInterceptor = new LinkInterceptor(this, this.element)
this.formInterceptor = new FormInterceptor(this, this.element)
}
connect() {
if (this.loadingStyle == FrameLoadingStyle.lazy) {
this.appearanceObserver.start()
}
this.linkInterceptor.start()
this.formInterceptor.start()
}
disconnect() {
this.appearanceObserver.stop()
this.linkInterceptor.stop()
this.formInterceptor.stop()
}
sourceURLChanged() {
if (this.loadingStyle == FrameLoadingStyle.eager) {
this.loadSourceURL()
}
}
loadingStyleChanged() {
if (this.loadingStyle == FrameLoadingStyle.lazy) {
this.appearanceObserver.start()
} else {
this.appearanceObserver.stop()
this.loadSourceURL()
}
}
async loadSourceURL() {
if (this.isActive && this.sourceURL && this.sourceURL != this.loadingURL) {
try {
this.loadingURL = this.sourceURL
this.element.loaded = this.visit(this.sourceURL)
this.appearanceObserver.stop()
await this.element.loaded
} finally {
delete this.loadingURL
}
}
}
async loadResponse(response: FetchResponse) {
try {
const html = await response.responseHTML
if (html) {
const { body } = parseHTMLDocument(html)
const snapshot = new Snapshot(await this.extractForeignFrameElement(body))
const renderer = new FrameRenderer(this.view.snapshot, snapshot, false)
await this.view.render(renderer)
}
} catch (error) {
console.error(error)
this.view.invalidate()
}
}
// Appearance observer delegate
elementAppearedInViewport(element: Element) {
this.loadSourceURL()
}
// Link interceptor delegate
shouldInterceptLinkClick(element: Element, url: string) {
return this.shouldInterceptNavigation(element)
}
linkClickIntercepted(element: Element, url: string) {
this.navigateFrame(element, url)
}
// Form interceptor delegate
shouldInterceptFormSubmission(element: HTMLFormElement) {
return this.shouldInterceptNavigation(element)
}
formSubmissionIntercepted(element: HTMLFormElement, submitter?: HTMLElement) {
if (this.formSubmission) {
this.formSubmission.stop()
}
this.formSubmission = new FormSubmission(this, element, submitter)
if (this.formSubmission.fetchRequest.isIdempotent) {
this.navigateFrame(element, this.formSubmission.fetchRequest.url.href)
} else {
this.formSubmission.start()
}
}
// Fetch request delegate
prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) {
headers["Turbo-Frame"] = this.id
}
requestStarted(request: FetchRequest) {
this.element.setAttribute("busy", "")
}
requestPreventedHandlingResponse(request: FetchRequest, response: FetchResponse) {
this.resolveVisitPromise()
}
async requestSucceededWithResponse(request: FetchRequest, response: FetchResponse) {
await this.loadResponse(response)
this.resolveVisitPromise()
}
requestFailedWithResponse(request: FetchRequest, response: FetchResponse) {
console.error(response)
this.resolveVisitPromise()
}
requestErrored(request: FetchRequest, error: Error) {
console.error(error)
this.resolveVisitPromise()
}
requestFinished(request: FetchRequest) {
this.element.removeAttribute("busy")
}
// Form submission delegate
formSubmissionStarted(formSubmission: FormSubmission) {
}
formSubmissionSucceededWithResponse(formSubmission: FormSubmission, response: FetchResponse) {
const frame = this.findFrameElement(formSubmission.formElement)
frame.delegate.loadResponse(response)
}
formSubmissionFailedWithResponse(formSubmission: FormSubmission, fetchResponse: FetchResponse) {
this.element.delegate.loadResponse(fetchResponse)
}
formSubmissionErrored(formSubmission: FormSubmission, error: Error) {
}
formSubmissionFinished(formSubmission: FormSubmission) {
}
// View delegate
viewWillRenderSnapshot(snapshot: Snapshot, isPreview: boolean) {
}
viewRenderedSnapshot(snapshot: Snapshot, isPreview: boolean) {
}
viewInvalidated() {
}
// Private
private async visit(url: Locatable) {
const request = new FetchRequest(this, FetchMethod.get, expandURL(url))
return new Promise<void>(resolve => {
this.resolveVisitPromise = () => {
this.resolveVisitPromise = () => {}
resolve()
}
request.perform()
})
}
private navigateFrame(element: Element, url: string) {
const frame = this.findFrameElement(element)
frame.src = url
}
private findFrameElement(element: Element) {
const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target")
return getFrameElementById(id) ?? this.element
}
async extractForeignFrameElement(container: ParentNode): Promise<FrameElement> {
let element
const id = CSS.escape(this.id)
if (element = activateElement(container.querySelector(`turbo-frame#${id}`))) {
return element
}
if (element = activateElement(container.querySelector(`turbo-frame[src][recurse~=${id}]`))) {
await element.loaded
return await this.extractForeignFrameElement(element)
}
console.error(`Response has no matching <turbo-frame id="${id}"> element`)
return new FrameElement()
}
private shouldInterceptNavigation(element: Element) {
const id = element.getAttribute("data-turbo-frame") || this.element.getAttribute("target")
if (!this.enabled || id == "_top") {
return false
}
if (id) {
const frameElement = getFrameElementById(id)
if (frameElement) {
return !frameElement.disabled
}
}
return true
}
// Computed properties
get id() {
return this.element.id
}
get enabled() {
return !this.element.disabled
}
get sourceURL() {
return this.element.src
}
get loadingStyle() {
return this.element.loading
}
get isLoading() {
return this.formSubmission !== undefined || this.loadingURL !== undefined
}
get isActive() {
return this.element.isActive
}
}
function getFrameElementById(id: string | null) {
if (id != null) {
const element = document.getElementById(id)
if (element instanceof FrameElement) {
return element
}
}
}
function activateElement(element: Node | null) {
if (element && element.ownerDocument !== document) {
element = document.importNode(element, true)
}
if (element instanceof FrameElement) {
return element
}
}