-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathupdates_for_element.js
142 lines (119 loc) · 3.96 KB
/
updates_for_element.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
import morphdom from 'morphdom'
import CableReady from '..'
import SubscribingElement from './subscribing_element'
import { shouldMorph } from '../morph_callbacks'
import activeElement from '../active_element'
import { debounce, assignFocus, dispatch, graciouslyFetch } from '../utils'
const template = `
<style>
:host {
display: block;
}
</style>
<slot></slot>
`
function url (ele) {
return ele.hasAttribute('url') ? ele.getAttribute('url') : location.href
}
export default class UpdatesForElement extends SubscribingElement {
constructor () {
super()
const shadowRoot = this.attachShadow({ mode: 'open' })
shadowRoot.innerHTML = template
}
async connectedCallback () {
if (this.preview) return
this.update = debounce(this.update.bind(this), this.debounce)
const consumer = await CableReady.consumer
if (consumer) {
this.createSubscription(consumer, 'CableReady::Stream', this.update)
} else {
console.error(
'The `updates-for` helper cannot connect without an ActionCable consumer.\nPlease run `rails generate cable_ready:helpers` to fix this.'
)
}
}
async update (data) {
const identifier = this.getAttribute('identifier')
const query = `updates-for[identifier="${identifier}"]`
const blocks = document.querySelectorAll(query)
if (blocks[0] !== this) return
const only = this.getAttribute('only')
if (
only &&
data.changed &&
!only.split(' ').some(attribute => data.changed.includes(attribute))
)
return
const html = {}
const template = document.createElement('template')
for (let i = 0; i < blocks.length; i++) {
blocks[i].setAttribute('updating', 'updating')
if (!html.hasOwnProperty(url(blocks[i]))) {
const response = await graciouslyFetch(url(blocks[i]), {
'X-Cable-Ready': 'update'
})
html[url(blocks[i])] = await response.text()
}
template.innerHTML = String(html[url(blocks[i])]).trim()
await this.resolveTurboFrames(template.content)
const fragments = template.content.querySelectorAll(query)
if (fragments.length <= i) {
console.warn('Update aborted due to mismatched number of elements')
return
}
activeElement.set(document.activeElement)
const operation = {
element: blocks[i],
html: fragments[i],
permanentAttributeName: 'data-ignore-updates'
}
dispatch(blocks[i], 'cable-ready:before-update', operation)
morphdom(blocks[i], fragments[i], {
childrenOnly: true,
onBeforeElUpdated: shouldMorph(operation),
onElUpdated: _ => {
blocks[i].removeAttribute('updating')
dispatch(blocks[i], 'cable-ready:after-update', operation)
assignFocus(operation.focusSelector)
}
})
}
}
async resolveTurboFrames (documentFragment) {
const reloadingTurboFrames = [
...documentFragment.querySelectorAll(
'turbo-frame[src]:not([loading="lazy"])'
)
]
return Promise.all(
reloadingTurboFrames.map(frame => {
return new Promise(async resolve => {
const frameResponse = await graciouslyFetch(
frame.getAttribute('src'),
{
'Turbo-Frame': frame.id,
'X-Cable-Ready': 'update'
}
)
const frameTemplate = document.createElement('template')
frameTemplate.innerHTML = await frameResponse.text()
// recurse here to get all nested eager loaded frames
await this.resolveTurboFrames(frameTemplate.content)
documentFragment.querySelector(
`turbo-frame#${frame.id}`
).innerHTML = String(
frameTemplate.content.querySelector(`turbo-frame#${frame.id}`)
.innerHTML
).trim()
resolve()
})
})
)
}
get debounce () {
return this.hasAttribute('debounce')
? parseInt(this.getAttribute('debounce'))
: 20
}
}