Sha256: a6a8fef5b90469a522f0b4fe98a36e271c3e16351ea78d6e8d69fa953c863894

Contents?: true

Size: 1.97 KB

Versions: 9

Compression:

Stored size: 1.97 KB

Contents

(($) ->
  class @EventRules
    constructor: (@templates = {}) ->

    add_fields: (button, content) ->
      new_id = new Date().getTime()
      regexp = new RegExp('new_event_rule', 'g')
      $('ul#event_rules').append(content.replace(regexp, new_id))
      # Setup event autocomplete for new field
      this.cloudfuji_event_autocomplete("input#event_rules_#{new_id}_cloudfuji_event")

    remove_fields: (button) ->
      container = $(button).closest('li.event_rule')
      index = container.data('index')
      # If rule has no id, just remove
      if $("#event_rules_#{index}_id").length == 0
        container.remove()
      else
        # If rule has an id, mark container as hidden and add _destroy field
        container.hide()
        container.append($("<input type='text' name='event_rules[#{index}][_destroy]' value='yes'>"))

    cloudfuji_event_autocomplete: (selector = 'input.cloudfuji_event') ->
      $(selector).autocomplete({source: observed_cloudfuji_events, minLength: 0})
      # Show all events on focus, if input is empty
      $(selector).focus ->
        $(this).autocomplete "search", "" if $(this).val() == ""

    show_field_group: (select, group_name, key) ->
      # Hide all fields, then show the selected field group
      container = $(select).closest('li.event_rule')
      container.find(".#{group_name}_fields").hide()
      container.find(".#{group_name}_#{key}").show()

  $(document).ready ->
    event_rules = new EventRules()
    # Initialize autocomplete for events
    event_rules.cloudfuji_event_autocomplete()

    $("button.add_event_rule").live "click", ->
      event_rules.add_fields this, $(this).data("content")
      false

    $(".remove_event_rule").live "click", ->
      event_rules.remove_fields this
      false

    $('.event_category_select').live "change", ->
      event_rules.show_field_group this, 'event_category', $(this).val()

    $('.action_select').live "change", ->
      event_rules.show_field_group this, 'action', $(this).val()


) jQuery

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ffcrm_cloudfuji-0.2.11 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.10 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.9 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.8 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.7 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.6 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.5 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.4 app/assets/javascripts/event_rules.js.coffee
ffcrm_cloudfuji-0.2.3 app/assets/javascripts/event_rules.js.coffee