Sha256: 69e81c99686167d67d4622ecbdd19a8e855adeb05891670bb49acc51a312fb0a

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

require 'json'

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

        # stringify to reduce possible errors when passing symbols
        js_default_options = PhraseApp::InContextEditor.js_options.inject({}) { |conf, (k,v)| conf[k.to_s] = v; conf}
        opts = opts.nil? ? {} : opts.inject({}) { |conf, (k,v)| conf[k.to_s] = v; conf}

        # js options
        configuration = {
          'projectId' => PhraseApp::InContextEditor.project_id,
          'prefix' => PhraseApp::InContextEditor.prefix,
          'suffix' => PhraseApp::InContextEditor.suffix,
          'apiBaseUrl' => "#{PhraseApp::InContextEditor.api_host}/api/v2",
        }.merge(js_default_options).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}#{PhraseApp::InContextEditor.js_path}?', 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

10 entries across 10 versions & 1 rubygems

Version Path
phraseapp-in-context-editor-ruby-1.4.0 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.3.1 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.3.0 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.2.1 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.2.0 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.1.0 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.0.6 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.0.5 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.0.4 lib/phraseapp-in-context-editor-ruby/view_helpers.rb
phraseapp-in-context-editor-ruby-1.0.3 lib/phraseapp-in-context-editor-ruby/view_helpers.rb