-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractDataView.java
2772 lines (2366 loc) · 88.4 KB
/
AbstractDataView.java
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2016 GwtMaterialDesign
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package gwt.material.design.client.data;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.Range;
import gwt.material.design.client.base.MaterialWidget;
import gwt.material.design.client.base.constants.TableCssName;
import gwt.material.design.client.base.density.Density;
import gwt.material.design.client.base.density.DisplayDensity;
import gwt.material.design.client.base.mixin.CssNameMixin;
import gwt.material.design.client.constants.IconType;
import gwt.material.design.client.data.component.*;
import gwt.material.design.client.data.component.CategoryComponent.OrphanCategoryComponent;
import gwt.material.design.client.data.events.*;
import gwt.material.design.client.data.factory.Category;
import gwt.material.design.client.data.factory.CategoryComponentFactory;
import gwt.material.design.client.data.factory.RowComponentFactory;
import gwt.material.design.client.jquery.JQueryExtension;
import gwt.material.design.client.jquery.JQueryMutate;
import gwt.material.design.client.js.*;
import gwt.material.design.client.ui.MaterialCheckBox;
import gwt.material.design.client.ui.MaterialProgress;
import gwt.material.design.client.ui.Selectors;
import gwt.material.design.client.ui.accessibility.DataTableAccessibilityControls;
import gwt.material.design.client.ui.table.*;
import gwt.material.design.client.ui.table.cell.*;
import gwt.material.design.jquery.client.api.Event;
import gwt.material.design.jquery.client.api.Functions;
import gwt.material.design.jquery.client.api.JQueryElement;
import gwt.material.design.jquery.client.api.MouseEvent;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static gwt.material.design.jquery.client.api.JQuery.$;
import static gwt.material.design.jquery.client.api.JQuery.window;
/**
* Abstract DataView handles the creation, preparation and UI logic for
* the table rows and subheaders (if enabled). All of the basic table
* rendering is handled.
*
* @param <T>
* @author Ben Dol
*/
public abstract class AbstractDataView<T> implements DataView<T> {
private static final Logger logger = Logger.getLogger(AbstractDataView.class.getName());
// Main
protected final String id;
protected DataDisplay<T> display;
protected DataSource<T> dataSource;
protected Renderer<T> renderer;
protected SortContext<T> sortContext;
protected Column<T, ?> autoSortColumn;
protected RowComponentFactory<T> rowFactory;
protected ComponentFactory<? extends CategoryComponent, Category> categoryFactory;
protected ProvidesKey<T> keyProvider;
//protected List<ComponentFactory<?, T>> componentFactories;
protected JsTableSubHeaders subheaderLib;
protected XScrollPanel xScrollPanel;
protected String height;
protected int categoryHeight = 0;
protected int frozenMarginLeft;
protected int frozenMarginRight;
protected boolean rendering;
protected boolean redraw;
protected boolean redrawCategories;
private boolean pendingRenderEvent;
private Style.TableLayout tableLayout;
private long lastRowClicked;
// DOM
protected Table table;
protected MaterialWidget thead;
protected MaterialWidget tbody;
protected TableFooter<T> footer;
protected MaterialProgress progressWidget;
protected TableRow headerRow;
protected JQueryElement container;
protected JsTableElement $table;
protected JQueryElement maskElement;
protected JQueryElement tableBody;
protected JQueryElement innerScroll;
protected JQueryElement topPanel;
// Configurations
protected Range range = new Range(0, 0);
protected int totalRows = 20;
protected int longPressDuration = 500;
protected int leftFrozenColumns = -1;
protected int rightFrozenColumns = -1;
protected int rowClickCooldown = 100;
protected int debounceThrottle = 250;
private int lastSelected;
private boolean setup;
private boolean loadMask;
private boolean shiftDown;
private boolean useRowExpansion;
private boolean useStickyHeader;
private boolean useStickyFooter;
private boolean useLoadOverlay;
private boolean useCategories;
private Boolean helpEnabled;
private Boolean columnTruncate;
private Integer columnMaxWidth;
private SelectionType selectionType = SelectionType.NONE;
private Density density = DisplayDensity.DEFAULT;
private DataTableAccessibilityControls accessibilityControl;
private ColumnFormatProvider defaultFormatProvider;
private String defaultBlankPlaceholder;
// Components
protected final Components<RowComponent<T>> rows = new Components<>();
protected final Components<RowComponent<T>> pendingRows = new Components<>();
protected final Categories<T> categories = new Categories();
// Mixin
protected CssNameMixin<Table, Density> densityCssNameMixin;
// Rendering
protected final List<Column<T, ?>> columns = new ArrayList<>();
protected final List<TableHeader> headers = new ArrayList<>();
protected HandlerRegistration attachHandler;
public static final String ORPHAN_PATTERN = "<@orphans@>";
private static final String expansionHtml = "<tr class='expansion'>" +
"<td class='expansion' colspan='100%'>" +
"<div>" +
"<section class='overlay'>" +
"<div class='progress' style='height:4px;top:-1px;'>" +
"<div class='indeterminate'></div>" +
"</div>" +
"</section>" +
"<div class='content'><br/><br/><br/></div>" +
"</div>" +
"</td></tr>";
public static final String maskHtml = "<div class='mask'>" +
//"<!--i style='left:50%;top:20%;z-index:9999;position:absolute;color:white' class='fa fa-3x fa-spinner fa-spin'></i-->" +
"</div>";
public static final String transitionEvents = "transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd";
public AbstractDataView() {
this("DataView");
}
public AbstractDataView(String id) {
this(id, null);
}
public AbstractDataView(ProvidesKey<T> keyProvider) {
this("DataView", keyProvider);
}
public AbstractDataView(String id, ProvidesKey<T> keyProvider) {
this.id = id;
this.keyProvider = keyProvider;
this.categoryFactory = new CategoryComponentFactory();
this.rowFactory = new RowComponentFactory<>();
this.accessibilityControl = new DataTableAccessibilityControls(this);
//this.componentFactories = new ArrayList<>();
setRenderer(new BaseRenderer<>());
onConstructed();
}
/**
* Called after the data view is constructed.
* Note that this is not when the data view is attached,
* see {@link #setup(TableScaffolding)}.
*/
protected void onConstructed() {
// Do nothing by default
}
@Override
public void render(Components<Component<?>> components) {
// Calculate the frozen columns.
calculateFrozenColumns();
// Clear the current row components
// This does not clear the rows DOM elements
this.rows.clearComponents();
// Render the new components
for (Component<?> component : components) {
renderComponent(component);
}
redraw = false;
prepareRows();
// Reset category indexes and row counts
if (isUseCategories()) {
for (CategoryComponent<T> category : categories) {
category.setCurrentIndex(-1);
category.setRowCount(0);
}
}
// Calculate computed columns.
calculateComputedColumns();
if (!components.isEmpty()) {
// Remove the last attach handler
if (attachHandler != null) {
attachHandler.removeHandler();
}
// When the last component has been rendered we will set the
// rendering flag to false.
Component<?> component = components.get(components.size() - 1);
Widget componentWidget = component.getWidget();
AttachEvent.Handler handler = event -> {
if (attachHandler != null) {
attachHandler.removeHandler();
}
// Recheck the row height to ensure
// the calculated row height is accurate.
getCalculatedRowHeight();
// Fixes an issue with heights updating too early.
// Also ensure the cell widths are updated.
subheaderLib.recalculate(true);
// Fixes an issue with heights updating too early.
subheaderLib.updateHeights();
maybeApplyFrozenMargins();
// Set the x-scroll panels initial width
xScrollPanel.setWidth((innerScroll.asElement().getScrollWidth() + frozenMarginLeft) + "px");
// Clear all row displays when not using categories
// This is an optimization for firefox rendering
if (!isUseCategories()) {
for (RowComponent<T> row : rows) {
row.getWidget().getElement().getStyle().clearDisplay();
}
}
rendering = false;
if (attachHandler != null) {
attachHandler.removeHandler();
}
ComponentsRenderedEvent.fire(this);
buildCategoryColumns();
if (pendingRenderEvent) {
RenderedEvent.fire(this);
pendingRenderEvent = false;
}
};
if (componentWidget == null || componentWidget.isAttached()) {
handler.onAttachOrDetach(null);
} else {
attachHandler = componentWidget.addAttachHandler(handler);
}
} else {
RowEmptyEvent.fire(this);
rendering = false;
}
// Guarantee the rows are visible at this point.
// There are cases where we require the table body to be visible
// before we perform recalculations or pixel chasing logic.
int[] maxTries = {0};
Scheduler.get().scheduleFixedDelay(() -> {
if (tbody.$this().is(":visible")) {
subheaderLib.recalculate(true);
RowsVisibleEvent.fire(this);
return false;
} else {
// we will only attempt to detect this for 5 seconds
return ++maxTries[0] < 125;
}
}, 40);
if (isUseStickyFooter()) {
Scheduler.get().scheduleDeferred(this::setupStickyFooter);
}
}
/**
* Returns a list of the visible headers indexes.
*/
protected List<Integer> getVisibleHeaderIndexes() {
List<Integer> visibleHeaders = new ArrayList<>();
for (int index = 0; index < getColumnCount(); index++) {
if (isHeaderVisible(index)) {
visibleHeaders.add(index);
}
}
return visibleHeaders;
}
/**
* Compile list of {@link RowComponent}'s and invoke a render.
* Controls the building of category components and custom components.
* Which then {@link #render(Components)} is invoked to perform DOM render.
* <p>
* Rows which are already rendered are reprocessed based on their equals method.
* If the data is equal to the row in the same index it use the existing row.
*
* @param rows list of rows to be rendered against the existing rows.
*/
protected boolean renderRows(Components<RowComponent<T>> rows) {
// Make sure we are setup, if we aren't then store the rows
// the rows will be attached upon setup.
if (!setup) {
pendingRows.clear();
pendingRows.addAll(rows);
return false; // early exit, not setup yet.
}
rendering = true;
Range visibleRange = getVisibleRange();
// Cache the visible headers for the renderer
renderer.setVisibleHeaderIndexes(getVisibleHeaderIndexes());
// Check if we need to redraw categories
if (redrawCategories) {
redrawCategories = false;
// When we perform a category redraw we have
// to clear the row elements also.
clearRows(false);
if (isUseCategories()) {
Categories<T> openCategories = getOpenCategories();
categories.clearWidgets();
for (CategoryComponent<T> category : categories) {
// Re-render the category component
renderComponent(category);
if (openCategories.contains(category) && category.isRendered()) {
subheaderLib.open(category.getWidget().$this());
}
}
} else {
categories.clearWidgets();
}
}
// The linear component list.
// This component list will be the rendering
// blueprint for the current view, so the sequence
// is compiled according to the component generation.
Components<Component<?>> components = new Components<>(visibleRange.getLength());
int index = 0;
for (RowComponent<T> row : rows) {
if (components.isFull()) {
break; // Component stack is full, break
}
if (isUseCategories()) {
CategoryComponent<T> category = row.getCategory();
if (category == null) {
category = buildCategoryComponent(row);
if (category != null) {
categories.add(category);
}
}
if (category != null && category.isRendered()) {
category.getWidget().setVisible(true);
}
}
// Do we have an existing row to use
if (index < this.rows.size()) {
RowComponent<T> existingRow = this.rows.get(index);
if (existingRow != null) {
TableRow widget = existingRow.getWidget();
if (widget != null && !row.getWidget().equals(widget)) {
// Replace the rows element with the
// existing indexes element.
row.setWidget(widget);
row.setRedraw(true);
// Rebuild the rows custom components
//existingRow.destroyChildren();
buildCustomComponents(row);
}
}
}
row.setIndex(index++);
components.add(row);
}
// Render the component stack
render(components);
return true;
}
@Override
@SuppressWarnings("unchecked")
public void renderComponent(Component<?> component) {
if (component != null) {
TableRow row;
int index = -1;
if (component instanceof RowComponent) {
RowComponent<T> rowComponent = (RowComponent<T>) component;
// Check if the row has a category
// Categories have been rendered before the rows
CategoryComponent<T> category = null;
if (isUseCategories()) {
category = rowComponent.getCategory();
// Ensure the category exists and is rendered
if (category != null) {
if (!category.isRendered()) {
renderComponent(category);
}
category.addRow(rowComponent);
}
}
T data = rowComponent.getData();
// Draw the table row
row = renderer.drawRow(this, rowComponent, getValueKey(data), columns, redraw);
if (row != null) {
if (category != null) {
if (categories.size() > 1) {
int categoryIndex = 0/*category.getCurrentIndex()*/;
//if(categoryIndex == -1) {
categoryIndex = tbody.getWidgetIndex(category.getWidget());
//category.setCurrentIndex(categoryIndex);
//}
int categoryCount = category.getRowCount() + 1;
category.setRowCount(categoryCount);
// Calculate the rows index
index = (categoryIndex + categoryCount) - 1;
}
// Check the display of the row
TableSubHeader subHeader = category.getWidget();
if (subHeader != null && subHeader.isOpen()) {
row.getElement().getStyle().clearDisplay();
}
} else {
// Not using categories
//row.getElement().getStyle().clearDisplay();
}
rows.add(rowComponent);
}
accessibilityControl.registerRowControl(rowComponent);
} else if (component instanceof CategoryComponent) {
CategoryComponent<T> categoryComponent = (CategoryComponent<T>) component;
row = bindCategoryEvents(renderer.drawCategory(categoryComponent, getColumnCount() - getColumnOffset(), this));
if (categoryComponent.isOpenByDefault()) {
row.addAttachHandler(event -> openCategory(categoryComponent), true);
}
accessibilityControl.registerCategoryControl(categoryComponent);
} else {
row = renderer.drawCustom(component);
}
if (row != null) {
if (row.getParent() == null) {
if (index < 0) {
tbody.add(row);
} else {
tbody.insert(row, index + 1);
}
} else {
// TODO: calculate row element repositioning.
// This will only apply if its index is different
// to the new index generated based on the category.
}
} else {
logger.warning("Attempted to add a null TableRow to tbody, the row was ignored.");
}
}
}
protected void renderColumns() {
for (Column<T, ?> column : columns) {
renderColumn(column);
}
}
public void renderColumn(Column<T, ?> column) {
int index = column.getIndex() + getColumnOffset();
TableHeader th = renderer.drawColumnHeader(getContainer(), column, column.name(), index);
if (th != null) {
if (column.sortable()) {
th.$this().on("click", e -> {
sort(rows, th, column, index);
return true;
});
th.addStyleName(TableCssName.SORTABLE);
accessibilityControl.registerHeaderControl(th);
}
th.setResetSortCallback(() -> {
resetSort();
ColumnResetSortEvent.fire(this);
});
addHeader(index, th);
th.setVisible(!column.isHidden());
}
for (RowComponent<T> row : rows) {
Context context = new Context(row.getIndex(), index, getValueKey(row.getData()));
renderer.drawColumn(row.getWidget(), context, row.getData(), column, index, true);
}
refreshStickyComponents();
}
@Override
public void refresh() {
// Recheck the row height to ensure
// the calculated row height is accurate.
getCalculatedRowHeight();
// Calculate the frozen columns
calculateFrozenColumns();
if (redraw && setup) {
// Render the rows
renderRows(rows);
}
}
@Override
public void setRenderer(Renderer<T> renderer) {
if (this.renderer != null) {
// Copy existing render properties.
renderer.copy(this.renderer);
}
this.renderer = renderer;
}
public Renderer<T> getRenderer() {
return renderer;
}
@Override
public void setDataSource(DataSource<T> dataSource) {
if (dataSource instanceof HasDataView) {
((HasDataView<T>) dataSource).setDataView(this);
}
this.dataSource = dataSource;
}
@Override
public DataSource<T> getDataSource() {
return dataSource;
}
@Override
public JsTableSubHeaders getSubheaderLib() {
return subheaderLib;
}
@Override
public void setup(TableScaffolding scaffolding) throws Exception {
try {
container = $(getContainer());
table = scaffolding.getTable();
tableBody = $(scaffolding.getTableBody());
innerScroll = $(scaffolding.getTableBody()).children("*").first();
topPanel = $(scaffolding.getTopPanel());
xScrollPanel = scaffolding.getXScrollPanel();
tbody = table.getBody();
thead = table.getHead();
footer = table.getFooter();
$table = table.getJsElement();
// apply the table-layout style property
if (tableLayout != null) {
table.getElement().getStyle().setTableLayout(tableLayout);
}
if (density != null) {
applyDensity(density);
}
headerRow = new TableRow();
thead.add(headerRow);
// Create progress widget
progressWidget = new MaterialProgress();
progressWidget.setTop(0);
progressWidget.setGwtDisplay(Display.NONE);
TableRow progressRow = new TableRow();
progressRow.addStyleName(TableCssName.STICKYEXCLUDE);
progressRow.setHeight("3px");
TableData progressTd = new TableData();
progressTd.getElement().setAttribute("colspan", "999");
progressTd.setPadding(0);
progressTd.setHeight("0px");
progressTd.add(progressWidget);
progressRow.add(progressTd);
thead.add(progressRow);
if (isUseRowExpansion()) {
// Add the expand header
TableHeader expandHeader = new TableHeader();
expandHeader.setStyleName(TableCssName.COLEX);
addHeader(0, expandHeader);
}
if (!selectionType.equals(SelectionType.NONE)) {
setupHeaderSelectionBox();
if (selectionType.equals(SelectionType.MULTIPLE)) {
setupShiftDetection();
}
}
// Setup the sticky header bar
if (useStickyHeader) {
setupStickyHeader();
}
// Setup the subheaders for categories
setupSubHeaders();
// Will need to make sure that we only call refresh() only once when mutation called.
Functions.Func2<Element, MutateData> refreshFunction = JsDebounce.debounce(250, (mutateEl, mutateData) -> {
// In the cases where the table is not currently attached.
if (getContainer().isAttached()) {
refresh();
}
});
// Setup the height mutations event handler
JQueryMutate.$(tableBody).mutate("height", refreshFunction);
// XScroll Setup
$table.on("resize." + id, e -> {
xScrollPanel.setWidth((innerScroll.asElement().getScrollWidth() + frozenMarginLeft) + "px");
return true;
});
// Setup inner scroll x scroll binding
JQueryElement $xScrollPanel = $(xScrollPanel);
$xScrollPanel.on("scroll." + id, e -> {
int scrollLeft = $xScrollPanel.scrollLeft();
innerScroll.prop("scrollLeft", scrollLeft);
if (hasFrozenColumns()) {
if (scrollLeft < 1) {
innerScroll.removeClass("inner-shadow");
} else {
innerScroll.addClass("inner-shadow");
}
}
return true;
});
innerScroll.on("scroll." + id, e -> {
$xScrollPanel.prop("scrollLeft", innerScroll.scrollLeft());
return true;
});
JQueryMutate.$(innerScroll).mutate("scrollWidth", (el, info) -> {
// This event is triggered when the component is unloaded.
if (getContainer().isAttached()) {
xScrollPanel.setWidth((el.getScrollWidth() + frozenMarginLeft) + "px");
}
});
setup = true;
onSetup(scaffolding);
SetupEvent.fire(this, scaffolding);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Problem setting up the DataView.", ex);
throw ex;
}
}
protected void onSetup(TableScaffolding scaffolding) {
// We are setup, lets check the render tasks
if (height != null) {
setHeight(height);
}
setSelectionType(selectionType);
// Setup Footer
if (footer != null) {
footer.load();
}
renderColumns();
for (CategoryComponent<T> category : categories) {
if (!category.isRendered()) {
renderCategory(category);
}
}
if (!pendingRows.isEmpty()) {
Components<RowComponent<T>> sortedRows = null;
if (maybeApplyAutoSortColumn()) {
// We have an auto sort column, sort the pending rows.
Column<T, ?> column = sortContext.getSortColumn();
sortedRows = sort(pendingRows, sortContext.getTableHeader(), column, column.getIndex() + getColumnOffset(), false);
}
if (sortedRows == null) {
renderRows(pendingRows);
pendingRows.clearComponents();
} else {
renderRows(sortedRows);
}
}
}
@Override
public void destroy() {
clearRows(true);
categories.clear();
columns.clear();
headers.clear();
headerRow.clear();
container.off("." + id);
tableBody.off("." + id);
$(window()).off("." + id);
$table.stickyTableHeaders("destroy");
subheaderLib.unload();
setRedraw(true);
rendering = false;
setup = false;
DestroyEvent.fire(this);
}
/**
* Prepare all row specific functionality.
*/
protected void prepareRows() {
JQueryElement rows = $table.find("tr.data-row");
rows.off("." + id);
// Select row click bind
// This will also update the check status of check all input.
rows.on("tap." + id + " click." + id, (e, o) -> {
long now = new Date().getTime();
if ((now - lastRowClicked) < getRowClickCooldown()) {
return false;
}
lastRowClicked = new Date().getTime();
Element row = $(e.getCurrentTarget()).asElement();
int rowIndex = getRowIndexByElement(row);
if (selectionType.equals(SelectionType.MULTIPLE) && shiftDown) {
if (lastSelected < rowIndex) {
// Increment
for (int i = lastSelected; i <= rowIndex; i++) {
if (i < getVisibleItemCount()) {
RowComponent<T> rowComponent = this.rows.get(i);
if (rowComponent != null && rowComponent.isRendered()) {
selectRow(rowComponent.getWidget().getElement(), true);
}
}
}
} else {
// Decrement
for (int i = lastSelected - 1; i >= rowIndex - 1; i--) {
if (i >= 0) {
RowComponent<T> rowComponent = this.rows.get(i);
if (rowComponent != null && rowComponent.isRendered()) {
selectRow(rowComponent.getWidget().getElement(), true);
}
}
}
}
} else {
toggleRowSelect(e, row);
}
return true;
});
rows.on("contextmenu." + id, (e, o) -> {
Element row = $(e.getCurrentTarget()).asElement();
// Fire row select event
RowContextMenuEvent.fire(this, (MouseEvent) e, getModelByRowElement(row), row);
return true;
});
rows.on("dblclick." + id, (e, o) -> {
Element row = $(e.getCurrentTarget()).asElement();
// Fire row select event
RowDoubleClickEvent.fire(this, e, getModelByRowElement(row), row);
return false;
});
JQueryExtension.$(rows).longpress(e -> {
Element row = $(e.getCurrentTarget()).asElement();
// Fire row select event
RowLongPressEvent.fire(this, e, getModelByRowElement(row), row);
return true;
}, e -> {
Element row = $(e.getCurrentTarget()).asElement();
// Fire row select event
RowShortPressEvent.fire(this, e, getModelByRowElement(row), row);
return true;
}, longPressDuration);
JQueryElement expands = $table.find("i#expand");
expands.off("." + id);
if (useRowExpansion) {
// Expand current row extra information
expands.on("tap." + id + " click." + id, e -> {
JQueryElement tr = $(e.getCurrentTarget()).parent().parent();
if (expandOrCollapseRow(tr)) {
e.stopPropagation();
}
return true;
});
}
subheaderLib.detect();
subheaderLib.recalculate(true);
}
@Override
public boolean expandRow(RowComponent<T> row, boolean expand) {
JQueryElement tr = row.getWidget() != null ? row.getWidget().$this() : null;
return tr != null && expandRow(tr, expand);
}
@Override
public boolean expandRow(JQueryElement tr, boolean expand) {
final boolean[] recalculated = {false};
if (!tr.hasClass("disabled") && !tr.is("[disabled]")) {
JQueryElement[] expansion = new JQueryElement[]{tr.next().find("td.expansion div")};
if (expansion[0].length() < 1) {
expansion[0] = $(expansionHtml).insertAfter(tr);
expansion[0] = expansion[0].find("td.expansion div");
}
final JQueryElement row = tr.next();
final T model = getModelByRowElement(tr.asElement());
RowExpansion<T> rowExpansion = new RowExpansion<>(model, row);
final JQueryElement content = rowExpansion.getContent();
JQueryElement[] copy = {expansion[0].find("div#copy")};
if (expand && hasFrozenColumns()) {
// This will open at the same time as the original.
copy[0] = content.clone().appendTo(expansion[0]);
copy[0].attr("id", "copy");
copy[0].css("height", "80px");
// Assign absolute and left 0 for frozen column support.
content.css("position", "absolute");
content.css("left", "0");
content.css("height", "80px");
}
expansion[0].one(transitionEvents, (e1, param1) -> {
if (!recalculated[0]) {
// Recalculate sub headers
subheaderLib.recalculate(true);
recalculated[0] = true;
// Apply overlay
JQueryElement overlay = row.find("section.overlay");
overlay.height(row.outerHeight(false));
JQueryElement icon = tr.find("i#expand");
if (expand) {
// Fire table expanded event
RowExpandedEvent.fire(this, rowExpansion);
icon.html(IconType.KEYBOARD_ARROW_UP.getCssName());
} else {
// Fire table collapsed event
RowCollapsedEvent.fire(this, rowExpansion);
icon.html(IconType.KEYBOARD_ARROW_DOWN.getCssName());
}
}
return true;
});
if (expand) {
// Fire table expand event
RowExpandingEvent.fire(this, rowExpansion);
} else {
// Destroy the copy
if (copy[0] != null) {
copy[0].remove();
}
content.css("position", "");
content.css("left", "");
RowCollapsingEvent.fire(this, rowExpansion);
}
Scheduler.get().scheduleDeferred(() -> {
if (copy[0] != null) {
copy[0].toggleClass("expanded");
}
expansion[0].toggleClass("expanded");
});
return true;
}
return false;
}
@Override
public boolean expandOrCollapseRow(RowComponent<T> row) {
JQueryElement tr = row.getWidget() != null ? row.getWidget().$this() : null;
return tr != null && expandOrCollapseRow(tr);
}
@Override
public boolean expandOrCollapseRow(JQueryElement tr) {
JQueryElement expansion = tr.next().find("td.expansion div");
return expandRow(tr, !expansion.hasClass("expanded"));
}
@Override
public void showRowLoading(T data) {
if (data != null) {
RowComponent<T> row = getRow(data);
if (row != null) {
row.loading(true);
}
}
}
@Override
public void hideRowLoading(T data) {
if (data != null) {
RowComponent<T> row = getRow(data);
if (row != null) {
row.loading(false);
}
}
}
@Override
public void highlightRow(T data) {
if (data != null) {
RowComponent<T> row = getRow(data);
if (row != null) {