-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquality-selector.min.js
6 lines (6 loc) · 7.57 KB
/
quality-selector.min.js
1
2
3
4
5
6
/*
* Clappr Quality Selector Plugin v2.0.0
* modded by: ewwink
* github: https://github.com/ewwink/clappr-quality-selector-plugin
*/
var QualitySelector=Clappr.UICorePlugin.extend({name:"quality_selector",version:"2.0",bindEvents:function(){this.listenTo(this.core,Clappr.Events.CORE_READY,this.bindPlaybackEvents);this.listenTo(this.core,Clappr.Events.CORE_ACTIVE_CONTAINER_CHANGED,this.reload);this.listenTo(this.core.mediaControl,Clappr.Events.MEDIACONTROL_RENDERED,this.render);this.listenTo(this.core.mediaControl,Clappr.Events.MEDIACONTROL_HIDE,this.hideSelectLevelMenu);this.listenTo(this.core.mediaControl,Clappr.Events.MEDIACONTROL_SHOW,this.showHDIcon)},unBindEvents:function(){var a=this.core.activePlayback;this.stopListening(this.core,Clappr.Events.CORE_READY);this.stopListening(this.core,Clappr.Events.CORE_ACTIVE_CONTAINER_CHANGED);this.stopListening(this.core.mediaControl,Clappr.Events.MEDIACONTROL_RENDERED);this.stopListening(this.core.mediaControl,Clappr.Events.MEDIACONTROL_HIDE);this.stopListening(this.core.mediaControl,Clappr.Events.MEDIACONTROL_SHOW);this.stopListening(a,Clappr.Events.PLAYBACK_LEVELS_AVAILABLE);this.stopListening(a,Clappr.Events.PLAYBACK_LEVEL_SWITCH_START);this.stopListening(a,Clappr.Events.PLAYBACK_LEVEL_SWITCH_END);this.stopListening(a,Clappr.Events.PLAYBACK_BITRATE);this.stopListening(a,Clappr.Events.PLAYBACK_STOP);this.stopListening(a,Clappr.Events.PLAYBACK_PLAY_INTENT)},bindPlaybackEvents:function(){var a=this.core.activePlayback;this.listenTo(a,Clappr.Events.PLAYBACK_LEVELS_AVAILABLE,this.fillLevels);this.listenTo(a,Clappr.Events.PLAYBACK_LEVEL_SWITCH_START,this.startLevelSwitch);this.listenTo(a,Clappr.Events.PLAYBACK_LEVEL_SWITCH_END,this.stopLevelSwitch);this.listenTo(a,Clappr.Events.PLAYBACK_BITRATE,this.updateCurrentLevel);this.listenTo(a,Clappr.Events.PLAYBACK_PLAY_INTENT,this.playbackIntent);this.listenTo(a,Clappr.Events.PLAYBACK_STOP,this.playbackStop);a.levels&&0<a.levels.length&&this.fillLevels(a.levels)},reload:function(){this.unBindEvents();this.bindEvents();this.bindPlaybackEvents()},shouldRender:function(){if(!this.core.activeContainer)return!1;var a=this.core.activePlayback;if(!a)return!1;var b=!!(this.levels&&1<this.levels.length);return void 0!==a.currentLevel&&b},render:function(){if(this.shouldRender()){var a=this.pluginStyle;this.$el.html(this.template(this.levels,this.getTitle()));this.$el.append(a);this.core.mediaControl.$(".media-control-right-panel").append(this.el);this.highlightCurrentLevel()}return this},template:function(a,b){var c="<button data-quality-selector-button>Auto</button><ul>";b&&(c+="<li data-title>"+b+"</li>");for(var d=a.length-1;-1<d;d--)c+='<li><a href="#" data-quality-selector-select="'+a[d].id+'">'+a[d].label+"</a></li>";return c+'<li><a href="#" data-quality-selector-select="-1">AUTO</a></li>'},showHDIcon:function(){if(!this.HDChecked){this.HDChecked=!0;for(var a=this.core.activePlayback._hls.levels,b=0;b<a.length;b++)if(720<=a[b].height||2E3<=a[b].bitrate/1E3){$(this._options.parentId).addClass("showHDIcon");break}}},playbackIntent:function(){if(this.FIRST_START){this.FIRST_START=!1;var a=this.DEFAULT_QUALITY=parseInt(localStorage.getItem("savedLevelId"));if(isNaN(a)){var b=this._options.qualitySelectorConfig;b&&!isNaN(b.defaultQuality)&&(a=b.defaultQuality)}isNaN(a)||(this.core.activePlayback.currentLevel=this.selectedLevelId=a)}},playbackStop:function(){this.FIRST_START=!0},fillLevels:function(a,b){void 0===this.selectedLevelId&&(this.selectedLevelId=b?b:-1);this.levels=a;this.configureLevelsLabels();this.render()},configureLevelsLabels:function(){if(void 0!==this.core.options.qualitySelectorConfig){var a=this.core.options.qualitySelectorConfig.labelCallback;if(a&&"function"!==typeof a)throw new TypeError("labelCallback must be a function");var b=this.core.options.qualitySelectorConfig.labels,c=b?this.core.options.qualitySelectorConfig.labels:{};if(a||b)for(var d,e=0;e<this.levels;e++)b=this.levels[e],d=c[b.id],a?b.label=a(b,d):d&&(b.label=d)}},onLevelSelect:function(a){this.selectedLevelId=parseInt(a.target.dataset.qualitySelectorSelect,10);var b=this.core.activePlayback._hls;if(b.nextLevel==this.selectedLevelId)return!1;var c=this.selectedLevelId;b.nextLevel=c;localStorage.setItem("savedLevelId",c);this.toggleContextMenu();a.stopPropagation();return!1},onShowLevelSelectMenu:function(){this.toggleContextMenu()},hideSelectLevelMenu:function(){this.$(".quality_selector ul").hide()},toggleContextMenu:function(){this.$(".quality_selector ul").toggle()},buttonElement:function(){return this.$(".quality_selector button")},levelElement:function(a){return this.$(".quality_selector ul a"+(isNaN(a)?"":'[data-quality-selector-select="'+a+'"]')).parent()},getTitle:function(){return(this.core.options.qualitySelectorConfig||{}).title},startLevelSwitch:function(){this.buttonElement().addClass("changing")},stopLevelSwitch:function(){this.buttonElement().removeClass("changing")},findLevelBy:function(a){var b;this.levels.forEach(function(c){c.id===a&&(b=c)});return b},updateText:function(a){if(-1===a)this.buttonElement().text(this.currentLevel?"AUTO ("+this.currentLevel.label+")":"AUTO");else try{this.buttonElement().text(this.findLevelBy(a).label)}catch(b){localStorage.setItem("savedLevelId","-1"),a=this.core.activePlayback,a.currentLevel=-1,a.trigger(Clappr.Events.PLAYBACK_LEVEL_SWITCH_END),this.updateText(-1)}},updateCurrentLevel:function(a){this.currentLevel=(a=this.findLevelBy(a.level))?a:null;this.highlightCurrentLevel()},highlightCurrentLevel:function(){this.levelElement().removeClass("current");this.currentLevel&&this.levelElement(this.currentLevel.id).addClass("current");this.updateText(this.selectedLevelId)},pluginStyle:'<style>.quality_selector[data-quality-selector]{float:right;position:relative;height:100%}.quality_selector button{cursor:pointer;min-width:80px}.quality_selector[data-quality-selector] button{background-color:transparent;color:#fff;font-family:Roboto,"Open Sans",Arial,sans-serif;-webkit-font-smoothing:antialiased;border:none;font-size:12px;height:100%}.quality_selector[data-quality-selector] button:hover{color:#c9c9c9}.quality_selector[data-quality-selector] button.changing{-webkit-animation:pulse .5s infinite alternate}.quality_selector[data-quality-selector] > ul{list-style-type:none;position:absolute;bottom:100%;display:none;background-color:rgba(28,28,28,0.9);white-space:nowrap}.quality_selector[data-quality-selector] li{font-size:12px;color:#eee}.quality_selector[data-quality-selector] li[data-title]{background-color:#333;padding:8px 25px}.quality_selector[data-quality-selector] li a{color:#eee;padding:5px 10px;display:block;text-decoration:none}.quality_selector[data-quality-selector] li a:hover{background-color:rgba(255,255,255,0.1);color:#fff}.quality_selector[data-quality-selector] li a:hover a{color:#fff;text-decoration:none}.quality_selector[data-quality-selector] li.current a{color:#2ecc71}@-webkit-keyframes pulse{0%{color:#fff}50%{color:#ff0101}100%{color:#B80000}}.showHDIcon button[data-hd-indicator]{display:inline-block !important}</style>',attributes:{"class":"quality_selector","data-quality-selector":""},events:{"click [data-quality-selector-select]":"onLevelSelect","click [data-quality-selector-button]":"onShowLevelSelectMenu"},DEFAULT_QUALITY:parseInt(localStorage.getItem("savedLevelId")),FIRST_START:!0,HDChecked:!1});Object.defineProperty(QualitySelector,"name",{value:"QualitySelector"});Clappr.Player.prototype.setPlaybackQuality=function(a){var b=this.getPlugin("quality_selector");b.levels&&a<=b.levels.length-1&&(b.selectedLevelId=this.core.activePlayback.currentLevel=a,b.updateCurrentLevel({level:a}))};Clappr.Player.prototype.getPlaybackQuality=function(){return this.getPlugin("hls").levels};