Class: Wedge::Component

Inherits:
Object show all
Includes:
Methods
Defined in:
lib/wedge/component.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/wedge/component.rb', line 343

def method_missing(method, *args, &block)
  if config.scope.respond_to?(method, true)
    config.scope.send method, *args, &block
  else
    super
  end
end

Class Attribute Details

.wedge_on_countObject

Returns the value of attribute wedge_on_count.



8
9
10
# File 'lib/wedge/component.rb', line 8

def wedge_on_count
  @wedge_on_count
end

Class Method Details

.configObject



118
119
120
# File 'lib/wedge/component.rb', line 118

def wedge_config
  @wedge_config ||= Config.new Wedge.config.data.dup.merge(klass: self)
end

.domObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/wedge/component.rb', line 113

def wedge_dom &block

  unless RUBY_ENGINE == 'opal'
    if block_given?
      yield
    end
  end

  @wedge_dom ||= DOM.new wedge_config.html
end

.htmlObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wedge/component.rb', line 79

def wedge_html(html = '', &block)
  unless RUBY_ENGINE == 'opal'
    wedge_config.html = begin
      File.read html
    rescue
      (html.is_a?(HTML::DSL) || html.is_a?(DOM)) ? html.to_html : html
    end.strip

    if block_given?
      yield
    end
  end
end

.html!(&b) ⇒ Object



214
215
216
217
218
# File 'lib/wedge/component.rb', line 214

def html!(&b)
  unless RUBY_ENGINE == 'opal'
    Wedge.html!(self, &b)
  end
end

.method_missing(method, *args, &block) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/wedge/component.rb', line 133

def method_missing(method, *args, &block)
  if wedge_config.scope.respond_to?(method, true)
    wedge_config.scope.send method, *args, &block
  else
    super
  end
end

.set_dom(dom) ⇒ Object



210
211
212
# File 'lib/wedge/component.rb', line 210

def set_dom dom
  @wedge_dom = dom.is_a?(Wedge::DOM) ? dom : Wedge::DOM.new(dom)
end

.storeObject



220
221
222
# File 'lib/wedge/component.rb', line 220

def store
  wedge_config.store
end

.tmplObject

Set templates

Examples:

tmpl :some_name, dom.find('#some-div')


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wedge/component.rb', line 101

def wedge_tmpl(name, dom = false, remove = true)
  if dom
    dom = remove ? dom.remove : dom
    wedge_config.tmpl[name] = {
      dom:  dom,
      html: dom.to_html
    }
  elsif t = wedge_config.tmpl[name]
    dom = DOM.new t[:html]
  else
    false
  end

  dom
end

.wedge_configObject



115
116
117
# File 'lib/wedge/component.rb', line 115

def wedge_config
  @wedge_config ||= Config.new Wedge.config.data.dup.merge(klass: self)
end

.wedge_dom(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/wedge/component.rb', line 103

def wedge_dom &block

  unless RUBY_ENGINE == 'opal'
    if block_given?
      yield
    end
  end

  @wedge_dom ||= DOM.new wedge_config.html
end

.wedge_html(html = '', &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wedge/component.rb', line 66

def wedge_html(html = '', &block)
  unless RUBY_ENGINE == 'opal'
    wedge_config.html = begin
      File.read html
    rescue
      (html.is_a?(HTML::DSL) || html.is_a?(DOM)) ? html.to_html : html
    end.strip

    if block_given?
      yield
    end
  end
end

.wedge_name(*args) ⇒ Object Also known as: name



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
# File 'lib/wedge/component.rb', line 36

def wedge_name(*args)
  if args.any?
    unless RUBY_ENGINE == 'opal'
      # set the file path
      path = "#{caller[0]}".gsub(/(?<=\.rb):.*/, '')
        .gsub(%r{(#{Dir.pwd}/|.*(?=wedge))}, '')
        .gsub(/\.rb$/, '')
    end

    @wedge_on_count = 0

    args.each do |name|
      # set the name
      wedge_config.name = name

      unless RUBY_ENGINE == 'opal'
        # set the file path
        wedge_config.path = path
        # add it to the component class list allow path or name
        Wedge.config.component_class[path.gsub(/\//, '__')] = self
      end

      Wedge.config.component_class[name] = self
    end
  else
    original_name
  end
end

.wedge_new(klass, *args, &block) ⇒ Object



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

def wedge_new(klass, *args, &block)
  obj = allocate

  %w(store scope).each do |meth|
    if value = klass.send(meth)
      obj.config.send "#{meth}=", value
    end
  end

  unless RUBY_ENGINE == 'opal'
    obj.config.on_compile.each do |blk|
      obj.instance_exec &blk
    end
  end

  if args.length > 0
    obj.config.initialize_args = args
    obj.send :initialize, *args, &block
  else
    obj.send :initialize, &block
  end

  obj
end

.wedge_on(*args, &block) ⇒ Object Also known as: on



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/wedge/component.rb', line 120

def wedge_on(*args, &block)
  case args.first.to_s
  when 'server'
    wedge_on_server(&block)
  when 'compile'
    wedge_config.on_compile << block unless RUBY_ENGINE == 'opal'
  else
    @wedge_on_count += 1
    Wedge.events.add config.name, *args, &block
  end
end

.wedge_on_server(&block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/wedge/component.rb', line 141

def wedge_on_server(&block)
  if server?
    m = Module.new(&block)

    yield

    m.public_instance_methods(false).each do |meth|
      config.server_methods << meth.to_s

      alias_method :"wedge_on_server_#{meth}", :"#{meth}"
      define_method "#{meth}" do |*args, &blk|
        o_name = "wedge_on_server_#{meth}"

        if method(o_name).parameters.length > 0
          result = send(o_name, *args, &block)
        else
          result = send(o_name, &block)
        end

        blk ? blk.call(result) : result
      end
    end
  else
    m = Module.new(&block)

    m.public_instance_methods(false).each do |meth|
      config.server_methods << meth.to_s

      define_method "#{meth}" do |*args, &blk|
        path_name = config.path

        payload = config.client_data.reject do |k, _|
          %w(html tmpl requires plugins object_events js_loaded).include? k
        end
        payload[:__wedge_name__]   = payload[:name]
        payload[:__wedge_method__] = meth
        payload[:__wedge_args__]   = args

        # we want to remove the assets key from the call so we don't get
        # an error if they assets_key has changed and the user hasn't
        # refreshed the browser yet.
        call_url = "#{Wedge.assets_url.sub("#{Wedge.config.assets_key}/",'')}/#{path_name}.call"

        HTTP.post(call_url,
          headers: {
            'X-CSRF-TOKEN' => Element.find('meta[name=_csrf]').attr('content'),
            'X-WEDGE-METHOD-REQUEST' => true
          },
          payload: payload) do |response|

            # We set the new csrf token
            xhr  = Native(response.xhr)
            csrf = xhr.getResponseHeader('WEDGE-CSRF-TOKEN')
            Element.find('meta[name=_csrf]').attr 'content', csrf
            ###########################

            res = JSON.from_object(`response`)

            blk.call res[:body], res
        end

        true
      end
    end

    include m
  end
end

.wedge_tmpl(name, dom = false, remove = true) ⇒ Object

Set templates

Examples:

tmpl :some_name, dom.find('#some-div')


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wedge/component.rb', line 86

def wedge_tmpl(name, dom = false, remove = true)
  if dom
    dom = remove ? dom.remove : dom
    wedge_config.tmpl[name] = {
      dom:  dom,
      html: dom.to_html
    }
  elsif t = wedge_config.tmpl[name]
    dom = DOM.new t[:html]
  else
    false
  end

  dom
end

Instance Method Details

#to_js(method = false, *args) ⇒ Object



331
332
333
334
335
336
# File 'lib/wedge/component.rb', line 331

def to_js(method = false, *args)
  response = args.any? ? send(method, *args) : send(method)
  response = response.to_html if response.is_a? DOM
  response << wedge_javascript(method, *args) if response.is_a? String
  response
end

#wedge(*args) ⇒ Object



226
227
228
# File 'lib/wedge/component.rb', line 226

def wedge(*args)
  Wedge[*args]
end

#wedge_configObject Also known as: config

Duplicate of class condig [Config]



247
248
249
# File 'lib/wedge/component.rb', line 247

def wedge_config
  @wedge_config ||= Config.new(self.class.wedge_config.data.dup)
end

#wedge_domObject Also known as: dom

Dom



261
262
263
264
265
266
267
268
269
# File 'lib/wedge/component.rb', line 261

def wedge_dom
  @wedge_dom ||= begin
    if server?
      DOM.new self.class.wedge_dom.to_html
    else
      DOM.new(Element)
    end
  end
end

#wedge_from_client?Boolean Also known as: from_client?

Returns:

  • (Boolean)


303
304
305
# File 'lib/wedge/component.rb', line 303

def wedge_from_client?
  !wedge_from_server?
end

#wedge_from_server?Boolean Also known as: from_server?

Returns:

  • (Boolean)


298
299
300
# File 'lib/wedge/component.rb', line 298

def wedge_from_server?
  !scope.respond_to?(:request) || (request && !request.env.include?('HTTP_X_WEDGE_METHOD_REQUEST'))
end

#wedge_function(*args, &block) ⇒ Object Also known as: function

Special method that acts like the javascript equivalent

Examples:

foo = {
  bar: function { |moo|
    moo.call 'something'
  }
}.to_n


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/wedge/component.rb', line 279

def wedge_function(*args, &block)
  args.any? && raise(ArgumentError, '`function` does not accept arguments')
  block || raise(ArgumentError, 'block required')
  proc do |*a|
    a.map! {|x| Native(`x`)}
    @this = Native(`this`)
    %x{
     var bs = block.$$s,
        result;
      block.$$s = null;
      result = block.apply(self, a);
      block.$$s = bs;

      return result;
    }
  end
end

#wedge_html(&b) ⇒ Object Also known as: html!



338
339
340
# File 'lib/wedge/component.rb', line 338

def wedge_html(&b)
  Wedge.html!(self, &b)
end

#wedge_javascript(method = false, *args) ⇒ Object Also known as: javscript



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/wedge/component.rb', line 308

def wedge_javascript(method = false, *args)
  return unless server?

  client_data = config.client_data.dup
  client_data.merge!(
    method_called: method,
    method_args: args,
    initialize_args: config.initialize_args
  )

  compiled_opts = Base64.encode64 client_data.to_json
  javascript = <<-JS
    Wedge.javascript('#{config.path}', JSON.parse(Base64.decode64('#{compiled_opts}')))
  JS
  "<script>#{Opal.compile(javascript)}</script>"
end

#wedge_plugin(name, *args, &block) ⇒ Object



230
231
232
# File 'lib/wedge/component.rb', line 230

def wedge_plugin(name, *args, &block)
  wedge("#{name}_plugin", *args, &block)
end

#wedge_scopeObject Also known as: scope



235
236
237
# File 'lib/wedge/component.rb', line 235

def wedge_scope
  wedge_config.scope
end

#wedge_storeObject Also known as: store



240
241
242
# File 'lib/wedge/component.rb', line 240

def wedge_store
  wedge_config.store
end

#wedge_tmpl(name) ⇒ Object Also known as: tmpl

Grab a copy of the template



254
255
256
# File 'lib/wedge/component.rb', line 254

def wedge_tmpl(name)
  self.class.wedge_tmpl name
end

#wedge_trigger(event_name, *args) ⇒ Object Also known as: trigger



326
327
328
# File 'lib/wedge/component.rb', line 326

def wedge_trigger(event_name, *args)
  Wedge.events.trigger config.name, event_name, *args
end