]> git.openstreetmap.org Git - rails.git/commitdiff
Remove inline javascript from page header
authorTom Hughes <tom@compton.nu>
Wed, 1 Mar 2017 21:19:32 +0000 (21:19 +0000)
committerTom Hughes <tom@compton.nu>
Wed, 1 Mar 2017 21:19:32 +0000 (21:19 +0000)
app/assets/javascripts/application.js
app/assets/javascripts/oauth.js
app/assets/javascripts/osm.js.erb
app/helpers/application_helper.rb
app/views/layouts/_head.html.erb

index 5064d115b91ada67fd2198077b3ad9125a12cfd6..7d5b99f601e54ef8eacd9882c766a4305be356eb 100644 (file)
@@ -115,4 +115,24 @@ $(document).ready(function () {
   $("nav.primary li a").on("click", function() {
     $("header").toggleClass("closed");
   });
   $("nav.primary li a").on("click", function() {
     $("header").toggleClass("closed");
   });
+
+  var application_data = $("head").data();
+
+  I18n.default_locale = OSM.DEFAULT_LOCALE;
+  I18n.locale = application_data.locale;
+  I18n.fallbacks = true;
+
+  OSM.preferred_editor = application_data.preferredEditor;
+
+  if (application_data.user) {
+    OSM.user = application_data.user;
+
+    if (application_data.userHome) {
+      OSM.home = application_data.userHome;
+    }
+  }
+
+  if (application_data.location) {
+    OSM.location = application_data.location;
+  }
 });
 });
index 98ce302e105925de575198d60624de4ce2013443..037e1d730c7d65badd47205fa32c5ff20c24f9af 100644 (file)
@@ -1,18 +1,20 @@
 //= require ohauth
 
 $(document).ready(function () {
 //= require ohauth
 
 $(document).ready(function () {
+  var application_data = $("head").data();
+
   function makeAbsolute(url) {
     var a = document.createElement('a');
     a.href = url;
     return a.href;
   }
 
   function makeAbsolute(url) {
     var a = document.createElement('a');
     a.href = url;
     return a.href;
   }
 
-  if (OSM.oauth_token) {
+  if (application_data.token) {
     var headerGenerator = window.ohauth.headerGenerator({
     var headerGenerator = window.ohauth.headerGenerator({
-      consumer_key: OSM.oauth_consumer_key,
-      consumer_secret: OSM.oauth_consumer_secret,
-      token: OSM.oauth_token,
-      token_secret: OSM.oauth_token_secret
+      consumer_key: application_data.consumerKey,
+      consumer_secret: application_data.consumerSecret,
+      token: application_data.token,
+      token_secret: application_data.tokenSecret
     });
 
     $.ajaxPrefilter(function(options, jqxhr) {
     });
 
     $.ajaxPrefilter(function(options, jqxhr) {
index 697017697f1d320451c64e7600fd18789d68e1ea..ee1c6a115e427a299e9735c4846c98ea187ccf42 100644 (file)
@@ -16,6 +16,7 @@ OSM = {
   MAPQUEST_DIRECTIONS_URL: <%= MAPQUEST_DIRECTIONS_URL.to_json %>,
   MAPZEN_VALHALLA_URL:     <%= MAPZEN_VALHALLA_URL.to_json %>,
   OSRM_URL:                <%= OSRM_URL.to_json %>,
   MAPQUEST_DIRECTIONS_URL: <%= MAPQUEST_DIRECTIONS_URL.to_json %>,
   MAPZEN_VALHALLA_URL:     <%= MAPZEN_VALHALLA_URL.to_json %>,
   OSRM_URL:                <%= OSRM_URL.to_json %>,
+  DEFAULT_LOCALE:          <%= I18n.default_locale.to_json %>,
 
 <% if defined?(MAPQUEST_KEY) %>
   MAPQUEST_KEY:            <%= MAPQUEST_KEY.to_json %>,
 
 <% if defined?(MAPQUEST_KEY) %>
   MAPQUEST_KEY:            <%= MAPQUEST_KEY.to_json %>,
index 329aa25e8c11143aca876943ddee6cc1760da035..3d0a97243c75fe993eb19551ab0139951c1fa286 100644 (file)
@@ -100,4 +100,30 @@ module ApplicationHelper
   def current_page_class(path)
     :current if current_page?(path)
   end
   def current_page_class(path)
     :current if current_page?(path)
   end
+
+  def application_data
+    data = {
+      :locale => I18n.locale,
+      :preferred_editor => preferred_editor
+    }
+
+    if @user
+      data[:user] = @user.id.to_json
+
+      unless @user.home_lon.nil? || @user.home_lat.nil?
+        data[:user_home] = { :lat => @user.home_lat, :lon => @user.home_lon }
+      end
+    end
+
+    data[:location] = session[:location] if session[:location]
+
+    if @oauth
+      data[:token] = @oauth.token
+      data[:token_secret] = @oauth.secret
+      data[:consumer_key] = @oauth.client_application.key
+      data[:consumer_secret] = @oauth.client_application.secret
+    end
+
+    data
+  end
 end
 end
index 8830a8692319bc1e11d53ee8e176c8e8a17578e1..ad91b01d1fff83f61eefd5f92437a16c5ab1a93e 100644 (file)
@@ -1,4 +1,4 @@
-<head>
+<%= content_tag :head, :data => application_data do %>
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0"/>
   <!--[if lt IE 9]><%= javascript_include_tag "html5shiv" %><![endif]-->
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0"/>
   <!--[if lt IE 9]><%= javascript_include_tag "html5shiv" %><![endif]-->
   <%= yield :head %>
   <%= yield :auto_discovery_link_tag %>
   <%= csrf_meta_tag %>
   <%= yield :head %>
   <%= yield :auto_discovery_link_tag %>
   <%= csrf_meta_tag %>
-  <script type="text/javascript">
-    I18n.defaultLocale = "<%= I18n.default_locale %>";
-    I18n.locale = "<%= I18n.locale %>";
-    I18n.fallbacks = true;
-    <% if @user -%>
-    OSM.user = <%= @user.id.to_json.html_safe %>;
-    <% unless @user.home_lon.nil? or @user.home_lat.nil? -%>
-    OSM.home = <%= { :lat => @user.home_lat, :lon => @user.home_lon }.to_json.html_safe %>;
-    <% end -%>
-    <% end -%>
-    <% if session[:location] -%>
-    OSM.location = <%= session[:location].to_json.html_safe %>;
-    <% end -%>
-    OSM.preferred_editor = <%= preferred_editor.to_json.html_safe %>;
-    <% if @oauth -%>
-    OSM.oauth_token = "<%= @oauth.token %>";
-    OSM.oauth_token_secret = "<%= @oauth.secret %>";
-    OSM.oauth_consumer_key = "<%= @oauth.client_application.key %>";
-    OSM.oauth_consumer_secret = "<%= @oauth.client_application.secret %>";
-    <% end -%>
-  </script>
   <title><%= t 'layouts.project_name.title' %><%= ' | ' + @title if @title %></title>
   <title><%= t 'layouts.project_name.title' %><%= ' | ' + @title if @title %></title>
-</head>
+<% end %>