ui-grid-core 0.1.7

Rust engine for ui-grid
Documentation
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::constants::{FilterCondition, SortDirection};

pub type GridRecord = Value;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub enum GridColumnType {
    #[default]
    String,
    Number,
    Boolean,
    Date,
    Object,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridFilterFlags {
    #[serde(default)]
    pub case_sensitive: bool,
    #[serde(default)]
    pub date: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridFilterDescriptor {
    #[serde(default)]
    pub term: Option<Value>,
    #[serde(default)]
    pub condition: Option<FilterCondition>,
    #[serde(default)]
    pub flags: GridFilterFlags,
    #[serde(default)]
    pub raw_term: bool,
    #[serde(default)]
    pub no_term: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridSortDescriptor {
    #[serde(default)]
    pub direction: Option<SortDirection>,
    #[serde(default)]
    pub priority: Option<u32>,
    #[serde(default)]
    pub ignore_sort: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GridColumnDef {
    pub name: String,
    #[serde(default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub field: Option<String>,
    #[serde(default)]
    pub r#type: GridColumnType,
    #[serde(default = "default_true")]
    pub visible: bool,
    #[serde(default = "default_true")]
    pub sortable: bool,
    #[serde(default = "default_true")]
    pub filterable: bool,
    #[serde(default = "default_true")]
    pub enable_sorting: bool,
    #[serde(default = "default_true")]
    pub enable_filtering: bool,
    #[serde(default = "default_true")]
    pub enable_grouping: bool,
    #[serde(default)]
    pub enable_cell_edit: bool,
    #[serde(default)]
    pub enable_cell_edit_on_focus: bool,
    #[serde(default)]
    pub pinned_left: bool,
    #[serde(default)]
    pub pinned_right: bool,
    #[serde(default = "default_true")]
    pub enable_pinning: bool,
    #[serde(default)]
    pub width: Option<String>,
    #[serde(default)]
    pub align: Option<String>,
    #[serde(default)]
    pub sort: Option<GridSortDescriptor>,
    #[serde(default)]
    pub filter: Option<GridFilterDescriptor>,
}

impl Default for GridColumnDef {
    fn default() -> Self {
        Self {
            name: String::new(),
            display_name: None,
            field: None,
            r#type: GridColumnType::default(),
            visible: true,
            sortable: true,
            filterable: true,
            enable_sorting: true,
            enable_filtering: true,
            enable_grouping: true,
            enable_cell_edit: false,
            enable_cell_edit_on_focus: false,
            pinned_left: false,
            pinned_right: false,
            enable_pinning: true,
            width: None,
            align: None,
            sort: None,
            filter: None,
        }
    }
}

fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridGroupingOptions {
    #[serde(default)]
    pub group_by: Vec<String>,
    #[serde(default)]
    pub start_collapsed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum GridIcon {
    Grip,
    Sort,
    SortAsc,
    SortDesc,
    Group,
    Ungroup,
    ChevronLeft,
    ChevronRight,
    ChevronDown,
    PinLeft,
    PinRight,
    Unpin,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GridIcons {
    pub sort_default: GridIcon,
    pub sort_asc: GridIcon,
    pub sort_desc: GridIcon,
    pub group_column: GridIcon,
    pub ungroup_column: GridIcon,
    pub group_collapse: GridIcon,
    pub group_expand: GridIcon,
    pub tree_collapse: GridIcon,
    pub tree_expand: GridIcon,
    pub expand_detail: GridIcon,
    pub collapse_detail: GridIcon,
    pub drag_handle: GridIcon,
    pub move_left: GridIcon,
    pub move_right: GridIcon,
    pub pin_left: GridIcon,
    pub pin_right: GridIcon,
    pub unpin: GridIcon,
}

impl Default for GridIcons {
    fn default() -> Self {
        Self {
            sort_default: GridIcon::Sort,
            sort_asc: GridIcon::SortAsc,
            sort_desc: GridIcon::SortDesc,
            group_column: GridIcon::Group,
            ungroup_column: GridIcon::Ungroup,
            group_collapse: GridIcon::ChevronDown,
            group_expand: GridIcon::ChevronRight,
            tree_collapse: GridIcon::ChevronDown,
            tree_expand: GridIcon::ChevronRight,
            expand_detail: GridIcon::ChevronDown,
            collapse_detail: GridIcon::ChevronDown,
            drag_handle: GridIcon::Grip,
            move_left: GridIcon::ChevronLeft,
            move_right: GridIcon::ChevronRight,
            pin_left: GridIcon::PinLeft,
            pin_right: GridIcon::PinRight,
            unpin: GridIcon::Unpin,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GridLabels {
    pub sort_default: String,
    pub sort_asc: String,
    pub sort_desc: String,
    pub group_column: String,
    pub ungroup_column: String,
    pub group_collapse: String,
    pub group_expand: String,
    pub tree_collapse: String,
    pub tree_expand: String,
    pub expand_detail: String,
    pub collapse_detail: String,
    pub filter_placeholder: String,
    pub filter_disabled: String,
    pub filter_column: String,
    pub pagination_previous: String,
    pub pagination_next: String,
    pub pagination_page: String,
    pub pagination_of: String,
    pub pagination_rows: String,
    pub empty_heading: String,
    pub empty_description: String,
    pub toolbar_of: String,
    pub toolbar_rows: String,
    pub stats_visible_rows: String,
    pub group_rows_suffix: String,
    pub pin_column: String,
    pub pin_left: String,
    pub pin_right: String,
    pub unpin: String,
}

impl Default for GridLabels {
    fn default() -> Self {
        Self {
            sort_default: "Sort".to_string(),
            sort_asc: "Sort ascending".to_string(),
            sort_desc: "Sort descending".to_string(),
            group_column: "Group by this column".to_string(),
            ungroup_column: "Remove grouping".to_string(),
            group_collapse: "Collapse group".to_string(),
            group_expand: "Expand group".to_string(),
            tree_collapse: "Collapse row".to_string(),
            tree_expand: "Expand row".to_string(),
            expand_detail: "Expand details".to_string(),
            collapse_detail: "Collapse details".to_string(),
            filter_placeholder: "Filter…".to_string(),
            filter_disabled: "Filter disabled".to_string(),
            filter_column: "Filter".to_string(),
            pagination_previous: "Previous page".to_string(),
            pagination_next: "Next page".to_string(),
            pagination_page: "Page".to_string(),
            pagination_of: "of".to_string(),
            pagination_rows: "Rows per page".to_string(),
            empty_heading: "No matching rows".to_string(),
            empty_description: "Adjust the active filters, grouping, or sort order.".to_string(),
            toolbar_of: "of".to_string(),
            toolbar_rows: "rows".to_string(),
            stats_visible_rows: "visible rows".to_string(),
            group_rows_suffix: "rows".to_string(),
            pin_column: "Pin column".to_string(),
            pin_left: "Pin left".to_string(),
            pin_right: "Pin right".to_string(),
            unpin: "Unpin".to_string(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridInfiniteScrollState {
    #[serde(default)]
    pub scroll_up: bool,
    #[serde(default)]
    pub scroll_down: bool,
    #[serde(default)]
    pub data_loading: bool,
    #[serde(default)]
    pub previous_visible_rows: usize,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GridOptions {
    pub id: String,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub data: Vec<GridRecord>,
    #[serde(default)]
    pub column_defs: Vec<GridColumnDef>,
    #[serde(default)]
    pub labels: GridLabels,
    #[serde(default)]
    pub icons: GridIcons,
    #[serde(default = "default_true")]
    pub enable_sorting: bool,
    #[serde(default = "default_true")]
    pub enable_filtering: bool,
    #[serde(default)]
    pub enable_grouping: bool,
    #[serde(default)]
    pub enable_column_moving: bool,
    #[serde(default)]
    pub enable_cell_edit: bool,
    #[serde(default)]
    pub enable_cell_edit_on_focus: bool,
    #[serde(default = "default_true")]
    pub enable_virtualization: bool,
    #[serde(default)]
    pub enable_pagination: bool,
    #[serde(default = "default_true")]
    pub enable_pagination_controls: bool,
    #[serde(default)]
    pub use_external_pagination: bool,
    #[serde(default)]
    pub pagination_page_sizes: Vec<usize>,
    #[serde(default)]
    pub pagination_page_size: Option<usize>,
    #[serde(default)]
    pub pagination_current_page: Option<usize>,
    #[serde(default)]
    pub total_items: Option<usize>,
    #[serde(default)]
    pub enable_expandable: bool,
    #[serde(default)]
    pub expandable_row_height: Option<usize>,
    #[serde(default)]
    pub expandable_row_header_width: Option<usize>,
    #[serde(default)]
    pub enable_tree_view: bool,
    #[serde(default)]
    pub tree_children_field: Option<String>,
    #[serde(default)]
    pub tree_indent: Option<usize>,
    #[serde(default = "default_true")]
    pub show_tree_expand_no_children: bool,
    #[serde(default)]
    pub tree_row_header_always_visible: bool,
    #[serde(default)]
    pub enable_auto_resize: bool,
    #[serde(default)]
    pub infinite_scroll_rows_from_end: Option<usize>,
    #[serde(default)]
    pub infinite_scroll_up: bool,
    #[serde(default)]
    pub infinite_scroll_down: Option<bool>,
    #[serde(default)]
    pub virtualization_threshold: Option<usize>,
    #[serde(default)]
    pub viewport_height: Option<usize>,
    #[serde(default)]
    pub grouping: Option<GridGroupingOptions>,
    #[serde(default)]
    pub enable_pinning: bool,
    #[serde(default)]
    pub row_id_field: Option<String>,
}

impl Default for GridOptions {
    fn default() -> Self {
        Self {
            id: String::new(),
            title: None,
            data: Vec::new(),
            column_defs: Vec::new(),
            labels: GridLabels::default(),
            icons: GridIcons::default(),
            enable_sorting: true,
            enable_filtering: false,
            enable_grouping: false,
            enable_column_moving: false,
            enable_cell_edit: false,
            enable_cell_edit_on_focus: false,
            enable_virtualization: true,
            enable_pagination: false,
            enable_pagination_controls: false,
            use_external_pagination: false,
            pagination_page_sizes: Vec::new(),
            pagination_page_size: None,
            pagination_current_page: None,
            total_items: None,
            enable_expandable: false,
            expandable_row_height: None,
            expandable_row_header_width: None,
            enable_tree_view: false,
            tree_children_field: None,
            tree_indent: None,
            show_tree_expand_no_children: false,
            tree_row_header_always_visible: false,
            enable_auto_resize: false,
            infinite_scroll_rows_from_end: None,
            infinite_scroll_up: false,
            infinite_scroll_down: None,
            virtualization_threshold: None,
            viewport_height: None,
            grouping: None,
            enable_pinning: false,
            row_id_field: Some("id".to_string()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridCellPosition {
    pub row_id: String,
    pub column_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct SortState {
    #[serde(default)]
    pub column_name: Option<String>,
    #[serde(default)]
    pub direction: SortDirection,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridSavedPaginationState {
    pub pagination_current_page: usize,
    pub pagination_page_size: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GridSavedState {
    #[serde(default)]
    pub column_order: Vec<String>,
    #[serde(default)]
    pub filters: BTreeMap<String, String>,
    #[serde(default)]
    pub sort: Option<SortState>,
    #[serde(default)]
    pub grouping: Vec<String>,
    #[serde(default)]
    pub pagination: Option<GridSavedPaginationState>,
    #[serde(default)]
    pub expandable: BTreeMap<String, bool>,
    #[serde(default)]
    pub tree_view: BTreeMap<String, bool>,
    #[serde(default)]
    pub pinning: BTreeMap<String, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GridRow {
    pub id: String,
    pub entity: GridRecord,
    pub index: usize,
    pub height: usize,
    #[serde(default)]
    pub invisible_reasons: Vec<String>,
    #[serde(default = "default_true")]
    pub visible: bool,
    #[serde(default)]
    pub is_selected: bool,
    #[serde(default)]
    pub tree_level: usize,
    #[serde(default)]
    pub parent_id: Option<String>,
    #[serde(default)]
    pub has_children: bool,
    #[serde(default)]
    pub child_count: usize,
    #[serde(default)]
    pub expanded: bool,
    #[serde(default)]
    pub expanded_row_height: usize,
}

impl GridRow {
    pub fn new(id: String, entity: GridRecord, index: usize, height: usize) -> Self {
        Self {
            id,
            entity,
            index,
            height,
            invisible_reasons: Vec::new(),
            visible: true,
            is_selected: false,
            tree_level: 0,
            parent_id: None,
            has_children: false,
            child_count: 0,
            expanded: false,
            expanded_row_height: 0,
        }
    }

    pub fn set_this_row_invisible(&mut self, reason: impl Into<String>) {
        let reason = reason.into();
        if !self
            .invisible_reasons
            .iter()
            .any(|existing| existing == &reason)
        {
            self.invisible_reasons.push(reason);
        }
        self.visible = false;
    }

    pub fn clear_this_row_invisible(&mut self, reason: &str) {
        self.invisible_reasons.retain(|existing| existing != reason);
        self.visible = self.invisible_reasons.is_empty();
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupItem {
    pub id: String,
    pub depth: usize,
    pub field: String,
    pub label: String,
    pub count: usize,
    pub collapsed: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RowItem {
    pub id: String,
    pub row: GridRow,
    pub visible_index: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpandableItem {
    pub id: String,
    pub row: GridRow,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum DisplayItem {
    Group(GroupItem),
    Row(RowItem),
    Expandable(ExpandableItem),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct BuildGridPipelineContext {
    pub options: GridOptions,
    #[serde(default)]
    pub columns: Vec<GridColumnDef>,
    #[serde(default)]
    pub active_filters: BTreeMap<String, String>,
    #[serde(default)]
    pub sort_state: SortState,
    #[serde(default)]
    pub group_by_columns: Vec<String>,
    #[serde(default)]
    pub collapsed_groups: BTreeMap<String, bool>,
    #[serde(default)]
    pub hidden_row_reasons: BTreeMap<String, Vec<String>>,
    #[serde(default)]
    pub expanded_rows: BTreeMap<String, bool>,
    #[serde(default)]
    pub expanded_tree_rows: BTreeMap<String, bool>,
    #[serde(default = "default_current_page")]
    pub current_page: usize,
    #[serde(default)]
    pub page_size: usize,
    #[serde(default = "default_row_size")]
    pub row_size: usize,
}

fn default_current_page() -> usize {
    1
}

fn default_row_size() -> usize {
    44
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct PipelineResult {
    #[serde(default)]
    pub visible_rows: Vec<GridRow>,
    #[serde(default)]
    pub display_items: Vec<DisplayItem>,
    #[serde(default)]
    pub virtualization_enabled: bool,
    #[serde(default)]
    pub pipeline_ms: f64,
    #[serde(default)]
    pub total_items: usize,
}