/
app.component.ts
994 lines (924 loc) · 33.2 KB
/
app.component.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
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import {
Component, OnInit, Inject, forwardRef, ViewChild, ViewChildren, QueryList, ElementRef,
EventEmitter, AfterViewChecked
} from '@angular/core';
import { IObservableCollection, SlickGrid, VirtualizedCollection } from 'angular2-slickgrid';
import {
ISlickRange, FieldType, IColumnDefinition, IGridDataRow,
IGridIcon, IMessage, IRange, ISelectionData, DbCellValue
} from '../../../../../models/interfaces';
import { DataService } from './../services/data.service';
import { ShortcutService } from './../services/shortcuts.service';
import { ContextMenu } from './contextmenu.component';
import { MessagesContextMenu } from './messagescontextmenu.component';
import * as Constants from './../constants';
import * as Utils from './../utils';
/** enableProdMode */
import { enableProdMode } from '@angular/core';
enableProdMode();
// text selection helper library
declare let rangy;
export interface IGridDataSet {
dataRows: IObservableCollection<IGridDataRow>;
columnDefinitions: IColumnDefinition[];
resized: EventEmitter<any>;
totalRows: number;
batchId: number;
resultId: number;
maxHeight: number | string;
minHeight: number | string;
}
// tslint:disable:max-line-length
const template = `
<div class="fullsize vertBox">
<div *ngIf="dataSets.length > 0" role="button" id="resultspane" tabIndex="0" class="boxRow header collapsible"
[class.collapsed]="!resultActive"
(click)="toggleResultsPane()"
(keydown)="handleResultsKeydown($event)"
[attr.aria-label]="Constants.resultPaneLabel"
[attr.aria-expanded]="resultActive">
<span> {{Constants.resultPaneLabel}} </span>
<span class="shortCut"> {{resultShortcut}} </span>
</div>
<div id="results" *ngIf="renderedDataSets.length > 0" class="results vertBox scrollable"
(onScroll)="onScroll($event)"
[scrollEnabled]="scrollEnabled"
[class.hidden]="!resultActive">
<div class="boxRow content horzBox slickgrid" *ngFor="let dataSet of renderedDataSets; let i = index"
[style.max-height]="renderedDataSets.length > 1 ? dataSet.maxHeight + 'px' : 'inherit'"
[style.min-height]="renderedDataSets.length > 1 ? dataSet.minHeight + 'px' : 'inherit'"
[style.font-size]="resultsFontSize + 'px'">
<slick-grid #slickgrid id="slickgrid_{{i}}" [columnDefinitions]="dataSet.columnDefinitions"
[ngClass]="i === activeGrid ? 'active' : ''"
[dataRows]="dataSet.dataRows"
(onContextMenu)="openContextMenu($event, dataSet.batchId, dataSet.resultId, i)"
enableAsyncPostRender="true"
showDataTypeIcon="false"
showHeader="true"
[resized]="dataSet.resized"
(mousedown)="navigateToGrid(i)"
(focusin)="setActiveGrid(i)"
[selectionModel]="selectionModel"
[plugins]="slickgridPlugins"
class="boxCol content vertBox slickgrid">
</slick-grid>
<span class="boxCol content vertBox">
<div class="boxRow content maxHeight" *ngFor="let icon of dataIcons">
<div *ngIf="icon.showCondition()" class="gridIcon">
<a class="icon" href="#" role="button"
(click)="icon.functionality(dataSet.batchId, dataSet.resultId, i)"
[title]="icon.hoverText()" [ngClass]="icon.icon()">
</a>
</div>
</div>
</span>
</div>
</div>
<context-menu #contextmenu (clickEvent)="handleContextClick($event)"></context-menu>
<msg-context-menu #messagescontextmenu (clickEvent)="handleMessagesContextClick($event)"></msg-context-menu>
<div id="messagepane" role="button" tabIndex="0" class="boxRow header collapsible"
[class.collapsed]="!messageActive"
[attr.aria-label]="Constants.messagePaneLabel"
[attr.aria-expanded]="messageActive"
(click)="toggleMessagesPane()"
(keydown)="handleMessagesKeydown($event)"
style="position: relative">
<div id="messageResizeHandle" class="resizableHandle"></div>
<span> {{Constants.messagePaneLabel}} </span>
<span class="shortCut"> {{messageShortcut}} </span>
</div>
<div id="messages" class="scrollable messages" [class.hidden]="!messageActive"
(contextmenu)="openMessagesContextMenu($event)"
(mousedown)="onMouseDown($event)">
<br>
<table id="messageTable">
<colgroup>
<col span="1" class="wide">
</colgroup>
<tbody>
<tr>
<th>{{Constants.messagesTableTimeStampColumn}}</th>
<th>{{Constants.messagesTableMessageColumn}}</th>
</tr>
<template ngFor let-message [ngForOf]="messages">
<tr class='messageRow'>
<td><span *ngIf="!Utils.isNumber(message.batchId)">[{{message.time}}]</span></td>
<td class="messageValue" [class.errorMessage]="message.isError" [class.batchMessage]="Utils.isNumber(message.batchId)">{{message.message}} <a *ngIf="message.link" href="#" (click)="sendGetRequest(message.selection)">{{message.link.text}}</a>
</td>
</tr>
</template>
<tr id='executionSpinner' *ngIf="!complete">
<td><span *ngIf="messages.length === 0">[{{startString}}]</span></td>
<td>
<img src="views/htmlcontent/src/images/progress_36x_animation.gif" height="18px" />
<span style="vertical-align: bottom">{{Constants.executeQueryLabel}}</span>
</td>
</tr>
<tr *ngIf="complete">
<td></td>
<td>{{Utils.formatString(Constants.elapsedTimeLabel, totalElapsedTimeSpan)}}</td>
</tr>
</tbody>
</table>
</div>
<div id="resizeHandle" [class.hidden]="!resizing" [style.top.px]="resizeHandleTop"></div>
</div>
`;
// tslint:enable:max-line-length
/**
* Top level app component which runs and controls the SlickGrid implementation
*/
@Component({
selector: 'my-app',
host: {
'(window:keydown)': 'keyEvent($event)',
'(window:gridnav)': 'keyEvent($event)',
'(window:resize)': 'resizeResults()'
},
template: template,
providers: [DataService, ShortcutService],
styles: [`
.errorMessage {
color: var(--color-error);
}
.batchMessage {
padding-left: 20px;
}
`]
})
export class AppComponent implements OnInit, AfterViewChecked {
// CONSTANTS
private scrollTimeOutTime = 200;
private windowSize = 50;
private maxScrollGrids = 8;
public selectionModel = 'DragRowSelectionModel';
public slickgridPlugins = ['AutoColumnSize'];
private _rowHeight = 29;
private _resultsPaneBoundary = 22;
private _defaultNumShowingRows = 8;
public Constants = Constants;
public Utils = Utils;
// the function implementations of keyboard available events
private shortcutfunc = {
'event.focusResultsGrid': () => {
this.slickgrids.toArray()[this.activeGrid]['_grid'].setActiveCell(0, 1);
},
'event.toggleResultPane': () => {
this.resultActive = !this.resultActive;
},
'event.toggleMessagePane': () => {
this.toggleMessagesPane();
},
'event.nextGrid': () => {
this.navigateToGrid(this.activeGrid + 1);
},
'event.prevGrid': () => {
this.navigateToGrid(this.activeGrid - 1);
},
'event.copySelection': () => {
let range: IRange = this.getSelectedRangeUnderMessages();
let messageText = range ? range.text() : '';
if (messageText.length > 0) {
this.executeCopy(messageText);
} else {
let activeGrid = this.activeGrid;
let selection = this.slickgrids.toArray()[activeGrid].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
this.dataService.copyResults(selection, this.renderedDataSets[activeGrid].batchId, this.renderedDataSets[activeGrid].resultId);
}
},
'event.copyWithHeaders': () => {
let activeGrid = this.activeGrid;
let selection = this.slickgrids.toArray()[activeGrid].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
this.dataService.copyResults(selection, this.renderedDataSets[activeGrid].batchId,
this.renderedDataSets[activeGrid].resultId, true);
},
'event.maximizeGrid': () => {
this.magnify(this.activeGrid);
},
'event.selectAll': () => {
this.slickgrids.toArray()[this.activeGrid].selection = true;
},
'event.saveAsCSV': () => {
this.sendSaveRequest('csv');
},
'event.saveAsJSON': () => {
this.sendSaveRequest('json');
},
'event.saveAsExcel': () => {
this.sendSaveRequest('excel');
},
'event.changeColumnWidth': async () => {
const activeGrid = this.slickgrids.toArray()[this.activeGrid]['_grid'];
if (activeGrid) {
const activeCell = activeGrid.getActiveCell();
const columns = activeGrid.getColumns();
if (!columns[activeCell.cell]?.resizable) {
return;
}
const newWidth = await this.dataService.getNewColumnWidth(columns[activeCell.cell].width);
if (newWidth) {
columns[activeCell.cell].width = newWidth;
activeGrid.setColumns(columns);
activeGrid.setActiveCell(activeCell.row, activeCell.cell);
}
}
}
};
private dataIcons: IGridIcon[] = [
{
showCondition: () => { return this.dataSets.length > 1; },
icon: () => {
return this.renderedDataSets.length === 1
? 'exitFullScreen'
: 'extendFullScreen';
},
hoverText: () => {
return this.renderedDataSets.length === 1
? Constants.restoreLabel
: Constants.maximizeLabel;
},
functionality: (batchId, resultId, index) => {
this.magnify(index);
}
},
{
showCondition: () => { return true; },
icon: () => { return 'saveCsv'; },
hoverText: () => { return Constants.saveCSVLabel; },
functionality: (batchId, resultId, index) => {
let selection = this.slickgrids.toArray()[index].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
if (selection.length <= 1) {
this.handleContextClick({ type: 'savecsv', batchId: batchId, resultId: resultId, index: index, selection: selection });
} else {
this.dataService.showWarning(Constants.msgCannotSaveMultipleSelections);
}
}
},
{
showCondition: () => { return true; },
icon: () => { return 'saveJson'; },
hoverText: () => { return Constants.saveJSONLabel; },
functionality: (batchId, resultId, index) => {
let selection = this.slickgrids.toArray()[index].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
if (selection.length <= 1) {
this.handleContextClick({ type: 'savejson', batchId: batchId, resultId: resultId, index: index, selection: selection });
} else {
this.dataService.showWarning(Constants.msgCannotSaveMultipleSelections);
}
}
},
{
showCondition: () => { return true; },
icon: () => { return 'saveExcel'; },
hoverText: () => { return Constants.saveExcelLabel; },
functionality: (batchId, resultId, index) => {
let selection = this.slickgrids.toArray()[index].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
if (selection.length <= 1) {
this.handleContextClick({ type: 'saveexcel', batchId: batchId, resultId: resultId, index: index, selection: selection });
} else {
this.dataService.showWarning(Constants.msgCannotSaveMultipleSelections);
}
}
}
];
public startString = new Date().toLocaleTimeString();
private config;
// FIELDS
// All datasets
private dataSets: IGridDataSet[] = [];
// Place holder data sets to buffer between data sets and rendered data sets
private placeHolderDataSets: IGridDataSet[] = [];
// Datasets currently being rendered on the DOM
private renderedDataSets: IGridDataSet[] = this.placeHolderDataSets;
private messages: IMessage[] = [];
private scrollTimeOut: NodeJS.Timeout;
private messagesAdded = false;
public resizing = false;
public resizeHandleTop = 0;
public scrollEnabled = true;
private resultActive = true;
private _messageActive = true;
private firstRender = true;
public resultsScrollTop = 0;
private activeGrid = 0;
public messageShortcut;
public resultShortcut;
public totalElapsedTimeSpan: number;
public complete = false;
private uri: string;
private hasRunQuery: boolean = false;
public resultsFontSize;
@ViewChild('contextmenu') contextMenu: ContextMenu;
@ViewChild('messagescontextmenu') messagesContextMenu: MessagesContextMenu;
@ViewChildren('slickgrid') slickgrids: QueryList<SlickGrid>;
set messageActive(input: boolean) {
this._messageActive = input;
if (this.resultActive) {
this.resizeGrids();
}
}
get messageActive(): boolean {
return this._messageActive;
}
constructor(@Inject(forwardRef(() => DataService)) public dataService: DataService,
@Inject(forwardRef(() => ShortcutService)) private shortcuts: ShortcutService,
@Inject(forwardRef(() => ElementRef)) private _el: ElementRef) { }
/**
* Called by Angular when the component is initialized
*/
ngOnInit(): void {
const self = this;
this.setupResizeBind();
this.dataService.config.then((config) => {
this.config = config;
self._messageActive = self.config.messagesDefaultOpen;
self.resultsFontSize = self.config.resultsFontSize;
this.shortcuts.stringCodeFor('event.toggleMessagePane').then((result) => {
self.messageShortcut = result;
});
this.shortcuts.stringCodeFor('event.toggleResultPane').then((result) => {
self.resultShortcut = result;
});
});
this.dataService.dataEventObs.subscribe(event => {
switch (event.type) {
case 'start':
self.uri = event.data;
// Empty the data set if the query is run
// again on the same panel
if (self.hasRunQuery) {
self.dataSets = [];
self.placeHolderDataSets = [];
self.renderedDataSets = self.placeHolderDataSets;
self.messages = [];
self.complete = false;
self.messagesAdded = false;
self.hasRunQuery = false;
}
break;
case 'complete':
self.totalElapsedTimeSpan = event.data;
self.complete = true;
self.messagesAdded = true;
self.hasRunQuery = true;
// reset results and messages expansion state
// for new queries
self.resultActive = true;
self.messageActive = true;
break;
case 'message':
self.messages.push(event.data);
break;
case 'resultSet':
let resultSet = event.data;
// Setup a function for generating a promise to lookup result subsets
let loadDataFunction = (offset: number, count: number): Promise<any[]> => {
return self.dataService.getRows(offset, count, resultSet.batchId, resultSet.id).then(response => {
if (!response) {
return [];
}
return response.rows;
});
};
// Precalculate the max height and min height
let maxHeight = resultSet.rowCount < self._defaultNumShowingRows
? Math.max((resultSet.rowCount + 1) * self._rowHeight, self.dataIcons.length * 30) + 10
: 'inherit';
let minHeight = resultSet.rowCount >= self._defaultNumShowingRows
? (self._defaultNumShowingRows + 1) * self._rowHeight + 10
: maxHeight;
// Store the result set from the event)
let dataSet: IGridDataSet = {
resized: undefined,
batchId: resultSet.batchId,
resultId: resultSet.id,
totalRows: resultSet.rowCount,
maxHeight: maxHeight,
minHeight: minHeight,
dataRows: new VirtualizedCollection(
self.windowSize,
resultSet.rowCount,
loadDataFunction,
index => { return { values: [] }; }
),
columnDefinitions: resultSet.columnInfo.map((c, i) => {
let isLinked = c.isXml || c.isJson;
let linkType = c.isXml ? 'xml' : 'json';
return {
id: i.toString(),
field: i.toString(),
name: c.columnName === 'Microsoft SQL Server 2005 XML Showplan'
? 'XML Showplan'
: Utils.htmlEntities(c.columnName),
type: self.stringToFieldType('string'),
formatter: isLinked ? self.hyperLinkFormatter : AppComponent.textFormatter,
asyncPostRender: isLinked ? self.linkHandler(linkType) : undefined
};
})
};
dataSet.columnDefinitions.unshift({
id: 'rowNumber',
name: '',
field: 'rowNumber',
width: 22,
type: FieldType.Integer,
focusable: true,
selectable: false,
formatter: r => { return `<span class="row-number">${r + 1}</span>`; }
});
self.dataSets.push(dataSet);
// Create a dataSet to render without rows to reduce DOM size
let undefinedDataSet = JSON.parse(JSON.stringify(dataSet));
undefinedDataSet.columnDefinitions = dataSet.columnDefinitions;
undefinedDataSet.dataRows = undefined;
undefinedDataSet.resized = new EventEmitter();
self.placeHolderDataSets.push(undefinedDataSet);
self.messagesAdded = true;
self.onScroll(0);
break;
default:
console.error('Unexpected proxy event type "' + event.type + '" sent');
break;
}
});
this.dataService.sendReadyEvent(this.uri);
}
ngAfterViewChecked(): void {
if (this.messagesAdded) {
this.messagesAdded = false;
this.scrollMessages();
}
}
/**
* Used to convert the string to a enum compatible with SlickGrid
*/
private stringToFieldType(input: string): FieldType {
let fieldtype: FieldType;
switch (input) {
case 'string':
fieldtype = FieldType.String;
break;
default:
fieldtype = FieldType.String;
break;
}
return fieldtype;
}
/**
* Toggle the messages pane
*/
private toggleMessagesPane(): void {
this.messageActive = !this.messageActive;
if (this.messageActive) {
this.resizeResults();
}
}
/**
* Toggle the results pane
*/
private toggleResultsPane(): void {
this.resultActive = !this.resultActive;
this.resizeResults();
}
/**
* Returns true if keydown was an Enter os Space
*/
private handleKeydown(event: KeyboardEvent): boolean {
// Enter
if ((event.keyCode === 13 || event.code === 'Enter') ||
// Space bar
(event.keyCode === 32 || event.code === 'Space')) {
event.stopPropagation();
event.preventDefault();
return true;
}
return false;
}
/**
* Handles toggling messages via key event
*/
public handleMessagesKeydown(event: KeyboardEvent): void {
if (this.handleKeydown(event)) {
this.toggleMessagesPane();
}
}
/**
* Handles toggling messages via key event
*/
public handleResultsKeydown(event: KeyboardEvent): void {
if (this.handleKeydown(event)) {
this.toggleResultsPane();
}
}
/**
* Send save result set request to service
*/
handleContextClick(event: { type: string, batchId: number, resultId: number, index: number, selection: ISlickRange[] }): void {
switch (event.type) {
case 'savecsv':
this.dataService.sendSaveRequest(event.batchId, event.resultId, 'csv', event.selection);
break;
case 'savejson':
this.dataService.sendSaveRequest(event.batchId, event.resultId, 'json', event.selection);
break;
case 'saveexcel':
this.dataService.sendSaveRequest(event.batchId, event.resultId, 'excel', event.selection);
break;
case 'selectall':
this.activeGrid = event.index;
this.shortcutfunc['event.selectAll']();
break;
case 'copySelection':
this.dataService.copyResults(event.selection, event.batchId, event.resultId);
break;
case 'copyWithHeaders':
this.dataService.copyResults(event.selection, event.batchId, event.resultId, true);
break;
case 'copyAllHeaders':
this.dataService.copyResults(undefined, event.batchId, event.resultId, true);
break;
default:
break;
}
}
openContextMenu(event: MouseEvent, batchId, resultId, index): void {
let selection: ISlickRange[] = this.slickgrids.toArray()[index].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
this.contextMenu.show(event.clientX, event.clientY, batchId, resultId, index, selection);
event.preventDefault();
event.stopPropagation();
}
private tryCombineSelectionsForResults(selections: ISlickRange[]): ISlickRange[] {
// need to take row number column in to consideration.
return this.tryCombineSelections(selections).map(range => {
return {
fromCell: range.fromCell - 1,
fromRow: range.fromRow,
toCell: range.toCell - 1,
toRow: range.toRow
};
});
}
private tryCombineSelections(selections: ISlickRange[]): ISlickRange[] {
if (!selections || selections.length === 0 || selections.length === 1) {
return selections;
}
// If the selections combine into a single continuous selection, this will be the selection
let unifiedSelection: ISlickRange = {
fromCell: selections.map(range => range.fromCell).reduce((min, next) => next < min ? next : min),
fromRow: selections.map(range => range.fromRow).reduce((min, next) => next < min ? next : min),
toCell: selections.map(range => range.toCell).reduce((max, next) => next > max ? next : max),
toRow: selections.map(range => range.toRow).reduce((max, next) => next > max ? next : max)
};
// Verify whether all cells in the combined selection have actually been selected
let verifiers: ((cell: [number, number]) => boolean)[] = [];
selections.forEach(range => {
verifiers.push((cell: [number, number]) => {
return cell[0] >= range.fromRow && cell[0] <= range.toRow && cell[1] >= range.fromCell && cell[1] <= range.toCell;
});
});
for (let row = unifiedSelection.fromRow; row <= unifiedSelection.toRow; row++) {
for (let column = unifiedSelection.fromCell; column <= unifiedSelection.toCell; column++) {
// If some cell in the combined selection isn't actually selected, return the original selections
if (!verifiers.some(verifier => verifier([row, column]))) {
return selections;
}
}
}
return [unifiedSelection];
}
private sendSaveRequest(format: string): void {
let activeGrid = this.activeGrid;
let batchId = this.renderedDataSets[activeGrid].batchId;
let resultId = this.renderedDataSets[activeGrid].resultId;
let selection = this.slickgrids.toArray()[activeGrid].getSelectedRanges();
selection = this.tryCombineSelectionsForResults(selection);
this.dataService.sendSaveRequest(batchId, resultId, format, selection);
}
/**
* Perform copy and do other actions for context menu on the messages component
*/
handleMessagesContextClick(event: { type: string, selectedRange: IRange }): void {
switch (event.type) {
case 'copySelection':
let selectedText = event.selectedRange.text();
this.executeCopy(selectedText);
break;
default:
break;
}
}
openMessagesContextMenu(event: any): void {
event.preventDefault();
let selectedRange: IRange = this.getSelectedRangeUnderMessages();
this.messagesContextMenu.show(event.clientX, event.clientY, selectedRange);
}
getSelectedRangeUnderMessages(): IRange {
let selectedRange: IRange = undefined;
let msgEl = this._el.nativeElement.querySelector('#messages');
if (msgEl) {
selectedRange = this.getSelectedRangeWithin(msgEl);
}
return selectedRange;
}
getSelectedRangeWithin(el): IRange {
let selectedRange = undefined;
let sel = rangy.getSelection();
let elRange = <IRange>rangy.createRange();
elRange.selectNodeContents(el);
if (sel.rangeCount) {
selectedRange = sel.getRangeAt(0).intersection(elRange);
}
elRange.detach();
return selectedRange;
}
// Copy text as text
executeCopy(text: string): void {
let input = document.createElement('textarea');
document.body.appendChild(input);
input.value = text;
input.style.position = 'absolute';
input.style.bottom = '100%';
input.focus();
input.select();
document.execCommand('copy');
input.remove();
}
/**
* Add handler for clicking on xml link
*/
xmlLinkHandler = (cellRef: string, row: number, dataContext: JSON, colDef: any) => {
this.handleLink(cellRef, row, dataContext, colDef, 'xml');
}
/**
* Add handler for clicking on json link
*/
jsonLinkHandler = (cellRef: string, row: number, dataContext: JSON, colDef: any) => {
this.handleLink(cellRef, row, dataContext, colDef, 'json');
}
private handleLink(cellRef: string, row: number, dataContext: JSON, colDef: any, linkType: string): void {
const self = this;
let value = self.getCellValueString(dataContext, colDef);
$(cellRef).children('.xmlLink').click(function (): void {
self.dataService.openLink(value, colDef.name, linkType);
});
}
private getCellValueString(dataContext: JSON, colDef: any): string {
let returnVal = '';
if (!dataContext) {
return returnVal;
}
let value = dataContext[colDef.field];
if (Utils.isDbCellValue(value)) {
returnVal = value.displayValue;
} else if (typeof value === 'string') {
returnVal = value;
}
return returnVal;
}
/**
* Return asyncPostRender handler based on type
*/
public linkHandler(type: string): Function {
if (type === 'xml') {
return this.xmlLinkHandler;
} else if (type === 'json') {
return this.jsonLinkHandler;
}
}
/**
* Format xml field into a hyperlink and performs HTML entity encoding
*/
public hyperLinkFormatter(row: number, cell: any, value: DbCellValue, columnDef: any, dataContext: any): string {
let cellClasses = 'grid-cell-value-container';
let valueToDisplay: string;
if (Utils.isDbCellValue(value)) {
// Show NULL values as text
if (Utils.isNullValueCell(value)) {
return AppComponent.textFormatter(row, cell, value, columnDef, dataContext);
}
cellClasses += ' xmlLink';
valueToDisplay = Utils.htmlEntities(value.displayValue);
return `<a class="${cellClasses}" href="#" >${valueToDisplay}</a>`;
}
// If we make it to here, we don't have a DbCellValue
cellClasses += ' missing-value';
return `<span class="${cellClasses}"></span>`;
}
/**
* Format all text to replace all new lines with spaces and performs HTML entity encoding
*/
static textFormatter(row: number, cell: any, value: DbCellValue, columnDef: any, dataContext: any): string {
let cellClasses = 'grid-cell-value-container';
let valueToDisplay: string;
if (Utils.isDbCellValue(value)) {
valueToDisplay = Utils.htmlEntities(value.displayValue.replace(/(\r\n|\n|\r)/g, ' '));
if (value.isNull) {
cellClasses += ' missing-value';
}
} else {
valueToDisplay = '';
}
return `<span title="${valueToDisplay}" class="${cellClasses}">${valueToDisplay}</span>`;
}
/**
* Handles rendering the results to the DOM that are currently being shown
* and destroying any results that have moved out of view
* @param scrollTop The scrolltop value, if not called by the scroll event should be 0
*/
onScroll(scrollTop): void {
const self = this;
clearTimeout(self.scrollTimeOut);
this.scrollTimeOut = setTimeout(() => {
if (self.dataSets.length < self.maxScrollGrids) {
self.scrollEnabled = false;
for (let i = 0; i < self.placeHolderDataSets.length; i++) {
self.placeHolderDataSets[i].dataRows = self.dataSets[i].dataRows;
self.placeHolderDataSets[i].resized.emit();
}
} else {
self.scrollEnabled = true;
let gridHeight = self._el.nativeElement.getElementsByTagName('slick-grid')[0].offsetHeight;
let tabHeight = self._el.nativeElement.querySelector('#results').offsetHeight;
let numOfVisibleGrids = Math.ceil((tabHeight / gridHeight)
+ ((scrollTop % gridHeight) / gridHeight));
let min = Math.floor(scrollTop / gridHeight);
let max = min + numOfVisibleGrids;
for (let i = 0; i < self.placeHolderDataSets.length; i++) {
if (i >= min && i < max) {
if (self.placeHolderDataSets[i].dataRows === undefined) {
self.placeHolderDataSets[i].dataRows = self.dataSets[i].dataRows;
self.placeHolderDataSets[i].resized.emit();
}
} else if (self.placeHolderDataSets[i].dataRows !== undefined) {
self.placeHolderDataSets[i].dataRows = undefined;
}
}
}
if (self.firstRender) {
self.firstRender = false;
setTimeout(() => {
self.slickgrids.toArray()[0].setActive();
});
}
}, self.scrollTimeOutTime);
}
/**
* Sends a get request to the provided uri without changing the active page
* @param uri The URI to send a get request to
*/
sendGetRequest(selectionData: ISelectionData): void {
this.dataService.setEditorSelection(selectionData);
}
/**
* Sets up the resize for the messages/results panes bar
*/
setupResizeBind(): void {
let $resizeHandle = $(this._el.nativeElement.querySelector('#messageResizeHandle'));
let $messagePane = $(this._el.nativeElement.querySelector('#messages'));
$resizeHandle.bind('dragstart', (e) => {
this.resizing = true;
this.resizeHandleTop = e.pageY;
return true;
});
$resizeHandle.bind('drag', (e) => {
this.resizeHandleTop = e.pageY;
});
$resizeHandle.bind('dragend', (e) => {
this.resizing = false;
// redefine the min size for the messages based on the final position
$messagePane.css('min-height', $(window).height() - (e.pageY + 22));
this.resizeResults();
});
}
/**
* Ensures the messages tab is scrolled to the bottom
*/
scrollMessages(): void {
let messagesDiv = this._el.nativeElement.querySelector('#messages');
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
/**
* Makes a resultset take up the full result height if this is not already true
* Otherwise rerenders the result sets from default
*/
magnify(index: number): void {
const self = this;
if (this.renderedDataSets.length > 1) {
this.renderedDataSets = [this.placeHolderDataSets[index]];
} else {
this.renderedDataSets = this.placeHolderDataSets;
this.onScroll(0);
}
setTimeout(() => {
for (let grid of self.renderedDataSets) {
grid.resized.emit();
}
self.slickgrids.toArray()[0].setActive();
});
}
/**
*
*/
async keyEvent(e: any): Promise<void> {
// set selection to none for messages so that ctrl + A
// only selects the results grid
const isGridSelection: boolean = e.target.classList.contains('slick-cell');
if (isGridSelection) {
$('#messages').css('user-select', 'none');
} else {
// otherwise make the messages selectable
$('#messages').css('user-select', 'text');
}
let eString = this.shortcuts.buildEventString(e);
let result = await this.shortcuts.getEvent(eString);
if (result) {
let eventName = <string>result;
// Don't select all the grid if it's not grid selection
// otherwise run every shortcut function
if (!(eventName === 'event.selectAll' && !isGridSelection)) {
this.shortcutfunc[eventName]();
}
if (eventName === 'event.selectAll') {
window.getSelection().empty();
rangy.getSelection().removeAllRanges();
}
e.stopImmediatePropagation();
}
}
/**
*
*/
onMouseDown(event: MouseEvent): void {
this.slickgrids.toArray()[this.activeGrid].selection = false;
window.getSelection().empty();
rangy.getSelection().removeAllRanges();
$('#messages').css('user-select', 'text').focus();
}
/**
* Resizes the results pane
*/
resizeResults(): void {
let scrollableHeight = $('.results.vertBox.scrollable').get(0).clientHeight;
$('.horzBox').get(0).style.height = `${scrollableHeight - this._resultsPaneBoundary}px`;
this.resizeGrids();
}
/**
* Handles rendering and unrendering necessary resources in order to properly
* navigate from one grid another. Should be called any time grid navigation is performed
* @param targetIndex The index in the renderedDataSets to navigate to
* @returns A boolean representing if the navigation was successful
*/
navigateToGrid(targetIndex: number): boolean {
const result = this.setActiveGrid(targetIndex);
if (!result) { return false; }
// scrolling logic
let resultsWindow = $('#results');
let scrollTop = resultsWindow.scrollTop();
let scrollBottom = scrollTop + resultsWindow.height();
let gridHeight = $(this._el.nativeElement).find('slick-grid').height();
if (scrollBottom < gridHeight * (targetIndex + 1)) {
scrollTop += (gridHeight * (targetIndex + 1)) - scrollBottom;
resultsWindow.scrollTop(scrollTop);
}
if (scrollTop > gridHeight * targetIndex) {
scrollTop = (gridHeight * targetIndex);
resultsWindow.scrollTop(scrollTop);
}
return true;
}
setActiveGrid(targetIndex: number): boolean {
// check if the target index is valid
if (targetIndex >= this.renderedDataSets.length || targetIndex < 0) {
return false;
}
// Deselect any text since we are navigating to a new grid
// Do this even if not switching grids, since this covers clicking on the grid after message selection
rangy.getSelection().removeAllRanges();
// check if you are actually trying to change navigation
if (this.activeGrid === targetIndex) {
return false;
}
this.slickgrids.toArray()[this.activeGrid].selection = false;
this.slickgrids.toArray()[targetIndex].setActive();
this.activeGrid = targetIndex;
return true;
}
resizeGrids(): void {
const self = this;
setTimeout(() => {
for (let grid of self.renderedDataSets) {
grid.resized.emit();
}
});
}
}