2017-09-24 01:54:46 +02:00
if ( Debug . debug )
console . log ( "Loading: Resizer.js" ) ;
2018-01-02 03:36:29 +01:00
// restore watchdog. While true, _res_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.
var _res _restore _wd = false ;
2017-09-24 01:54:46 +02:00
var _res _char = function ( newAr , video , player ) {
// Kot vhodni argument dobimo razmerje stranic. Problem je, ker pri nekaterih ločljivostih lahko razmerje stranic
// videa/našega zaslona minimalno odstopa od idealnega razmerja — npr 2560x1080 ni natanko 21:9, 1920x1080 ni
// natanko 16:9. Zato ob podanem razmerju stranic izračunamo dejansko razmerje stranic.
//
// The aspect ratio we get as an argument is an ideal aspect ratio. Some (most) resolutions' aspect ratios differ
// from that ideal aspect ratio (by a minimal amount) — e.g. 2560x1080 isn't exactly 21:9, 1920x1080 isn't exactly
// 16:9. What is more, both 3440x1440 and 2560x1080 are considered "21:9", but their aspect ratios are slightly
// different. This has the potential to result in annoying black bars, so we correct the aspect ratio we're given
// to something that's slightly more correct.
var ar ;
var res _219 = [ [ 2560 , 1080 ] , [ 3440 , 1440 ] ] ;
var res _169 = [ [ 1920 , 1080 ] , [ 1280 , 720 ] , [ 1366 , 768 ] ] ;
if ( newAr == ( 21 / 9 ) ) {
for ( var i = 0 ; i < res _219 . length ; i ++ ) {
if ( player . height == res _219 [ i ] [ 1 ] ) {
ar = res _219 [ i ] [ 0 ] / res _219 [ i ] [ 1 ] ;
set _video _ar ( ar , video , player ) ;
return ;
}
}
}
else if ( new _ar == ( 16 / 9 ) ) {
for ( var i = 0 ; i < res _169 . length ; i ++ ) {
if ( player . height == res _169 [ i ] [ 1 ] ) {
ar = res _169 [ i ] [ 0 ] / res _169 [ i ] [ 1 ] ;
setVideoAr ( ar , video , player ) ;
return ;
}
}
}
_res _setVideoAr ( new _ar , video , player ) ;
}
2018-01-27 22:25:25 +01:00
// Skrbi za "stare" možnosti, kot na primer "na širino zaslona", "na višino zaslona" in "ponastavi".
// Približevanje opuščeno.
2017-10-17 22:17:51 +02:00
// handles "legacy" options, such as 'fit to widht', 'fit to height' and 'reset'. No zoom tho
var _res _legacyAr = function ( action ) {
2018-01-20 22:59:31 +01:00
var vid = GlobalVars . video ;
2018-01-27 20:27:11 +01:00
var ar ;
if ( GlobalVars . playerDimensions === null || GlobalVars . playerDimensions === undefined ) {
ar = screen . width / screen . height ;
}
else {
ar = GlobalVars . playerDimensions . width / GlobalVars . playerDimensions . height ;
}
2018-02-12 23:28:31 +01:00
// POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
//
// IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// setting a static aspect ratio (even if the function is called from here or ArDetect).
2017-09-24 01:54:46 +02:00
2018-02-12 23:28:31 +01:00
var fileAr = vid . videoWidth / vid . videoHeight ;
if ( action == "fitw" ) {
2018-01-20 22:59:31 +01:00
_res _setAr ( ar > fileAr ? ar : fileAr ) ;
2017-09-24 01:54:46 +02:00
}
2018-02-12 23:28:31 +01:00
else if ( action == "fith" ) {
2018-01-20 22:59:31 +01:00
_res _setAr ( ar < fileAr ? ar : fileAr ) ;
2017-10-17 22:17:51 +02:00
}
2018-02-12 23:28:31 +01:00
else if ( action == "reset" ) {
2018-01-27 22:25:25 +01:00
_res _setAr ( fileAr ) ;
2018-02-12 23:28:31 +01:00
GlobalVars . lastAr = { type : "original" } ;
2017-10-17 22:17:51 +02:00
return ;
2017-09-24 01:54:46 +02:00
}
2018-02-12 23:28:31 +01:00
else if ( action == "autoar" || action == "auto" ) {
GlobalVars . lastAr = { type : "auto" , ar : null } ;
2017-12-30 12:55:58 +01:00
ArDetect . init ( ) ;
}
2018-02-12 23:28:31 +01:00
GlobalVars . lastAr = { type : "legacy" , action : action } ;
2017-11-13 22:35:04 +01:00
}
2017-10-02 00:27:01 +02:00
var _res _setAr = function ( ar , playerDimensions ) {
2018-01-18 22:34:42 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_setAr] trying to set ar. args are: ar->" , ar , "; playerDimensions->" , playerDimensions ) ;
2018-01-27 22:25:25 +01:00
GlobalVars . lastAr = { type : "static" , ar : ar } ;
2018-01-18 22:34:42 +01:00
2018-01-20 22:59:31 +01:00
var vid = GlobalVars . video ;
2018-02-04 17:39:26 +01:00
if ( vid == null || vid == undefined || vid . videoWidth == 0 || vid . videoHeight == 0 ) {
vid = document . getElementsByTagName ( "video" ) [ 0 ] ;
GlobalVars . video = vid ;
if ( vid == null || vid == undefined || vid . videoWidth == 0 || vid . videoHeight == 0 ) {
if ( Debug . debug )
2018-02-12 23:28:31 +01:00
console . log ( "[Resizer::_res_setAr] I lied. Tricked you! You thought there is a video, didn't you? Never would be." , vid ) ; // of course that's thorin reference -> https://youtu.be/OY5gGkeQn1c?t=1m20s
2018-02-04 17:39:26 +01:00
return ;
}
}
2017-09-24 01:54:46 +02:00
2018-02-04 17:39:26 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_setAr] video:" , vid , "width:" , vid . videoWidth , "height:" , vid . videoHeight ) ;
2018-01-18 22:34:42 +01:00
2017-09-24 01:54:46 +02:00
// Dejansko razmerje stranic datoteke/<video> značke
// Actual aspect ratio of the file/<video> tag
var fileAr = vid . videoWidth / vid . videoHeight ;
2017-12-29 23:34:40 +01:00
if ( ar == "default" )
ar = fileAr ;
2018-02-04 17:39:26 +01:00
2017-09-24 01:54:46 +02:00
2017-10-17 22:17:51 +02:00
if ( Debug . debug )
2018-02-04 17:39:26 +01:00
console . log ( "[Resizer::_res_setAr] ar is " , ar , ", file ar is" , fileAr , ", playerDimensions are " , playerDimensions ) ;
2017-10-17 22:17:51 +02:00
2017-09-24 01:54:46 +02:00
var videoDimensions = {
width : 0 ,
height : 0
}
2017-10-02 00:27:01 +02:00
2018-01-20 22:59:31 +01:00
if ( playerDimensions === undefined ) {
2018-01-18 00:11:03 +01:00
playerDimensions = PlayerDetect . getPlayerDimensions ( vid ) ;
2018-01-20 22:59:31 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_setAr] playerDimensions are undefined, trying to determine new ones ... new dimensions:" , playerDimensions ) ;
}
2018-01-18 00:11:03 +01:00
if ( Debug . debug ) {
console . log ( "[Resizer::_res_setAr] Player dimensions?" , playerDimensions ) ;
2017-09-24 01:54:46 +02:00
}
if ( fileAr < ar ) {
// imamo letterbox zgoraj in spodaj -> spremenimo velikost videa (ampak nikoli na več, kot je širina zaslona)
// letterbox -> change video size (but never to wider than monitor width)
2017-10-02 00:27:01 +02:00
videoDimensions . width = Math . min ( playerDimensions . height * ar , playerDimensions . width ) ;
2017-09-24 01:54:46 +02:00
videoDimensions . height = videoDimensions . width * ( 1 / fileAr ) ;
}
else {
2017-10-02 00:27:01 +02:00
videoDimensions . height = Math . min ( playerDimensions . width * ( 1 / ar ) , playerDimensions . height ) ;
2017-09-27 02:26:47 +02:00
videoDimensions . width = videoDimensions . height * fileAr ;
}
if ( Debug . debug ) {
2018-02-04 17:39:26 +01:00
console . log ( "[Resizer::_res_setAr] Video dimensions: " , videoDimensions , "playerDimensions:" , playerDimensions ) ;
2017-09-24 01:54:46 +02:00
}
var cssValues = _res _computeOffsets ( videoDimensions , playerDimensions ) ;
2017-09-27 02:26:47 +02:00
if ( Debug . debug ) {
2018-02-04 17:39:26 +01:00
console . log ( "[Resizer::_res_setAr] Offsets for css are: " , cssValues ) ;
2017-09-27 02:26:47 +02:00
}
2017-09-24 01:54:46 +02:00
_res _applyCss ( cssValues ) ;
}
var _res _computeOffsets = function ( vidDim , playerDim ) {
2017-12-29 23:34:40 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_computeOffsets] video will be aligned to " , Settings . miscFullscreenSettings . videoFloat ) ;
2017-09-24 01:54:46 +02:00
var offsets = {
width : vidDim . width ,
height : vidDim . height ,
left : 0 ,
top : ( ( playerDim . height - vidDim . height ) / 2 )
}
if ( Settings . miscFullscreenSettings . videoFloat == "center" ) {
offsets . left = ( playerDim . width - vidDim . width ) / 2 ;
}
else if ( Settings . miscFullscreenSettings . videoFloat == "right" ) {
offsets . left = ( playerDim . width - vidDim . width ) ;
}
return offsets ;
}
2017-12-29 23:34:40 +01:00
var _res _align = function ( float ) {
if ( ! float )
float = Settings . miscFullscreenSettings . videoFloat ;
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
var dimensions = { left : 0 } ;
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
if ( float == "left" ) {
_res _applyCss ( dimensions ) ;
return ;
2017-09-24 01:54:46 +02:00
}
2017-12-29 23:34:40 +01:00
if ( float == "center" ) {
// dimensions.left =
// _res_applyCss(
2017-09-24 01:54:46 +02:00
}
}
2018-01-02 03:36:29 +01:00
var _res _setStyleString _maxRetries = 3 ;
var _res _setStyleString = function ( vid , styleString , count ) {
vid . setAttribute ( "style" , styleString ) ;
if ( _res _restore _wd ) {
2018-01-20 22:59:31 +01:00
var vid2 = GlobalVars . video ;
2018-01-02 03:36:29 +01:00
if (
styleString . indexOf ( "width: " + vid2 . style . width ) == - 1 ||
styleString . indexOf ( "height: " + vid2 . style . height ) == - 1 ) {
// css ni nastavljen?
// css not set?
if ( Debug . debug )
console . log ( "[Resizer::_res_setStyleString] Style string not set ???" ) ;
if ( count ++ < _res _setStyleString _maxRetries ) {
setTimeout ( _res _setStyleString , 200 , count ) ;
}
else if ( Debug . debug ) {
console . log ( "[Resizer::_res_setStyleString] we give up. css string won't be set" ) ;
}
}
else {
_res _restore _wd = false ;
}
}
else {
if ( Debug . debug )
console . log ( "[Resizer::_res_setStyleString] css applied. Style string:" , styleString ) ;
}
}
2017-12-29 23:34:40 +01:00
function _res _applyCss ( dimensions ) {
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_applyCss] Starting to apply css. this is what we're getting in:" , dimensions ) ;
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
if ( dimensions . top !== undefined )
dimensions . top = "top: " + Math . round ( dimensions . top ) + "px !important" ;
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
if ( dimensions . left !== undefined )
dimensions . left = "left: " + Math . round ( dimensions . left ) + "px !important" ;
2017-09-24 01:54:46 +02:00
2017-12-29 23:34:40 +01:00
if ( dimensions . width !== undefined )
dimensions . width = "width: " + Math . round ( dimensions . width ) + "px !important" ;
2017-11-13 22:35:04 +01:00
2017-12-29 23:34:40 +01:00
if ( dimensions . height !== undefined )
dimensions . height = "height: " + Math . round ( dimensions . height ) + "px !important" ;
2017-12-02 21:09:08 +01:00
// misc.
dimensions . position = "position: absolute !important" ;
2018-01-12 22:53:07 +01:00
dimensions . margin = "margin: 0px !important" ;
2017-11-13 22:35:04 +01:00
2018-02-05 22:46:38 +01:00
// save values for left and top to GlobalVars
GlobalVars . currentCss . top = dimensions . top ;
GlobalVars . currentCss . left = dimensions . left ;
2018-01-20 22:59:31 +01:00
var vid = GlobalVars . video ;
2017-12-29 23:34:40 +01:00
if ( Debug . debug )
console . log ( "[Resizer::_res_applyCss] trying to apply css. Css strings: " , dimensions , "video tag: " , vid ) ;
2017-12-02 21:09:08 +01:00
var styleArrayStr = vid . getAttribute ( 'style' ) ;
2017-11-13 22:35:04 +01:00
2017-12-02 21:09:08 +01:00
if ( styleArrayStr !== null && styleArrayStr !== undefined ) {
2017-11-13 22:35:04 +01:00
2017-12-29 23:34:40 +01:00
var styleArray = styleArrayStr . split ( ";" ) ;
2017-12-02 21:09:08 +01:00
for ( var i in styleArray ) {
styleArray [ i ] = styleArray [ i ] . trim ( ) ;
2017-12-29 23:34:40 +01:00
if ( styleArray [ i ] . startsWith ( "top:" ) ) {
2017-12-02 21:09:08 +01:00
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 ;
2018-01-20 22:59:31 +01:00
delete dimensions . position ;
}
else if ( styleArray [ i ] . startsWith ( "margin:" ) ) {
styleArray [ i ] = dimensions . margin ;
delete dimensions . margin ;
2017-12-02 21:09:08 +01:00
}
2017-11-13 22:35:04 +01:00
}
}
2017-12-02 21:09:08 +01:00
else {
var styleArray = [ ] ;
}
2017-09-24 01:54:46 +02:00
2017-11-13 22:35:04 +01:00
// add remaining elements
for ( var key in dimensions )
styleArray . push ( dimensions [ key ] ) ;
// build style string back
var styleString = "" ;
for ( var i in styleArray )
if ( styleArray [ i ] !== undefined && styleArray [ i ] !== "" )
styleString += styleArray [ i ] + "; " ;
2018-01-02 03:36:29 +01:00
_res _setStyleString ( vid , styleString ) ;
2017-12-29 23:34:40 +01:00
}
2018-02-05 22:46:38 +01:00
var _res _antiCssOverride = function ( ) {
// this means we haven't set our CSS yet, or that we changed video.
if ( GlobalVars . currentCss . top === null )
return ;
var styleArrayStr = GlobalVars . video . getAttribute ( 'style' ) ;
if ( styleArrayStr !== null && styleArrayStr !== undefined ) {
var styleArray = styleArrayStr . split ( ";" ) ;
var stuffChecked = 0 ;
var stuffToCheck = 2 ;
for ( var i in styleArray ) {
styleArray [ i ] = styleArray [ i ] . trim ( ) ;
if ( styleArray [ i ] . startsWith ( "top:" ) ) {
if ( styleArray [ i ] != GlobalVars . currentCss . top ) {
if ( Debug . debug ) {
console . log ( "[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)" ) ;
}
2018-02-12 23:28:31 +01:00
_res _restore ( ) ;
2018-02-05 22:46:38 +01:00
return ;
}
stuffChecked ++ ;
}
else if ( styleArray [ i ] . startsWith ( "left:" ) ) {
if ( styleArray [ i ] != GlobalVars . currentCss . left ) {
if ( Debug . debug ) {
console . log ( "[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)" ) ;
}
2018-02-12 23:28:31 +01:00
_res _restore ( ) ;
2018-02-05 22:46:38 +01:00
return ;
}
stuffChecked ++ ;
}
2018-02-06 00:36:48 +01:00
if ( stuffChecked == stuffToCheck ) {
2018-02-12 23:28:31 +01:00
// 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 ;
}
}
}
}
2018-01-02 03:36:29 +01:00
2017-12-29 23:34:40 +01:00
var _res _restore = function ( ) {
if ( Debug . debug ) {
console . log ( "[Resizer::_res_restore] attempting to restore aspect ratio. this & settings:" , { 'this' : this , "settings" : Settings } ) ;
}
2018-01-02 03:36:29 +01:00
// this is true until we verify that css has actually been applied
_res _restore _wd = true ;
2018-01-27 22:25:25 +01:00
if ( GlobalVars . lastAr . type == "legacy" ) {
_res _legacyAr ( GlobalVars . lastAr . action ) ;
}
else if ( GlobalVars . lastAr . type == "static" ) {
_res _setAr ( GlobalVars . lastAr . ar ) ;
}
else if ( GlobalVars . lastAr . type == "original" ) {
_res _legacyAr ( "reset" ) ;
}
2018-02-12 23:28:31 +01:00
else if ( GlobalVars . lastAr . type == "auto" ) {
// do same as static, except keep lastAr to 'auto'. If we're here, this means video probably changed
// and there's something broken that prevents AR from working properly
// var storeLastAr = {type: GlobalVars.lastAr.type, ar: GlobalVars.lastAr.ar};
// _res_setAr(GlobalVars.lastAr.ar);
// GlobalVars.lastAr = storeLastAr;
ArDetect . init ( ) ;
}
2018-01-27 22:25:25 +01:00
}
var _res _reset = function ( ) {
_res _legacyAr ( "reset" ) ;
2017-09-24 01:54:46 +02:00
}
var Resizer = {
2017-12-29 23:34:40 +01:00
_currentAr : - 1 ,
align : _res _align ,
2018-01-18 22:34:42 +01:00
setAr : _res _setAr ,
2017-11-13 22:35:04 +01:00
legacyAr : _res _legacyAr ,
2017-12-29 23:34:40 +01:00
reset : _res _reset ,
2018-02-05 22:46:38 +01:00
restore : _res _restore ,
antiCssOverride : _res _antiCssOverride
2017-09-24 01:54:46 +02:00
}