import { AfterViewInit, Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; import { Subscription } from 'rxjs/Subscription' import { HttpClient } from '@angular/common/http' import * as OpenSeadragon from 'openseadragon'; // import * as krpano from '../viewers/krpano.js'; // Internal Dependencies import { Asset } from "../asset.interface" // Browser API delcarations declare var ActiveXObject: any declare var embedpano: any enum viewerState { inactive, loading, playing, recording } @Component({ selector: 'artstor-viewer', providers: [], template: "
\"{{
", styles: ["/*20px: 20px; 90vh: 90vh; 600px: 600px; 50vh: 50vh; #f1f8f1: #f1f8f1; // Breakpoints $xsphone: 320px; $phone: 576px; $tablet: 768px; $desktop: 992px;*/ .hidden { display: none; pointer-events: none; } .mwEmbedKalturaIframe { min-height: 600px !important; } .asset-viewer { text-align: center; padding: 0; z-index: 0; position: relative; min-height: 600px; min-height: 90vh; overflow: hidden; /*@media(max-width: $phone) { height: 50vh; min-height: 50vh; .asset-viewer__buttons { opacity: 1; } } &:hover { .asset-viewer__buttons { opacity: 1; } }*/ } .asset-viewer__buttons { opacity: 0; transition: opacity 0.2s linear; position: absolute; top: 20px; left: 20px; z-index: 1000; padding: 10px; /*@media(max-width: $phone) { top: calc(100% - 40px); left: 50%; transform: translateX(-50%); }*/ } .video-player { min-height: 600px; min-height: 90vh; /*@media(max-width: $phone) { height: 50vh; min-height: 50vh; } & iframe { min-width:100%; min-height: 600px; min-height: 90vh; }*/ } .osd-wrap { position: relative; height: 600px; height: 90vh; width: 100%; min-height: 90vh; /*@media(max-width: $phone) { height: 50vh; min-height: 50vh; }*/ } .asset-viewer.fullscreen { min-height: 100%; width: 100%; z-index: 1; padding: 0px; /*& .item { position: absolute !important; width: 100%; height: 100%; min-height: 100% !important; } @for $n from 0 through 20 { &.asset-viewer-#{ $n } { width: 100%/$n; } }*/ } .item.item--thumbnail, .asset-viewer.fullscreen .item.item--thumbnail { position: absolute; width: auto; height: auto; min-height:0px !important; min-width:0px; max-height: 100%; max-width: 100%; top: 50%; left: 50%; transform: translate3d(-50%,-50%,0); } .fullscreen-metadata { position: absolute; background: #f1f8f1; left: 0px; bottom: 0px; z-index: 1; opacity: 0.98; width: 100%; height: 80px; padding: 20px; transition: transform 0.2s linear; transform: translateY(0px); /*&.slideAway { transform: translateY(100%); } & i{ vertical-align: top; margin: 3px; cursor: pointer; } & .title{ font-family: 'Lyon Display Web', Georgia, Times; font-size: 18px; display: inline-block; width: 350px; margin: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; vertical-align: top; } & .page-tooltip{ font-weight: normal; font-size: 10px; min-width: 120px; line-height: 21px; display: block; position: absolute; z-index: 5000; top: 100%; left: 50%; -webkit-transform: translateX(-50%); -moz-transform: translateX(-50%); -ms-transform: translateX(-50%); -o-transform: translateX(-50%); transform: translateX(-50%); box-shadow: 1px 2px 3px rgba(0, 0, 0, 0.3); background: white; color: black; padding: 2px 15px 0px; opacity: 0; -webkit-transition: opacity linear 0.3s; -moz-transition: opacity linear 0.3s; transition: opacity linear 0.3s; } &:hover .page-tooltip{ opacity: 1; } & .caption-link{ display: inline-block; float: left; font-size: 12px; cursor: pointer; border-bottom: 1px dotted #373a3c; }*/ } .caption-cntnr{ display: block; padding: 10px; position: absolute; bottom: 0px; background: rgba(255,255,255,0.95); width: 100%; } "] }) export class ArtstorViewer implements OnInit, OnDestroy, AfterViewInit { private _assetId: string = '' @Input() set assetId(value: string) { if (value != this._assetId) { this._assetId = value this.loadAssetById() } } get assetId(): string { // other logic return this._assetId } private asset: Asset private assetSub: Subscription private index: number = 0 private assetCompareCount: number private assetGroupCount: number private assetNumber: number private assets: Asset[] private prevAssetResults: any private isFullscreen: boolean private showCaption: boolean private quizMode: boolean @Output() fullscreenChange = new EventEmitter() @Output() nextPage = new EventEmitter() @Output() prevPage = new EventEmitter() @Output() removeAsset = new EventEmitter() @Output() assetDrawer = new EventEmitter() private isLoading: boolean = true // private isFullscreen: boolean = false private openSeaDragonReady: boolean = false private isOpenSeaDragonAsset: boolean = false private isKrpanoAsset: boolean = false private isKalturaAsset: boolean = false private mediaLoadingFailed: boolean = false private removableAsset: boolean = false private subscriptions: Subscription[] = [] private fallbackFailed: boolean = false private tileSource: string private lastZoomValue: number // Thumbanil Size is decremented if load fails (see thumbnailError) private thumbnailSize: number = 2 // private showCaption: boolean = true private kalturaUrl: string private osdViewer: any constructor( // private _assets: AssetService, // private _auth: AuthService, // private _analytics: Angulartics2 // private hostElement: ElementRef, private _http: HttpClient ) { } ngOnInit() { this.loadAssetById() // Assets don't initialize with fullscreen variable // And assets beyond the first/primary only show in fullscreen if (this.index > 0) { this.isFullscreen = true; } // Events for fullscreen/Presentation mode document.addEventListener('fullscreenchange', () => { this.changeHandler(); }, false); document.addEventListener('mozfullscreenchange', () => { this.changeHandler(); }, false); document.addEventListener('webkitfullscreenchange', () => { this.changeHandler(); }, false); } ngOnDestroy() { this.subscriptions.forEach((sub) => { sub.unsubscribe(); }); } ngAfterViewInit() { } private loadAssetById(): void { this.asset = new Asset(this.assetId, this._http) if (this.assetSub) { this.assetSub.unsubscribe() } this.assetSub = this.asset.isDataLoaded.subscribe(loaded => { if (loaded) { this.loadViewer() } }) } private loadViewer(): void { // Object types that need loaders switch (this.asset.typeName()) { default: // Display thumbnail this.mediaLoadingFailed = true; case 'image': // Image, try IIF this.loadIIIF(); break; case 'audio': // Kaltura media this.loadKaltura(); break; case 'kaltura': // Kaltura media this.loadKaltura(); break; case 'qtvr': this.loadKrpanoViewer(); break; } } /** * Gets information needed to load IIIF viewers, such as OpenSeaDragon */ private loadIIIF(): void { console.log("LOAD IIIF") if (this.asset.tileSource) { this.tileSource = this.asset.tileSource this.loadOpenSea() } else { this.mediaLoadingFailed = true } } /** * Loads the OpenSeaDragon on element at 'viewer-' + id * - Requires this.asset to have an id */ private loadOpenSea(): void { console.log("Load open sea") this.isOpenSeaDragonAsset = true; // OpenSeaDragon Initializer let id = this.asset.id + '-' + this.index; this.osdViewer = new OpenSeadragon({ id: 'osd-' + id, // prefix for Icon Images // prefixUrl: 'assets/img/osd/', tileSources: this.tileSource, gestureSettingsMouse: { scrollToZoom: true, pinchToZoom: true }, controlsFadeLength: 500, // debugMode: true, zoomInButton: 'zoomIn-' + id, zoomOutButton: 'zoomOut-' + id, homeButton: 'zoomFit-' + id, sequenceMode: true, initialPage: 0, nextButton: 'nextButton', showNavigator: true, navigatorPosition: 'BOTTOM_LEFT', navigatorSizeRatio: 0.15 }); // ---- Use handler in case other error crops up this.osdViewer.addOnceHandler('open-failed', (e: Event) => { console.error("OSD Opening source failed:",e) this.mediaLoadingFailed = true; this.osdViewer.destroy(); }); this.osdViewer.addHandler('pan', (value: any) => { // Save viewport pan for downloading the view this.asset.viewportDimensions.center = value.center }); this.osdViewer.addHandler('zoom', (value: any) => { this.lastZoomValue = value.zoom; // Save viewport values for downloading the view this.asset.viewportDimensions.containerSize = this.osdViewer.viewport.containerSize this.asset.viewportDimensions.contentSize = this.osdViewer.viewport._contentSize this.asset.viewportDimensions.zoom = value.zoom }); this.osdViewer.addOnceHandler('tile-load-failed', (e: Event) => { console.warn("OSD Loading tiles failed:", e) this.mediaLoadingFailed = true; this.osdViewer.destroy(); }); this.osdViewer.addOnceHandler('ready', () => { console.info("Tiles are ready"); this.openSeaDragonReady = true; }); // this.osdViewer.addHangler('open', (event) => { // var img = this.osdViewer.drawer.canvas.toDataURL("image/png"); // console.log(img) // var downloadlink = document.getElementById("downloadViewViaAssetViewer"); // downloadlink['href'] = img; // }) if (this.osdViewer && this.osdViewer.ButtonGroup) { this.osdViewer.ButtonGroup.element.addClass('button-group'); } this.osdViewer.navigator.element.style.marginBottom = "50px"; } private loadKrpanoViewer(): void{ if( this.asset.viewerData && this.asset.viewerData.panorama_xml ){ this.isKrpanoAsset = true // krpano.embedpano({ // xml: this.asset.viewerData.panorama_xml , // target: "pano-" + this.index, // onready: (viewer) => { // // See if there was an unreported error during final load // setTimeout(() => { // let content = viewer.innerHTML ? viewer.innerHTML : '' // // Workaround for detecting load failure // if (content.search('FATAL ERROR') >= 0 && content.search('loading failed') >= 0) { // this.mediaLoadingFailed = true // } // }, 500) // }, // onerror: (err) => { // // This handler does not fire for "Fatal Error" when loading XML // console.log(err) // }, // }) } else{ // If the media resolver info is not available then fallback to image viewer this.loadIIIF() } } private requestFullScreen(el: any): void { // Supports most browsers and their versions. var requestMethod = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen; if (requestMethod) { // Native full screen. requestMethod.call(el); } else if (window['ActiveXObject'] && typeof window['ActiveXObject'] !== "undefined") { // Older IE. var wscript = new ActiveXObject("WScript.Shell"); if (wscript !== null) { wscript.SendKeys("{F11}"); } } } private exitFullScreen(): void { // Being permissive to reference possible Document methods let document: any = window.document; if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if (document.exitFullscreen) { document.exitFullscreen(); } } private changeHandler() { if (document['webkitIsFullScreen'] || document['mozFullScreen'] || document['fullscreen']) { this.setFullscreen(true); } else { this.assets.splice(1); for(let i = 0; i < this.prevAssetResults.thumbnails.length; i++){ this.prevAssetResults.thumbnails[i].selected = false; } this.setFullscreen(false); } } public togglePresentationMode(): void { // Make the body go full screen. var elem = document.body; if (!this.isFullscreen) { this.requestFullScreen(elem); this.setFullscreen(true); } else { this.exitFullScreen(); this.setFullscreen(false); } }; /** * Setter for isFullscreen * - Ensures event is sent out to the Asset Page! */ private setFullscreen(isFullscreen: boolean): void { this.isFullscreen = isFullscreen; this.fullscreenChange.emit(isFullscreen); } /** * Setup the embedded Kaltura player */ private loadKaltura(): void { console.log("Load Kaltura") let targetId = 'kalturaIframe-' + this.index; if (this.asset.kalturaUrl) { document.getElementById(targetId).setAttribute('src', this.asset.kalturaUrl); this.mediaLoadingFailed = false this.isKalturaAsset = true this.isOpenSeaDragonAsset = false } }; /** * Generate Thumbnail URL */ public makeThumbUrl(imagePath: string, size ?: number): string { if (imagePath) { if (size) { imagePath = imagePath.replace(/(size)[0-4]/g, 'size' + size); } // Ensure relative if (imagePath.indexOf('artstor.org') > -1) { imagePath = imagePath.substring(imagePath.indexOf('artstor.org') + 12); } if (imagePath[0] != '/') { imagePath = '/' + imagePath; } if (imagePath.indexOf('thumb') < 0) { imagePath = '/thumb' + imagePath; } } else { imagePath = ''; } // Ceanup return '//mdxstage.artstor.org' + imagePath; } // Keep: We will want to dynamically load the Kaltura player // private getAndLoadKalturaId(data): void { // let kalturaId: string; // let targetId = 'video-' + this.asset.id + '-' + this.index; // if (data['imageUrl']) { // kalturaId = data['imageUrl'].substr(data['imageUrl'].lastIndexOf(':') + 1, data['imageUrl'].length - 1); // } // if (kalturaId && kalturaId.length > 0) { // this.isKalturaAsset = true; // this.isOpenSeaDragonAsset = false; // kWidget.embed({ // 'targetId': targetId, // 'wid': '_101', // 'uiconf_id': '23448189', // 'entry_id': kalturaId, // 'flashvars': { // // We provide our own fullscreen interface // 'fullScreenBtn.plugin': false // }, // 'readyCallback': function(playerId) { // var kdp: any = document.getElementById(playerId); // kdp.kBind('mediaError', function() { // console.error('Media error!'); // this.mediaLoadingFailed = true; // }); // } // }); // let kPlayer = document.getElementById(targetId); // } // }; /** * disableContextMenu * - Disable browser context menu / right click on the image viewer */ private disableContextMenu(event: Event): boolean{ return false; } /** * When thumbnail fails to load, try to load a different size * - Decrements the thumbnail size * - Workaround for missing sizes of particular thumbnails */ private thumbnailError(event: Event) : void { this.thumbnailSize-- } }