Skip to content

Commit a7167ad

Browse files
authored
Merge pull request #31 from github/muan/eslint
Add eslint-plugin-github and babel
2 parents eaf5c5d + 559266b commit a7167ad

File tree

11 files changed

+3097
-1416
lines changed

11 files changed

+3097
-1416
lines changed

.babelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"plugins": [
3+
"transform-es2015-modules-umd"
4+
],
5+
"presets": ["es2015"]
6+
}

.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+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bower_components/
22
node_modules/
3+
dist/

.jshintrc

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

include-fragment-element.js

Lines changed: 96 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,162 @@
1-
(function() {
2-
'use strict';
1+
/* eslint-disable github/no-then */
32

4-
var privateData = new WeakMap();
3+
;(function() {
4+
'use strict'
5+
6+
const privateData = new WeakMap()
57

68
function fire(name, target) {
79
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)
1214
}
1315

1416
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')
2027
}
21-
}, function() {
22-
el.classList.add('is-error');
23-
});
28+
)
2429
}
2530

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

2833
Object.defineProperty(IncludeFragmentPrototype, 'src', {
29-
get: function() {
30-
var src = this.getAttribute('src');
34+
get() {
35+
const src = this.getAttribute('src')
3136
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
3540
} else {
36-
return '';
41+
return ''
3742
}
3843
},
39-
set: function(value) {
40-
this.setAttribute('src', value);
44+
45+
set(value) {
46+
this.setAttribute('src', value)
4147
}
42-
});
48+
})
4349

4450
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)
4753
if (data && data.src === src) {
48-
return data.data;
54+
return data.data
4955
} else {
5056
if (src) {
51-
data = el.load();
57+
data = el.load()
5258
} else {
53-
data = Promise.reject(new Error('missing src'));
59+
data = Promise.reject(new Error('missing src'))
5460
}
55-
privateData.set(el, {src: src, data: data});
56-
return data;
61+
privateData.set(el, {src, data})
62+
return data
5763
}
5864
}
5965

6066
Object.defineProperty(IncludeFragmentPrototype, 'data', {
61-
get: function() {
62-
return getData(this);
67+
get() {
68+
return getData(this)
6369
}
64-
});
70+
})
6571

6672
IncludeFragmentPrototype.attributeChangedCallback = function(attrName) {
6773
if (attrName === 'src') {
6874
// Reload data load cache.
69-
var data = getData(this);
75+
const data = getData(this)
7076

7177
// Source changed after attached so replace element.
7278
if (this._attached) {
73-
handleData(this, data);
79+
handleData(this, data)
7480
}
7581
}
76-
};
82+
}
7783

7884
IncludeFragmentPrototype.createdCallback = function() {
7985
// Preload data cache
8086
getData(this)['catch'](function() {
8187
// Ignore `src missing` error on pre-load.
82-
});
83-
};
88+
})
89+
}
8490

8591
IncludeFragmentPrototype.attachedCallback = function() {
86-
this._attached = true;
92+
this._attached = true
8793
if (this.src) {
88-
handleData(this, getData(this));
94+
handleData(this, getData(this))
8995
}
90-
};
96+
}
9197

9298
IncludeFragmentPrototype.detachedCallback = function() {
93-
this._attached = false;
94-
};
99+
this._attached = false
100+
}
95101

96102
IncludeFragmentPrototype.request = function() {
97-
var src = this.src;
103+
const src = this.src
98104
if (!src) {
99-
throw new Error('missing src');
105+
throw new Error('missing src')
100106
}
101107

102108
return new Request(src, {
103109
method: 'GET',
104110
credentials: 'same-origin',
105111
headers: {
106-
'Accept': 'text/html'
112+
Accept: 'text/html'
107113
}
108-
});
109-
};
114+
})
115+
}
110116

111117
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+
}
143154

144155
IncludeFragmentPrototype.fetch = function(request) {
145-
return fetch(request);
146-
};
156+
return fetch(request)
157+
}
147158

148159
window.IncludeFragmentElement = document.registerElement('include-fragment', {
149160
prototype: IncludeFragmentPrototype
150-
});
151-
})();
161+
})
162+
})()

0 commit comments

Comments
 (0)