@@ -18,6 +18,7 @@ import {
18
18
Component ,
19
19
EventEmitter ,
20
20
Input ,
21
+ OnDestroy ,
21
22
Output ,
22
23
} from '@angular/core' ;
23
24
import {
@@ -32,23 +33,43 @@ import {
32
33
relativeTimeFormatter ,
33
34
} from '../line_chart_v2/lib/formatter' ;
34
35
36
+ enum Side {
37
+ RIGHT ,
38
+ LEFT ,
39
+ }
40
+
41
+ const preventDefault = function ( e : MouseEvent ) {
42
+ e . preventDefault ( ) ;
43
+ } ;
44
+
35
45
@Component ( {
36
46
selector : 'tb-data-table' ,
37
47
templateUrl : 'data_table_component.ng.html' ,
38
48
styleUrls : [ 'data_table_component.css' ] ,
39
49
changeDetection : ChangeDetectionStrategy . OnPush ,
40
50
} )
41
- export class DataTableComponent {
51
+ export class DataTableComponent implements OnDestroy {
42
52
// The order of this array of headers determines the order which they are
43
53
// displayed in the table.
44
54
@Input ( ) headers ! : ColumnHeaders [ ] ;
45
55
@Input ( ) data ! : SelectedStepRunData [ ] ;
46
56
@Input ( ) sortingInfo ! : SortingInfo ;
57
+ @Input ( ) columnCustomizationEnabled ! : boolean ;
47
58
48
59
@Output ( ) sortDataBy = new EventEmitter < SortingInfo > ( ) ;
60
+ @Output ( ) orderColumns = new EventEmitter < ColumnHeaders [ ] > ( ) ;
49
61
50
62
readonly ColumnHeaders = ColumnHeaders ;
51
63
readonly SortingOrder = SortingOrder ;
64
+ readonly Side = Side ;
65
+
66
+ draggingHeader : ColumnHeaders | undefined ;
67
+ highlightedColumn : ColumnHeaders | undefined ;
68
+ highlightSide : Side = Side . RIGHT ;
69
+
70
+ ngOnDestroy ( ) {
71
+ document . removeEventListener ( 'dragover' , preventDefault ) ;
72
+ }
52
73
53
74
getHeaderTextColumn ( columnHeader : ColumnHeaders ) : string {
54
75
switch ( columnHeader ) {
@@ -200,4 +221,63 @@ export class DataTableComponent {
200
221
}
201
222
this . sortDataBy . emit ( { header, order : SortingOrder . ASCENDING } ) ;
202
223
}
224
+
225
+ dragStart ( header : ColumnHeaders ) {
226
+ this . draggingHeader = header ;
227
+
228
+ // This stop the end drag animation
229
+ document . addEventListener ( 'dragover' , preventDefault ) ;
230
+ }
231
+
232
+ dragEnd ( ) {
233
+ if ( ! this . draggingHeader || ! this . highlightedColumn ) {
234
+ return ;
235
+ }
236
+
237
+ this . orderColumns . emit (
238
+ this . moveHeader (
239
+ this . headers . indexOf ( this . draggingHeader ) ,
240
+ this . headers . indexOf ( this . highlightedColumn )
241
+ )
242
+ ) ;
243
+ this . draggingHeader = undefined ;
244
+ this . highlightedColumn = undefined ;
245
+ document . removeEventListener ( 'dragover' , preventDefault ) ;
246
+ }
247
+
248
+ dragEnter ( header : ColumnHeaders ) {
249
+ if ( ! this . draggingHeader ) {
250
+ return ;
251
+ }
252
+ if (
253
+ this . headers . indexOf ( header ) < this . headers . indexOf ( this . draggingHeader )
254
+ ) {
255
+ this . highlightSide = Side . LEFT ;
256
+ } else {
257
+ this . highlightSide = Side . RIGHT ;
258
+ }
259
+ this . highlightedColumn = header ;
260
+ }
261
+
262
+ // Move the item at sourceIndex to destinationIndex
263
+ moveHeader ( sourceIndex : number , destinationIndex : number ) {
264
+ const newHeaders = [ ...this . headers ] ;
265
+ // Delete from original location
266
+ newHeaders . splice ( sourceIndex , 1 ) ;
267
+ // Insert at destinationIndex.
268
+ newHeaders . splice ( destinationIndex , 0 , this . headers [ sourceIndex ] ) ;
269
+ return newHeaders ;
270
+ }
271
+
272
+ getHeaderHighlightStyle ( header : ColumnHeaders ) {
273
+ if ( header !== this . highlightedColumn ) {
274
+ return { } ;
275
+ }
276
+
277
+ return {
278
+ highlight : true ,
279
+ 'highlight-border-right' : this . highlightSide === Side . RIGHT ,
280
+ 'highlight-border-left' : this . highlightSide === Side . LEFT ,
281
+ } ;
282
+ }
203
283
}
0 commit comments