@lawchihon/videojs-dailymotion
Version:
Dailymotion playback technology for Video.js
1 lines • 9.62 kB
JavaScript
(function(root,factory){if(typeof window!=="undefined"&&window.videojs){factory(window.videojs)}else if(typeof exports==="object"&&typeof module!=="undefined"){var videojs=require("video.js");module.exports=factory(videojs.default||videojs)}else if(typeof define==="function"&&define.amd){define(["videojs"],function(videojs){return root.Dailymotion=factory(videojs)})}else{root.Dailymotion=factory(root.videojs)}})(this,function(videojs){"use strict";var _isOnMobile=videojs.browser.IS_IOS||videojs.browser.IS_NATIVE_ANDROID;var Tech=videojs.getTech("Tech");var Dailymotion=videojs.extend(Tech,{constructor:function(options,ready){Tech.call(this,options,ready);this.setSrc(this.options_.source);var vm=this;setTimeout(function(){if(this.el_){this.el_.parentNode.className+=" vjs-dailymotion";if(_isOnMobile){this.el_.parentNode.className+=" vjs-dailymotion-mobile"}if(Dailymotion.isSdkReady){vm.initDMPlayer()}else{Dailymotion.sdkReadyQueue.push(vm)}}}.bind(this))},_getPlayerParams:function(){var playerParams={autoplay:false,mute:false,controls:false,"queue-autoplay-next":false,"queue-enable":false};var params=["api","autoplay","autoplay-mute","id","mute","origin","quality","queue-autoplay-next","queue-enable","sharing-enable","start","subtitles-default","syndication","ui-highlight","ui-logo","ui-start-screen-info","ui-theme","apimode","playlist"];var options=this.options_;params.forEach(function(param){if(typeof options[param]==="undefined"){return}playerParams[param]=options[param]});if(typeof this.options_.dmControls!=="undefined"){playerParams.controls=this.options_.dmControls}if(this.url&&typeof this.url.playlist!=="undefined"){playerParams.playlist=this.url.playlist}if(typeof this.options_.customVars!=="undefined"){var customVars=this.options_.customVars;Object.keys(customVars).forEach(function(key){playerParams[key]=customVars[key]})}return playerParams},_getPlayerConfig:function(){var playerConfig={width:"100%",height:"100%",params:this._getPlayerParams()};if(this.url&&typeof this.url.video!=="undefined"){playerConfig.video=this.url.video}else if(typeof this.options_.video!=="undefined"){playerConfig.video=this.options_.video}return playerConfig},initDMPlayer:function(){if(this.dmPlayer){return}this.dmPlayer=new DM.player(document.getElementById(this.options_.techId),this._getPlayerConfig());var vm=this;this.isApiReady=false;this.dmPlayer.addEventListener("apiready",function(){vm.triggerReady();vm.isApiReady=true});this.dmPlayer.addEventListener("durationchange",function(){vm.trigger("durationchange")});this.dmPlayer.addEventListener("end",function(){vm.trigger("ended")});this.dmPlayer.addEventListener("error",function(){vm.trigger("error")});this.dmPlayer.addEventListener("loadedmetadata",function(){vm.trigger("loadeddata");vm.trigger("loadedmetadata")});this.dmPlayer.addEventListener("pause",function(){vm.trigger("pause")});this.dmPlayer.addEventListener("play",function(){vm.trigger("loadStart");vm.trigger("play");vm.trigger("playing");vm.trigger("waiting")});this.dmPlayer.addEventListener("playback_ready",function(){vm.trigger("loadeddata")});this.dmPlayer.addEventListener("timeupdate",function(){vm.trigger("timeupdate")});this.dmPlayer.addEventListener("volumechange",function(){vm.trigger("volumechange")})},autoplay:function(autoplay){if(typeof autoplay!=="undefined"){return this.setAutoplay(autoplay)}return this.options_.autoplay},setAutoplay:function(val){return this.options_.autoplay=val},buffered:function(){if(!this.dmPlayer||!this.dmPlayer.bufferedTime){return videojs.createTimeRange()}return videojs.createTimeRange(0,this.dmPlayer.bufferedTime)},createEl:function(){var div=document.createElement("div");div.setAttribute("id",this.options_.techId);div.setAttribute("style","width:100%;height:100%;top:0;left:0;position:absolute");div.setAttribute("class","vjs-tech");var divWrapper=document.createElement("div");divWrapper.appendChild(div);if(!_isOnMobile&&!this.options_.dmControls){}return divWrapper},currentSrc:function(){return this.source&&this.source.src},currentTime:function(seconds){if(typeof seconds!=="undefined"){return this.setCurrentTime(seconds)}return this.dmPlayer&&this.dmPlayer.currentTime},setCurrentTime:function(seconds){if(!this.dmPlayer||!this.dmPlayer.seek){return}return this.dmPlayer.seek(seconds)},dispose:function(){if(DM&&DM.destroy){DM.destroy(this.options_.techId)}else{var index=Dailymotion.sdkReadyQueue.indexOf(this);if(index!==-1){Dailymotion.sdkReadyQueue.splice(index,1)}}this.dmPlayer=undefined;this.el_.parentNode.className=this.el_.parentNode.className.replace(" vjs-dailymotion","").replace(" vjs-dailymotion-mobile","");this.el_.parentNode.removeChild(this.el_);Tech.prototype.dispose.call(this)},duration:function(seconds){if(typeof seconds!=="undefined"){return this.setDuration(seconds)}return this.dmPlayer?this.dmPlayer.duration:0},setDuration:function(seconds){if(!this.dmPlayer||!this.dmPlayer.seek){return}return this.dmPlayer.seek(seconds)},ended:function(){return this.dmPlayer&&this.dmPlayer.ended},enterFullWindow:function(){if(!this.dmPlayer||!this.dmPlayer.setFullscreen){return}return this.dmPlayer.setFullscreen(true)},error:function(){return this.dmPlayer&&this.dmPlayer.error},exitFullscreen:function(){if(!this.dmPlayer||!this.dmPlayer.setFullscreen){return}return this.dmPlayer.setFullscreen(false)},isFullscreen:function(){return this.dmPlayer&&this.dmPlayer.fullscreen},language:function(){return undefined},languages:function(){return undefined},load:function(){if(!this.dmPlayer||!this.dmPlayer.load){return}return this.dmPlayer.load(this._getPlayerConfig())},loop:function(){return undefined},muted:function(muted){if(typeof muted!==undefined){return this.setMuted(muted)}return this.dmPlayer&&this.dmPlayer.mute},setMuted:function(mute){if(typeof mute==="undefined"||!this.dmPlayer||!this.dmPlayer.setMuted){return}if(mute){this.volumeBeforeMute=this.volume();this.setVolume(0)}else{this.setVolume(this.volumeBeforeMute)}this.dmPlayer.setMuted(mute)},networkState:function(){if(!this.dmPlayer||this.dmPlayer.error){return 0}if(this.dmPlayer.seeking){return 2}},pause:function(){if(!this.dmPlayer||!this.dmPlayer.pause){return}return this.dmPlayer.pause()},paused:function(){return this.dmPlayer&&this.dmPlayer.paused},play:function(){if(!this.isApiReady||!this.dmPlayer||!this.dmPlayer.play){return}this.trigger("loadStart");this.trigger("waiting");return this.dmPlayer.play()},playbackRate:function(){return 1},poster:function(){return undefined},preload:function(){return undefined},readyState:function(){if(!this.dmPlayer||this.dmPlayer.error){return 0}if(this.dmPlayer.seeking){return 1}return 4},remainingTime:function(){return this.dmPlayer&&this.dmPlayer.duration-this.dmPlayer.currentTime},requestFullscreen:function(){return this.enterFullWindow()},enterFullScreen:function(){return this.enterFullWindow()},reset:function(){this.load()},seekable:function(){if(!this.dmPlayer){return videojs.createTimeRange()}return videojs.createTimeRange(0,this.dmPlayer.duration)},seeking:function(){return this.dmPlayer&&this.dmPlayer.seeking},src:function(source){if(typeof source!=="undefined"){return this.setSrc(source)}return this.source},setSrc:function(source){if(typeof source==="undefined"){return}this.source=source;this.url=Dailymotion.parseUrl(source.src||source);if(Dailymotion.isSdkReady){this.load()}return this.source},supportsFullScreen:function(){return true},volume:function(){return this.dmPlayer?this.dmPlayer.volume:1},setVolume:function(percentAsDecimal){if(!this.dmPlayer||!this.dmPlayer.setMuted||!this.dmPlayer.setVolume){return}this.dmPlayer.setMuted(false);this.dmPlayer.setVolume(percentAsDecimal)}});Dailymotion.isSupported=function(){return true};Dailymotion.canPlaySource=function(e){return Dailymotion.canPlayType(e.type)};Dailymotion.canPlayType=function(e){return e==="video/dailymotion"};Dailymotion.parseUrl=function(url){var result={};var regex=/video\/[^?|^\/]*/;var match=url.match(regex);if(match&&match[0]){result.video=match[0].replace("video/","")}var regPlaylist=/playlist(=|\/)[^&]*/;match=url.match(regPlaylist);if(match&&match[0]){result.playlist=match[0].replace(/playlist(=|\/)/,"")}return result};function apiLoaded(){Dailymotion.isSdkReady=true;for(var i=0;i<Dailymotion.sdkReadyQueue.length;++i){Dailymotion.sdkReadyQueue[i].initDMPlayer()}}function loadScript(src,callback){var loaded=false;var tag=document.createElement("script");var firstScriptTag=document.getElementsByTagName("script")[0];if(!firstScriptTag){return}firstScriptTag.parentNode.insertBefore(tag,firstScriptTag);tag.onload=function(){if(!loaded){loaded=true;callback()}};tag.onreadystatechange=function(){if(!loaded&&(this.readyState==="complete"||this.readyState==="loaded")){loaded=true;callback()}};tag.src=src}function injectCss(){var css=".vjs-dailymotion .vjs-iframe-blocker { display: none; }"+".vjs-dailymotion.vjs-user-inactive .vjs-iframe-blocker { display: block; }"+".vjs-dailymotion .vjs-poster { background-size: cover; }"+".vjs-dailymotion-mobile .vjs-big-play-button { display: none; }";var head=document.head||document.getElementsByTagName("head")[0];var style=document.createElement("style");style.setAttribute("type","text/css");if(style.styleSheet){style.styleSheet.cssText=css}else{style.appendChild(document.createTextNode(css))}head.appendChild(style)}Dailymotion.sdkReadyQueue=[];if(typeof document!=="undefined"){loadScript("https://api.dmcdn.net/all.js",apiLoaded);injectCss()}if(typeof videojs.registerTech!=="undefined"){videojs.registerTech("Dailymotion",Dailymotion)}else{videojs.registerComponent("Dailymotion",Dailymotion)}});