Add gradient service
This commit is contained in:
parent
d8730e77ad
commit
b850f5d621
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "farmmaps-lib-app",
|
"name": "farmmaps-lib-app",
|
||||||
"version": "2.0.3",
|
"version": "2.0.4",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"ng": "ng",
|
"ng": "ng",
|
||||||
"start": "ng serve",
|
"start": "ng serve",
|
||||||
|
@ -35,6 +35,7 @@ import { PackageService } from './services/package.service';
|
|||||||
import { PackagePreloadStrategy } from './services/package-preload-strategy.service';
|
import { PackagePreloadStrategy } from './services/package-preload-strategy.service';
|
||||||
import { SenmlService } from './services/senml-service';
|
import { SenmlService } from './services/senml-service';
|
||||||
import { DeviceService } from './services/device.service';
|
import { DeviceService } from './services/device.service';
|
||||||
|
import { GradientService} from './services/gradient.service';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
FolderService,
|
FolderService,
|
||||||
@ -61,7 +62,8 @@ export {
|
|||||||
PackageService,
|
PackageService,
|
||||||
SenmlService,
|
SenmlService,
|
||||||
PackagePreloadStrategy,
|
PackagePreloadStrategy,
|
||||||
DeviceService
|
DeviceService,
|
||||||
|
GradientService
|
||||||
};
|
};
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Component, Input, OnInit ,OnChanges, SimpleChanges,ChangeDetectorRef} from '@angular/core';
|
import { Component, Input, OnInit ,OnChanges, SimpleChanges,ChangeDetectorRef} from '@angular/core';
|
||||||
import { IItem} from '../../models/item'
|
import { IItem} from '../../models/item'
|
||||||
import { IGradientstop } from '../../models/gradient';
|
import { IGradientstop } from '../../models/gradient';
|
||||||
|
import { GradientService } from '../../common-service.module';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -14,23 +15,12 @@ export class GradientComponent implements OnInit,OnChanges {
|
|||||||
@Input() gradientItem:IItem;
|
@Input() gradientItem:IItem;
|
||||||
public gradientStyle:any = {};
|
public gradientStyle:any = {};
|
||||||
|
|
||||||
constructor(private ref: ChangeDetectorRef) { }
|
constructor(private ref: ChangeDetectorRef,private gradientService:GradientService) { }
|
||||||
|
|
||||||
getGradientStyle(item:IItem):any {
|
getGradientStyle(item:IItem):any {
|
||||||
if(item.data && item.data.gradient) {
|
if(item.data && item.data.gradient) {
|
||||||
let gradient = item.data.gradient as IGradientstop[];
|
let gradient = item.data.gradient as IGradientstop[];
|
||||||
let gd = '{ "background": "linear-gradient(to right,';
|
return this.gradientService.getGradientStyle(gradient);
|
||||||
for(var i=0;i<gradient.length;i++) {
|
|
||||||
let gs = gradient[i];
|
|
||||||
if(i>0) gd+=",";
|
|
||||||
gd += `rgba(${gs.color.red},${gs.color.green},${gs.color.blue},${gs.color.alpha/255})`;
|
|
||||||
gd +=` ${gs.relativestop*100}%`
|
|
||||||
}
|
|
||||||
gradient.forEach((gs) => {
|
|
||||||
});
|
|
||||||
gd+=')"}';
|
|
||||||
|
|
||||||
return JSON.parse(gd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,5 +39,4 @@ export class GradientComponent implements OnInit,OnChanges {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
import {AfterViewInit, Directive, Input, OnDestroy, TemplateRef, ViewContainerRef} from '@angular/core';
|
||||||
|
import {Subscription} from 'rxjs';
|
||||||
|
import {PackageService} from '../../services/package.service';
|
||||||
|
import * as appCommonReducer from '../../reducers/app-common.reducer';
|
||||||
|
import {Store} from '@ngrx/store';
|
||||||
|
|
||||||
|
@Directive({
|
||||||
|
selector: '[fmPackageExists]',
|
||||||
|
})
|
||||||
|
export class PackageExistsDirective implements OnDestroy, AfterViewInit {
|
||||||
|
|
||||||
|
@Input()
|
||||||
|
set fmPackageExists(packageIdentifier: string) {
|
||||||
|
this.packageIdentifier = packageIdentifier;
|
||||||
|
this.updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Input()
|
||||||
|
set fmPackageExistsThen(thenTemplate: TemplateRef<any>) {
|
||||||
|
this.thenTemplate = thenTemplate;
|
||||||
|
this.updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Input()
|
||||||
|
set fmPackageExistsElse(thenTemplate: TemplateRef<any>) {
|
||||||
|
this.elseTemplate = thenTemplate;
|
||||||
|
this.updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
private packageIdentifier: string;
|
||||||
|
private thenTemplate: TemplateRef<any>;
|
||||||
|
private elseTemplate: TemplateRef<any>;
|
||||||
|
private packSub: Subscription;
|
||||||
|
|
||||||
|
constructor(private hostTemplateRef: TemplateRef<any>, private viewContainerRef: ViewContainerRef,
|
||||||
|
private store: Store<appCommonReducer.State>, private packageService: PackageService) {
|
||||||
|
this.thenTemplate = hostTemplateRef;
|
||||||
|
this.packSub = this.store.select(appCommonReducer.SelectGetPackages).subscribe((_) => {
|
||||||
|
this.updateView();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ngAfterViewInit() {
|
||||||
|
this.updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateView() {
|
||||||
|
this.viewContainerRef.clear();
|
||||||
|
if (this.packageService.packageExists(this.packageIdentifier)) {
|
||||||
|
this.viewContainerRef.createEmbeddedView(this.thenTemplate);
|
||||||
|
} else if (this.elseTemplate) {
|
||||||
|
this.viewContainerRef.createEmbeddedView(this.elseTemplate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
if (this.packSub) {
|
||||||
|
this.packSub.unsubscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user