1
1
/* eslint-disable github/no-then */
2
2
3
+ const privateData = new WeakMap ( )
4
+
5
+ function fire ( name , target ) {
6
+ setTimeout ( function ( ) {
7
+ const event = target . ownerDocument . createEvent ( 'Event' )
8
+ event . initEvent ( name , false , false )
9
+ target . dispatchEvent ( event )
10
+ } , 0 )
11
+ }
12
+
13
+ function handleData ( data , target ) {
14
+ return data . then (
15
+ function ( html ) {
16
+ const parentNode = target . parentNode
17
+ if ( parentNode ) {
18
+ target . insertAdjacentHTML ( 'afterend' , html )
19
+ parentNode . removeChild ( target )
20
+ }
21
+ } ,
22
+ function ( ) {
23
+ target . classList . add ( 'is-error' )
24
+ }
25
+ )
26
+ }
27
+
3
28
export class IncludeFragmentElement extends HTMLElement {
4
29
constructor ( ) {
5
30
super ( )
6
- this . _privateData = new WeakMap ( )
7
31
// Preload data cache
8
- this . _getData ( ) [ 'catch' ] ( function ( ) {
32
+ this . getData ( ) [ 'catch' ] ( function ( ) {
9
33
// Ignore `src missing` error on pre-load.
10
34
} )
11
35
}
12
36
13
- _fire ( name , target ) {
14
- setTimeout ( function ( ) {
15
- const event = target . ownerDocument . createEvent ( 'Event' )
16
- event . initEvent ( name , false , false )
17
- target . dispatchEvent ( event )
18
- } , 0 )
19
- }
20
-
21
- _handleData ( data ) {
22
- return data . then (
23
- function ( html ) {
24
- const parentNode = this . parentNode
25
- if ( parentNode ) {
26
- this . insertAdjacentHTML ( 'afterend' , html )
27
- parentNode . removeChild ( this )
28
- }
29
- } . bind ( this ) ,
30
- function ( ) {
31
- this . classList . add ( 'is-error' )
32
- } . bind ( this )
33
- )
34
- }
35
-
36
37
static get observedAttributes ( ) {
37
38
return [ 'src' ]
38
39
}
@@ -56,50 +57,50 @@ export class IncludeFragmentElement extends HTMLElement {
56
57
}
57
58
}
58
59
59
- _getData ( ) {
60
+ getData ( ) {
60
61
const src = this . src
61
- let data = this . _privateData . get ( this )
62
+ let data = privateData . get ( this )
62
63
if ( data && data . src === src ) {
63
64
return data . data
64
65
} else {
65
66
if ( src ) {
66
- data = this . _load ( )
67
+ data = this . load ( )
67
68
} else {
68
69
data = Promise . reject ( new Error ( 'missing src' ) )
69
70
}
70
- this . _privateData . set ( this , { src, data} )
71
+ privateData . set ( this , { src, data} )
71
72
return data
72
73
}
73
74
}
74
75
75
76
get data ( ) {
76
- return this . _getData ( )
77
+ return this . getData ( )
77
78
}
78
79
79
80
attributeChangedCallback ( attribute ) {
80
81
if ( attribute === 'src' ) {
81
82
// Reload data load cache.
82
- const data = this . _getData ( )
83
+ const data = this . getData ( )
83
84
84
85
// Source changed after attached so replace element.
85
86
if ( this . _attached ) {
86
- this . _handleData ( data )
87
+ handleData ( data , this )
87
88
}
88
89
}
89
90
}
90
91
91
92
connectedCallback ( ) {
92
93
this . _attached = true
93
94
if ( this . src ) {
94
- this . _handleData ( this . _getData ( ) )
95
+ handleData ( this . getData ( ) , this )
95
96
}
96
97
}
97
98
98
99
disconnectedCallback ( ) {
99
100
this . _attached = false
100
101
}
101
102
102
- _request ( ) {
103
+ request ( ) {
103
104
const src = this . src
104
105
if ( ! src ) {
105
106
throw new Error ( 'missing src' )
@@ -114,14 +115,14 @@ export class IncludeFragmentElement extends HTMLElement {
114
115
} )
115
116
}
116
117
117
- _load ( ) {
118
+ load ( ) {
118
119
const self = this
119
120
120
121
return Promise . resolve ( )
121
122
. then ( function ( ) {
122
- const request = self . _request ( )
123
- self . _fire ( 'loadstart' , self )
124
- return self . _fetch ( request )
123
+ const request = self . request ( )
124
+ fire ( 'loadstart' , self )
125
+ return self . fetch ( request )
125
126
} )
126
127
. then ( function ( response ) {
127
128
if ( response . status !== 200 ) {
@@ -140,19 +141,19 @@ export class IncludeFragmentElement extends HTMLElement {
140
141
} )
141
142
. then (
142
143
function ( data ) {
143
- self . _fire ( 'load' , self )
144
- self . _fire ( 'loadend' , self )
144
+ fire ( 'load' , self )
145
+ fire ( 'loadend' , self )
145
146
return data
146
147
} ,
147
148
function ( error ) {
148
- self . _fire ( 'error' , self )
149
- self . _fire ( 'loadend' , self )
149
+ fire ( 'error' , self )
150
+ fire ( 'loadend' , self )
150
151
throw error
151
152
}
152
153
)
153
154
}
154
155
155
- _fetch ( request ) {
156
+ fetch ( request ) {
156
157
return fetch ( request )
157
158
}
158
159
}
0 commit comments