-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathview-grid.ts
More file actions
174 lines (154 loc) · 5.02 KB
/
view-grid.ts
File metadata and controls
174 lines (154 loc) · 5.02 KB
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
/* eslint-disable */
// @ts-nocheck
import {
KeywordLocation,
SearchType,
} from '../../common/types/search-api-types'
import { id } from '../../common/utils/utils'
import { state } from '../state'
import { formatDocumentType, formatPublishingApp } from '../utils/formatters'
import {
loadGridColumnStateFromCache,
cacheGridColumnState,
} from '../utils/localStorageService'
import { fieldFormat, fieldName } from './utils'
import { viewPagination } from './view-pagination'
import CustomAgGridHeader from './customAgGridHeader'
import debounce from '../utils/debounce'
import { URLCellRenderer } from './customURLCellRenderer'
const overlayElement = () => {
const el = document.createElement('div')
el.id = 'grid-overlay'
return el
}
const createAgGrid = () => {
if (!state.searchResults || state.searchResults?.length <= 0) {
return {}
}
const currentPageRecords = state.searchResults
const excludeOccurrences =
state.searchParams.searchType === SearchType.Language ||
state.searchParams.keywordLocation === KeywordLocation.Title
const excludedFields = [excludeOccurrences ? 'occurrences' : '']
const enabledFields = Object.entries(state.showFields)
.filter(([field, enable]) => {
return enable && !excludedFields.includes(field as string)
})
.map(([key]) => key)
const rowData = currentPageRecords.map((record) =>
Object.entries(record).reduce(
(acc, [k, v]) => ({ ...acc, [k]: fieldFormat(k, v as string) }),
{}
)
)
const cellRenderers = {
url: URLCellRenderer,
documentType: (p) => formatDocumentType(p.value),
publishing_app: (p) => formatPublishingApp(p.value),
}
const parsePageViews = (x) => (x.charAt(0) === '<' ? 0 : parseInt(x))
const sortConfig = {
page_views: {
sortable: true,
sort: state.sorting.page_views,
comparator: (a, b) => {
return parsePageViews(a) - parsePageViews(b)
},
},
}
const columnDefs = enabledFields.map((field) => ({
field,
headerName: fieldName(field),
cellRenderer: cellRenderers[field] || null,
resizable: true,
suppressSizeToFit: ['url', 'title'].includes(field),
width: ['url', 'title'].includes(field) ? 500 : null,
sortable: field !== 'contentId',
...(sortConfig[field] || {}),
}))
const suppressKeyboardEvent = (
params: SuppressKeyboardEventParams
): boolean => {
return params.event.key.toUpperCase() === 'TAB'
}
const suppressHeaderKeyboardEvent = (params): boolean => {
const colNames = gridOptions.columnApi
.getAllDisplayedColumns()
.map((col) => col.colId)
const lastCol = colNames[colNames.length - 1]
return (
params.event.key.toUpperCase() === 'TAB' &&
params.column.getId() === lastCol
)
}
const gridOptions = {
rowData,
columnDefs,
defaultColDef: {
suppressKeyboardEvent,
suppressHeaderKeyboardEvent,
},
onPaginationChanged: function () {
viewPagination(gridOptions)
},
suppressColumnMoveAnimation: true,
suppressDragLeaveHidesColumns: true,
pagination: true,
paginationPageSize: state.pagination.resultsPerPage,
suppressPaginationPanel: true,
domLayout: 'autoHeight',
ensureDomOrder: true,
enableCellTextSelection: true,
alwaysShowHorizontalScroll: true,
alwaysShowVerticalScroll: true,
components: {
agColumnHeader: CustomAgGridHeader,
},
}
const gridDiv = id('results-grid-container')
/* eslint-disable */
const grid = new agGrid.Grid(gridDiv, gridOptions)
const initColumnState = async (callBack) => {
const cachedColumnState = loadGridColumnStateFromCache()
if (cachedColumnState) {
gridOptions.columnApi.applyColumnState({
state: cachedColumnState,
applyOrder: true,
})
}
callBack()
}
const initGoToCurrentPage = () => {
// For cached pagination, we need to set the current page after the grid has been initialised
if (
state.pagination.currentPage !==
gridOptions.api.paginationGetCurrentPage()
) {
gridOptions.api.paginationGoToPage(state.pagination.currentPage)
}
}
const cacheColumnState = () => {
const colState = gridOptions.columnApi.getColumnState()
cacheGridColumnState(colState)
}
const addOverlayElementForPaginationRefresh = () =>
id('grid-wrapper').appendChild(overlayElement())
const onColumnMoved = () => cacheColumnState()
const onGridSort = () => cacheColumnState()
const onColumnResized = debounce((event: any) => cacheColumnState(), 100)
const onGridReady = () => {
addOverlayElementForPaginationRefresh()
addEventListenners()
}
const addEventListenners = () => {
// Grid event listenners
gridOptions.api.addEventListener('gridReady', onGridReady)
gridOptions.api.addEventListener('columnMoved', onColumnMoved)
gridOptions.api.addEventListener('columnResized', onColumnResized)
gridOptions.api.addEventListener('sortChanged', onGridSort)
}
initGoToCurrentPage()
initColumnState(onGridReady)
return { grid, gridOptions }
}
export { createAgGrid }