2014-02-27 16:38:17 -05:00
|
|
|
/*globals window, document, jQuery, _, Backbone, _wpmejsSettings */
|
2014-02-24 13:08:16 -05:00
|
|
|
|
|
|
|
(function ($, _, Backbone) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var WPPlaylistView = Backbone.View.extend({
|
2014-03-20 09:33:14 -04:00
|
|
|
initialize : function (options) {
|
2014-03-31 11:35:15 -04:00
|
|
|
this.index = 0;
|
|
|
|
this.settings = {};
|
2014-09-01 15:55:18 -04:00
|
|
|
this.data = options.metadata || $.parseJSON( this.$('script.wp-playlist-script').html() );
|
2014-02-24 13:08:16 -05:00
|
|
|
this.playerNode = this.$( this.data.type );
|
|
|
|
|
|
|
|
this.tracks = new Backbone.Collection( this.data.tracks );
|
|
|
|
this.current = this.tracks.first();
|
|
|
|
|
|
|
|
if ( 'audio' === this.data.type ) {
|
2014-03-31 11:35:15 -04:00
|
|
|
this.currentTemplate = wp.template( 'wp-playlist-current-item' );
|
2014-02-24 13:08:16 -05:00
|
|
|
this.currentNode = this.$( '.wp-playlist-current-item' );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.renderCurrent();
|
|
|
|
|
|
|
|
if ( this.data.tracklist ) {
|
2014-03-31 11:35:15 -04:00
|
|
|
this.itemTemplate = wp.template( 'wp-playlist-item' );
|
2014-03-09 20:15:14 -04:00
|
|
|
this.playingClass = 'wp-playlist-playing';
|
2014-02-24 13:08:16 -05:00
|
|
|
this.renderTracks();
|
|
|
|
}
|
|
|
|
|
2014-07-15 18:18:14 -04:00
|
|
|
this.playerNode.attr( 'src', this.current.get( 'src' ) );
|
2014-02-24 13:08:16 -05:00
|
|
|
|
2014-03-31 11:35:15 -04:00
|
|
|
_.bindAll( this, 'bindPlayer', 'bindResetPlayer', 'setPlayer', 'ended', 'clickTrack' );
|
2014-02-24 13:08:16 -05:00
|
|
|
|
2014-02-27 16:38:17 -05:00
|
|
|
if ( ! _.isUndefined( window._wpmejsSettings ) ) {
|
2016-01-20 10:47:28 -05:00
|
|
|
this.settings = _.clone( _wpmejsSettings );
|
2014-02-24 13:08:16 -05:00
|
|
|
}
|
2014-03-31 11:35:15 -04:00
|
|
|
this.settings.success = this.bindPlayer;
|
|
|
|
this.setPlayer();
|
|
|
|
},
|
|
|
|
|
|
|
|
bindPlayer : function (mejs) {
|
2014-04-19 23:53:15 -04:00
|
|
|
this.mejs = mejs;
|
|
|
|
this.mejs.addEventListener( 'ended', this.ended );
|
2014-03-31 11:35:15 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
bindResetPlayer : function (mejs) {
|
|
|
|
this.bindPlayer( mejs );
|
2014-07-15 18:18:14 -04:00
|
|
|
this.playCurrentSrc();
|
2014-04-19 23:53:15 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setPlayer: function (force) {
|
|
|
|
if ( this.player ) {
|
|
|
|
this.player.pause();
|
|
|
|
this.player.remove();
|
2014-03-31 11:35:15 -04:00
|
|
|
this.playerNode = this.$( this.data.type );
|
2014-04-19 23:53:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (force) {
|
2014-07-15 18:18:14 -04:00
|
|
|
this.playerNode.attr( 'src', this.current.get( 'src' ) );
|
2014-03-31 11:35:15 -04:00
|
|
|
this.settings.success = this.bindResetPlayer;
|
|
|
|
}
|
2014-04-19 23:53:15 -04:00
|
|
|
|
2014-03-31 11:35:15 -04:00
|
|
|
/**
|
|
|
|
* This is also our bridge to the outside world
|
|
|
|
*/
|
2014-04-19 23:53:15 -04:00
|
|
|
this.player = new MediaElementPlayer( this.playerNode.get(0), this.settings );
|
2014-03-31 11:35:15 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
playCurrentSrc : function () {
|
|
|
|
this.renderCurrent();
|
2014-04-19 23:53:15 -04:00
|
|
|
this.mejs.setSrc( this.playerNode.attr( 'src' ) );
|
|
|
|
this.mejs.load();
|
|
|
|
this.mejs.play();
|
2014-02-24 13:08:16 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
renderCurrent : function () {
|
2014-04-18 13:39:15 -04:00
|
|
|
var dimensions, defaultImage = 'wp-includes/images/media/video.png';
|
2014-02-24 13:08:16 -05:00
|
|
|
if ( 'video' === this.data.type ) {
|
2014-04-18 13:39:15 -04:00
|
|
|
if ( this.data.images && this.current.get( 'image' ) && -1 === this.current.get( 'image' ).src.indexOf( defaultImage ) ) {
|
2014-02-24 13:08:16 -05:00
|
|
|
this.playerNode.attr( 'poster', this.current.get( 'image' ).src );
|
|
|
|
}
|
|
|
|
dimensions = this.current.get( 'dimensions' ).resized;
|
2014-02-27 16:38:17 -05:00
|
|
|
this.playerNode.attr( dimensions );
|
2014-02-24 13:08:16 -05:00
|
|
|
} else {
|
|
|
|
if ( ! this.data.images ) {
|
|
|
|
this.current.set( 'image', false );
|
|
|
|
}
|
|
|
|
this.currentNode.html( this.currentTemplate( this.current.toJSON() ) );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renderTracks : function () {
|
2014-03-09 20:15:14 -04:00
|
|
|
var self = this, i = 1, tracklist = $( '<div class="wp-playlist-tracks"></div>' );
|
2014-02-24 13:08:16 -05:00
|
|
|
this.tracks.each(function (model) {
|
2014-03-09 20:15:14 -04:00
|
|
|
if ( ! self.data.images ) {
|
2014-02-24 13:08:16 -05:00
|
|
|
model.set( 'image', false );
|
|
|
|
}
|
2014-03-09 20:15:14 -04:00
|
|
|
model.set( 'artists', self.data.artists );
|
|
|
|
model.set( 'index', self.data.tracknumbers ? i : false );
|
|
|
|
tracklist.append( self.itemTemplate( model.toJSON() ) );
|
2014-02-24 13:08:16 -05:00
|
|
|
i += 1;
|
|
|
|
});
|
|
|
|
this.$el.append( tracklist );
|
2014-03-09 20:15:14 -04:00
|
|
|
|
|
|
|
this.$( '.wp-playlist-item' ).eq(0).addClass( this.playingClass );
|
2014-02-24 13:08:16 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
events : {
|
|
|
|
'click .wp-playlist-item' : 'clickTrack',
|
|
|
|
'click .wp-playlist-next' : 'next',
|
|
|
|
'click .wp-playlist-prev' : 'prev'
|
|
|
|
},
|
|
|
|
|
|
|
|
clickTrack : function (e) {
|
2014-04-07 18:28:16 -04:00
|
|
|
e.preventDefault();
|
|
|
|
|
2014-02-24 13:08:16 -05:00
|
|
|
this.index = this.$( '.wp-playlist-item' ).index( e.currentTarget );
|
|
|
|
this.setCurrent();
|
|
|
|
},
|
|
|
|
|
|
|
|
ended : function () {
|
|
|
|
if ( this.index + 1 < this.tracks.length ) {
|
|
|
|
this.next();
|
|
|
|
} else {
|
|
|
|
this.index = 0;
|
2014-06-01 20:27:18 -04:00
|
|
|
this.setCurrent();
|
2014-02-24 13:08:16 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
next : function () {
|
|
|
|
this.index = this.index + 1 >= this.tracks.length ? 0 : this.index + 1;
|
|
|
|
this.setCurrent();
|
|
|
|
},
|
|
|
|
|
|
|
|
prev : function () {
|
|
|
|
this.index = this.index - 1 < 0 ? this.tracks.length - 1 : this.index - 1;
|
|
|
|
this.setCurrent();
|
|
|
|
},
|
|
|
|
|
|
|
|
loadCurrent : function () {
|
2014-04-19 23:53:15 -04:00
|
|
|
var last = this.playerNode.attr( 'src' ) && this.playerNode.attr( 'src' ).split('.').pop(),
|
2014-03-31 11:35:15 -04:00
|
|
|
current = this.current.get( 'src' ).split('.').pop();
|
|
|
|
|
2014-04-19 23:53:15 -04:00
|
|
|
this.mejs && this.mejs.pause();
|
2014-03-31 11:35:15 -04:00
|
|
|
|
|
|
|
if ( last !== current ) {
|
2014-04-19 23:53:15 -04:00
|
|
|
this.setPlayer( true );
|
2014-07-23 13:54:15 -04:00
|
|
|
} else {
|
2014-04-01 15:04:14 -04:00
|
|
|
this.playerNode.attr( 'src', this.current.get( 'src' ) );
|
2014-03-31 11:35:15 -04:00
|
|
|
this.playCurrentSrc();
|
|
|
|
}
|
2014-02-24 13:08:16 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
setCurrent : function () {
|
|
|
|
this.current = this.tracks.at( this.index );
|
2014-03-09 20:15:14 -04:00
|
|
|
|
|
|
|
if ( this.data.tracklist ) {
|
|
|
|
this.$( '.wp-playlist-item' )
|
|
|
|
.removeClass( this.playingClass )
|
|
|
|
.eq( this.index )
|
|
|
|
.addClass( this.playingClass );
|
|
|
|
}
|
|
|
|
|
2014-02-24 13:08:16 -05:00
|
|
|
this.loadCurrent();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
2014-07-15 18:18:14 -04:00
|
|
|
$('.wp-playlist').each( function() {
|
|
|
|
return new WPPlaylistView({ el: this });
|
|
|
|
} );
|
2014-02-24 13:08:16 -05:00
|
|
|
});
|
|
|
|
|
2014-03-20 09:33:14 -04:00
|
|
|
window.WPPlaylistView = WPPlaylistView;
|
|
|
|
|
2016-01-20 10:47:28 -05:00
|
|
|
}(jQuery, _, Backbone));
|