Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

176 wiersze
4.6 KiB

  1. /* global _wpmejsSettings, MediaElementPlayer */
  2. (function ($, _, Backbone) {
  3. 'use strict';
  4. var WPPlaylistView = Backbone.View.extend({
  5. initialize : function (options) {
  6. this.index = 0;
  7. this.settings = {};
  8. this.data = options.metadata || $.parseJSON( this.$('script.wp-playlist-script').html() );
  9. this.playerNode = this.$( this.data.type );
  10. this.tracks = new Backbone.Collection( this.data.tracks );
  11. this.current = this.tracks.first();
  12. if ( 'audio' === this.data.type ) {
  13. this.currentTemplate = wp.template( 'wp-playlist-current-item' );
  14. this.currentNode = this.$( '.wp-playlist-current-item' );
  15. }
  16. this.renderCurrent();
  17. if ( this.data.tracklist ) {
  18. this.itemTemplate = wp.template( 'wp-playlist-item' );
  19. this.playingClass = 'wp-playlist-playing';
  20. this.renderTracks();
  21. }
  22. this.playerNode.attr( 'src', this.current.get( 'src' ) );
  23. _.bindAll( this, 'bindPlayer', 'bindResetPlayer', 'setPlayer', 'ended', 'clickTrack' );
  24. if ( ! _.isUndefined( window._wpmejsSettings ) ) {
  25. this.settings = _.clone( _wpmejsSettings );
  26. }
  27. this.settings.success = this.bindPlayer;
  28. this.setPlayer();
  29. },
  30. bindPlayer : function (mejs) {
  31. this.mejs = mejs;
  32. this.mejs.addEventListener( 'ended', this.ended );
  33. },
  34. bindResetPlayer : function (mejs) {
  35. this.bindPlayer( mejs );
  36. this.playCurrentSrc();
  37. },
  38. setPlayer: function (force) {
  39. if ( this.player ) {
  40. this.player.pause();
  41. this.player.remove();
  42. this.playerNode = this.$( this.data.type );
  43. }
  44. if (force) {
  45. this.playerNode.attr( 'src', this.current.get( 'src' ) );
  46. this.settings.success = this.bindResetPlayer;
  47. }
  48. /**
  49. * This is also our bridge to the outside world
  50. */
  51. this.player = new MediaElementPlayer( this.playerNode.get(0), this.settings );
  52. },
  53. playCurrentSrc : function () {
  54. this.renderCurrent();
  55. this.mejs.setSrc( this.playerNode.attr( 'src' ) );
  56. this.mejs.load();
  57. this.mejs.play();
  58. },
  59. renderCurrent : function () {
  60. var dimensions, defaultImage = 'wp-includes/images/media/video.png';
  61. if ( 'video' === this.data.type ) {
  62. if ( this.data.images && this.current.get( 'image' ) && -1 === this.current.get( 'image' ).src.indexOf( defaultImage ) ) {
  63. this.playerNode.attr( 'poster', this.current.get( 'image' ).src );
  64. }
  65. dimensions = this.current.get( 'dimensions' ).resized;
  66. this.playerNode.attr( dimensions );
  67. } else {
  68. if ( ! this.data.images ) {
  69. this.current.set( 'image', false );
  70. }
  71. this.currentNode.html( this.currentTemplate( this.current.toJSON() ) );
  72. }
  73. },
  74. renderTracks : function () {
  75. var self = this, i = 1, tracklist = $( '<div class="wp-playlist-tracks"></div>' );
  76. this.tracks.each(function (model) {
  77. if ( ! self.data.images ) {
  78. model.set( 'image', false );
  79. }
  80. model.set( 'artists', self.data.artists );
  81. model.set( 'index', self.data.tracknumbers ? i : false );
  82. tracklist.append( self.itemTemplate( model.toJSON() ) );
  83. i += 1;
  84. });
  85. this.$el.append( tracklist );
  86. this.$( '.wp-playlist-item' ).eq(0).addClass( this.playingClass );
  87. },
  88. events : {
  89. 'click .wp-playlist-item' : 'clickTrack',
  90. 'click .wp-playlist-next' : 'next',
  91. 'click .wp-playlist-prev' : 'prev'
  92. },
  93. clickTrack : function (e) {
  94. e.preventDefault();
  95. this.index = this.$( '.wp-playlist-item' ).index( e.currentTarget );
  96. this.setCurrent();
  97. },
  98. ended : function () {
  99. if ( this.index + 1 < this.tracks.length ) {
  100. this.next();
  101. } else {
  102. this.index = 0;
  103. this.setCurrent();
  104. }
  105. },
  106. next : function () {
  107. this.index = this.index + 1 >= this.tracks.length ? 0 : this.index + 1;
  108. this.setCurrent();
  109. },
  110. prev : function () {
  111. this.index = this.index - 1 < 0 ? this.tracks.length - 1 : this.index - 1;
  112. this.setCurrent();
  113. },
  114. loadCurrent : function () {
  115. var last = this.playerNode.attr( 'src' ) && this.playerNode.attr( 'src' ).split('.').pop(),
  116. current = this.current.get( 'src' ).split('.').pop();
  117. this.mejs && this.mejs.pause();
  118. if ( last !== current ) {
  119. this.setPlayer( true );
  120. } else {
  121. this.playerNode.attr( 'src', this.current.get( 'src' ) );
  122. this.playCurrentSrc();
  123. }
  124. },
  125. setCurrent : function () {
  126. this.current = this.tracks.at( this.index );
  127. if ( this.data.tracklist ) {
  128. this.$( '.wp-playlist-item' )
  129. .removeClass( this.playingClass )
  130. .eq( this.index )
  131. .addClass( this.playingClass );
  132. }
  133. this.loadCurrent();
  134. }
  135. });
  136. $(document).ready(function () {
  137. $('.wp-playlist').each( function() {
  138. return new WPPlaylistView({ el: this });
  139. } );
  140. });
  141. window.WPPlaylistView = WPPlaylistView;
  142. }(jQuery, _, Backbone));