Skip to content

Commit 2f3a83c

Browse files
authored
Merge pull request #32 from github/v1
Use Custom Elements v1 API
2 parents a7167ad + 5505870 commit 2f3a83c

File tree

8 files changed

+1071
-491
lines changed

8 files changed

+1071
-491
lines changed

.babelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"plugins": [
3+
"transform-custom-element-classes",
34
"transform-es2015-modules-umd"
45
],
56
"presets": ["es2015"]

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ Available on [Bower](http://bower.io) as **include-fragment-element**.
1111
$ bower install include-fragment-element
1212
```
1313

14-
This component is built on the [Web Component](http://webcomponents.org/) stack. Specifically, it requires a feature called [Custom Elements](http://www.html5rocks.com/en/tutorials/webcomponents/customelements/). You'll need to use a polyfill to get this feature today. Either the [Polymer](http://www.polymer-project.org/) or [X-Tag](http://www.x-tags.org/) frameworks supply a polyfill, or you can install the standalone [CustomElements](https://github.com/Polymer/CustomElements) polyfill.
14+
This component is built on the [Web Component](http://webcomponents.org/) stack. Specifically, it requires a feature called [Custom Elements](http://www.html5rocks.com/en/tutorials/webcomponents/customelements/). You'll need to use a polyfill to get this feature today. Either the [Polymer](http://www.polymer-project.org/) or [X-Tag](http://www.x-tags.org/) frameworks supply a polyfill, or you can install the standalone [CustomElements](https://github.com/webcomponents/webcomponentsjs) polyfill.
15+
16+
Legacy browsers require other generic polyfills. See `examples/index.html` for details.
1517

1618
``` html
17-
<script src="https://cdnjs.cloudflare.com/ajax/libs/polymer/0.3.4/platform.js"></script>
19+
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.22/webcomponents-hi-ce.js"></script>
1820
```
1921

2022
## Usage

examples/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<!-- For Promise & Reflect.construct -->
6+
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.34.2/es6-shim.min.js"></script>
7+
<!-- For CustomEvent -->
8+
<script src="https://unpkg.com/@webcomponents/[email protected]/webcomponents-platform.js"></script>
9+
<!-- For Template -->
10+
<script src="https://unpkg.com/@webcomponents/[email protected]/template.js"></script>
11+
<!-- Fetch -->
12+
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
13+
<!-- HTML Import & Custom Elements -->
14+
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.22/webcomponents-hi-ce.js"></script>
15+
<script src="../dist/index-umd.js"></script>
16+
<title>include-fragment demo</title>
17+
</head>
18+
<body>
19+
<include-fragment src="./pull.html">Loading</include-fragment>
20+
</body>
21+
</html>

examples/pull.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<b>Works.</b>

include-fragment-element.js

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,84 @@
11
/* eslint-disable github/no-then */
22

3-
;(function() {
4-
'use strict'
5-
6-
const privateData = new WeakMap()
7-
8-
function fire(name, target) {
9-
setTimeout(function() {
10-
const event = target.ownerDocument.createEvent('Event')
11-
event.initEvent(name, false, false)
12-
target.dispatchEvent(event)
13-
}, 0)
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(el, data) {
14+
return data.then(
15+
function(html) {
16+
const parentNode = el.parentNode
17+
if (parentNode) {
18+
el.insertAdjacentHTML('afterend', html)
19+
parentNode.removeChild(el)
20+
}
21+
},
22+
function() {
23+
el.classList.add('is-error')
24+
}
25+
)
26+
}
27+
28+
function getData(el) {
29+
const src = el.src
30+
let data = privateData.get(el)
31+
if (data && data.src === src) {
32+
return data.data
33+
} else {
34+
if (src) {
35+
data = el.load()
36+
} else {
37+
data = Promise.reject(new Error('missing src'))
38+
}
39+
privateData.set(el, {src, data})
40+
return data
1441
}
42+
}
1543

16-
function handleData(el, data) {
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')
27-
}
28-
)
44+
export class IncludeFragmentElement extends HTMLElement {
45+
constructor() {
46+
super()
47+
// Preload data cache
48+
getData(this)['catch'](function() {
49+
// Ignore `src missing` error on pre-load.
50+
})
2951
}
3052

31-
const IncludeFragmentPrototype = Object.create(window.HTMLElement.prototype)
32-
33-
Object.defineProperty(IncludeFragmentPrototype, 'src', {
34-
get() {
35-
const src = this.getAttribute('src')
36-
if (src) {
37-
const link = this.ownerDocument.createElement('a')
38-
link.href = src
39-
return link.href
40-
} else {
41-
return ''
42-
}
43-
},
53+
static get observedAttributes() {
54+
return ['src']
55+
}
4456

45-
set(value) {
46-
this.setAttribute('src', value)
57+
get src() {
58+
const src = this.getAttribute('src')
59+
if (src) {
60+
const link = this.ownerDocument.createElement('a')
61+
link.href = src
62+
return link.href
63+
} else {
64+
return ''
4765
}
48-
})
66+
}
4967

50-
function getData(el) {
51-
const src = el.src
52-
let data = privateData.get(el)
53-
if (data && data.src === src) {
54-
return data.data
68+
set src(val) {
69+
if (val) {
70+
this.setAttribute('src', val)
5571
} else {
56-
if (src) {
57-
data = el.load()
58-
} else {
59-
data = Promise.reject(new Error('missing src'))
60-
}
61-
privateData.set(el, {src, data})
62-
return data
72+
this.removeAttribute('src')
6373
}
6474
}
6575

66-
Object.defineProperty(IncludeFragmentPrototype, 'data', {
67-
get() {
68-
return getData(this)
69-
}
70-
})
76+
get data() {
77+
return getData(this)
78+
}
7179

72-
IncludeFragmentPrototype.attributeChangedCallback = function(attrName) {
73-
if (attrName === 'src') {
80+
attributeChangedCallback(attribute) {
81+
if (attribute === 'src') {
7482
// Reload data load cache.
7583
const data = getData(this)
7684

@@ -81,25 +89,18 @@
8189
}
8290
}
8391

84-
IncludeFragmentPrototype.createdCallback = function() {
85-
// Preload data cache
86-
getData(this)['catch'](function() {
87-
// Ignore `src missing` error on pre-load.
88-
})
89-
}
90-
91-
IncludeFragmentPrototype.attachedCallback = function() {
92+
connectedCallback() {
9293
this._attached = true
9394
if (this.src) {
9495
handleData(this, getData(this))
9596
}
9697
}
9798

98-
IncludeFragmentPrototype.detachedCallback = function() {
99+
disconnectedCallback() {
99100
this._attached = false
100101
}
101102

102-
IncludeFragmentPrototype.request = function() {
103+
request() {
103104
const src = this.src
104105
if (!src) {
105106
throw new Error('missing src')
@@ -114,7 +115,7 @@
114115
})
115116
}
116117

117-
IncludeFragmentPrototype.load = function() {
118+
load() {
118119
const self = this
119120

120121
return Promise.resolve()
@@ -152,11 +153,12 @@
152153
)
153154
}
154155

155-
IncludeFragmentPrototype.fetch = function(request) {
156+
fetch(request) {
156157
return fetch(request)
157158
}
159+
}
158160

159-
window.IncludeFragmentElement = document.registerElement('include-fragment', {
160-
prototype: IncludeFragmentPrototype
161-
})
162-
})()
161+
if (!window.customElements.get('include-fragment')) {
162+
window.IncludeFragmentElement = IncludeFragmentElement
163+
window.customElements.define('include-fragment', IncludeFragmentElement)
164+
}

0 commit comments

Comments
 (0)