/
basic-link.js
186 lines (163 loc) · 6.17 KB
/
basic-link.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
(function() {
define( [ "core/logger", "core/eventmanager", "comm/comm", "previewer/link", "previewer/media" ], function( Logger, EventManager, Comm, Link, Media ) {
var BasicLink = function( options ) {
var link, comm, butterMapping = {};
var trackEventAddedHandler = function( e ) {
var media = link.currentMedia;
media.popcorn[ e.data.type ]( e.data.popcornOptions );
butterMapping[ e.data.id ] = media.popcorn.getLastTrackEventId();
}; //trackEventAddedHandler
var trackEventUpdatedHandler = function( e ) {
var media = link.currentMedia;
if ( butterMapping[ e.data.id ] ) {
media.popcorn.removeTrackEvent( butterMapping[ e.data.id ] );
}
media.popcorn[ e.data.type ]( e.data.popcornOptions );
butterMapping[ e.data.id ] = media.popcorn.getLastTrackEventId();
comm.send( "Created popcorn event" + e.data.id, "log" );
}; //trackEventUpdatedHandler
var trackEventRemovedHandler = function( e ) {
var media = link.currentMedia;
if ( butterMapping[ e.data.id ] ) {
media.popcorn.removeTrackEvent( butterMapping[ e.data.id ] );
}
}; //trackEventRemovedHandler
var mediaChangedHandler = function( e ) {
if ( link.currentMedia ) {
link.removeMediaHandlers( comm );
}
var currentMedia = link.currentMedia = link.getMedia( e.data.id );
if ( currentMedia ) {
link.addMediaHandlers({
'trackeventadded': trackEventAddedHandler,
'trackeventupdated': trackEventUpdatedHandler,
'trackeventremoved': trackEventRemovedHandler,
'play': currentMedia.play,
'pause': currentMedia.pause,
'mute': currentMedia.mute
});
}
}; //mediaChangedHandler
var mediaAddedHandler = function( e ) {
if ( !link.getMedia( e.data.id ) ) {
var media = new Media( e.data );
link.addMedia( media );
buildMedia( media, function( media ) {
link.sendMedia( media );
});
}
else {
console.log('media', e.data.id, 'already exists');
}
}; //mediaAddedHandler
var mediaRemovedHandler = function( e ) {
link.removeMedia( link.getMedia( e.data.id ) );
}; //mediaRemovedHandler
var mediaTimeUpdateHandler = function( e ) {
link.currentMedia.popcorn.currentTime( e.data );
}; //mediaTimeUpdateHandler
var mediaContentChangedHandler = function( e ) {
var container = link.currentMedia.mediaElement;
while( container.firstChild ) {
container.removeChild( container.firstChild );
}
if ( [ "AUDIO", "VIDEO" ].indexOf( container.nodeName ) > -1 ) {
container.currentSrc = "";
container.src = "";
} //if
link.currentMedia.url = e.data;
var storedEvents = link.currentMedia.popcorn.getTrackEvents(),
newEvents = [];
for ( var i=0; i<storedEvents.length; ++i ) {
var ev = storedEvents[ i ],
manifestOptions = ev._natives.manifest.options
newOptions = {},
butterId = undefined;
for ( var option in manifestOptions ) {
if ( manifestOptions.hasOwnProperty( option ) ) {
newOptions[ option ] = ev[ option ];
} //if
} //for
for ( var id in butterMapping ) {
if ( butterMapping.hasOwnProperty( id ) ) {
if ( butterMapping[ id ] === ev._id ) {
butterId = id;
} //if
} //if
} //for
newEvents.push({
popcornId: ev._id,
butterId: butterId,
options: newOptions
});
link.currentMedia.popcorn.removeTrackEvent( ev._id );
}
buildMedia( link.currentMedia, function( media ) {
for ( var i=0; i<newEvents.length; ++i ) {
var newEvent = newEvents[ i ];
link.currentMedia.popcorn[ ev._natives.type ]( newEvent.options );
var newId = link.currentMedia.popcorn.getLastTrackEventId();
butterMapping[ newEvent.butterId ] = newId;
}
comm.send( "mediacontentchanged", "mediacontentchanged" );
});
}; //mediaContentChangedHandler
var fetchHTMLHandler = function( e ) {
return link.getHTML( e.data );
}; //fetchHTMLHandler
link = new Link({
onmediachanged: mediaChangedHandler,
onmediaadded: mediaAddedHandler,
onmediaremoved: mediaRemovedHandler,
onmediatimeupdate: mediaTimeUpdateHandler,
onmediacontentchanged: mediaContentChangedHandler,
onfetchhtml: fetchHTMLHandler,
defaultMedia: options.defaultMedia,
importData: options.importData,
popcornUrl: options.popcornUrl
});
comm = link.comm;
var buildMedia = function( media, callback ) {
function isPopcornReady( e, readyCallback ) {
if ( !window.Popcorn ) {
setTimeout( function() {
isPopcornReady( e, readyCallback );
}, 1000 );
}
else {
readyCallback();
} //if
} //isPopcornReady
function popcornIsReady() {
media.clear();
media.prepare({
success: function( successOptions ) {
link.setupPopcornHandlers();
callback( media );
},
timeout: function() {
link.sendTimeoutError( media );
},
error: function( e ) {
link.sendLoadError( e );
}
});
} //popcornIsReady
if ( !window.Popcorn ) {
insertPopcorn();
isPopcornReady( null, popcornIsReady );
}
else {
popcornIsReady();
} //if
}; //buildMedia
var insertPopcorn = function() {
var popcornSourceScript = document.createElement( "script" );
popcornSourceScript.src = popcornUrl;
document.head.appendChild( popcornSourceScript );
}; //insertPopcorn
link.scrape();
}; //BasicLink
return BasicLink;
}); //define
})();