-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeaturedVideo.coffee
More file actions
412 lines (307 loc) · 13.1 KB
/
featuredVideo.coffee
File metadata and controls
412 lines (307 loc) · 13.1 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
###!
featuredVideo v1.1.8 (https://github.com/TechTarget/featuredVideo)
Author: Morgan Wigmanich <okize123@gmail.com> (http://github.com/okize)
Copyright (c) 2013 | Licensed under the MIT license
http://www.opensource.org/licenses/mit-license.php
###
((factory) ->
# use AMD or browser globals to create a jQuery plugin.
if typeof define is 'function' and define.amd
define [ 'jquery' ], factory
else
factory jQuery
) ($) ->
'use strict'
pluginName = 'featuredVideo'
# default plugin options
defaults =
autoplayFirstVideo: true # play the first video automagically as soon as it's loaded
maintainState: true # supports appending videoId in url hash to link to any video in playlist
showPlaylist: true # show a playlist alongside the video player
showPlaylistTooltips: true # show 'tooltips' of the video summary inside the playlist
tooltipHtml: '<div class="featuredVideoPlaylistTooltip"></div>' # html for dom element of tooltip
class Video
# plugin constructor
constructor: (@element, options) ->
@el = $(@element) # featured video component dom container
@options = $.extend({}, defaults, options)
@player = @el.find('.featuredplayer') # the video player dom element
@playlist = @el.find('.featuredVideoPlaylist') # the playlist dom element
@playlistVideos = @playlist.find('li') # each video item in the playlist
@playlistVideosCount = @playlistVideos.length # count of videos in the playlist
@playlistFirstVideoId = @playlistVideos.eq(0).data('videoId') or null # id of first video in playlist
@playType = if @options.autoplayFirstVideo then 'load' else 'cue' # set the playType based on user option
@stateKey = 'videoId' # string to use for the url hash state maintenance
@activeVideoId = null # stores the video id of the video currently in the player
@hashVideoId = @getIdFromUrl() # get video id from url hash
@hashObject = null
@init()
# plugin initializer
init: ->
# sanity check
@sanityCheck()
# initialize video player
@getPlayer()
# initialize video playing from in-page links
@watchHash()
# checks to make sure a few critical pieces of information are being supplied
# in the html before trying to render the video component
sanityCheck: ->
# make sure there is at least one video in the playlist
if @playlistVideosCount <= 0
@el.hide()
return $.error('no videos in the playlist!')
# make sure at least the first video has an id
if (not @playlistFirstVideoId or @playlistFirstVideoId is '')
@el.hide()
return $.error('no video ids specified in playlist!')
# make sure that there are no duplicate ids in playlist array
duplicateIds = @getDuplicatePlaylistIds()
if duplicateIds.length > 0 && typeof console == 'object'
console.error('WARNING! duplicate ids found in the playlist: ', duplicateIds)
# this will hold the api object that brightcove returns
player: {}
# loads video player script if it hasn't been loaded yet
getPlayer: ->
# script configuration
playerScript =
url: 'http://admin.brightcove.com/js/BrightcoveExperiences.js' # brightcove script to load aysnc
isLoaded: $(document).data('playerScriptLoaded') or false # check via data attr if the script has been loaded yet
# get the 3rd party script if it hasn't been loaded yet
playerScriptIsLoaded = if (not playerScript.isLoaded) then @loadPlayerScript(playerScript.url) else @resolve()
# load the player script async
$.when(playerScriptIsLoaded).done(=>
@initializePlayer()
).fail ->
$.error 'Brightcove script failed to load'
# loads 3rd party video player provider script
loadPlayerScript: (url) ->
# attempt to load 3rd party script and return promise
playerScriptIsLoaded = $.ajax(url,
dataType: 'script'
)
playerScriptIsLoaded
# inits the player when it's ready
initializePlayer: ->
# brightcove object
brightcove = window.brightcove or {}
# all the data for player has been received by the browser
# can now get references to the overall player and API modules
window.brightcovePlayerLoaded = (brightcovePlayerId) =>
brightcoveExp = brightcove.api.getExperience(brightcovePlayerId)
@player = brightcoveExp.getModule(brightcove.api.modules.APIModules.VIDEO_PLAYER)
# the player has now been fully instantiated and
# is ready to interact with via the API
# only call methods of the API modules after the template ready event has fired
window.brightcovePlayerReady = =>
# add video to player (3rd arg is an event object)
@updateState(@getVideo(), @playType, undefined)
# now can initialize the playlist to interact with the player
@initializePlaylist()
# no idea what this does, but if it's not here FF & IE9 won't work... go figure
brightcove.createExperiences()
# loads video into player by play type (load or cue)
playVideo: (playType, videoId) ->
if playType is 'load'
@player.loadVideoByID(videoId)
else if playType is 'cue'
@player.cueVideoByID(videoId)
# determines which video to play and sets the activeVideoId
getVideo: (el) ->
# if an element is passed in, grab the video id from it's data attr
if typeof el isnt 'undefined'
@activeVideoId = $(el).data('videoId')
# check if there was a video id set in url hash
# and check that videoid is a valid id inside the playlist
else if @hashVideoId and @hasValidId(@hashVideoId)
@activeVideoId = @hashVideoId
# otherwise use the id of the first video in the playlist
else
@activeVideoId = @playlistFirstVideoId
# return videoId
@activeVideoId
# check that the video id arg exists in the playlist
hasValidId: (videoId) ->
idList = @getPlaylistIds()
# ie7/8 does not support array.indexOf
i = 0
len = idList.length
while i < len
return true if idList[i] is videoId
i++
false
# 'selects' an item in the playlist
selectPlaylistItem: ($el, eventType) ->
# if no element parameter passed, try to select el based on data-attr
if typeof $el is 'undefined'
$el = @playlist.find('li[data-video-id=' + @activeVideoId + ']')
# make sure the playlist item can be seen in the playlist overflow
# if it's from a click in the playlist, do nothing because it's
# annoying to have the item you just clicked on jump to the top
@bringPlaylistItemIntoView($el.get(0)) if eventType isnt 'click'
# remove active class from list
@playlistVideos.removeClass('active')
# apply class to active element
$el.addClass('active')
# this will bring the video into view inside the playlist
bringPlaylistItemIntoView: (el) ->
# if there's no el, just make it the first video
return false if typeof el is 'undefined'
# use scrollIntoViewIfNeeded if available, else fallback to scrollIntoView
if el.scrollIntoViewIfNeeded
el.scrollIntoViewIfNeeded(true)
else
el.scrollIntoView(true)
# if playlist is not enabled, add a class to component, remove playlist
# from dom and exit, otherwise initialize the playlist
initializePlaylist: ->
unless @options.showPlaylist
@el.addClass 'noPlaylist'
@playlist.remove()
return
# event handler for playlist clicks
@playlist.on 'click', 'li', (e) =>
e.preventDefault()
@updateState(@getVideo(e.currentTarget), 'load', e)
# if video tooltips are enabled, initialize them
@initializePlaylistTooltips() if @options.showPlaylistTooltips
# show the playlist
@playlist.css('visibility', 'visible')
# 'tooltips' that display summary of video
initializePlaylistTooltips: ->
# dom element that contains 'tooltip'
tooltipContainer = $(@options.tooltipHtml)
playlistOffset = @playlist.offset()
# add tooltip to the dom
@el.append tooltipContainer
# event handler for mouse hover over playlist
@playlistVideos.on
mouseenter: ->
$this = $(this)
# text of video summary
videoSummary = $this.find('.featuredVideoSummary').text()
# position tooltip to the top of the playlist item
position = top: $this.offset().top - playlistOffset.top
# check that a description exists for the video before displaying the tooltip
if videoSummary isnt ''
tooltipContainer.text(videoSummary).css('top', position.top).show()
mouseleave: ->
tooltipContainer.hide()
# returns array of video ids from the playlist
getPlaylistIds: ->
arr = []
@playlistVideos.each ->
videoId = $(this).data('videoId').toString()
arr.push videoId
arr
# returns array of duplicate ids, if any
getDuplicatePlaylistIds: ->
ids = @getPlaylistIds()
duplicates = []
collection = {}
key = undefined
i = 0
len = ids.length
while i < len
key = ids[i].toString()
if typeof collection[key] is 'undefined'
collection[key] = true
else
duplicates.push key
i++
duplicates
# allows in-page links to update the video by passing video ids through hash in url
watchHash: ->
# bind to hash
if 'onhashchange' of window
$(window).on 'hashchange', (e) =>
e.preventDefault()
id = @getIdFromUrl()
if id is undefined or id == @hashObject[@stateKey]
return
else
@hashVideoId = @activeVideoId = id
@updateState(@getVideo(), 'load', undefined)
# checks if there's a hash for video player state maintenance
# if there is, set activeVideo var to hash state
getStateFromHash: ->
@hashObject = @getHashObject()
return null if !@hashObject
state = @hashObject[@stateKey] ? null
return null if !state
@activeTab = @hashObject[@stateKey]
# update url hash with current video id
updateUrlHash: (videoId) ->
window.location.hash = 'videoId=' + videoId if @options.maintainState
# returns the hash from the current window or null
getUrlHash: ->
if window.location.hash then window.location.hash.substring(1) else null
# returns object created from hash or null
getHashObject: ->
hash = @getUrlHash()
return null if !hash
args = {}
arr = hash.split('&')
for item in arr
arg = item.split('=')
if (arg.length > 1)
args[arg[0]] = arg[1]
else
args[arg[0]] = undefined
args
# attempts to return a video id that was passed in the url
getIdFromUrl: ->
obj = @getHashObject()
# if an object is returned set videoId to @stateKey in obj
if obj?
videoId = obj[@stateKey]
# if there's no hash object, also check for videoId in query string
else
videoId = @getIdFromQueryString()
# a hash object could be returned with no id
if typeof videoId is 'undefined' or videoId is ''
videoId = null
videoId
# edge-case where url may be loaded with a query string that contains a video id param
# example: ?bcpid=2117382598001&bckey=AQ~~,AAAAAFGE4wo~,g57wOIK2TXKMBHTPnffWcp0t79yQC9T_&bctid=1897188942001
# this function is a fallback that will return the video id from that string
getIdFromQueryString: ->
match = window.location.search.match(new RegExp('[?&]' + 'bctid' + '=([^&]+)(&|$)'))
return match && decodeURIComponent(match[1].replace(/\+/g, ' '))
# converts the hash object into a string for the url hash
buildHashObject: () ->
$.param(@hashObject)
# updates the cached hash object and then updates url
updateHash: (id) ->
# convert to string
id += ''
# get fresh hash obj in case another component has altered it
@hashObject = @getHashObject()
# if @hashObject is null, create it
@hashObject = {} if !@hashObject
# update hash
@hashObject[@stateKey] = id
@setUrlHash(@buildHashObject())
# updates url hash with tab identifier
setUrlHash: (hash) ->
window.location.hash = hash
# updates the state of the component
updateState: (videoId, playType, e) ->
@activeVideoId = videoId
# update url hash
if @options.maintainState
@updateHash(@activeVideoId)
# play the selected video
@playVideo(playType, videoId)
# if showing a playlist, 'select' the correct video in the list
if @options.showPlaylist
item = if e and e.currentTarget then $(e.currentTarget) else undefined
eventType = if e and e.type then e.type else 'none'
@selectPlaylistItem(item, eventType)
# lightweight wrapper around the constructor that prevents multiple instantiations
$.fn[pluginName] = (options) ->
@each ->
if !$.data(@, 'plugin_#{pluginName}')
$.data(@, 'plugin_#{pluginName}', new Video(@, options))
return
return