/
phoenix_live_view.js
1117 lines (967 loc) · 31.6 KB
/
phoenix_live_view.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
================================================================================
Phoenix LiveView JavaScript Client
================================================================================
See the hexdocs at `https://hexdocs.pm/phoenix_live_view` for documentation.
*/
import morphdom from "morphdom"
import {Socket} from "phoenix"
const PHX_VIEW = "data-phx-view"
const PHX_LIVE_LINK = "data-phx-live-link"
const PHX_CONNECTED_CLASS = "phx-connected"
const PHX_LOADING_CLASS = "phx-loading"
const PHX_DISCONNECTED_CLASS = "phx-disconnected"
const PHX_ERROR_CLASS = "phx-error"
const PHX_PARENT_ID = "data-phx-parent-id"
const PHX_VIEW_SELECTOR = `[${PHX_VIEW}]`
const PHX_ERROR_FOR = "data-phx-error-for"
const PHX_HAS_FOCUSED = "phx-has-focused"
const PHX_BOUND = "data-phx-bound"
const FOCUSABLE_INPUTS = ["text", "textarea", "number", "email", "password", "search", "tel", "url"]
const PHX_HAS_SUBMITTED = "phx-has-submitted"
const PHX_SESSION = "data-phx-session"
const PHX_STATIC = "data-phx-static"
const PHX_READONLY = "data-phx-readonly"
const PHX_DISABLED = "data-phx-disabled"
const PHX_DISABLE_WITH = "disable-with"
const PHX_HOOK = "hook"
const PHX_UPDATE = "update"
const LOADER_TIMEOUT = 1
const BEFORE_UNLOAD_LOADER_TIMEOUT = 200
const BINDING_PREFIX = "phx-"
const PUSH_TIMEOUT = 30000
const LINK_HEADER = "x-requested-with"
const PHX_PREV_APPEND = "phxPrevAppend"
export let debug = (view, kind, msg, obj) => {
console.log(`${view.id} ${kind}: ${msg} - `, obj)
}
// wraps value in closure or returns closure
let closure = (val) => typeof val === "function" ? val : function(){ return val }
let clone = (obj) => { return JSON.parse(JSON.stringify(obj)) }
let closestPhxBinding = (el, binding) => {
do {
if(el.matches(`[${binding}]`)){ return el }
el = el.parentElement || el.parentNode
} while(el !== null && el.nodeType === 1 && !el.matches(PHX_VIEW_SELECTOR))
return null
}
let isObject = (obj) => {
return obj !== null && typeof obj === "object" && !(obj instanceof Array)
}
let isEmpty = (obj) => {
for (let x in obj){ return false }
return true
}
let maybe = (el, key) => {
if(el){
return el[key]
} else {
return null
}
}
let serializeForm = (form, meta = {}) => {
let formData = new FormData(form)
let params = new URLSearchParams()
for(let [key, val] of formData.entries()){ params.append(key, val) }
for(let metaKey in meta){ params.append(metaKey, meta[metaKey]) }
return params.toString()
}
let recursiveMerge = (target, source) => {
for(let key in source){
let val = source[key]
let targetVal = target[key]
if(isObject(val) && isObject(targetVal)){
if(targetVal.dynamics && !val.dynamics){ delete targetVal.dynamics}
recursiveMerge(targetVal, val)
} else {
target[key] = val
}
}
}
let Session = {
get(el){ return el.getAttribute(PHX_SESSION) },
isEqual(el1, el2){ return this.get(el1) === this.get(el2) }
}
export let Rendered = {
mergeDiff(source, diff){
if(this.isNewFingerprint(diff)){
return diff
} else {
recursiveMerge(source, diff)
return source
}
},
isNewFingerprint(diff = {}){ return !!diff.static },
toString(rendered){
let output = {buffer: ""}
this.toOutputBuffer(rendered, output)
return output.buffer
},
toOutputBuffer(rendered, output){
if(rendered.dynamics){ return this.comprehensionToBuffer(rendered, output) }
let {static: statics} = rendered
output.buffer += statics[0]
for(let i = 1; i < statics.length; i++){
this.dynamicToBuffer(rendered[i - 1], output)
output.buffer += statics[i]
}
},
comprehensionToBuffer(rendered, output){
let {dynamics: dynamics, static: statics} = rendered
for(let d = 0; d < dynamics.length; d++){
let dynamic = dynamics[d]
output.buffer += statics[0]
for(let i = 1; i < statics.length; i++){
this.dynamicToBuffer(dynamic[i - 1], output)
output.buffer += statics[i]
}
}
},
dynamicToBuffer(rendered, output){
if(isObject(rendered)){
this.toOutputBuffer(rendered, output)
} else {
output.buffer += rendered
}
}
}
// todo document LiveSocket specific options like viewLogger
export class LiveSocket {
constructor(url, opts = {}){
this.unloaded = false
this.socket = new Socket(url, opts)
this.bindingPrefix = opts.bindingPrefix || BINDING_PREFIX
this.opts = opts
this.views = {}
this.params = closure(opts.params || {})
this.viewLogger = opts.viewLogger
this.activeElement = null
this.prevActive = null
this.prevInput = null
this.prevValue = null
this.silenced = false
this.root = null
this.linkRef = 0
this.href = window.location.href
this.pendingLink = null
this.currentLocation = clone(window.location)
this.hooks = opts.hooks || {}
this.socket.onOpen(() => {
if(this.isUnloaded()){
this.destroyAllViews()
this.joinRootViews()
}
this.unloaded = false
})
window.addEventListener("beforeunload", e => {
this.unloaded = true
})
this.bindTopLevelEvents()
}
getSocket(){ return this.socket }
log(view, kind, msgCallback){
if(this.viewLogger){
let [msg, obj] = msgCallback()
this.viewLogger(view, kind, msg, obj)
}
}
connect(){
if(["complete", "loaded","interactive"].indexOf(document.readyState) >= 0){
this.joinRootViews()
} else {
document.addEventListener("DOMContentLoaded", () => {
this.joinRootViews()
})
}
return this.socket.connect()
}
disconnect(){ this.socket.disconnect() }
// private
getHookCallbacks(hookName){ return this.hooks[hookName] }
isUnloaded(){ return this.unloaded }
getBindingPrefix(){ return this.bindingPrefix }
binding(kind){ return `${this.getBindingPrefix()}${kind}` }
channel(topic, params){ return this.socket.channel(topic, params) }
joinRootViews(){
Browser.all(document, `${PHX_VIEW_SELECTOR}:not([${PHX_PARENT_ID}])`, rootEl => {
let view = this.joinView(rootEl, null, this.getHref())
this.root = this.root || view
})
}
replaceRoot(href, callback = null, linkRef = this.setPendingLink(href)){
this.root.showLoader(LOADER_TIMEOUT)
let rootEl = this.root.el
let rootID = this.root.id
let wasLoading = this.root.isLoading()
Browser.fetchPage(href, (status, html) => {
if(status !== 200){ return Browser.redirect(href) }
let div = document.createElement("div")
div.innerHTML = html
this.joinView(div.firstChild, null, href, newRoot => {
if(!this.commitPendingLink(linkRef)){
newRoot.destroy()
return
}
callback && callback()
this.destroyViewById(rootID)
rootEl.replaceWith(newRoot.el)
this.root = newRoot
if(wasLoading){ this.root.showLoader() }
})
})
}
joinView(el, parentView, href, callback){
if(this.getViewById(el.id)){ return }
let view = new View(el, this, parentView, href)
this.views[view.id] = view
view.join(callback)
return view
}
owner(childEl, callback){
let view = this.getViewById(maybe(childEl.closest(PHX_VIEW_SELECTOR), "id"))
if(view){ callback(view) }
}
getViewById(id){ return this.views[id] }
onViewError(view){
this.dropActiveElement(view)
}
destroyAllViews(){
for(let id in this.views){ this.destroyViewById(id) }
}
destroyViewById(id){
let view = this.views[id]
if(view){
delete this.views[view.id]
if(this.root && view.id === this.root.id){ this.root = null }
view.destroy()
}
}
setActiveElement(target){
if(this.activeElement === target){ return }
this.activeElement = target
let cancel = () => {
if(target === this.activeElement){ this.activeElement = null }
target.removeEventListener("mouseup", this)
target.removeEventListener("touchend", this)
}
target.addEventListener("mouseup", cancel)
target.addEventListener("touchend", cancel)
}
getActiveElement(){
if(document.activeElement === document.body){
return this.activeElement || document.activeElement
} else {
return document.activeElement
}
}
dropActiveElement(view){
if(this.prevActive && view.ownsElement(this.prevActive)){
this.prevActive = null
}
}
restorePreviouslyActiveFocus(){
if(this.prevActive && this.prevActive !== document.body){
this.prevActive.focus()
}
}
blurActiveElement(){
this.prevActive = this.getActiveElement()
if(this.prevActive !== document.body){ this.prevActive.blur() }
}
bindTopLevelEvents(){
this.bindClicks()
this.bindNav()
this.bindForms()
this.bindTargetable({keyup: "keyup", keydown: "keydown"}, (e, type, view, target, phxEvent, phxTarget) => {
view.pushKey(target, type, phxEvent, {
altGraphKey: e.altGraphKey,
altKey: e.altKey,
charCode: e.charCode,
code: e.code,
ctrlKey: e.ctrlKey,
key: e.key,
keyCode: e.keyCode,
keyIdentifier: e.keyIdentifier,
keyLocation: e.keyLocation,
location: e.location,
metaKey: e.metaKey,
repeat: e.repeat,
shiftKey: e.shiftKey,
which: e.which
})
})
this.bindTargetable({blur: "focusout", focus: "focusin"}, (e, type, view, targetEl, phxEvent, phxTarget) => {
if(!phxTarget){
view.pushEvent(type, targetEl, phxEvent, {type: "focus"})
}
})
this.bindTargetable({blur: "blur", focus: "focus"}, (e, type, view, targetEl, phxEvent, phxTarget) => {
// blur and focus are triggered on document and window. Discard one to avoid dups
if(phxTarget && !phxTarget !== "window"){
view.pushEvent(type, targetEl, phxEvent, {type: e.type})
}
})
}
setPendingLink(href){
this.linkRef++
let ref = this.linkRef
this.pendingLink = href
return this.linkRef
}
commitPendingLink(linkRef){
if(this.linkRef !== linkRef){
return false
} else {
this.href = this.pendingLink
this.pendingLink = null
return true
}
}
getHref(){ return this.href }
hasPendingLink(){ return !!this.pendingLink }
bindTargetable(events, callback){
for(let event in events){
let browserEventName = events[event]
this.on(browserEventName, e => {
let binding = this.binding(event)
let bindTarget = this.binding("target")
let targetPhxEvent = e.target.getAttribute && e.target.getAttribute(binding)
if(targetPhxEvent && !e.target.getAttribute(bindTarget)){
this.owner(e.target, view => callback(e, event, view, e.target, targetPhxEvent, null))
} else {
Browser.all(document, `[${binding}][${bindTarget}=window]`, el => {
let phxEvent = el.getAttribute(binding)
this.owner(el, view => callback(e, event, view, el, phxEvent, "window"))
})
}
})
}
}
bindClicks(){
window.addEventListener("click", e => {
let click = this.binding("click")
let target = closestPhxBinding(e.target, click)
let phxEvent = target && target.getAttribute(click)
if(!phxEvent){ return }
e.preventDefault()
let meta = {
altKey: e.altKey,
shiftKey: e.shiftKey,
ctrlKey: e.ctrlKey,
metaKey: e.metaKey,
x: e.x || e.clientX,
y: e.y || e.clientY,
pageX: e.pageX,
pageY: e.pageY,
screenX: e.screenX,
screenY: e.screenY,
}
this.owner(target, view => view.pushEvent("click", target, phxEvent, meta))
}, false)
}
bindNav(){
if(!Browser.canPushState()){ return }
window.onpopstate = (event) => {
if(!this.registerNewLocation(window.location)){ return }
let href = window.location.href
if(this.root.isConnected()) {
this.root.pushInternalLink(href)
} else {
this.replaceRoot(href)
}
}
window.addEventListener("click", e => {
let target = closestPhxBinding(e.target, PHX_LIVE_LINK)
let phxEvent = target && target.getAttribute(PHX_LIVE_LINK)
if(!phxEvent){ return }
let href = target.href
e.preventDefault()
this.root.pushInternalLink(href, () => {
Browser.pushState(phxEvent, {}, href)
this.registerNewLocation(window.location)
})
}, false)
}
registerNewLocation(newLocation){
let {pathname, search} = this.currentLocation
if(pathname + search === newLocation.pathname + newLocation.search){
return false
} else {
this.currentLocation = clone(newLocation)
return true
}
}
bindForms(){
this.on("submit", e => {
let phxEvent = e.target.getAttribute(this.binding("submit"))
if(!phxEvent){ return }
e.preventDefault()
e.target.disabled = true
this.owner(e.target, view => view.submitForm(e.target, phxEvent))
}, false)
for(let type of ["change", "input"]){
this.on(type, e => {
let input = e.target
let key = input.type === "checkbox" ? "checked" : "value"
if(this.prevInput === input && this.prevValue === input[key]){ return }
this.prevInput = input
this.prevValue = input[key]
let phxEvent = input.form && input.form.getAttribute(this.binding("change"))
if(!phxEvent){ return }
this.owner(input, view => {
if(DOM.isTextualInput(input)){
input[PHX_HAS_FOCUSED] = true
} else {
this.setActiveElement(input)
}
view.pushInput(input, phxEvent, e)
})
}, false)
}
}
silenceEvents(callback){
this.silenced = true
callback()
this.silenced = false
}
on(event, callback){
window.addEventListener(event, e => {
if(!this.silenced){ callback(e) }
})
}
}
export let Browser = {
all(node, query, callback){
node.querySelectorAll(query).forEach(callback)
},
canPushState(){ return (typeof(history.pushState) !== "undefined") },
fetchPage(href, callback){
let req = new XMLHttpRequest()
req.open("GET", href, true)
req.timeout = PUSH_TIMEOUT
req.setRequestHeader("content-type", "text/html")
req.setRequestHeader("cache-control", "max-age=0, no-cache, no-store, must-revalidate, post-check=0, pre-check=0")
req.setRequestHeader(LINK_HEADER, "live-link")
req.onerror = () => callback(400)
req.ontimeout = () => callback(504)
req.onreadystatechange = () => {
if(req.readyState !== 4){ return }
if(req.getResponseHeader(LINK_HEADER) !== "live-link"){ return callback(400) }
if(req.status !== 200){ return callback(req.status) }
callback(200, req.responseText)
}
req.send()
},
pushState(kind, meta, to){
if(this.canPushState()){
if(to !== window.location.href){ history[kind + "State"](meta, "", to) }
} else {
this.redirect(to)
}
},
dispatchEvent(target, eventString){
let event = null
if(typeof(Event) === "function"){
event = new Event(eventString)
} else {
event = document.createEvent("Event")
event.initEvent(eventString, true, true)
}
target.dispatchEvent(event)
},
setCookie(name, value){
document.cookie = `${name}=${value}`
},
getCookie(name){
return document.cookie.replace(new RegExp(`(?:(?:^|.*;\s*)${name}\s*\=\s*([^;]*).*$)|^.*$`), "$1")
},
redirect(toURL, flash){
if(flash){ Browser.setCookie("__phoenix_flash__", flash + "; max-age=60000; path=/") }
window.location = toURL
}
}
let DOM = {
disableForm(form, prefix){
let disableWith = `${prefix}${PHX_DISABLE_WITH}`
form.classList.add(PHX_LOADING_CLASS)
Browser.all(form, `[${disableWith}]`, el => {
let value = el.getAttribute(disableWith)
el.setAttribute(`${disableWith}-restore`, el.innerText)
el.innerText = value
})
Browser.all(form, "button", button => {
button.setAttribute(PHX_DISABLED, button.disabled)
button.disabled = true
})
Browser.all(form, "input", input => {
input.setAttribute(PHX_READONLY, input.readOnly)
input.readOnly = true
})
},
restoreDisabledForm(form, prefix){
let disableWith = `${prefix}${PHX_DISABLE_WITH}`
form.classList.remove(PHX_LOADING_CLASS)
Browser.all(form, `[${disableWith}]`, el => {
let value = el.getAttribute(`${disableWith}-restore`)
if(value){
if(el.nodeName === "INPUT") {
el.value = value
} else {
el.innerText = value
}
el.removeAttribute(`${disableWith}-restore`)
}
})
Browser.all(form, "button", button => {
let prev = button.getAttribute(PHX_DISABLED)
if(prev){
button.disabled = prev === "true"
button.removeAttribute(PHX_DISABLED)
}
})
Browser.all(form, "input", input => {
let prev = input.getAttribute(PHX_READONLY)
if(prev){
input.readOnly = prev === "true"
input.removeAttribute(PHX_READONLY)
}
})
},
discardError(el){
let field = el.getAttribute && el.getAttribute(PHX_ERROR_FOR)
if(!field) { return }
let input = document.getElementById(field)
if(field && !(input[PHX_HAS_FOCUSED] || input.form[PHX_HAS_SUBMITTED])){
el.style.display = "none"
}
},
isPhxChild(node){
return node.getAttribute && node.getAttribute(PHX_PARENT_ID)
},
applyPhxUpdate(fromEl, toEl, phxUpdate, phxHook, changes){
let type = toEl.getAttribute && toEl.getAttribute(phxUpdate)
if(!type || type === "replace"){
return false
} else {
DOM.mergeAttrs(fromEl, toEl)
}
switch(type){
case "ignore": break
case "append":
case "prepend":
let newHTML = toEl.innerHTML
if(fromEl[PHX_PREV_APPEND] === newHTML){ break }
fromEl[PHX_PREV_APPEND] = newHTML
toEl.querySelectorAll("[id]").forEach(el => {
let existing = fromEl.querySelector(`[id="${el.id}"]`)
if(existing){
changes.discarded.push(existing)
el.remove()
existing.replaceWith(el)
}
})
let operation = type === "append" ? "beforeend" : "afterbegin"
fromEl.insertAdjacentHTML(operation, toEl.innerHTML)
fromEl.querySelectorAll(`[${phxHook}]`).forEach(el => changes.added.push(el))
break
default: throw new Error(`unsupported phx-update "${type}"`)
}
changes.updated.push({fromEl, toEl: fromEl})
return true
},
patch(view, container, id, html){
let changes = {added: [], updated: [], discarded: []}
let focused = view.liveSocket.getActiveElement()
let selectionStart = null
let selectionEnd = null
let phxUpdate = view.liveSocket.binding(PHX_UPDATE)
let phxHook = view.liveSocket.binding(PHX_HOOK)
let diffContainer = container.cloneNode()
diffContainer.innerHTML = html
if(DOM.isTextualInput(focused)){
selectionStart = focused.selectionStart
selectionEnd = focused.selectionEnd
}
morphdom(container, diffContainer, {
childrenOnly: true,
onBeforeNodeAdded: function(el){
//input handling
DOM.discardError(el)
return el
},
onNodeAdded: function(el){
// nested view handling
if(DOM.isPhxChild(el) && view.ownsElement(el)){
view.onNewChildAdded()
return true
} else {
changes.added.push(el)
}
},
onBeforeNodeDiscarded: function(el){
// nested view handling
if(DOM.isPhxChild(el)){
view.liveSocket.destroyViewById(el.id)
return true
}
changes.discarded.push(el)
},
onBeforeElUpdated: function(fromEl, toEl) {
if(fromEl.isEqualNode(toEl)){ return false } // Skip subtree if both elems and children are equal
if(DOM.applyPhxUpdate(fromEl, toEl, phxUpdate, phxHook, changes)){
return false
}
// nested view handling
if(DOM.isPhxChild(toEl)){
let prevStatic = fromEl.getAttribute(PHX_STATIC)
if(!Session.isEqual(toEl, fromEl)){
view.liveSocket.destroyViewById(fromEl.id)
view.onNewChildAdded()
}
DOM.mergeAttrs(fromEl, toEl)
fromEl.setAttribute(PHX_STATIC, prevStatic)
return false
}
// input handling
if(fromEl.getAttribute && fromEl[PHX_HAS_SUBMITTED]){
toEl[PHX_HAS_SUBMITTED] = true
}
if(fromEl[PHX_HAS_FOCUSED]){
toEl[PHX_HAS_FOCUSED] = true
}
DOM.discardError(toEl)
if(DOM.isTextualInput(fromEl) && fromEl === focused){
DOM.mergeInputs(fromEl, toEl)
changes.updated.push({fromEl, toEl: fromEl})
return false
} else {
changes.updated.push({fromEl, toEl})
return true
}
}
})
view.liveSocket.silenceEvents(() => {
DOM.restoreFocus(focused, selectionStart, selectionEnd)
})
Browser.dispatchEvent(document, "phx:update")
return changes
},
mergeAttrs(target, source){
var attrs = source.attributes
for (let i = 0, length = attrs.length; i < length; i++){
let name = attrs[i].name
let value = source.getAttribute(name)
target.setAttribute(name, value)
}
},
mergeInputs(target, source){
DOM.mergeAttrs(target, source)
target.readOnly = source.readOnly
},
restoreFocus(focused, selectionStart, selectionEnd){
if(!DOM.isTextualInput(focused)){ return }
if(focused.value === "" || focused.readOnly){ focused.blur()}
focused.focus()
if(focused.setSelectionRange && focused.type === "text" || focused.type === "textarea"){
focused.setSelectionRange(selectionStart, selectionEnd)
}
},
isTextualInput(el){
return FOCUSABLE_INPUTS.indexOf(el.type) >= 0
}
}
export class View {
constructor(el, liveSocket, parentView, href){
this.liveSocket = liveSocket
this.parent = parentView
this.newChildrenAdded = false
this.gracefullyClosed = false
this.el = el
this.id = this.el.id
this.view = this.el.getAttribute(PHX_VIEW)
this.loaderTimer = null
this.pendingDiffs = []
this.href = href
this.joinedOnce = false
this.viewHooks = {}
this.channel = this.liveSocket.channel(`lv:${this.id}`, () => {
return {
url: this.href || this.liveSocket.root.href,
params: this.liveSocket.params(this.view),
session: this.getSession(),
static: this.getStatic()
}
})
this.showLoader(LOADER_TIMEOUT)
this.bindChannel()
}
isConnected(){ return this.channel.canPush() }
getSession(){ return Session.get(this.el) }
getStatic(){
let val = this.el.getAttribute(PHX_STATIC)
return val === "" ? null : val
}
destroy(callback = function(){}){
clearTimeout(this.loaderTimer)
let onFinished = () => {
callback()
for(let id in this.viewHooks){ this.destroyHook(this.viewHooks[id]) }
}
if(this.hasGracefullyClosed()){
this.log("destroyed", () => ["the server view has gracefully closed"])
onFinished()
} else {
this.log("destroyed", () => ["the child has been removed from the parent"])
this.channel.leave()
.receive("ok", onFinished)
.receive("error", onFinished)
.receive("timeout", onFinished)
}
}
setContainerClasses(...classes){
this.el.classList.remove(
PHX_CONNECTED_CLASS,
PHX_DISCONNECTED_CLASS,
PHX_ERROR_CLASS
)
this.el.classList.add(...classes)
}
isLoading(){ return this.el.classList.contains(PHX_DISCONNECTED_CLASS)}
showLoader(timeout){
clearTimeout(this.loaderTimer)
if(timeout){
this.loaderTimer = setTimeout(() => this.showLoader(), timeout)
} else {
for(let id in this.viewHooks){ this.viewHooks[id].__trigger__("disconnected") }
this.setContainerClasses(PHX_DISCONNECTED_CLASS)
}
}
hideLoader(){
clearTimeout(this.loaderTimer)
for(let id in this.viewHooks){ this.viewHooks[id].__trigger__("reconnected") }
this.setContainerClasses(PHX_CONNECTED_CLASS)
}
log(kind, msgCallback){
this.liveSocket.log(this, kind, msgCallback)
}
onJoin({rendered, live_redirect}){
this.log("join", () => ["", JSON.stringify(rendered)])
this.rendered = rendered
this.hideLoader()
let changes = DOM.patch(this, this.el, this.id, Rendered.toString(this.rendered))
changes.added.push(this.el)
Browser.all(this.el, `[${this.binding(PHX_HOOK)}]`, hookEl => changes.added.push(hookEl))
this.triggerHooks(changes)
this.joinNewChildren()
if(live_redirect){
let {kind, to} = live_redirect
Browser.pushState(kind, {}, to)
}
}
joinNewChildren(){
Browser.all(document, `${PHX_VIEW_SELECTOR}[${PHX_PARENT_ID}="${this.id}"]`, el => {
let child = this.liveSocket.getViewById(el.id)
if(!child){
this.liveSocket.joinView(el, this)
}
})
}
update(diff){
if(isEmpty(diff)){ return }
if(this.liveSocket.hasPendingLink()){ return this.pendingDiffs.push(diff) }
this.log("update", () => ["", JSON.stringify(diff)])
this.rendered = Rendered.mergeDiff(this.rendered, diff)
let html = Rendered.toString(this.rendered)
this.newChildrenAdded = false
this.triggerHooks(DOM.patch(this, this.el, this.id, html))
if(this.newChildrenAdded){ this.joinNewChildren() }
}
getHook(el){ return this.viewHooks[ViewHook.elementID(el)] }
addHook(el){ if(ViewHook.elementID(el) || !el.getAttribute){ return }
let callbacks = this.liveSocket.getHookCallbacks(el.getAttribute(this.binding(PHX_HOOK)))
if(callbacks && this.ownsElement(el)){
let hook = new ViewHook(this, el, callbacks)
this.viewHooks[ViewHook.elementID(hook.el)] = hook
hook.__trigger__("mounted")
}
}
destroyHook(hook){
hook.__trigger__("destroyed")
delete this.viewHooks[ViewHook.elementID(hook.el)]
}
triggerHooks(changes){
changes.updated.push({fromEl: this.el, toEl: this.el})
changes.added.forEach(el => this.addHook(el))
changes.updated.forEach(({fromEl, toEl}) => {
let hook = this.getHook(fromEl)
let phxAttr = this.binding(PHX_HOOK)
if(hook && toEl.getAttribute && fromEl.getAttribute(phxAttr) === toEl.getAttribute(phxAttr)){
hook.__trigger__("updated")
} else if(hook){
this.destroyHook(hook)
this.addHook(fromEl)
}
})
changes.discarded.forEach(el => {
let hook = this.getHook(el)
hook && this.destroyHook(hook)
})
}
applyPendingUpdates(){
this.pendingDiffs.forEach(diff => this.update(diff))
this.pendingDiffs = []
}
onNewChildAdded(){
this.newChildrenAdded = true
}
bindChannel(){
this.channel.on("diff", (diff) => this.update(diff))
this.channel.on("redirect", ({to, flash}) => this.onRedirect({to, flash}))
this.channel.on("live_redirect", ({to, kind}) => this.onLiveRedirect({to, kind}))
this.channel.on("external_live_redirect", ({to, kind}) => this.onExternalLiveRedirect({to, kind}))
this.channel.on("session", ({token}) => this.el.setAttribute(PHX_SESSION, token))
this.channel.onError(reason => this.onError(reason))
this.channel.onClose(() => this.onGracefulClose())
}
onGracefulClose(){
this.gracefullyClosed = true
this.liveSocket.destroyViewById(this.id)
}
onExternalLiveRedirect({to, kind}){
this.liveSocket.replaceRoot(to, () => Browser.pushState(kind, {}, to))
}
onLiveRedirect({to, kind}){
Browser.pushState(kind, {}, to)
}
onRedirect({to, flash}){ Browser.redirect(to, flash) }
hasGracefullyClosed(){ return this.gracefullyClosed }
join(callback){
if(this.parent){
this.parent.channel.onClose(() => this.onGracefulClose())
this.parent.channel.onError(() => this.liveSocket.destroyViewById(this.id))
}
this.channel.join()
.receive("ok", data => {
if(!this.joinedOnce){ callback && callback(this) }
this.joinedOnce = true
this.onJoin(data)
})
.receive("error", resp => this.onJoinError(resp))
.receive("timeout", () => this.onJoinError("timeout"))
}
onJoinError(resp){
if(resp.redirect){ return this.onRedirect(resp.redirect) }
if(resp.external_live_redirect){ return this.onExternalLiveRedirect(resp.external_live_redirect) }
this.displayError()
this.log("error", () => ["unable to join", resp])
}
onError(reason){
this.log("error", () => ["view crashed", reason])
this.liveSocket.onViewError(this)
document.activeElement.blur()
if(this.liveSocket.isUnloaded()){
this.showLoader(BEFORE_UNLOAD_LOADER_TIMEOUT)
} else {
this.displayError()
}