UNPKG

ng-beautiful-gauges

Version:

Angular 6+ component wrapper for the canvas-gauges lib written by @Mikhus

124 lines (123 loc) 4.88 kB
/*! * The MIT License (MIT) * * Copyright (c) 2017 Vlad Martynenko <vladimir.martynenko.work@gmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import { NgZone, ElementRef, OnInit, AfterViewInit } from '@angular/core'; import * as CanvasGauges from 'canvas-gauges'; /** * Base gauge component for the Gauges rendering * T - Type of the Gauge to be rendered (Currently RadialGauge, LinearGauge from the original library) * T2 - Type of config options used by the particular gauge (RadialGaugeOptions, LinearGaugeOptions) */ export declare abstract class BaseGauge<T extends CanvasGauges.BaseGauge, T2 extends CanvasGauges.GenericOptions> implements OnInit, AfterViewInit { private el; zone: NgZone; /** * Canvas element on the template used by the library to draw gauge element */ protected canvas: ElementRef; /** * A gauge instance responsible for rendering and updates on the canvas. * Subclasses should initialize in their ngOnInit implementation. */ protected gauge: T; /** * Flag indicating that OnViewInit life-cycle has completed */ private isInited; /** * value property of gauge prior to component view initialization */ private preInitValue; /** * options property of gauge prior to component view initialization */ private preInitOptions; /** * Listen for attribute changes, i.e., options properties that are stored * as attributes on this ElementRef */ private domListener; /** * * @param el - reference to the element of the whole component, used to scrape options declared on the component itself * @param zone - required to redraw gauge outside of Angular, due to animation lags caused by the ovewritten function of the ngZone */ constructor(el: ElementRef, zone: NgZone); /** * Subclasses should instantiate the CanvasGauge object in the child component */ abstract ngOnInit(): void; /** * Returns gauges properties as an options object. * Option properties consist of the attribute-based properties and those * explicitly set. * @returns <T2> */ /** * Assign gauge options at anytime in the lifecycle. * @param newOptions - assign the style and size properties */ options: T2; /** * Assign the value of the gauge visual indicator such as a needle or pointer * @param newValue the guage new value */ value: number; /** * Update the gauge options. Do not use until after OnViewInit() before using. * * Special implementation note - options.properties are maintained as * attribute name->value on this component's elementRef. Thus this method * maps each newOptions property onto the property's corresponding attribute. * The attribute update triggers a DOM mutation event which "this" listens for. * See #listenForDOMEvents() * * @param newOptions - the options to update the gauge */ update(newOptions: T2 | {}): void; /** * Perform gauge initialization. * Subclasses that override this method must this super version * for proper operation. */ ngAfterViewInit(): void; /** * Listen for attribute-change events that are created when updating * the options of this gauge. */ protected listenForDOMEvents(): void; /** * Discontinue listening for attribute change events. */ protected stopListeningForDOMEvents(): void; /** * Initalize the gauge with all options defined by attributes and * parent component options. */ protected initGauge(): void; /** * Performs the gauge update using the current options * @param options The options for the guage */ protected basicUpdate(options: T2): void; }