x4js 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/application.d.ts +95 -0
- package/lib/application.js +137 -0
- package/lib/base64.d.ts +31 -0
- package/lib/base64.js +135 -0
- package/lib/base_component.d.ts +64 -0
- package/lib/base_component.js +77 -0
- package/lib/button.d.ts +145 -0
- package/lib/button.js +235 -0
- package/lib/calendar.d.ts +77 -0
- package/lib/calendar.js +236 -0
- package/lib/canvas.d.ts +88 -0
- package/lib/canvas.js +354 -0
- package/lib/cardview.d.ts +83 -0
- package/lib/cardview.js +152 -0
- package/lib/checkbox.d.ts +72 -0
- package/lib/checkbox.js +126 -0
- package/lib/color.d.ts +144 -0
- package/lib/color.js +584 -0
- package/lib/colorpicker.d.ts +98 -0
- package/lib/colorpicker.js +1457 -0
- package/lib/combobox.d.ts +97 -0
- package/lib/combobox.js +246 -0
- package/lib/component.d.ts +572 -0
- package/lib/component.js +1712 -0
- package/lib/datastore.d.ts +392 -0
- package/lib/datastore.js +986 -0
- package/lib/dialog.d.ts +171 -0
- package/lib/dialog.js +468 -0
- package/lib/dom_events.d.ts +284 -0
- package/lib/dom_events.js +13 -0
- package/lib/drag_manager.d.ts +26 -0
- package/lib/drag_manager.js +118 -0
- package/lib/drawtext.d.ts +43 -0
- package/lib/drawtext.js +261 -0
- package/lib/fileupload.d.ts +60 -0
- package/lib/fileupload.js +158 -0
- package/lib/form.d.ts +122 -0
- package/lib/form.js +293 -0
- package/lib/formatters.d.ts +31 -0
- package/lib/formatters.js +75 -0
- package/lib/gridview.d.ts +171 -0
- package/lib/gridview.js +786 -0
- package/lib/hosts/host.d.ts +44 -0
- package/lib/hosts/host.js +69 -0
- package/lib/i18n.d.ts +67 -0
- package/lib/i18n.js +169 -0
- package/lib/icon.d.ts +56 -0
- package/lib/icon.js +173 -0
- package/lib/image.d.ts +51 -0
- package/lib/image.js +149 -0
- package/lib/index.js +1 -0
- package/lib/input.d.ts +86 -0
- package/lib/input.js +172 -0
- package/lib/label.d.ts +54 -0
- package/lib/label.js +86 -0
- package/lib/layout.d.ts +77 -0
- package/lib/layout.js +261 -0
- package/lib/link.d.ts +46 -0
- package/lib/link.js +55 -0
- package/lib/listview.d.ts +173 -0
- package/lib/listview.js +532 -0
- package/lib/md5.d.ts +56 -0
- package/lib/md5.js +397 -0
- package/lib/menu.d.ts +122 -0
- package/lib/menu.js +276 -0
- package/lib/messagebox.d.ts +64 -0
- package/lib/messagebox.js +141 -0
- package/lib/panel.d.ts +42 -0
- package/lib/panel.js +61 -0
- package/lib/popup.d.ts +71 -0
- package/lib/popup.js +373 -0
- package/lib/property_editor.d.ts +67 -0
- package/lib/property_editor.js +247 -0
- package/lib/radiobtn.d.ts +68 -0
- package/lib/radiobtn.js +131 -0
- package/lib/rating.d.ts +49 -0
- package/lib/rating.js +93 -0
- package/lib/request.d.ts +48 -0
- package/lib/request.js +220 -0
- package/lib/router.d.ts +13 -0
- package/lib/router.js +27 -0
- package/lib/settings.d.ts +33 -0
- package/lib/settings.js +63 -0
- package/lib/sidebarview.d.ts +44 -0
- package/lib/sidebarview.js +73 -0
- package/lib/smartedit.d.ts +103 -0
- package/lib/smartedit.js +381 -0
- package/lib/spreadsheet.d.ts +214 -0
- package/lib/spreadsheet.js +1073 -0
- package/lib/styles.d.ts +81 -0
- package/lib/styles.js +262 -0
- package/lib/svgcomponent.d.ts +165 -0
- package/lib/svgcomponent.js +350 -0
- package/lib/tabbar.d.ts +41 -0
- package/lib/tabbar.js +66 -0
- package/lib/tabview.d.ts +45 -0
- package/lib/tabview.js +79 -0
- package/lib/textarea.d.ts +59 -0
- package/lib/textarea.js +119 -0
- package/lib/textedit.d.ts +118 -0
- package/lib/textedit.js +406 -0
- package/lib/texthiliter.d.ts +56 -0
- package/lib/texthiliter.js +219 -0
- package/lib/toaster.d.ts +38 -0
- package/lib/toaster.js +58 -0
- package/lib/tools.d.ts +382 -0
- package/lib/tools.js +1096 -0
- package/lib/tooltips.d.ts +42 -0
- package/lib/tooltips.js +148 -0
- package/lib/treeview.d.ts +128 -0
- package/lib/treeview.js +490 -0
- package/lib/x4_events.d.ts +253 -0
- package/lib/x4_events.js +363 -0
- package/package.json +21 -0
- package/src/README.md +2 -0
- package/src/application.ts +191 -0
- package/src/base64.ts +162 -0
- package/src/base_component.ts +118 -0
- package/src/button.ts +327 -0
- package/src/calendar.ts +312 -0
- package/src/canvas.ts +501 -0
- package/src/cardview.ts +220 -0
- package/src/checkbox.ts +178 -0
- package/src/color.ts +748 -0
- package/src/colorpicker.ts +1618 -0
- package/src/combobox.ts +348 -0
- package/src/component.ts +2330 -0
- package/src/datastore.ts +1318 -0
- package/src/dialog.ts +631 -0
- package/src/dom_events.ts +297 -0
- package/src/drag_manager.ts +168 -0
- package/src/drawtext.ts +342 -0
- package/src/fileupload.ts +208 -0
- package/src/form.ts +362 -0
- package/src/formatters.ts +96 -0
- package/src/gridview.ts +1051 -0
- package/src/hosts/electron.ts +161 -0
- package/src/hosts/host.ts +100 -0
- package/src/hosts/nwjs.ts +141 -0
- package/src/hosts/nwjs_types.ts +339 -0
- package/src/i18n.ts +205 -0
- package/src/icon.ts +237 -0
- package/src/image.ts +198 -0
- package/src/input.ts +236 -0
- package/src/label.ts +124 -0
- package/src/layout.ts +366 -0
- package/src/link.ts +82 -0
- package/src/listview.ts +749 -0
- package/src/md5.ts +432 -0
- package/src/menu.ts +394 -0
- package/src/messagebox.ts +199 -0
- package/src/panel.ts +81 -0
- package/src/popup.ts +488 -0
- package/src/property_editor.ts +333 -0
- package/src/radiobtn.ts +190 -0
- package/src/rating.ts +131 -0
- package/src/request.ts +296 -0
- package/src/router.ts +43 -0
- package/src/settings.ts +75 -0
- package/src/sidebarview.ts +97 -0
- package/src/smartedit.ts +532 -0
- package/src/spreadsheet.ts +1423 -0
- package/src/styles.ts +332 -0
- package/src/svgcomponent.ts +440 -0
- package/src/tabbar.ts +105 -0
- package/src/tabview.ts +106 -0
- package/src/textarea.ts +183 -0
- package/src/textedit.ts +535 -0
- package/src/texthiliter.ts +284 -0
- package/src/toaster.ts +76 -0
- package/src/tools.ts +1391 -0
- package/src/tooltips.ts +185 -0
- package/src/treeview.ts +670 -0
- package/src/x4.less +1940 -0
- package/src/x4_events.ts +558 -0
- package/tsconfig.json +14 -0
package/src/form.ts
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file form.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
* @license
|
|
11
|
+
* Copyright (c) 2019-2021 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* This program is free software; you can redistribute it and/or modify
|
|
14
|
+
* it under the terms of the GNU General Public License as published by
|
|
15
|
+
* the Free Software Foundation; either version 3 of the License, or
|
|
16
|
+
* (at your option) any later version.
|
|
17
|
+
*
|
|
18
|
+
* This program is distributed in the hope that it will be useful,
|
|
19
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21
|
+
* GNU General Public License for more details.
|
|
22
|
+
*
|
|
23
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
24
|
+
**/
|
|
25
|
+
|
|
26
|
+
import { Component, Container, CProps, ContainerEventMap, ComponentContent, flyWrap } from './component'
|
|
27
|
+
import { HLayout, VLayout } from './layout'
|
|
28
|
+
import { Button } from './button'
|
|
29
|
+
import { Input } from './input'
|
|
30
|
+
import { TextEdit } from './textedit'
|
|
31
|
+
import { ajaxRequest, RequestProps } from './request'
|
|
32
|
+
import { EventCallback } from './x4_events'
|
|
33
|
+
import { EvBtnClick } from './dialog'
|
|
34
|
+
|
|
35
|
+
import { _tr } from './i18n'
|
|
36
|
+
|
|
37
|
+
// ============================================================================
|
|
38
|
+
// [FORM]
|
|
39
|
+
// ============================================================================
|
|
40
|
+
|
|
41
|
+
export type FormBtn = 'ok' | 'cancel' | 'ignore' | 'yes' | 'no' | 'close' | 'save' | 'dontsave';
|
|
42
|
+
export type FormButtons = (FormBtn | Button | Component)[];
|
|
43
|
+
|
|
44
|
+
export interface FormEventMap extends ContainerEventMap {
|
|
45
|
+
btnClick?: EvBtnClick;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface FormProps extends CProps<FormEventMap> {
|
|
49
|
+
disableSuggestions?: boolean;
|
|
50
|
+
buttons?: FormButtons;
|
|
51
|
+
btnClick?: EventCallback<EvBtnClick>; // shortcut for events: { btnClick: ... }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
export class Form extends VLayout<FormProps, FormEventMap>
|
|
59
|
+
{
|
|
60
|
+
protected m_height: string | number;
|
|
61
|
+
protected m_container: Container;
|
|
62
|
+
protected m_buttons: HLayout;
|
|
63
|
+
protected m_dirty: boolean;
|
|
64
|
+
protected m_watchChanges: boolean;
|
|
65
|
+
|
|
66
|
+
constructor(props: FormProps) {
|
|
67
|
+
|
|
68
|
+
let content = props.content;
|
|
69
|
+
props.content = null;
|
|
70
|
+
|
|
71
|
+
// save height, because real form height is 'height' PLUS button bar height
|
|
72
|
+
let height = props.height;
|
|
73
|
+
props.height = undefined;
|
|
74
|
+
|
|
75
|
+
super(props);
|
|
76
|
+
|
|
77
|
+
this.setProp('tag', props.disableSuggestions ? 'section' : 'form');
|
|
78
|
+
this.mapPropEvents(props, 'btnClick');
|
|
79
|
+
this.updateContent(content, props.buttons, height);
|
|
80
|
+
|
|
81
|
+
this.m_dirty = false;
|
|
82
|
+
this.m_watchChanges = false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* returns the container object
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
get container(): Container {
|
|
90
|
+
return this.m_container;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
override componentCreated(): void {
|
|
98
|
+
super.componentCreated()
|
|
99
|
+
if (this.m_watchChanges) {
|
|
100
|
+
this.watchChanges();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
public updateContent(items: ComponentContent, buttons: FormButtons, height: string | number = 0) {
|
|
109
|
+
|
|
110
|
+
if (height) {
|
|
111
|
+
// keep height for next time
|
|
112
|
+
this.m_height = height;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this._makeButtons(buttons);
|
|
116
|
+
|
|
117
|
+
let content = [
|
|
118
|
+
this.m_container = new VLayout({
|
|
119
|
+
cls: 'container',
|
|
120
|
+
height: this.m_height,
|
|
121
|
+
content: items
|
|
122
|
+
}),
|
|
123
|
+
this.m_buttons,
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
super.setContent(content);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param els
|
|
132
|
+
* @param refreshAll
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
setContent(els: ComponentContent, refreshAll = true) {
|
|
136
|
+
this.m_container.setContent(els, refreshAll);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param buttons
|
|
142
|
+
*/
|
|
143
|
+
setButtons(buttons: FormButtons) {
|
|
144
|
+
this._makeButtons(buttons);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* enable a button by it's name
|
|
149
|
+
*/
|
|
150
|
+
|
|
151
|
+
enableButton(name: string, enable = true) {
|
|
152
|
+
let button = this.getButton(name);
|
|
153
|
+
if (button) {
|
|
154
|
+
button.enable(enable);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* return a button by it's name
|
|
160
|
+
* @param name
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
getButton(name: string) {
|
|
164
|
+
let button = this.m_buttons?.itemWithRef<Button>('@' + name);
|
|
165
|
+
return button;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
*
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
private _makeButtons(buttons?: FormButtons): void {
|
|
174
|
+
|
|
175
|
+
if (!this.m_buttons) {
|
|
176
|
+
this.m_buttons = new HLayout({
|
|
177
|
+
cls: 'footer',
|
|
178
|
+
ref: 'buttons',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let btns: Component[] = [];
|
|
183
|
+
|
|
184
|
+
if (buttons) {
|
|
185
|
+
for (let b of buttons) {
|
|
186
|
+
if (b instanceof Component) {
|
|
187
|
+
btns.push(b);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
switch (b) {
|
|
191
|
+
case 'ok': { btns.push(new Button({ ref: '@' + b, text: _tr.global.ok, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
192
|
+
case 'cancel': { btns.push(new Button({ ref: '@' + b, text: _tr.global.cancel, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
193
|
+
case 'ignore': { btns.push(new Button({ ref: '@' + b, text: _tr.global.ignore, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
194
|
+
case 'yes': { btns.push(new Button({ ref: '@' + b, text: _tr.global.yes, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
195
|
+
case 'no': { btns.push(new Button({ ref: '@' + b, text: _tr.global.no, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
196
|
+
case 'close': { btns.push(new Button({ ref: '@' + b, text: _tr.global.close, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
197
|
+
case 'save': { btns.push(new Button({ ref: '@' + b, text: _tr.global.save, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
198
|
+
case 'dontsave': { btns.push(new Button({ ref: '@' + b, text: _tr.global.dontsave, click: () => { this._click(<FormBtn>b); } })); break; }
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (btns.length == 1) {
|
|
205
|
+
btns[0].setAttribute('autofocus', true);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
this.m_buttons.setContent(btns);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
*
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
public validate() {
|
|
216
|
+
let inputs = this.queryAll('input'),
|
|
217
|
+
result = true;
|
|
218
|
+
|
|
219
|
+
for (let i = 0; i < inputs.length; i++) {
|
|
220
|
+
let input = Component.getElement(inputs[i], TextEdit);
|
|
221
|
+
if (input && !input.validate()) {
|
|
222
|
+
result = false;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return result;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
*
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
private _click(btn: FormBtn) {
|
|
234
|
+
this.emit('btnClick', EvBtnClick(btn));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* replacement for HTMLFormElement.elements
|
|
239
|
+
* as chrome shows suggestions on form elements even if we ask him (not to do that)
|
|
240
|
+
* we removed <form> element.
|
|
241
|
+
* so we have to get children by hand
|
|
242
|
+
*/
|
|
243
|
+
|
|
244
|
+
private _getElements() {
|
|
245
|
+
console.assert(!!this.dom);
|
|
246
|
+
const els = this.queryAll('[name]');
|
|
247
|
+
return els;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
*
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
public setValues(values: any) {
|
|
255
|
+
|
|
256
|
+
let elements = this._getElements();
|
|
257
|
+
for (let e = 0; e < elements.length; e++) {
|
|
258
|
+
|
|
259
|
+
let input = <HTMLInputElement>elements[e];
|
|
260
|
+
|
|
261
|
+
let item = Component.getElement(input);
|
|
262
|
+
if (!item.hasAttribute("name")) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let name = item.getAttribute('name'),
|
|
267
|
+
type = item.getAttribute('type');
|
|
268
|
+
|
|
269
|
+
if (values[name] !== undefined) {
|
|
270
|
+
(<Input>item).setStoreValue(values[name]);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
this.setDirty(false);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* values are not escaped
|
|
280
|
+
* checkbox set true when checked
|
|
281
|
+
* radio set value when checked
|
|
282
|
+
*/
|
|
283
|
+
|
|
284
|
+
public getValues(): any {
|
|
285
|
+
let elements = this._getElements();
|
|
286
|
+
let result = {};
|
|
287
|
+
|
|
288
|
+
for (let e = 0; e < elements.length; e++) {
|
|
289
|
+
|
|
290
|
+
let el = <HTMLElement>elements[e];
|
|
291
|
+
let item = <Input>Component.getElement(el);
|
|
292
|
+
if (!item.hasAttribute("name")) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
let name = item.getAttribute('name'),
|
|
297
|
+
value = item.getStoreValue();
|
|
298
|
+
|
|
299
|
+
if (value !== undefined) {
|
|
300
|
+
result[name] = value;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return result;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* send the query to the desired handler
|
|
309
|
+
*/
|
|
310
|
+
|
|
311
|
+
public submit(cfg: RequestProps, cbvalidation: Function) {
|
|
312
|
+
|
|
313
|
+
if (!this.validate()) {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
let values = this.getValues();
|
|
318
|
+
if (cbvalidation) {
|
|
319
|
+
if (!cbvalidation(values)) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let form = new FormData();
|
|
325
|
+
for (let n in values) {
|
|
326
|
+
if (values.hasOwnProperty(n)) {
|
|
327
|
+
form.append(n, values[n] === undefined ? '' : values[n]);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
cfg.params = form;
|
|
332
|
+
return ajaxRequest(cfg);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
*
|
|
337
|
+
*/
|
|
338
|
+
|
|
339
|
+
watchChanges() {
|
|
340
|
+
if (this.dom) {
|
|
341
|
+
const els = this.queryAll('input[name], textarea[name]');
|
|
342
|
+
els.forEach(el => {
|
|
343
|
+
flyWrap<Input>(el).setDomEvent('input', () => {
|
|
344
|
+
this.setDirty();
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
this.m_watchChanges = false;
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
this.m_watchChanges = true;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
setDirty(set = true) {
|
|
356
|
+
this.m_dirty = set;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
isDirty() {
|
|
360
|
+
return this.m_dirty;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file formatters.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
* @license
|
|
11
|
+
* Copyright (c) 2019-2021 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* This program is free software; you can redistribute it and/or modify
|
|
14
|
+
* it under the terms of the GNU General Public License as published by
|
|
15
|
+
* the Free Software Foundation; either version 3 of the License, or
|
|
16
|
+
* (at your option) any later version.
|
|
17
|
+
*
|
|
18
|
+
* This program is distributed in the hope that it will be useful,
|
|
19
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21
|
+
* GNU General Public License for more details.
|
|
22
|
+
*
|
|
23
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
24
|
+
**/
|
|
25
|
+
|
|
26
|
+
import { formatIntlDate, roundTo } from './tools'
|
|
27
|
+
|
|
28
|
+
export type FormatFunc = (input: any, rec: any) => string;
|
|
29
|
+
|
|
30
|
+
let locale: string;
|
|
31
|
+
let moneyFmt: Intl.NumberFormat;
|
|
32
|
+
|
|
33
|
+
export function setCurrencySymbol( symbol: string | null ) {
|
|
34
|
+
|
|
35
|
+
if( symbol ) {
|
|
36
|
+
moneyFmt = new Intl.NumberFormat( locale, { style: 'currency', currency: symbol /*, currencyDisplay: 'symbol'*/ } );
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
moneyFmt = new Intl.NumberFormat( locale, { style: 'decimal', useGrouping: true, minimumFractionDigits: 2, maximumFractionDigits: 2 } );
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function sql_date_formatter(input: any): string {
|
|
44
|
+
|
|
45
|
+
if (input === null || input === undefined || input === '') {
|
|
46
|
+
return '';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let dte = new Date(Date.parse(input));
|
|
50
|
+
|
|
51
|
+
//todo: better implementation
|
|
52
|
+
const options: Intl.DateTimeFormatOptions = { /*weekday: 'short',*/ month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
|
|
53
|
+
return dte.toLocaleDateString(locale, options)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function date_formatter(input: any): string {
|
|
57
|
+
|
|
58
|
+
if (input === null || input === undefined || input === '') {
|
|
59
|
+
return '';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let dte: Date = typeof (input) == 'string' ? new Date(Date.parse(input)) : input;
|
|
63
|
+
return formatIntlDate(dte);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function money_formatter(input: any): string {
|
|
67
|
+
|
|
68
|
+
if (input === null || input === undefined || input === '') {
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let val: number = roundTo(typeof (input) == 'string' ? parseFloat(input) : input, 2);
|
|
73
|
+
if (val === -0.00) val = 0.00;
|
|
74
|
+
|
|
75
|
+
let res = moneyFmt.format(val);
|
|
76
|
+
return res;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function money_formatter_nz(input: any): string {
|
|
80
|
+
|
|
81
|
+
if (input === null || input === undefined || input === '') {
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let val: number = roundTo(typeof (input) == 'string' ? parseFloat(input) : input, 2);
|
|
86
|
+
if (!val) { // do not show zeros
|
|
87
|
+
return '';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let res = moneyFmt.format(val);
|
|
91
|
+
return res;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function bool_formatter(input: any): string {
|
|
95
|
+
return input ? 'oui' : '-';
|
|
96
|
+
}
|