Skip to content

Commit

Permalink
#78 closed: "Anonymous user should be able to add content"
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatov committed Jul 27, 2011
1 parent a81c476 commit 32539d9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/main/scala/scala/tools/colladoc/model/mapper/Content.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,28 @@ class Content extends LongKeyedMapper[Content] with IdPK {
override def defaultValue = true
}

/** Changed comment user name email if author is null. */
object name extends MappedString(this, 255)

/** Changed comment email if author is null. */
object email extends MappedEmail(this, 255)

/** Get change author's username. */
def userName: String = User.find(user.is) match {
case Full(u) => u.userName
case _ => ""
case _ => name.is
}

/** Link to user's profile. */
def authorProfileHyperlink = User.find(user.is) match {
case Full(u) => u.profileHyperlink
case _ => NodeSeq.Empty
case _ => <span>{name.is}</span>
}

/** Author's email. */
/**Author's email. */
def authorEmail = User.find(user.is) match {
case Full(u) => u.email.is
case _ => ""
case _ => email.is
}

/** Get change author's username and date. */
Expand Down
81 changes: 76 additions & 5 deletions src/main/scala/scala/tools/colladoc/page/Template.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package page {

import lib.util.Helpers._
import lib.util.NameUtils._
import lib.util.PathUtils._
import lib.js.JqJsCmds._
import lib.js.JqUI._
import lib.widgets.Editor
Expand All @@ -34,7 +33,7 @@ import model.Model.factory._
import model.mapper.{Discussion, Comment, Content, User}

import net.liftweb.common._
import net.liftweb.http.SHtml
import net.liftweb.http.{S, SHtml}
import net.liftweb.http.js._
import net.liftweb.http.js.jquery.JqJE._
import net.liftweb.http.js.JE._
Expand All @@ -43,7 +42,7 @@ import net.liftweb.mapper._
import net.liftweb.util.Helpers._

import tools.nsc.doc.model._
import xml.{NodeSeq, Text}
import xml.{Elem, NodeSeq, Text}
import lib.DependencyFactory._
import tools.nsc.util.NoPosition
import net.liftweb.widgets.gravatar.Gravatar
Expand Down Expand Up @@ -357,7 +356,12 @@ class Template(tpl: DocTemplateEntity) extends tools.nsc.doc.html.page.Template(
<ul id="content_thread">
{contentComments map (d => contentToHtmlWithActions(d))}
</ul>
{ if (User.loggedIn_?) contentCommentAddButton }
{
if (User.loggedIn_?)
contentCommentAddButton
else
contentCommentAddButtonForAnonymous
}
</div>
</div>

Expand Down Expand Up @@ -397,6 +401,11 @@ class Template(tpl: DocTemplateEntity) extends tools.nsc.doc.html.page.Template(
private def contentCommentAddButton = {
SHtml.ajaxButton(Text("Add comment"), contentEditor(None) _, ("class", "button"), ("id", "add_content_button"))
}

/** Render add comment button. */
private def contentCommentAddButtonForAnonymous = {
SHtml.ajaxButton(Text("Add comment"), () => contentEditorForAnonymous, ("class", "button"), ("id", "add_content_button"))
}

/** Render editor. */
private def contentEditor(maybe: Option[Content] = None)(): JsCmd = {
Expand Down Expand Up @@ -455,7 +464,69 @@ class Template(tpl: DocTemplateEntity) extends tools.nsc.doc.html.page.Template(
private def saveContentComment(text: String) {
Content.create.qualifiedName(tpl.qualifiedName).comment(text).dateTime(now).user(User.currentUser.open_!).valid(true).save
}


def contentEditorForAnonymous: JsCmd = {
var name, email, text = ""

def form(n: Elem) =
<lift:form class="edit content_form" method="GET">
<fieldset>
<div>
<label for="name">Name</label>
{
SHtml.text(name, (s: String) => {name = s},
("id", "name"),
("class", "text required ui-widget-content ui-corner-all"))
}
</div>
<div>
<label for="content_anonymous_email">Email</label>
{
SHtml.email(email, (s: String) => {email = s},
("id", "content_anonymous_email"),
("class", "email required ui-widget-content ui-corner-all"))
}
</div>
<div class="editor">
<label>Comment
{ n }
</label>
<div class="buttons">
<content:save />
{
SHtml.a(Text("Cancel"),
reloadContent,
("class", "button"))
}
</div>
</div>
<content:submit />
</fieldset>
</lift:form>

def bindedForm(n: Elem) = bind("content", form(n),
"submit" -> SHtml.hidden(() => saveContentWithEmail(text, name, email)),
"save" -> SHtml.a(Text("Save"), SubmitFormWithValidation(".content_form"), ("class", "button "))
)

Editor.editorObj("", preview _, text = _) match {
case (n, j) =>
Replace("add_content_button", bindedForm(n)) & j & Jq(Str(".button")) ~> Button()
case _ => JsCmds.Noop
}
}

def saveContentWithEmail(text: String, name: String, email: String) = {
val c = Content.create.qualifiedName(tpl.qualifiedName).comment(text).dateTime(now).name(name).email(email).valid(true)
c.validate match {
case Nil =>
c.save
case n =>
S.error(n)
}
reloadContent
}

/** Render delete button for content comment. */
def deleteContentButton(d: Content) = SHtml.a(
ColladocConfirm("Confirm delete"), () => {d.valid(false).save; reloadContent}, Text("Delete"))
Expand Down
33 changes: 33 additions & 0 deletions src/main/webapp/cotemplate.css
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,36 @@ background-color: #E5E5E5;
.content_comment_actions > a:hover {
text-decoration: underline;
}

.content_form label {
display: block;
font-weight: bold;
margin-bottom: 2px;
}

.content_form input {
display: block;
border: 1px solid gray;
}

.content_form div {
margin-bottom: 5px;
}

form.edit div.buttons > a {
font-weight: normal;
color: #555;
}

#content_form fieldset {
padding: 0;
border: 0;
margin-top: 8px;
}

.content_form .error {
color: #666666;
display: block;
/*font-size: 8pt;*/
padding-top: 0.2em;
}

0 comments on commit 32539d9

Please sign in to comment.