-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
index.js
163 lines (128 loc) · 4.18 KB
/
index.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
import DOM from '@/dom/dom'
import '@/dom/polyfills/index'
import store from '@/Store'
import Connection from '@/connection'
import Polling from '@/component/Polling'
import Component from '@/component/index'
import { dispatch, wireDirectives } from '@/util'
import FileUploads from '@/component/FileUploads'
import LaravelEcho from '@/component/LaravelEcho'
import DirtyStates from '@/component/DirtyStates'
import DisableForms from '@/component/DisableForms'
import FileDownloads from '@/component/FileDownloads'
import LoadingStates from '@/component/LoadingStates'
import OfflineStates from '@/component/OfflineStates'
import SyncBrowserHistory from '@/component/SyncBrowserHistory'
import SupportAlpine from '@/component/SupportAlpine'
import SupportStacks from '@/component/SupportStacks'
class Livewire {
constructor() {
this.connection = new Connection()
this.components = store
this.devToolsEnabled = false
this.onLoadCallback = () => { }
}
first() {
return Object.values(this.components.componentsById)[0].$wire
}
find(componentId) {
return this.components.componentsById[componentId].$wire
}
all() {
return Object.values(this.components.componentsById).map(
component => component.$wire
)
}
directive(name, callback) {
this.components.registerDirective(name, callback)
}
hook(name, callback) {
this.components.registerHook(name, callback)
}
onLoad(callback) {
this.onLoadCallback = callback
}
onError(callback) {
this.components.onErrorCallback = callback
}
emit(event, ...params) {
this.components.emit(event, ...params)
}
emitTo(name, event, ...params) {
this.components.emitTo(name, event, ...params)
}
on(event, callback) {
this.components.on(event, callback)
}
addHeaders(headers) {
this.connection.headers = { ...this.connection.headers, ...headers}
}
devTools(enableDevtools) {
this.devToolsEnabled = enableDevtools
}
restart() {
this.stop()
this.start()
}
stop() {
this.components.tearDownComponents()
}
start() {
DOM.rootComponentElementsWithNoParents().forEach(el => {
this.components.addComponent(new Component(el, this.connection))
})
this.onLoadCallback()
dispatch('livewire:load')
document.addEventListener(
'visibilitychange',
() => {
this.components.livewireIsInBackground = document.hidden
},
false
)
this.components.initialRenderIsFinished = true
}
rescan(node = null) {
DOM.rootComponentElementsWithNoParents(node).forEach(el => {
const componentId = wireDirectives(el).get('id').value
if (this.components.hasComponent(componentId)) return
this.components.addComponent(new Component(el, this.connection))
})
}
onPageExpired(callback) {
this.components.sessionHasExpiredCallback = callback
}
}
if (!window.Livewire) {
window.Livewire = Livewire
}
monkeyPatchDomSetAttributeToAllowAtSymbols()
SyncBrowserHistory()
SupportAlpine()
SupportStacks()
FileDownloads()
OfflineStates()
LoadingStates()
DisableForms()
FileUploads()
LaravelEcho()
DirtyStates()
Polling()
dispatch('livewire:available')
export default Livewire
function monkeyPatchDomSetAttributeToAllowAtSymbols() {
// Because morphdom may add attributes to elements containing "@" symbols
// like in the case of an Alpine `@click` directive, we have to patch
// the standard Element.setAttribute method to allow this to work.
let original = Element.prototype.setAttribute
let hostDiv = document.createElement('div')
Element.prototype.setAttribute = function newSetAttribute(name, value) {
if (! name.includes('@')) {
return original.call(this, name, value)
}
hostDiv.innerHTML = `<span ${name}="${value}"></span>`
let attr = hostDiv.firstElementChild.getAttributeNode(name)
hostDiv.firstElementChild.removeAttributeNode(attr)
this.setAttributeNode(attr)
}
}