Skip to content

Commit 1d90663

Browse files
committed
refactor collection list to use column names instead of indices
1 parent 986f416 commit 1d90663

File tree

1 file changed

+53
-84
lines changed

1 file changed

+53
-84
lines changed

sde_indexing_helper/static/js/collection_list.js

Lines changed: 53 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Define column constants for better maintainability
2+
const COLUMNS = {
3+
NAME: 0,
4+
URL: 1,
5+
DIVISION: 2,
6+
DELTA_URLS: 3,
7+
WORKFLOW_STATUS: 4,
8+
CURATOR: 5,
9+
CONNECTOR_TYPE: 6,
10+
REINDEXING_STATUS: 7,
11+
WORKFLOW_STATUS_RAW: 8,
12+
CURATOR_ID: 9,
13+
REINDEXING_STATUS_RAW: 10
14+
};
15+
116
var uniqueId; //used for logic related to contents on column customization modal
217

318
function modalContents(tableName) {
@@ -107,156 +122,127 @@ let table = $("#collection_table").DataTable({
107122
},
108123
],
109124
columnDefs: [
125+
// hide the data columns
110126
{
111-
targets: [8, 9], // Added 9 for reindexing status ID
127+
targets: [COLUMNS.WORKFLOW_STATUS_RAW, COLUMNS.CURATOR_ID, COLUMNS.REINDEXING_STATUS_RAW],
112128
visible: false,
113129
},
114-
{ width: "200px", targets: 1 },
130+
{ width: "200px", targets: COLUMNS.URL },
115131
{
116132
searchPanes: {
117133
options: [
118134
{
119135
label: "0 URLs",
120136
value: function (rowData, rowIdx) {
121-
return $(rowData[3]).text() == 0;
137+
return $(rowData[COLUMNS.DELTA_URLS]).text() == 0;
122138
},
123139
},
124140
{
125141
label: "1 solo URL",
126142
value: function (rowData, rowIdx) {
127-
return $(rowData[3]).text() == 1;
143+
return $(rowData[COLUMNS.DELTA_URLS]).text() == 1;
128144
},
129145
},
130146
{
131147
label: "1 to 100 URLs",
132148
value: function (rowData, rowIdx) {
133-
return $(rowData[3]).text() <= 100 && $(rowData[3]).text() > 1;
149+
return $(rowData[COLUMNS.DELTA_URLS]).text() <= 100 && $(rowData[COLUMNS.DELTA_URLS]).text() > 1;
134150
},
135151
},
136152
{
137153
label: "100 to 1,000 URLs",
138154
value: function (rowData, rowIdx) {
139-
return $(rowData[3]).text() <= 1000 && $(rowData[3]).text() > 100;
155+
return $(rowData[COLUMNS.DELTA_URLS]).text() <= 1000 && $(rowData[COLUMNS.DELTA_URLS]).text() > 100;
140156
},
141157
},
142158
{
143159
label: "1,000 to 10,000 URLs",
144160
value: function (rowData, rowIdx) {
145-
return (
146-
$(rowData[3]).text() <= 10000 && $(rowData[3]).text() > 1000
147-
);
161+
return $(rowData[COLUMNS.DELTA_URLS]).text() <= 10000 && $(rowData[COLUMNS.DELTA_URLS]).text() > 1000;
148162
},
149163
},
150164
{
151165
label: "10,000 to 100,000 URLs",
152166
value: function (rowData, rowIdx) {
153-
return (
154-
$(rowData[3]).text() <= 100000 && $(rowData[3]).text() > 10000
155-
);
167+
return $(rowData[COLUMNS.DELTA_URLS]).text() <= 100000 && $(rowData[COLUMNS.DELTA_URLS]).text() > 10000;
156168
},
157169
},
158170
{
159171
label: "Over 100,000 URLs",
160172
value: function (rowData, rowIdx) {
161-
return $(rowData[3]).text() > 100000;
173+
return $(rowData[COLUMNS.DELTA_URLS]).text() > 100000;
162174
},
163175
},
164176
],
165177
},
166-
targets: [3],
178+
targets: [COLUMNS.DELTA_URLS],
167179
type: "num-fmt",
168180
},
181+
// hide the data panes
169182
{
170183
searchPanes: {
171184
show: false,
172185
},
173-
targets: [8, 9, 10], // this hides the id columns for reindexing status and some other one
186+
targets: [COLUMNS.WORKFLOW_STATUS_RAW, COLUMNS.CURATOR_ID, COLUMNS.REINDEXING_STATUS_RAW],
174187
},
175188
{
176189
searchPanes: {
177190
dtOpts: {
178191
scrollY: "100%",
179192
},
180193
},
181-
targets: [5],
194+
targets: [COLUMNS.CURATOR],
182195
},
183196
{
184197
searchPanes: {
185198
dtOpts: {
186199
scrollY: "100%",
187200
},
188201
},
189-
targets: [6], // Add searchPane for reindexing status column
202+
targets: [COLUMNS.CONNECTOR_TYPE],
190203
},
191204
],
192205
});
193206

194207
$("#collection-dropdown-4").on("change", function () {
195208
table
196-
.columns(7)
209+
.columns(COLUMNS.WORKFLOW_STATUS_RAW)
197210
.search(this.value ? "^" + this.value + "$" : "", true, false)
198211
.draw();
199212
});
200213

201214
$("#collection-dropdown-5").on("change", function () {
202215
table
203-
.columns(8)
216+
.columns(COLUMNS.CURATOR_ID)
204217
.search(this.value ? "^" + this.value + "$" : "", true, false)
205218
.draw();
206219
});
207220

221+
$("#collection-dropdown-6").on("change", function () {
222+
table
223+
.columns(COLUMNS.REINDEXING_STATUS_RAW)
224+
.search(this.value ? "^" + this.value + "$" : "", true, false)
225+
.draw();
226+
});
208227

209228
$("#nameFilter").on("keyup", function () {
210-
table.columns(0).search(this.value).draw();
229+
table.columns(COLUMNS.NAME).search(this.value).draw();
211230
});
212231

213232
$("#urlFilter").on("keyup", function () {
214-
table.columns(1).search(this.value).draw();
233+
table.columns(COLUMNS.URL).search(this.value).draw();
215234
});
216235

217236
$("#divisionFilter").on("keyup", function () {
218-
table.columns(2).search(this.value).draw();
237+
table.columns(COLUMNS.DIVISION).search(this.value).draw();
219238
});
220239

221240
$("#connectorTypeFilter").on("keyup", function () {
222-
table.columns(6).search(this.value).draw();
241+
table.columns(COLUMNS.CONNECTOR_TYPE).search(this.value).draw();
223242
});
224243

225244
var csrftoken = $('input[name="csrfmiddlewaretoken"]').val();
226245

227-
// I don't think this function is being used
228-
// function handleCurationStatusSelect() {
229-
// $("body").on("click", ".curation_status_select", function () {
230-
// var collection_id = $(this).data('collection-id');
231-
// var curation_status = $(this).attr('value');
232-
// var curation_status_text = $(this).text();
233-
// var color_choices = {
234-
// 1: "btn-light",
235-
// 2: "btn-danger",
236-
// 3: "btn-warning",
237-
// 4: "btn-info",
238-
// 5: "btn-success",
239-
// 6: "btn-primary",
240-
// 7: "btn-info",
241-
// 8: "btn-secondary",
242-
// }
243-
244-
// $possible_buttons = $('body').find(`[id="curation-status-button-${collection_id}"]`);
245-
// if ($possible_buttons.length > 1) {
246-
// $button = $possible_buttons[1];
247-
// $button = $($button);
248-
// } else {
249-
// $button = $(`#curation-status-button-${collection_id}`);
250-
// }
251-
// $button.text(curation_status_text);
252-
// $button.removeClass('btn-light btn-danger btn-warning btn-info btn-success btn-primary btn-secondary');
253-
// $button.addClass(color_choices[parseInt(curation_status)]);
254-
// $('#collection_table').DataTable().searchPanes.rebuildPane(6);
255-
// var collection_division = $(this).data('collection-division');
256-
// postCurationStatus(collection_id, curation_status, collection_division);
257-
// });
258-
// }
259-
260246
function handleWorkflowStatusSelect() {
261247
$("body").on("click", ".workflow_status_select", function () {
262248
var collection_id = $(this).data("collection-id");
@@ -297,20 +283,21 @@ function handleWorkflowStatusSelect() {
297283
$button.addClass(color_choices[parseInt(workflow_status)]);
298284
var row = table.row("#" + collection_id);
299285
let index = row.index();
300-
var $html = $("<div />", { html: table.data()[index][4] });
286+
var $html = $("<div />", { html: table.data()[index][COLUMNS.WORKFLOW_STATUS] });
301287
$html.find("button").html(workflow_status_text);
302288
$html
303289
.find("button")
304290
.removeClass(
305291
"btn-light btn-danger btn-warning btn-info btn-success btn-primary btn-secondary"
306292
);
307293
$html.find("button").addClass(color_choices[parseInt(workflow_status)]);
308-
table.data()[index][4] = $html.html();
309-
$("#collection_table").DataTable().searchPanes.rebuildPane(4);
294+
table.data()[index][COLUMNS.WORKFLOW_STATUS] = $html.html();
295+
$("#collection_table").DataTable().searchPanes.rebuildPane(COLUMNS.WORKFLOW_STATUS);
310296

311297
postWorkflowStatus(collection_id, workflow_status);
312298
});
313299
}
300+
314301
function handleReindexingStatusSelect() {
315302
$("body").on("click", ".reindexing_status_select", function () {
316303
var collection_id = $(this).data("collection-id");
@@ -341,22 +328,21 @@ function handleReindexingStatusSelect() {
341328
$button.addClass(color_choices[parseInt(reindexing_status)]);
342329
var row = table.row("#" + collection_id);
343330
let index = row.index();
344-
var $html = $("<div />", { html: table.data()[index][7] }); // Assuming this is column index 7
331+
var $html = $("<div />", { html: table.data()[index][COLUMNS.REINDEXING_STATUS] });
345332
$html.find("button").html(reindexing_status_text);
346333
$html
347334
.find("button")
348335
.removeClass(
349336
"btn-light btn-danger btn-warning btn-info btn-success btn-primary btn-secondary"
350337
);
351338
$html.find("button").addClass(color_choices[parseInt(reindexing_status)]);
352-
table.data()[index][7] = $html.html();
353-
$("#collection_table").DataTable().searchPanes.rebuildPane(7);
339+
table.data()[index][COLUMNS.REINDEXING_STATUS] = $html.html();
340+
$("#collection_table").DataTable().searchPanes.rebuildPane(COLUMNS.REINDEXING_STATUS);
354341

355342
postReindexingStatus(collection_id, reindexing_status);
356343
});
357344
}
358345

359-
360346
function handleCuratorSelect() {
361347
$("body").on("click", ".curator_select", function () {
362348
var collection_id = $(this).data("collection-id");
@@ -380,10 +366,10 @@ function handleCuratorSelect() {
380366
$button.addClass("btn-success");
381367
var row = table.row("#" + collection_id);
382368
let index = row.index();
383-
var $html = $("<div />", { html: table.data()[index][5] });
369+
var $html = $("<div />", { html: table.data()[index][COLUMNS.CURATOR] });
384370
$html.find("button").html(curator_text);
385-
table.data()[index][5] = $html.html();
386-
table.searchPanes.rebuildPane(5);
371+
table.data()[index][COLUMNS.CURATOR] = $html.html();
372+
table.searchPanes.rebuildPane(COLUMNS.CURATOR);
387373
postCurator(collection_id, curator_id);
388374
});
389375
}
@@ -406,23 +392,6 @@ function postReindexingStatus(collection_id, reindexing_status) {
406392
});
407393
}
408394

409-
function postCurationStatus(collection_id, curation_status) {
410-
var url = `/api/collections/${collection_id}/`;
411-
$.ajax({
412-
url: url,
413-
type: "PUT",
414-
data: {
415-
curation_status: curation_status,
416-
csrfmiddlewaretoken: csrftoken,
417-
},
418-
headers: {
419-
"X-CSRFToken": csrftoken,
420-
},
421-
success: function (data) {
422-
toastr.success("Curation Status Updated!");
423-
},
424-
});
425-
}
426395

427396
function postWorkflowStatus(collection_id, workflow_status) {
428397
var url = `/api/collections/${collection_id}/`;
@@ -486,6 +455,7 @@ $(document).ready(function () {
486455
// Clear previous search
487456
table.search('').columns().search('');
488457

458+
// TODO: this section might still need to be refactored to align with our column index definitions
489459
// Filter the table based on the query in the collection name and config folder data attribute
490460
table.rows().every(function () {
491461
let row = $(this.node());
@@ -519,7 +489,6 @@ $(document).ready(function () {
519489
});
520490

521491
function setupClickHandlers() {
522-
// handleCurationStatusSelect();
523492
handleWorkflowStatusSelect();
524493
handleReindexingStatusSelect();
525494
handleCuratorSelect();

0 commit comments

Comments
 (0)