controlled-rising-skyline
Version:
This library provides a rising skyline chart with an horizontal control panel control. This widget is linked to a dynamic history of buildings. An animation displays the rising of the skyline.
323 lines (315 loc) • 16.2 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, EventEmitter, Component, Input, HostBinding, Output, NgModule } from '@angular/core';
import * as i1 from 'rising-skyline';
import { Building, RisingSkylineService, ColorService, RisingSkylineModule } from 'rising-skyline';
import { BehaviorSubject } from 'rxjs';
import { CommonModule, DatePipe } from '@angular/common';
import { MatSliderModule } from '@angular/material/slider';
class ControlledRisingSkylineService {
constructor(skylineService) {
this.skylineService = skylineService;
}
/**
* Generate a random Skyline history with 100 buildings for testing purpose.
* @param skyline$ the observable whch will emit the array of buildings
*/
randomSkylineHistory(skyline$) {
const upperYear = 2020;
const upperWeek = 45;
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function addDays(theDate, days) {
return new Date(theDate.getTime() + days * 24 * 60 * 60 * 1000);
}
const buildings = [];
for (let id = 0; id < 100; id++) {
// The building starts to rise on this date
const startDate = new Date(randomInteger(2017, 2018), randomInteger(1, 12), randomInteger(1, 27));
// The building ends its rising on this date
const endDate = new Date(randomInteger(2019, 2020), randomInteger(1, 12), randomInteger(1, 27));
for (let d = startDate.clone(), stepHeight = 1; d <= endDate; d.addDays(7), stepHeight++) {
buildings.push(new Building(id, this.skylineService.toYearWeek(d).year, this.skylineService.toYearWeek(d).week, 40, stepHeight * 2, randomInteger(0, 100), 'Building ' + id));
}
}
skyline$.next(buildings);
}
}
ControlledRisingSkylineService.ɵprov = i0.ɵɵdefineInjectable({ factory: function ControlledRisingSkylineService_Factory() { return new ControlledRisingSkylineService(i0.ɵɵinject(i1.RisingSkylineService)); }, token: ControlledRisingSkylineService, providedIn: "root" });
ControlledRisingSkylineService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
ControlledRisingSkylineService.ctorParameters = () => [
{ type: RisingSkylineService }
];
class ControlledRisingSkylineComponent {
constructor(skylineService) {
this.skylineService = skylineService;
/**
* The width of the container, with its units of mesure (px, %, em...).
* Defaut value is __'600px'__;
*/
this.width = '600px';
/**
* The height of the container, with its units of mesure (px, %, em...).
* Defaut value is _'400px'_;
*/
this.height = '400px';
/**
* The margin around the container.
* Defaut value is _'10px'_;
*/
this.margin = '10px';
/**
/**
* The width of each building on the skyline without the unit of measure.
* Defaut value is __40__;
*/
this.buildingWidth = 40;
/**
* This behaviorSubject emits the complete story of the rising skyline.
*
* This reveived array will be passed to the __SkylineService__.
*/
this.risingSkylineHistory$ = new BehaviorSubject([]);
/**
* Starting speed of the animation in ms.
* Default value is _30_
*/
this.speed = 30;
/**
* __Starting__ color of the color gradation.
* Default value is _ping_
*/
this.startingColor = 'red';
/**
* __Ending__ color of the color gradation.
* Default value is _blue_
*/
this.endingColor = 'green';
/**
* Always display the vertical title on top of each building.
*
* Default value is **false**.
*/
this.displayVerticalTitle = false;
/**
* Display the vertical title when the building height reaches this value.
*
* Default value is **10**.
*/
this.buildingMinimumHeightVerticalTitle = 10;
/**
* Color of the skyline container
*/
this.skylineBackgroundColor = 'transparent';
/**
* Color of the skyline control panel
*/
this.controlBackgroundColor = 'lightGrey';
/**
* __Slider__ color.
* Default value is _violet_
*/
this.sliderColor = 'violet';
/**
* (_Internal_) debug mode.
* Default value is **false**
*/
this.debug = false;
/**
* This messenger wil inform the parent container that the user has clicked a building.
*/
this.onClickBuilding = new EventEmitter();
/**
* This messenger will inform the parent container that the user is entering on a building.
*/
this.onEnterBuilding = new EventEmitter();
/**
* This messenger wil inform the parent container that the user is entering on a building.
*/
this.onLeaveBuilding = new EventEmitter();
/**
* The current position of floor _(or episode)_ being drawn.
*/
this.positionOfFloor = 0;
}
ngOnInit() {
if (this.debug) {
console.log('Skyline w ' + this.width + ', h ' + this.height);
}
this.subscriptionSkyline = this.skylineService.episode$
.subscribe({
next: floors => this.positionOfFloor++
});
}
/**
* After view initialization : Here, we set the width of the container.
*/
ngAfterViewInit() {
const mainDiv = document.getElementById('mainDiv');
if (mainDiv) {
mainDiv.setAttribute('style', 'width:' + this.width + 'px;height:' + this.height + 'px');
if (this.debug) {
console.log(mainDiv.getAttribute('style'));
}
}
else {
console.error('INTERNAL ERROR : Did not retrieve the main div');
}
}
/**
* Mouse is entering the building.
*/
mouseEnterBuilding($event) {
this.onEnterBuilding.emit($event);
}
/**
* Mouse is leaving the building.
*/
mouseLeaveBuilding($event) {
this.onLeaveBuilding.emit($event);
}
/**
* Mouse is clicking the building.
*/
mouseClickBuilding($event) {
this.onClickBuilding.emit($event);
}
ngOnDestroy() {
this.subscriptionSkyline.unsubscribe();
}
}
ControlledRisingSkylineComponent.decorators = [
{ type: Component, args: [{
selector: 'controlled-rising-skyline',
template: "<div id=\"mainDiv\" >\n\n\t<rising-skyline\n\t\t[width] = width\n\t\t[height] = height\n\t\t[margin] = margin\n\t\t[risingSkylineHistory$] = risingSkylineHistory$\n\t\t[speed] = speed\n\t\t[displayVerticalTitle] = displayVerticalTitle\n\t\t[buildingMinimumHeightVerticalTitle]=buildingMinimumHeightVerticalTitle\n\t\t[startingColor] = startingColor\n\t\t[endingColor] = endingColor\n\t\t[font] = font\n\t\t(onClickBuilding)=\"mouseClickBuilding($event)\"\n\t\t(onEnterBuilding)=\"mouseEnterBuilding($event)\"\n\t\t(onLeaveBuilding)=\"mouseLeaveBuilding($event)\">\t\t\n\t</rising-skyline>\n\n\t<app-panel-control \n\t\t[backgroundColor] = controlBackgroundColor\n\t\t[sliderColor] = sliderColor\n\t\t[debug] = debug>\n\t</app-panel-control>\n</div>",
styles: ["#mainDiv{background-color:var(--skyline-background-color);width:var(--mainDiv-width);height:var(--mainDiv-height)}\n"]
},] }
];
ControlledRisingSkylineComponent.ctorParameters = () => [
{ type: RisingSkylineService }
];
ControlledRisingSkylineComponent.propDecorators = {
width: [{ type: Input }, { type: HostBinding, args: ['style.--mainDiv-width',] }],
height: [{ type: Input }, { type: HostBinding, args: ['style.--mainDiv-height',] }],
margin: [{ type: Input }],
buildingWidth: [{ type: Input }],
risingSkylineHistory$: [{ type: Input }],
speed: [{ type: Input }],
startingColor: [{ type: Input }],
endingColor: [{ type: Input }],
displayVerticalTitle: [{ type: Input }],
buildingMinimumHeightVerticalTitle: [{ type: Input }],
font: [{ type: Input }],
skylineBackgroundColor: [{ type: HostBinding, args: ['style.--skyline-background-color',] }, { type: Input }],
controlBackgroundColor: [{ type: Input }],
sliderColor: [{ type: Input }],
debug: [{ type: Input }],
onClickBuilding: [{ type: Output }],
onEnterBuilding: [{ type: Output }],
onLeaveBuilding: [{ type: Output }]
};
class PanelControlComponent {
constructor(skylineService, colorService) {
this.skylineService = skylineService;
this.colorService = colorService;
/**
* Slider color of the panel control
*/
this.sliderColor = 'violet';
/**
* __Debug__ mode default value is **False**.
*/
this.debug = false;
/**
* The Skyline subscription.
*/
this.skylineSubscription = null;
this.formatYearWeek = (value) => {
return this.skylineService.currentYear + '/'
+ ((this.skylineService.currentWeek < 10) ? '0' + this.skylineService.currentWeek : this.skylineService.currentWeek);
};
}
ngOnInit() {
}
ngAfterViewInit() {
this.skylineService.episode$
.subscribe(floors => {
if (floors.length > 0) {
const allIndex = floors
.filter(floor => floor.height > 0)
.map(floor => floor.index)
.reduce((theSum, index) => theSum + index, 0);
const meanIndex = Math.floor(allIndex / floors.filter(floor => floor.height > 0).length);
if (this.debug) {
console.log('Index of color used for the thumb coin', meanIndex);
}
const htmlThumbLabelCoin = document.getElementsByClassName('mat-slider-thumb-label').item(0);
if (htmlThumbLabelCoin) {
const color = this.colorService.color(meanIndex);
if (this.debug) {
console.log('Index of color used for the thumb coin %d is processing the color %s', meanIndex, color);
}
htmlThumbLabelCoin.setAttribute('style', 'background-color:' + color);
}
}
});
}
ngOnDestroy() {
if (this.skylineSubscription) {
this.skylineSubscription.unsubscribe();
}
}
onSliderChange($event) {
const yw = this.skylineService.yearWeeks[Math.max(0, $event.value - 2)];
this.skylineService.currentYear = yw.year;
this.skylineService.currentWeek = yw.week;
this.skylineService.currentEpisode = $event.value - 2;
if (this.debug) {
console.log('Position %d points to (%d; %d)', $event.value, yw.year, yw.week);
}
const episode = this.skylineService.extractSkylineEpisode(yw.year, yw.week);
this.skylineService.drawEpisode(episode);
}
}
PanelControlComponent.decorators = [
{ type: Component, args: [{
selector: 'app-panel-control',
template: "<div id=\"controlPanel\" class=\"container-fluid\" *ngIf=\"(skylineService.episode$|async).length > 0\">\n\n <div class=\"row\">\n\n <div class=\"col-1\"></div>\n\n <div id=\"firstDate\" class=\"col-1 my-auto\">\n {{skylineService.toYearWeek(skylineService.firstDate).year}}/{{skylineService.toYearWeek(skylineService.firstDate).week | number : '2.0-0'}}\n </div>\n\n <div id=\"slider\" class=\"col-6 my-auto\">\n <mat-slider class=\"cdk-focused\" \n aria-label=\"Episodes timeline\" role=\"slider\"\n min=\"1\" max=\"{{this.skylineService.countEpisodes}}\" [value]=\"this.skylineService.currentEpisode\"\n thumbLabel [displayWith]=\"formatYearWeek\"\n (change)=\"onSliderChange($event)\" >\n </mat-slider>\n </div>\n\n <div id=\"lastDate\" class=\"col-4 my-auto\">\n <span>\n {{skylineService.toYearWeek(skylineService.lastDate).year}}/{{skylineService.toYearWeek(skylineService.lastDate).week | number : '2.0-0'}}\n </span>\n <button *ngIf=\"!skylineService.pause\" \n class=\"btn btn-outline-primary btn-circle control-button\" \n (click)=\"skylineService.pauseRising()\"\n aria-label=\"Pause the animation\">\n <em class=\"fas fa-pause\"></em>\n </button>\n \n <button \n *ngIf=\"skylineService.pause\" \n class=\"btn btn-outline-primary btn-circle control-button\" \n (click)=\"skylineService.playRising()\"\n aria-label=\"Start or restart the animation\">\n <i class=\"fas fa-play\"></i>\n </button>\n\n <button \n class=\"btn btn-outline-primary btn-circle control-button\" \n (click)=\"skylineService.rotateVariation()\"\n aria-label=\"Increase or decrease the speed of the animation\">\n <span class=\"speed number\" innerHTML = \"{{skylineService.variation.title}}\"></span><span class=\"speed x\">x</span>\n </button>\n </div>\n\n </div>\n\n</div>",
styles: ["#controlPanel{height:60px;width:100%;display:block;background-color:var(--control-background-color);padding-top:5px}.mat-slider{width:100%;padding-right:0;padding-left:0}:host ::ng-deep .mat-slider-horizontal .mat-slider-thumb-label{width:42px;height:42px;transform:translateY(20px) scale(1) rotate(45deg);color:#fff}:host ::ng-deep .mat-slider-thumb-label-text{opacity:1;font-size:10px;color:#fff}:host ::ng-deep .mat-slider:not(.mat-slider-disabled).cdk-focused .mat-slider-thumb-label{width:42px;height:42px;margin-top:20px;right:-20px;background-color:#006400;color:#fff}:host ::ng-deep .mat-slider.mat-slider-horizontal .mat-slider-track-background,.mat-slider.mat-slider-horizontal .mat-slider-track-fill{height:100%}:host ::ng-deep .mat-slider:not(.mat-slider-disabled).cdk-focused .mat-slider-thumb-label-text{opacity:1;font-size:10px}:host ::ng-deep .mat-slider:not(.mat-slider-disabled).cdk-focused .mat-slider-thumb-label{border-radius:50%;color:#fff}:host ::ng-deep .mat-slider.mat-slider-horizontal .mat-slider-track-fill{background-color:var(--slider-color)}button.pause{height:30px;width:30px}#slider{padding-left:15px;padding-right:13px}#firstDate{padding-right:0;text-align:right;height:100%}#lastDate{padding-left:0;padding-right:0;text-align:left}button.control-button{color:var(--slider-color);border-color:gray;border-radius:50%;margin-left:4px;padding-bottom:7px;padding-left:12px}button.control-button:hover{color:#fff;background-color:var(--slider-color)}span.speed{font-size:13px}span.x{position:relative;bottom:1px}\n"]
},] }
];
PanelControlComponent.ctorParameters = () => [
{ type: RisingSkylineService },
{ type: ColorService }
];
PanelControlComponent.propDecorators = {
backgroundColor: [{ type: HostBinding, args: ['style.--control-background-color',] }, { type: Input }],
sliderColor: [{ type: HostBinding, args: ['style.--slider-color',] }, { type: Input }],
debug: [{ type: Input }]
};
class ControlledRisingSkylineModule {
}
ControlledRisingSkylineModule.decorators = [
{ type: NgModule, args: [{
declarations: [ControlledRisingSkylineComponent, PanelControlComponent],
imports: [
CommonModule,
RisingSkylineModule,
MatSliderModule
],
providers: [DatePipe],
exports: [ControlledRisingSkylineComponent]
},] }
];
/*
* Public API Surface of controlled-rising-skyline
*/
/**
* Generated bundle index. Do not edit.
*/
export { ControlledRisingSkylineComponent, ControlledRisingSkylineModule, ControlledRisingSkylineService, PanelControlComponent as ɵa };
//# sourceMappingURL=controlled-rising-skyline.js.map