UNPKG

angular-duo-pane

Version:

A library for Angular projects to easily add a two pane view for desktops, foldables and dual screen devices.

472 lines (463 loc) 18 kB
import { __decorate, __awaiter } from 'tslib'; import { TemplateRef, ChangeDetectorRef, ViewChild, ViewContainerRef, Input, Component, ɵɵdefineInjectable, Injectable, EventEmitter, ComponentFactoryResolver, Output, Directive, NgModule } from '@angular/core'; class Segment { constructor(width, height, top, left) { this.width = width; this.height = height; this.top = top; this.left = left; } get width() { return this.Width; } set width(value) { this.Width = value; } get height() { return this.Height; } set height(value) { this.Height = value; } get top() { return this.Top; } set top(value) { this.Top = value; } get left() { return this.Left; } set left(value) { this.Left = value; } } let DuoPaneComponent = class DuoPaneComponent { constructor(changeDetectorRef) { this.changeDetectorRef = changeDetectorRef; this.embeddedViewRef = null; this.Segment = new Segment(0, 0, 0, 0); this.ContentTemplateRef = undefined; } get segment() { return this.Segment; } set segment(value) { this.Segment = value; } get contentTemplateRef() { return this.ContentTemplateRef; } set contentTemplateRef(value) { if (value instanceof TemplateRef) { this.ContentTemplateRef = value; this.renderView(); } } ngAfterViewInit() { this.renderView(); } renderView() { if (this.ContentTemplateRef && this.viewContainerRef) { if (this.embeddedViewRef) { this.embeddedViewRef.destroy(); } this.viewContainerRef.clear(); this.embeddedViewRef = this.viewContainerRef.createEmbeddedView(this.ContentTemplateRef); this.changeDetectorRef.detectChanges(); } } }; DuoPaneComponent.ctorParameters = () => [ { type: ChangeDetectorRef } ]; __decorate([ ViewChild('viewContainer', { read: ViewContainerRef, static: true }) ], DuoPaneComponent.prototype, "viewContainerRef", void 0); __decorate([ Input() ], DuoPaneComponent.prototype, "segment", null); __decorate([ Input() ], DuoPaneComponent.prototype, "contentTemplateRef", null); DuoPaneComponent = __decorate([ Component({ selector: 'duo-pane', template: "<div class=\"dual-screen-view-pane\" [style.left]=\"segment.left + 'px'\" [style.top]=\"segment.top + 'px'\" [style.height]=\"segment.height + 'px'\" [style.width]=\"segment.width + 'px'\">\n <ng-container #viewContainer></ng-container>\n</div>\n", styles: [".dual-screen-view-pane{position:absolute;overflow:auto}"] }) ], DuoPaneComponent); var DuoPaneInformationService_1; let DuoPaneInformationService = DuoPaneInformationService_1 = class DuoPaneInformationService { constructor() { this.Spanning = 'none'; this.FoldSize = 0; this.BrowserShellSize = 0; this.Segments = null; this.needsDispatch = false; this.Spanning = sessionStorage.getItem(`${DuoPaneInformationService_1.ns}-spanning`) || 'none'; this.FoldSize = +sessionStorage.getItem(`${DuoPaneInformationService_1.ns}-fold-size`) || 0; this.BrowserShellSize = +sessionStorage.getItem(`${DuoPaneInformationService_1.ns}-browser-shell-size`) || 0; this.eventTarget = document.createDocumentFragment(); // Web-based emulator runs this polyfill in an iframe, we need to // communicate emulator state changes to the site. // Should only be registered once (in CSS or JS polyfill, not both). window.addEventListener('message', ev => { if (ev.data.action === 'update') { Object.assign(this, ev.data.value); } }); window.addEventListener('resize', () => this.debounce(this.invalidate(), 200)); } addEventListener(type, listener, options) { this.eventTarget.addEventListener(type, listener, options); } removeEventListener(type, callback, options) { this.eventTarget.removeEventListener(type, callback, options); } dispatchEvent(event) { if (event.type !== 'change') { return; } const methodName = `on${event.type}`; if (typeof this[methodName] === 'function') { this[methodName](event); } return this.eventTarget.dispatchEvent(event); } get foldSize() { return this.FoldSize; } set foldSize(foldSize) { sessionStorage.setItem(`${DuoPaneInformationService_1.ns}-fold-size`, foldSize.toString()); this.FoldSize = foldSize; this.invalidate(); } get browserShellSize() { return this.BrowserShellSize; } set browserShellSize(browserShellSize) { sessionStorage.setItem(`${DuoPaneInformationService_1.ns}-browser-shell-size`, browserShellSize.toString()); this.BrowserShellSize = browserShellSize; this.invalidate(); } get spanning() { return this.Spanning; } set spanning(spanning) { sessionStorage.setItem(`${DuoPaneInformationService_1.ns}-spanning`, spanning); this.Spanning = spanning; this.invalidate(); } set segments(segments) { this.Segments = segments; } getTwoLargestSegments() { const segments = this.windowSegments; if (segments.length > 2) { const twoLargestSegments = [null, null]; let largestSize = 0; for (const segment of segments) { const segmentSize = segment.height * segment.width; if (segmentSize >= largestSize) { largestSize = segmentSize; twoLargestSegments[0] = twoLargestSegments[1]; twoLargestSegments[1] = segment; } } return twoLargestSegments; } else { return segments; } } get windowSegments() { // TODO: replace with window.getWindowSegments() once it is a web standard if (this.Segments !== null && this.Segments.length > 0) { return this.Segments; } if (this.spanning === 'none') { return [ new Segment(window.innerWidth, window.innerHeight, 0, 0) ]; } if (this.spanning === 'single-fold-horizontal') { const screenCenter = (window.innerHeight - this.browserShellSize) / 2; const width = window.innerWidth; return [ new Segment(width, screenCenter - this.foldSize / 2, 0, 0), new Segment(width, this.foldSize, screenCenter - this.foldSize / 2, 0), new Segment(width, window.innerHeight, screenCenter + this.foldSize / 2, 0) ]; } if (this.spanning === 'single-fold-vertical') { const width = window.innerWidth / 2 - this.foldSize / 2; const height = window.innerHeight; return [ new Segment(width, height, 0, 0), new Segment(this.foldSize, height, 0, width), new Segment(width, height, 0, window.innerWidth / 2 + this.foldSize / 2) ]; } } /** * Returns a function that won't call `fn` if it was invoked at a * faster interval than `wait`. */ debounce(fn, wait) { let timeout; return () => { clearTimeout(timeout); timeout = setTimeout(function () { fn.apply(this, arguments); }, wait); }; } invalidate() { return __awaiter(this, void 0, void 0, function* () { if (!this.needsDispatch) { this.needsDispatch = true; this.needsDispatch = yield Promise.resolve(false); this.dispatchEvent(new Event('change')); } }); } }; DuoPaneInformationService.ns = '__foldables__'; DuoPaneInformationService.ɵprov = ɵɵdefineInjectable({ factory: function DuoPaneInformationService_Factory() { return new DuoPaneInformationService(); }, token: DuoPaneInformationService, providedIn: "root" }); DuoPaneInformationService = DuoPaneInformationService_1 = __decorate([ Injectable({ providedIn: 'root' }) ], DuoPaneInformationService); let DuoPaneDirective = class DuoPaneDirective { constructor(componentfactoryResolver, templateRef, viewContainer, dualScreenInformationService) { this.componentfactoryResolver = componentfactoryResolver; this.templateRef = templateRef; this.viewContainer = viewContainer; this.dualScreenInformationService = dualScreenInformationService; this.secondaryPaneTemplateRef = null; this.EnsureSecondaryPaneVisible = false; /* Variables set to control two pane if only one window segment is available-*/ this.TwoPaneMinWidthSingleSegment = 0; this.TwoPaneMinHeightSingleSegment = 0; this.TwoPaneSpanningModeSingleSegment = 'none'; this.PrimaryPanePercentageSingleSegment = 50; this.paneFactory = this.componentfactoryResolver.resolveComponentFactory(DuoPaneComponent); this.primaryPaneRef = null; this.secondaryPaneRef = null; this.secondaryPaneVisibilityHandler = new EventEmitter(); this.secondaryPaneVisible = undefined; dualScreenInformationService.addEventListener('change', () => { this.updateView(); }); this.updateView(); } get primaryPanePercentageSingleSegment() { return this.PrimaryPanePercentageSingleSegment; } set primaryPanePercentageSingleSegment(value) { if (typeof value === 'string') { const fractionValue = Number(value); if (!isNaN(fractionValue) && fractionValue >= 0 && fractionValue <= 100) { this.PrimaryPanePercentageSingleSegment = fractionValue; this.updateView(); } } else if (typeof value === 'number') { if (value >= 0 && value <= 100) { this.PrimaryPanePercentageSingleSegment = value; this.updateView(); } } } set secondaryPane(secondaryPane) { this.secondaryPaneTemplateRef = secondaryPane; this.destroySecondaryPaneRef(); this.updateView(); } destroySecondaryPaneRef() { if (this.secondaryPaneRef) { const secondaryPaneIndex = this.viewContainer.indexOf(this.secondaryPaneRef.hostView); if (secondaryPaneIndex >= 0) { this.viewContainer.remove(secondaryPaneIndex); } this.secondaryPaneRef.destroy(); this.secondaryPaneRef = null; } } destroyPrimaryPaneRef() { if (this.primaryPaneRef) { const primaryPaneIndex = this.viewContainer.indexOf(this.primaryPaneRef.hostView); if (primaryPaneIndex >= 0) { this.viewContainer.remove(primaryPaneIndex); } this.primaryPaneRef.destroy(); this.primaryPaneRef = null; } } set ensureSecondaryPaneVisible(value) { this.EnsureSecondaryPaneVisible = value; this.updateView(); } set twoPaneMinWidthSingleSegment(minWidth) { if (typeof minWidth === 'string') { const minWidthValue = Number(minWidth); if (minWidthValue >= 0 && !isNaN(minWidthValue)) { this.TwoPaneMinWidthSingleSegment = minWidthValue; this.updateView(); } } else if (typeof minWidth === 'number' && minWidth >= 0) { this.TwoPaneMinWidthSingleSegment = minWidth; this.updateView(); } } set twoPaneMinHeightSingleSegment(minHeight) { if (typeof minHeight === 'string') { const minHeightValue = Number(minHeight); if (minHeightValue >= 0 && !isNaN(minHeightValue)) { this.TwoPaneMinHeightSingleSegment = minHeightValue; this.updateView(); } } else if (typeof minHeight === 'number' && minHeight >= 0) { this.TwoPaneMinHeightSingleSegment = minHeight; this.updateView(); } } set twoPaneSpanningModeSingleSegment(spanningMode) { if (spanningMode) { this.TwoPaneSpanningModeSingleSegment = spanningMode; this.updateView(); } } emitSecondaryPaneVisibility(visible) { if (this.secondaryPaneVisible === undefined || this.secondaryPaneVisible === null || visible !== this.secondaryPaneVisible) { this.secondaryPaneVisible = visible; this.secondaryPaneVisibilityHandler.emit(visible); } } updateView() { /*this.viewContainer.createEmbeddedView(this.templateRef); if(this.secondaryPaneTemplateRef != null) { this.viewContainer.createEmbeddedView(this.secondaryPaneTemplateRef); }*/ const windowSegements = this.dualScreenInformationService.windowSegments; if (windowSegements.length > 0) { if (windowSegements.length > 1) { this.renderDualPanes(this.dualScreenInformationService.getTwoLargestSegments()); } else if (windowSegements[0].height >= this.TwoPaneMinHeightSingleSegment && windowSegements[0].width >= this.TwoPaneMinWidthSingleSegment && this.TwoPaneSpanningModeSingleSegment !== 'none') { this.renderBothPanes(windowSegements[0]); } else { if (this.EnsureSecondaryPaneVisible) { this.destroyPrimaryPaneRef(); this.renderSecondaryPane(windowSegements[0]); } else { this.destroySecondaryPaneRef(); this.renderPrimaryPane(windowSegements[0]); this.emitSecondaryPaneVisibility(false); } } } } renderBothPanes(windowSegment) { let primarySegment; let secondarySegment; if (this.TwoPaneSpanningModeSingleSegment === 'single-fold-horizontal') { const fraction = this.PrimaryPanePercentageSingleSegment / 100; primarySegment = new Segment(windowSegment.width, windowSegment.height * fraction, 0, 0); secondarySegment = new Segment(windowSegment.width, windowSegment.height * (1 - fraction), windowSegment.height * fraction, 0); } else if (this.TwoPaneSpanningModeSingleSegment === 'single-fold-vertical') { const fraction = this.PrimaryPanePercentageSingleSegment / 100; primarySegment = new Segment(windowSegment.width * fraction, windowSegment.height, 0, 0); secondarySegment = new Segment(windowSegment.width * (1 - fraction), windowSegment.height, 0, windowSegment.width * fraction); } else { return; } this.renderDualPanes([primarySegment, secondarySegment]); } renderDualPanes(windowSegments) { if (windowSegments.length > 0) { this.renderPrimaryPane(windowSegments[0]); } if (windowSegments.length > 1) { this.renderSecondaryPane(windowSegments[1]); } } renderPrimaryPane(segment) { // const paneFactory = this.componentfactoryResolver.resolveComponentFactory(PrimaryPaneComponent); if (!this.primaryPaneRef) { this.primaryPaneRef = this.viewContainer.createComponent(this.paneFactory); } this.primaryPaneRef.instance.segment = segment; this.primaryPaneRef.instance.contentTemplateRef = this.templateRef; } renderSecondaryPane(segment) { if (this.secondaryPaneTemplateRef !== null && this.secondaryPaneTemplateRef !== undefined) { // const paneFactory = this.componentfactoryResolver.resolveComponentFactory(PrimaryPaneComponent); if (!this.secondaryPaneRef) { this.secondaryPaneRef = this.viewContainer.createComponent(this.paneFactory); } this.secondaryPaneRef.instance.segment = segment; this.secondaryPaneRef.instance.contentTemplateRef = this.secondaryPaneTemplateRef; this.emitSecondaryPaneVisibility(true); } } }; DuoPaneDirective.ctorParameters = () => [ { type: ComponentFactoryResolver }, { type: TemplateRef }, { type: ViewContainerRef }, { type: DuoPaneInformationService } ]; __decorate([ Input() ], DuoPaneDirective.prototype, "primaryPanePercentageSingleSegment", null); __decorate([ Output() ], DuoPaneDirective.prototype, "secondaryPaneVisibilityHandler", void 0); __decorate([ Input() ], DuoPaneDirective.prototype, "secondaryPane", null); __decorate([ Input() ], DuoPaneDirective.prototype, "ensureSecondaryPaneVisible", null); __decorate([ Input() ], DuoPaneDirective.prototype, "twoPaneMinWidthSingleSegment", null); __decorate([ Input() ], DuoPaneDirective.prototype, "twoPaneMinHeightSingleSegment", null); __decorate([ Input() ], DuoPaneDirective.prototype, "twoPaneSpanningModeSingleSegment", null); DuoPaneDirective = __decorate([ Directive({ selector: '[duoPane]' }) ], DuoPaneDirective); let AngularDuoPaneModule = class AngularDuoPaneModule { }; AngularDuoPaneModule = __decorate([ NgModule({ declarations: [DuoPaneComponent, DuoPaneDirective], imports: [], exports: [DuoPaneDirective, DuoPaneComponent] }) ], AngularDuoPaneModule); /* * Public API Surface of angular-duo-pane */ /** * Generated bundle index. Do not edit. */ export { AngularDuoPaneModule, DuoPaneDirective, Segment, DuoPaneComponent as ɵa, DuoPaneInformationService as ɵb }; //# sourceMappingURL=angular-duo-pane.js.map