1
+ var csrftoken = $ ( 'input[name="csrfmiddlewaretoken"]' ) . val ( ) ;
2
+ var INDIVIDUAL_URL = 1 ;
3
+ var MULTI_URL_PATTERN = 2 ;
4
+ collection_id = getCollectionId ( ) ;
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
+ $ ( document ) . ready ( function ( ) {
11
+ // handleAjaxStartAndStop();
12
+ initializeDataTable ( ) ;
13
+ setupClickHandlers ( ) ;
14
+ } ) ;
15
+
16
+ function initializeDataTable ( ) {
17
+ var affected_urls_table = $ ( "#urlsTable" ) . DataTable ( {
18
+ pageLength : 100 ,
19
+ colReorder : true ,
20
+ stateSave : true ,
21
+ layout : {
22
+ bottomEnd : "inputPaging" ,
23
+ topEnd : null ,
24
+ topStart : {
25
+ info : true ,
26
+ pageLength : {
27
+ menu : [
28
+ [ 25 , 50 , 100 , 500 ] ,
29
+ [ "Show 25" , "Show 50" , "Show 100" , "Show 500" ] ,
30
+ ] ,
31
+ } ,
32
+ } ,
33
+ serverSide : true ,
34
+ orderCellsTop : true ,
35
+ pagingType : "input" ,
36
+ rowId : "url"
37
+ }
38
+ } )
39
+ }
40
+
41
+ function setupClickHandlers ( ) {
42
+ handleIncludeIndividualUrlClick ( ) ;
43
+ }
44
+
45
+ function handleIncludeIndividualUrlClick ( ) {
46
+ $ ( "body" ) . on ( "click" , ".include-url-btn" , function ( ) {
47
+
48
+ const span = this . querySelector ( 'span' ) ;
49
+ if ( span . classList . contains ( 'cross-mark' ) ) {
50
+ // Change to tick mark
51
+ span . classList . remove ( 'cross-mark' ) ;
52
+ span . innerHTML = '✔' ; // Tick mark
53
+ // then add that URL to the includeURLs list
54
+ match_pattern = remove_protocol ( $ ( this ) . attr ( "value" ) ) ;
55
+ match_pattern_type = INDIVIDUAL_URL ;
56
+ console . log ( match_pattern ) ;
57
+ postIncludePatterns (
58
+ match_pattern = match_pattern ,
59
+ match_pattern_type = match_pattern_type ,
60
+ true
61
+ ) ;
62
+
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 ) ;
67
+
68
+ //Along with this, remove this pattern from the exclude_patterns
69
+ // First, check if similar kind of pattern is available in the exclude_pattern table
70
+ // If yes, this run this block of code.
71
+ // postExcludePatterns(
72
+ // match_pattern = match_pattern,
73
+ // match_pattern_type = match_pattern_type,
74
+ // true
75
+ // );
76
+
77
+
78
+ } else {
79
+ // Change back to cross mark
80
+ span . classList . add ( 'cross-mark' ) ;
81
+ span . innerHTML = '❌' ; // Cross mark
82
+ }
83
+
84
+ } ) ;
85
+ }
86
+
87
+ function handleExcludeIndividualUrlClick ( ) {
88
+ // exclude that URL
89
+ // check in the include patterns if similar URL is present
90
+ // if yes then delete that URL in the
91
+
92
+ }
93
+
94
+ function postIncludePatterns ( match_pattern , match_pattern_type = 0 ) {
95
+ if ( ! match_pattern ) {
96
+ toastr . error ( "Please highlight a pattern to include." ) ;
97
+ return ;
98
+ }
99
+
100
+ // if pattern exists in table already
101
+ // var table = $("#include_patterns_table").DataTable();
102
+ // var itemIdColumnData = table.column(0).data().toArray();
103
+ // if (itemIdColumnData.includes(match_pattern)) {
104
+ // toastr.success("Pattern already exists");
105
+ // return;
106
+ // }
107
+
108
+ $ . ajax ( {
109
+ url : "/api/include-patterns/" ,
110
+ type : "POST" ,
111
+ data : {
112
+ collection : collection_id ,
113
+ match_pattern : match_pattern ,
114
+ match_pattern_type : match_pattern_type ,
115
+ csrfmiddlewaretoken : csrftoken ,
116
+ } ,
117
+ success : function ( data ) {
118
+ console . log ( "Success on adding to the Included URLs" ) ;
119
+ // $("#candidate_urls_table").DataTable().ajax.reload(null, false);
120
+ // $("#include_patterns_table").DataTable().ajax.reload(null, false);
121
+ // if(currentTab === ""){ //Only add a notification if we are on the first tab
122
+ // newIncludePatternsCount = newIncludePatternsCount + 1;
123
+ // $("#includePatternsTab").html(
124
+ // `Include Patterns <span class="pill notifyBadge badge badge-pill badge-primary">` +
125
+ // newIncludePatternsCount + " new" +
126
+ // `</span>`
127
+ // );
128
+ // }
129
+ } ,
130
+ error : function ( xhr , status , error ) {
131
+ var errorMessage = xhr . responseText ;
132
+ toastr . error ( errorMessage ) ;
133
+ } ,
134
+ } ) ;
135
+ }
136
+
137
+ function getCollectionId ( ) {
138
+ return collection_id ;
139
+ }
140
+
141
+ function remove_protocol ( url ) {
142
+ return url . replace ( / ( ^ \w + : | ^ ) \/ \/ / , "" ) ;
143
+ }
144
+
145
+
146
+ function postExcludePatterns ( match_pattern , match_pattern_type = 0 , force ) {
147
+ // if (!match_pattern) {
148
+ // toastr.error("Please highlight a pattern to exclude.");
149
+ // return;
150
+ // }
151
+ // if (!force) {
152
+ // //If the user clicked the icon in the table, we make the change regardless
153
+ // // if pattern exists in table already (unless another pattern overrules it)
154
+ // var table = $("#exclude_patterns_table").DataTable();
155
+ // var itemIdColumnData = table.column(0).data().toArray();
156
+ // if (itemIdColumnData.includes(match_pattern)) {
157
+ // toastr.success("Pattern already exists");
158
+ // return;
159
+ // }
160
+ // }
161
+
162
+ $ . ajax ( {
163
+ url : "/api/exclude-patterns/" ,
164
+ type : "POST" ,
165
+ data : {
166
+ collection : collection_id ,
167
+ match_pattern : match_pattern ,
168
+ match_pattern_type : match_pattern_type ,
169
+ csrfmiddlewaretoken : csrftoken ,
170
+ } ,
171
+ success : function ( data ) {
172
+ console . log ( "Success on removing from Excluded URLs" ) ;
173
+ // $("#candidate_urls_table").DataTable().ajax.reload(null, false);
174
+ // $("#exclude_patterns_table").DataTable().ajax.reload(null, false);
175
+ // if(currentTab === ""){ //Only add a notification if we are on the first tab
176
+ // newExcludePatternsCount = newExcludePatternsCount + 1;
177
+ // $("#excludePatternsTab").html(
178
+ // `Exclude Patterns <span class="pill notifyBadge badge badge-pill badge-primary">` +
179
+ // newExcludePatternsCount + " new" +
180
+ // `</span>`
181
+ // );
182
+ // }
183
+ } ,
184
+ error : function ( xhr , status , error ) {
185
+ var errorMessage = xhr . responseText ;
186
+ toastr . error ( errorMessage ) ;
187
+ } ,
188
+ } ) ;
189
+ }
190
+
191
+ function deleteRowById ( rowId ) {
192
+ // Find the DataTable instance
193
+ var affected_urls_table = $ ( "#urlsTable" ) . DataTable ( ) ;
194
+
195
+ // Find the row with ID 1
196
+ var rowToDelete = affected_urls_table . row ( rowId ) ; // Adjust based on 0-indexing
197
+
198
+ if ( rowToDelete . length ) {
199
+ rowToDelete . remove ( ) ; // Remove the row
200
+ affected_urls_table . draw ( ) ; // Redraw the table
201
+ } else {
202
+ console . log ( "Row not found." ) ;
203
+ }
204
+ }
205
+
0 commit comments