Class: Roda::Component::Events

Inherits:
Struct
  • Object
show all
Defined in:
lib/roda/component/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#component_optsObject

Returns the value of attribute component_opts

Returns:

  • (Object)

    the current value of component_opts



10
11
12
# File 'lib/roda/component/events.rb', line 10

def component_opts
  @component_opts
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



10
11
12
# File 'lib/roda/component/events.rb', line 10

def klass
  @klass
end

#requestObject

Returns the value of attribute request

Returns:

  • (Object)

    the current value of request



10
11
12
# File 'lib/roda/component/events.rb', line 10

def request
  @request
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



10
11
12
# File 'lib/roda/component/events.rb', line 10

def scope
  @scope
end

Instance Method Details

#on(name, options = {}, form_klass = false, extra_opts = false, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/roda/component/events.rb', line 11

def on name, options = {}, form_klass = false, extra_opts = false, &block
  options = '' if options.empty? && (name.to_s == 'history_change' || name.to_s == 'ready')

  if client? && options.is_a?(String)
    class_name   = klass._name
    class_events = (events[class_name] ||= {})
    event        = (class_events[:_jquery_events] ||= [])
    event        << [block, class_name, options, form_klass, extra_opts, name]
  elsif options.is_a?(Hash)
    limit_if = options.delete(:if) || []
    limit_if = [limit_if] unless limit_if.is_a? Array

    class_name   = options.delete(:for) || klass._name
    class_events = (events[class_name] ||= {})
    event        = (class_events[name.to_s] ||= [])

    # remove the type, if we have an on if and it isn't in the engine_type
    if limit_if.any? && !limit_if.include?(engine_type.to_sym)
      block = Proc.new {}
    end

    event << [block, klass._name, options]
  end
end

#trigger(name, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/roda/component/events.rb', line 112

def trigger name, options = {}
  content = ''
  e = events[klass._name]

  return unless e

  (e[name.to_s] || []).each do |event|
    block, comp, opts = event

    if !opts[:socket]
      response = Component::Instance.new(component(comp), scope).instance_exec options, &block

      if response.is_a? Roda::Component::DOM
        content = response.to_xml
      else
        content = response
      end
    else
      $faye.publish("/components/#{klass._name}", {
        name: klass._name,
        type: 'event',
        event_type: 'call',
        event_method: name.to_s,
        data: options
      })
    end
  end

  content
end

#trigger_jquery_eventsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/roda/component/events.rb', line 36

def trigger_jquery_events
  return unless e = events[klass._name]

  vip_list = ['history_change']

  j_events = (e[:_jquery_events] || [])
  j_events = j_events.sort_by do |x|
    [vip_list.index(x.last.to_s) || vip_list.length, j_events.index(x)]
  end

  j_events.each do |event|
    block, comp, selector, form_klass, opts, name = event

    opts = {} unless opts

    name = name.to_s

    case name.to_s
    when 'ready'
      el = Element.find(selector != '' ? selector : 'body')

      Component::Instance.new(component(comp), scope).instance_exec el, &block
    when 'history_change'
      $window.history.change do |he|
        Component::Instance.new(component(comp), scope).instance_exec he, &block
      end
    when 'form'
      warn 'missing form class option' unless form_klass

      el = Element.find(selector)

      el.on :submit do |evt|
        evt.prevent_default

        params = {}

        # loop through all the forum values
        el.serialize_array.each do |row|
          field, _ = row

          # we need to make it native to access it like ruby
          field    = Native(field)
          name     = field['name']
          value    = field['value']

          params[name] = value
        end

        params_obj = {}

        params.each do |param, value|
          keys = param.gsub(/[^a-z0-9_]/, '|').gsub(/\|\|/, '|').gsub(/\|$/, '').split('|')
          params_obj = params_obj.deep_merge keys.reverse.inject(value) { |a, n| { n => a } }
        end

        opts[:dom] = el

        if opts && key = opts[:key]
          form = form_klass.new params_obj[key], opts
        else
          form = form_klass.new params_obj, opts
        end

        el.find(opts[:error_selector] || '.field-error').remove

        Component::Instance.new(component(comp), scope).instance_exec form, evt.current_target, evt, &block
      end
    else
      Document.on name, selector do |evt|
        el = evt.current_target
        Component::Instance.new(component(comp), scope).instance_exec el, evt, &block
      end
    end
  end
end