Sha256: d57f17f406b7ea27f839f381cda85037d24cb085a59f7f8f82c315e95a5d6e76

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'json'

module PhraseApp
  module InContextEditor
    module ViewHelpers
      def phraseapp_in_context_editor_js(opts={})
        return "" unless PhraseApp::InContextEditor.enabled?

        opts = opts.nil? ? {} : opts.inject({}) { |conf, (k,v)| conf[k.to_s] = v; conf} # stringify to reduce possible errors when passing symbols
        configuration = {
          'projectId' => PhraseApp::InContextEditor.project_id,
          'prefix' => PhraseApp::InContextEditor.prefix,
          'suffix' => PhraseApp::InContextEditor.suffix,
          'apiBaseUrl' => "#{PhraseApp::InContextEditor.api_host}/api/v2",
        }.merge(opts)

        snippet = <<-eos
          <script>
            window.PHRASEAPP_CONFIG = #{configuration.to_json};
            (function() {
              var phraseapp = document.createElement('script'); phraseapp.type = 'text/javascript'; phraseapp.async = true;
              phraseapp.src = ['#{PhraseApp::InContextEditor.js_use_ssl ? 'https' : 'http'}://', '#{PhraseApp::InContextEditor.js_host}/assets/in-context-editor/2.0/app.js?', new Date().getTime()].join('');
              var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(phraseapp, s);
            })();
          </script>
        eos
        snippet.respond_to?(:html_safe) ? snippet.html_safe : snippet
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phraseapp-in-context-editor-ruby-1.0.2 lib/phraseapp-in-context-editor-ruby/view_helpers.rb