-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathplaybutton.js
More file actions
61 lines (50 loc) · 1.66 KB
/
playbutton.js
File metadata and controls
61 lines (50 loc) · 1.66 KB
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
/* global d3, document */
var playButton = {
el: document.querySelector(".js-button"),
states: {
playing: {
nextState: "paused",
iconEl: document.querySelector("#pause-icon")
},
paused: {
nextState: "playing",
iconEl: document.querySelector("#play-icon")
}
},
animationDuration: 350,
init: function () {
this.setInitialState();
this.replaceUseEl();
//this.el.addEventListener("click", this.goToNextState.bind(this));
},
setInitialState: function () {
var initialIconRef = this.el.querySelector("use").getAttribute("xlink:href");
var stateName = this.el.querySelector(initialIconRef).getAttribute("data-state");
this.setState(stateName);
},
replaceUseEl: function () {
d3.select(this.el.querySelector("use")).remove();
var x = d3.select(this.el.querySelector("svg"));
x.append("path")
.attr("class", "js-icon")
.attr("d", this.stateIconPath());
},
goToState: function (stateName) {
console.log("called", stateName, this.state.nextState);
if (this.state.nextState == stateName) {
this.goToNextState();
}
},
goToNextState: function () {
this.setState(this.state.nextState);
d3.select(this.el.querySelector(".js-icon")).transition()
.duration(this.animationDuration)
.attr("d", this.stateIconPath());
},
setState: function (stateName) {
this.state = this.states[stateName];
},
stateIconPath: function () {
return this.state.iconEl.getAttribute("d");
}
};