Class: Wedge

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Methods
Defined in:
lib/wedge/component.rb,
lib/wedge.rb,
lib/wedge/dom.rb,
lib/wedge/html.rb,
lib/wedge/opal.rb,
lib/wedge/store.rb,
lib/wedge/config.rb,
lib/wedge/events.rb,
lib/wedge/railtie.rb,
lib/wedge/version.rb,
lib/wedge/middleware.rb,
lib/wedge/plugins/form.rb,
lib/wedge/plugins/pjax.rb,
lib/wedge/plugins/render.rb,
lib/wedge/utilis/methods.rb,
lib/wedge/plugins/factory.rb,
lib/wedge/plugins/history.rb,
lib/wedge/plugins/popover.rb,
lib/wedge/plugins/uploader.rb,
lib/wedge/plugins/form_backup.rb,
lib/wedge/plugins/ability_list.rb,
lib/wedge/plugins/current_user.rb,
lib/wedge/utilis/indifferent_hash.rb,
lib/wedge/plugins/form/validations.rb

Overview

uses drop.js

Defined Under Namespace

Modules: HTML, Methods, Opal, Plugins, Store Classes: Component, Config, DOM, Events, IndifferentHash, Middleware, Railtie

Constant Summary collapse

ATTR_ACCESSORS =
%i{scope store config events method_called}
VERSION =
'0.2.8'
Form =
Wedge::Plugins::Form

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Class Method Details

.[](*args, &block) ⇒ Wedge::Component#method

Used to call a component.

Examples:

Browser[:foo].bar

Parameters:

  • name (String, Symbol, #to_s)

    The unique name given to a component.

Returns:



167
168
169
170
# File 'lib/wedge.rb', line 167

def [](*args, &block)
  name = args.shift
  config.component_class[name.to_sym].wedge_new self, *args, &block
end

.assets_urlObject



69
70
71
72
# File 'lib/wedge.rb', line 69

def assets_url
  url = config.assets_url.gsub(%r{^(http(|s)://[^\/]*\/|\/)}, '/')
  url.gsub /\n/, ''
end

.assets_url_regexObject



74
75
76
77
78
79
80
81
# File 'lib/wedge.rb', line 74

def assets_url_regex
  @assets_url_regex ||= begin
    assets_url = ::Wedge.assets_url.gsub(%r{^\/}, '')
    # # We also allow for no assets key so when we post server methods there
    # # isn't an error if the key has been changed since a browser refresh.
    %r{(?:/|)#{assets_url}/(.*)\.(.*)$}
  end
end

.assets_url_with_hostObject



83
84
85
# File 'lib/wedge.rb', line 83

def assets_url_with_host
  "#{config.assets_url}#{config.cache_assets ? "/#{config.assets_key}" : ''}"
end

.configObject



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/wedge.rb', line 253

def config
  @config ||= begin
    args = { component_class: IndifferentHash.new }

    unless RUBY_ENGINE == 'opal'
      args[:path]       = method(:assets_url).source_location.first.sub('/wedge.rb', '')
      args[:assets_key] = ::File.exist?('.wedge_assets_key') ? ::File.read('.wedge_assets_key') : nil
    end

    Config.new(args)
  end
end

.create_assets_keyObject



245
246
247
248
249
250
251
# File 'lib/wedge.rb', line 245

def create_assets_key
  o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
  key = (0...50).map { o[rand(o.length)] }.join
  ::FileUtils.mkdir_p(File.dirname('.wedge_assets_key'))
  ::File.open('.wedge_assets_key', 'wb'){|f| f.write(key) }
  config.assets_key = key
end

.eventsObject



156
157
158
# File 'lib/wedge.rb', line 156

def events
  @events ||= Events.new
end

.html!(scope = false, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/wedge.rb', line 87

def html!(scope = false, &block)
  if !block_given?
    html = HTML::DSL.html(&scope).to_html
  else
    html = HTML::DSL.scope!(scope).html(&block).to_html
  end

  DOM.new html
end

.javascript(path_name = 'wedge', options = {}, url = false) ⇒ Object

Return the opal javascript.



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/wedge.rb', line 212

def javascript(path_name = 'wedge', options = {}, url = false)
  path_name = path_name.to_s

  if server?
    javascript_cache[path_name] ||= begin
      build(path_name, options).to_s
    end
  else
    trigger_javascript_loaded path_name, options
  end
end

.load_settings(settings) ⇒ Object



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

def load_settings settings
  return unless settings

  case settings
  when Proc
    Wedge.config.instance_eval &settings
  else
    settings.each { |k, v| Wedge.config.send "#{k}=", v }
  end

  Wedge.config.opal = { server: Wedge::Opal::Server.new { |s|
    s.append_path "#{Dir.pwd}/#{Wedge.config.app_dir}"
    s.prefix  = Wedge.assets_url
    s.debug   = Wedge.config.debug
    s.headers = Wedge.config.assets_headers
    s.gzip    = Wedge.config.gzip_assets
  }}

  if Wedge.config.debug
    Wedge.config.opal[:sprockets]   = Wedge.config.opal[:server].sprockets
    Wedge.config.opal[:maps_prefix] = "#{Wedge.assets_url}/__OPAL_SOURCE_MAPS__"
    Wedge.config.opal[:maps_app]    = Opal::SourceMapServer.new Wedge.config.opal[:sprockets], Wedge.config.opal[:maps_prefix]

    Wedge::Opal::Sprockets::SourceMapHeaderPatch.inject! Wedge.config.opal[:maps_prefix]
  end
end

.trigger(wedge_name, event_name, *args) ⇒ Object



152
153
154
# File 'lib/wedge.rb', line 152

def trigger(wedge_name, event_name, *args)
  events.trigger wedge_name, event_name, *args
end

.trigger_javascript_loaded(path_name, options) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/wedge.rb', line 224

def trigger_javascript_loaded path_name, options
  # fix: at the moment to_js called from the server will set the class
  # store for whatever method it calls.  we need to think of a better idea
  # for global and local data store.
  Wedge.config.component_class[options[:name]].config.store = options[:store].indifferent

  if initialize_args = options.delete(:initialize_args)
    comp = Wedge[options[:name], *initialize_args]
  else
    comp = Wedge[options[:name]]
  end

  if options[:method_args].any?
    comp.send(options[:method_called], options[:method_args])
  else
    comp.send(options[:method_called])
  end

  Wedge.trigger_browser_events
end

.versionObject



89
90
91
# File 'lib/wedge/opal.rb', line 89

def self.version
  Wedge::VERSION
end

Instance Method Details

#append_pathsArray

Append the correct paths to opal.

Returns:

  • (Array)

    List of opal paths.



201
202
203
204
205
206
207
208
# File 'lib/wedge.rb', line 201

def append_paths
  @append_paths ||= begin
    Wedge::Opal.append_path method(:assets_url).source_location.first.sub('/wedge.rb', '')
    Wedge::Opal.append_path "#{Dir.pwd}/#{config.app_dir}"

    true
  end
end

#build(path = 'wedge', options = {}) ⇒ String, Opal::Builder#build

Returns the build object for opal.

Parameters:

  • path (String) (defaults to: 'wedge')

    require path to file to build.

Returns:



189
190
191
# File 'lib/wedge.rb', line 189

def build(path = 'wedge', options = {})
  Opal::Builder.build(path, options) if append_paths
end

#get_asset_urls(name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/wedge.rb', line 118

def get_asset_urls name
  sprockets = Wedge.config.opal[:server].sprockets
  prefix = Wedge.assets_url_with_host
  asset = sprockets[name]
  raise "Cannot find asset: #{name}" if asset.nil?
  urls = []

  if Wedge.config.opal[:server].debug
    asset.to_a.map do |dependency|
      urls << %{#{prefix}/#{dependency.logical_path}?body=1}
    end
  else
    urls << %{#{prefix}/#{name}.js}
  end

  urls
end

#javascript_cacheObject



136
137
138
# File 'lib/wedge.rb', line 136

def javascript_cache
  @javascript_cache ||= IndifferentHash.new
end

#script_tag(name = 'wedge') ⇒ Object

def script_tag

# note: ?body=1 is a hack for sprockets to make source maps work # correctly.
"<script src='#{assets_url_with_host}/wedge.js#{Wedge.config.debug ? '?body=1' : ''}' type='text/javascript'></script>"

end



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

def script_tag name = 'wedge'
  sprockets = Wedge.config.opal[:server].sprockets
  asset = sprockets[name]
  raise "Cannot find asset: #{name}" if asset.nil?
  scripts = []

  get_asset_urls(name).each do |url|
    scripts << %{<script src="#{url}"></script>}
  end

  scripts << %{<script>#{Wedge::Opal::Sprockets.load_asset(name, sprockets)}</script>}

  scripts.join "\n"
end

#source_map(path = 'wedge', options = {}) ⇒ Object

Source maps for the javascript



194
195
196
# File 'lib/wedge.rb', line 194

def source_map(path = 'wedge', options = {})
  build(path, options).source_map
end

#trigger_browser_eventsObject



142
143
144
145
146
147
148
149
# File 'lib/wedge.rb', line 142

def trigger_browser_events
  config.component_class.each do |k, klass|
    next if klass.config.triggered_browser_events
    klass.config.triggered_browser_events = true

    Wedge.trigger klass.config.name, :browser_events
  end
end