Skip to content

Instantly share code, notes, and snippets.

@tuhlmann
Created July 19, 2013 13:27
Show Gist options
  • Save tuhlmann/6039092 to your computer and use it in GitHub Desktop.
Save tuhlmann/6039092 to your computer and use it in GitHub Desktop.
trait DocumentForm[OwnerType <: MongoIdRecord[OwnerType] with ClientId[OwnerType]] extends ObjectIdRefListField[OwnerType, Document] {
self: ObjectIdRefListField[OwnerType, Document] =>
def uploadDoneCallback: JsCmd
override def toForm =
uniqueFieldId match {
case Full(id) => Full(elem(id))
case _ => Full(elem(nextFuncName))
}
def elem(id: String) = {
uploadedDocsForm(id, fph => {
for {user <- User.currentUser
docInfo <- Document.documentService.save(List(owner.clientId.is.toString, user.id.is.toString), fph)} {
FileUploadInProgress.set(true) // This is to notify the form not to trigger a finish, but a refresh
val doc = Document.createInstance(user, docInfo).save
set(doc.id.is :: is)
}
})
}
def uploadedDocsForm(elemId: String, func: FileParamHolder => Any, attrs: ElemAttr*): Elem = {
val f2: FileParamHolder => Any = fp => if (fp.length > 0) func(fp)
S.fmapFunc(BinFuncHolder(f2)) { name =>
val id = nextFuncName
val doneFunc = AnonFunc(uploadDoneCallback)
attrs.foldLeft(
<div id={elemId}>
<span class="btn fileinput-button">
<i class="icon-plus icon-black"></i>
<span>{S ? "document.upload.form.button"}</span>
<input class="jq-fileupload" type="file" name={ name } multiple="multiple"/>
</span>
<br/>
<div id={ "prg_" + id } style="width:50%;" class="progress progress-striped">
<div class="bar"></div>
</div>
<div id={ "files_" + id }>{attachedDocuments()}</div>
<div name="jq-fileupload-script">
{ Script(OnLoad(Run("App.views.common.Upload.initFileupload('#%s .jq-fileupload', '%s', %s)".format(elemId, id, doneFunc.toJsCmd)))) }
</div>
</div>) { _ % _ }
}
}
def markDocumentForRemoval(doc: Document): Unit = doc.markForRemoval(true)
def downloadDocumentUrl(doc: Document): String = {
Document.documentService.downloadPath(doc.guid.is.toString())
}
def doRemoveDocument(doc: Document): Boolean = {
if (Document.documentService.remove(doc.relativeFilePath)) {
set(objs.filterNot(_.guid.is == doc.guid.is).map(_.id.is))
true
} else false
}
def attachedDocuments(isEditable: Boolean = true): NodeSeq = {
def confirmDeleteDocument(elemId: String, doc: Document): JsCmd = {
ConfirmRemoveDialog("Dokument löschen",
s"Möchten Sie das Dokument '${doc.fileName.is}' tatsächlich löschen?",
()=> { markDocumentForRemoval(doc)
Run("$('#%s').fadeOut('fast').remove()".format(elemId))
}, "backdrop" -> false)
}
def asTableRow(doc: Document): Elem = {
val id = nextFuncName
<tr id={id}>
<td>{doc.createdAt.asHtml}</td>
<td><a href={downloadDocumentUrl(doc)} target="garten_docs" >{doc.fileName.is}</a></td>
<td>{doc.userId.obj.map(_.displayName).openOr("")}</td>
{if (isEditable)
<td class="action-td"><a href="Javascript://" onclick={SHtml.ajaxInvoke(()=>{confirmDeleteDocument(id, doc)})._2.toJsCmd}><i class="icon-remove"></i></a></td>
}
</tr>
}
if (objs.filterNot(_.markForRemoval.is).nonEmpty) {
<table class="table table-condensed table-striped">
<thead>
<tr><td>Hinzugefügt</td><td>Name</td><td>Autor</td>{if (isEditable) <td></td>}</tr>
</thead>
<tbody> {objs.filterNot(_.markForRemoval.is).sortBy(_.createdAt.is).map(asTableRow)} </tbody>
</table>
} else NodeSeq.Empty
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment