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/button.ts
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file button.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
|
+
|
|
27
|
+
import { Component, CProps, CEventMap, HtmlString } from './component'
|
|
28
|
+
import { EventCallback, EvClick, EvChange } from './x4_events'
|
|
29
|
+
|
|
30
|
+
import { Icon, IconID } from './icon'
|
|
31
|
+
import { Label } from './label'
|
|
32
|
+
import { Menu, MenuItem, MenuOrSep } from './menu'
|
|
33
|
+
import { isFunction } from './tools'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Button events
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
interface ButtonEventMap extends CEventMap {
|
|
40
|
+
click: EvClick;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type MenuCallBack = () => MenuOrSep[];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Button properties
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
interface ButtonProps<E extends ButtonEventMap = ButtonEventMap> extends CProps<E> {
|
|
50
|
+
text?: string | HtmlString; // initial button text
|
|
51
|
+
icon?: IconID; // optional icon id
|
|
52
|
+
rightIcon?: IconID; // optional icon id
|
|
53
|
+
align?: 'center' | 'left' | 'right'; // text alignment
|
|
54
|
+
autoRepeat?: number; // time in ms or 0/undefined for none
|
|
55
|
+
menu?: MenuOrSep[] | MenuCallBack;
|
|
56
|
+
|
|
57
|
+
click?: EventCallback<EvClick>; // shortcut to events: { click: ... }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Base button
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
export class BaseButton<P extends ButtonProps = ButtonProps, E extends ButtonEventMap = ButtonEventMap> extends Component<P, E> {
|
|
66
|
+
|
|
67
|
+
constructor(props: P) {
|
|
68
|
+
super(props);
|
|
69
|
+
|
|
70
|
+
this.setProp('tag', 'button');
|
|
71
|
+
|
|
72
|
+
this.setDomEvent('click', (e) => this._handleClick(e));
|
|
73
|
+
this.setDomEvent('mousedown', () => { this._startAutoRep(true) });
|
|
74
|
+
this.setDomEvent('mouseup', () => { this._startAutoRep(false) });
|
|
75
|
+
this.setDomEvent('keydown', (e) => this._handleKeyDown(e));
|
|
76
|
+
|
|
77
|
+
this.mapPropEvents( props, 'click' );
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
render(props: ButtonProps) {
|
|
81
|
+
|
|
82
|
+
let icon = props.icon ? new Icon({ icon: props.icon, cls: 'left', ref: 'l_icon' }) : null;
|
|
83
|
+
let label = new Label({ flex: 1, text: props.text ?? '', align: props.align, ref: 'label' });
|
|
84
|
+
let ricon = props.rightIcon ? new Icon({ icon: props.rightIcon, cls: 'right', ref: 'r_icon' }) : null;
|
|
85
|
+
|
|
86
|
+
this.setContent([icon, label, ricon]);
|
|
87
|
+
this._setTabIndex(props.tabIndex);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* starts/stops the autorepeat
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
private _startAutoRep(start: boolean) {
|
|
95
|
+
|
|
96
|
+
if (!this.m_props.autoRepeat) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (start) {
|
|
101
|
+
// 1st timer 1s
|
|
102
|
+
this.startTimer('repeat', 700, false, () => {
|
|
103
|
+
// auto click
|
|
104
|
+
this.startTimer('repeat', this.m_props.autoRepeat, true, this._sendClick);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
this.stopTimer('repeat');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
*
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
protected _handleKeyDown(ev: KeyboardEvent) {
|
|
117
|
+
if (!ev.ctrlKey && !ev.shiftKey && !ev.altKey) {
|
|
118
|
+
if (ev.key == 'Enter' || ev.key == ' ') {
|
|
119
|
+
this._sendClick();
|
|
120
|
+
ev.preventDefault();
|
|
121
|
+
ev.stopPropagation();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* called by the system on click event
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
protected _handleClick(ev: MouseEvent) {
|
|
131
|
+
|
|
132
|
+
if (this.m_props.menu) {
|
|
133
|
+
let menu = new Menu({
|
|
134
|
+
items: isFunction(this.m_props.menu) ? this.m_props.menu() : this.m_props.menu
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let rc = this.getBoundingRect();
|
|
138
|
+
menu.displayAt(rc.left, rc.bottom, 'tl');
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this._sendClick();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
ev.preventDefault();
|
|
145
|
+
ev.stopPropagation();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* sends a click to the observers
|
|
150
|
+
*/
|
|
151
|
+
|
|
152
|
+
protected _sendClick() {
|
|
153
|
+
if (this.m_props.menu) {
|
|
154
|
+
let menu = new Menu({
|
|
155
|
+
items: isFunction(this.m_props.menu) ? this.m_props.menu() : this.m_props.menu
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
let rc = this.getBoundingRect();
|
|
159
|
+
menu.displayAt(rc.left, rc.bottom, 'tl');
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
this.emit('click', EvClick());
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* change the button text
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* let btn = new Button( {
|
|
171
|
+
* text: 'hello'
|
|
172
|
+
* });
|
|
173
|
+
*
|
|
174
|
+
* btn.text = 'world';
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
public set text(text: string | HtmlString) {
|
|
179
|
+
this.m_props.text = text;
|
|
180
|
+
|
|
181
|
+
let label = this.itemWithRef<Label>('label');
|
|
182
|
+
if (label) { label.text = text; }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public get text(): string | HtmlString {
|
|
186
|
+
let label = this.itemWithRef<Label>('label');
|
|
187
|
+
return label?.text;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* change the button icon
|
|
192
|
+
* todo: do nothing if no icon defined at startup
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```ts
|
|
196
|
+
* let btn = new Button( {
|
|
197
|
+
* text: 'hello',
|
|
198
|
+
* icon: 'close'
|
|
199
|
+
* });
|
|
200
|
+
* btn.setIcon( 'open' );
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
public set icon(icon: IconID) {
|
|
205
|
+
this.m_props.icon = icon;
|
|
206
|
+
|
|
207
|
+
let ico = this.itemWithRef<Icon>('l_icon');
|
|
208
|
+
if (ico) {
|
|
209
|
+
ico.icon = icon;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
this.update( );
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
public get icon() {
|
|
217
|
+
let ico = this.itemWithRef<Icon>('l_icon');
|
|
218
|
+
return ico?.icon;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* change the button right icon
|
|
223
|
+
* todo: do nothing if no icon defined at startup
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* let btn = new Button( {
|
|
228
|
+
* text: 'hello',
|
|
229
|
+
* icon: 'close'
|
|
230
|
+
* });
|
|
231
|
+
* btn.setIcon( 'open' );
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
public set rightIcon(icon: IconID) {
|
|
236
|
+
this.m_props.rightIcon = icon;
|
|
237
|
+
let ico = this.itemWithRef<Icon>('r_icon');
|
|
238
|
+
if (ico) {
|
|
239
|
+
ico.icon = icon;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public get rightIcon() {
|
|
244
|
+
let ico = this.itemWithRef<Icon>('l_icon');
|
|
245
|
+
return ico?.icon;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
*
|
|
250
|
+
*/
|
|
251
|
+
|
|
252
|
+
set menu( items: MenuItem[] ) {
|
|
253
|
+
this.m_props.menu = items;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// :: BUTTON ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
*
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
export class Button extends BaseButton<ButtonProps> {
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// :: TOGGLE BUTTON ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
interface ToggleButtonEventMap extends ButtonEventMap {
|
|
270
|
+
change: EvChange;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
interface ToggleButtonProps extends ButtonProps<ToggleButtonEventMap> {
|
|
274
|
+
checked: boolean;
|
|
275
|
+
checkedIcon?: IconID;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
*
|
|
280
|
+
*/
|
|
281
|
+
|
|
282
|
+
export class ToggleButton extends BaseButton<ToggleButtonProps, ToggleButtonEventMap> {
|
|
283
|
+
|
|
284
|
+
constructor(props: ToggleButtonProps) {
|
|
285
|
+
super(props);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
*
|
|
290
|
+
*/
|
|
291
|
+
|
|
292
|
+
render(props: ToggleButtonProps) {
|
|
293
|
+
super.render(props);
|
|
294
|
+
|
|
295
|
+
if (props.checked) {
|
|
296
|
+
this.addClass('checked');
|
|
297
|
+
this._updateIcon( );
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
*
|
|
303
|
+
*/
|
|
304
|
+
|
|
305
|
+
protected _sendClick() {
|
|
306
|
+
super._sendClick();
|
|
307
|
+
|
|
308
|
+
this.m_props.checked = !this.m_props.checked;
|
|
309
|
+
this.setClass('checked', this.m_props.checked);
|
|
310
|
+
this.emit('change', EvChange(this.m_props.checked));
|
|
311
|
+
|
|
312
|
+
this._updateIcon( );
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private _updateIcon( ) {
|
|
316
|
+
if( this.m_props.checkedIcon ) {
|
|
317
|
+
const ic = this.m_props.checked ? this.m_props.checkedIcon : this.m_props.icon;
|
|
318
|
+
let ico = this.itemWithRef<Icon>('l_icon');
|
|
319
|
+
if (ico) {
|
|
320
|
+
ico.icon = ic;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
package/src/calendar.ts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / \____ _|
|
|
6
|
+
* /__/\__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file calendar.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 { Button } from './button';
|
|
27
|
+
import { Popup } from './popup';
|
|
28
|
+
import { Component, CProps, ContainerEventMap, Flex } from './component'
|
|
29
|
+
import { EvChange, EventCallback } from './x4_events'
|
|
30
|
+
|
|
31
|
+
import { _tr } from './i18n';
|
|
32
|
+
import { Label } from './label';
|
|
33
|
+
import { HLayout, VLayout } from './layout'
|
|
34
|
+
import { date_hash, date_clone, formatIntlDate } from './tools'
|
|
35
|
+
import { Menu, MenuItem } from './menu';
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
interface CalendarEventMap extends ContainerEventMap {
|
|
39
|
+
change?: EvChange;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
interface CalendarProps extends CProps<CalendarEventMap> {
|
|
44
|
+
date?: Date; // initial date to display
|
|
45
|
+
minDate?: Date; // minimal date before the user cannot go
|
|
46
|
+
maxDate?: Date; // maximal date after the user cannot go
|
|
47
|
+
|
|
48
|
+
change?: EventCallback<EvChange>; // shortcut to events: { change: ... }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* default calendar control
|
|
54
|
+
*
|
|
55
|
+
* fires:
|
|
56
|
+
* EventChange ( value = Date )
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
export class Calendar extends VLayout<CalendarProps, CalendarEventMap>
|
|
60
|
+
{
|
|
61
|
+
private m_date: Date;
|
|
62
|
+
|
|
63
|
+
constructor(props: CalendarProps) {
|
|
64
|
+
super(props);
|
|
65
|
+
|
|
66
|
+
this.mapPropEvents( props, 'change' );
|
|
67
|
+
this.m_date = props.date?.clone() ?? new Date();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @ignore */
|
|
71
|
+
|
|
72
|
+
render(props: CalendarProps) {
|
|
73
|
+
|
|
74
|
+
let month_start = date_clone(this.m_date);
|
|
75
|
+
month_start.setDate(1);
|
|
76
|
+
|
|
77
|
+
let day = month_start.getDay();
|
|
78
|
+
if (day == 0) {
|
|
79
|
+
day = 7;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
month_start.setDate(-day + 1 + 1);
|
|
83
|
+
let dte = date_clone(month_start);
|
|
84
|
+
|
|
85
|
+
let today = this.m_date.hash();
|
|
86
|
+
|
|
87
|
+
let month_end = date_clone(this.m_date);
|
|
88
|
+
month_end.setDate(1);
|
|
89
|
+
month_end.setMonth(month_end.getMonth() + 1);
|
|
90
|
+
month_end.setDate(0);
|
|
91
|
+
|
|
92
|
+
let end_of_month = date_hash(month_end);
|
|
93
|
+
|
|
94
|
+
let rows: HLayout[] = [];
|
|
95
|
+
|
|
96
|
+
// month selector
|
|
97
|
+
let header = new HLayout({
|
|
98
|
+
cls: 'month-sel',
|
|
99
|
+
content: [
|
|
100
|
+
new Label({
|
|
101
|
+
cls: 'month',
|
|
102
|
+
text: formatIntlDate(this.m_date, 'O'),
|
|
103
|
+
dom_events: {
|
|
104
|
+
click: () => this._choose('month')
|
|
105
|
+
}
|
|
106
|
+
}),
|
|
107
|
+
new Label({
|
|
108
|
+
cls: 'year',
|
|
109
|
+
text: formatIntlDate(this.m_date, 'Y'),
|
|
110
|
+
dom_events: {
|
|
111
|
+
click: () => this._choose('year')
|
|
112
|
+
}
|
|
113
|
+
}),
|
|
114
|
+
new Flex(),
|
|
115
|
+
new Button({ text: '<', click: () => this._next(false) } ),
|
|
116
|
+
new Button({ text: '>', click: () => this._next(true) } )
|
|
117
|
+
]
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
rows.push(header);
|
|
121
|
+
|
|
122
|
+
// calendar part
|
|
123
|
+
let day_names = [];
|
|
124
|
+
|
|
125
|
+
// day names
|
|
126
|
+
// empty week num
|
|
127
|
+
day_names.push(new HLayout({
|
|
128
|
+
cls: 'weeknum cell',
|
|
129
|
+
}));
|
|
130
|
+
|
|
131
|
+
for (let d = 0; d < 7; d++) {
|
|
132
|
+
day_names.push(new Label({
|
|
133
|
+
cls: 'cell',
|
|
134
|
+
flex: 1,
|
|
135
|
+
text: _tr.global.day_short[(d + 1) % 7]
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
rows.push(new HLayout({
|
|
140
|
+
cls: 'week header',
|
|
141
|
+
content: day_names
|
|
142
|
+
}));
|
|
143
|
+
|
|
144
|
+
let cmonth = this.m_date.getMonth();
|
|
145
|
+
|
|
146
|
+
// weeks
|
|
147
|
+
let first = true;
|
|
148
|
+
while (date_hash(dte) <= end_of_month) {
|
|
149
|
+
|
|
150
|
+
let days = [
|
|
151
|
+
new HLayout({ cls: 'weeknum cell', content: new Component({ tag: 'span', content: formatIntlDate(dte, 'w') }) })
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
// days
|
|
155
|
+
for (let d = 0; d < 7; d++) {
|
|
156
|
+
|
|
157
|
+
let cls = 'cell day';
|
|
158
|
+
if (dte.hash() == today) {
|
|
159
|
+
cls += ' today';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (dte.getMonth() != cmonth) {
|
|
163
|
+
cls += ' out';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
days.push(new HLayout({
|
|
167
|
+
cls,
|
|
168
|
+
flex: 1,
|
|
169
|
+
content: new Component({
|
|
170
|
+
tag: 'span',
|
|
171
|
+
content: formatIntlDate(dte, 'd'),
|
|
172
|
+
}),
|
|
173
|
+
dom_events: {
|
|
174
|
+
click: () => this.select(dte.clone())
|
|
175
|
+
}
|
|
176
|
+
}));
|
|
177
|
+
|
|
178
|
+
dte.setDate(dte.getDate() + 1);
|
|
179
|
+
first = false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
rows.push(new HLayout({
|
|
183
|
+
cls: 'week',
|
|
184
|
+
flex: 1,
|
|
185
|
+
content: days
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
this.setContent(rows);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* select the given date
|
|
194
|
+
* @param date
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
private select(date: Date) {
|
|
198
|
+
this.m_date = date;
|
|
199
|
+
this.emit('change', EvChange(date));
|
|
200
|
+
this.update();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
*
|
|
205
|
+
*/
|
|
206
|
+
|
|
207
|
+
private _next(n: boolean) {
|
|
208
|
+
this.m_date.setMonth(this.m_date.getMonth() + (n ? 1 : -1));
|
|
209
|
+
this.update();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
*
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
private _choose(type: 'month' | 'year') {
|
|
217
|
+
|
|
218
|
+
let items: MenuItem[] = [];
|
|
219
|
+
|
|
220
|
+
if (type == 'month') {
|
|
221
|
+
for (let m = 0; m < 12; m++) {
|
|
222
|
+
items.push(new MenuItem({
|
|
223
|
+
text: _tr.global.month_long[m],
|
|
224
|
+
click: () => { this.m_date.setMonth(m); this.update(); }
|
|
225
|
+
}));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else if (type == 'year') {
|
|
229
|
+
|
|
230
|
+
let min = this.m_props.minDate?.getFullYear() ?? 1900;
|
|
231
|
+
let max = this.m_props.maxDate?.getFullYear() ?? 2037;
|
|
232
|
+
|
|
233
|
+
for (let m = min; m < max; m++) {
|
|
234
|
+
items.push(new MenuItem({
|
|
235
|
+
text: '' + m,
|
|
236
|
+
click: () => { this.m_date.setFullYear(m); this.update(); }
|
|
237
|
+
}));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
let menu = new Menu({
|
|
242
|
+
items
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
let rc = this.getBoundingRect();
|
|
246
|
+
menu.displayAt(rc.left, rc.top);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
get date() {
|
|
250
|
+
return this.m_date;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
set date(date: Date) {
|
|
254
|
+
this.m_date = date;
|
|
255
|
+
this.update();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* default popup calendar
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
export class PopupCalendar extends Popup {
|
|
264
|
+
|
|
265
|
+
m_cal: Calendar;
|
|
266
|
+
|
|
267
|
+
constructor(props: CalendarProps) {
|
|
268
|
+
super({ tabIndex: 1 });
|
|
269
|
+
|
|
270
|
+
this.enableMask(false);
|
|
271
|
+
|
|
272
|
+
this.m_cal = new Calendar(props);
|
|
273
|
+
this.m_cal.addClass('@fit');
|
|
274
|
+
|
|
275
|
+
this.setContent(this.m_cal);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// binded
|
|
279
|
+
private _handleClick = (e: MouseEvent) => {
|
|
280
|
+
if (!this.dom) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let newfocus = <HTMLElement>e.target;
|
|
285
|
+
|
|
286
|
+
// child of this: ok
|
|
287
|
+
if (this.dom.contains(newfocus)) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// menu: ok
|
|
292
|
+
let dest = Component.getElement(newfocus, MenuItem);
|
|
293
|
+
if (dest) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
this.close();
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** @ignore */
|
|
301
|
+
show(modal?: boolean) {
|
|
302
|
+
document.addEventListener('mousedown', this._handleClick);
|
|
303
|
+
super.show(modal);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** @ignore */
|
|
307
|
+
close() {
|
|
308
|
+
document.removeEventListener('mousedown', this._handleClick);
|
|
309
|
+
super.close();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
}
|