Sha256: 1cea7c7816cb2d8af18cb21c8d6e170933263e585312b1527e16d70a153fec72

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

class Form
  constructor: (@page, @$form) ->

  process: ->
    self = this

    if self._include_blank_url_params()
      self.page.load(self._url(), self._target(), self._type())
    else
      self._without_blank_url_params(
        ->
          self.page.load(self._url(), self._target(), self._type())
      )

  _without_blank_url_params: (callback) ->
    selector = 'select:not(:disabled),input:not(:disabled)'
    $disable = @$form.find(selector).filter(-> !$(this).val())

    $disable.attr('disabled', true)
    callback()
    $disable.attr('disabled', false)

  _params: ->
    hash = {}

    for item in @$form.serializeArray()
      if item.name != 'utf8'
        # if name ends with [], then we try to optimize it
        name = if item.name.indexOf('[]', item.name.length - '[]'.length) != -1
          item.name.substr(0, item.name.length - 2)
        else
          item.name

        if hash[name]?
          hash[name] = hash[name] + ",#{item.value}"
        else
          hash[name] = item.value

    hash

  _include_blank_url_params: ->
    @$form.data('include-blank-url-params') == true

  _optimize_url_params: ->
    @$form.data('optimize-url-params') != false

  _type: ->
    if (@$form.data('push') == 'partial') then 'partial' else 'template'

  _target: ->
    @$form.data('target')

  _url: ->
    self = this

    serialized = if self._optimize_url_params()
      params = []

      # To find out why encodeURIComponent should be used, follow the link:
      # http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent
      #
      for key, value of this._params()
        params.push("#{key}=#{encodeURIComponent(value).replace(/%2C/g, ',')}")

      params.join('&')
    else
      @$form.serialize()

    url = @$form.attr("action").replace(/\?.*$/, '')
    url += "?#{serialized}" if serialized.length > 0
    url

window._Wiselinks ?= {}
window._Wiselinks.Form = Form

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
wiselinks-artirix-1.3.0 lib/assets/javascripts/_form.js.coffee
wiselinks-1.2.1 lib/assets/javascripts/_form.js.coffee
wiselinks-1.2.0 lib/assets/javascripts/_form.js.coffee
wiselinks-1.1.4 lib/assets/javascripts/_form.js.coffee