Skip to content

Instantly share code, notes, and snippets.

@qrilka
Created January 9, 2011 14:44
Show Gist options
  • Select an option

  • Save qrilka/771734 to your computer and use it in GitHub Desktop.

Select an option

Save qrilka/771734 to your computer and use it in GitHub Desktop.
Sketch of ManyToMany editing
package ru.qrilka.snippet
import net.liftweb.util.Helpers._
import xml._
import net.liftweb.mapper.view._
import net.liftweb.http._
import js.JE.JsRaw
import js.JsCmd
import js.JsCmds._
import ru.qrilka.model._
import net.liftweb.common._
import net.liftweb.mapper.By
class Roles extends PaginatedModelSnippet[Role] {
val view = new ModelView(new Role, this)
val paginator = new MapperPaginatorSnippet(Role) {
override def itemsPerPage = 3
}
def list(xhtml: NodeSeq): NodeSeq = Role.findAll.flatMap({ role =>
bind("role", xhtml,
"name" -> Text(role.name.is),
new ModelView(role, this).editAction
)
})
def edit(xhtml: NodeSeq): NodeSeq = {
val logger = Logger(classOf[Roles])
val role = view.entity
var domainStr = ""
val listId = "list"
val selectId = "a"
def updateDomainsInView: JsCmd = {
Replace(listId, listDomains(role)) &
Replace(selectId, getDomains)
}
def deleteDomain(domain: Domain): JsCmd = {
role.domains -= domain
role.domains.save
logger.info("deleted" + domain.name)
updateDomainsInView &
Alert("Deleted")
}
def itemTemplate =
<div id={listId}>
<domain:item>
<item:name/>
<item:delete/>
<br/>
</domain:item>
</div>
def listDomains(role: Role): NodeSeq = role.domains.all match {
case Nil => NodeSeq.Empty
case xs => bind("domain", itemTemplate,
"item" -> (lst => xs.flatMap(domain => bind("item", lst,
"name" -> Text(domain.name.is),
"delete" -> SHtml.ajaxButton("[X]", () => { deleteDomain(domain)})))))
}
def getDomains: NodeSeq = {
val domains = Domain.findAll().map(domain => (domain.id.toString, domain.name.toString))
SHtml.select(domains, Empty, domainStr = _, "id" -> selectId)
}
def addDomain(): JsCmd = {
logger.info("adding : '" + domainStr + "'.")
asInt(domainStr) match {
case Full(id) => {
Domain.find(By(Domain.id, id)) match {
case Full(domain) => {
role.domains += domain
role.domains.save
updateDomainsInView &
Alert(domain.name)
}
case _ => { S.error("id", "No such domain"); Noop}
}
}
case _ => {S.error("id", "Not an integer id"); Noop}
}
}
val domains = Domain.findAll().map(domain => (domain.id.toString, domain.name.toString))
bind("role", xhtml,
"name" -> Text(role.name.is),
"domains" -> listDomains(role),
"available_domains" ->
(SHtml.select(domains, Empty, domainStr = _, "id" -> selectId) ++ SHtml.hidden(addDomain))
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment