Class: Wedge::Component
- Includes:
- Methods
- Defined in:
- lib/wedge/component.rb
Direct Known Subclasses
Plugins::Form, Plugins::History, Plugins::Pjax, Plugins::Popover, Plugins::Uploader
Class Attribute Summary collapse
-
.wedge_on_count ⇒ Object
Returns the value of attribute wedge_on_count.
Instance Attribute Summary collapse
-
#wedge_method_called ⇒ Object
(also: #method_called)
Returns the value of attribute wedge_method_called.
Class Method Summary collapse
- .config ⇒ Object
- .dom ⇒ Object
- .html ⇒ Object
- .html!(&b) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .plugin(*args) ⇒ Object
- .set_dom(dom) ⇒ Object
- .store ⇒ Object
-
.tmpl ⇒ Object
Set templates.
- .wedge_config ⇒ Object
- .wedge_dom(&block) ⇒ Object
- .wedge_html(html = '', &block) ⇒ Object
- .wedge_name(*args) ⇒ Object (also: name)
- .wedge_new(klass, *args, &block) ⇒ Object
- .wedge_on(*args, &block) ⇒ Object (also: on)
- .wedge_on_server(m = false, &block) ⇒ Object
-
.wedge_tmpl(name, dom = false, remove = true) ⇒ Object
Set templates.
Instance Method Summary collapse
- #method_missing(method, *args, &block) ⇒ Object
- #to_js(method = false, *args) ⇒ Object
-
#wedge(*args, &block) ⇒ Object
We want the scope to override this method if defined.
- #wedge_class_store ⇒ Object (also: #class_store)
-
#wedge_config ⇒ Object
(also: #config)
Duplicate of class condig [Config].
-
#wedge_dom ⇒ Object
(also: #dom)
Dom.
- #wedge_from_client? ⇒ Boolean (also: #from_client?)
- #wedge_from_server? ⇒ Boolean (also: #from_server?)
-
#wedge_function(*args, &block) ⇒ Object
(also: #function)
Special method that acts like the javascript equivalent.
- #wedge_html(&b) ⇒ Object (also: #html!)
- #wedge_javascript(method = false, *args) ⇒ Object (also: #javscript)
- #wedge_method_url(method, *args) ⇒ Object (also: #method_url)
-
#wedge_plugin(name, *args, &block) ⇒ Object
We want the scope to override this method if defined.
- #wedge_scope ⇒ Object (also: #scope)
- #wedge_store ⇒ Object (also: #store)
-
#wedge_tmpl(name) ⇒ Object
(also: #tmpl)
Grab a copy of the template.
- #wedge_trigger(event_name, *args) ⇒ Object (also: #trigger)
Methods included from Methods
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
405 406 407 408 409 410 411 |
# File 'lib/wedge/component.rb', line 405 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_count ⇒ Object
Returns the value of attribute wedge_on_count.
10 11 12 |
# File 'lib/wedge/component.rb', line 10 def wedge_on_count @wedge_on_count end |
Instance Attribute Details
#wedge_method_called ⇒ Object Also known as: method_called
Returns the value of attribute wedge_method_called.
7 8 9 |
# File 'lib/wedge/component.rb', line 7 def wedge_method_called @wedge_method_called end |
Class Method Details
.config ⇒ Object
127 128 129 |
# File 'lib/wedge/component.rb', line 127 def wedge_config @wedge_config ||= Config.new klass: self, scope: Wedge.config.scope, plugins: Wedge.config.plugins end |
.dom ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/wedge/component.rb', line 122 def wedge_dom &block unless RUBY_ENGINE == 'opal' if block_given? yield end end @wedge_dom ||= DOM.new wedge_config.html end |
.html ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/wedge/component.rb', line 88 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
246 247 248 |
# File 'lib/wedge/component.rb', line 246 def html!(&b) Wedge.html!(self, &b) end |
.method_missing(method, *args, &block) ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/wedge/component.rb', line 142 def method_missing(method, *args, &block) if wedge_config.scope.respond_to?(method, true) wedge_config.scope.send method, *args, &block else puts method super end end |
.plugin(*args) ⇒ Object
40 41 42 |
# File 'lib/wedge/component.rb', line 40 def plugin *args Wedge.plugin *args end |
.set_dom(dom) ⇒ Object
242 243 244 |
# File 'lib/wedge/component.rb', line 242 def set_dom dom @wedge_dom = dom.is_a?(Wedge::DOM) ? dom : Wedge::DOM.new(dom) end |
.store ⇒ Object
250 251 252 |
# File 'lib/wedge/component.rb', line 250 def store wedge_config.store end |
.tmpl ⇒ Object
Set templates
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/wedge/component.rb', line 110 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_config ⇒ Object
124 125 126 |
# File 'lib/wedge/component.rb', line 124 def wedge_config @wedge_config ||= Config.new klass: self, scope: Wedge.config.scope, plugins: Wedge.config.plugins end |
.wedge_dom(&block) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/wedge/component.rb', line 112 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
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wedge/component.rb', line 75 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
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 |
# File 'lib/wedge/component.rb', line 45 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.config.app_dir}/|.*(?=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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/wedge/component.rb', line 12 def wedge_new(klass, *args, &block) obj = allocate # discuss: come up with something better obj.wedge_method_called = klass.method_called %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
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wedge/component.rb', line 129 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(m = false, &block) ⇒ Object
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/wedge/component.rb', line 151 def wedge_on_server(m = false, &block) m ||= Module.new(&block) if server? yield if block_given? 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.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.config.assets_key.present?? Wedge.assets_url.sub("#{Wedge.config.assets_key}/",'') : Wedge.assets_url}/#{path_name}.call" if block_given? HTTP.post(call_url, headers: { 'X-CSRF-TOKEN' => Element.find('meta[name=_csrf]').attr('content'), 'X-WEDGE-METHOD-REQUEST' => meth }, payload: payload) do |response| # We set the new csrf token xhr = Native(response.xhr) # discuss: I don't think we should update the csrf token every ajax call # 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 else data = { headers: { 'X-CSRF-TOKEN' => "#{Element.find('meta[name=_csrf]').attr('content')}", 'X-WEDGE-METHOD-REQUEST' => meth }, dataType: 'json', type: 'POST', url: call_url, data: payload, async: false }.to_n response = `$.ajax(data).responseText` begin JSON.parse response rescue if response.empty? raise "Ajax response to #{call_url} was empty." else puts response end end end end end include m end end |
.wedge_tmpl(name, dom = false, remove = true) ⇒ Object
Set templates
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/wedge/component.rb', line 95 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
393 394 395 396 397 398 |
# File 'lib/wedge/component.rb', line 393 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, &block) ⇒ Object
We want the scope to override this method if defined
256 257 258 259 260 261 |
# File 'lib/wedge/component.rb', line 256 def wedge(*args, &block) # fix: can't pass block to Wedge, opal error: # https://github.com/opal/opal/issues/959 # scope.respond_to?(:wedge) ? scope.wedge(*args, &block) : Wedge[*args, &block] scope.respond_to?(:wedge) ? scope.wedge(*args, &block) : Wedge[*args] end |
#wedge_class_store ⇒ Object Also known as: class_store
278 279 280 |
# File 'lib/wedge/component.rb', line 278 def wedge_class_store self.class.wedge_config.store end |
#wedge_config ⇒ Object Also known as: config
Duplicate of class condig [Config]
285 286 287 |
# File 'lib/wedge/component.rb', line 285 def wedge_config @wedge_config ||= Config.new(self.class.wedge_config.data.deep_dup) end |
#wedge_dom ⇒ Object Also known as: dom
Dom
299 300 301 302 303 304 305 306 307 |
# File 'lib/wedge/component.rb', line 299 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?
361 362 363 364 365 366 367 |
# File 'lib/wedge/component.rb', line 361 def wedge_from_client? begin wedge_method_called == caller_locations(1,1)[0].label rescue false end end |
#wedge_from_server? ⇒ Boolean Also known as: from_server?
350 351 352 353 354 355 356 |
# File 'lib/wedge/component.rb', line 350 def wedge_from_server? begin !(wedge_method_called == caller_locations(1,1)[0].label) rescue true end end |
#wedge_function(*args, &block) ⇒ Object Also known as: function
Special method that acts like the javascript equivalent
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/wedge/component.rb', line 317 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!
400 401 402 |
# File 'lib/wedge/component.rb', line 400 def wedge_html(&b) Wedge.html!(self, &b) end |
#wedge_javascript(method = false, *args) ⇒ Object Also known as: javscript
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/wedge/component.rb', line 370 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 = "Wedge.javascript('\#{config.path}', JSON.parse(Base64.decode64('\#{compiled_opts}')))\n" "<script>#{Opal.compile(javascript)}</script>" end |
#wedge_method_url(method, *args) ⇒ Object Also known as: method_url
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/wedge/component.rb', line 336 def wedge_method_url method, *args call_url = "#{Wedge.config.assets_key.present?? Wedge.assets_url.sub("#{Wedge.config.assets_key}/",'') : Wedge.assets_url}/#{wedge_config.path}.call" wedge_args = [] args.each do |arg| wedge_args << "__wedge_args__[]=#{arg}" end wedge_args = (wedge_args.length > 0 ? "&#{wedge_args.join('&')}" : '') "#{call_url}?__wedge_name__=#{wedge_config.name}&__wedge_method__=#{method}#{wedge_args}" end |
#wedge_plugin(name, *args, &block) ⇒ Object
We want the scope to override this method if defined
264 265 266 |
# File 'lib/wedge/component.rb', line 264 def wedge_plugin(name, *args, &block) scope.respond_to?(:wedge_plugin) ? scope.wedge_plugin(*args, &block) : wedge("#{name}_plugin", *args, &block) end |
#wedge_scope ⇒ Object Also known as: scope
268 269 270 |
# File 'lib/wedge/component.rb', line 268 def wedge_scope wedge_config.scope end |
#wedge_store ⇒ Object Also known as: store
273 274 275 |
# File 'lib/wedge/component.rb', line 273 def wedge_store wedge_config.store end |
#wedge_tmpl(name) ⇒ Object Also known as: tmpl
Grab a copy of the template
292 293 294 |
# File 'lib/wedge/component.rb', line 292 def wedge_tmpl(name) self.class.wedge_tmpl name end |
#wedge_trigger(event_name, *args) ⇒ Object Also known as: trigger
388 389 390 |
# File 'lib/wedge/component.rb', line 388 def wedge_trigger(event_name, *args) Wedge.events.trigger config.name, event_name, *args end |