Class: Gmaps4rails::JsBuilder
- Inherits:
-
Object
- Object
- Gmaps4rails::JsBuilder
- Defined in:
- lib/gmaps4rails/js_builder.rb
Defined Under Namespace
Classes: Datum
Constant Summary collapse
- DEFAULT_MAP_ID =
"map"
- DATA_KEYS =
[:markers, :polylines, :polygons, :circles, :direction, :kml]
Instance Method Summary collapse
- #create_js ⇒ Object
- #data ⇒ Object
- #gmap_id ⇒ Object
-
#initialize(option_hash) ⇒ JsBuilder
constructor
the ‘option_hash’ must have the following structure { :map_options => hash, :markers => { :data => json, :options => hash }, :polylines => { :data => json, :options => hash }, :polygons => { :data => json, :options => hash }, :circles => { :data => json, :options => hash }, :direction => { :data => hash, :options => hash }, :kml => { :data => json, :options => hash } } should be with only symbol keys or with indifferent access.
- #js_function_name ⇒ Object
- #load_map? ⇒ Boolean
- #map_constructor ⇒ Object
- #map_id ⇒ Object
- #map_options ⇒ Object
- #process_data ⇒ Object
- #process_map_options ⇒ Object
Constructor Details
#initialize(option_hash) ⇒ JsBuilder
the ‘option_hash’ must have the following structure {
:map_options => hash,
:markers => { :data => json, :options => hash },
:polylines => { :data => json, :options => hash },
:polygons => { :data => json, :options => hash },
:circles => { :data => json, :options => hash },
:direction => { :data => hash, :options => hash },
:kml => { :data => json, :options => hash }
} should be with only symbol keys or with indifferent access
18 19 20 21 |
# File 'lib/gmaps4rails/js_builder.rb', line 18 def initialize(option_hash) @js = Array.new @hash = option_hash end |
Instance Method Details
#create_js ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gmaps4rails/js_builder.rb', line 23 def create_js @js << "#{gmap_id} = new #{ map_constructor };" @js << "Gmaps.#{js_function_name} = function() {" @js << "#{gmap_id}.initialize();" process_data @js << "#{gmap_id}.adjustMapToBounds();" @js << "#{gmap_id}.callback();" @js << "};" @js << "Gmaps.oldOnload = window.onload;\n window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };" if load_map? @js * ("\n") end |
#data ⇒ Object
65 66 67 |
# File 'lib/gmaps4rails/js_builder.rb', line 65 def data @hash.select{|key, value| DATA_KEYS.include?(key.to_sym) } end |
#gmap_id ⇒ Object
77 78 79 |
# File 'lib/gmaps4rails/js_builder.rb', line 77 def gmap_id @gmap_id ||= "Gmaps." + map_id end |
#js_function_name ⇒ Object
73 74 75 |
# File 'lib/gmaps4rails/js_builder.rb', line 73 def js_function_name "load_" + map_id end |
#load_map? ⇒ Boolean
69 70 71 |
# File 'lib/gmaps4rails/js_builder.rb', line 69 def load_map? @hash[:last_map].nil? || @hash[:last_map] == true end |
#map_constructor ⇒ Object
85 86 87 |
# File 'lib/gmaps4rails/js_builder.rb', line 85 def map_constructor .try(:[],:provider) ? "Gmaps4Rails#{[:provider].capitalize}()" : "Gmaps4RailsGoogle()" end |
#map_id ⇒ Object
81 82 83 |
# File 'lib/gmaps4rails/js_builder.rb', line 81 def map_id @map_id ||= .try(:[],:id) || DEFAULT_MAP_ID end |
#map_options ⇒ Object
61 62 63 |
# File 'lib/gmaps4rails/js_builder.rb', line 61 def @hash[:map_options] end |
#process_data ⇒ Object
54 55 56 57 58 59 |
# File 'lib/gmaps4rails/js_builder.rb', line 54 def process_data data.each do |name, hash| datum = ::Gmaps4rails::JsBuilder::Datum.new(gmap_id, name, hash) @js.concat datum.create_js end end |
#process_map_options ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gmaps4rails/js_builder.rb', line 41 def return unless .each do |option_key, option_value| next if [:class, :container_class].include? option_key.to_sym case option_key.to_sym when :bounds, :raw #particular case, render the content unescaped @js << "#{gmap_id}.map_options.#{option_key} = #{option_value};" else @js << "#{gmap_id}.map_options.#{option_key} = #{option_value.to_json};" end end end |