gmaps4rails 1.4.8 → 1.5.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +10 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +175 -0
  5. data/Guardfile +38 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.rdoc +26 -37
  8. data/Rakefile +4 -0
  9. data/app/assets/javascripts/gmaps4rails/gmaps4rails.base.js.coffee +40 -34
  10. data/app/assets/javascripts/gmaps4rails/gmaps4rails.googlemaps.js.coffee +4 -2
  11. data/app/views/gmaps4rails/_gmaps4rails.html.erb +13 -12
  12. data/gmaps4rails.gemspec +38 -0
  13. data/lib/gmaps4rails.rb +0 -8
  14. data/lib/gmaps4rails/acts_as_gmappable.rb +55 -33
  15. data/lib/gmaps4rails/base.rb +7 -12
  16. data/lib/gmaps4rails/helper/gmaps4rails_helper.rb +16 -42
  17. data/lib/gmaps4rails/js_builder.rb +121 -0
  18. data/lib/gmaps4rails/json_builder.rb +127 -0
  19. data/lib/gmaps4rails/version.rb +3 -0
  20. data/lib/gmaps4rails/view_helper.rb +171 -0
  21. data/lib/tasks/gmaps4rails_tasks.rake +4 -0
  22. data/lib/tasks/jasmine.rake +8 -0
  23. data/public/javascripts/gmaps4rails/gmaps4rails.base.js +81 -30
  24. data/public/javascripts/gmaps4rails/gmaps4rails.bing.js +34 -11
  25. data/public/javascripts/gmaps4rails/gmaps4rails.googlemaps.js +45 -21
  26. data/public/javascripts/gmaps4rails/gmaps4rails.mapquest.js +33 -14
  27. data/public/javascripts/gmaps4rails/gmaps4rails.openlayers.js +33 -20
  28. data/spec/base/base_spec.rb +185 -0
  29. data/spec/base/geocoding_spec.rb +17 -0
  30. data/spec/dummy/.gitignore +2 -0
  31. data/spec/dummy/.rspec +1 -0
  32. data/spec/dummy/README.rdoc +261 -0
  33. data/spec/dummy/Rakefile +7 -0
  34. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  35. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  36. data/spec/dummy/app/assets/stylesheets/gmaps4rails.css +24 -0
  37. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  38. data/spec/dummy/app/controllers/users_controller.rb +96 -0
  39. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy/app/mailers/.gitkeep +0 -0
  41. data/spec/dummy/app/models/.gitkeep +0 -0
  42. data/spec/dummy/app/models/user.rb +9 -0
  43. data/spec/dummy/app/views/layouts/application.html.erb +16 -0
  44. data/spec/dummy/app/views/users/_form.html.erb +37 -0
  45. data/spec/dummy/app/views/users/_my_template.html.erb +2 -0
  46. data/spec/dummy/app/views/users/_my_template2.html.erb +1 -0
  47. data/spec/dummy/app/views/users/ajax_data.js.erb +2 -0
  48. data/spec/dummy/app/views/users/ajax_test.html.erb +22 -0
  49. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  50. data/spec/dummy/app/views/users/index.html.erb +238 -0
  51. data/spec/dummy/app/views/users/new.html.erb +5 -0
  52. data/spec/dummy/app/views/users/show.html.erb +30 -0
  53. data/spec/dummy/config.ru +4 -0
  54. data/spec/dummy/config/application.rb +62 -0
  55. data/spec/dummy/config/boot.rb +10 -0
  56. data/spec/dummy/config/database.yml +25 -0
  57. data/spec/dummy/config/environment.rb +5 -0
  58. data/spec/dummy/config/environments/development.rb +37 -0
  59. data/spec/dummy/config/environments/production.rb +67 -0
  60. data/spec/dummy/config/environments/test.rb +37 -0
  61. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/dummy/config/initializers/inflections.rb +15 -0
  63. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  64. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  65. data/spec/dummy/config/initializers/session_store.rb +8 -0
  66. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  67. data/spec/dummy/config/locales/en.yml +5 -0
  68. data/spec/dummy/config/routes.rb +60 -0
  69. data/spec/dummy/db/migrate/20120408170155_create_users.rb +16 -0
  70. data/spec/dummy/db/schema.rb +29 -0
  71. data/spec/dummy/lib/assets/.gitkeep +0 -0
  72. data/spec/dummy/log/.gitkeep +0 -0
  73. data/spec/dummy/public/404.html +26 -0
  74. data/spec/dummy/public/422.html +26 -0
  75. data/spec/dummy/public/500.html +25 -0
  76. data/spec/dummy/public/favicon.ico +0 -0
  77. data/spec/dummy/public/javascripts/Player.js +22 -0
  78. data/spec/dummy/public/javascripts/Song.js +7 -0
  79. data/spec/dummy/script/rails +6 -0
  80. data/spec/factories/user_factory.rb +23 -0
  81. data/spec/javascripts/basic_methods_spec.js +103 -0
  82. data/spec/javascripts/helpers/.gitkeep +0 -0
  83. data/spec/javascripts/helpers/SpecHelper.js +87 -0
  84. data/spec/javascripts/map_spec.js +49 -0
  85. data/spec/javascripts/markers_methods_spec.js +368 -0
  86. data/spec/javascripts/mocks_spec.js +27 -0
  87. data/spec/javascripts/support/jasmine.yml +76 -0
  88. data/spec/javascripts/support/jasmine_config.rb +23 -0
  89. data/spec/javascripts/support/jasmine_runner.rb +20 -0
  90. data/spec/launchers/all_but_requests.rb +0 -0
  91. data/spec/launchers/all_specs.rb +0 -0
  92. data/spec/launchers/requests.rb +0 -0
  93. data/spec/models/user_spec.rb +447 -0
  94. data/spec/spec_helper.rb +44 -0
  95. data/spec/support/geocoding.rb +14 -0
  96. data/spec/support/matchers.rb +43 -0
  97. metadata +364 -31
  98. data/LICENSE.txt +0 -21
  99. data/app/assets/javascripts/gmaps4rails/all_apis.js +0 -5
  100. data/app/assets/javascripts/gmaps4rails/bing.js +0 -2
  101. data/app/assets/javascripts/gmaps4rails/googlemaps.js +0 -2
  102. data/app/assets/javascripts/gmaps4rails/mapquest.js +0 -2
  103. data/app/assets/javascripts/gmaps4rails/openlayers.js +0 -2
  104. data/app/views/gmaps4rails/_html.html.erb +0 -12
  105. data/app/views/gmaps4rails/_scripts.html.erb +0 -37
  106. data/lib/gmaps4rails/js_handler.rb +0 -112
  107. data/lib/gmaps4rails/json_handler.rb +0 -116
  108. data/public/images/marker.png +0 -0
@@ -34,6 +34,7 @@ class @Gmaps4RailsGoogle extends Gmaps4Rails
34
34
  strokeWeight: 2
35
35
  fillColor: "#000000"
36
36
  fillOpacity: 0.35
37
+ clickable: false
37
38
 
38
39
  #Polyline Styling
39
40
  @polylines_conf = #default style for polylines
@@ -111,7 +112,7 @@ class @Gmaps4RailsGoogle extends Gmaps4Rails
111
112
  markerLatLng = @createLatLng(args.Lat, args.Lng)
112
113
  #Marker sizes are expressed as a Size of X,Y
113
114
  if args.marker_picture == "" and args.rich_marker == null
114
- defaultOptions = {position: markerLatLng, map: @map, title: args.marker_title, draggable: args.marker_draggable}
115
+ defaultOptions = {position: markerLatLng, map: @map, title: args.marker_title, draggable: args.marker_draggable, zIndex: args.zindex}
115
116
  mergedOptions = @mergeObjectWithDefault @markers_conf.raw, defaultOptions
116
117
  return new google.maps.Marker mergedOptions
117
118
 
@@ -123,6 +124,7 @@ class @Gmaps4RailsGoogle extends Gmaps4Rails
123
124
  content: args.rich_marker
124
125
  flat: if args.marker_anchor == null then false else args.marker_anchor[1]
125
126
  anchor: if args.marker_anchor == null then 0 else args.marker_anchor[0]
127
+ zIndex: args.zindex
126
128
  })
127
129
 
128
130
  #default behavior
@@ -132,7 +134,7 @@ class @Gmaps4RailsGoogle extends Gmaps4Rails
132
134
  #create or retrieve existing MarkerImages
133
135
  markerImage = @createOrRetrieveImage(args.marker_picture, args.marker_width, args.marker_height, imageAnchorPosition)
134
136
  shadowImage = @createOrRetrieveImage(args.shadow_picture, args.shadow_width, args.shadow_height, shadowAnchorPosition)
135
- defaultOptions = {position: markerLatLng, map: @map, icon: markerImage, title: args.marker_title, draggable: args.marker_draggable, shadow: shadowImage}
137
+ defaultOptions = {position: markerLatLng, map: @map, icon: markerImage, title: args.marker_title, draggable: args.marker_draggable, shadow: shadowImage, zIndex: args.zindex}
136
138
  mergedOptions = @mergeObjectWithDefault @markers_conf.raw, defaultOptions
137
139
  return new google.maps.Marker mergedOptions
138
140
 
@@ -1,19 +1,20 @@
1
- <% #thanks to enable_css, user can avoid this css to be loaded
2
- if enable_css == true && options[:scripts].nil? %>
3
- <% unless gmaps4rails_pipeline_enabled? %>
4
- <% content_for :head do %>
5
- <%= stylesheet_link_tag 'gmaps4rails' %>
6
- <% end %>
7
- <% end %>
8
- <% end %>
9
-
10
1
  <% content_for :scripts do %>
2
+ <%= javascript_include_tag *js_dependencies %>
11
3
 
12
- <%= gmaps4rails_js_files(options[:map_options], options[:scripts], enable_js, options[:markers].try(:[], :options)) %>
13
4
  <script type="text/javascript" charset="utf-8">
14
5
  <%=raw options.to_gmaps4rails %>
15
6
  </script>
16
-
17
7
  <% end %>
18
8
 
19
- <%= gmaps4rails_html(options[:map_options]) %>
9
+ <% case dom.map_provider %>
10
+ <% when "mapquest" %>
11
+ <div id="<%= dom.map_id %>" style="width:750px; height:475px;">
12
+ <!-- TODO: change this style dynamically -->
13
+ </div>
14
+ <% when "bing" %>
15
+ <div id="<%= dom.map_id %>" class="<%= dom.map_class %>"></div>
16
+ <% else %>
17
+ <div class="<%= dom.container_class %>">
18
+ <div id="<%= dom.map_id %>" class="<%= dom.map_class %>"></div>
19
+ </div>
20
+ <% end %>
@@ -0,0 +1,38 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "gmaps4rails/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "gmaps4rails"
9
+ s.version = Gmaps4rails::VERSION
10
+ s.authors = [%q{Benjamin Roth}, %q{David Ruyer}]
11
+ s.email = [%q{apnea.diving.deep@gmail.com}, %q{david.ruyer@gmail.com}]
12
+ s.homepage = %q{http://github.com/apneadiving/Google-Maps-for-Rails}
13
+ s.summary = %q{Maps made easy for Rails 3}
14
+ s.description = %q{Enables easy display of items (taken from a Rails 3 model) on a Google Maps (JS API V3), OpenLayers, Mapquest and Bing. Geocoding + Directions included.}
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- spec/*`.split("\n")
17
+
18
+ s.add_development_dependency "rails", "~> 3.2.1"
19
+ s.add_development_dependency "sqlite3"
20
+ s.add_development_dependency "jquery-rails"
21
+
22
+ s.add_development_dependency "rspec-rails"
23
+ s.add_development_dependency 'database_cleaner'
24
+ #s.add_development_dependency "capybara"
25
+ #s.add_development_dependency 'launchy'
26
+
27
+ s.add_development_dependency "jasmine"
28
+
29
+ s.add_development_dependency "guard", "~> 1.0.1"
30
+ s.add_development_dependency "guard-spork"
31
+ s.add_development_dependency 'guard-coffeescript'
32
+ s.add_development_dependency 'guard-jasmine'
33
+ s.add_development_dependency "guard-rspec"
34
+
35
+ s.add_development_dependency "factory_girl_rails"
36
+
37
+ s.add_development_dependency 'pry'
38
+ end
data/lib/gmaps4rails.rb CHANGED
@@ -8,14 +8,6 @@ module Gmaps4rails
8
8
  ActionView::Base.send :include, Gmaps4railsHelper
9
9
  end
10
10
 
11
- initializer "add asset directories to pipeline" do |app|
12
- if Rails::VERSION::MAJOR >= 3 && Rails::VERSION::MINOR >= 1
13
- app.config.assets.paths << "#{root}/public/stylesheets"
14
- else
15
- app.middleware.use ::ActionDispatch::Static, "#{root}/public"
16
- end
17
- end
18
-
19
11
  end
20
12
 
21
13
  class Railtie < Rails::Railtie
@@ -8,34 +8,55 @@ module Gmaps4rails
8
8
 
9
9
  module InstanceMethods
10
10
 
11
- # This is a before_filter to trigger the geocoding and save its results
12
-
11
+ # This is a validation method which triggers the geocoding and save its results
13
12
  def process_geocoding
14
13
  #to prevent geocoding each time a save is made
15
- return true if gmaps4rails_options[:check_process] == true && self.send(gmaps4rails_options[:checker]) == true
16
- #try to geocode
14
+ return true if gmaps4rails_prevent_geocoding?
17
15
  begin
18
16
  coordinates = Gmaps4rails.geocode(self.send(gmaps4rails_options[:address]), gmaps4rails_options[:language], false, gmaps4rails_options[:protocol])
19
17
  rescue GeocodeStatus, GeocodeInvalidQuery => e #address was invalid, add error to address.
20
18
  Rails.logger.warn(e)
21
19
  errors[gmaps4rails_options[:address]] << gmaps4rails_options[:msg] if Gmaps4rails.condition_eval(self, gmaps4rails_options[:validation])
22
- rescue GeocodeNetStatus => e #connection error, No need to prevent save.
20
+ rescue GeocodeNetStatus => e #connection error, No need to prevent save.
23
21
  Rails.logger.warn(e)
24
- #TODO add customization here?
25
22
  else #if no exception, save the values
26
- self.send("#{gmaps4rails_options[:lng_column]}=", coordinates.first[:lng]) if self.respond_to?("#{gmaps4rails_options[:lng_column]}=")
27
- self.send("#{gmaps4rails_options[:lat_column]}=", coordinates.first[:lat]) if self.respond_to?("#{gmaps4rails_options[:lat_column]}=")
28
- unless gmaps4rails_options[:normalized_address].nil?
29
- self.send("#{gmaps4rails_options[:normalized_address]}=", coordinates.first[:matched_address])
30
- end
31
- # Call the callback method to let the user do what he wants with the data
32
- self.send(gmaps4rails_options[:callback], coordinates.first[:full_data]) unless gmaps4rails_options[:callback].nil?
33
- if Gmaps4rails.condition_eval(self, gmaps4rails_options[:check_process])
34
- self.send("#{gmaps4rails_options[:checker]}=", true)
35
- end
23
+ self.gmaps4rails_save_data(coordinates)
36
24
  end
37
25
  end
38
-
26
+
27
+ # saves coordinates according to the various options
28
+ def gmaps4rails_save_data(coordinates)
29
+ self.send("#{gmaps4rails_options[:lng_column]}=", coordinates.first[:lng])
30
+ self.send("#{gmaps4rails_options[:lat_column]}=", coordinates.first[:lat])
31
+ # save normalized address if required
32
+ self.send("#{gmaps4rails_options[:normalized_address]}=", coordinates.first[:matched_address]) unless gmaps4rails_options[:normalized_address].nil?
33
+ # Call the callback method to let the user do what he wants with the data
34
+ self.send(gmaps4rails_options[:callback], coordinates.first[:full_data]) unless gmaps4rails_options[:callback].nil?
35
+ # update checker if required
36
+ self.send("#{gmaps4rails_options[:checker]}=", true) if gmaps4rails_check_geocoding?
37
+ end
38
+
39
+ # if process_geocoding is a TrueClass or a FalseClass, 'check_process' and 'checker' play an additional role
40
+ # if process_geocoding is a Proc or a Symbol, 'check_process' and 'checker' are skipped since process_geocoding bears the whole logic
41
+ def gmaps4rails_prevent_geocoding?
42
+ if gmaps4rails_options[:process_geocoding].is_a?(TrueClass) || gmaps4rails_options[:process_geocoding].is_a?(FalseClass)
43
+ return true if !Gmaps4rails.condition_eval(self, gmaps4rails_options[:process_geocoding])
44
+ Gmaps4rails.condition_eval(self, gmaps4rails_options[:check_process]) && self.send("#{gmaps4rails_options[:checker]}") == true
45
+ else
46
+ !Gmaps4rails.condition_eval(self, gmaps4rails_options[:process_geocoding])
47
+ end
48
+ end
49
+
50
+ # Do we have to check the geocoding
51
+ def gmaps4rails_check_geocoding?
52
+ if gmaps4rails_options[:process_geocoding].is_a?(TrueClass) || gmaps4rails_options[:process_geocoding].is_a?(FalseClass)
53
+ Gmaps4rails.condition_eval(self, gmaps4rails_options[:check_process])
54
+ else
55
+ false
56
+ end
57
+ end
58
+
59
+ # creates json for one instance
39
60
  def to_gmaps4rails(&block)
40
61
  json = "["
41
62
  object_json = Gmaps4rails.create_json(self, &block)
@@ -49,26 +70,27 @@ module Gmaps4rails
49
70
 
50
71
  def acts_as_gmappable args = {}
51
72
 
52
- # disable before_filter if explicitly set
53
-
54
- unless args[:process_geocoding] == false
55
- validate :process_geocoding
56
- end
73
+ validate :process_geocoding
57
74
 
58
75
  #instance method containing all the options to configure the behaviour of the gem regarding the current Model
59
76
  define_method "gmaps4rails_options" do
60
77
  {
61
- :lat_column => args[:lat] || "latitude",
62
- :lng_column => args[:lng] || "longitude",
63
- :check_process => args[:check_process].nil? ? true : args[:check_process],
64
- :checker => args[:checker] || "gmaps",
65
- :msg => args[:msg] || "Address invalid",
66
- :validation => args[:validation].nil? ? true : args[:validation],
67
- :normalized_address => args[:normalized_address],
68
- :address => args[:address] || "gmaps4rails_address",
69
- :callback => args[:callback],
70
- :language => args[:language] || "en",
71
- :protocol => args[:protocol] || "http"
78
+ :process_geocoding => args[:process_geocoding].nil? ? true : args[:process_geocoding],
79
+ :check_process => args[:check_process].nil? ? true : args[:check_process],
80
+ :checker => args[:checker] || "gmaps",
81
+
82
+ :lat_column => args[:lat] || "latitude",
83
+ :lng_column => args[:lng] || "longitude",
84
+
85
+ :msg => args[:msg] || "Address invalid",
86
+ :validation => args[:validation].nil? ? true : args[:validation],
87
+
88
+ :language => args[:language] || "en",
89
+ :protocol => args[:protocol] || "http",
90
+
91
+ :address => args[:address] || "gmaps4rails_address",
92
+ :callback => args[:callback],
93
+ :normalized_address => args[:normalized_address]
72
94
  }
73
95
  end
74
96
 
@@ -1,7 +1,8 @@
1
1
  require 'gmaps4rails/acts_as_gmappable'
2
2
 
3
- require 'gmaps4rails/js_handler'
4
- require 'gmaps4rails/json_handler'
3
+ require 'gmaps4rails/js_builder'
4
+ require 'gmaps4rails/json_builder'
5
+ require 'gmaps4rails/view_helper'
5
6
  require 'gmaps4rails/geocoding'
6
7
  require 'gmaps4rails/google_places'
7
8
  require 'gmaps4rails/extensions/array'
@@ -27,20 +28,14 @@ module Gmaps4rails
27
28
 
28
29
  def Gmaps4rails.condition_eval(object, condition)
29
30
  case condition
30
- when Symbol then object.send condition
31
- when Proc then condition.call(object)
32
- when TrueClass then condition
33
- when FalseClass then condition
31
+ when Symbol, String then object.send condition
32
+ when Proc then condition.call(object)
33
+ when TrueClass, FalseClass then condition
34
34
  end
35
35
  end
36
36
 
37
37
  private
38
-
39
- # To create valid js, this method escapes everything but Numeric, true or false
40
- def Gmaps4rails.filter(data)
41
- data.to_json
42
- end
43
-
38
+
44
39
  # get the response from the url encoded address string
45
40
  def Gmaps4rails.get_response(url)
46
41
  url = URI.parse(url)
@@ -1,53 +1,27 @@
1
1
  module Gmaps4railsHelper
2
2
 
3
- # shortcut helper for basic marker display
4
-
5
- def gmaps4rails(builder, enable_css = true, enable_js = true )
3
+ # shortcut helper for marker display with convenient default settings
4
+ # @params [String] builder is a json string
5
+ def gmaps4rails(builder)
6
6
  options = {
7
7
  :map_options => { :auto_adjust => true},
8
8
  :markers => { :data => builder, :options => {:do_clustering => true} }
9
9
  }
10
- render :partial => '/gmaps4rails/gmaps4rails', :locals => { :options => options.with_indifferent_access, :enable_css => enable_css, :enable_js => enable_js }
10
+ gmaps(options)
11
11
  end
12
12
 
13
- # complete helper to pass all variables alongside their options
14
-
15
- def gmaps(options, enable_css = true, enable_js = true )
16
- render :partial => '/gmaps4rails/gmaps4rails', :locals => { :options => options.with_indifferent_access, :enable_css => enable_css, :enable_js => enable_js }
13
+ # full helper to pass all variables and their options
14
+ # @params [Hash] options is a Hash containing data and options. Example: { markers:{ data: @json, options: { do_clustering: true } } }
15
+ def gmaps(options)
16
+ options_with_indifferent_access = options.with_indifferent_access
17
+ view_helper = Gmaps4rails::ViewHelper.new(options_with_indifferent_access)
18
+
19
+ render :partial => '/gmaps4rails/gmaps4rails',
20
+ :locals => {
21
+ :options => options_with_indifferent_access,
22
+ :js_dependencies => view_helper.js_dependencies_array,
23
+ :dom => view_helper.dom_attributes
24
+ }
17
25
  end
18
26
 
19
- def gmaps4rails_js_libraries(libraries_array)
20
- return "" if libraries_array.nil?
21
- "," + libraries_array.join(",")
22
- end
23
-
24
- def gmaps4rails_map_language(map_options)
25
- "language=#{map_options.try(:[], :language) || ''}&hl=#{map_options.try(:[], :hl) || ''}&region=#{map_options.try(:[], :region) || ''}".html_safe
26
- end
27
-
28
- def gmaps4rails_map_id(map_options)
29
- map_options.try(:[], :id) || Gmaps4rails::DEFAULT_MAP_ID
30
- end
31
-
32
- def gmaps4rails_container_class(map_options)
33
- map_options.try(:[], :container_class) || "map_container"
34
- end
35
-
36
- def gmaps4rails_map_class(map_options)
37
- default_class = map_options.try(:[], :provider) == "bing" ? "bing_map" : "gmaps4rails_map"
38
- map_options.try(:[], :class) || default_class
39
- end
40
-
41
-
42
- def gmaps4rails_js_files(map_options = nil, scripts = nil, enable_js = true, marker_options = nil)
43
- render "/gmaps4rails/scripts", :map_options => map_options, :scripts => scripts, :enable_js => enable_js, :marker_options => marker_options
44
- end
45
-
46
- def gmaps4rails_html(map_options = nil)
47
- render "/gmaps4rails/html", :map_options => map_options
48
- end
49
-
50
- def gmaps4rails_pipeline_enabled?
51
- Rails.configuration.methods.include?(:assets) && Rails.configuration.assets.enabled
52
- end
53
27
  end
@@ -0,0 +1,121 @@
1
+ module Gmaps4rails
2
+
3
+ def Gmaps4rails.create_js_from_hash(hash)
4
+ ::Gmaps4rails::JsBuilder.new.create_js_from_hash(hash)
5
+ end
6
+
7
+ class JsBuilder
8
+
9
+ DEFAULT_MAP_ID = "map"
10
+
11
+ #the variable 'hash' must have the following structure
12
+ #{
13
+ # :map_options => hash,
14
+ # :markers => { :data => json, :options => hash },
15
+ # :polylines => { :data => json, :options => hash },
16
+ # :polygons => { :data => json, :options => hash },
17
+ # :circles => { :data => json, :options => hash },
18
+ # :direction => { :data => hash, :options => hash },
19
+ # :kml => { :data => json, :options => hash }
20
+ #}
21
+ #should be with only symbol keys or with indifferent access
22
+ def create_js_from_hash(hash)
23
+ result = Array.new
24
+ map_id = "Gmaps." + get_map_id(hash[:map_options])
25
+
26
+ #means we are creating a new map
27
+ result << "#{map_id} = new #{get_constructor hash[:map_options] }" + ";"
28
+ result << "Gmaps.#{js_function_name hash } = function() {"
29
+ result << create_map_js(hash[:map_options], map_id) unless hash[:map_options].nil?
30
+ result << "#{map_id}.initialize();"
31
+
32
+ hash.each do |category, content| #loop through options hash
33
+ skipped_categories = [:map_options, :last_map, :scripts]
34
+ unless skipped_categories.include? category.to_sym
35
+ if category.to_sym == :direction
36
+ result << create_direction_js(content, map_id)
37
+ else
38
+ result << create_general_js(content, map_id, category)
39
+ end
40
+ end
41
+ end
42
+ result << "#{map_id}.adjustMapToBounds();"
43
+ result << "#{map_id}.callback();"
44
+
45
+ result << "};"
46
+ if hash[:last_map].nil? || hash[:last_map] == true
47
+ result << "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };"
48
+ end
49
+
50
+ result * ('
51
+ ')
52
+ end
53
+
54
+ def js_function_name(hash)
55
+ "load_" + get_map_id(hash[:map_options])
56
+ end
57
+
58
+ def get_map_id(hash)
59
+ hash.nil? || hash[:id].nil? ? DEFAULT_MAP_ID : hash[:id]
60
+ end
61
+
62
+ def get_constructor(hash)
63
+ hash.nil? || hash[:provider].nil? ? "Gmaps4RailsGoogle()" : "Gmaps4Rails#{hash[:provider].capitalize}()"
64
+ end
65
+
66
+ def create_map_js(map_hash, map_id)
67
+ output = Array.new
68
+ skipped_keys = [:class, :container_class]
69
+ map_hash.each do |option_k, option_v|
70
+ unless skipped_keys.include? option_k.to_sym
71
+ case option_k.to_sym
72
+ when :bounds, :raw #particular case, render the content unescaped
73
+ output << "#{map_id}.map_options.#{option_k} = #{option_v};"
74
+ else
75
+ output << "#{map_id}.map_options.#{option_k} = #{option_v.to_json};"
76
+ end
77
+ end
78
+ end
79
+ output
80
+ end
81
+
82
+ def create_general_js(hash, map_id, category)
83
+ output = Array.new
84
+ output << "#{map_id}.#{category} = #{hash[:data]};"
85
+ hash[:options] ||= Array.new
86
+ hash[:options].each do |option_k, option_v|
87
+ if option_k.to_sym == :raw
88
+ output << "#{map_id}.#{category}_conf.#{option_k} = #{option_v};"
89
+ else
90
+ output << "#{map_id}.#{category}_conf.#{option_k} = #{option_v.to_json};"
91
+ end
92
+ end
93
+ output << "#{map_id}.create_#{category}();"
94
+ output
95
+ end
96
+
97
+ def create_direction_js(hash, map_id)
98
+ output = Array.new
99
+ output << "#{map_id}.direction_conf.origin = '#{hash["data"]["from"]}';"
100
+ output << "#{map_id}.direction_conf.destination = '#{hash["data"]["to"]}';"
101
+ hash[:options] ||= Array.new
102
+ hash[:options].each do |option_k, option_v|
103
+ if option_k.to_sym == :waypoints
104
+ waypoints = Array.new
105
+ option_v.each do |waypoint|
106
+ waypoints << { "location" => waypoint, "stopover" => true }.to_json
107
+ end
108
+ output << "#{map_id}.direction_conf.waypoints = [#{waypoints * (",")}];"
109
+ else #option_k != "waypoint"
110
+ output << "#{map_id}.direction_conf.#{option_k} = #{option_v.to_json};"
111
+ end
112
+ end #end .each
113
+ output << "#{map_id}.create_direction();"
114
+ output
115
+ end
116
+
117
+ end
118
+
119
+
120
+ end
121
+