Changeset 38189 for main/trunk/greenstone3/web/interfaces/default/js
- Timestamp:
- 2023-09-22T21:55:51+12:00 (8 months ago)
- Location:
- main/trunk/greenstone3/web/interfaces/default/js
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js
r38188 r38189 1003 1003 } 1004 1004 1005 gs.functions.removeMetadataAtPos = function(collection, site, documentID, metadataName, metadataPosition, successResponseFunction, errorResponseFunction) 1006 { 1007 if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 1008 1009 var nameArray = ["removeImportMetadata", "removeArchivesMetadata", "removeIndexMetadata"]; 1010 var functionArray = ["remove-import-metadata", "remove-archives-metadata", "remove-metadata"]; 1011 1012 for(var i = 0; i < nameArray.length; i++) 1013 { 1014 gs.functions._callMetadataServer( 1015 nameArray[i], 1016 gs.functions.getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, null /*metamode*/, null /*metadataValue*/, null /*prevmetavalue*/, metadataPosition), 1017 successResponseFunction, 1018 errorResponseFunction); 1019 } 1020 } 1021 1005 1022 1006 1023 /*********************************************************** -
main/trunk/greenstone3/web/interfaces/default/js/user_comments.js
r38188 r38189 112 112 var json_result_str = (data.responseText) ? data.responseText : data; 113 113 114 //console.log("Got user comments to display: " + json_result_str);115 116 114 var result = JSON.parse(json_result_str); 117 115 // result contains only one docrec (result[0]), since we asked for the usercomments of one docid … … 122 120 123 121 var canDeleteComments = gs.variables["userCanDeleteComments"]; 124 //alert("canDeleteComments: " + canDeleteComments);125 126 122 127 123 // if there's at least one existing comment OR if the form is currently being displayed … … 157 153 158 154 // for each usercomment, create a child div with the username, timestamp and comment 159 gs.usercomments.displayInUserCommentList(usercommentdiv, username, timestamp, comment, canDeleteComments );155 gs.usercomments.displayInUserCommentList(usercommentdiv, username, timestamp, comment, canDeleteComments, i); 160 156 161 157 i++; … … 163 159 } 164 160 165 if(canDeleteComments) { 161 if(canDeleteComments != 0) { 162 var delButtonDiv = document.createElement("div"); 163 delButtonDiv.setAttribute("id", "del-button-div"); 166 164 var delCommentsButton = document.createElement("button"); 167 165 delCommentsButton.innerHTML = "Delete selected"; … … 170 168 // alternatively: https://www.w3schools.com/jsref/met_element_addeventlistener.asp 171 169 delCommentsButton.addEventListener("click", gs.usercomments.deleteSelectedComments); 172 usercommentdiv.appendChild(delCommentsButton); 173 170 171 delButtonDiv.appendChild(delCommentsButton); 172 173 var prevNode = document.getElementById("usercomments"); 174 gs.usercomments.insertAfter(delButtonDiv, prevNode); 174 175 } 175 176 … … 183 184 184 185 185 gs.usercomments.displayInUserCommentList = function(usercommentdiv, username, timestamp, comment, canDeleteComments) { 186 // https://www.javascripttutorial.net/javascript-dom/javascript-insertafter/ 187 gs.usercomments.insertAfter = function(newNode, existingNode) { 188 existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling); 189 } 190 191 gs.usercomments.displayInUserCommentList = function(usercommentdiv, username, timestamp, comment, canDeleteComments, metapos) { 192 193 if(metapos == undefined) { 194 metapos = document.querySelectorAll('div[class=usercomment]').length; 195 } 186 196 187 197 var divgroup=document.createElement("div"); … … 194 204 var divcomment=document.createElement("div"); 195 205 206 if(canDeleteComments != 0) { 207 var deleteCheckBox = document.createElement("input"); 208 deleteCheckBox.style.cssFloat = "left"; 209 deleteCheckBox.setAttribute("type", "checkbox"); 210 deleteCheckBox.setAttribute("class", "del-me-comment"); 211 deleteCheckBox.setAttribute("data-metapos", metapos); 212 deleteCheckBox.setAttribute("data-username", username); 213 deleteCheckBox.setAttribute("data-timestamp", timestamp); 214 deleteCheckBox.setAttribute("data-comment", comment); 215 divgroup.appendChild(deleteCheckBox); 216 } 196 217 197 218 divgroup.appendChild(divuser); 198 219 var txt=document.createTextNode(username); 199 if(canDeleteComments) { 200 var deleteCheckBox = document.createElement("input"); 201 deleteCheckBox.setAttribute("type", "checkbox"); 202 deleteCheckBox.setAttribute("class", "del-me-comment"); 203 deleteCheckBox.setAttribute("data-user", username); 204 deleteCheckBox.setAttribute("data-timestamp", timestamp); 205 deleteCheckBox.setAttribute("data-comment", comment); 206 divuser.appendChild(deleteCheckBox); 207 } 220 208 221 divuser.appendChild(txt); 209 222 … … 225 238 } 226 239 227 228 240 gs.usercomments.deleteSelectedComments = function() { 229 241 // https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array 230 242 // https://stackoverflow.com/questions/6166763/jquery-multiple-checkboxes-array 231 243 232 //var selectedComments = document.getElementsByClassName("del-me-comment");233 // var selectedComments = document.querySelectorAll('input[class=del-me-comment]:checked');234 244 var selectedComments = document.querySelectorAll('input[class=del-me-comment][type=checkbox]:checked'); 235 245 236 if(selectedComments.length==0) { 237 alert("Delete pressed, but nothing selected"); 238 } else { 239 //alert("Delete pressed " + selectedComments.length); 240 alert("Delete pressed for docID: " + gs.variables["docID"] 241 + " on num comments: " + selectedComments.length); 242 } 246 if(selectedComments.length > 0) { 247 248 // Don't allow the user to submit further comments or delete comments 249 // until the metadata has been updated 250 document.getElementById("usercommentSubmitButton").disabled = true; 251 var delCommentsButton = document.getElementById("delCommentsButton"); 252 if(delCommentsButton != undefined) {// should be defined when in this function 253 delCommentsButton.disabled = true; 254 } 255 256 // User comment meta are at topmost section of the document. 257 // So only get the docId up to any period mark: 258 var _docid = document.AddUserCommentForm.d.value; 259 var period = _docid.indexOf("."); 260 if(period != -1) { 261 _docid = _docid.substring(0, period); 262 } 263 264 //document.querySelectorAll('input[class=del-me-comment][type=checkbox]:checked').forEach(elem => console.log(elem.getAttribute('data-username'))); 265 266 var modifiedMetapos = []; 267 var failure = false; 268 269 // start deleting from end of metadata list, so we don't have to recalculate metapos each time 270 for (var i = selectedComments.length-1; i >= 0; i--) { 271 272 var metapos = selectedComments[i].getAttribute("data-metapos"); 273 //var username = selectedComments[i].getAttribute("data-username"); 274 //var timestamp = selectedComments[i].getAttribute("data-timestamp"); 275 //var comment = selectedComments[i].getAttribute("data-comment"); 276 //console.log("metapos: " + metapos); 277 278 // There is no remove array version yet!!! 279 280 //var result = gs.functions.removeMetadataAtPos(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index"); 281 gs.functions.removeMetadataAtPos(gs.variables["c"], 282 gs.variables["site"], 283 _docid, 284 "gs.username", 285 metapos, 286 //"import|archives|index", 287 null, //function(ajaxResult) { alert(ajaxResult); }, 288 function(ajaxError) { return gs.usercomments.removeFailed(ajaxError, metapos); }, 289 true); // true for synchronous, meaning is opposite to explanation for addUserComment() function below 290 291 if(failure) { 292 continue; 293 } 294 gs.functions.removeMetadataAtPos(gs.variables["c"], 295 gs.variables["site"], 296 _docid, 297 "gs.usertimestamp", 298 metapos, 299 //"import|archives|index", 300 null, //function(ajaxResult) { alert(ajaxResult); }, 301 function(ajaxError) { return gs.usercomments.removeFailed(ajaxError, metapos); }, 302 true); // true for synchronous, meaning is opposite to explanation for addUserComment() function below 303 304 if(failure) { 305 continue; 306 } 307 gs.functions.removeMetadataAtPos(gs.variables["c"], 308 gs.variables["site"], 309 _docid, 310 "gs.usercomment", 311 metapos, 312 //"import|archives|index", 313 null, //function(ajaxResult) { alert(ajaxResult); }, 314 function(ajaxError) { return gs.usercomments.removeFailed(ajaxError, metapos); }, 315 true); // true for synchronous, meaning is opposite to explanation for addUserComment() function below 316 317 318 gs.usercomments.removedMetadata(metapos, modifiedMetapos); 319 failure = false; // reinitialise 320 } 321 322 //console.log("modifiedMetapos: " + modifiedMetapos); 323 324 gs.usercomments.doneRemovingMetadata(modifiedMetapos); 325 } 326 } 327 328 329 gs.usercomments.removedMetadata = function(metapos, modifiedMetapos) { 330 modifiedMetapos.unshift(metapos); // add metapos to start of array 331 } 332 333 gs.usercomments.removeFailed = function(data, metapos) { 334 var result = (data.responseText) ? data.responseText : data; 335 alert("Remove failed. Got: " + result); 336 failure = true; 337 } 338 339 gs.usercomments.doneRemovingMetadata = function(modifiedMetapos) { 340 var userCommentsDiv = document.getElementById("usercomments"); 341 342 // modifiedMetaPos has all the positions for usercomments metadata that need to be removed, in normal (ascending) order 343 // So start at end, removing usercomments with highest metapos 344 for (var i = modifiedMetapos.length-1; i >= 0; i--) { 345 var metapos = modifiedMetapos[i]; 346 347 //console.log("About to remove usercomment at metapos " + metapos); 348 349 // find the checkbox with data-metapos=metapos and delete its username, timestamp, comment 350 var removeUserCommentForCheckbox = document.querySelector("input[data-metapos=\""+metapos+"\"]"); 351 var usercommentDivToRemove = removeUserCommentForCheckbox.parentElement; // get the <div class="usercomment"/> above this checkbox 352 userCommentsDiv.removeChild(usercommentDivToRemove); 353 } 354 355 // Now re-number the metapos for all the input class=del-me-comment 356 var usercommentCheckboxes = document.querySelectorAll('input[class=del-me-comment][type=checkbox]'); 357 for (var i = 0; i < usercommentCheckboxes.length; i++) { 358 usercommentCheckboxes[i].setAttribute("data-metapos", i); 359 } 360 361 // whether there was an error or not, re-enable the submit button now 362 // that the removeMetada ajax operations have completed. 363 document.getElementById("usercommentSubmitButton").disabled = false; 364 var delCommentsButton = document.getElementById("delCommentsButton"); 365 if(delCommentsButton != undefined) { 366 delCommentsButton.disabled = false; 367 } 368 243 369 } 244 370
Note:
See TracChangeset
for help on using the changeset viewer.