UNPKG

angular-duo-pane

Version:

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

571 lines (562 loc) 23.3 kB
import { __decorate, __values, __awaiter, __generator } from 'tslib'; import { TemplateRef, ChangeDetectorRef, ViewChild, ViewContainerRef, Input, Component, ɵɵdefineInjectable, Injectable, EventEmitter, ComponentFactoryResolver, Output, Directive, NgModule } from '@angular/core'; var Segment = /** @class */ (function () { function Segment(width, height, top, left) { this.width = width; this.height = height; this.top = top; this.left = left; } Object.defineProperty(Segment.prototype, "width", { get: function () { return this.Width; }, set: function (value) { this.Width = value; }, enumerable: true, configurable: true }); Object.defineProperty(Segment.prototype, "height", { get: function () { return this.Height; }, set: function (value) { this.Height = value; }, enumerable: true, configurable: true }); Object.defineProperty(Segment.prototype, "top", { get: function () { return this.Top; }, set: function (value) { this.Top = value; }, enumerable: true, configurable: true }); Object.defineProperty(Segment.prototype, "left", { get: function () { return this.Left; }, set: function (value) { this.Left = value; }, enumerable: true, configurable: true }); return Segment; }()); var DuoPaneComponent = /** @class */ (function () { function DuoPaneComponent(changeDetectorRef) { this.changeDetectorRef = changeDetectorRef; this.embeddedViewRef = null; this.Segment = new Segment(0, 0, 0, 0); this.ContentTemplateRef = undefined; } Object.defineProperty(DuoPaneComponent.prototype, "segment", { get: function () { return this.Segment; }, set: function (value) { this.Segment = value; }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneComponent.prototype, "contentTemplateRef", { get: function () { return this.ContentTemplateRef; }, set: function (value) { if (value instanceof TemplateRef) { this.ContentTemplateRef = value; this.renderView(); } }, enumerable: true, configurable: true }); DuoPaneComponent.prototype.ngAfterViewInit = function () { this.renderView(); }; DuoPaneComponent.prototype.renderView = function () { 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 = function () { return [ { 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); return DuoPaneComponent; }()); var DuoPaneInformationService = /** @class */ (function () { function DuoPaneInformationService() { var _this = this; 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', function (ev) { if (ev.data.action === 'update') { Object.assign(_this, ev.data.value); } }); window.addEventListener('resize', function () { return _this.debounce(_this.invalidate(), 200); }); } DuoPaneInformationService_1 = DuoPaneInformationService; DuoPaneInformationService.prototype.addEventListener = function (type, listener, options) { this.eventTarget.addEventListener(type, listener, options); }; DuoPaneInformationService.prototype.removeEventListener = function (type, callback, options) { this.eventTarget.removeEventListener(type, callback, options); }; DuoPaneInformationService.prototype.dispatchEvent = function (event) { if (event.type !== 'change') { return; } var methodName = "on" + event.type; if (typeof this[methodName] === 'function') { this[methodName](event); } return this.eventTarget.dispatchEvent(event); }; Object.defineProperty(DuoPaneInformationService.prototype, "foldSize", { get: function () { return this.FoldSize; }, set: function (foldSize) { sessionStorage.setItem(DuoPaneInformationService_1.ns + "-fold-size", foldSize.toString()); this.FoldSize = foldSize; this.invalidate(); }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneInformationService.prototype, "browserShellSize", { get: function () { return this.BrowserShellSize; }, set: function (browserShellSize) { sessionStorage.setItem(DuoPaneInformationService_1.ns + "-browser-shell-size", browserShellSize.toString()); this.BrowserShellSize = browserShellSize; this.invalidate(); }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneInformationService.prototype, "spanning", { get: function () { return this.Spanning; }, set: function (spanning) { sessionStorage.setItem(DuoPaneInformationService_1.ns + "-spanning", spanning); this.Spanning = spanning; this.invalidate(); }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneInformationService.prototype, "segments", { set: function (segments) { this.Segments = segments; }, enumerable: true, configurable: true }); DuoPaneInformationService.prototype.getTwoLargestSegments = function () { var e_1, _a; var segments = this.windowSegments; if (segments.length > 2) { var twoLargestSegments = [null, null]; var largestSize = 0; try { for (var segments_1 = __values(segments), segments_1_1 = segments_1.next(); !segments_1_1.done; segments_1_1 = segments_1.next()) { var segment = segments_1_1.value; var segmentSize = segment.height * segment.width; if (segmentSize >= largestSize) { largestSize = segmentSize; twoLargestSegments[0] = twoLargestSegments[1]; twoLargestSegments[1] = segment; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (segments_1_1 && !segments_1_1.done && (_a = segments_1.return)) _a.call(segments_1); } finally { if (e_1) throw e_1.error; } } return twoLargestSegments; } else { return segments; } }; Object.defineProperty(DuoPaneInformationService.prototype, "windowSegments", { get: function () { // 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') { var screenCenter = (window.innerHeight - this.browserShellSize) / 2; var 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') { var width = window.innerWidth / 2 - this.foldSize / 2; var 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) ]; } }, enumerable: true, configurable: true }); /** * Returns a function that won't call `fn` if it was invoked at a * faster interval than `wait`. */ DuoPaneInformationService.prototype.debounce = function (fn, wait) { var timeout; return function () { clearTimeout(timeout); timeout = setTimeout(function () { fn.apply(this, arguments); }, wait); }; }; DuoPaneInformationService.prototype.invalidate = function () { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!!this.needsDispatch) return [3 /*break*/, 2]; this.needsDispatch = true; _a = this; return [4 /*yield*/, Promise.resolve(false)]; case 1: _a.needsDispatch = _b.sent(); this.dispatchEvent(new Event('change')); _b.label = 2; case 2: return [2 /*return*/]; } }); }); }; var DuoPaneInformationService_1; 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); return DuoPaneInformationService; }()); var DuoPaneDirective = /** @class */ (function () { function DuoPaneDirective(componentfactoryResolver, templateRef, viewContainer, dualScreenInformationService) { var _this = this; 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', function () { _this.updateView(); }); this.updateView(); } Object.defineProperty(DuoPaneDirective.prototype, "primaryPanePercentageSingleSegment", { get: function () { return this.PrimaryPanePercentageSingleSegment; }, set: function (value) { if (typeof value === 'string') { var 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(); } } }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneDirective.prototype, "secondaryPane", { set: function (secondaryPane) { this.secondaryPaneTemplateRef = secondaryPane; this.destroySecondaryPaneRef(); this.updateView(); }, enumerable: true, configurable: true }); DuoPaneDirective.prototype.destroySecondaryPaneRef = function () { if (this.secondaryPaneRef) { var secondaryPaneIndex = this.viewContainer.indexOf(this.secondaryPaneRef.hostView); if (secondaryPaneIndex >= 0) { this.viewContainer.remove(secondaryPaneIndex); } this.secondaryPaneRef.destroy(); this.secondaryPaneRef = null; } }; DuoPaneDirective.prototype.destroyPrimaryPaneRef = function () { if (this.primaryPaneRef) { var primaryPaneIndex = this.viewContainer.indexOf(this.primaryPaneRef.hostView); if (primaryPaneIndex >= 0) { this.viewContainer.remove(primaryPaneIndex); } this.primaryPaneRef.destroy(); this.primaryPaneRef = null; } }; Object.defineProperty(DuoPaneDirective.prototype, "ensureSecondaryPaneVisible", { set: function (value) { this.EnsureSecondaryPaneVisible = value; this.updateView(); }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneDirective.prototype, "twoPaneMinWidthSingleSegment", { set: function (minWidth) { if (typeof minWidth === 'string') { var 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(); } }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneDirective.prototype, "twoPaneMinHeightSingleSegment", { set: function (minHeight) { if (typeof minHeight === 'string') { var 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(); } }, enumerable: true, configurable: true }); Object.defineProperty(DuoPaneDirective.prototype, "twoPaneSpanningModeSingleSegment", { set: function (spanningMode) { if (spanningMode) { this.TwoPaneSpanningModeSingleSegment = spanningMode; this.updateView(); } }, enumerable: true, configurable: true }); DuoPaneDirective.prototype.emitSecondaryPaneVisibility = function (visible) { if (this.secondaryPaneVisible === undefined || this.secondaryPaneVisible === null || visible !== this.secondaryPaneVisible) { this.secondaryPaneVisible = visible; this.secondaryPaneVisibilityHandler.emit(visible); } }; DuoPaneDirective.prototype.updateView = function () { /*this.viewContainer.createEmbeddedView(this.templateRef); if(this.secondaryPaneTemplateRef != null) { this.viewContainer.createEmbeddedView(this.secondaryPaneTemplateRef); }*/ var 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); } } } }; DuoPaneDirective.prototype.renderBothPanes = function (windowSegment) { var primarySegment; var secondarySegment; if (this.TwoPaneSpanningModeSingleSegment === 'single-fold-horizontal') { var 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') { var 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]); }; DuoPaneDirective.prototype.renderDualPanes = function (windowSegments) { if (windowSegments.length > 0) { this.renderPrimaryPane(windowSegments[0]); } if (windowSegments.length > 1) { this.renderSecondaryPane(windowSegments[1]); } }; DuoPaneDirective.prototype.renderPrimaryPane = function (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; }; DuoPaneDirective.prototype.renderSecondaryPane = function (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 = function () { return [ { 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); return DuoPaneDirective; }()); var AngularDuoPaneModule = /** @class */ (function () { function AngularDuoPaneModule() { } AngularDuoPaneModule = __decorate([ NgModule({ declarations: [DuoPaneComponent, DuoPaneDirective], imports: [], exports: [DuoPaneDirective, DuoPaneComponent] }) ], AngularDuoPaneModule); return 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