2017-09-24 01:54:46 +02:00
if ( Debug . debug )
console . log ( "Loading: Resizer.js" ) ;
2018-05-12 02:51:58 +02:00
var StretchMode = {
2018-05-13 21:05:11 +02:00
NO _STRETCH : 0 ,
CONDITIONAL : 1 ,
FULL : 2
2018-05-12 02:51:58 +02:00
}
2018-01-02 03:36:29 +01:00
2018-05-08 23:35:16 +02:00
class Resizer {
2017-09-24 01:54:46 +02:00
2018-05-12 02:51:58 +02:00
constructor ( videoData ) {
this . conf = videoData ;
2018-05-08 23:35:16 +02:00
this . video = videoData . video ;
2018-05-12 02:51:58 +02:00
this . scaler = new Scaler ( ) ;
this . stretcher = new Stretcher ( ) ;
this . zoom = new Zoom ( ) ;
// load up default values
2018-05-13 15:22:28 +02:00
this . correctedVideoDimensions = { } ;
this . currentCss = { } ;
2018-05-12 02:51:58 +02:00
this . stretch = { mode : ExtensionConf . stretch . initialMode } ;
// restore watchdog. While true, applyCss() tries to re-apply new css until this value becomes false again
// value becomes false when width and height of <video> tag match with what we want to set. Only necessary when
// calling _res_restore() for some weird reason.
this . restore _wd = false ;
2018-05-13 15:22:28 +02:00
this . lastAr = { type : 'original' } ;
2017-09-24 01:54:46 +02:00
}
2018-05-13 15:22:28 +02:00
2018-05-12 02:51:58 +02:00
setAr ( ar , lastAr ) {
2018-05-08 23:35:16 +02:00
if ( Debug . debug ) {
console . log ( '[Resizer::setAr] trying to set ar. New ar:' , ar )
}
2017-09-24 01:54:46 +02:00
2018-05-12 02:51:58 +02:00
if ( lastAr ) {
this . lastAr = lastAr ;
} else {
2018-05-13 15:22:28 +02:00
if ( isNaN ( ar ) ) {
this . lastAr = { type : 'legacy' , ar : ar }
} else {
this . lastAr = { type : 'static' , ar : ar } ;
}
2018-05-12 02:51:58 +02:00
}
2018-05-01 23:09:58 +02:00
2018-05-08 23:35:16 +02:00
if ( ! this . video ) {
console . log ( "No video detected." )
2018-05-13 15:22:28 +02:00
// this.videoData.destroy();
2018-05-08 23:35:16 +02:00
}
2018-05-01 23:09:58 +02:00
2017-11-13 22:35:04 +01:00
2018-05-12 02:51:58 +02:00
var dimensions = Scaler . calculateCrop ( ar , this . video , this . conf . player . dimensions ) ;
2018-05-13 15:22:28 +02:00
var stretchFactors = undefined ;
2018-01-18 22:34:42 +01:00
2018-05-12 02:51:58 +02:00
// if we set stretching, we apply stretching
if ( this . stretch . mode == StretchMode . FULL ) {
stretchFactors = Stretcher . calculateStretch ( dimensions ) ;
} else if ( this . stretch . mode == StretchMode . CONDITIONAL ) {
stretchFactors = Stretcher . conditionalStretch ( dimensions , ExtensionConf . stretch . conditionalDifferencePercent ) ;
2018-02-04 17:39:26 +01:00
}
2018-05-02 17:52:25 +02:00
2018-05-12 02:51:58 +02:00
zoom . applyZoom ( dimensions ) ;
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
cssOffsets = this . computeOffsets ( dimensions ) ;
this . applyCss ( cssOffsets , stretchFactors ) ;
2018-05-12 02:51:58 +02:00
}
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
setStretchMode ( stretchMode ) {
this . stretch . mode = stretchMode ;
}
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
setPan ( relativeMousePosX , relativeMousePosY ) {
// relativeMousePos[X|Y] - on scale from 0 to 1, how close is the mouse to player edges.
// use these values: top, left: 0, bottom, right: 1
if ( ! this . pan ) {
this . pan = { } ;
}
2018-02-04 17:39:26 +01:00
2018-05-13 15:22:28 +02:00
this . pan . relativeOffsetX = relativeMousePosX * 2.5 - 1.25 ;
this . pan . relativeOffsetY = relativeMousePosY * 2.5 - 1.25 ;
}
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
startCssWatcher ( ) {
this . cssWatcherTimeout = setInterval ( this . cssWatcher , 200 ) ;
}
2017-09-24 01:54:46 +02:00
2018-05-13 15:22:28 +02:00
stopCssWatcher ( ) {
clearInterval ( this . cssWatcherTimeout ) ;
}
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
restore ( ) {
if ( Debug . debug ) {
console . log ( "[Resizer::restore] attempting to restore aspect ratio. this & settings:" , { 'this' : this , "settings" : Settings } ) ;
}
// this is true until we verify that css has actually been applied
this . restore _wd = true ;
if ( this . lastAr . type === 'original' ) {
this . setAr ( 'reset' ) ;
}
else {
this . setAr ( this . lastAr . ar , this . lastAr )
}
}
2018-05-02 17:52:25 +02:00
2018-05-13 15:22:28 +02:00
reset ( ) {
this . setStretchMode ( StretchMode . NO _STRETCH ) ;
this . zoom . setZoom ( 1 ) ;
this . resetPan ( ) ;
this . setAr ( 'reset' ) ;
2017-09-24 01:54:46 +02:00
}
2018-05-13 15:22:28 +02:00
resetPan ( ) {
this . pan = undefined ;
2017-09-24 01:54:46 +02:00
}
2018-05-13 15:22:28 +02:00
resetZoom ( ) {
this . zoom . setZoom ( 1 ) ;
this . restore ( ) ;
2017-09-24 01:54:46 +02:00
}
2018-05-13 15:22:28 +02:00
resetCrop ( ) {
this . setAr ( 'reset' ) ;
2017-09-24 01:54:46 +02:00
}
2018-05-13 15:22:28 +02:00
resetStretch ( ) {
this . stretch . mode = StretchMode . NO _STRETCH ;
this . restore ( ) ;
2017-09-24 01:54:46 +02:00
}
2018-01-02 03:36:29 +01:00
2018-05-13 15:22:28 +02:00
// mostly internal stuff
computeOffsets ( videoDimensions ) {
if ( Debug . debug )
console . log ( "[Resizer::_res_computeOffsets] video will be aligned to " , ExtensionConf . miscFullscreenSettings . videoFloat ) ;
2018-01-02 03:36:29 +01:00
2018-05-13 15:22:28 +02:00
var offsets = {
width : videoDimensions . width ,
height : videoDimensions . height ,
left : 0 ,
top : ( ( this . conf . player . dimensions . height - videoDimensions . height ) / 2 )
}
2018-04-22 14:35:40 +02:00
2018-05-13 15:22:28 +02:00
if ( this . pan ) {
var defaultOffset = ( this . conf . player . dimensions . height - videoDimensions . height ) / 2 ;
offsets . top = defaultOffset + ( defualtOffset * this . pan . relativeOffsetY ) ;
defaultOffset = ( this . conf . player . dimensions . height - videoDimensions . width ) / 2 ;
offsets . left = defaultOffset + ( defaultOffset * this . pan . relativeOffsetX ) ;
} else {
if ( ExtensionConf . miscFullscreenSettings . videoFloat == "center" ) {
offsets . left = ( this . conf . player . dimensions . height - videoDimensions . width ) / 2 ;
}
else if ( ExtensionConf . miscFullscreenSettings . videoFloat == "right" ) {
offsets . left = ( this . conf . player . dimensions . height - videoDimensions . width ) ;
}
}
this . correctedVideoDimensions . width = parseInt ( offsets . width ) ;
this . correctedVideoDimensions . height = parseInt ( offsets . height ) ;
this . correctedVideoDimensions . left = parseInt ( offsets . left ) ;
this . correctedVideoDimensions . top = parseInt ( offsets . top ) ;
return offsets ;
}
applyCss ( dimensions , stretchFactors ) {
if ( this . video == undefined || this . video == null ) {
2018-04-22 14:35:40 +02:00
if ( Debug . debug )
2018-05-13 15:22:28 +02:00
console . log ( "[Resizer::_res_applyCss] Video went missing, doing nothing." ) ;
2018-04-22 14:35:40 +02:00
return ;
}
2018-01-02 03:36:29 +01:00
2018-05-13 15:22:28 +02:00
if ( Debug . debug )
console . log ( "[Resizer::_res_applyCss] Starting to apply css. this is what we're getting in:" , dimensions ) ;
if ( dimensions . top !== undefined )
dimensions . top = "top: " + Math . round ( dimensions . top ) + "px !important" ;
if ( dimensions . left !== undefined )
dimensions . left = "left: " + Math . round ( dimensions . left ) + "px !important" ;
if ( dimensions . width !== undefined )
dimensions . width = "width: " + Math . round ( dimensions . width ) + "px !important" ;
if ( dimensions . height !== undefined )
dimensions . height = "height: " + Math . round ( dimensions . height ) + "px !important" ;
// misc.
dimensions . position = "position: absolute !important" ;
dimensions . margin = "margin: 0px !important" ;
// save values for left and top to
this . currentCss . top = dimensions . top ;
this . currentCss . left = dimensions . left ;
if ( Debug . debug )
console . log ( "[Resizer::_res_applyCss] trying to apply css. Css strings: " , dimensions , "video tag: " , this . video ) ;
var styleArrayStr = this . video . getAttribute ( 'style' ) ;
if ( styleArrayStr !== null && styleArrayStr !== undefined ) {
2018-01-02 03:36:29 +01:00
2018-05-13 15:22:28 +02:00
var styleArray = styleArrayStr . split ( ";" ) ;
for ( var i in styleArray ) {
styleArray [ i ] = styleArray [ i ] . trim ( ) ;
if ( styleArray [ i ] . startsWith ( "top:" ) ) {
styleArray [ i ] = dimensions . top ;
delete dimensions . top ;
}
else if ( styleArray [ i ] . startsWith ( "left:" ) ) {
styleArray [ i ] = dimensions . left ;
delete dimensions . left ;
}
else if ( styleArray [ i ] . startsWith ( "width:" ) ) {
styleArray [ i ] = dimensions . width ;
delete dimensions . width ;
}
else if ( styleArray [ i ] . startsWith ( "height:" ) ) {
styleArray [ i ] = dimensions . height ;
delete dimensions . height ;
}
else if ( styleArray [ i ] . startsWith ( "position:" ) ) {
styleArray [ i ] = dimensions . position ;
delete dimensions . position ;
}
else if ( styleArray [ i ] . startsWith ( "margin:" ) ) {
styleArray [ i ] = dimensions . margin ;
delete dimensions . margin ;
}
else if ( styleArray [ i ] . startsWith ( "transform:" ) ) {
if ( stretchFactors ) {
styleArray [ i ] = ` scale( ${ stretchFactors . x } , ${ stretchFactors . y } ` ;
stretchFactors = undefined ;
}
}
2018-01-02 03:36:29 +01:00
}
}
else {
2018-05-13 15:22:28 +02:00
var styleArray = [ ] ;
}
// add remaining elements
for ( var key in dimensions )
styleArray . push ( dimensions [ key ] ) ;
if ( stretchFactors ) {
styleArray . push ( ` scale( ${ stretchFactors . x } , ${ stretchFactors . y } ` ) ;
2018-01-02 03:36:29 +01:00
}
2018-04-22 14:35:40 +02:00
2018-05-13 15:22:28 +02:00
// build style string back
var styleString = "" ;
for ( var i in styleArray )
if ( styleArray [ i ] !== undefined && styleArray [ i ] !== "" )
styleString += styleArray [ i ] + "; " ;
this . setStyleString ( styleString ) ;
2018-04-22 14:35:40 +02:00
}
2018-05-13 15:22:28 +02:00
setStyleString ( styleString , count = 0 ) {
vid . setAttribute ( "style" , styleString ) ;
2017-11-13 22:35:04 +01:00
2018-05-13 15:22:28 +02:00
if ( this . restore _wd ) {
if ( this . video == undefined || this . video == null ) {
if ( Debug . debug )
console . log ( "[Resizer::_res_setStyleString] Video element went missing, nothing to do here." )
return ;
2017-12-02 21:09:08 +01:00
}
2018-05-13 15:22:28 +02:00
if (
styleString . indexOf ( "width: " + this . video . style . width ) == - 1 ||
styleString . indexOf ( "height: " + this . video . style . height ) == - 1 ) {
// css ni nastavljen?
// css not set?
if ( Debug . debug )
console . log ( "[Resizer::_res_setStyleString] Style string not set ???" ) ;
if ( count < ExtensionConf . resizer . setStyleString . maxRetries ) {
setTimeout ( this . setStyleString , ExtensionConf . resizer . setStyleString . retryTimeout , count + 1 ) ;
}
else if ( Debug . debug ) {
console . log ( "[Resizer::_res_setStyleString] we give up. css string won't be set" ) ;
}
2018-01-20 22:59:31 +01:00
}
2018-05-13 15:22:28 +02:00
else {
this . restore _wd = false ;
2017-12-02 21:09:08 +01:00
}
2017-11-13 22:35:04 +01:00
}
2018-05-13 15:22:28 +02:00
else {
if ( Debug . debug )
console . log ( "[Resizer::_res_setStyleString] css applied. Style string:" , styleString ) ;
}
2017-11-13 22:35:04 +01:00
}
2018-04-25 20:39:52 +02:00
2018-05-13 15:22:28 +02:00
cssWatcher ( ) {
2017-11-13 22:35:04 +01:00
2018-05-13 15:22:28 +02:00
// this means we haven't set our CSS yet, or that we changed video.
if ( ! this . currentCss . top )
return ;
// this means video went missing.
if ( ! this . video )
return ;
// // our current css is fucky? Null, undefined and 0 are invalid values.
// if(! GlobalVars.currentCss.width || ! GlobalVars.currentCss.height )
// return;
2018-02-05 22:46:38 +01:00
2018-05-13 15:22:28 +02:00
var styleArrayStr = this . video . getAttribute ( 'style' ) ;
2018-02-05 22:46:38 +01:00
2018-05-13 15:22:28 +02:00
if ( styleArrayStr ) {
var styleArray = styleArrayStr . split ( ";" ) ;
var stuffChecked = 0 ;
var stuffToCheck = 2 ;
2018-02-05 22:46:38 +01:00
2018-05-13 15:22:28 +02:00
for ( var i in styleArray ) {
styleArray [ i ] = styleArray [ i ] . trim ( ) ;
if ( styleArray [ i ] . startsWith ( "top:" ) ) {
// don't force css restore if currentCss.top is not defined
if ( this . currentCss . top && styleArray [ i ] != this . currentCss . top ) {
if ( Debug . debug ) {
console . log ( "[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)" ) ;
console . log ( "[Resizer::_res_antiCssOverride] MA SPAGHETT: top:" , this . currentCss . top , "left:" , this . currentCss . left , "thing that touched ma spaghett" , styleArrayStr ) ;
}
_res _restore ( ) ;
return ;
2018-02-05 22:46:38 +01:00
}
2018-05-13 15:22:28 +02:00
stuffChecked ++ ;
2018-02-05 22:46:38 +01:00
}
2018-05-13 15:22:28 +02:00
else if ( styleArray [ i ] . startsWith ( "left:" ) ) {
// don't force css restore if currentCss.left is not defined
if ( this . currentCss . left && styleArray [ i ] != this . currentCss . left ) {
if ( Debug . debug ) {
console . log ( "[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)" ) ;
console . log ( "[Resizer::_res_antiCssOverride] MA SPAGHETT: width:" , this . currentCss . width , "height:" , this . currentCss . height , "thing that touched ma spaghett" , styleArrayStr ) ;
}
_res _restore ( ) ;
return ;
2018-02-05 22:46:38 +01:00
}
2018-05-13 15:22:28 +02:00
stuffChecked ++ ;
}
if ( stuffChecked == stuffToCheck ) {
// if(Debug.debug){
// console.log("[Resizer::_res_antiCssOverride] My spaghett rests untouched. (nobody overrode our CSS, doing nothing)");
// }
2018-02-05 22:46:38 +01:00
return ;
}
}
}
}
}