Class: Wedge::Component
- Includes:
- Methods
- Defined in:
- lib/wedge/component.rb
Direct Known Subclasses
Plugins::Form, Plugins::History, Plugins::Pjax, Plugins::Uploader
Class Attribute Summary collapse
-
.wedge_on_count ⇒ Object
Returns the value of attribute wedge_on_count.
Class Method Summary collapse
- .config ⇒ Object
- .dom ⇒ Object
- .html ⇒ Object
- .html!(&b) ⇒ Object
- .method_missing(method, *args, &block) ⇒ 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(&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) ⇒ Object
-
#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_plugin(name, *args, &block) ⇒ Object
- #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
341 342 343 344 345 346 347 |
# File 'lib/wedge/component.rb', line 341 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.
8 9 10 |
# File 'lib/wedge/component.rb', line 8 def wedge_on_count @wedge_on_count end |
Class Method Details
.config ⇒ Object
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 |
.dom ⇒ Object
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 |
.html ⇒ Object
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 |
# File 'lib/wedge/component.rb', line 214 def html!(&b) Wedge.html!(self, &b) 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 |
.store ⇒ Object
218 219 220 |
# File 'lib/wedge/component.rb', line 218 def store wedge_config.store end |
.tmpl ⇒ Object
Set templates
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_config ⇒ Object
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
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
329 330 331 332 333 334 |
# File 'lib/wedge/component.rb', line 329 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
224 225 226 |
# File 'lib/wedge/component.rb', line 224 def wedge(*args) Wedge[*args] end |
#wedge_config ⇒ Object Also known as: config
Duplicate of class condig [Config]
245 246 247 |
# File 'lib/wedge/component.rb', line 245 def wedge_config @wedge_config ||= Config.new(self.class.wedge_config.data.dup) end |
#wedge_dom ⇒ Object Also known as: dom
Dom
259 260 261 262 263 264 265 266 267 |
# File 'lib/wedge/component.rb', line 259 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?
301 302 303 |
# File 'lib/wedge/component.rb', line 301 def wedge_from_client? !wedge_from_server? end |
#wedge_from_server? ⇒ Boolean Also known as: from_server?
296 297 298 |
# File 'lib/wedge/component.rb', line 296 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
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/wedge/component.rb', line 277 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!
336 337 338 |
# File 'lib/wedge/component.rb', line 336 def wedge_html(&b) Wedge.html!(self, &b) end |
#wedge_javascript(method = false, *args) ⇒ Object Also known as: javscript
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/wedge/component.rb', line 306 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
228 229 230 |
# File 'lib/wedge/component.rb', line 228 def wedge_plugin(name, *args, &block) wedge("#{name}_plugin", *args, &block) end |
#wedge_scope ⇒ Object Also known as: scope
233 234 235 |
# File 'lib/wedge/component.rb', line 233 def wedge_scope wedge_config.scope end |
#wedge_store ⇒ Object Also known as: store
238 239 240 |
# File 'lib/wedge/component.rb', line 238 def wedge_store wedge_config.store end |
#wedge_tmpl(name) ⇒ Object Also known as: tmpl
Grab a copy of the template
252 253 254 |
# File 'lib/wedge/component.rb', line 252 def wedge_tmpl(name) self.class.wedge_tmpl name end |