ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
135 lines (117 loc) • 3.84 kB
text/typescript
import { Component, Input, OnChanges, SimpleChanges, ViewEncapsulation } from '@angular/core';
import { toBoolean } from '../util/convert';
export class NzSliderMarksComponent implements OnChanges {
private _vertical = false;
private _included = false;
// Dynamic properties
nzLowerBound: number = null;
nzUpperBound: number = null;
nzMarksArray: MarksArray;
// Static properties
nzClassName: string;
nzMin: number; // Required
nzMax: number; // Required
set nzVertical(value: boolean) { // Required
this._vertical = toBoolean(value);
}
get nzVertical(): boolean {
return this._vertical;
}
set nzIncluded(value: boolean) {
this._included = toBoolean(value);
}
get nzIncluded(): boolean {
return this._included;
}
// TODO: using named interface
attrs: Array<{ id: number, value: number, offset: number, classes: { [key: string]: boolean }, style: object, label: Mark }>; // points for inner use
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzMarksArray) {
this.buildAttrs();
}
if (changes.nzMarksArray || changes.nzLowerBound || changes.nzUpperBound) {
this.togglePointActive();
}
}
trackById(index: number, attr: { id: number, value: number, offset: number, classes: { [key: string]: boolean }, style: object, label: Mark }): number {
return attr.id;
}
buildAttrs(): void {
const range = this.nzMax - this.nzMin;
this.attrs = this.nzMarksArray.map(mark => {
const { value, offset, config } = mark;
// calc styles
let label = config;
let style: object;
if (this.nzVertical) {
style = {
marginBottom: '-50%',
bottom : `${(value - this.nzMin) / range * 100}%`
};
} else {
const marksCount = this.nzMarksArray.length;
const unit = 100 / (marksCount - 1);
const markWidth = unit * 0.9;
style = {
width : `${markWidth}%`,
marginLeft: `${-markWidth / 2}%`,
left : `${(value - this.nzMin) / range * 100}%`
};
}
// custom configuration
if (typeof config === 'object') {
label = config.label;
if (config.style) {
style = { ...style, ...config.style };
}
}
return {
id : value,
value,
offset,
classes: {
[`${this.nzClassName}-text`]: true
},
style,
label
};
}); // END - map
}
togglePointActive(): void {
if (this.attrs && this.nzLowerBound !== null && this.nzUpperBound !== null) {
this.attrs.forEach(attr => {
const value = attr.value;
const isActive = (!this.nzIncluded && value === this.nzUpperBound) ||
(this.nzIncluded && value <= this.nzUpperBound && value >= this.nzLowerBound);
attr.classes[ `${this.nzClassName}-text-active` ] = isActive;
});
}
}
}
// DEFINITIONS
export type Mark = string | {
style: object;
label: string;
};
export class Marks {
number: Mark;
}
// TODO: extends Array could cause unexpected behavior when targeting es5 or below
export class MarksArray extends Array<{ value: number, offset: number, config: Mark }> {
[index: number]: {
value: number;
offset: number;
config: Mark;
}
}