@@ -3,18 +3,14 @@ var INDIVIDUAL_URL = 1;
3
3
var MULTI_URL_PATTERN = 2 ;
4
4
collection_id = getCollectionId ( ) ;
5
5
6
- // Maybe you need to define a new DataTable for this page as well
7
- // So that you can refresh it any way you want
8
- // Or maybe this is not necessary
9
-
10
6
$ ( document ) . ready ( function ( ) {
11
7
// handleAjaxStartAndStop();
12
8
initializeDataTable ( ) ;
13
9
setupClickHandlers ( ) ;
14
- } ) ;
10
+ } ) ;
15
11
16
12
function initializeDataTable ( ) {
17
- var affected_urls_table = $ ( "#urlsTable " ) . DataTable ( {
13
+ var affected_urls_table = $ ( "#affectedURLsTable " ) . DataTable ( {
18
14
pageLength : 100 ,
19
15
colReorder : true ,
20
16
stateSave : true ,
@@ -34,7 +30,18 @@ function initializeDataTable() {
34
30
orderCellsTop : true ,
35
31
pagingType : "input" ,
36
32
rowId : "url"
37
- }
33
+ } ,
34
+ // createdRow: function (row, data, dataIndex) {
35
+ // // Assuming the 'Include URL' column is at index 3
36
+ // let includeUrlCell = $(row).find('td').eq(2);
37
+
38
+ // // Check if the cell has the cross-mark
39
+ // if (includeUrlCell.find('i.cross-mark').length > 0) {
40
+ // // Highlight the row if it contains a cross-mark
41
+ // $(row).css('background-color', 'rgba(255, 61, 87, 0.36)'); // Light red background
42
+ // }
43
+ // }
44
+
38
45
} )
39
46
}
40
47
@@ -45,11 +52,22 @@ function setupClickHandlers() {
45
52
function handleIncludeIndividualUrlClick ( ) {
46
53
$ ( "body" ) . on ( "click" , ".include-url-btn" , function ( ) {
47
54
48
- const span = this . querySelector ( 'span ' ) ;
49
- if ( span . classList . contains ( 'cross-mark' ) ) {
55
+ const i = this . querySelector ( 'i ' ) ;
56
+ if ( i . classList . contains ( 'cross-mark' ) ) {
50
57
// Change to tick mark
51
- span . classList . remove ( 'cross-mark' ) ;
52
- span . innerHTML = '✔' ; // Tick mark
58
+ i . classList . remove ( 'cross-mark' ) ;
59
+ i . classList . add ( 'tick-mark' ) ; // Add the tick-mark class
60
+ i . style . color = 'green' ; // Change color to green
61
+ i . textContent = 'check' ; // Set the text to "check"
62
+ let parentCol3 = i . closest ( '.col-3' ) ;
63
+ // Change the data-sort attribute of the parent element
64
+ if ( parentCol3 ) {
65
+ parentCol3 . setAttribute ( 'data-sort' , '1' ) ; // Set data-sort to '1' for the check-mark
66
+ }
67
+
68
+ // // Change to tick mark
69
+ // i.classList.remove('cross-mark');
70
+ // i.innerHTML = '✔'; // Tick mark
53
71
// then add that URL to the includeURLs list
54
72
match_pattern = remove_protocol ( $ ( this ) . attr ( "value" ) ) ;
55
73
match_pattern_type = INDIVIDUAL_URL ;
@@ -60,10 +78,10 @@ function handleIncludeIndividualUrlClick() {
60
78
true
61
79
) ;
62
80
63
- const row = $ ( this ) . closest ( 'tr' ) ; // Get the closest table row
64
- const rowId = $ ( "#urlsTable " ) . DataTable ( ) . row ( row ) . index ( ) ;
65
- console . log ( rowId ) ;
66
- deleteRowById ( rowId ) ;
81
+ // const row = $(this).closest('tr'); // Get the closest table row
82
+ // const rowId = $("#affectedURLsTable ").DataTable().row(row).index();
83
+ // console.log(rowId);
84
+ // deleteRowById(rowId);
67
85
68
86
//Along with this, remove this pattern from the exclude_patterns
69
87
// First, check if similar kind of pattern is available in the exclude_pattern table
@@ -74,11 +92,42 @@ function handleIncludeIndividualUrlClick() {
74
92
// true
75
93
// );
76
94
77
-
95
+
78
96
} else {
79
- // Change back to cross mark
80
- span . classList . add ( 'cross-mark' ) ;
81
- span . innerHTML = '❌' ; // Cross mark
97
+ // Handle the functionality of excluding that URL again (maybe delete that include pattern which was just created)
98
+ var url = $ ( this ) . attr ( "value" ) ;
99
+ console . log ( "url" , url ) ;
100
+ getCorrespondingIncludePattern ( url ) . then ( function ( patternId ) {
101
+ if ( patternId !== null ) {
102
+ console . log ( 'Pattern ID:' , patternId ) ;
103
+ currentURLtoDelete = `/api/include-patterns/${ patternId } /` ;
104
+ deletePattern (
105
+ currentURLtoDelete ,
106
+ ( data_type = "Include Pattern" )
107
+ ) ;
108
+
109
+ // Change back to cross mark
110
+ i . classList . remove ( 'tick-mark' ) ;
111
+ i . classList . add ( 'cross-mark' ) ; // Add the cross-mark class
112
+ i . style . color = 'red' ; // Change color to red
113
+ i . textContent = 'close' ; // Set the text to "close"
114
+ let parentCol3 = i . closest ( '.col-3' ) ;
115
+ // Change the data-sort attribute of the parent element
116
+ if ( parentCol3 ) {
117
+ parentCol3 . setAttribute ( 'data-sort' , '0' ) ; // Set data-sort to '0' for the cross-mark
118
+ }
119
+
120
+
121
+ console . log ( "URL removed from the include pattern" )
122
+
123
+
124
+ } else {
125
+ console . log ( 'No matching pattern found.' ) ;
126
+ }
127
+ } ) . catch ( function ( error ) {
128
+ console . error ( "Error occurred:" , error ) ;
129
+ } ) ;
130
+
82
131
}
83
132
84
133
} ) ;
@@ -190,7 +239,7 @@ function postExcludePatterns(match_pattern, match_pattern_type = 0, force) {
190
239
191
240
function deleteRowById ( rowId ) {
192
241
// Find the DataTable instance
193
- var affected_urls_table = $ ( "#urlsTable " ) . DataTable ( ) ;
242
+ var affected_urls_table = $ ( "#affectedURLsTable " ) . DataTable ( ) ;
194
243
195
244
// Find the row with ID 1
196
245
var rowToDelete = affected_urls_table . row ( rowId ) ; // Adjust based on 0-indexing
@@ -202,4 +251,44 @@ function deleteRowById(rowId) {
202
251
console . log ( "Row not found." ) ;
203
252
}
204
253
}
205
-
254
+
255
+ function deletePattern (
256
+ url ,
257
+ data_type ,
258
+ url_type = null ,
259
+ candidate_urls_count = null
260
+ ) {
261
+ $ . ajax ( {
262
+ url : url ,
263
+ type : "DELETE" ,
264
+ data : {
265
+ csrfmiddlewaretoken : csrftoken ,
266
+ } ,
267
+ headers : {
268
+ "X-CSRFToken" : csrftoken ,
269
+ } ,
270
+ success : function ( data ) {
271
+ console . log ( "Successfully deleted." )
272
+ } ,
273
+ } ) ;
274
+ }
275
+
276
+ function getCorrespondingIncludePattern ( url ) {
277
+ return $ . ajax ( {
278
+ url : `/api/include-patterns/?format=datatables&collection_id=${ collection_id } ` ,
279
+ method : 'GET' ,
280
+ dataType : 'json'
281
+ } ) . then ( function ( response ) {
282
+ // Iterate through the 'data' array to find a matching pattern
283
+ for ( let i = 0 ; i < response . data . length ; i ++ ) {
284
+ let pattern = response . data [ i ] . match_pattern ;
285
+ if ( pattern === remove_protocol ( url ) ) {
286
+ return response . data [ i ] . id ; // Return the first matching pattern id
287
+ }
288
+ }
289
+ return null ; // Return null if no pattern matches
290
+ } ) . catch ( function ( error ) {
291
+ console . error ( "Error fetching include patterns:" , error ) ;
292
+ return null ;
293
+ } ) ;
294
+ }
0 commit comments