-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathes6.js
69 lines (66 loc) · 1.89 KB
/
es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// This example shows an example "complex" slideshow element in use with ES6
// syntax care of babel: https://babeljs.io/
import BaseElement from '../index.js'
class Slides extends BaseElement {
constructor (el) {
super(el)
// Create a property for the actively shown slide
this.activeIndex = 0
// When a slide is selected, set that as the activeIndex then render
this.addEventListener('select', idx => {
this.activeIndex = idx
this.render(this.data)
})
// Inline styles for this element
this.style = {
display: 'table',
margin: '0',
padding: '0'
}
}
render (slides) {
// Save a local copy of data for re-rendering
this.data = slides
slides = slides.map((slide, idx) => {
let tag = 'li'
// If this is the active slide, set the content and .active class
if (idx === this.activeIndex) {
tag = 'li.active'
this.content = slide.content
}
// Render each <li><button/></li>
return this.html(tag, {
style: {
listStyle: 'none',
float: 'left',
margin: '0 .5em'
}
}, [
this.html('button', {
onclick: e => {
// onclick send a select event up with index
this.send('select', idx)
}
}, slide.name)
])
})
// Build our tree with <div><ul>buttons</ul><div>content</div></div>
let vtree = this.html('div', [
this.html('ul.sections', this, slides),
this.html('.content', {
style: {
padding: '1em'
}
}, this.content)
])
// Return the tree wrapped in afterRender
return this.afterRender(vtree)
}
}
// The end user API from here on down
let slides = new Slides(document.body)
slides.render([
{ name: 'intro', content: 'Intro...' },
{ name: 'examples', content: 'Examples...' },
{ name: 'outro', content: 'Outro...' }
])