import org.codehaus.groovy.grails.plugins.web.taglib.FormTagLib
class JTFormTagLib extends FormTagLib {
static namespace = "jttext"
static String javascript = """
"""
/* deprecated: bad name - use script instead */
def changeScript = { attrs ->
out << javascript
}
def script = { attrs ->
out << javascript
}
/**
* Creates a TextArea HTML element, where the maximum number of characters is
* limited by the constraints on the domain object, and tabs can be allowed if
* desired.
*
* Sample use:
*
*
* ...
*
*
*
* The number of remaining characters available are displayed in the status div.
* If allowTabs is set to true, tabs can be entered in the text area.
*/
def textArea = {attrs ->
resolveAttributes(attrs)
attrs.id = attrs.id ? attrs.id : attrs.name
// Pull out values to use as content not attrib
def object = attrs.remove('object')
def maxlength = object.constraints."${attrs.name}".getMaxSize()
def value = fieldValue(bean:object,field:attrs.name)
def escapeHtml = true
if (attrs.escapeHtml) escapeHtml = Boolean.valueOf(attrs.remove('escapeHtml'))
def allowTabs = Boolean.valueOf(attrs.remove('allowTabs'))
def onKeyDown = ""
def onKeyUp = ""
if(maxlength) {
onKeyDown = "checkLength('${attrs.id}',${maxlength});"
onKeyUp = "checkLength('${attrs.id}',${maxlength});"
}
if(allowTabs) {
onKeyDown += "checkTab(event, this);"
}
attrs.put('onkeydown',onKeyDown)
attrs.put('onkeyup',onKeyUp)
out << ""
}
def statusDiv = { attrs ->
out << "
"
}
/** Creates a text input type using maxlength from the objects constraints.
* Sample use:
*
*
* ...
*
*
* Generates:
*
*
* When Book.groovy looks like:
* class Book {
* String title="my new book"
*
* static def constraints = {
* title(maxSize:20)
* }
* }
*/
def textField = {attrs ->
attrs.type = "text"
attrs.tagName = "textField"
def object = attrs.remove('object')
def maxlength = object.constraints."${attrs.name}".getMaxSize()
if(maxlength) {
attrs.put('maxlength', maxlength)
}
attrs.put('value', fieldValue(bean:object,field:attrs.name))
def result = field(attrs)
if (result) {
out << result
}
}
}