x4js 1.5.17 → 1.5.20
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/{README.md → README.txt} +0 -0
- package/lib/cjs/index.js +39 -103
- package/lib/cjs/index.js.map +7 -0
- package/lib/esm/index.mjs +39 -87
- package/lib/esm/index.mjs.map +7 -0
- package/lib/{license.md → licence.md} +0 -0
- package/lib/src/MIT-license.md +14 -0
- package/lib/{types/action.d.ts → src/action.ts} +64 -33
- package/lib/src/alpha.jpg +0 -0
- package/lib/src/app_sockets.ts +81 -0
- package/lib/src/application.ts +251 -0
- package/lib/src/autocomplete.ts +197 -0
- package/lib/src/base64.ts +166 -0
- package/lib/src/base_component.ts +152 -0
- package/lib/src/button.ts +355 -0
- package/lib/src/calendar.ts +328 -0
- package/lib/src/canvas.ts +505 -0
- package/lib/src/cardview.ts +227 -0
- package/lib/src/checkbox.ts +188 -0
- package/lib/src/color.ts +752 -0
- package/lib/src/colorpicker.ts +1639 -0
- package/lib/src/combobox.ts +465 -0
- package/lib/src/component.ts +2329 -0
- package/lib/{esm/dom_events.js → src/copyright.txt} +27 -29
- package/lib/src/datastore.ts +1322 -0
- package/lib/src/dialog.ts +656 -0
- package/lib/src/dom_events.ts +315 -0
- package/lib/src/drag_manager.ts +199 -0
- package/lib/src/drawtext.ts +355 -0
- package/lib/src/fileupload.ts +213 -0
- package/lib/src/form.ts +375 -0
- package/lib/src/formatters.ts +105 -0
- package/lib/src/gridview.ts +1185 -0
- package/lib/src/i18n.ts +346 -0
- package/lib/src/icon.ts +335 -0
- package/lib/src/image.ts +204 -0
- package/lib/src/index.ts +89 -0
- package/lib/src/input.ts +249 -0
- package/lib/src/label.ts +128 -0
- package/lib/src/layout.ts +430 -0
- package/lib/src/link.ts +86 -0
- package/lib/src/listview.ts +765 -0
- package/lib/{esm/md5.js → src/md5.ts} +438 -403
- package/lib/src/menu.ts +425 -0
- package/lib/src/messagebox.ts +224 -0
- package/lib/src/panel.ts +86 -0
- package/lib/src/popup.ts +494 -0
- package/lib/src/property_editor.ts +337 -0
- package/lib/src/radiobtn.ts +197 -0
- package/lib/src/rating.ts +135 -0
- package/lib/src/request.ts +300 -0
- package/lib/src/router.ts +185 -0
- package/lib/src/settings.ts +77 -0
- package/lib/src/sidebarview.ts +103 -0
- package/lib/src/spreadsheet.ts +1449 -0
- package/lib/src/styles.ts +343 -0
- package/lib/src/svgcomponent.ts +577 -0
- package/lib/src/tabbar.ts +151 -0
- package/lib/src/tabview.ts +110 -0
- package/lib/src/textarea.ts +235 -0
- package/lib/src/textedit.ts +544 -0
- package/lib/src/toaster.ts +80 -0
- package/lib/src/tools.ts +1473 -0
- package/lib/src/tooltips.ts +191 -0
- package/lib/src/treeview.ts +716 -0
- package/lib/{esm/version.js → src/version.ts} +30 -29
- package/lib/{styles → src}/x4.less +0 -0
- package/lib/src/x4dom.ts +57 -0
- package/lib/src/x4events.ts +585 -0
- package/lib/src/x4react.ts +90 -0
- package/package.json +52 -49
- package/lib/cjs/action.js +0 -58
- package/lib/cjs/app_sockets.js +0 -74
- package/lib/cjs/application.js +0 -182
- package/lib/cjs/autocomplete.js +0 -155
- package/lib/cjs/base_component.js +0 -124
- package/lib/cjs/button.js +0 -263
- package/lib/cjs/calendar.js +0 -254
- package/lib/cjs/canvas.js +0 -361
- package/lib/cjs/cardview.js +0 -161
- package/lib/cjs/checkbox.js +0 -139
- package/lib/cjs/color.js +0 -591
- package/lib/cjs/colorpicker.js +0 -1468
- package/lib/cjs/combobox.js +0 -349
- package/lib/cjs/component.js +0 -1719
- package/lib/cjs/datastore.js +0 -985
- package/lib/cjs/dialog.js +0 -478
- package/lib/cjs/dom_events.js +0 -30
- package/lib/cjs/drag_manager.js +0 -145
- package/lib/cjs/drawtext.js +0 -276
- package/lib/cjs/fileupload.js +0 -167
- package/lib/cjs/form.js +0 -304
- package/lib/cjs/formatters.js +0 -90
- package/lib/cjs/gridview.js +0 -881
- package/lib/cjs/i18n.js +0 -266
- package/lib/cjs/icon.js +0 -272
- package/lib/cjs/image.js +0 -157
- package/lib/cjs/input.js +0 -174
- package/lib/cjs/label.js +0 -95
- package/lib/cjs/layout.js +0 -322
- package/lib/cjs/link.js +0 -64
- package/lib/cjs/listview.js +0 -546
- package/lib/cjs/md5.js +0 -403
- package/lib/cjs/menu.js +0 -301
- package/lib/cjs/messagebox.js +0 -177
- package/lib/cjs/panel.js +0 -68
- package/lib/cjs/popup.js +0 -383
- package/lib/cjs/property_editor.js +0 -252
- package/lib/cjs/radiobtn.js +0 -140
- package/lib/cjs/rating.js +0 -102
- package/lib/cjs/request.js +0 -246
- package/lib/cjs/router.js +0 -139
- package/lib/cjs/settings.js +0 -69
- package/lib/cjs/sidebarview.js +0 -82
- package/lib/cjs/spreadsheet.js +0 -1084
- package/lib/cjs/styles.js +0 -275
- package/lib/cjs/svgcomponent.js +0 -447
- package/lib/cjs/tabbar.js +0 -111
- package/lib/cjs/tabview.js +0 -85
- package/lib/cjs/textarea.js +0 -165
- package/lib/cjs/textedit.js +0 -415
- package/lib/cjs/toaster.js +0 -64
- package/lib/cjs/tools.js +0 -1197
- package/lib/cjs/tooltips.js +0 -157
- package/lib/cjs/treeview.js +0 -535
- package/lib/cjs/version.js +0 -32
- package/lib/cjs/x4dom.js +0 -32
- package/lib/cjs/x4events.js +0 -384
- package/lib/cjs/x4react.js +0 -72
- package/lib/esm/action.js +0 -53
- package/lib/esm/app_sockets.js +0 -70
- package/lib/esm/application.js +0 -184
- package/lib/esm/autocomplete.js +0 -155
- package/lib/esm/base_component.js +0 -107
- package/lib/esm/button.js +0 -257
- package/lib/esm/calendar.js +0 -249
- package/lib/esm/canvas.js +0 -358
- package/lib/esm/cardview.js +0 -159
- package/lib/esm/checkbox.js +0 -134
- package/lib/esm/color.js +0 -588
- package/lib/esm/colorpicker.js +0 -1475
- package/lib/esm/combobox.js +0 -351
- package/lib/esm/component.js +0 -1711
- package/lib/esm/datastore.js +0 -990
- package/lib/esm/dialog.js +0 -490
- package/lib/esm/drag_manager.js +0 -147
- package/lib/esm/drawtext.js +0 -271
- package/lib/esm/fileupload.js +0 -163
- package/lib/esm/form.js +0 -304
- package/lib/esm/formatters.js +0 -81
- package/lib/esm/gridview.js +0 -889
- package/lib/esm/i18n.js +0 -257
- package/lib/esm/icon.js +0 -261
- package/lib/esm/image.js +0 -154
- package/lib/esm/input.js +0 -182
- package/lib/esm/label.js +0 -90
- package/lib/esm/layout.js +0 -310
- package/lib/esm/link.js +0 -59
- package/lib/esm/listview.js +0 -534
- package/lib/esm/menu.js +0 -300
- package/lib/esm/messagebox.js +0 -161
- package/lib/esm/panel.js +0 -65
- package/lib/esm/popup.js +0 -379
- package/lib/esm/property_editor.js +0 -251
- package/lib/esm/radiobtn.js +0 -136
- package/lib/esm/rating.js +0 -97
- package/lib/esm/request.js +0 -224
- package/lib/esm/router.js +0 -136
- package/lib/esm/settings.js +0 -66
- package/lib/esm/sidebarview.js +0 -79
- package/lib/esm/spreadsheet.js +0 -1097
- package/lib/esm/styles.js +0 -270
- package/lib/esm/svgcomponent.js +0 -450
- package/lib/esm/tabbar.js +0 -107
- package/lib/esm/tabview.js +0 -83
- package/lib/esm/textarea.js +0 -160
- package/lib/esm/textedit.js +0 -412
- package/lib/esm/toaster.js +0 -62
- package/lib/esm/tools.js +0 -1158
- package/lib/esm/tooltips.js +0 -153
- package/lib/esm/treeview.js +0 -527
- package/lib/esm/x4dom.js +0 -29
- package/lib/esm/x4events.js +0 -376
- package/lib/esm/x4react.js +0 -71
- package/lib/types/app_sockets.d.ts +0 -29
- package/lib/types/application.d.ts +0 -104
- package/lib/types/autocomplete.d.ts +0 -58
- package/lib/types/base_component.d.ts +0 -88
- package/lib/types/button.d.ts +0 -151
- package/lib/types/calendar.d.ts +0 -82
- package/lib/types/canvas.d.ts +0 -92
- package/lib/types/cardview.d.ts +0 -87
- package/lib/types/checkbox.d.ts +0 -77
- package/lib/types/color.d.ts +0 -148
- package/lib/types/colorpicker.d.ts +0 -107
- package/lib/types/combobox.d.ts +0 -107
- package/lib/types/component.d.ts +0 -601
- package/lib/types/datastore.d.ts +0 -396
- package/lib/types/dialog.d.ts +0 -175
- package/lib/types/dom_events.d.ts +0 -302
- package/lib/types/drag_manager.d.ts +0 -54
- package/lib/types/drawtext.d.ts +0 -48
- package/lib/types/fileupload.d.ts +0 -64
- package/lib/types/form.d.ts +0 -133
- package/lib/types/formatters.d.ts +0 -35
- package/lib/types/gridview.d.ts +0 -175
- package/lib/types/i18n.d.ts +0 -73
- package/lib/types/icon.d.ts +0 -64
- package/lib/types/image.d.ts +0 -55
- package/lib/types/index.d.ts +0 -84
- package/lib/types/input.d.ts +0 -91
- package/lib/types/label.d.ts +0 -58
- package/lib/types/layout.d.ts +0 -87
- package/lib/types/link.d.ts +0 -50
- package/lib/types/listview.d.ts +0 -178
- package/lib/types/md5.d.ts +0 -61
- package/lib/types/menu.d.ts +0 -130
- package/lib/types/messagebox.d.ts +0 -69
- package/lib/types/panel.d.ts +0 -47
- package/lib/types/popup.d.ts +0 -75
- package/lib/types/property_editor.d.ts +0 -71
- package/lib/types/radiobtn.d.ts +0 -73
- package/lib/types/rating.d.ts +0 -53
- package/lib/types/request.d.ts +0 -52
- package/lib/types/router.d.ts +0 -42
- package/lib/types/settings.d.ts +0 -37
- package/lib/types/sidebarview.d.ts +0 -49
- package/lib/types/spreadsheet.d.ts +0 -222
- package/lib/types/styles.d.ts +0 -85
- package/lib/types/svgcomponent.d.ts +0 -215
- package/lib/types/tabbar.d.ts +0 -58
- package/lib/types/tabview.d.ts +0 -49
- package/lib/types/textarea.d.ts +0 -77
- package/lib/types/textedit.d.ts +0 -123
- package/lib/types/toaster.d.ts +0 -42
- package/lib/types/tools.d.ts +0 -394
- package/lib/types/tooltips.d.ts +0 -46
- package/lib/types/treeview.d.ts +0 -144
- package/lib/types/version.d.ts +0 -29
- package/lib/types/x4dom.d.ts +0 -49
- package/lib/types/x4events.d.ts +0 -269
- package/lib/types/x4react.d.ts +0 -41
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ___ ___ __
|
|
3
|
+
* \ \_/ / / _
|
|
4
|
+
* \ / /_| |_
|
|
5
|
+
* / _ \____ _|
|
|
6
|
+
* /__/ \__\ |_|
|
|
7
|
+
*
|
|
8
|
+
* @file listview.ts
|
|
9
|
+
* @author Etienne Cochard
|
|
10
|
+
*
|
|
11
|
+
* Copyright (c) 2019-2022 R-libre ingenierie
|
|
12
|
+
*
|
|
13
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
* in the Software without restriction, including without limitation the rights
|
|
16
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
17
|
+
* of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
18
|
+
* subject to the following conditions:
|
|
19
|
+
* The above copyright notice and this permission notice shall be included in all copies
|
|
20
|
+
* or substantial portions of the Software.
|
|
21
|
+
*
|
|
22
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
23
|
+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
24
|
+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
25
|
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
26
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
27
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
|
+
**/
|
|
29
|
+
|
|
30
|
+
import { x4document } from './x4dom'
|
|
31
|
+
|
|
32
|
+
import { Container, Component, ContainerProps, ContainerEventMap, EvDblClick } from './component'
|
|
33
|
+
import { IconID } from './icon';
|
|
34
|
+
import { HLayout, VLayout } from './layout'
|
|
35
|
+
import { Popup, PopupEventMap, PopupProps } from './popup';
|
|
36
|
+
import { HtmlString, isFunction } from './tools';
|
|
37
|
+
import { Menu, MenuItem } from "./menu";
|
|
38
|
+
|
|
39
|
+
import { EvContextMenu, EvSelectionChange, EvClick, EventCallback, BasicEvent, EvChange } from "./x4events";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* item definition
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
export interface ListViewItem {
|
|
46
|
+
id: any;
|
|
47
|
+
text?: string | HtmlString; // if you need pure text
|
|
48
|
+
html?: boolean; // if text is html
|
|
49
|
+
icon?: IconID;
|
|
50
|
+
data?: any;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* callback to render item
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
export interface RenderListItem {
|
|
58
|
+
(item: ListViewItem): any;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* callback to fill the list
|
|
63
|
+
*/
|
|
64
|
+
export interface PopulateItems {
|
|
65
|
+
( filter: string ): ListViewItem[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* listview can generate these events
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
export interface ListViewEventMap extends ContainerEventMap {
|
|
73
|
+
click?: EvClick;
|
|
74
|
+
dblClick?: EvDblClick;
|
|
75
|
+
contextMenu?: EvContextMenu;
|
|
76
|
+
selectionChange?: EvSelectionChange;
|
|
77
|
+
cancel?: EvCancel;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* listview properties
|
|
83
|
+
*/
|
|
84
|
+
export interface ListViewProps<E extends ListViewEventMap = ListViewEventMap> extends ContainerProps<E> {
|
|
85
|
+
items?: ListViewItem[];
|
|
86
|
+
populate?: PopulateItems;
|
|
87
|
+
gadgets?: Component[]; // gadgets to instert at bottom
|
|
88
|
+
|
|
89
|
+
virtual?: boolean; // if true, items will be rendered on demand
|
|
90
|
+
itemHeight?: number; // in case of a virtual list, item height
|
|
91
|
+
|
|
92
|
+
renderItem?: RenderListItem;
|
|
93
|
+
|
|
94
|
+
click?: EventCallback<EvClick>; // shortcut to events: { click: ... }
|
|
95
|
+
dblClick?: EventCallback<EvDblClick>;// shortcut to events: { dblClick: ... }
|
|
96
|
+
contextMenu?: EventCallback<EvContextMenu>;// shortcut to events: { contentMenu: ... }
|
|
97
|
+
selectionChange?: EventCallback<EvSelectionChange>;// shortcut to events: { selectionChange: ... }
|
|
98
|
+
cancel?: EventCallback<EvCancel>; // shortcut to events: { cancel: ... }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Standard listview class
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
export class ListView extends VLayout<ListViewProps,ListViewEventMap> {
|
|
106
|
+
|
|
107
|
+
protected m_selection: {
|
|
108
|
+
item: ListViewItem;
|
|
109
|
+
citem: Component;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
protected m_defer_sel: any;
|
|
113
|
+
|
|
114
|
+
protected m_container: Container;
|
|
115
|
+
protected m_view: Container;
|
|
116
|
+
|
|
117
|
+
protected m_topIndex: number;
|
|
118
|
+
protected m_itemHeight: number;
|
|
119
|
+
|
|
120
|
+
protected m_cache: Map<number, Component>; // recycling elements
|
|
121
|
+
|
|
122
|
+
constructor(props: ListViewProps) {
|
|
123
|
+
super(props);
|
|
124
|
+
|
|
125
|
+
this.setDomEvent('keydown', (e) => this.handleKey(e));
|
|
126
|
+
this.setDomEvent('click', (e) => this._handleClick(e));
|
|
127
|
+
this.setDomEvent('dblclick', (e) => this._handleClick(e));
|
|
128
|
+
this.setDomEvent('contextmenu', (e) => this._handleCtxMenu(e));
|
|
129
|
+
|
|
130
|
+
this._setTabIndex(props.tabIndex, 0);
|
|
131
|
+
this.mapPropEvents(props, 'click', 'dblClick', 'contextMenu', 'selectionChange', 'cancel' );
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
componentCreated() {
|
|
135
|
+
|
|
136
|
+
if (this.m_props.virtual) {
|
|
137
|
+
this._buildItems();
|
|
138
|
+
}
|
|
139
|
+
else if( this.m_props.populate ) {
|
|
140
|
+
this.items = this.m_props.populate( null );
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
render(props: ListViewProps) {
|
|
145
|
+
|
|
146
|
+
props.items = props.items || [];
|
|
147
|
+
props.gadgets = props.gadgets;
|
|
148
|
+
props.renderItem = props.renderItem;
|
|
149
|
+
props.virtual = props.virtual ?? false;
|
|
150
|
+
|
|
151
|
+
this.m_topIndex = 0;
|
|
152
|
+
|
|
153
|
+
if (props.virtual) {
|
|
154
|
+
console.assert(props.itemHeight !== undefined);
|
|
155
|
+
this.m_itemHeight = props.itemHeight;
|
|
156
|
+
this.m_cache = new Map<number, Component>();
|
|
157
|
+
this.addClass('virtual');
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
this.m_itemHeight = undefined;
|
|
161
|
+
this.m_cache = undefined;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this._buildContent();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* change the list of item displayed
|
|
169
|
+
* @param items - new array of items
|
|
170
|
+
* @deprecated
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
public set items(items: ListViewItem[]) {
|
|
174
|
+
this.setItems( items );
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public get items( ) {
|
|
178
|
+
return this.m_props.items;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* change the list of item displayed
|
|
183
|
+
* @param items - new array of items
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
public setItems( items: ListViewItem[], keepSel = true ) {
|
|
187
|
+
this.m_props.items = items;
|
|
188
|
+
|
|
189
|
+
if( !keepSel ) {
|
|
190
|
+
this.m_selection = null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if( !this.m_container ) {
|
|
194
|
+
this._buildContent( );
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this._buildItems( );
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public handleKey(ev: KeyboardEvent) {
|
|
202
|
+
|
|
203
|
+
let moveSel = (sens) => {
|
|
204
|
+
|
|
205
|
+
let items: ListViewItem[];
|
|
206
|
+
if (isFunction(this.m_props.items)) {
|
|
207
|
+
items = this.m_props.items();
|
|
208
|
+
this.m_props.items = items;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
items = this.m_props.items;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let newsel: ListViewItem;
|
|
215
|
+
if (!this.m_selection) {
|
|
216
|
+
if (items) {
|
|
217
|
+
newsel = items[0];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
let index = items.findIndex((item) => item === this.m_selection.item);
|
|
222
|
+
if (sens > 0 && index < (items.length - 1)) {
|
|
223
|
+
newsel = items[index + 1];
|
|
224
|
+
}
|
|
225
|
+
else if (sens < 0 && index > 0) {
|
|
226
|
+
newsel = items[index - 1];
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
newsel = this.selection
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
let citem = this._findItemWithId(newsel?.id);
|
|
234
|
+
this._selectItem(newsel, citem, true);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
switch (ev.key) {
|
|
238
|
+
case 'ArrowDown': {
|
|
239
|
+
moveSel(1);
|
|
240
|
+
ev.preventDefault( );
|
|
241
|
+
ev.stopPropagation();
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
case 'ArrowUp': {
|
|
246
|
+
moveSel(-1);
|
|
247
|
+
ev.preventDefault( );
|
|
248
|
+
ev.stopPropagation();
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** @ignore */
|
|
255
|
+
private _buildContent() {
|
|
256
|
+
|
|
257
|
+
let props = this.m_props;
|
|
258
|
+
|
|
259
|
+
if (props.virtual) {
|
|
260
|
+
this.m_container = new Container({
|
|
261
|
+
cls: '@scroll-container',
|
|
262
|
+
content: []
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
this.m_view = new Container({
|
|
266
|
+
cls: '@scroll-view',
|
|
267
|
+
flex: 1,
|
|
268
|
+
content: this.m_container,
|
|
269
|
+
dom_events: {
|
|
270
|
+
sizechange: () => this._updateScroll(true),
|
|
271
|
+
scroll: () => this._updateScroll(false),
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
this.setContent(
|
|
276
|
+
[
|
|
277
|
+
this.m_view,
|
|
278
|
+
props.gadgets ? new HLayout({
|
|
279
|
+
cls: 'gadgets',
|
|
280
|
+
content: props.gadgets
|
|
281
|
+
}) : null,
|
|
282
|
+
]
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
this.m_view = undefined;
|
|
287
|
+
this.m_container = new VLayout({
|
|
288
|
+
cls: '@scroll-container',
|
|
289
|
+
content: []
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
this.addClass('@scroll-view');
|
|
293
|
+
this.setContent(this.m_container, false);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (props.virtual) {
|
|
297
|
+
this.m_container.setStyleValue('height', props.items.length * this.m_itemHeight);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (this.dom || !props.virtual ) {
|
|
301
|
+
this._buildItems();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
*
|
|
307
|
+
*/
|
|
308
|
+
|
|
309
|
+
private _updateScroll(forceUpdate: boolean) {
|
|
310
|
+
|
|
311
|
+
const update = () => {
|
|
312
|
+
let newTop = Math.floor(this.m_view.dom.scrollTop / this.m_itemHeight);
|
|
313
|
+
|
|
314
|
+
if (newTop != this.m_topIndex || forceUpdate) {
|
|
315
|
+
this.m_topIndex = newTop;
|
|
316
|
+
this._buildItems();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (forceUpdate) {
|
|
321
|
+
this.startTimer('scroll', 10, false, update);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
update();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private async _buildItems() {
|
|
329
|
+
let props = this.m_props;
|
|
330
|
+
let items: Component[] = [];
|
|
331
|
+
|
|
332
|
+
let list_items = props.items;
|
|
333
|
+
if (isFunction(list_items)) {
|
|
334
|
+
list_items = list_items();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
let selId = this.m_selection?.item.id;
|
|
338
|
+
let selFnd = false;
|
|
339
|
+
|
|
340
|
+
if (props.virtual) {
|
|
341
|
+
let rc = this.getBoundingRect();
|
|
342
|
+
let limit = 100;
|
|
343
|
+
|
|
344
|
+
let y = 0;
|
|
345
|
+
let top = this.m_topIndex * this.m_itemHeight;
|
|
346
|
+
let index = this.m_topIndex;
|
|
347
|
+
let height = rc.height;
|
|
348
|
+
let count = props.items.length;
|
|
349
|
+
|
|
350
|
+
let newels = [];
|
|
351
|
+
|
|
352
|
+
let cache = this.m_cache;
|
|
353
|
+
this.m_cache = new Map<number, Component>();
|
|
354
|
+
|
|
355
|
+
while (y < height && index < count && --limit > 0) {
|
|
356
|
+
|
|
357
|
+
let it = props.items[index];
|
|
358
|
+
let itm: Component;
|
|
359
|
+
|
|
360
|
+
if (cache.has(it.id)) {
|
|
361
|
+
itm = cache.get(it.id); // reuse it
|
|
362
|
+
cache.delete(it.id); // cache will contain only elements to remove
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
itm = this._renderItem(it);
|
|
366
|
+
newels.push(itm);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (selId == it.id) {
|
|
370
|
+
itm.addClass('@selected');
|
|
371
|
+
selFnd = true;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
itm.setStyleValue('top', top + y);
|
|
375
|
+
items.push(itm);
|
|
376
|
+
|
|
377
|
+
this.m_cache.set(it.id, itm); // keep it for next time
|
|
378
|
+
|
|
379
|
+
y += this.m_itemHeight;
|
|
380
|
+
index++;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// all element remaining here are to remove
|
|
384
|
+
cache.forEach((c) => {
|
|
385
|
+
c.dispose();
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// append new elements
|
|
389
|
+
this.m_container.appendChild(newels);
|
|
390
|
+
this.m_container.setStyleValue( 'height', count*this.m_itemHeight );
|
|
391
|
+
|
|
392
|
+
// check that it's still existing
|
|
393
|
+
if( !selFnd ) {
|
|
394
|
+
if( !list_items.some( it => selId == it.id ) ) {
|
|
395
|
+
this.m_selection = null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
list_items.forEach((it) => {
|
|
402
|
+
let itm = this._renderItem(it);
|
|
403
|
+
if (selId == it.id) {
|
|
404
|
+
itm.addClass('@selected');
|
|
405
|
+
selFnd = true;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
items.push(itm);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
this.m_container.setContent(items, false);
|
|
412
|
+
|
|
413
|
+
if( !selFnd ) {
|
|
414
|
+
this.m_selection = null;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (this.m_defer_sel) {
|
|
419
|
+
let t = this.m_defer_sel;
|
|
420
|
+
this.m_defer_sel = undefined;
|
|
421
|
+
this.selection = t;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** @ignore
|
|
426
|
+
* default rendering of an item
|
|
427
|
+
*/
|
|
428
|
+
|
|
429
|
+
private _renderItem(item: ListViewItem): Component {
|
|
430
|
+
|
|
431
|
+
const newItem = this.onRenderItem( item );
|
|
432
|
+
|
|
433
|
+
newItem.setAttribute( 'data-id', item.id );
|
|
434
|
+
newItem.addClass('@list-item');
|
|
435
|
+
newItem.setData('item-id', item.id);
|
|
436
|
+
|
|
437
|
+
return newItem;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
onRenderItem( item: ListViewItem): Component {
|
|
441
|
+
if (this.m_props.renderItem) {
|
|
442
|
+
return this.m_props.renderItem(item);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
return new HLayout({ content: item.text });
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** @ignore */
|
|
450
|
+
private _handleClick(e: MouseEvent) {
|
|
451
|
+
|
|
452
|
+
e.stopImmediatePropagation();
|
|
453
|
+
e.preventDefault( );
|
|
454
|
+
|
|
455
|
+
let dom = e.target as HTMLElement,
|
|
456
|
+
self = this.dom,
|
|
457
|
+
list_items = this.m_props.items as ListViewItem[]; // already created by build
|
|
458
|
+
|
|
459
|
+
// go up until we find something interesting
|
|
460
|
+
while (dom && dom != self) {
|
|
461
|
+
let itm = Component.getElement(dom),
|
|
462
|
+
id = itm?.getData('item-id');
|
|
463
|
+
|
|
464
|
+
if (id!==undefined) {
|
|
465
|
+
// find the element
|
|
466
|
+
let item = list_items.find((item) => item.id == id);
|
|
467
|
+
if (item) {
|
|
468
|
+
let event: BasicEvent;
|
|
469
|
+
if (e.type == 'click') {
|
|
470
|
+
event = EvClick(item);
|
|
471
|
+
this.emit('click', event);
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
event = EvDblClick(item);
|
|
475
|
+
this.emit('dblClick', event);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (!event.defaultPrevented) {
|
|
479
|
+
this._selectItem(item, itm);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
this._selectItem(null, null);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
dom = dom.parentElement;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
this._selectItem(null, null);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** @ignore */
|
|
496
|
+
private _handleCtxMenu(e: MouseEvent) {
|
|
497
|
+
|
|
498
|
+
e.preventDefault();
|
|
499
|
+
|
|
500
|
+
let dom = e.target as HTMLElement,
|
|
501
|
+
self = this.dom,
|
|
502
|
+
list_items = this.m_props.items as ListViewItem[]; // already created by build;
|
|
503
|
+
|
|
504
|
+
while (dom && dom != self) {
|
|
505
|
+
let itm = Component.getElement(dom),
|
|
506
|
+
id = itm?.getData('item-id');
|
|
507
|
+
|
|
508
|
+
if (id) {
|
|
509
|
+
|
|
510
|
+
// find the element
|
|
511
|
+
let item = list_items.find((item) => item.id == id);
|
|
512
|
+
if (item) {
|
|
513
|
+
this._selectItem(item, itm);
|
|
514
|
+
this.emit('contextMenu', EvContextMenu(e, item));
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
dom = dom.parentElement;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
this.emit('contextMenu', EvContextMenu(e, null));
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* @ignore
|
|
528
|
+
* called when an item is selected by mouse
|
|
529
|
+
*/
|
|
530
|
+
|
|
531
|
+
protected _selectItem(item: ListViewItem, citem: Component, notify = true) {
|
|
532
|
+
|
|
533
|
+
if (this.m_selection && this.m_selection.citem) {
|
|
534
|
+
this.m_selection.citem.removeClass('@selected');
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
this.m_selection = item ? {
|
|
538
|
+
item: item,
|
|
539
|
+
citem: citem
|
|
540
|
+
} : null;
|
|
541
|
+
|
|
542
|
+
if (this.m_selection && this.m_selection.citem) {
|
|
543
|
+
this.m_selection.citem.addClass('@selected');
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (notify) {
|
|
547
|
+
this.emit('selectionChange', EvSelectionChange(item));
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* return the current selection or null
|
|
553
|
+
*/
|
|
554
|
+
|
|
555
|
+
public get selection() {
|
|
556
|
+
return this.m_selection ? this.m_selection.item : null;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
public set selection(id: any) {
|
|
560
|
+
if (id === null || id === undefined) {
|
|
561
|
+
this._selectItem(null, null);
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
if (isFunction(this.m_props.items)) {
|
|
565
|
+
this.m_defer_sel = id;
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
let item = this.m_props.items.find((item) => item.id == id);
|
|
569
|
+
let citem = this._findItemWithId(item.id);
|
|
570
|
+
this._selectItem(item, citem, false);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
private _findItemWithId(id: any) {
|
|
576
|
+
let citem: Component = null;
|
|
577
|
+
|
|
578
|
+
if (this.dom) {
|
|
579
|
+
// make the element visible to user
|
|
580
|
+
// todo: problem with virtual listview
|
|
581
|
+
this.m_container.enumChilds((c: Component) => {
|
|
582
|
+
if (c.getData('item-id') == id) {
|
|
583
|
+
c.scrollIntoView();
|
|
584
|
+
citem = c;
|
|
585
|
+
return true;
|
|
586
|
+
}
|
|
587
|
+
})
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
return citem;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* append or prepend a new item
|
|
595
|
+
* @param item
|
|
596
|
+
* @param prepend
|
|
597
|
+
* @param select
|
|
598
|
+
*/
|
|
599
|
+
|
|
600
|
+
appendItem( item: ListViewItem, prepend = false, select = true ) {
|
|
601
|
+
|
|
602
|
+
if( prepend ) {
|
|
603
|
+
this.m_props.items.unshift( item );
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
this.m_props.items.push( item );
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if( select ) {
|
|
610
|
+
this.selection = null;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if( !this.m_container ) {
|
|
614
|
+
this._buildContent();
|
|
615
|
+
}
|
|
616
|
+
else {
|
|
617
|
+
this._buildItems();
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if( select ) {
|
|
621
|
+
this.selection = item.id;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* update an item
|
|
627
|
+
*/
|
|
628
|
+
|
|
629
|
+
updateItem( id: any, item: ListViewItem ) {
|
|
630
|
+
|
|
631
|
+
// find item
|
|
632
|
+
const idx = this.m_props.items.findIndex( itm => itm.id === id );
|
|
633
|
+
if( idx<0 ) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// take care of selection
|
|
638
|
+
let was_sel = false;
|
|
639
|
+
if( this.m_selection && this.m_selection.item===this.m_props.items[idx] ) {
|
|
640
|
+
was_sel = true;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// replace it in the list
|
|
644
|
+
this.m_props.items[idx] = item;
|
|
645
|
+
|
|
646
|
+
// rebuild & replace it's line
|
|
647
|
+
const oldDOM = this.queryItem( `[data-id="${item.id}"]` )?.dom;
|
|
648
|
+
if( oldDOM ) {
|
|
649
|
+
const _new = this._renderItem( item );
|
|
650
|
+
|
|
651
|
+
if( was_sel ) {
|
|
652
|
+
_new.addClass( '@selected' );
|
|
653
|
+
this.m_selection.citem = _new;
|
|
654
|
+
this.m_selection.item = item;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const newDOM = _new._build( );
|
|
658
|
+
this.m_container.dom.replaceChild( newDOM, oldDOM );
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
/**
|
|
668
|
+
* Cancel Event
|
|
669
|
+
*/
|
|
670
|
+
|
|
671
|
+
export interface EvCancel extends BasicEvent {
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export function EvCancel( context = null ) {
|
|
675
|
+
return BasicEvent<EvCancel>({ context });
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
interface PopupListViewEventMap extends PopupEventMap {
|
|
679
|
+
cancel: EvCancel;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
interface PopupListViewProps extends PopupProps<PopupListViewEventMap> {
|
|
683
|
+
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
*
|
|
689
|
+
*/
|
|
690
|
+
|
|
691
|
+
export class PopupListView extends Popup<PopupListViewProps,PopupListViewEventMap> {
|
|
692
|
+
|
|
693
|
+
m_list: ListView;
|
|
694
|
+
|
|
695
|
+
constructor(props: ListViewProps) {
|
|
696
|
+
super({ tabIndex: false });
|
|
697
|
+
|
|
698
|
+
this.enableMask(false);
|
|
699
|
+
this.addClass( "@non-maskable" );
|
|
700
|
+
|
|
701
|
+
props.tabIndex = false;
|
|
702
|
+
this.m_list = new ListView(props);
|
|
703
|
+
//this.m_list.addClass( '@fit' );
|
|
704
|
+
|
|
705
|
+
this.setContent(this.m_list);
|
|
706
|
+
this.mapPropEvents( props, 'cancel' );
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
set items( items: ListViewItem[] ) {
|
|
710
|
+
this.m_list.items = items;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
public handleKey( ev: KeyboardEvent ) {
|
|
714
|
+
this.m_list.handleKey( ev );
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
|
|
718
|
+
// todo: move into popup
|
|
719
|
+
private _handleClick = (e: MouseEvent) => {
|
|
720
|
+
if (!this.dom) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
let newfocus = <HTMLElement>e.target;
|
|
725
|
+
|
|
726
|
+
// child of this: ok
|
|
727
|
+
if (this.dom.contains(newfocus)) {
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// menu: ok
|
|
732
|
+
let dest = Component.getElement(newfocus, MenuItem);
|
|
733
|
+
if (dest) {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
this.signal( 'cancel', EvCancel() );
|
|
738
|
+
this.close();
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// todo: move into popup
|
|
742
|
+
show(modal?: boolean) {
|
|
743
|
+
x4document.addEventListener('mousedown', this._handleClick);
|
|
744
|
+
super.show(modal);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
hide( ) {
|
|
748
|
+
x4document.removeEventListener('mousedown', this._handleClick);
|
|
749
|
+
super.hide( );
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// todo: move into popup
|
|
753
|
+
close() {
|
|
754
|
+
x4document.removeEventListener('mousedown', this._handleClick);
|
|
755
|
+
super.close();
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
get selection(): any {
|
|
759
|
+
return this.m_list.selection;
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
set selection(itemId: any) {
|
|
763
|
+
this.m_list.selection = itemId;
|
|
764
|
+
}
|
|
765
|
+
}
|