/
dorf-css-classes.ts
231 lines (211 loc) · 6.12 KB
/
dorf-css-classes.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { DorfConfigService } from '../dorf-config.service';
import { IDorfDefinitionBase } from '../fields/base/abstract-dorf-field.definition';
// TODO: objects support as in NgClass from Angular - allowing objects to be passed
/**
* General type, used in DORF CSS interfaces.
* Classes should be passed as string values with spaces, but some definitions, like e.g. buttons, are nested within other definitions.
*/
export type NgDorfClassType = string | IDorfCommonCssClasses | undefined;
/**
* Common part of CSS classes. Interfaces are not strict, so additional fields may be defined and used in DORF extensions.
*
* @stable
*/
export interface IDorfCommonCssClasses {
/**
* Classes to be used with custom templates.
*/
[key: string]: NgDorfClassType;
}
/**
* Represents CSS classes for DORF buttons.
*
* @stable
*/
export interface IDorfButtonsCssClasses extends IDorfCommonCssClasses {
/**
* Classes for a section which groups all the buttons.
*/
group?: NgDorfClassType;
/**
* Classes for 'Save' button.
*/
save?: NgDorfClassType;
/**
* Classes for 'Reset' button.
*/
reset?: NgDorfClassType;
}
/**
* Represents CSS classes for typical things connected with DORF field.
* Should be used e.g. when including [DORF module]{@link DorfModule}.
*
* There are various places, where CSS classes from this interface may be applied.
* <ol>
* <li>General, {@link DorfConfigService} level - applicable only if no other styles defined</li>
* <li>Field level within the `dorfFields` array from {@link DorfConfigService}
* <li>Definition level - the closest to the field (the highest priority)</li>
* </ol>
*
* @example
* ```
*
* //
* DorfModule.forRoot({
* css: {
* wrapper: "form-group",
* error: "error-message",
* label: "control-label",
* htmlField: "form-control"
* },
* dorfFields: [{
* tag: DorfField.CHECKBOX,
* css: {
* htmlField: "no-class",
* label: "checkbox-inline"
* }
* }, {
* tag: DorfField.RADIO,
* css: {
* htmlField: "radio-inline",
* label: "checkbox-inline"
* }
* }]
* })
* ```
*
* @stable
*/
export interface IDorfFieldCssClasses extends IDorfCommonCssClasses {
/**
* Classes for wrapper, which groups all field elements (label, control, error).
*/
wrapper?: NgDorfClassType;
/**
* Classes for a label.
*/
label?: NgDorfClassType;
/**
* Classes assigned to the [`dorf-field`]{@link DorfFieldComponent}, which stores one of the fields at a time.
*/
fieldGeneralization?: NgDorfClassType;
/**
* Classes assigned to the concrete field component, under [`dorf-field`]{@link DorfFieldComponent}, e.g. to `dorf-input`.
*/
dorfField?: NgDorfClassType;
/**
* Classes for the HTML control.
*/
htmlField?: NgDorfClassType;
/**
* Classes for the error, below the field.
*/
error?: NgDorfClassType;
}
/**
* Some fields, like radio and checkbox, may have 2 labels and corresponding CSS classes.
*
* @stable
*/
export interface IDorfMultipleLabelsCssClasses extends IDorfFieldCssClasses {
/**
* Classes for a label which is around the field. Used especially with a checkbox or radio buttons.
*/
innerLabel?: NgDorfClassType;
}
/**
* Classes for the form (like fieldset, legend elements) and the fallback classes for fields (applicable if nowhere else defined).
*
* @stable
*/
export interface IDorfGeneralCssClasses extends IDorfMultipleLabelsCssClasses {
/**
* CSS classes for a whole form.
*/
form?: NgDorfClassType;
/**
* CSS classes for a fieldset around all the form fields.
*/
fieldset?: NgDorfClassType;
/**
* CSS classes for a legend under the fieldset.
*/
legend?: NgDorfClassType;
/**
* CSS classes for a group of DORF wrapping divs (each wrapping div contains HTML field, label and error).
* Classes assigned here are usually connected with a grid system (e.g. `'row'` class from Bootstrap).
*/
section?: NgDorfClassType;
}
/**
* The most general set of classes, used for the form component and for the fallbacks.
* Form, fieldset, section and button classes are defined just once, during adding [DORF module]{@link DorfModule}.
*
* @example
* ```
*
* //
* DorfModule.forRoot({
* css: {
* wrapper: "form-group",
* error: "error-message",
* label: "control-label",
* htmlField: "form-control",
* form: 'pure-form pure-form-aligned',
* buttons: {
* save: 'pure-button pure-button-primary',
* reset: 'hidden',
* group: 'pure-controls'
* }
* },
* dorfFields: [{
* tag: DorfField.CHECKBOX,
* css: {
* htmlField: "no-class",
* label: "checkbox-inline"
* }
* }, {
* tag: DorfField.RADIO,
* css: {
* htmlField: "radio-inline",
* label: "checkbox-inline"
* }
* }]
* })
* ```
*
* @stable
*/
export interface IDorfGeneralWithButtonsCssClasses extends IDorfGeneralCssClasses {
/**
* CSS classes for various buttons connected with form.
*/
buttons?: IDorfButtonsCssClasses;
}
/**
* Implementation of {@link IDorfCommonCssClasses} for a fast creation with an empty values.
*
* @stable
*/
export class DorfCssClasses implements IDorfCommonCssClasses {
/** @inheritdoc */
[key: string]: NgDorfClassType;
constructor(options?: IDorfCommonCssClasses) {
let populateOptions = (target: IDorfCommonCssClasses, opts?: IDorfCommonCssClasses) => {
if (opts) {
for (let cls in opts) {
if (!target.hasOwnProperty(cls)) {
let val = opts[cls];
if (typeof val === 'string') {
target[cls] = val;
} else {
target[cls] = {};
populateOptions(target[cls] as IDorfCommonCssClasses, val);
}
}
}
}
}
populateOptions(this, options);
}
}