|
1 |
| -(function() { |
2 |
| - 'use strict'; |
| 1 | +/* eslint-disable github/no-then */ |
3 | 2 |
|
4 |
| - var privateData = new WeakMap(); |
| 3 | +;(function() { |
| 4 | + 'use strict' |
| 5 | + |
| 6 | + const privateData = new WeakMap() |
5 | 7 |
|
6 | 8 | function fire(name, target) {
|
7 | 9 | setTimeout(function() {
|
8 |
| - var event = target.ownerDocument.createEvent('Event'); |
9 |
| - event.initEvent(name, false, false); |
10 |
| - target.dispatchEvent(event); |
11 |
| - }, 0); |
| 10 | + const event = target.ownerDocument.createEvent('Event') |
| 11 | + event.initEvent(name, false, false) |
| 12 | + target.dispatchEvent(event) |
| 13 | + }, 0) |
12 | 14 | }
|
13 | 15 |
|
14 | 16 | function handleData(el, data) {
|
15 |
| - return data.then(function(html) { |
16 |
| - var parentNode = el.parentNode; |
17 |
| - if (parentNode) { |
18 |
| - el.insertAdjacentHTML('afterend', html); |
19 |
| - parentNode.removeChild(el); |
| 17 | + return data.then( |
| 18 | + function(html) { |
| 19 | + const parentNode = el.parentNode |
| 20 | + if (parentNode) { |
| 21 | + el.insertAdjacentHTML('afterend', html) |
| 22 | + parentNode.removeChild(el) |
| 23 | + } |
| 24 | + }, |
| 25 | + function() { |
| 26 | + el.classList.add('is-error') |
20 | 27 | }
|
21 |
| - }, function() { |
22 |
| - el.classList.add('is-error'); |
23 |
| - }); |
| 28 | + ) |
24 | 29 | }
|
25 | 30 |
|
26 |
| - var IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype); |
| 31 | + const IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype) |
27 | 32 |
|
28 | 33 | Object.defineProperty(IncludeFragmentPrototype, 'src', {
|
29 |
| - get: function() { |
30 |
| - var src = this.getAttribute('src'); |
| 34 | + get() { |
| 35 | + const src = this.getAttribute('src') |
31 | 36 | if (src) {
|
32 |
| - var link = this.ownerDocument.createElement('a'); |
33 |
| - link.href = src; |
34 |
| - return link.href; |
| 37 | + const link = this.ownerDocument.createElement('a') |
| 38 | + link.href = src |
| 39 | + return link.href |
35 | 40 | } else {
|
36 |
| - return ''; |
| 41 | + return '' |
37 | 42 | }
|
38 | 43 | },
|
39 |
| - set: function(value) { |
40 |
| - this.setAttribute('src', value); |
| 44 | + |
| 45 | + set(value) { |
| 46 | + this.setAttribute('src', value) |
41 | 47 | }
|
42 |
| - }); |
| 48 | + }) |
43 | 49 |
|
44 | 50 | function getData(el) {
|
45 |
| - var src = el.src; |
46 |
| - var data = privateData.get(el); |
| 51 | + const src = el.src |
| 52 | + let data = privateData.get(el) |
47 | 53 | if (data && data.src === src) {
|
48 |
| - return data.data; |
| 54 | + return data.data |
49 | 55 | } else {
|
50 | 56 | if (src) {
|
51 |
| - data = el.load(); |
| 57 | + data = el.load() |
52 | 58 | } else {
|
53 |
| - data = Promise.reject(new Error('missing src')); |
| 59 | + data = Promise.reject(new Error('missing src')) |
54 | 60 | }
|
55 |
| - privateData.set(el, {src: src, data: data}); |
56 |
| - return data; |
| 61 | + privateData.set(el, {src, data}) |
| 62 | + return data |
57 | 63 | }
|
58 | 64 | }
|
59 | 65 |
|
60 | 66 | Object.defineProperty(IncludeFragmentPrototype, 'data', {
|
61 |
| - get: function() { |
62 |
| - return getData(this); |
| 67 | + get() { |
| 68 | + return getData(this) |
63 | 69 | }
|
64 |
| - }); |
| 70 | + }) |
65 | 71 |
|
66 | 72 | IncludeFragmentPrototype.attributeChangedCallback = function(attrName) {
|
67 | 73 | if (attrName === 'src') {
|
68 | 74 | // Reload data load cache.
|
69 |
| - var data = getData(this); |
| 75 | + const data = getData(this) |
70 | 76 |
|
71 | 77 | // Source changed after attached so replace element.
|
72 | 78 | if (this._attached) {
|
73 |
| - handleData(this, data); |
| 79 | + handleData(this, data) |
74 | 80 | }
|
75 | 81 | }
|
76 |
| - }; |
| 82 | + } |
77 | 83 |
|
78 | 84 | IncludeFragmentPrototype.createdCallback = function() {
|
79 | 85 | // Preload data cache
|
80 | 86 | getData(this)['catch'](function() {
|
81 | 87 | // Ignore `src missing` error on pre-load.
|
82 |
| - }); |
83 |
| - }; |
| 88 | + }) |
| 89 | + } |
84 | 90 |
|
85 | 91 | IncludeFragmentPrototype.attachedCallback = function() {
|
86 |
| - this._attached = true; |
| 92 | + this._attached = true |
87 | 93 | if (this.src) {
|
88 |
| - handleData(this, getData(this)); |
| 94 | + handleData(this, getData(this)) |
89 | 95 | }
|
90 |
| - }; |
| 96 | + } |
91 | 97 |
|
92 | 98 | IncludeFragmentPrototype.detachedCallback = function() {
|
93 |
| - this._attached = false; |
94 |
| - }; |
| 99 | + this._attached = false |
| 100 | + } |
95 | 101 |
|
96 | 102 | IncludeFragmentPrototype.request = function() {
|
97 |
| - var src = this.src; |
| 103 | + const src = this.src |
98 | 104 | if (!src) {
|
99 |
| - throw new Error('missing src'); |
| 105 | + throw new Error('missing src') |
100 | 106 | }
|
101 | 107 |
|
102 | 108 | return new Request(src, {
|
103 | 109 | method: 'GET',
|
104 | 110 | credentials: 'same-origin',
|
105 | 111 | headers: {
|
106 |
| - 'Accept': 'text/html' |
| 112 | + Accept: 'text/html' |
107 | 113 | }
|
108 |
| - }); |
109 |
| - }; |
| 114 | + }) |
| 115 | + } |
110 | 116 |
|
111 | 117 | IncludeFragmentPrototype.load = function() {
|
112 |
| - var self = this; |
113 |
| - |
114 |
| - return Promise.resolve().then(function() { |
115 |
| - var request = self.request(); |
116 |
| - fire('loadstart', self); |
117 |
| - return self.fetch(request); |
118 |
| - }).then(function(response) { |
119 |
| - if (response.status !== 200) { |
120 |
| - throw new Error('Failed to load resource: ' + |
121 |
| - 'the server responded with a status of ' + response.status); |
122 |
| - } |
123 |
| - |
124 |
| - var ct = response.headers.get('Content-Type'); |
125 |
| - if (!ct || !ct.match(/^text\/html/)) { |
126 |
| - throw new Error('Failed to load resource: ' + |
127 |
| - 'expected text/html but was ' + ct); |
128 |
| - } |
129 |
| - |
130 |
| - return response; |
131 |
| - }).then(function(response) { |
132 |
| - return response.text(); |
133 |
| - }).then(function(data) { |
134 |
| - fire('load', self); |
135 |
| - fire('loadend', self); |
136 |
| - return data; |
137 |
| - }, function(error) { |
138 |
| - fire('error', self); |
139 |
| - fire('loadend', self); |
140 |
| - throw error; |
141 |
| - }); |
142 |
| - }; |
| 118 | + const self = this |
| 119 | + |
| 120 | + return Promise.resolve() |
| 121 | + .then(function() { |
| 122 | + const request = self.request() |
| 123 | + fire('loadstart', self) |
| 124 | + return self.fetch(request) |
| 125 | + }) |
| 126 | + .then(function(response) { |
| 127 | + if (response.status !== 200) { |
| 128 | + throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`) |
| 129 | + } |
| 130 | + |
| 131 | + const ct = response.headers.get('Content-Type') |
| 132 | + if (!ct || !ct.match(/^text\/html/)) { |
| 133 | + throw new Error(`Failed to load resource: expected text/html but was ${ct}`) |
| 134 | + } |
| 135 | + |
| 136 | + return response |
| 137 | + }) |
| 138 | + .then(function(response) { |
| 139 | + return response.text() |
| 140 | + }) |
| 141 | + .then( |
| 142 | + function(data) { |
| 143 | + fire('load', self) |
| 144 | + fire('loadend', self) |
| 145 | + return data |
| 146 | + }, |
| 147 | + function(error) { |
| 148 | + fire('error', self) |
| 149 | + fire('loadend', self) |
| 150 | + throw error |
| 151 | + } |
| 152 | + ) |
| 153 | + } |
143 | 154 |
|
144 | 155 | IncludeFragmentPrototype.fetch = function(request) {
|
145 |
| - return fetch(request); |
146 |
| - }; |
| 156 | + return fetch(request) |
| 157 | + } |
147 | 158 |
|
148 | 159 | window.IncludeFragmentElement = document.registerElement('include-fragment', {
|
149 | 160 | prototype: IncludeFragmentPrototype
|
150 |
| - }); |
151 |
| -})(); |
| 161 | + }) |
| 162 | +})() |
0 commit comments