Skip to content

Commit 52f834e

Browse files
committed
Add eslint and prettify
1 parent eaf5c5d commit 52f834e

File tree

9 files changed

+2115
-318
lines changed

9 files changed

+2115
-318
lines changed

.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": [
3+
"plugin:github/es6",
4+
"plugin:github/browser"
5+
]
6+
}

.jshintrc

Lines changed: 0 additions & 25 deletions
This file was deleted.

include-fragment-element.js

Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,160 @@
1-
(function() {
2-
'use strict';
1+
;(function() {
2+
'use strict'
33

4-
var privateData = new WeakMap();
4+
const privateData = new WeakMap()
55

66
function fire(name, target) {
77
setTimeout(function() {
8-
var event = target.ownerDocument.createEvent('Event');
9-
event.initEvent(name, false, false);
10-
target.dispatchEvent(event);
11-
}, 0);
8+
const event = target.ownerDocument.createEvent('Event')
9+
event.initEvent(name, false, false)
10+
target.dispatchEvent(event)
11+
}, 0)
1212
}
1313

1414
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);
15+
return data.then(
16+
function(html) {
17+
const parentNode = el.parentNode
18+
if (parentNode) {
19+
el.insertAdjacentHTML('afterend', html)
20+
parentNode.removeChild(el)
21+
}
22+
},
23+
function() {
24+
el.classList.add('is-error')
2025
}
21-
}, function() {
22-
el.classList.add('is-error');
23-
});
26+
)
2427
}
2528

26-
var IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype);
29+
const IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype)
2730

2831
Object.defineProperty(IncludeFragmentPrototype, 'src', {
29-
get: function() {
30-
var src = this.getAttribute('src');
32+
get() {
33+
const src = this.getAttribute('src')
3134
if (src) {
32-
var link = this.ownerDocument.createElement('a');
33-
link.href = src;
34-
return link.href;
35+
const link = this.ownerDocument.createElement('a')
36+
link.href = src
37+
return link.href
3538
} else {
36-
return '';
39+
return ''
3740
}
3841
},
39-
set: function(value) {
40-
this.setAttribute('src', value);
42+
43+
set(value) {
44+
this.setAttribute('src', value)
4145
}
42-
});
46+
})
4347

4448
function getData(el) {
45-
var src = el.src;
46-
var data = privateData.get(el);
49+
const src = el.src
50+
let data = privateData.get(el)
4751
if (data && data.src === src) {
48-
return data.data;
52+
return data.data
4953
} else {
5054
if (src) {
51-
data = el.load();
55+
data = el.load()
5256
} else {
53-
data = Promise.reject(new Error('missing src'));
57+
data = Promise.reject(new Error('missing src'))
5458
}
55-
privateData.set(el, {src: src, data: data});
56-
return data;
59+
privateData.set(el, {src, data})
60+
return data
5761
}
5862
}
5963

6064
Object.defineProperty(IncludeFragmentPrototype, 'data', {
61-
get: function() {
62-
return getData(this);
65+
get() {
66+
return getData(this)
6367
}
64-
});
68+
})
6569

6670
IncludeFragmentPrototype.attributeChangedCallback = function(attrName) {
6771
if (attrName === 'src') {
6872
// Reload data load cache.
69-
var data = getData(this);
73+
const data = getData(this)
7074

7175
// Source changed after attached so replace element.
7276
if (this._attached) {
73-
handleData(this, data);
77+
handleData(this, data)
7478
}
7579
}
76-
};
80+
}
7781

7882
IncludeFragmentPrototype.createdCallback = function() {
7983
// Preload data cache
8084
getData(this)['catch'](function() {
8185
// Ignore `src missing` error on pre-load.
82-
});
83-
};
86+
})
87+
}
8488

8589
IncludeFragmentPrototype.attachedCallback = function() {
86-
this._attached = true;
90+
this._attached = true
8791
if (this.src) {
88-
handleData(this, getData(this));
92+
handleData(this, getData(this))
8993
}
90-
};
94+
}
9195

9296
IncludeFragmentPrototype.detachedCallback = function() {
93-
this._attached = false;
94-
};
97+
this._attached = false
98+
}
9599

96100
IncludeFragmentPrototype.request = function() {
97-
var src = this.src;
101+
const src = this.src
98102
if (!src) {
99-
throw new Error('missing src');
103+
throw new Error('missing src')
100104
}
101105

102106
return new Request(src, {
103107
method: 'GET',
104108
credentials: 'same-origin',
105109
headers: {
106-
'Accept': 'text/html'
110+
Accept: 'text/html'
107111
}
108-
});
109-
};
112+
})
113+
}
110114

111115
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-
};
116+
const self = this
117+
118+
return Promise.resolve()
119+
.then(function() {
120+
const request = self.request()
121+
fire('loadstart', self)
122+
return self.fetch(request)
123+
})
124+
.then(function(response) {
125+
if (response.status !== 200) {
126+
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
127+
}
128+
129+
const ct = response.headers.get('Content-Type')
130+
if (!ct || !ct.match(/^text\/html/)) {
131+
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
132+
}
133+
134+
return response
135+
})
136+
.then(function(response) {
137+
return response.text()
138+
})
139+
.then(
140+
function(data) {
141+
fire('load', self)
142+
fire('loadend', self)
143+
return data
144+
},
145+
function(error) {
146+
fire('error', self)
147+
fire('loadend', self)
148+
throw error
149+
}
150+
)
151+
}
143152

144153
IncludeFragmentPrototype.fetch = function(request) {
145-
return fetch(request);
146-
};
154+
return fetch(request)
155+
}
147156

148157
window.IncludeFragmentElement = document.registerElement('include-fragment', {
149158
prototype: IncludeFragmentPrototype
150-
});
151-
})();
159+
})
160+
})()

0 commit comments

Comments
 (0)