-
Notifications
You must be signed in to change notification settings - Fork 420
/
frame_controller.ts
255 lines (204 loc) · 7.21 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
import { FetchMethod, FetchRequest, FetchRequestDelegate } from "../../http/fetch_request"
import { FetchResponse } from "../../http/fetch_response"
import { FormInterceptor, FormInterceptorDelegate } from "./form_interceptor"
import { FormSubmission, FormSubmissionDelegate } from "../drive/form_submission"
import { FrameElement } from "../../elements/frame_element"
import { LinkInterceptor, LinkInterceptorDelegate } from "./link_interceptor"
import { Locatable, Location } from "../location"
import { nextAnimationFrame } from "../../util"
export class FrameController implements FetchRequestDelegate, FormInterceptorDelegate, FormSubmissionDelegate, LinkInterceptorDelegate {
readonly element: FrameElement
readonly linkInterceptor: LinkInterceptor
readonly formInterceptor: FormInterceptor
formSubmission?: FormSubmission
private resolveVisitPromise = () => {}
constructor(element: FrameElement) {
this.element = element
this.linkInterceptor = new LinkInterceptor(this, this.element)
this.formInterceptor = new FormInterceptor(this, this.element)
}
connect() {
this.linkInterceptor.start()
this.formInterceptor.start()
}
disconnect() {
this.linkInterceptor.stop()
this.formInterceptor.stop()
}
shouldInterceptLinkClick(element: Element, url: string) {
return this.shouldInterceptNavigation(element)
}
linkClickIntercepted(element: Element, url: string) {
this.navigateFrame(element, url)
}
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)
} else {
this.formSubmission.start()
}
}
async visit(url: Locatable) {
const location = Location.wrap(url)
const request = new FetchRequest(this, FetchMethod.get, location)
return new Promise<void>(resolve => {
this.resolveVisitPromise = () => {
this.resolveVisitPromise = () => {}
resolve()
}
request.perform()
})
}
additionalHeadersForRequest(request: FetchRequest) {
return { "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")
}
formSubmissionStarted(formSubmission: FormSubmission) {
}
formSubmissionSucceededWithResponse(formSubmission: FormSubmission, response: FetchResponse) {
const frame = this.findFrameElement(formSubmission.formElement)
frame.controller.loadResponse(response)
}
formSubmissionFailedWithResponse(formSubmission: FormSubmission, fetchResponse: FetchResponse) {
}
formSubmissionErrored(formSubmission: FormSubmission, error: Error) {
}
formSubmissionFinished(formSubmission: FormSubmission) {
}
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")
return getFrameElementById(id) ?? this.element
}
private async loadResponse(response: FetchResponse): Promise<void> {
const fragment = fragmentFromHTML(await response.responseHTML)
const element = await this.extractForeignFrameElement(fragment)
if (element) {
await nextAnimationFrame()
this.loadFrameElement(element)
this.scrollFrameIntoView(element)
await nextAnimationFrame()
this.focusFirstAutofocusableElement()
}
}
private async extractForeignFrameElement(container: ParentNode): Promise<FrameElement | undefined> {
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)
}
}
private loadFrameElement(frameElement: FrameElement) {
const destinationRange = document.createRange()
destinationRange.selectNodeContents(this.element)
destinationRange.deleteContents()
const sourceRange = frameElement.ownerDocument?.createRange()
if (sourceRange) {
sourceRange.selectNodeContents(frameElement)
this.element.appendChild(sourceRange.extractContents())
}
}
private focusFirstAutofocusableElement(): boolean {
const element = this.firstAutofocusableElement
if (element) {
element.focus()
return true
}
return false
}
private scrollFrameIntoView(frame: FrameElement): boolean {
if (this.element.autoscroll || frame.autoscroll) {
const element = this.element.firstElementChild
const block = readScrollLogicalPosition(this.element.getAttribute("data-autoscroll-block"), "end")
if (element) {
element.scrollIntoView({ block })
return true
}
}
return false
}
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
}
get firstAutofocusableElement(): HTMLElement | null {
const element = this.element.querySelector("[autofocus]")
return element instanceof HTMLElement ? element : null
}
get id() {
return this.element.id
}
get enabled() {
return !this.element.disabled
}
}
function getFrameElementById(id: string | null) {
if (id != null) {
const element = document.getElementById(id)
if (element instanceof FrameElement) {
return element
}
}
}
function readScrollLogicalPosition(value: string | null, defaultValue: ScrollLogicalPosition): ScrollLogicalPosition {
if (value == "end" || value == "start" || value == "center" || value == "nearest") {
return value
} else {
return defaultValue
}
}
function fragmentFromHTML(html = "") {
const foreignDocument = document.implementation.createHTMLDocument()
return foreignDocument.createRange().createContextualFragment(html)
}
function activateElement(element: Node | null) {
if (element && element.ownerDocument !== document) {
element = document.importNode(element, true)
}
if (element instanceof FrameElement) {
return element
}
}