<commit_before>/*
 * Copyright (C) 2013-2014 by <NAME> Brinkmann
 */

package twibs.form.base

import scala.collection.mutable.ListBuffer
import scala.xml.{Text, Elem, NodeSeq}
import twibs.util.JavaScript.JsCmd
import twibs.util.Message
import twibs.util.XmlUtils._
import twibs.web.Response

trait Result {
  var result: Result.Value = Result.Ignored

  def AfterFormDisplay(js: JsCmd) = Result.AfterFormDisplay(js)

  def BeforeFormDisplay(js: JsCmd) = Result.AfterFormDisplay(js)

  def InsteadOfFormDisplay(js: JsCmd) = Result.InsteadOfFormDisplay(js)
}

object Result {

  trait Value

  case object Ignored extends Value

  case class AfterFormDisplay(js: JsCmd) extends Value

  case class BeforeFormDisplay(js: JsCmd) extends Value

  case class InsteadOfFormDisplay(js: JsCmd) extends Value

  case class UseResponse(response: Response) extends Value

}

trait Validatable {
  def isValid: Boolean
}

trait Rendered {
  def html: NodeSeq

  def enrichedHtml: NodeSeq = html
}

trait RenderedItem extends BaseItem with Rendered {
  final override def enrichedHtml: NodeSeq = itemIsVisible match {
    case false => NodeSeq.Empty
    case true => isRevealed match {
      case true => html
      case false =>
        html match {
          case s@NodeSeq.Empty => s
          case n: Elem => n.addClass(itemIsConcealed, "concealed")
          case n: NodeSeq => <div>{n}</div>.addClass(itemIsConcealed, "concealed")
        }
    }
  }
}

class DisplayMessage(condition: => Boolean, message: => Message)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(message: => Message)(implicit parent: BaseParentItem) = this(true, message)(parent)

  override def itemIsVisible: Boolean = condition

  def html = parent.form.renderer.renderMessage(message)
}

class DisplayHtml(condition: => Boolean, gethtml: => NodeSeq)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(gethtml: => NodeSeq)(implicit parent: BaseParentItem) = this(true, gethtml)(parent)

  override def itemIsVisible: Boolean = condition

  def html = gethtml
}

class DisplayText(condition: => Boolean, gettext: => String)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(gettext: => String)(implicit parent: BaseParentItem) = this(true, gettext)(parent)

  override def itemIsVisible: Boolean = condition

  override def html = Text(gettext)
}

class Messages()(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  private val _messages = ListBuffer[Message]()

  def messages = _messages.toList

  def append(message: Message): Unit = _messages += message

  override def reset(): Unit = {
    super.reset()
    _messages.clear()
  }

  override def html = <div>{messages.map(parent.form.renderer.renderMessage)}</div>
}

abstract class HiddenInput(val ilk: String)(implicit val parent: BaseParentItem) extends BaseField with RenderedItem {
  override def html = inputs.map(input => HiddenInputRenderer(name, input.string))

  // TODO: Check that this works
  def executionLink(value: ValueType) = "" //bootstrapForm.actionLinkWithContextPath + "?" + parameterNameForExecution + "=" + valueToStringConverter(value)
}

trait Renderer {
  def renderMessage(message: Message): NodeSeq
}
<commit_msg>- added InitLink and ActionButton to Form.<commit_after>/*
 * Copyright (C) 2013-2014 by <NAME>
 */

package twibs.form.base

import scala.collection.mutable.ListBuffer
import scala.xml.{Text, Elem, NodeSeq}
import twibs.util.JavaScript.JsCmd
import twibs.util.Message
import twibs.util.XmlUtils._
import twibs.web.{Request, Response}

trait Result {
  var result: Result.Value = Result.Ignored

  def AfterFormDisplay(js: JsCmd) = Result.AfterFormDisplay(js)

  def BeforeFormDisplay(js: JsCmd) = Result.AfterFormDisplay(js)

  def InsteadOfFormDisplay(js: JsCmd) = Result.InsteadOfFormDisplay(js)
}

object Result {

  trait Value

  case object Ignored extends Value

  case class AfterFormDisplay(js: JsCmd) extends Value

  case class BeforeFormDisplay(js: JsCmd) extends Value

  case class InsteadOfFormDisplay(js: JsCmd) extends Value

  case class UseResponse(response: Response) extends Value

}

trait Validatable {
  def isValid: Boolean
}

trait Rendered {
  def html: NodeSeq

  def enrichedHtml: NodeSeq = html
}

trait RenderedItem extends BaseItem with Rendered {
  final override def enrichedHtml: NodeSeq = itemIsVisible match {
    case false => NodeSeq.Empty
    case true => isRevealed match {
      case true => html
      case false =>
        html match {
          case s@NodeSeq.Empty => s
          case n: Elem => n.addClass(itemIsConcealed, "concealed")
          case n: NodeSeq => <div>{n}</div>.addClass(itemIsConcealed, "concealed")
        }
    }
  }
}

class DisplayMessage(condition: => Boolean, message: => Message)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(message: => Message)(implicit parent: BaseParentItem) = this(true, message)(parent)

  override def itemIsVisible: Boolean = condition

  def html = parent.form.renderer.renderMessage(message)
}

class DisplayHtml(condition: => Boolean, gethtml: => NodeSeq)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(gethtml: => NodeSeq)(implicit parent: BaseParentItem) = this(true, gethtml)(parent)

  override def itemIsVisible: Boolean = condition

  def html = gethtml
}

class DisplayText(condition: => Boolean, gettext: => String)(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  def this(gettext: => String)(implicit parent: BaseParentItem) = this(true, gettext)(parent)

  override def itemIsVisible: Boolean = condition

  override def html = Text(gettext)
}

class Messages()(implicit val parent: BaseParentItem) extends BaseChildItem with RenderedItem {
  private val _messages = ListBuffer[Message]()

  def messages = _messages.toList

  def append(message: Message): Unit = _messages += message

  override def reset(): Unit = {
    super.reset()
    _messages.clear()
  }

  override def html = <div>{messages.map(parent.form.renderer.renderMessage)}</div>
}

abstract class HiddenInput(val ilk: String)(implicit val parent: BaseParentItem) extends BaseField with RenderedItem {
  override def html = inputs.map(input => HiddenInputRenderer(name, input.string))

  def executionLink(value: ValueType) = parent.form.actionLinkWithContextPath + "?" + name + "=" + valueToStringConverter(value)
}

abstract class InitInput()(implicit val parent: BaseParentItem) extends BaseField {
  def ilk: String = "init"

  def link(value: ValueType): String = parent.form.actionLinkWithContextPath + "?" + name + "=" + valueToStringConverter(value)

  override def execute(request: Request): Unit = valueOption.map(initWithVar)

  def <<(initWith: (ValueType) => Unit) : (ValueType) => String = {
    initWithVar = initWith
    link
  }

  private var initWithVar:(ValueType) => Unit = (ValueType) => Unit
}

abstract class ActionButton(val ilk: String)(implicit val parent: BaseParentItem) extends BaseField {
  def link(value: ValueType): String = parent.form.actionLinkWithContextPath + "?" + name + "=" + valueToStringConverter(value)

  override def execute(request: Request): Unit = valueOption.map(execute)

  def execute(value: ValueType): Unit
}

trait Renderer {
  def renderMessage(message: Message): NodeSeq
}

{'fraction_non_alphanumeric': 0.10150375939849623, 'fraction_numerical': 0.0021482277121374865, 'mean_word_length': 4.6819221967963385, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4623160', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2168, 'n_words': 622}