collection_button_click = (target, options = {}) ->
  return if target.data('confirmation') and not confirm(target.data('confirmation'))
  action_name = target.data('action')
  [resource, scenario] = full_resource.split('/') if full_resource = target.data('resource')
  if not full_resource and target.attr('href') # direct collection_edit links
    url = "#{target.attr('href')}&reveal=yes"
  else
    request_params = options.request_params || target.data('requestParams')
    return unless request_params
    # important to make a copy of options.request_params, otherwize the ids will be persisted in there
    request_params = $.extend({}, request_params)

    # See if multiselect ids is requested
    for k, v of request_params
      if v is 'selected_ids' # Implement multi select by inserting ids here
        selected_ids = Dunlop.selected_ids()
        return alert("Nothing selected") unless selected_ids.length
        request_params[k] = selected_ids.join('~')
        break

    url_path ="collection_edit_#{resource}_#{scenario.pluralize()}_path"
    request_params['reveal'] = 'yes'
    return alert("The route #{url_path} does not exist") unless Routes[url_path]
    url = Routes[url_path](request_params)
  Dunlop.action_in_progress = 1
  $.get url, (result) ->
    $('#workflow-step-modal .content').html result
    $('#workflow-step-modal').foundation('reveal', 'open')
    Dunlop.setup $('#workflow-step-modal'), ->
      $('#workflow-step-modal .workflow-step-back-button').click (e) ->
        e.preventDefault()
        $('#workflow-step-modal').foundation('reveal', 'close')
        false
  false

Dunlop.register_setup 'collection-button', (target) ->
  # setup collection edit buttons
  target.find('.collection-edit').click (evt) ->
    evt.preventDefault()
    collection_button_click $(this)
    false

