1/*globals window, document, jQuery, _, Backbone, _wpmejsSettings */ 2 3(function ($, _, Backbone) { 4 "use strict"; 5 6 var WPPlaylistView = Backbone.View.extend({ 7 initialize : function (options) { 8 9 // Resize players to 100% 10 $('video').css({'width': '100%', 'height': '100%'}) 11 .attr('width', '100%').attr('height', '100%'); 12 $('audio').css('width','100%').attr('width', '100%'); 13 14 this.index = 0; 15 this.settings = {}; 16 this.data = options.metadata || $.parseJSON( this.$('script').html() ); 17 this.playerNode = this.$( this.data.type ); 18 19 20 this.tracks = new Backbone.Collection( this.data.tracks ); 21 this.current = this.tracks.first(); 22 23 if ( 'audio' === this.data.type ) { 24 this.currentTemplate = wp.template( 'wp-playlist-current-item' ); 25 this.currentNode = this.$( '.wp-playlist-current-item' ); 26 } 27 28 this.renderCurrent(); 29 30 if ( this.data.tracklist ) { 31 this.itemTemplate = wp.template( 'wp-playlist-item' ); 32 this.playingClass = 'wp-playlist-playing'; 33 this.renderTracks(); 34 } 35 36 this.playerNode.attr( 'src', this.current.get( 'src' ) ); 37 38 _.bindAll( this, 'bindPlayer', 'bindResetPlayer', 'setPlayer', 'ended', 'clickTrack' ); 39 40 if ( ! _.isUndefined( window._wpmejsSettings ) ) { 41 this.settings.pluginPath = _wpmejsSettings.pluginPath; 42 } 43 this.settings.success = this.bindPlayer; 44 this.setPlayer(); 45 }, 46 47 bindPlayer : function (mejs) { 48 this.player = mejs; 49 this.player.addEventListener( 'ended', this.ended ); 50 51 }, 52 53 bindResetPlayer : function (mejs) { 54 this.bindPlayer( mejs ); 55 this.playCurrentSrc(); 56 }, 57 58 setPlayer: function () { 59 if ( this._player ) { 60 this._player.pause(); 61 this._player.remove(); 62 this.playerNode = this.$( this.data.type ); 63 this.playerNode.attr( 'src', this.current.get( 'src' ) ); 64 this.settings.success = this.bindResetPlayer; 65 } 66 /** 67 * This is also our bridge to the outside world 68 */ 69 this._player = new MediaElementPlayer( this.playerNode.get(0), this.settings ); 70 }, 71 72 playCurrentSrc : function () { 73 this.renderCurrent(); 74 this.player.setSrc( this.playerNode.attr( 'src' ) ); 75 this.player.load(); 76 this.player.play(); 77 }, 78 79 renderCurrent : function () { 80 var dimensions; 81 if ( 'video' === this.data.type ) { 82 if ( this.data.images && this.current.get( 'image' ) ) { 83 this.playerNode.attr( 'poster', this.current.get( 'image' ).src ); 84 } 85 dimensions = this.current.get( 'dimensions' ).resized; 86 this.playerNode.attr( dimensions ); 87 } else { 88 if ( ! this.data.images ) { 89 this.current.set( 'image', false ); 90 } 91 this.currentNode.html( this.currentTemplate( this.current.toJSON() ) ); 92 } 93 }, 94 95 renderTracks : function () { 96 var self = this, i = 1, tracklist = $( '<div class="wp-playlist-tracks"></div>' ); 97 this.tracks.each(function (model) { 98 if ( ! self.data.images ) { 99 model.set( 'image', false ); 100 } 101 model.set( 'artists', self.data.artists ); 102 model.set( 'index', self.data.tracknumbers ? i : false ); 103 tracklist.append( self.itemTemplate( model.toJSON() ) ); 104 i += 1; 105 }); 106 this.$el.append( tracklist ); 107 108 this.$( '.wp-playlist-item' ).eq(0).addClass( this.playingClass ); 109 }, 110 111 events : { 112 'click .wp-playlist-item' : 'clickTrack', 113 'click .wp-playlist-next' : 'next', 114 'click .wp-playlist-prev' : 'prev' 115 }, 116 117 clickTrack : function (e) { 118 e.preventDefault(); 119 120 this.index = this.$( '.wp-playlist-item' ).index( e.currentTarget ); 121 this.setCurrent(); 122 }, 123 124 ended : function () { 125 if ( this.index + 1 < this.tracks.length ) { 126 this.next(); 127 } else { 128 this.index = 0; 129 this.current = this.tracks.at( this.index ); 130 this.loadCurrent(); 131 } 132 }, 133 134 next : function () { 135 this.index = this.index + 1 >= this.tracks.length ? 0 : this.index + 1; 136 this.setCurrent(); 137 }, 138 139 prev : function () { 140 this.index = this.index - 1 < 0 ? this.tracks.length - 1 : this.index - 1; 141 this.setCurrent(); 142 }, 143 144 loadCurrent : function () { 145 var last = this.playerNode.attr( 'src' ).split('.').pop(), 146 current = this.current.get( 'src' ).split('.').pop(); 147 148 this.player.pause(); 149 150 if ( last !== current ) { 151 this.setPlayer(); 152 } else { 153 this.playerNode.attr( 'src', this.current.get( 'src' ) ); 154 this.playCurrentSrc(); 155 } 156 }, 157 158 setCurrent : function () { 159 this.current = this.tracks.at( this.index ); 160 161 if ( this.data.tracklist ) { 162 this.$( '.wp-playlist-item' ) 163 .removeClass( this.playingClass ) 164 .eq( this.index ) 165 .addClass( this.playingClass ); 166 } 167 168 this.loadCurrent(); 169 } 170 }); 171 172 $(document).ready(function () { 173 if ( ! $( 'body' ).hasClass( 'wp-admin' ) || $( 'body' ).hasClass( 'about-php' ) ) { 174 $('.wp-playlist').each(function () { 175 return new WPPlaylistView({ el: this }); 176 }); 177 } 178 }); 179 180 window.WPPlaylistView = WPPlaylistView; 181 182}(jQuery, _, Backbone));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.