<commit_before>Handlebars = require 'hbsfy/runtime'
_ = require 'underscore'

createLabel = (text, name, className="control-label") ->
  tag 'label',
    for: "input-#{name}"
    className: className
  , text

wrap = (content, options={}) ->
  _.defaults options,
    label: null
    help: null
    className: 'form-group'
  content = createLabel(options.label) + content if options.label
  content += tag 'p', {className: 'help-block'}, options.help if options.help
  tag 'div', class: options.className, content

tag = (el, attrs={}, content='', options={}) ->
  html = "<#{el}"
  for key, val of attrs
    key = 'class' if key is 'className'
    html += " #{key}=\"#{val}\"" if val? and val isnt ''
  html += ">"
  html += content if content
  html += "</#{el}>" unless options.selfClosing

  new Handlebars.SafeString html

Handlebars.registerHelper 'input', (name, value, options) ->
  settings = _.defaults options.hash,
    className: 'form-control'
    type: 'text'

  params =
    name: name
    value: value
    className: settings.className
    id: settings.id
    placeholder: settings.placeholder
    tabindex: 0
    type: settings.type
    autocomplete: 'off'

  params.className += ' has-slug'

  if settings.slugName
    _.extend params,
      'data-sluggify': settings.slugName

  input = tag('input', params, false, selfClosing: true)

  if settings.slugName
    slug = tag('input', {
      className: 'form-control input-slug input-sm'
      name: settings.slugName
      value: settings.slugValue
      placeholder: 'slug'
      tabindex: '-1'
    })
    input += slug

  wrap input,
    label: settings.label
    help: settings.help

Handlebars.registerHelper 'textarea', (name, value, options) ->
  settings = options.hash

  textarea = tag 'textarea',
    name: name
    className: settings.className || 'form-control'
    id: "input-#{name}"
    placeholder: settings.placeholder
    tabindex: settings.tabindex
    rows: settings.rows
  , value

  if settings.label
    wrap textarea, label: settings.label, help: settings.help
  else
    textarea

Handlebars.registerHelper 'submit', (text, options) ->

  settings = _.defaults options.hash,
    className: 'btn btn-primary ladda-button'

  tag 'button',
    className: settings.className
    'data-style': 'zoom-in'
    type: 'submit'
  , text

Handlebars.registerHelper 'hidden', (name, value) ->
  tag 'input',
    type: 'hidden'
    name: name
    value: value

Handlebars.registerHelper 'checkbox', (name, value, options) ->
  label = options.hash.label

  params =
    type: 'checkbox'
    name: name
    value: 1

  params.checked = 'checked' if value

  cb = tag 'input', params
  cb = tag 'label', {}, cb + " #{label}" if label
  wrap cb,
    help: options.hash.help
    className: 'checkbox'

Handlebars.registerHelper 'select', (name, value, selectOptions, options) ->

  return unless selectOptions?.length > 0

  settings = _.defaults options.hash,
    className: 'form-control'
    valueKey: '_id'
    nameKey: 'name'

  settings.selected = 'selected' if value

  optionEls = []
  for opt in selectOptions
    optionEls.push tag 'option',
      value: opt[settings.valueKey]
      selected: if opt[settings.valueKey] is value
        "selected"
      else
        ""
    , opt[settings.nameKey]<commit_msg>Small form improvements<commit_after>Handlebars = require 'hbsfy/runtime'
_ = require 'underscore'

createLabel = (text, name, className="control-label") ->
  tag 'label',
    for: "input-#{name}"
    className: className
  , text

wrap = (content, options={}) ->
  _.defaults options,
    label: null
    help: null
    className: 'form-group'
  content = tag('p', {className: 'help-block'}, options.help) + content if options.help
  content = createLabel(options.label) + content if options.label
  tag 'div', class: options.className, content

tag = (el, attrs={}, content='', options={}) ->
  html = "<#{el}"
  for key, val of attrs
    key = 'class' if key is 'className'
    html += " #{key}=\"#{val}\"" if val? and val isnt ''
  html += ">"
  html += content if content
  html += "</#{el}>" unless options.selfClosing

  new Handlebars.SafeString html

Handlebars.registerHelper 'input', (name, value, options) ->
  settings = _.defaults options.hash,
    className: 'form-control'
    type: 'text'

  params =
    name: name
    value: value
    className: settings.className
    id: settings.id
    placeholder: settings.placeholder
    tabindex: 1
    type: settings.type
    autocomplete: 'off'

  params.className += ' has-slug'

  if settings.slugName
    _.extend params,
      'data-sluggify': settings.slugName

  input = tag('input', params, false, selfClosing: true)

  if settings.slugName
    slug = tag('input', {
      className: 'form-control input-slug input-sm'
      name: settings.slugName
      value: settings.slugValue
      placeholder: 'slug'
      tabindex: 0
    })
    input += slug

  wrap input,
    label: settings.label
    help: settings.help

Handlebars.registerHelper 'textarea', (name, value, options) ->

  settings = _.extend options.hash,
    tabindex: 1
    className: 'form-control'

  textarea = tag 'textarea',
    name: name
    className: settings.className
    id: "input-#{name}"
    placeholder: settings.placeholder
    tabindex: settings.tabindex
    rows: settings.rows
  , value

  if settings.label
    wrap textarea, label: settings.label, help: settings.help
  else
    textarea

Handlebars.registerHelper 'submit', (text, options) ->

  settings = _.defaults options.hash,
    className: 'btn btn-primary ladda-button'
    tabindex: 1

  tag 'button',
    className: settings.className
    'data-style': 'zoom-in'
    type: 'submit'
    tabindex: settings.tabindex
  , text

Handlebars.registerHelper 'hidden', (name, value) ->
  tag 'input',
    type: 'hidden'
    name: name
    value: value

Handlebars.registerHelper 'checkbox', (name, value, options) ->
  label = options.hash.label

  params =
    type: 'checkbox'
    name: name
    value: 1

  params.checked = 'checked' if value

  cb = tag 'input', params
  cb = tag 'label', {}, cb + " #{label}" if label
  wrap cb,
    help: options.hash.help
    className: 'checkbox'

Handlebars.registerHelper 'select', (name, value, selectOptions, options) ->

  return unless selectOptions?.length > 0

  settings = _.defaults options.hash,
    className: 'form-control'
    valueKey: '_id'
    nameKey: 'name'
    tabIndex: 1

  settings.selected = 'selected' if value

  optionEls = []
  for opt in selectOptions
    optionEls.push tag 'option',
      value: opt[settings.valueKey]
      selected: if opt[settings.valueKey] is value
        "selected"
      else
        ""
    , opt[settings.nameKey]
{'fraction_non_alphanumeric': 0.12976758045292014, 'fraction_numerical': 0.0016388557806912992, 'mean_word_length': 3.1108389467238213, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4313056', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2051, 'n_words': 593}