Class: Gmaps4rails::JsBuilder

Inherits:
Object
  • Object
show all
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

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_jsObject



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() {"
  
  process_map_options
  
  @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

#dataObject



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_idObject



77
78
79
# File 'lib/gmaps4rails/js_builder.rb', line 77

def gmap_id
  @gmap_id ||= "Gmaps." + map_id
end

#js_function_nameObject



73
74
75
# File 'lib/gmaps4rails/js_builder.rb', line 73

def js_function_name
  "load_" + map_id
end

#load_map?Boolean

Returns:

  • (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_constructorObject



85
86
87
# File 'lib/gmaps4rails/js_builder.rb', line 85

def map_constructor
  map_options.try(:[],:provider) ? "Gmaps4Rails#{map_options[:provider].capitalize}()" : "Gmaps4RailsGoogle()"
end

#map_idObject



81
82
83
# File 'lib/gmaps4rails/js_builder.rb', line 81

def map_id
  @map_id ||= map_options.try(:[],:id) || DEFAULT_MAP_ID
end

#map_optionsObject



61
62
63
# File 'lib/gmaps4rails/js_builder.rb', line 61

def map_options
  @hash[:map_options]
end

#process_dataObject



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_optionsObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gmaps4rails/js_builder.rb', line 41

def process_map_options
  return unless map_options
  map_options.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