Module: PhraseApp::InContextEditor::ViewHelpers

Defined in:
lib/phraseapp-in-context-editor-ruby/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#phraseapp_in_context_editor_js(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/phraseapp-in-context-editor-ruby/view_helpers.rb', line 6

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