1 line
No EOL
11 KiB
JavaScript
1 line
No EOL
11 KiB
JavaScript
import skinManager from"./../skinmanager.js";function mode(array){if(!array.length)return null;var modeMap={};let maxEl=array[0],maxCount=1;for(let i=0;i<array.length;i++){var el=array[i];let newValue;null==modeMap[el]?modeMap[el]=newValue=1:(newValue=modeMap[el]+1,modeMap[el]=newValue),newValue>maxCount&&(maxEl=el,maxCount=newValue)}return maxEl}function normalizeAspect(result){return 1.555555<=result?16/9:1.133333<=result?4/3:.8333333<=result?1:2/3}function getPrimaryImageAspectRatio(items,options){var values=[],showProgramImage=options?.showCurrentProgramImage;for(let i=0,length=items.length;i<length;i++){var item=items[i];let imageItem;item=(imageItem=showProgramImage?item.CurrentProgram||item:item.ProgramInfo||item).PrimaryImageAspectRatio||0;item&&(values[values.length]=normalizeAspect(item))}return values.length?mode(values):null}function getDefaultShape(items){items=items[0];if(items)switch(items.Type){case"Movie":case"SeriesTimer":case"Timer":return"portrait";case"Episode":case"Program":case"Video":return"backdrop"}return"square"}function getShapeFromAspect(primaryImageAspectRatio){return primaryImageAspectRatio?3<=primaryImageAspectRatio?"banner":1.4<=primaryImageAspectRatio?"backdrop":1.2<primaryImageAspectRatio?"fourThree":.73<primaryImageAspectRatio?"square":"portrait":null}function getShape(items,options){let shape=null;var primaryImageAspectRatio=getPrimaryImageAspectRatio(items,options);return shape=(shape=getShapeFromAspect(primaryImageAspectRatio))||options.defaultShape||getDefaultShape(items)}function getAspectFromShape(shape,options){switch(shape){case"portrait":return{aspect:2/3,aspectCss:"2/3"};case"backdrop":return{aspect:16/9,aspectCss:"16/9"};case"square":return{aspect:1,aspectCss:"1"};case"fourThree":return{aspect:4/3,aspectCss:"4/3"};case"banner":return options.sideFooter?{aspect:1,aspectCss:"1"}:{aspect:1e3/185,aspectCss:"1000/185"};default:return console.log("Unrecognized shape: "+shape+"--"+(new Error).stack),{}}}function getImageUrl(item,apiClient,options,shape){var width=options.width,adjustForPixelRatio=options.adjustForPixelRatio;let imgUrl,imageType;if(item.ImageUrl)return imgUrl=item.ImageUrl,options.addImageSizeToUrl&&options.width&&(imgUrl+="&maxWidth="+width),{imgUrl:imgUrl,aspect:item.PrimaryImageAspectRatio};let height=null;var primaryImageAspectRatio=item.PrimaryImageAspectRatio;let forceName=!1,imageAspect;var imageTags=item.ImageTags,uiAspect=options.uiAspect,keepAnimation=options.keepImageAnimation||null,preferThumb=options.preferThumb;return preferThumb&&imageTags&&imageTags.Thumb?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Thumb",maxWidth:width,tag:imageTags.Thumb,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):(options.preferBanner||"banner"===shape)&&imageTags&&imageTags.Banner?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Banner",maxWidth:width,tag:imageTags.Banner,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=1e3/185):options.preferDisc&&imageTags&&imageTags.Disc?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Disc",maxWidth:width,tag:imageTags.Disc,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=1):options.preferLogo&&imageTags&&imageTags.Logo?(imageType="Logo",imgUrl=apiClient.getImageUrl(item.Id,{type:imageType,maxWidth:width,tag:imageTags.Logo,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):options.preferLogo&&item.ParentLogoImageTag&&item.ParentLogoItemId?(imageType="Logo",imgUrl=apiClient.getImageUrl(item.ParentLogoItemId,{type:imageType,maxWidth:width,tag:item.ParentLogoImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):options.showChannelLogo&&item.ChannelPrimaryImageTag&&item.ChannelId?(imgUrl=apiClient.getImageUrl(item.ChannelId,{type:"Primary",maxWidth:width,tag:item.ChannelPrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):preferThumb&&item.ParentThumbItemId&&!1!==options.inheritThumb&&"Photo"!==item.MediaType?(imgUrl=apiClient.getImageUrl(item.ParentThumbItemId,{type:"Thumb",maxWidth:width,tag:item.ParentThumbImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):preferThumb&&item.BackdropImageTags&&item.BackdropImageTags.length?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Backdrop",maxWidth:width,tag:item.BackdropImageTags[0],adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),forceName=!0,imageAspect=16/9):preferThumb&&item.ParentBackdropImageTags&&item.ParentBackdropImageTags.length&&!1!==options.inheritThumb&&"Episode"===item.Type?(imgUrl=apiClient.getImageUrl(item.ParentBackdropItemId,{type:"Backdrop",maxWidth:width,tag:item.ParentBackdropImageTags[0],adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),preferThumb&&(forceName=!0),imageAspect=16/9):item.SeriesPrimaryImageTag&&options.preferSeriesImage?(imgUrl=apiClient.getImageUrl(item.SeriesId,{type:"Primary",maxWidth:width,tag:item.SeriesPrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=2/3):imageTags&&imageTags.Primary?(options.ignoreUIAspect||(height=width&&uiAspect?Math.round(width/uiAspect):null),"TvChannel"===item.Type||"ChannelManagementInfo"===item.Type?imgUrl=apiClient.getLogoImageUrl(item,{maxHeight:height,maxWidth:width,blur:options.blur,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation},skinManager.getPreferredLogoImageTypes()):(imgUrl=apiClient.getImageUrl(item.Id,{type:"Primary",maxHeight:height,maxWidth:width,tag:imageTags.Primary,blur:options.blur,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),preferThumb&&(forceName=!0)),imageAspect=primaryImageAspectRatio):options.backdropAsSecondary&&item.ParentBackdropImageTags&&item.ParentBackdropImageTags.length?(imgUrl=apiClient.getImageUrl(item.ParentBackdropItemId,{type:"Backdrop",maxWidth:width,tag:item.ParentBackdropImageTags[0],adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),options.preferBackdrop&&(forceName=!0),imageAspect=16/9):item.ImageTag&&"Plugin"===item.Type?(imgUrl=apiClient.getUrl("Plugins/"+item.Id+"/Thumb",{maxHeight:height,maxWidth:width,tag:item.ImageTag,keepAnimation:keepAnimation}),imageAspect=16/9):item.ImageTag&&null!=item.ChapterIndex?(imgUrl=apiClient.getImageUrl(item.ItemId||item.Id,{maxWidth:width,tag:item.ImageTag,type:"Chapter",index:item.ChapterIndex,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation,mediaSourceId:item.MediaSourceId}),imageAspect=primaryImageAspectRatio):item.PrimaryImageTag?(options.ignoreUIAspect||(height=width&&uiAspect?Math.round(width/uiAspect):null),"User"===item.Type?imgUrl=apiClient.getUserImageUrl(item.Id,{maxHeight:height,maxWidth:width,tag:item.PrimaryImageTag,type:"Primary",adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}):(imgUrl=apiClient.getImageUrl(item.PrimaryImageItemId||item.Id||item.ItemId,{type:"Primary",maxHeight:height,maxWidth:width,tag:item.PrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),preferThumb&&(forceName=!0)),imageAspect=primaryImageAspectRatio):item.AlbumId&&item.AlbumPrimaryImageTag?(options.ignoreUIAspect||(height=width&&uiAspect?Math.round(width/uiAspect):null),imgUrl=apiClient.getImageUrl(item.AlbumId,{type:"Primary",maxHeight:height,maxWidth:width,tag:item.AlbumPrimaryImageTag,blur:options.blur,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=1):item.ParentThumbItemId&&!1!==options.inheritThumb&&uiAspect&&1.4<=uiAspect&&"Photo"!==item.MediaType?(imgUrl=apiClient.getImageUrl(item.ParentThumbItemId,{type:"Thumb",maxWidth:width,tag:item.ParentThumbImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.ParentPrimaryImageTag?(imgUrl=apiClient.getImageUrl(item.ParentPrimaryImageItemId,{type:"Primary",maxWidth:width,tag:item.ParentPrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect="Episode"===item.Type?2/3:1):item.SeriesPrimaryImageTag?(imgUrl=apiClient.getImageUrl(item.SeriesId,{type:"Primary",maxWidth:width,tag:item.SeriesPrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=2/3):"Season"===item.Type&&imageTags&&imageTags.Thumb?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Thumb",maxWidth:width,tag:imageTags.Thumb,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.ChannelPrimaryImageTag&&item.ChannelId?(imgUrl=apiClient.getImageUrl(item.ChannelId,{type:"Primary",maxWidth:width,tag:item.ChannelPrimaryImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.BackdropImageTags&&item.BackdropImageTags.length?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Backdrop",maxWidth:width,tag:item.BackdropImageTags[0],adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):imageTags&&imageTags.Thumb?(imgUrl=apiClient.getImageUrl(item.Id,{type:"Thumb",maxWidth:width,tag:imageTags.Thumb,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.ParentThumbItemId&&!1!==options.inheritThumb?(imgUrl=apiClient.getImageUrl(item.ParentThumbItemId,{type:"Thumb",maxWidth:width,tag:item.ParentThumbImageTag,adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.ParentBackdropImageTags&&item.ParentBackdropImageTags.length&&!1!==options.inheritThumb?(imgUrl=apiClient.getImageUrl(item.ParentBackdropItemId,{type:"Backdrop",maxWidth:width,tag:item.ParentBackdropImageTags[0],adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=16/9):item.PrimaryImageItemId?(imgUrl=apiClient.getImageUrl(item.PrimaryImageItemId,{type:"Primary",maxHeight:height,maxWidth:width,adjustForPixelRatio:adjustForPixelRatio}),imageAspect=primaryImageAspectRatio):item.UserPrimaryImageTag&&"ActiveSession"!==item.Type&&(imgUrl=apiClient.getUserImageUrl(item.UserId,{maxHeight:height,maxWidth:width,tag:item.UserPrimaryImageTag,type:"Primary",adjustForPixelRatio:adjustForPixelRatio,keepAnimation:keepAnimation}),imageAspect=1),{imgUrl:imgUrl,forceName:forceName,aspect:imageAspect,imageType:imageType}}let coverImageTolerance=.28;function getCoveredImageFit(item,apiClient,imgUrlInfo,uiAspect,force){var imageType=imgUrlInfo.imageType;if("Logo"===imageType)return"contain";imageType=force||Math.abs(imgUrlInfo.aspect-uiAspect)<=coverImageTolerance;if(imageType)switch(item.Type){case"Chapter":return null;case"TvChannel":case"ChannelManagementInfo":return"contain";case"Movie":case"Series":case"Episode":case"Season":case"BoxSet":case"MusicAlbum":return apiClient&&!1!==apiClient.hasImageEnhancers?"fill":"cover";default:return"cover"}return null}function getCoveredImageClass(item,apiClient,imgUrlInfo,uiAspect,force){item=getCoveredImageFit(item,apiClient,imgUrlInfo,uiAspect,force);if(item)switch(item){case"contain":return" coveredImage coveredImage-contain";case"fill":return" coveredImage";default:return" coveredImage coveredImage-noScale"}return null}export default{getPrimaryImageAspectRatio:getPrimaryImageAspectRatio,getShape:getShape,getAspectFromShape:getAspectFromShape,getShapeFromAspect:getShapeFromAspect,getImageUrl:getImageUrl,getCoveredImageFit:getCoveredImageFit,getCoveredImageClass:getCoveredImageClass}; |