Module: Wedge
- Includes:
- Methods
- Defined in:
- lib/wedge.rb,
lib/wedge/dom.rb,
lib/wedge/html.rb,
lib/wedge/opal.rb,
lib/wedge/config.rb,
lib/wedge/events.rb,
lib/wedge/version.rb,
lib/wedge/component.rb,
lib/wedge/plugins/form.rb,
lib/wedge/plugins/pjax.rb,
lib/wedge/utilis/methods.rb,
lib/wedge/plugins/history.rb,
lib/wedge/plugins/validations.rb,
lib/wedge/utilis/indifferent_hash.rb
Defined Under Namespace
Modules: HTML, Methods, Plugins Classes: Component, Config, DOM, Events, IndifferentHash
Constant Summary collapse
- Opal =
Create our own opal instance.
::Opal.dup
- VERSION =
"0.0.1"
- Form =
Wedge::Plugins::Form
Class Attribute Summary collapse
-
.javascript_cache ⇒ Object
Returns the value of attribute javascript_cache.
-
.loaded_requires ⇒ Object
Returns the value of attribute loaded_requires.
-
.loaded_requires_events ⇒ Object
Returns the value of attribute loaded_requires_events.
-
.requires ⇒ Object
Returns the value of attribute requires.
-
.wedge_javascript_loaded ⇒ Object
Returns the value of attribute wedge_javascript_loaded.
Class Method Summary collapse
-
.[](name, *args) ⇒ Wedge::Component#method
Used to call a component.
- .assets_url ⇒ Object
- .cache ⇒ Object
- .components ⇒ Object
- .config ⇒ Object
- .get_requires(reqs, requires_array = []) ⇒ Object
-
.javascript(path_name = 'wedge', options = {}, promise = false) ⇒ Object
Return the opal javascript.
- .load_comp(options = {}, promise = Promise.new) ⇒ Object
- .load_requires(requires, promise = Promise.new) ⇒ Object
- .opts ⇒ Object
- .script_tag ⇒ Object
-
.setup {|Config| ... } ⇒ Object
Used to setup the component with default options.
- .trigger_requires_events(requires) ⇒ Object
Instance Method Summary collapse
-
#append_paths ⇒ Array
Append the correct paths to opal.
-
#build(path = 'wedge', options = {}) ⇒ String, Opal::Builder#build
Returns the build object for opal.
-
#source_map(path = 'wedge', options = {}) ⇒ Object
Source maps for the javascript.
Methods included from Methods
Class Attribute Details
.javascript_cache ⇒ Object
Returns the value of attribute javascript_cache.
24 25 26 |
# File 'lib/wedge.rb', line 24 def javascript_cache @javascript_cache end |
.loaded_requires ⇒ Object
Returns the value of attribute loaded_requires.
24 25 26 |
# File 'lib/wedge.rb', line 24 def loaded_requires @loaded_requires end |
.loaded_requires_events ⇒ Object
Returns the value of attribute loaded_requires_events.
24 25 26 |
# File 'lib/wedge.rb', line 24 def loaded_requires_events @loaded_requires_events end |
.requires ⇒ Object
Returns the value of attribute requires.
24 25 26 |
# File 'lib/wedge.rb', line 24 def requires @requires end |
.wedge_javascript_loaded ⇒ Object
Returns the value of attribute wedge_javascript_loaded.
24 25 26 |
# File 'lib/wedge.rb', line 24 def wedge_javascript_loaded @wedge_javascript_loaded end |
Class Method Details
.[](name, *args) ⇒ Wedge::Component#method
Used to call a component.
46 47 48 49 50 |
# File 'lib/wedge.rb', line 46 def [](name, *args) component = components[name.to_sym] component.klass.new(*args) end |
.assets_url ⇒ Object
31 32 33 |
# File 'lib/wedge.rb', line 31 def assets_url "#{opts.assets_url}#{opts.cache_assets ? "/#{opts.assets_key}" : ''}" end |
.cache ⇒ Object
27 28 29 |
# File 'lib/wedge.rb', line 27 def cache javascript end |
.components ⇒ Object
52 53 54 |
# File 'lib/wedge.rb', line 52 def components @components ||= OpenStruct.new end |
.config ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/wedge.rb', line 221 def config @config ||= begin args = { klass: self } unless RUBY_ENGINE == 'opal' args[:file_path] = caller.first.gsub(/(?<=\.rb):.*/, '') args[:assets_key] = ENV.fetch('SOURCE_VERSION') { `git rev-parse HEAD 2>/dev/null`.to_s.strip } end Config.new(args) end end |
.get_requires(reqs, requires_array = []) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/wedge.rb', line 177 def get_requires reqs, requires_array = [] new_reqs = [] reqs.each do |r| if r[:requires].any? get_requires(r[:requires], requires_array) end new_reqs << r end requires_array << new_reqs if new_reqs.any? requires_array end |
.javascript(path_name = 'wedge', options = {}, promise = false) ⇒ Object
Return the opal javascript.
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 120 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 |
# File 'lib/wedge.rb', line 84 def javascript(path_name = 'wedge', = {}, promise = false) if server? if path_name == 'wedge' @wedge_javascript ||= begin @wedge_javascript_loaded = true requires = {} @javascript_cache ||= {} components.to_h.each do |k, v| requires[k] = v.klass.wedge_config.get_requires javascript(v.klass.wedge_opts[:path_name]) end compiled_requires = Base64.encode64 requires.to_json assets_key = opts.assets_key cache_assets = opts.cache_assets js = build(path_name, ).javascript js << Opal.compile("Wedge.instance_variable_set(:@requires, JSON.parse(Base64.decode64('#{compiled_requires}')))") # fix: we need to just merge in all config opts and just reject # certain ones js << Opal.compile("Wedge.config.assets_key('#{assets_key}')") if assets_key js << Opal.compile("Wedge.config.cache_assets('#{cache_assets}')") if cache_assets ############################################################## js end else @javascript_cache[path_name] ||= begin js = build(path_name, ).javascript comp_name = components.to_h.select { |k, v| v.path_name == path_name }.first.last.name comp = Wedge[comp_name] = comp.client_wedge_opts compiled_opts = Base64.encode64 .to_json js << Opal.compile("Wedge.components[:#{comp_name}].klass.instance_variable_set(:@wedge_config, Wedge::Config.new(Wedge.components[:#{comp_name}].klass.wedge_config.opts_dup.merge(JSON.parse(Base64.decode64('#{compiled_opts}')))))") end end else Wedge.loaded_requires ||= [] Wedge.loaded_requires_events ||= [] reqs = Wedge.requires[[:name].to_sym].dup promise = Promise.new requires = get_requires(reqs) load_requires(requires.dup, promise) promise.then do load_comp().then do method_called = [:method_called] method_args = [:method_args] name = [:name] comp = Wedge[name, ] Document.ready? do trigger_requires_events requires.dup comp.send(method_called, *method_args) if method_called comp.wedge_trigger :browser_events end end end end end |
.load_comp(options = {}, promise = Promise.new) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/wedge.rb', line 193 def load_comp = {}, promise = Promise.new path_name = [:path_name] assets_url = Wedge.assets_url # fix: this could give people unwanted behaviour, change getScript to just # use ajax. `jQuery.ajaxSetup({ cache: true })` if Wedge.opts.cache_assets `$.getScript(assets_url + "/" + path_name + ".js").done(function(){` promise.resolve true `}).fail(function(jqxhr, settings, exception){ window.console.log(exception); });` ######################################################################### promise end |
.load_requires(requires, promise = Promise.new) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/wedge.rb', line 160 def load_requires requires, promise = Promise.new reqs = requires.shift promises = [] reqs.each do |r| next if Wedge.loaded_requires.include? r[:name] Wedge.loaded_requires << r[:name] promises << -> { load_comp r } end Promise.when(*promises.map!(&:call)).then do requires.any?? load_requires(requires, promise) : promise.resolve(true) end end |
.opts ⇒ Object
234 235 236 |
# File 'lib/wedge.rb', line 234 def opts config.opts end |
.script_tag ⇒ Object
35 36 37 |
# File 'lib/wedge.rb', line 35 def script_tag "<script src='#{assets_url}/wedge.js'></script>" end |
.setup {|Config| ... } ⇒ Object
Used to setup the component with default options.
217 218 219 |
# File 'lib/wedge.rb', line 217 def setup(&block) block.call config if block_given? end |
.trigger_requires_events(requires) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/wedge.rb', line 147 def trigger_requires_events requires reqs = requires.shift reqs.each do |r| next if Wedge.loaded_requires_events.include? r[:name] Wedge.loaded_requires_events << r[:name] comp = Wedge[r[:name], r] comp.wedge_trigger :browser_events end trigger_requires_events requires if requires.any? end |
Instance Method Details
#append_paths ⇒ Array
Append the correct paths to opal.
74 75 76 77 78 79 80 |
# File 'lib/wedge.rb', line 74 def append_paths @append_paths ||= begin file = method(:components).source_location.first.sub('/wedge.rb', '') Wedge::Opal.append_path file Wedge::Opal.append_path Dir.pwd end end |
#build(path = 'wedge', options = {}) ⇒ String, Opal::Builder#build
Returns the build object for opal.
61 62 63 64 |
# File 'lib/wedge.rb', line 61 def build(path = 'wedge', = {}) append_paths Opal::Builder.build(path, ) end |
#source_map(path = 'wedge', options = {}) ⇒ Object
Source maps for the javascript
67 68 69 |
# File 'lib/wedge.rb', line 67 def source_map(path = 'wedge', = {}) build(path, ).source_map end |