This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 764
/
layout.service.ts
60 lines (48 loc) · 1.6 KB
/
layout.service.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
/*
* Copyright (c) 2016-2019 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { Injectable } from '@angular/core';
export enum Layouts {
VERTICAL = 'vertical',
HORIZONTAL = 'horizontal',
COMPACT = 'compact',
}
@Injectable()
export class LayoutService {
readonly minLabelSize = 1;
readonly maxLabelSize = 12;
layout: Layouts = Layouts.HORIZONTAL;
// This is basically a replacement for Object.values(), which IE11 and Node <9 don't support :(
// String enums cannot be reverse-mapped, meaning Layouts['COMPACT'] does not return 'compact' so
// this exists to deal with this little caveat to get the list of the values as an array.
private layoutValues: string[] = Object.keys(Layouts).map(key => Layouts[key]);
private _labelSize: number = 2;
set labelSize(size: number) {
if (this.labelSizeIsValid(size)) {
this._labelSize = size;
}
}
get labelSize(): number {
return this._labelSize;
}
isVertical(): boolean {
return this.layout === Layouts.VERTICAL;
}
isHorizontal(): boolean {
return this.layout === Layouts.HORIZONTAL;
}
isCompact(): boolean {
return this.layout === Layouts.COMPACT;
}
get layoutClass(): string {
return `clr-form-${this.layout}`;
}
isValid(layout: string): boolean {
return this.layoutValues.indexOf(layout) > -1;
}
labelSizeIsValid(labelSize: number): boolean {
return Number.isInteger(labelSize) && labelSize >= this.minLabelSize && labelSize <= this.maxLabelSize;
}
}