Module: BrowserIO

Includes:
Methods
Defined in:
lib/browserio.rb,
lib/browserio/dom.rb,
lib/browserio/html.rb,
lib/browserio/opal.rb,
lib/browserio/config.rb,
lib/browserio/events.rb,
lib/browserio/version.rb,
lib/browserio/component.rb,
lib/browserio/plugins/form.rb,
lib/browserio/plugins/pjax.rb,
lib/browserio/utilis/methods.rb,
lib/browserio/plugins/history.rb,
lib/browserio/plugins/validations.rb,
lib/browserio/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.4"
Form =
BrowserIO::Plugins::Form

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Class Method Details

.[](name, *args) ⇒ BrowserIO::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:



31
32
33
34
35
# File 'lib/browserio.rb', line 31

def [](name, *args)
  component = components[name.to_sym]

  component.klass.new(*args)
end

.componentsObject



37
38
39
# File 'lib/browserio.rb', line 37

def components
  @components ||= OpenStruct.new
end

.get_requires(requires, reqs = [], from_get = false) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/browserio.rb', line 116

def get_requires requires, reqs = [], from_get = false
  promises = []

  requires.each do |r|
    if r[:requires].any?
      promises << (promise = ::Opal::Promise.new)

      a = []
      c = []

      get_requires(r[:requires], a, true)

      a.each do |re|
        c << -> do
          p = ::Opal::Promise.new

          path_name = re.delete(:path_name)
          BrowserIO.javascript(path_name, re.reject { |k, v| k.to_s == 'requires'}, p)

          p
        end
      end

      ::Opal::Promise.when(*c.map!(&:call)).then do |*args|
        path_name = r.delete(:path_name)
        BrowserIO.javascript(path_name, r.reject { |k, v| k.to_s == 'requires'}, promise)
      end
    else
      reqs << r

      if !from_get
        promises << (promise = ::Opal::Promise.new)

        path_name = r.delete(:path_name)
        BrowserIO.javascript(path_name, r.reject { |k, v| k.to_s == 'requires'}, promise)
      end
    end
  end

  promises
end

.javascript(name = 'browserio', options = {}, promise = false) ⇒ Object

Return the opal javascript.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/browserio.rb', line 69

def javascript(name = 'browserio', options = {}, promise = false)
  if server?
    if name == 'browserio'
      @bio_javascript ||= build(name, options).javascript
    else
      js = build(name, options).javascript
      comp_name = components.to_h.select { |k, v| v.path_name == name }.first.last.name
      comp = BrowserIO[comp_name]
      options = comp.client_bio_opts
      compiled_opts = Base64.encode64 options.to_json
      js << Opal.compile("BrowserIO.components[:#{comp_name}].klass.instance_variable_set(:@bio_config, BrowserIO::Config.new(BrowserIO.components[:#{comp_name}].klass.bio_config.opts_dup.merge(JSON.parse(Base64.decode64('#{compiled_opts}')))))")
      js
    end
  else
    opts.loaded ||= {}

    if !opts.loaded.keys.include? name
      opts.loaded[name] = false

      assets_url = options[:assets_url]

      `$.getScript("/" + assets_url + "/" + name + ".js").done(function(){`
        BrowserIO.opts.loaded[name] = true
        method_called = options.delete(:method_called)
        method_args   = options.delete(:method_args)
        name          = options.delete(:name)
        comp          = BrowserIO[name, options]
        requires      = comp.bio_opts.requires

        if requires.present? && requires.first.is_a?(Hash)
          comps = []

          ::Opal::Promise.when(*get_requires(requires, comps)).then do
            comp.send(method_called, *method_args) if method_called
            comp.bio_trigger :browser_events
          end
        else
          comp.send(method_called, *method_args) if method_called
          comp.bio_trigger :browser_events
        end

        promise.resolve true if promise
      `}).fail(function(jqxhr, settings, exception){ window.console.log(exception); });`
    end
  end
end

.optsObject



185
186
187
# File 'lib/browserio.rb', line 185

def opts
  config.opts
end

.setup {|Config| ... } ⇒ Object

Used to setup the component with default options.

Examples:

class SomeComponent < Component
  setup do |config|
    config.name :some
  end
end

Yields:



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

def setup(&block)
  javascript # This pre-compiles the core and store it in mem
  block.call config
end

Instance Method Details

#append_pathsArray

Append the correct paths to opal.

Returns:

  • (Array)

    List of opal paths.



59
60
61
62
63
64
65
# File 'lib/browserio.rb', line 59

def append_paths
  @append_paths ||= begin
    file = method(:components).source_location.first.sub('/browserio.rb', '')
    BrowserIO::Opal.append_path file
    BrowserIO::Opal.append_path Dir.pwd
  end
end

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

Returns the build object for opal.

Parameters:

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

    require path to file to build.

Returns:



46
47
48
49
# File 'lib/browserio.rb', line 46

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

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

Source maps for the javascript



52
53
54
# File 'lib/browserio.rb', line 52

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