Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 0.0.2
  3.  * playlist for plyr
  4.  *
  5.  * @ blackui.com All Rights Reserved.
  6.  * Author url: blackui.com
  7.  */
  8.  
  9. window.Plyrist = {};
  10. window.Plyrists = [];
  11.  
  12. (function ($, undefined) {
  13.   "use strict";
  14.  
  15.   Plyrist = function(el, items, options){
  16.     var self = this;
  17.     this.active = 0;
  18.     this.repeat = false;
  19.     this.shuffle = false;
  20.     this.items = items || [];
  21.     this.oitems = $.merge([],items);
  22.  
  23.     this.el = $.extend({}, this._el, el);
  24.     this.options = $.extend(true, {}, this._options, options);
  25.  
  26.     $.each(this.el, function(key, value) {
  27.       if(key !== 'playlist' && key !=='controls'){
  28.         self.el[key] = self.el.playlist + ' ' + self.el[key];
  29.       }
  30.     });
  31.  
  32.     this._createControls();
  33.     this._createItems();
  34.  
  35.     this.player = new Plyr(this.el.player, this.options);
  36.     Plyrists.push(this.player);
  37.  
  38.     this.player.on('ready', event => {
  39.         self._initEvents();
  40.         self._initState();
  41.     });
  42.  
  43.     this.player.on('play', event => {
  44.       Plyrists.forEach(function(player) {
  45.           if(player !== self.player){
  46.             player.pause();
  47.           }
  48.       });
  49.     });
  50.  
  51.     this.player.on('loadedmetadata', event => {
  52.       var duration = $(this.el.playlist + ' .plyr__time--duration');
  53.       this.player.duration == 'Infinity' ? duration.hide() : duration.show();
  54.     });
  55.  
  56.     this.player.on('ended', function() {
  57.       if(self.repeat){
  58.         self._play(self.active);
  59.       }else{
  60.         if(self.items.length > 1){
  61.           self.next();
  62.         }else{
  63.           self.player.restart();
  64.         }
  65.       }
  66.     });
  67.  
  68.     this._init();
  69.   }
  70.  
  71.   Plyrist.prototype = {
  72.     _el: {
  73.       playlist: '#playlist',
  74.       player: "audio",
  75.       items: '.plyr-list',
  76.       item: '.plyr-item',
  77.       itemHtml: '<div class="plyr-item"><div class="plyr-item-poster"></div><div class="flex"><div class="plyr-item-title h-1x"></div><div class="plyr-item-author text-sm text-fade"></div></div><button class="plyr-item-close close text">&times;</button></div>',
  78.       itemTitle: '.plyr-item-title',
  79.       itemAuhtor: '.plyr-item-author',
  80.       itemPoster: '.plyr-item-poster',
  81.       itemClose: '.plyr-item-close',
  82.       poster: '.plyr__poster',
  83.       title: '.plyr__title',
  84.       author: '.plyr__author',
  85.       prev: '[data-plyr="prev"]',
  86.       next: '[data-plyr="next"]',
  87.       like: '[data-plyr="like"]',
  88.       shuffle: '[data-plyr="shuffle"]',
  89.       repeat: '[data-plyr="repeat"]',
  90.       list: '[data-plyr="list"]',
  91.       controls: {
  92.         prev: '<button type="button" class="plyr__control" data-plyr="prev"><svg role="presentation"><use xlink:href="#plyr-prev"></use></svg><span class="plyr__tooltip" role="tooltip">Prev</span></button>',
  93.         play: '<button type="button" class="plyr__control" aria-pressed="false" aria-label="Play, {title}" data-plyr="play"><svg class="icon--pressed" role="presentation"><use xlink:href="#plyr-pause"></use></svg><svg class="icon--not-pressed" role="presentation"><use xlink:href="#plyr-play"></use></svg><span class="label--pressed plyr__tooltip" role="tooltip">Pause</span><span class="label--not-pressed plyr__tooltip" role="tooltip">Play</span></button>',
  94.         next: '<button type="button" class="plyr__control" data-plyr="next"><svg role="presentation"><use xlink:href="#plyr-next"></use></svg><span class="plyr__tooltip" role="tooltip">Next</span></button>',
  95.         poster: '<div class="plyr__poster"></div>',
  96.         title: '<a class="plyr__title ajax" href="#"></a>',
  97.         author: '<div class="plyr__author"></div>',
  98.         progress: '<div class="plyr__progress"><input data-plyr="seek" type="range" min="0" max="100" step="0.01" value="0" aria-label="Seek"><progress class="plyr__progress__buffer" min="0" max="100" value="0">% buffered</progress><span role="tooltip" class="plyr__tooltip">00:00</span></div>',
  99.         currentTime: '<div class="plyr__time plyr__time--current" aria-label="Current time">00:00</div>',
  100.         duration: '<div class="plyr__time plyr__time--duration" aria-label="Duration">00:00</div>',
  101.         mute: '<button type="button" class="plyr__control" aria-pressed="false" aria-label="Mute" data-plyr="mute"><svg class="icon--pressed" role="presentation"><use xlink:href="#plyr-muted"></use></svg><svg class="icon--not-pressed" role="presentation"><use xlink:href="#plyr-volume"></use></svg><span class="label--pressed plyr__tooltip" role="tooltip">Unmute</span><span class="label--not-pressed plyr__tooltip" role="tooltip">Mute</span></button>',
  102.         volume: '<div class="plyr__volume"><input data-plyr="volume" type="range" min="0" max="1" step="0.05" value="1" autocomplete="off" aria-label="Volume"></div>',
  103.         like: '<button type="button" class="plyr__control" aria-pressed="false" data-plyr="like"><svg class="icon--pressed" role="presentation"><use xlink:href="#plyr-liked"></use></svg><svg class="icon--not-pressed" role="presentation"><use xlink:href="#plyr-like"></use></svg><span class="label--pressed plyr__tooltip" role="tooltip">Dislike</span><span class="label--not-pressed plyr__tooltip" role="tooltip">Like</span></button>',
  104.         shuffle: '<button type="button" class="plyr__control" data-plyr="shuffle"><svg role="presentation"><use xlink:href="#plyr-shuffle"></use></svg><span class="plyr__tooltip" role="tooltip">Shuffle</span></button>',
  105.         repeat: '<button type="button" class="plyr__control" data-plyr="repeat"><svg role="presentation"><use xlink:href="#plyr-repeat"></use></svg><span class="plyr__tooltip" role="tooltip">Repeat</span></button>',
  106.         list: '<button type="button" class="plyr__control" data-plyr="list"><svg role="presentation"><use xlink:href="#plyr-list"></use></svg><span class="plyr__tooltip" role="tooltip">Playlist</span></button>'
  107.       }
  108.     },
  109.     _options: {
  110.       theme: 0,
  111.       iconUrl: '../libs/plyrist/src/plyrist.svg',
  112.       autoplay: true,
  113.       hideControls: false,
  114.       fullscreen:{ enabled: false }
  115.     },
  116.     _init: function(){
  117.       var self = this;
  118.       if(self.options.autoplay) {
  119.         self._play(self.active);
  120.       }else{
  121.         self.select(self.active);
  122.       }
  123.     },
  124.     _initState: function(){
  125.       // to fix css when use audio player to play video
  126.       $('.plyr--video', this.el.playlist+'.plyrist_audio').addClass('plyr--audio');
  127.       this.shuffle ? $(this.el.shuffle).addClass('is--shuffle') : $(this.el.shuffle).removeClass('is--shuffle');
  128.       this.repeat ? $(this.el.repeat).addClass('is--repeat') : $(this.el.repeat).removeClass('is--repeat');
  129.     },
  130.     _initEvents: function() {
  131.       var self = this;
  132.  
  133.       $(this.el.prev).off().on('click', function(){
  134.         self.prev();
  135.       });
  136.  
  137.       $(this.el.next).off().on('click', function(){
  138.         self.next();
  139.       });
  140.  
  141.       $(this.el.like).off().on('click', function(){
  142.         $(this).attr('aria-pressed', $(this).attr('aria-pressed') == 'true' ? 'false' : 'true');
  143.       });
  144.  
  145.       $(this.el.shuffle).off().on('click', function(){
  146.         self.shuffle = ! self.shuffle;
  147.         self._initState();
  148.       });
  149.  
  150.       $(this.el.repeat).off().on('click', function(){
  151.         self.repeat = ! self.repeat;
  152.         self._initState();
  153.       });
  154.  
  155.       $(this.el.list).off().on('click', function(){
  156.         $(self.el.playlist).toggleClass('open');
  157.       });
  158.     },
  159.     _createControls: function(){
  160.       if(this.options.controls) return;
  161.       switch(this.options.theme){
  162.         case 0:
  163.             this.options.controls = this._buildControls([
  164.               this.el.controls.prev,
  165.               this.el.controls.play,
  166.               this.el.controls.next,
  167.               this.el.controls.poster,
  168.               '<div class="plyr__col">',
  169.               '<div class="plyr__row">',
  170.               '<div class="plyr__info plyr__row">',
  171.               this.el.controls.title,
  172.               this.el.controls.author,
  173.               '</div>',
  174.               this.el.controls.currentTime,
  175.               this.el.controls.duration,
  176.               '</div>',
  177.               this.el.controls.progress,
  178.               '</div>',
  179.               this.el.controls.mute,
  180.               this.el.controls.volume,
  181.               this.el.controls.like,
  182.               this.el.controls.repeat,
  183.               this.el.controls.shuffle,
  184.               this.el.controls.list
  185.             ]);
  186.             break;
  187.         case 1:
  188.             this.options.controls = this._buildControls([
  189.               this.el.controls.poster,
  190.               '<div class="plyr__col">',
  191.               this.el.controls.title,
  192.               this.el.controls.author,
  193.               '<div class="plyr__row">',
  194.               this.el.controls.play,
  195.               this.el.controls.prev,
  196.               this.el.controls.next,
  197.               '<div class="plyr__row"></div>',
  198.               this.el.controls.mute,
  199.               this.el.controls.volume,
  200.               this.el.controls.like,
  201.               this.el.controls.repeat,
  202.               this.el.controls.shuffle,
  203.               this.el.controls.list,
  204.               '</div>',
  205.               '<div class="plyr__row">',
  206.               this.el.controls.progress,
  207.               this.el.controls.currentTime,
  208.               this.el.controls.duration,
  209.               '</div>',
  210.               '</div>'
  211.             ]);
  212.             break;
  213.         case 2:
  214.             this.options.controls = this._buildControls([
  215.               this.el.controls.progress,
  216.               this.el.controls.poster,
  217.               '<div class="plyr__col plyr__info">',
  218.               this.el.controls.title,
  219.               this.el.controls.author,
  220.               '</div>',
  221.               this.el.controls.repeat,
  222.               this.el.controls.prev,
  223.               this.el.controls.play,
  224.               this.el.controls.next,
  225.               this.el.controls.shuffle,
  226.               this.el.controls.like,
  227.               '<div class="plyr__row"></div>',
  228.               this.el.controls.currentTime,
  229.               this.el.controls.duration,
  230.               this.el.controls.mute,
  231.               this.el.controls.volume,
  232.               this.el.controls.list
  233.             ]);
  234.             break;
  235.         default:
  236.             break;
  237.       }
  238.       $(this.el.playlist).addClass('plyrist-theme-'+this.options.theme);
  239.     },
  240.     _buildControls: function(arr){
  241.       var self = this;
  242.       if(self.options.iconUrl){
  243.         $.each(arr, function(key, value) {
  244.           arr[key] = value.replace(/xlink:href="/g, 'xlink:href="'+self.options.iconUrl);
  245.         });
  246.       }
  247.       return '<div class="plyr__controls">'+arr.join('')+'</div>';
  248.     },
  249.     _createItems: function() {
  250.       var self = this;
  251.       //get items from html
  252.       if($(this.el.item).length > 0){
  253.         $(this.el.item).map(function(i) {
  254.           var item = $(this);
  255.           self._initItemEvent(item);
  256.           self.items.push(
  257.             {
  258.               id: item.attr("data-id"),
  259.               title: item.find(self._el.itemTitle).text(),
  260.               author: item.find(self._el.itemAuhtor).text(),
  261.               type: item.attr("data-type"),
  262.               source: item.attr("data-source"),
  263.               provider: item.attr("data-provider"),
  264.               poster: item.attr("data-poster"),
  265.               el: item
  266.             }
  267.           );
  268.         });
  269.       }
  270.  
  271.       // build item html from array
  272.       $.each(self.oitems, function(key, item) {
  273.         self._buildItem(item);
  274.       });
  275.     },
  276.     _buildItem: function(item){
  277.       var el = $(this._el.itemHtml);
  278.       el.attr('data-id', item.id);
  279.       el.find(this._el.itemPoster).css('background-image', item.poster);
  280.       el.find(this._el.itemTitle).text(item.title);
  281.       el.find(this._el.itemAuhtor).text(item.author);
  282.  
  283.       item.el = el;
  284.       $(this.el.items).append(item.el);
  285.       this._initItemEvent(item.el);
  286.       return item;
  287.     },
  288.     _initItemEvent: function(item){
  289.       var self = this;
  290.       $(item).on("click", function(e) {
  291.         var id = $(this).attr('data-id');
  292.         var i = self.getIndex(id);
  293.         if($(e.target).is('.plyr-item-close')){
  294.           e.preventDefault();
  295.           self._removeItem({id: id});
  296.           return;
  297.         }
  298.         if (!$(this).hasClass("active")) {
  299.           self.active = i;
  300.           self._play(i);
  301.         }
  302.       });
  303.     },
  304.     _addItem: function(item){
  305.       var item = this._buildItem(item);
  306.       this.items.push(item);
  307.     },
  308.     _removeItem: function(item){
  309.       var self = this;
  310.       if(item.id == null){
  311.         return;
  312.       }
  313.       var index = this.getIndex(item.id);
  314.       if(index > -1){
  315.         this.items[index].el.remove();
  316.         this.items.splice(index, 1);
  317.         if(this.active == index){
  318.           this.setIndex(self.active, function() {
  319.             self._play(self.active);
  320.           });
  321.         }
  322.       }
  323.     },
  324.     _play: function(index) {
  325.       if(!this.items[index]) return;
  326.       var source = this.items[index] && {
  327.         type: this.items[index]['type'],
  328.         sources: [
  329.           {
  330.             src: this.items[index]["source"],
  331.             provider: this.items[index]["provider"]
  332.           }
  333.         ]
  334.       };
  335.       this.active = index;
  336.       this.player.source = source;
  337.       this.player.play();
  338.       this.select(this.active);
  339.     },
  340.     getItems: function(){
  341.       return this.items;
  342.     },
  343.     setItems: function(items){
  344.       var self = this;
  345.       this.items = items;
  346.       $.each(self.items, function(key, item) {
  347.         self._buildItem(item);
  348.       });
  349.     },
  350.     select: function(index){
  351.       var item = this.items[this.active];
  352.       $(this.el.item).removeClass('active');
  353.       $(this.el.title).html(item.title);
  354.       $(this.el.title).attr('href',item.uri);
  355.       $(this.el.author).html(item.author);
  356.       $(this.el.poster).css('background-image','url('+item.poster+')');
  357.       item.el.addClass('active');
  358.       $(document).trigger('refresh');
  359.     },
  360.     setIndex: function(index, callback) {
  361.       this.active =
  362.         index > this.items.length - 1
  363.           ? 0
  364.           : index < 0 ? this.items.length - 1 : index;
  365.  
  366.       if(this.shuffle){
  367.         this.active = Math.floor(Math.random() * this.items.length);
  368.       }
  369.       if(callback) {
  370.         callback();
  371.       }
  372.     },
  373.     getIndex: function(id){
  374.       for(var i=0; i<this.items.length; i++){
  375.         if(this.items[i].id===id){
  376.             return i;
  377.         }
  378.       }
  379.       return -1;
  380.     },
  381.     getCurrent: function(){
  382.       return this.items[this.active];
  383.     },
  384.     play: function(item, callback){
  385.       var index = this.getIndex(item.id);
  386.       if(index > -1){
  387.         if(index !== this.active){
  388.           this._play(index);
  389.         }else{
  390.           this.player.togglePlay();
  391.         }
  392.       }else{
  393.         this.add(item, true);
  394.       }
  395.       if(callback) {
  396.         callback();
  397.       }
  398.     },
  399.     prev: function() {
  400.       var self = this;
  401.       this.setIndex(self.active - 1, function() {
  402.         self._play(self.active);
  403.       });
  404.     },
  405.     next: function() {
  406.       var self = this;
  407.       this.setIndex(self.active + 1, function() {
  408.         self._play(self.active);
  409.       });
  410.     },
  411.     add: function(item, play){
  412.       this._addItem(item);
  413.       if(play == true){
  414.         this._play(this.items.length - 1);
  415.       }
  416.     },
  417.     remove: function(id){
  418.       this._removeItem({id: id});
  419.     }
  420.   }
  421.  
  422. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement