Sha256: 57cf97502fdce65380f7debccad903d20677727d301e0f7c1645765dab237daf

Contents?: true

Size: 1.9 KB

Versions: 20

Compression:

Stored size: 1.9 KB

Contents

class Tenon.features.RecordListUpdater
  constructor: ->
    $(document).on('click', 'a.record-list-updater', @updateRecordList)
    $(document).on('submit', 'form.record-list-updater', @updateRecordList)
    $(window).on('popstate', @popUpdate)

  updateRecordList: (e) =>
    e.preventDefault()
    $el = $(e.currentTarget)
    @clearQuery = $el.attr('data-clear-record-list-params')
    if $el.prop("tagName").toLowerCase() == 'form'
      @_updateWithQuery(@_processFormData($el), 'Search')
    else
      @_updateWithQuery(URI($el.attr('href')).query(true), $el.textContent)

  _processFormData: (form) =>
    formData = URI("?" + form.serialize()).query(true)
    form.find("input:checkbox").each ->
      if !(@.checked)
        formData[@.name] = "0"
    formData

  _updateWithQuery: (query, title) =>
    query = if @clearQuery then query else @_mergedQuery(query)
    state = if $.isEmptyObject(query) then "?" else "?#{query.toString()}"
    history.pushState({recordListUpdate: true}, title, state)
    Tenon.refreshed = false
    Tenon.activeRecordList.refresh(clear: true)

  # Get a combination of the current params and any new params that
  # the link or form is adding.  This allows us to, for example, submit
  # an advanced search form and then change the sort order with a link.
  # Add data-clear-record-list-params to prevent this merge.
  _mergedQuery: (newQueryObj) =>
    oldQueryObj = URI(location.href).query(true)
    $.param($.extend(oldQueryObj, newQueryObj))

  popUpdate: (e) =>
    # Chrome triggers window.onpopstate on page load, so elsewhere
    # at page load we set Tenon.refreshed to true to indicate that
    # as of yet nothing has been pushed to the history stack.
    #
    # Failing to do this results in a double AJAX load when the
    # page is first loaded in Chrome, and possible other browsers.
    if !Tenon.refreshed && Tenon.activeRecordList
      Tenon.activeRecordList.refresh(clear: true)

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
tenon-1.0.19 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.18 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.17 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.16 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.15 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.14 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.13 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.12 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.11 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.10 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.9 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.8 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.7 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.6 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.5 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.4 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.3 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.2 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0.1 app/assets/javascripts/tenon/features/record_list_updater.js.coffee
tenon-1.0 app/assets/javascripts/tenon/features/record_list_updater.js.coffee