-
-
Notifications
You must be signed in to change notification settings - Fork 245
/
config.ts
198 lines (180 loc) · 5.13 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { CSSDeclaration, OneDArray, Status, TColumn, TData } from './types';
import Storage from './storage/storage';
import Pipeline from './pipeline/pipeline';
import Tabular from './tabular';
import { Search, SearchConfig } from './view/plugin/search/search';
import { Pagination, PaginationConfig } from './view/plugin/pagination';
import Header from './header';
import { ServerStorageOptions } from './storage/server';
import { GenericSortConfig } from './view/plugin/sort/sort';
import { Language, Translator } from './i18n/language';
import { ComponentChild, createContext, createRef, RefObject } from 'preact';
import StorageUtils from './storage/storageUtils';
import PipelineUtils from './pipeline/pipelineUtils';
import { EventEmitter } from './util/eventEmitter';
import { GridEvents } from './events';
import { PluginManager, PluginPosition, Plugin } from './plugin';
import Grid from './grid';
import { Store } from './state/store';
export const ConfigContext = createContext(null);
export interface Config {
// a reference to the current Grid.js instance
instance: Grid;
store: Store;
eventEmitter: EventEmitter<GridEvents>;
plugin: PluginManager;
/** container element that is used to mount the Grid.js to */
// TODO: change this to an element reference
container?: Element;
/** pointer to the main table element */
tableRef?: RefObject<HTMLTableElement>;
data?: TData | (() => TData) | (() => Promise<TData>);
server?: ServerStorageOptions;
header?: Header;
/** to parse a HTML table and load the data */
from: HTMLElement;
storage: Storage<any>;
/** Pipeline process throttle timeout in milliseconds */
processingThrottleMs: number;
pipeline: Pipeline<Tabular>;
/** to automatically calculate the columns width */
autoWidth: boolean;
/** sets the width of the container and table */
width: string;
/** sets the height of the table */
height: string;
pagination: PaginationConfig | boolean;
sort: GenericSortConfig | boolean;
translator: Translator;
/** fixes the table header to the top of the table */
fixedHeader: boolean;
/** Resizable columns? */
resizable: boolean;
columns: OneDArray<TColumn | string | ComponentChild>;
search: SearchConfig | boolean;
language: Language;
plugins?: Plugin<any>[];
style?: Partial<{
table: CSSDeclaration;
td: CSSDeclaration;
th: CSSDeclaration;
container: CSSDeclaration;
header: CSSDeclaration;
footer: CSSDeclaration;
}>;
className?: Partial<{
table: string;
th: string;
thead: string;
tbody: string;
tr: string;
td: string;
container: string;
footer: string;
header: string;
search: string;
sort: string;
pagination: string;
paginationSummary: string;
paginationButton: string;
paginationButtonNext: string;
paginationButtonCurrent: string;
paginationButtonPrev: string;
loading: string;
notfound: string;
error: string;
}>;
}
export class Config {
public constructor() {
Object.assign(this, Config.defaultConfig());
}
/**
* Assigns `updatedConfig` keys to the current config file
*
* @param partialConfig
*/
assign(partialConfig: Partial<Config>): Config {
return Object.assign(this, partialConfig);
}
/**
* Updates the config from a partial Config
*
* @param partialConfig
*/
update(partialConfig: Partial<Config>): Config {
if (!partialConfig) return this;
this.assign(
Config.fromPartialConfig({
...this,
...partialConfig,
}),
);
return this;
}
static defaultConfig(): Partial<Config> {
return {
store: new Store({
status: Status.Init,
header: undefined,
data: null,
}),
plugin: new PluginManager(),
tableRef: createRef(),
width: '100%',
height: 'auto',
processingThrottleMs: 100,
autoWidth: true,
style: {},
className: {},
};
}
static fromPartialConfig(partialConfig: Partial<Config>): Partial<Config> {
const config = new Config().assign(partialConfig);
// Sort
if (typeof partialConfig.sort === 'boolean' && partialConfig.sort) {
config.assign({
sort: {
multiColumn: true,
},
});
}
// Header
config.assign({
header: Header.createFromConfig(config),
});
config.assign({
storage: StorageUtils.createFromConfig(config),
});
config.assign({
pipeline: PipelineUtils.createFromConfig(config),
});
// Translator
config.assign({
translator: new Translator(config.language),
});
// clear existing plugins list to prevent duplicate errors
config.plugin = new PluginManager();
if (config.search) {
// Search
config.plugin.add({
id: 'search',
position: PluginPosition.Header,
component: Search,
});
}
if (config.pagination) {
// Pagination
config.plugin.add({
id: 'pagination',
position: PluginPosition.Footer,
component: Pagination,
});
}
// Additional plugins
if (config.plugins) {
config.plugins.forEach((p) => config.plugin.add(p));
}
return config;
}
}