1
|
|
|
import { Inject, Injectable } from '@nestjs/common'; |
2
|
|
|
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator'; |
3
|
|
|
import { ITemplates } from 'src/Infrastructure/Templates/ITemplates'; |
4
|
|
|
import { ICell } from '.'; |
5
|
|
|
import { htmlToText } from '../Common/Utils/formatUtils'; |
6
|
|
|
|
7
|
|
|
class ValueCell implements ICell { |
8
|
|
|
public readonly name = 'scalar'; |
9
|
|
|
|
10
|
|
|
constructor(private readonly value: any) {} |
11
|
|
|
|
12
|
|
|
public renderHtml(): string { |
13
|
|
|
return this.renderText(); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public renderText(): string { |
17
|
|
|
return this.value.toString(); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
class TemplateCell implements ICell { |
22
|
|
|
public readonly name = 'template'; |
23
|
|
|
|
24
|
|
|
constructor( |
25
|
|
|
private readonly templateName: string, |
26
|
|
|
private readonly context: object, |
27
|
|
|
private readonly templates: ITemplates |
28
|
|
|
) {} |
29
|
|
|
|
30
|
|
|
public renderHtml(): string { |
31
|
|
|
return this.templates.render(this.templateName, this.context); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public renderText(): string { |
35
|
|
|
return htmlToText(this.renderHtml()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
export type ActionsOptions = { |
40
|
|
|
view?: { |
41
|
|
|
url: string; |
42
|
|
|
}; |
43
|
|
|
edit?: { |
44
|
|
|
url: string; |
45
|
|
|
}; |
46
|
|
|
delete?: { |
47
|
|
|
url: string; |
48
|
|
|
}; |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
class ActionsCell implements ICell { |
52
|
|
|
public readonly name = 'actions'; |
53
|
|
|
|
54
|
|
|
constructor( |
55
|
|
|
private readonly options: ActionsOptions, |
56
|
|
|
private readonly templates: ITemplates |
57
|
|
|
) {} |
58
|
|
|
|
59
|
|
|
public renderHtml(): string { |
60
|
|
|
return this.templates.render('tables/cells/actions.njk', { |
61
|
|
|
actions: this.options |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public renderText(): string { |
66
|
|
|
throw new Error('Does not render to text'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
class TransCell implements ICell { |
71
|
|
|
public readonly name = 'trans'; |
72
|
|
|
|
73
|
|
|
constructor( |
74
|
|
|
public readonly message: string, |
75
|
|
|
public readonly params: object, |
76
|
|
|
private readonly translator: ITranslator |
77
|
|
|
) {} |
78
|
|
|
|
79
|
|
|
public renderHtml(): string { |
80
|
|
|
return this.renderText(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public renderText(): string { |
84
|
|
|
return this.translator.translate(this.message, this.params); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
@Injectable() |
89
|
|
|
export class RowFactory { |
90
|
|
|
constructor( |
91
|
|
|
@Inject('ITranslator') |
92
|
|
|
private readonly translator: ITranslator, |
93
|
|
|
@Inject('ITemplates') |
94
|
|
|
private readonly templates: ITemplates |
95
|
|
|
) {} |
96
|
|
|
|
97
|
|
|
public createBuilder(): RowBuilder { |
98
|
|
|
return new RowBuilder(this.translator, this.templates); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
export class RowBuilder { |
103
|
|
|
private cells: ICell[]; |
104
|
|
|
|
105
|
|
|
constructor( |
106
|
|
|
private readonly translator: ITranslator, |
107
|
|
|
private readonly templates: ITemplates |
108
|
|
|
) { |
109
|
|
|
this.cells = []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public apply(cb: (b: RowBuilder) => void): RowBuilder { |
113
|
|
|
cb(this); |
114
|
|
|
return this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public value(value: string | number): RowBuilder { |
118
|
|
|
this.cells.push(new ValueCell(value)); |
119
|
|
|
return this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public template(name: string, context: object): RowBuilder { |
123
|
|
|
this.cells.push(new TemplateCell(name, context, this.templates)); |
124
|
|
|
return this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public trans(message: string, params?: object): RowBuilder { |
128
|
|
|
this.cells.push(new TransCell(message, params, this.translator)); |
129
|
|
|
return this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public picto(picto: string, message: string, attr: any = {}): RowBuilder { |
133
|
|
|
this.cells.push( |
134
|
|
|
new TemplateCell( |
135
|
|
|
'tables/cells/picto.njk', |
136
|
|
|
{ message, picto, attr }, |
137
|
|
|
this.templates |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
return this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public actions(options: ActionsOptions): RowBuilder { |
144
|
|
|
this.cells.push(new ActionsCell(options, this.templates)); |
145
|
|
|
return this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public build(): ICell[] { |
149
|
|
|
return this.cells; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|