-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSoundCloud.js
173 lines (143 loc) · 4.1 KB
/
SoundCloud.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Module dependencies
*/
import React from 'react';
import createWidget from './lib/createWidget';
/**
* Create a new `SoundCloud` component.
*
* This is essentially a glorified wrapper over the existing
* HTML5 widget from SoundCloud. Programmatic control not included.
*
* NOTE: Changing `props.url` will cause the component to load it.
* Unfortunately, SoundCloud adds an entry to `window.history` every time
* a new url is loaded, so changing `props.url` __will__ break the back button.
*/
class SoundCloud extends React.Component {
/**
* @param {Object} props
*/
constructor(props) {
super(props);
this._internalWidget = null;
}
componentDidMount() {
this._createWidget();
}
/**
* @param {Object} nextProps
* @returns {Boolean}
*/
shouldComponentUpdate(nextProps) {
return nextProps.url !== this.props.url;
}
componentDidUpdate() {
this._reloadWidget();
}
componentWillUnmount() {
this._unbindEvents();
}
/**
* Called on the initial render, this uses the rendered iframe
* as a base for creating a new `_internalWidget`.
*/
_createWidget() {
createWidget(this.props.id, (widget) => {
this._setupWidget(widget);
this._reloadWidget();
});
}
/**
* Integrate a newly created `widget` with the rest of the component.
*
* @param {Object} Widget
*/
_setupWidget(widget) {
this._internalWidget = widget;
this._bindEvents();
}
/**
* This is the only way to manipulate the embedded iframe, it's essentially
* refreshed and reloaded.
*
* NOTE: SoundCloud adds an entry to `window.history` after reloading. This is
* __really__ annoying, but unavoidable at the moment, so basically every
* time the url changes it breaks the back button. Super bummer.
*/
_reloadWidget() {
this._internalWidget.load(this.props.url, this.props.opts);
}
/**
* Listen for events coming from `widget`, and pass them up the
* chain to the parent component if needed.
*/
_bindEvents() {
this._internalWidget.bind(window.SC.Widget.Events.PLAY, this.props.onPlay);
this._internalWidget.bind(window.SC.Widget.Events.PAUSE, this.props.onPause);
this._internalWidget.bind(window.SC.Widget.Events.FINISH, this.props.onEnd);
}
/**
* Remove all event bindings.
*/
_unbindEvents() {
this._internalWidget.unbind(window.SC.Widget.Events.PLAY);
this._internalWidget.unbind(window.SC.Widget.Events.PAUSE);
this._internalWidget.unbind(window.SC.Widget.Events.FINISH);
}
/**
* @returns {Object}
*/
render() {
return (
<iframe id={this.props.id}
width="100%"
height={this.props.height || (this.props.opts.visual ? '450' : '166')}
scrolling="no"
frameBorder="no"
src="https://w.soundcloud.com/player/?url="
/>
);
}
}
SoundCloud.propTypes = {
// url to play. It's kept in sync, changing it will
// cause the widget to refresh and play the new url.
url: React.PropTypes.string.isRequired,
// custom ID for widget iframe element
id: React.PropTypes.string,
height: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]),
// widget parameters: appearance, auto play, and callback for SC.Widget.load()
opts: React.PropTypes.shape({
auto_play: React.PropTypes.bool,
visual: React.PropTypes.bool,
buying: React.PropTypes.bool,
liking: React.PropTypes.bool,
download: React.PropTypes.bool,
sharing: React.PropTypes.bool,
show_artwork: React.PropTypes.bool,
show_comments: React.PropTypes.bool,
show_playcount: React.PropTypes.bool,
show_user: React.PropTypes.bool,
show_reposts: React.PropTypes.bool,
hide_related: React.PropTypes.bool,
callback: React.PropTypes.func
}),
// event subscriptions
onPlay: React.PropTypes.func,
onPause: React.PropTypes.func,
onEnd: React.PropTypes.func,
};
SoundCloud.defaultProps = {
id: 'react-sc-widget',
opts: {},
onPlay: () => {},
onPause: () => {},
onEnd: () => {},
};
/**
* Expose `SoundCloud` component
*/
export default SoundCloud;