Author: jonathan
Date: Tue Jan 22 15:12:56 2008
New Revision: 12710

Log:

MASHUP-562 (partial): Made posting comments an ajax operation (no more 
duplicate posts on page refreshes now).
Added graphical delete buttons (look better and no more scrolling to the top of 
the page when deleting).
Added "busy" cursor while waiting for an ajax operation to complete.
Simpler error message reporting.

Added:
   trunk/mashup/java/modules/www/ajax_comment.jsp
   trunk/mashup/java/modules/www/images/smalldelete.gif   (contents, props 
changed)
Modified:
   trunk/mashup/java/modules/www/ajax_tag.jsp
   trunk/mashup/java/modules/www/mashup.jsp

Added: trunk/mashup/java/modules/www/ajax_comment.jsp
==============================================================================
--- (empty file)
+++ trunk/mashup/java/modules/www/ajax_comment.jsp      Tue Jan 22 15:12:56 2008
@@ -0,0 +1,114 @@
+<%--
+ * Copyright 2006,2007 WSO2, Inc. http://www.wso2.org
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+--%>
+<%@ page import="org.wso2.mashup.webapp.utils.QueryParamUtils" %>
+<%@ page import="org.wso2.mashup.webapp.utils.RegistryUtils" %>
+<%@ page import="org.wso2.registry.Comment" %>
+<%@ page import="org.wso2.registry.Registry" %>
+<%@ page import="org.wso2.registry.RegistryException" %>
+<%@ page import="org.wso2.registry.Resource" %>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+
+<%
+    Registry registry = RegistryUtils.getRegistry(request);
+    
+    boolean success = true;
+    String path = "", author = "", commentText, reason = "";
+    Resource resource;
+    Comment[] comments = new Comment[] {};
+    String currentUser = RegistryUtils.getCurrentUser(registry);
+
+    try {
+        path = request.getParameter("path");
+
+        if (!RegistryUtils.isLoggedIn(registry)) {
+            success = false;
+            reason = "Unauthorized attempt to add tag - please log in and try 
again.";
+        }
+
+        if (request.getMethod().equals("POST")) {
+            // Prototype.js simulates a DELETE with a POST with an additional 
method.  YUK?!
+            String method = request.getParameter("_method");
+            if (method != null && method.equals("delete")) {
+                commentText = request.getParameter("comment");
+                String commentTime = request.getParameter("time");
+
+                // delete the comment here.
+                success = false;
+                reason = "Unable to delete tags at this time";
+            } else {
+                commentText = RegistryUtils.getRequestBody(request);
+
+                try {
+                    registry.addComment(path, new Comment(commentText));
+                } catch (RegistryException e) {
+                    success = false;
+                    reason = e.getMessage();
+                }
+            }
+        }
+
+        resource = registry.get(path);
+        author = resource.getAuthorUserName();
+        comments = registry.getComments(resource.getPath());
+
+    } catch (RegistryException e) {
+        success = false;
+        reason = e.getMessage();
+    }
+%>
+<div id="comments" class="detail-section">
+    <table width="100%" border="0" cellpadding="3" cellspacing="0" 
class="data">
+
+        <tbody>
+            <tr>
+                <td width="10%" class="headers">Date</td>
+                <td width="16%" class="headers">Author</td>
+                <td class="headers">Comment</td>
+            </tr>
+
+            <%
+                for (int i = 0; i < comments.length; i++) {
+                    Comment c = comments[i];
+                    String commenter = c.getUser();
+            %>
+
+            <tr>
+                <td>
+                    <nobr><%=QueryParamUtils.friendlyDate(c.getTime())%></nobr>
+                </td>
+                <td><a href="user.jsp?name=<%=commenter%>"><%=  RegistryUtils
+                        .getFullName(request, commenter) %> (<%=commenter%>)
+                </a></td>
+                <td>
+                    <%= c.getText() %>
+                    <%
+                        if (author.equals(currentUser) || 
commenter.equals(currentUser)) {
+                            %>&nbsp;<span class="edit-link"><img 
src="images/smalldelete.gif" alt="remove this comment" 
onClick="removeComment('<%=c.getTime()%>')"/></span><%
+                        }
+                    %>
+                </td>
+
+                <!-- todo: fix commented timezone -->
+            </tr>
+
+            <% } %>
+
+        </tbody>
+    </table>
+    <% if (!success) { %>
+            <div class="error">Error! <%=reason%></div>
+    <%  } %>
+</div>

Modified: trunk/mashup/java/modules/www/ajax_tag.jsp
==============================================================================
--- trunk/mashup/java/modules/www/ajax_tag.jsp  (original)
+++ trunk/mashup/java/modules/www/ajax_tag.jsp  Tue Jan 22 15:12:56 2008
@@ -94,13 +94,12 @@
         } %>
         <a class="link-cloud2" 
href="search.jsp?query=mashups&for=<%=name%>&tags=true"><%=name%></a><%
         if (canDelete) {
-            %>&nbsp;<span class="edit-link">[<a href="#" title="remove this 
tag" onClick="removeTag('<%=name%>')">x</a>]</span><%
+            %>&nbsp;<span class="edit-link"><img src="images/smalldelete.gif" 
alt="remove this tag" onClick="removeTag('<%=name%>')"/></span><%
         }
         if (i < tags.length - 1) out.print(","); %>&nbsp; <%
     } 
 
     if (!success) { %>
-        <div class="error">Unable to add tag!</div>
-        <div class="error"><%=reason%></div>
+        <div class="error">Error! <%=reason%></div>
 <%  } %>
 </div>

Added: trunk/mashup/java/modules/www/images/smalldelete.gif
==============================================================================
Binary file. No diff available.

Modified: trunk/mashup/java/modules/www/mashup.jsp
==============================================================================
--- trunk/mashup/java/modules/www/mashup.jsp    (original)
+++ trunk/mashup/java/modules/www/mashup.jsp    Tue Jan 22 15:12:56 2008
@@ -183,13 +183,15 @@
         if (newValue == "") return;
         var path = $('addtag_path').value;
         var tagger = $('addtag_tagger').value;
-
+        $('tags_area').style.cursor = "progress";
+        
         new Ajax.Request("ajax_tag.jsp?path=" + path + "&tagger=" + tagger, {
             method: "post",
             contentType: "text/html",
             postBody: newValue,
             onSuccess: function (transport) {
                 $('taglist').replace(transport.responseText);
+                $('tags_area').style.cursor = "default";
             },
             onFailure: function (transport) {
                 alert("error");
@@ -201,6 +203,7 @@
     function removeTag(tag) {
         var path = $('addtag_path').value;
         var tagger = $('addtag_tagger').value;
+        $('tags_area').style.cursor = "progress";
 
         new Ajax.Request("ajax_tag.jsp", {
             method: "delete",
@@ -211,6 +214,7 @@
             },
             onSuccess: function (transport) {
                 $('taglist').replace(transport.responseText);
+                $('tags_area').style.cursor = "default";
             },
             onFailure: function (transport) {
                 alert("error");
@@ -228,6 +232,61 @@
         var isEnter = (key == 13);
         if (isEnter) addTag();        
     }
+
+    function addComment() {
+        var newValue = $("Comment_commentText").value;
+        if (newValue == "") return;
+        var path = $('Comment_path').value;
+        var commenter = $('Comment_commenter').value;
+        $('comments_area').style.cursor = "progress";
+
+        new Ajax.Request("ajax_comment.jsp?path=" + path + "&commenter=" + 
commenter, {
+            method: "post",
+            contentType: "text/html",
+            postBody: newValue,
+            onSuccess: function (transport) {
+                $('comments').replace(transport.responseText);
+                $('comments_area').style.cursor = "default";
+            },
+            onFailure: function (transport) {
+                alert("error");
+            }
+        });
+        $("Comment_commentText").value = "";
+    }
+
+    function removeComment(time) {
+        var path = $('Comment_path').value;
+        var commenter = $('Comment_commenter').value;
+        $('comments_area').style.cursor = "progress";
+
+        new Ajax.Request("ajax_comment.jsp", {
+            method: "delete",
+            parameters: {
+                "path" : path,
+                "commenter" : commenter,
+                "time" : time
+            },
+            onSuccess: function (transport) {
+                $('comments').replace(transport.responseText);
+                $('comments_area').style.cursor = "default";
+            },
+            onFailure: function (transport) {
+                alert("error");
+            }
+        });
+    }
+
+    function checkCommentEnter(e) {
+        var key;
+        if (window.event)
+            key = window.event.keyCode;     //IE
+        else
+            key = e.which;     //firefox
+
+        var isEnter = (key == 13);
+        if (isEnter) addComment();
+    }
 </script>
 
 </head>
@@ -376,7 +435,7 @@
     <td class="mashup_label">
         <nobr>tags:</nobr>
     </td>
-    <td>
+    <td id="tags_area">
         <div class="link-cloud" id="taglist">
 
             <% org.wso2.registry.Tag[] tags = 
registry.getTags(resource.getPath());
@@ -401,7 +460,7 @@
                     } %>
                     <a class="link-cloud2" 
href="search.jsp?query=mashups&for=<%=name%>&tags=true"><%=name%></a><%
                     if (canDelete) {
-                        %>&nbsp;<span class="edit-link">[<a href="#" 
title="remove this tag" onClick="removeTag('<%=name%>')">x</a>]</span><%
+                        %>&nbsp;<span class="edit-link"><img 
src="images/smalldelete.gif" alt="remove this tag" 
onClick="removeTag('<%=name%>')"/></span><%
                     }
                     if (i < tags.length - 1) out.print(","); %>&nbsp; <%
                 } %>
@@ -431,10 +490,15 @@
 
 <br/>
 
+<div id="comments_area">
+
 <div class="heading">Comments</div>
 <img src="images/blank.gif" width="10" height="12"/>
 
-<div class="detail-section">
+<%
+    Comment[] comments = registry.getComments(resource.getPath());
+%>
+<div id="comments" class="detail-section">
     <table width="100%" border="0" cellpadding="3" cellspacing="0" 
class="data">
 
         <tbody>
@@ -445,29 +509,28 @@
             </tr>
 
             <%
-                String commentText = request.getParameter("commentText");
-                if (commentText != null && commentText != "" &&
-                        RegistryUtils.isLoggedIn(registry)) {
-                    registry.addComment(resource.getPath(), new 
Comment(commentText));
-                }
-
-                Comment[] comments = registry.getComments(resource.getPath());
-
                 for (int i = 0; i < comments.length; i++) {
                     Comment c = comments[i];
+                    String commenter = c.getUser();
             %>
 
             <tr>
                 <td>
                     <nobr><%=QueryParamUtils.friendlyDate(c.getTime())%></nobr>
                 </td>
-                <td><a href="user.jsp?name=<%=c.getUser()%>"><%=  RegistryUtils
-                        .getFullName(request, c.getUser()) 
%>&nbsp;(<%=c.getUser()%>)
+                <td><a href="user.jsp?name=<%=commenter%>"><%=  RegistryUtils
+                        .getFullName(request, commenter) %> (<%=commenter%>)
                 </a></td>
-                <td><%= c.getText() %>
+                <td>
+                    <%= c.getText() %>
+                    <%
+                        if (author.equals(currentUser) || 
commenter.equals(currentUser)) {
+                            %>&nbsp;<span class="edit-link"><img 
src="images/smalldelete.gif" alt="remove this comment" 
onClick="removeComment('<%=c.getTime()%>')"/></span><%
+                        }
+                    %>
                 </td>
-                <!-- todo: add delete comment controls (when appropriate)
-                    todo: fix commented timezone -->
+
+                <!-- todo: fix commented timezone -->
             </tr>
 
             <% } %>
@@ -481,18 +544,18 @@
 <div class="detail-section">
 
     <% if (RegistryUtils.isLoggedIn(registry)) { %>
-    <form id="Comment" name="frmComment" action="mashup.jsp?path=<%= path %>" 
method="post">
         <input type="hidden" name="path" value="<%= path %>" 
id="Comment_path"/>
-        <input type="text" name="commentText" size="70" 
id="Comment_commentText" maxlength="500"/>
-        <input type="submit" value="Post comment"/>
-    </form>
-    <% } else { %>
-    Sign in to post a comment.
+        <input type="hidden" value="<%=currentUser%>" id="Comment_commenter"/>
+        <input type="text" name="commentText" size="70" 
id="Comment_commentText" maxlength="500" onkeydown="checkCommentEnter(event)"/>
+        <input type="submit" id="Comment_submit" value="Post comment" 
onclick="addComment()"/>
+     <% } else { %>
+        Sign in to post a comment.
     <% } %>
 
 </div>
 
 <br/>
+</div>
 </td>
 
 </tr>

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to