Sha256: 79b8a66c06d7a26869ccaa541b44fbd0673761043e84e6418fcf1ba384c63ecd

Contents?: true

Size: 1 KB

Versions: 4

Compression:

Stored size: 1 KB

Contents

require 'opal/sprockets'

module OpalHelper
  def opal_tag(opal_code = nil, &block)
    opal_code ||= capture(&block)
    compiler_options = Opal::Config.compiler_options.merge(requirable: false)
    compiler = Opal::Compiler.new(opal_code, compiler_options)
    js_code = compiler.compile
    javascript_tag js_code
  end

  def javascript_include_tag(*sources)
    options = sources.extract_options!
    skip_loader = options.delete(:skip_opal_loader)
    skip_onload = options.delete(:force_opal_loader_tag)

    return super(*sources, options) if skip_loader

    script_tags = "".html_safe
    sources.each do |source|
      load_asset_code = Opal::Sprockets.load_asset(source)
      loading_code = "if(window.Opal && Opal.modules[#{source.to_json}]){#{load_asset_code}}"

      if skip_onload
        script_tags << super(source, options)
        script_tags << javascript_tag(loading_code)
      else
        script_tags << super(source, options.merge(onload: loading_code))
      end
    end
    script_tags
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
opal-rails-1.1.1 app/helpers/opal_helper.rb
opal-rails-1.1.0 app/helpers/opal_helper.rb
opal-rails-1.0.1 app/helpers/opal_helper.rb
opal-rails-1.0.0 app/helpers/opal_helper.rb