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/drawtext.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file drawtext.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 { deferCall, Point, Rect, roundTo } from './tools'
|
|
27
|
+
|
|
28
|
+
// adapted & modified from Canvas-txt:
|
|
29
|
+
// https://github.com/geongeorge/Canvas-Txt/blob/master/src/index.js
|
|
30
|
+
|
|
31
|
+
// Hair space character for precise justification
|
|
32
|
+
const SPACE = '\u200a';
|
|
33
|
+
|
|
34
|
+
export interface DrawTextStyle {
|
|
35
|
+
align?: 'left' | 'center' | 'right' | 'justify',
|
|
36
|
+
vAlign?: 'top' | 'middle' | 'bottom',
|
|
37
|
+
fontSize?: number, // in pixels
|
|
38
|
+
fontWeight?: number | 'bold' | 'light' | 'normal', // in em (fontHeight)
|
|
39
|
+
fontStyle?: string,
|
|
40
|
+
fontVariant?: string,
|
|
41
|
+
fontFamily?: string,
|
|
42
|
+
lineHeight?: number,
|
|
43
|
+
clip?: boolean,
|
|
44
|
+
columns?: number;
|
|
45
|
+
columnGap?: number;
|
|
46
|
+
lineBreak?: boolean;
|
|
47
|
+
rotation?: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const defStyle: DrawTextStyle = {
|
|
51
|
+
align: 'center',
|
|
52
|
+
vAlign: 'middle',
|
|
53
|
+
fontSize: 14,
|
|
54
|
+
fontWeight: null,
|
|
55
|
+
fontStyle: '',
|
|
56
|
+
fontVariant: '',
|
|
57
|
+
fontFamily: 'Arial',
|
|
58
|
+
lineHeight: 0,
|
|
59
|
+
clip: true,
|
|
60
|
+
columns: 1,
|
|
61
|
+
columnGap: 0,
|
|
62
|
+
lineBreak: true,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
interface ITextWord {
|
|
70
|
+
width: number; // word width
|
|
71
|
+
text: string; // text to print
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface ITextLine {
|
|
75
|
+
width: number; // line width
|
|
76
|
+
words: ITextWord[]; // words in line
|
|
77
|
+
space: number; // space between words
|
|
78
|
+
last?: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function drawText(ctx: CanvasRenderingContext2D, input_Text: string, rc: Rect, drawStyle: DrawTextStyle) {
|
|
82
|
+
|
|
83
|
+
if (rc.width <= 0 || rc.height <= 0) {
|
|
84
|
+
//width or height or font size cannot be 0
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//console.time( 'drawtext' );
|
|
89
|
+
|
|
90
|
+
drawStyle = { ...defStyle, ...drawStyle };
|
|
91
|
+
|
|
92
|
+
ctx.save();
|
|
93
|
+
|
|
94
|
+
if (drawStyle.clip) {
|
|
95
|
+
ctx.beginPath();
|
|
96
|
+
ctx.rect(rc.left, rc.top, rc.width, rc.height);
|
|
97
|
+
ctx.clip();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if( drawStyle.rotation ) {
|
|
101
|
+
const center = new Point(rc.left+rc.width/2, rc.top+rc.height/2);
|
|
102
|
+
const rad = drawStyle.rotation / 180 * Math.PI;
|
|
103
|
+
|
|
104
|
+
ctx.translate( center.x, center.y );
|
|
105
|
+
ctx.rotate( rad );
|
|
106
|
+
ctx.translate( -center.x, -center.y );
|
|
107
|
+
|
|
108
|
+
//ctx.beginPath();
|
|
109
|
+
//ctx.rect( rc.left, rc.top, rc.width, rc.height );
|
|
110
|
+
//ctx.stroke( );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
ctx.textBaseline = 'bottom';
|
|
114
|
+
|
|
115
|
+
// End points
|
|
116
|
+
let fontSize = roundTo(drawStyle.fontSize, 2) ?? 12;
|
|
117
|
+
//let style = `${drawStyle.fontStyle ?? ''} ${drawStyle.fontVariant ?? ''} ${drawStyle.fontWeight ?? ''} ${fontSize}px ${drawStyle.fontFamily ?? 'arial'}`;
|
|
118
|
+
let style = '';
|
|
119
|
+
|
|
120
|
+
if( drawStyle.fontStyle ) {style += drawStyle.fontStyle + ' ';}
|
|
121
|
+
if( drawStyle.fontVariant ) {style += drawStyle.fontVariant + ' ';}
|
|
122
|
+
if( drawStyle.fontWeight ) {style += drawStyle.fontWeight + ' ';}
|
|
123
|
+
style += fontSize + 'px ';
|
|
124
|
+
|
|
125
|
+
let family = drawStyle.fontFamily ?? 'sans-serif';
|
|
126
|
+
if( family.indexOf('.')>0 ) {
|
|
127
|
+
family = '"'+family+'"';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
style += family;
|
|
131
|
+
ctx.font = style.trim();
|
|
132
|
+
|
|
133
|
+
let textarray: ITextLine[] = [];
|
|
134
|
+
let lines = input_Text.split('\n');
|
|
135
|
+
|
|
136
|
+
const columns = drawStyle.columns < 1 ? 1 : drawStyle.columns;
|
|
137
|
+
const gap = drawStyle.columnGap;
|
|
138
|
+
|
|
139
|
+
let col_width = (rc.width - gap * (columns - 1)) / columns;
|
|
140
|
+
let col_left = rc.left;
|
|
141
|
+
let hlimit = col_width;
|
|
142
|
+
|
|
143
|
+
if( !drawStyle.lineBreak ) {
|
|
144
|
+
hlimit = 99999999;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const spaceW = _measureText(ctx, ' ' );
|
|
148
|
+
|
|
149
|
+
lines.forEach((text) => {
|
|
150
|
+
|
|
151
|
+
let line: ITextLine = { width: 0, words: [], space: 0 };
|
|
152
|
+
|
|
153
|
+
// fit in width ?
|
|
154
|
+
let lwidth = _measureText(ctx, text);
|
|
155
|
+
if (lwidth < hlimit) {
|
|
156
|
+
line.width = lwidth;
|
|
157
|
+
line.words.push({ width: lwidth, text });
|
|
158
|
+
textarray.push(line);
|
|
159
|
+
}
|
|
160
|
+
// break line to fit in width
|
|
161
|
+
else {
|
|
162
|
+
// make word list & measure them
|
|
163
|
+
let twords = text.split(/\s/).filter(w => w !== '');
|
|
164
|
+
|
|
165
|
+
let words: ITextWord[] = twords.map( w => {
|
|
166
|
+
const wwidth = _measureText(ctx, w );
|
|
167
|
+
const word: ITextWord = {
|
|
168
|
+
width: wwidth,
|
|
169
|
+
text: w
|
|
170
|
+
};
|
|
171
|
+
return word;
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// then compute lines
|
|
175
|
+
let n = 0;
|
|
176
|
+
let e = 0;
|
|
177
|
+
|
|
178
|
+
while (n < words.length) {
|
|
179
|
+
|
|
180
|
+
const word = words[n];
|
|
181
|
+
let test = line.width;
|
|
182
|
+
if( test ) {
|
|
183
|
+
test += spaceW;
|
|
184
|
+
}
|
|
185
|
+
test += word.width;
|
|
186
|
+
|
|
187
|
+
//console.log( word, test, col_width );
|
|
188
|
+
if (test > col_width && e > 0) {
|
|
189
|
+
textarray.push(line);
|
|
190
|
+
|
|
191
|
+
// restart
|
|
192
|
+
e = 0;
|
|
193
|
+
lwidth = 0;
|
|
194
|
+
line = { width: 0, words: [], space: 0 };
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
line.words.push(word);
|
|
198
|
+
line.width = test;
|
|
199
|
+
n++;
|
|
200
|
+
e++;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (e) {
|
|
205
|
+
textarray.push(line);
|
|
206
|
+
line.last = true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const textSize = _calcTextHeight(ctx, "Ag");
|
|
212
|
+
let lineHeight = (drawStyle.lineHeight??1.3) * textSize; // * 1.2 = map to pdf
|
|
213
|
+
const nlines = textarray.length;
|
|
214
|
+
|
|
215
|
+
// calc vertical Align
|
|
216
|
+
let col_top = rc.top;
|
|
217
|
+
|
|
218
|
+
if (columns == 1) {
|
|
219
|
+
|
|
220
|
+
let fullHeight = lineHeight * nlines;
|
|
221
|
+
if( nlines==1 ) {
|
|
222
|
+
lineHeight = textSize;
|
|
223
|
+
fullHeight = textSize;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (drawStyle.vAlign === 'middle') {
|
|
227
|
+
col_top = rc.top + rc.height / 2 - fullHeight / 2;
|
|
228
|
+
col_top += lineHeight / 2;
|
|
229
|
+
ctx.textBaseline = 'middle';
|
|
230
|
+
}
|
|
231
|
+
else if (drawStyle.vAlign === 'bottom') {
|
|
232
|
+
if (fullHeight < rc.height) {
|
|
233
|
+
col_top = rc.top + rc.height - fullHeight + lineHeight;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
col_top = rc.top;
|
|
238
|
+
ctx.textBaseline = 'top';
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
// always top, cannot justify multi-columns vertically
|
|
243
|
+
// todo: for now
|
|
244
|
+
col_top += textSize;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const justify = drawStyle.align == 'justify';
|
|
248
|
+
|
|
249
|
+
let column = columns;
|
|
250
|
+
let y = col_top;
|
|
251
|
+
let align = 0;
|
|
252
|
+
|
|
253
|
+
// faster test..
|
|
254
|
+
switch (drawStyle.align) {
|
|
255
|
+
case 'right': align = 1; break;
|
|
256
|
+
case 'center': align = 2; break;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
//print all lines of text
|
|
260
|
+
let idx = 1, yy = 0;
|
|
261
|
+
textarray.some( line => {
|
|
262
|
+
|
|
263
|
+
//console.log( idx++, yy );
|
|
264
|
+
|
|
265
|
+
line.space = spaceW;
|
|
266
|
+
if (justify && !line.last) {
|
|
267
|
+
_justify(line, col_width, spaceW);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
let x = col_left;
|
|
271
|
+
if (align == 1) {
|
|
272
|
+
x += col_width - line.width;
|
|
273
|
+
}
|
|
274
|
+
else if (align == 2) {
|
|
275
|
+
x += col_width / 2 - line.width / 2;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ...debug
|
|
279
|
+
/*ctx.lineWidth = 1;
|
|
280
|
+
ctx.beginPath( );
|
|
281
|
+
ctx.moveTo( rc.left, y );
|
|
282
|
+
ctx.lineTo( rc.right, y );
|
|
283
|
+
ctx.strokeStyle = 'white';
|
|
284
|
+
ctx.stroke( );*/
|
|
285
|
+
|
|
286
|
+
line.words.forEach(w => {
|
|
287
|
+
/*ctx.beginPath( );
|
|
288
|
+
ctx.moveTo( x, y );
|
|
289
|
+
ctx.lineTo( x, y-40 );
|
|
290
|
+
ctx.strokeStyle = 'red';
|
|
291
|
+
ctx.stroke( );*/
|
|
292
|
+
|
|
293
|
+
/*ctx.beginPath( );
|
|
294
|
+
ctx.moveTo( x+w.width, y );
|
|
295
|
+
ctx.lineTo( x+w.width, y-40 );
|
|
296
|
+
ctx.strokeStyle = 'green';
|
|
297
|
+
ctx.stroke( );*/
|
|
298
|
+
|
|
299
|
+
ctx.fillText(w.text, x, y);
|
|
300
|
+
x += w.width + line.space;
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
y += lineHeight;
|
|
304
|
+
yy += lineHeight;
|
|
305
|
+
|
|
306
|
+
if (y > (rc.bottom+lineHeight) ) {
|
|
307
|
+
y = col_top;
|
|
308
|
+
col_left += col_width + gap;
|
|
309
|
+
if( --column==0 ) {
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
} )
|
|
314
|
+
|
|
315
|
+
ctx.restore();
|
|
316
|
+
|
|
317
|
+
//console.timeEnd( 'drawtext' );
|
|
318
|
+
|
|
319
|
+
// todo autogrow + multi-columns
|
|
320
|
+
return { height: (textarray.length+0.3) * lineHeight };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Calculate Height of the font
|
|
324
|
+
function _calcTextHeight(ctx: CanvasRenderingContext2D, text: string) {
|
|
325
|
+
const size = ctx.measureText(text);
|
|
326
|
+
return size.actualBoundingBoxAscent+size.actualBoundingBoxDescent;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function _measureText(ctx: CanvasRenderingContext2D, text: string) {
|
|
330
|
+
return roundTo(ctx.measureText(text).width,2);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function _justify(line: ITextLine, width: number, spaceW: number ) {
|
|
334
|
+
|
|
335
|
+
let delta = (width - line.width) / (line.words.length - 1) + spaceW;
|
|
336
|
+
if (delta <= 0) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
line.width = width;
|
|
341
|
+
line.space = delta;
|
|
342
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file fileupload.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, CProps } from './component'
|
|
27
|
+
|
|
28
|
+
import { HLayout } from './layout'
|
|
29
|
+
import { Input } from './input'
|
|
30
|
+
import { Image } from './image'
|
|
31
|
+
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// [FILEUPLOAD]
|
|
34
|
+
// ============================================================================
|
|
35
|
+
|
|
36
|
+
export interface FileUploadProps extends CProps {
|
|
37
|
+
name: string;
|
|
38
|
+
value: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class FileUpload extends HLayout<FileUploadProps> {
|
|
42
|
+
constructor(props: FileUploadProps) {
|
|
43
|
+
super(props);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
clear() {
|
|
47
|
+
this.m_props.value = '';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export class ImageUpload extends FileUpload {
|
|
56
|
+
|
|
57
|
+
private m_path: any;
|
|
58
|
+
private m_ui_img: Image;
|
|
59
|
+
private m_ui_input: Input;
|
|
60
|
+
|
|
61
|
+
/** @ignore */
|
|
62
|
+
render(props: FileUploadProps) {
|
|
63
|
+
|
|
64
|
+
let ename = "up" + this.uid;
|
|
65
|
+
|
|
66
|
+
this.setContent([
|
|
67
|
+
|
|
68
|
+
new Component({
|
|
69
|
+
tag: 'label', attrs: { for: ename }, content: [
|
|
70
|
+
this.m_ui_img = new Image({ src: this.m_props.value }),
|
|
71
|
+
]
|
|
72
|
+
}),
|
|
73
|
+
|
|
74
|
+
this.m_ui_input = new Input({
|
|
75
|
+
cls: '@hidden',
|
|
76
|
+
id: ename,
|
|
77
|
+
type: 'file',
|
|
78
|
+
name: this.m_props.name,
|
|
79
|
+
value_hook: {
|
|
80
|
+
get: () => { return this._get_value() },
|
|
81
|
+
set: (v) => { this._set_value(v); }
|
|
82
|
+
},
|
|
83
|
+
attrs: {
|
|
84
|
+
accept: 'image/*'
|
|
85
|
+
},
|
|
86
|
+
dom_events: {
|
|
87
|
+
change: (e) => { this._handleChange(e) }
|
|
88
|
+
}
|
|
89
|
+
}),
|
|
90
|
+
]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
clear() {
|
|
94
|
+
super.clear();
|
|
95
|
+
(<HTMLInputElement>this.m_ui_input.dom).value = '';
|
|
96
|
+
this.m_ui_img.setImage(null, false);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private _get_value() {
|
|
100
|
+
return this.m_path;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private _set_value(v) {
|
|
104
|
+
debugger;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private _handleChange(e) {
|
|
108
|
+
|
|
109
|
+
let self = this;
|
|
110
|
+
function createThumbnail(file) {
|
|
111
|
+
|
|
112
|
+
let reader = new FileReader();
|
|
113
|
+
reader.addEventListener('load', (e) => {
|
|
114
|
+
self.m_ui_img.setImage(reader.result.toString());
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
reader.readAsDataURL(file);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const allowedTypes = ['png', 'jpg', 'jpeg', 'gif'];
|
|
121
|
+
|
|
122
|
+
let files = e.target.files,
|
|
123
|
+
filesLen = files.length;
|
|
124
|
+
|
|
125
|
+
for (let i = 0; i < filesLen; i++) {
|
|
126
|
+
|
|
127
|
+
let imgType: string = files[i].name.split('.');
|
|
128
|
+
imgType = imgType[imgType.length - 1];
|
|
129
|
+
imgType = imgType.toLowerCase();
|
|
130
|
+
|
|
131
|
+
if (allowedTypes.indexOf(imgType) != -1) {
|
|
132
|
+
createThumbnail(files[i]);
|
|
133
|
+
this.m_path = files[i];
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let g_file_input: Component = null;
|
|
141
|
+
|
|
142
|
+
function _createFileInput() {
|
|
143
|
+
if (!g_file_input) {
|
|
144
|
+
g_file_input = new Component({
|
|
145
|
+
tag: 'input',
|
|
146
|
+
style: {
|
|
147
|
+
display: 'none',
|
|
148
|
+
id: 'fileDialog',
|
|
149
|
+
},
|
|
150
|
+
attrs: {
|
|
151
|
+
type: 'file'
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// ajoute un input type:file caché pour pouvoir choir un fichier a ouvrir
|
|
156
|
+
document.body.appendChild(g_file_input._build());
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
g_file_input.clearDomEvent('change');
|
|
160
|
+
return g_file_input;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* show openfile dialog
|
|
165
|
+
* @param extensions - string - ex: '.doc,.docx'
|
|
166
|
+
* @param cb - callback to call when user select a file
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
export function openFile(extensions: string, cb: (filename: FileList) => void, multiple = false) {
|
|
170
|
+
|
|
171
|
+
let fi = _createFileInput();
|
|
172
|
+
|
|
173
|
+
fi.removeAttribute('nwsaveas');
|
|
174
|
+
fi.setAttribute('accept', extensions);
|
|
175
|
+
fi.setAttribute('multiple', multiple);
|
|
176
|
+
|
|
177
|
+
// Set up the file chooser for the on change event
|
|
178
|
+
fi.setDomEvent("change", (evt) => {
|
|
179
|
+
// When we reach this point, it means the user has selected a file,
|
|
180
|
+
let files = (<HTMLInputElement>fi.dom).files
|
|
181
|
+
cb(files);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
fi.dom.click();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* open saveas dialog
|
|
189
|
+
* @param defFileName - string - proposed filename
|
|
190
|
+
* @param cb - callback to call when user choose the destination
|
|
191
|
+
*/
|
|
192
|
+
|
|
193
|
+
export function saveFile(defFileName: string, extensions: string, cb: (filename: File) => void) {
|
|
194
|
+
|
|
195
|
+
let fi = _createFileInput();
|
|
196
|
+
fi.setAttribute('nwsaveas', defFileName);
|
|
197
|
+
fi.setAttribute('accept', extensions);
|
|
198
|
+
|
|
199
|
+
// Set up the file chooser for the on change event
|
|
200
|
+
fi.setDomEvent("change", (evt) => {
|
|
201
|
+
// When we reach this point, it means the user has selected a file,
|
|
202
|
+
let files = (<HTMLInputElement>fi.dom).files
|
|
203
|
+
cb(files[0]);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
fi.dom.click();
|
|
207
|
+
}
|
|
208
|
+
|