/
view.component.ts
106 lines (93 loc) · 2.72 KB
/
view.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Host,
Input,
OnChanges,
Optional,
Renderer2,
TemplateRef,
ViewChild,
ViewEncapsulation,
} from '@angular/core';
import { ResponsiveService } from '@delon/theme';
import { isEmpty, InputBoolean, InputNumber } from '@delon/util';
import { SVContainerComponent } from './view-container.component';
const prefixCls = `sv`;
@Component({
selector: 'sv, [sv]',
exportAs: 'sv',
templateUrl: './view.component.html',
host: {
'[style.padding-left.px]': 'paddingValue',
'[style.padding-right.px]': 'paddingValue',
},
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class SVComponent implements AfterViewInit, OnChanges {
@ViewChild('conEl', { static: false })
private conEl: ElementRef;
private el: HTMLElement;
private clsMap: string[] = [];
// #region fields
@Input() optional: string | TemplateRef<void>;
@Input() optionalHelp: string | TemplateRef<void>;
@Input() label: string | TemplateRef<void>;
@Input() unit: string | TemplateRef<void>;
@Input() @InputNumber(null) col: number;
@Input() @InputBoolean(null) default: boolean;
@Input() type: 'primary' | 'success' | 'danger' | 'warning';
// #endregion
get paddingValue(): number {
return this.parent && this.parent.gutter / 2;
}
get labelWidth(): number | null {
const { labelWidth, layout } = this.parent;
return layout === 'horizontal' ? labelWidth : null;
}
constructor(
el: ElementRef,
@Host() @Optional() public parent: SVContainerComponent,
private rep: ResponsiveService,
private ren: Renderer2,
) {
if (parent == null) {
throw new Error(`[sv] must include 'sv-container' component`);
}
this.el = el.nativeElement;
}
private setClass() {
const { el, ren, col, clsMap, type, rep } = this;
clsMap.forEach(cls => ren.removeClass(el, cls));
clsMap.length = 0;
clsMap.push(...rep.genCls(col != null ? col : this.parent.col));
clsMap.push(`${prefixCls}__item`);
if (this.parent.labelWidth) clsMap.push(`${prefixCls}__item-fixed`);
if (type) clsMap.push(`${prefixCls}__type-${type}`);
clsMap.forEach(cls => ren.addClass(el, cls));
}
ngAfterViewInit() {
this.setClass();
this.checkContent();
}
ngOnChanges() {
this.setClass();
}
checkContent() {
const { conEl } = this;
const def = this.default;
if (!(def != null ? def : this.parent.default)) return;
const el = conEl.nativeElement as HTMLElement;
const cls = `sv__default`;
if (el.classList.contains(cls)) {
el.classList.remove(cls);
}
if (isEmpty(el)) {
el.classList.add(cls);
}
}
}