From 853db2c29561021ef97eb13530c7cffb44ec3e34 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Fri, 26 Jan 2018 16:00:37 +0100
Subject: [PATCH] ask for reason dialog added when removing comment

---
 frontend-js/src/main/js/ServerConnector.js    |   4 +-
 .../main/js/gui/admin/CommentsAdminPanel.js   | 146 ++++++++++--------
 frontend-js/src/main/js/map/data/Comment.js   |   9 +-
 3 files changed, 96 insertions(+), 63 deletions(-)

diff --git a/frontend-js/src/main/js/ServerConnector.js b/frontend-js/src/main/js/ServerConnector.js
index cf8845a05b..a76aedaa23 100644
--- a/frontend-js/src/main/js/ServerConnector.js
+++ b/frontend-js/src/main/js/ServerConnector.js
@@ -1689,7 +1689,9 @@ ServerConnector.removeComment = function (params) {
   var queryParams = {
     commentId: params.commentId
   };
-  var filterParams = {};
+  var filterParams = {
+    reason: params.reason
+  };
   return self.getProjectId(params.projectId).then(function (result) {
     queryParams.projectId = result;
     return self.sendDeleteRequest(self.deleteCommentUrl(queryParams), filterParams);
diff --git a/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js b/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
index 75e221292c..424b951e96 100644
--- a/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
+++ b/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
@@ -19,86 +19,86 @@ function CommentsAdminPanel(params) {
 CommentsAdminPanel.prototype = Object.create(AbstractAdminPanel.prototype);
 CommentsAdminPanel.prototype.constructor = CommentsAdminPanel;
 
-CommentsAdminPanel.prototype._createGui = function() {
+CommentsAdminPanel.prototype._createGui = function () {
   var self = this;
   var projectsDiv = Functions.createElement({
-    type : "div",
+    type: "div",
   });
   self.getElement().appendChild(projectsDiv);
 
   var dataDiv = Functions.createElement({
-    type : "div",
-    style : "display:table",
+    type: "div",
+    style: "display:table",
   });
   projectsDiv.appendChild(dataDiv);
 
   var commentsCell = Functions.createElement({
-    type : "div",
-    style : "display:table-cell;width:100%;vertical-align:top",
+    type: "div",
+    style: "display:table-cell;width:100%;vertical-align:top",
   });
   projectsDiv.appendChild(commentsCell);
 
   var commentsTable = Functions.createElement({
-    type : "table",
-    name : "commentsTable",
-    className : "display",
-    style : "width:100%",
+    type: "table",
+    name: "commentsTable",
+    className: "display",
+    style: "width:100%",
   });
   commentsCell.appendChild(commentsTable);
 
   $(commentsTable).DataTable({
-    columns : [ {
-      title : 'Id',
+    columns: [{
+      title: 'Id',
     }, {
-      title : 'Title',
+      title: 'Title',
     }, {
-      title : 'Author',
+      title: 'Author',
     }, {
-      title : 'Email',
+      title: 'Email',
     }, {
-      title : 'Content',
+      title: 'Content',
     }, {
-      title : 'Removed',
+      title: 'Removed',
     }, {
-      title : 'Pinned',
-    }, ],
+      title: 'Pinned',
+    },],
   });
 
   var projectsCell = Functions.createElement({
-    type : "div",
-    style : "display:table-cell",
+    type: "div",
+    style: "display:table-cell",
   });
   projectsDiv.appendChild(projectsCell);
 
   var selectProject = Functions.createElement({
-    type : "select",
-    name : "projectSelect",
+    type: "select",
+    name: "projectSelect",
   });
   selectProject.size = "12";
-  selectProject.onchange = function() {
+  selectProject.onchange = function () {
     return self.onProjectChange();
   };
 
   projectsCell.appendChild(Functions.createElement({
-    type : "h3",
-    content : "Project",
+    type: "h3",
+    content: "Project",
   }));
   projectsCell.appendChild(selectProject);
 };
 
-CommentsAdminPanel.prototype.init = function() {
+CommentsAdminPanel.prototype.init = function () {
   var self = this;
   return self.refreshProjects();
 };
 
-CommentsAdminPanel.prototype.refreshProjects = function() {
+CommentsAdminPanel.prototype.refreshProjects = function () {
   var self = this;
   var projectSelect = $("[name='projectSelect']", self.getElement())[0];
-  return ServerConnector.getProjects().then(function(projects) {
+  return ServerConnector.getProjects().then(function (projects) {
     while (projectSelect.firstChild) {
       projectSelect.removeChild(projectSelect.firstChild);
     }
-    projects.sort(function(project1, project2) {
+    projects.sort(function (project1, project2) {
       if (project1.getProjectId() < project2.getProjectId()) {
         return -1;
       } else if (project1.getProjectId() > project2.getProjectId()) {
@@ -115,45 +115,50 @@ CommentsAdminPanel.prototype.refreshProjects = function() {
       projectSelect.appendChild(new Option(name, value));
     }
     return ServerConnector.getProjectId();
-  }).then(function(projectId) {
+  }).then(function (projectId) {
     projectSelect.value = projectId;
     return self.onProjectChange();
   });
 };
 
-CommentsAdminPanel.prototype.getSelectedProjectId = function() {
+CommentsAdminPanel.prototype.getSelectedProjectId = function () {
   var projectSelect = $("[name='projectSelect']", this.getElement())[0];
   return projectSelect.value;
 };
 
-CommentsAdminPanel.prototype.onProjectChange = function() {
+CommentsAdminPanel.prototype.onProjectChange = function () {
   var self = this;
 
   return ServerConnector.getComments({
-    projectId : self.getSelectedProjectId(),
-  }).then(function(comments) {
+    projectId: self.getSelectedProjectId()
+  }).then(function (comments) {
     var dataTable = $($("[name='commentsTable']", self.getElement())[0]).DataTable();
     var data = [];
     for (var i = 0; i < comments.length; i++) {
       data.push(self.commentToTableRow(comments[i]));
     }
     dataTable.clear().rows.add(data).draw();
-    $("[name='commentsTable']", self.getElement()).on("click", "[name='removeComment']", function() {
+    $("[name='commentsTable']", self.getElement()).on("click", "[name='removeComment']", function () {
       var button = this;
-      return ServerConnector.removeComment({
-        commentId : $(button).attr("data")
-      }).then(function() {
-        $(button).after("<span>YES</span>");
-        button.style.display = "none";
-      });
+      return self.askConfirmRemoval().then(function (param) {
+        if (param.status) {
+          return ServerConnector.removeComment({
+            commentId: $(button).attr("data"),
+            reason: param.reason
+          }).then(function () {
+            $(button).after("<span>YES (" + param.reason + ")</span>");
+            button.style.display = "none";
+          });
+        }
+      })
     });
   });
 };
 
-CommentsAdminPanel.prototype.commentToTableRow = function(comment) {
+CommentsAdminPanel.prototype.commentToTableRow = function (comment) {
   var self = this;
   var projectId = self.getSelectedProjectId();
-  var toYesNo = function(val) {
+  var toYesNo = function (val) {
     if (val) {
       return "YES";
     } else {
@@ -163,10 +168,10 @@ CommentsAdminPanel.prototype.commentToTableRow = function(comment) {
   var title = null;
   if (!comment.isRemoved()) {
     var commentLink = "index.xhtml?id=" + projectId + // 
-    "&x=" + comment.getCoordinates().x + // 
-    "&y=" + comment.getCoordinates().y + // 
-    "&zoom=12" + //
-    "&comments=on";
+      "&x=" + comment.getCoordinates().x + //
+      "&y=" + comment.getCoordinates().y + //
+      "&zoom=12" + //
+      "&comments=on";
     title = "<a href='" + commentLink + "' target='" + projectId + "'>" + comment.getTitle() + "</a>";
   } else {
     title = comment.getTitle();
@@ -174,7 +179,7 @@ CommentsAdminPanel.prototype.commentToTableRow = function(comment) {
 
   var remove = null;
   if (comment.isRemoved()) {
-    remove = "YES";
+    remove = "YES (" + comment.getRemoveReason() + ")";
   } else {
     remove = "<button name='removeComment' data='" + comment.getId() + "'>REMOVE</button>";
   }
@@ -189,22 +194,41 @@ CommentsAdminPanel.prototype.commentToTableRow = function(comment) {
     email = "N/A";
   }
 
-  var row = [ comment.getId(), // 
-  title, //
-  author, // 
-  email, // 
-  comment.getContent(), // 
-  remove, //
-  toYesNo(comment.isPinned()) ];
+  var row = [comment.getId(), //
+    title, //
+    author, //
+    email, //
+    comment.getContent(), //
+    remove, //
+    toYesNo(comment.isPinned())];
   return row;
 };
 
 CommentsAdminPanel.prototype.destroy = function () {
-    var self = this;
-    var table = $("[name='commentsTable']", self.getElement())[0];
-    if ($.fn.DataTable.isDataTable(table)) {
-        $(table).DataTable().destroy();
-    }
+  var self = this;
+  var table = $("[name='commentsTable']", self.getElement())[0];
+  if ($.fn.DataTable.isDataTable(table)) {
+    $(table).DataTable().destroy();
+  }
+};
+
+CommentsAdminPanel.prototype.askConfirmRemoval = function () {
+  return new Promise(function (resolve) {
+    $('<form><input type="text" style="z-index:10000" name="name"><br></form>').dialog({
+      modal: true,
+      title: "Why do you want to remove this comment?",
+      buttons: {
+        'OK': function () {
+          $(this).dialog('close');
+          resolve({reason: $('input[name="name"]').val(), status: true});
+        },
+        'Cancel': function () {
+          $(this).dialog('close');
+          resolve({status: false});
+        }
+      }
+    });
+  })
 };
 
 module.exports = CommentsAdminPanel;
diff --git a/frontend-js/src/main/js/map/data/Comment.js b/frontend-js/src/main/js/map/data/Comment.js
index 735e508c1d..b075e57792 100644
--- a/frontend-js/src/main/js/map/data/Comment.js
+++ b/frontend-js/src/main/js/map/data/Comment.js
@@ -7,10 +7,11 @@ function Comment(javaObject) {
     id : javaObject.elementId,
     type : javaObject.type,
     modelId : javaObject.modelId,
-    icon : javaObject.icon,
+    icon : javaObject.icon
   }));
   this.setId(javaObject.id);
   this.setRemoved(javaObject.removed);
+  this.setRemoveReason(javaObject.removeReason);
   this.setPinned(javaObject.pinned);
 
   if (javaObject.title !== undefined) {
@@ -82,5 +83,11 @@ Comment.prototype.setEmail = function(email) {
 Comment.prototype.getEmail = function() {
   return this._email;
 };
+Comment.prototype.setRemoveReason = function(removeReason) {
+  this._removeReason = removeReason;
+};
+Comment.prototype.getRemoveReason = function() {
+  return this._removeReason;
+};
 
 module.exports = Comment;
-- 
GitLab