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



3
4
5
# File 'lib/roda/component/events.rb', line 3

def component_opts
  @component_opts
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



3
4
5
# File 'lib/roda/component/events.rb', line 3

def klass
  @klass
end

#requestObject

Returns the value of attribute request

Returns:

  • (Object)

    the current value of request



3
4
5
# File 'lib/roda/component/events.rb', line 3

def request
  @request
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



3
4
5
# File 'lib/roda/component/events.rb', line 3

def scope
  @scope
end

Instance Method Details

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



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/roda/component/events.rb', line 4

def on name, options = false, form_klass = false, extra_opts = false, &block

  if client? && (options.is_a?(String) || !options)
    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) || !options
    options = {} unless options

    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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/roda/component/events.rb', line 121

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_html
      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
      }) if $faye
    end
  end

  content
end

#trigger_jquery_eventsObject



30
31
32
33
34
35
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
111
112
113
114
115
116
117
118
119
# File 'lib/roda/component/events.rb', line 30

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

      Document.on :submit, selector do |evt|
        el = evt.current_target
        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']

          # Check to see if this is a multiple value form (form name
          # should end in [])
          if name =~ /\[\]$/
            name = name.gsub(/\[\]$/,'')
            params[name] = [] if params[name].nil?
            params[name].push value
          else
            params[name] = value
          end
        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
      args = [name]
      args << selector if selector

      Document.on(*args) do |evt|
        el = evt.current_target
        Component::Instance.new(component(comp), scope).instance_exec el, evt, &block
      end

      if name =~ /ready/
        el = Element.find(selector != '' ? selector : 'body')
        Component::Instance.new(component(comp), scope).instance_exec el, &block
      end
    end
  end
end