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-03-20 09:33:14 -04:00
|
|
|
this.data = options.metadata || $.parseJSON( this.$('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-03-31 11:35:15 -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 ) ) {
|
2014-03-31 11:35:15 -04:00
|
|
|
this.settings.pluginPath = _wpmejsSettings.pluginPath;
|
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) {
|
|
|
|
this.player = mejs;
|
|
|
|
this.player.addEventListener( 'ended', this.ended );
|
|
|
|
},
|
|
|
|
|
|
|
|
bindResetPlayer : function (mejs) {
|
|
|
|
this.bindPlayer( mejs );
|
|
|
|
this.playCurrentSrc();
|
|
|
|
},
|
2014-02-24 13:08:16 -05:00
|
|
|
|
2014-03-31 11:35:15 -04:00
|
|
|
setPlayer: function () {
|
|
|
|
if ( this._player ) {
|
|
|
|
this._player.pause();
|
|
|
|
this._player.remove();
|
|
|
|
this.playerNode = this.$( this.data.type );
|
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.settings.success = this.bindResetPlayer;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This is also our bridge to the outside world
|
|
|
|
*/
|
|
|
|
this._player = new MediaElementPlayer( this.playerNode.get(0), this.settings );
|
|
|
|
},
|
|
|
|
|
|
|
|
playCurrentSrc : function () {
|
|
|
|
this.renderCurrent();
|
2014-04-01 15:04:14 -04:00
|
|
|
this.player.setSrc( this.playerNode.attr( 'src' ) );
|
2014-03-31 11:35:15 -04:00
|
|
|
this.player.load();
|
|
|
|
this.player.play();
|
2014-02-24 13:08:16 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
renderCurrent : function () {
|
|
|
|
var dimensions;
|
|
|
|
if ( 'video' === this.data.type ) {
|
|
|
|
if ( this.data.images && this.current.get( 'image' ) ) {
|
|
|
|
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;
|
|
|
|
this.current = this.tracks.at( this.index );
|
|
|
|
this.loadCurrent();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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-03-31 11:35:15 -04:00
|
|
|
var last = this.playerNode.attr( 'src' ).split('.').pop(),
|
|
|
|
current = this.current.get( 'src' ).split('.').pop();
|
|
|
|
|
2014-02-24 13:08:16 -05:00
|
|
|
this.player.pause();
|
2014-03-31 11:35:15 -04:00
|
|
|
|
|
|
|
if ( last !== current ) {
|
|
|
|
this.setPlayer();
|
|
|
|
} 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-04-16 13:40:14 -04:00
|
|
|
if ( ! $( 'body' ).hasClass( 'wp-admin' ) || $( 'body' ).hasClass( 'about-php' ) ) {
|
2014-03-20 09:33: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;
|
|
|
|
|
2014-02-24 13:08:16 -05:00
|
|
|
}(jQuery, _, Backbone));
|