This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
203 lines (190 loc) · 6.59 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(function(){
var apiKey = 'AIzaSyCNOGQXHNq3MJTaZzmf2u94xXxXEcrXcBM';
var youtubeUrl = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&playlistId=UUHxNwi3l5CGZo1kG47k7i2Q&key='+apiKey;
//var youtubeUrl = 'https://gdata.youtube.com/feeds/api/users/UCHxNwi3l5CGZo1kG47k7i2Q/uploads?alt=json-in-script';
var libsynUrl = '/rss';
function makeVideoUrl(id) {
return ' https://www.googleapis.com/youtube/v3/videos?part=player&id='+id+'&key=' + apiKey;
}
function embeddedPlayerUrl(id) {
return 'http://www.youtube.com/v/'+id+'?version=3&enablejsapi=1';
}
function currentUri() {
// TODO - Cross browser?
if(/.*localhost.*/.exec(window.location.href)) {
return '/';
}
return window.location.pathname;
}
function isDetailsUri() {
var uri = currentUri();
return uri && uri != '/';
}
function normalizeName(name) {
return name.replace(/[^\w]/gi, '').toLowerCase();
}
function titleMatchesUri(podcast) {
return normalizeName(currentUri().slice(1)) == normalizeName(podcast.title());
}
function Podcast(config) {
var self = this;
self.timestamp = config.timestamp || new Date();
self.id = ko.observable(config.id || '');
self.title = ko.observable(config.title);
self.date = config.date;
self.year = config.year;
self.thumbnail = config.thumbnail || '';
self.hasVideo = ko.computed(function() {
return self.id() != '';
});
self.videoInitialized = ko.observable(false);
self.youtubeVideoHtml = ko.observable('');
self.expandedVideo = ko.observable(false);
self.embedUrl = ko.computed(function() {
return embeddedPlayerUrl(self.id()) + "&showinfo=0&autoplay=1";
});
self.audio = ko.observable(config.audio);
self.hasAudio = ko.observable(config.audio ? true : false);
// Loaded later
self.audioDisplayCss = ko.computed(function() {
return self.hasAudio() ? '' : 'hide';
});
self.audioLink = ko.observable();
self.shownotes = ko.observable(config.text);
// User controlled state/actions
self.toggleVideo = function() {
self.expandedVideo(!self.expandedVideo());
}
self.expandedNotes = ko.observable(false);
self.toogleNotes = function() {
self.expandedNotes(!self.expandedNotes());
}
// Play controls - TODO - move these into custom model...
self.audioLoaded = ko.observable(false);
self.play = function() {
//if(!self.audioLoaded()) { self.audioLoaded(true); }
// TODO - feed events to audio widget?
var audio = self.audioEl();
if(audio) {
if(audio.paused) {
audio.play();
} else {
audio.pause();
}
}
};
self.playing = ko.observable(false);
self.pausedCss = ko.computed(function() {
return self.playing() ? 'pause' : self.audioDisplayCss();
});
// TODO - figure out how to bind this directly to the audio element...
self.currentTime = ko.observable(0);
self.duration = ko.observable(1);
self.percent = ko.computed(function() {
return (self.currentTime() / self.duration())*100;
});
self.audioEl = ko.observable();
self.skipTo = function(data, e) {
var audio = self.audioEl();
if (audio) {
var left = e.offsetX;
var progress = e.currentTarget;
var percent = left / ($(progress).width() );
audio.currentTime = percent * audio.duration;
}
};
self.setupPlayer = function(elements) {
var audio = $(elements).filter('audio')[0];
audio.addEventListener('ended', function(evt) {
audio.pause();
self.playing(false);
self.curentTime(0);
});
audio.addEventListener('timeupdate', function(e) {
self.currentTime(this.currentTime);
self.duration(this.duration);
});
audio.addEventListener('loadedmetadata', function(e){
self.duration(this.duration);
});
audio.addEventListener('play', function(e) {
self.playing(true);
});
audio.addEventListener('pause', function(e) {
self.playing(false);
});
self.audioEl(audio);
};
function timeToString(time) {
if(time) {
var min = Math.round( time / 60 ),
sec = Math.round( time % 60 );
if (sec < 10) sec = "0"+sec;
return min + ":" + sec;
}
return '';
}
self.durationString = ko.computed(function() {
return timeToString(self.duration());
});
self.timeString = ko.computed(function() {
if(self.hasAudio()) {
return timeToString(self.currentTime()) + " / " + self.durationString();
}
return 'Audio not available';
});
}
function PageModel() {
var self = this;
self.podcasts = ko.observableArray();
self.sortedPodcasts = ko.computed(function() {
var copy = self.podcasts().slice(0);
return copy.sort(function(l,r) { return r.timestamp.getTime() - l.timestamp.getTime(); });
});
self.shownpodcasts = ko.computed(function() {
if(isDetailsUri()) {
// Just return the details of one podcast...
return ko.utils.arrayFilter(self.podcasts(), function(podcast) {
return titleMatchesUri(podcast);
});
}
//TODO - paginate!
return self.sortedPodcasts() //.slice(0,12);
});
self.load = function() {
feeds.readYoutube(youtubeUrl, function(feed) {
// TODO - should we bump into a model?
self.podcasts($.map(feed, function(i) { return new Podcast(i); }));
// Now load the audio ->
feeds.readLibsyn(libsynUrl, function(feed) {
$.each(feed, function(ignore, audio) {
// TODO - Try to line up the audio with the video
var podcasts = self.podcasts();
var found = false;
for(var i = 0; i < podcasts.length; ++i) {
var podcast = podcasts[i];
if((podcast.date == audio.date) &&
(podcast.year == audio.year)) {
// Assume these line up....
podcast.audio(audio.audio);
podcast.title(audio.title);
podcast.shownotes(audio.text);
podcast.hasAudio(true);
found = true;
}
}
if(!found) {
// Push podcast anyway (no corresponding video)
console.log('No podcast video for: ', audio);
self.podcasts.push(new Podcast(audio));
}
});
});
});
};
// Load by default.
self.load();
}
window.model = new PageModel();
ko.applyBindings(window.model);
})();