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
174
175
176
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import ChannelStore from '../stores/channel_store.jsx';
const ytRegex = /(?:http|https):\/\/(?:www\.)?(?:(?:youtube\.com\/(?:(?:v\/)|(\/u\/\w\/)|(?:(?:watch|embed\/watch)(?:\/|.*v=))|(?:embed\/)|(?:user\/[^\/]+\/u\/[0-9]\/)))|(?:youtu\.be\/))([^#\&\?]*)/;
export default class YoutubeVideo extends React.Component {
constructor(props) {
super(props);
this.updateStateFromProps = this.updateStateFromProps.bind(this);
this.handleReceivedMetadata = this.handleReceivedMetadata.bind(this);
this.play = this.play.bind(this);
this.stop = this.stop.bind(this);
this.stopOnChannelChange = this.stopOnChannelChange.bind(this);
this.state = {
playing: false,
title: ''
};
}
componentWillMount() {
this.updateStateFromProps(this.props);
}
componentWillReceiveProps(nextProps) {
this.updateStateFromProps(nextProps);
}
updateStateFromProps(props) {
const link = props.link;
const match = link.trim().match(ytRegex);
if (!match || match[2].length !== 11) {
return;
}
this.setState({
videoId: match[2],
time: this.handleYoutubeTime(link)
});
}
handleYoutubeTime(link) {
const timeRegex = /[\\?&]t=([0-9hms]+)/;
const time = link.match(timeRegex);
if (!time || !time[1]) {
return '';
}
const hours = time[1].match(/([0-9]+)h/);
const minutes = time[1].match(/([0-9]+)m/);
const seconds = time[1].match(/([0-9]+)s/);
let ticks = 0;
if (hours && hours[1]) {
ticks += parseInt(hours[1], 10) * 3600;
}
if (minutes && minutes[1]) {
ticks += parseInt(minutes[1], 10) * 60;
}
if (seconds && seconds[1]) {
ticks += parseInt(seconds[1], 10);
}
return '&start=' + ticks.toString();
}
componentDidMount() {
if (global.window.mm_config.GoogleDeveloperKey) {
$.ajax({
async: true,
url: 'https://www.googleapis.com/youtube/v3/videos',
type: 'GET',
data: {part: 'snippet', id: this.state.videoId, key: global.window.mm_config.GoogleDeveloperKey},
success: this.handleReceivedMetadata
});
}
}
handleReceivedMetadata(data) {
if (!data.items.length || !data.items[0].snippet) {
return null;
}
var metadata = data.items[0].snippet;
this.setState({
receivedYoutubeData: true,
title: metadata.title
});
return null;
}
play() {
this.setState({playing: true});
if (ChannelStore.getCurrentId() === this.props.channelId) {
ChannelStore.addChangeListener(this.stopOnChannelChange);
}
}
stop() {
this.setState({playing: false});
}
stopOnChannelChange() {
if (ChannelStore.getCurrentId() !== this.props.channelId) {
this.stop();
}
}
render() {
let header = 'Youtube';
if (this.state.title) {
header = header + ' - ';
}
let content;
if (this.state.playing) {
content = (
<iframe
src={'https://www.youtube.com/embed/' + this.state.videoId + '?autoplay=1&autohide=1&border=0&wmode=opaque&fs=1&enablejsapi=1' + this.state.time}
width='480px'
height='360px'
type='text/html'
frameBorder='0'
allowFullScreen='allowfullscreen'
/>
);
} else {
content = (
<div className='embed-responsive embed-responsive-4by3 video-div__placeholder'>
<div className='video-thumbnail__container'>
<img
className='video-thumbnail'
src={'https://i.ytimg.com/vi/' + this.state.videoId + '/hqdefault.jpg'}
/>
<div className='block'>
<span className='play-button'><span/></span>
</div>
</div>
</div>
);
}
return (
<div>
<h4>
<span className='video-type'>{header}</span>
<span className='video-title'><a href={this.props.link}>{this.state.title}</a></span>
</h4>
<div
className='video-div embed-responsive-item'
onClick={this.play}
>
{content}
</div>
</div>
);
}
static isYoutubeLink(link) {
return link.trim().match(ytRegex);
}
}
YoutubeVideo.propTypes = {
channelId: React.PropTypes.string.isRequired,
link: React.PropTypes.string.isRequired
};
|