Skip to content

Commit

Permalink
Refactored Google::Pane
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilio Karadanais committed Nov 8, 2008
1 parent 93ba428 commit 57554c7
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 140 deletions.
16 changes: 1 addition & 15 deletions NOTES.textile
Expand Up @@ -4,19 +4,12 @@ h2. N.B:
or a map with a name other than map. Suggest making the Google::Map avaible globally through some interface.

* Write "Eschaton showcase" to show all samples code and allow for some form of user contribution.

* rake update should not delete the parent plugin directory but rather its contents, then the repo should
be cloned into that dir(is this possible with git??)

* Track the "maps" that the user has created by keeping a hash by name, the main map being called "main".
Other objects can then refer to these maps using the abstraction

h2. In:

* Make Google::InfoWindow shareable between map and marker! update #open_info_window on both Map and Marker
Google::InfoWindow must also bring convenience the form of Google::InfoWindow#hide, Google::InfoWindow#show
Google::InfoWindow#toggle

* Support yaml files for map keys, so in one place and no need to edit dev.rb or prod.rb

h2. Intuitive Marker updates
Expand All @@ -34,12 +27,7 @@ h2. Eschaton general
* Eschaton seems to be loading up *twice* during startup in rails 2.1, investigate!

h2. Eschaton dev

* Make Google::InfoWindow shareable between map and marker! update #open_info_window on both Map and Marker

* #if_google_compatible should be merged into #google_map_script and #google_map_script should *always* be used.
There should never be a need to use it outside of this context.

* Move Pane code into eschaton.js and metafy as need be!

* See Google::Circle and refactor into various 'shape' classes.
Expand All @@ -52,9 +40,7 @@ h2. Eschaton Enhancments
* map.add_lines :from => location, :to => [other_location, another_location], :thickness => 10, :opacity => 0.7

* Map#add_geo_rss(url) : Render a geo rss feed on the map

* Implement Line#undo_last_vertex


* Keyboard navigation needs focus on div, sort this, how ???

h2. Higher level ideas, trippy shit
Expand Down
30 changes: 26 additions & 4 deletions generators/map/templates/eschaton.js
Expand Up @@ -6,7 +6,6 @@ function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, f
var latConv = center.distanceFrom(new GLatLng(center.lat() +0.1, center.lng()))/100;
var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

//Loop
var points = [];
var step = parseInt(360/nodes);
for(var i=0; i<=360; i+=step){
Expand All @@ -25,8 +24,7 @@ function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, f
return poly;
}

/**
* Modified by yawningman to work with eschaton
/* Modified by yawningman to work with eschaton
* For original see http://onemarco.com/2007/05/16/custom-tooltips-for-google-maps/
*
* Original Author = Marco Alionso Ramirez, marco@onemarco.com
Expand Down Expand Up @@ -128,4 +126,28 @@ Tooltip.prototype.show = function(){
Tooltip.prototype.hide = function(){
this.div_.style.visibility = 'hidden';
}
/* end tooltip */
/* end tooltip */

/* GooglePane - A simple pane for google maps
by yawningman */
function GooglePane(options){
this.default_position = options['position']

this.panel = document.createElement('div');
this.panel.id = options['id'];
this.panel.className = options['cssClass']
this.panel.innerHTML = options['text']
}

GooglePane.prototype = new GControl;
GooglePane.prototype.initialize = function(map) {
map.getContainer().appendChild(this.panel);

return this.panel;
};

GooglePane.prototype.getDefaultPosition = function() {
return this.default_position;
};

/* end pane */
20 changes: 12 additions & 8 deletions slices/google_maps/core_ext.rb
Expand Up @@ -32,16 +32,20 @@ def to_google_anchor

class Hash # :nodoc:

def to_google_position
self.default! :offset => [0, 0]

"new GControlPosition(#{self[:anchor].to_google_anchor}, #{self[:offset].to_google_size})"
end

def to_google_options
# ==== Options
# * +dont_convert+ - An array of keys that should *_not_* be converted to javascript.
def to_google_options(options = {})
dont_convert = (options[:dont_convert] || []).collect(&:to_s)
string_keys = self.stringify_keys

args = string_keys.keys.sort.collect do |key|
"#{key.to_js_method}: #{string_keys[key].to_js}"
value = if key.in?(dont_convert)
string_keys[key]
else
string_keys[key].to_js
end

"#{key.to_js_method}: #{value}"
end

"{#{args.join(', ')}}"
Expand Down
2 changes: 1 addition & 1 deletion slices/google_maps/google/map.rb
Expand Up @@ -209,7 +209,7 @@ def zoom=(zoom)
# map.add_control :map_type, :position => {:anchor => :top_left, :offset => [10, 10]}
def add_control(control, options = {})
control = "new #{control.to_google_control}()" if control.is_a?(Symbol)
position = options[:position].to_google_position if options[:position]
position = Google::OptionsHelper.to_google_position options[:position]
arguments = [control, position].compact

script << "#{self.var}.addControl(#{arguments.join(', ')});"
Expand Down
66 changes: 19 additions & 47 deletions slices/google_maps/google/pane.rb
@@ -1,60 +1,32 @@
module Google

class Pane < MapObject

# :style, :text, :partial

#
# ==== Options:
#
# * +text+ - Optional
# * +partial+ - Optional
#
# * +css_class+, Optional, defaulted to 'pane'
# * +anchor+ - Optional, defaulted to +top_left+
# * +offset+ - Optional, defaulted to [10, 10]
def initialize(options = {})
options.default! :var => 'pane', :style => {}, :anchor => :top_left,
:position_offset => [10, 10]

style = options[:style]
style.default! :width => :auto, :height => :auto, :background_color => "#fff",
:border => "1px solid #808080", :opacity => 1
options.default! :var => 'pane', :css_class => 'pane', :anchor => :top_left,
:offset => [10, 10]

anchor = options.extract(:anchor).to_google_anchor
position_offset = options.extract(:position_offset).to_google_size

super

text = if options[:partial]
Eschaton.current_view.render options
else
options[:text]
end

if create_var?
self << "
function MyPane(){}
MyPane.prototype = new GControl;
MyPane.prototype.initialize = function(map) {
var me = this;
me.panel = document.createElement('div');
me.panel.id = '#{self.var}';
me.panel.style.width = '#{style[:width]}';
me.panel.style.height = '#{style[:height]}';
me.panel.style.backgroundColor = '#{style[:background_color]}';
me.panel.style.border = '#{style[:border]}';
me.panel.style.opacity = '#{style[:opacity]}';
me.panel.innerHTML = #{text.to_js};
map.getContainer().appendChild(me.panel);
pane_options = {}

return me.panel;
};
pane_options[:id] = self.var
pane_options[:position] = OptionsHelper.to_google_position options
pane_options[:text] = OptionsHelper.to_content options
pane_options[:css_class] = options[:css_class].to_s

MyPane.prototype.getDefaultPosition = function() {
return new GControlPosition(#{anchor}, #{position_offset});
};
MyPane.prototype.getPanel = function() {
return me.panel;
};
"

self << "#{self.var} = new MyPane();"
if create_var?
google_options = pane_options.to_google_options(:dont_convert => [:position])
self << "#{self.var} = new GooglePane(#{google_options});"
end
end

Expand Down
8 changes: 8 additions & 0 deletions slices/google_maps/options_helper.rb
Expand Up @@ -88,6 +88,14 @@ def self.to_gravatar_icon(options)
Google::GravatarIcon.new options
end
end

def self.to_google_position(options)
if options.not_blank?
options.default! :anchor => :top_right, :offset => [0, 0]

"new GControlPosition(#{options[:anchor].to_google_anchor}, #{options[:offset].to_google_size})"
end
end

end

Expand Down
73 changes: 8 additions & 65 deletions test/core_ext_test.rb
Expand Up @@ -10,60 +10,11 @@ def teardown
JavascriptObject.global_script = nil
end

def test_to_google_position
assert_equal 'new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0))',
({:anchor => :top_left}).to_google_position
assert_equal 'new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 50))',
({:anchor => :top_left, :offset => [10, 50]}).to_google_position
assert_equal 'new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10))',
({:anchor => :top_right, :offset => [10, 10]}).to_google_position
end

def test_array_to_google_size
assert_equal "new GSize(10, 10)", [10, 10].to_google_size
assert_equal "new GSize(100, 50)", [100, 50].to_google_size
assert_equal "new GSize(200, 150)", [200, 150].to_google_size
end

def test_to_image
assert_equal "/images/green.png", Google::OptionsHelper.to_image(:green)
assert_equal "/images/green.png", Google::OptionsHelper.to_image("/images/green.png")
end

def test_options_helper_to_icon
assert_equal Google::Icon, Google::OptionsHelper.to_icon("green").class
assert_equal Google::Icon, Google::OptionsHelper.to_icon(:green).class
assert_equal Google::Icon, Google::OptionsHelper.to_icon({:image => :green}).class
end

def test_to_gravatar_icon
assert_equal Google::GravatarIcon, Google::OptionsHelper.to_gravatar_icon(:email_address => 'joesoap@email.com').class
assert_equal Google::GravatarIcon, Google::OptionsHelper.to_gravatar_icon('joesoap@email.com').class
end

def test_options_helper_to_location
assert_equal "location", Google::OptionsHelper.to_location("location")
assert_equal "map.getCenter()", Google::OptionsHelper.to_location("map.getCenter()")
assert_equal :location, Google::OptionsHelper.to_location(:location)
assert_equal :marker_location, Google::OptionsHelper.to_location(:marker_location)

location = Google::Location.new(:latitide => 34, :longitude => 18)
assert_equal location, Google::OptionsHelper.to_location(location)

location_hash = {:latitide => 34, :longitude => 18}
location = Google::OptionsHelper.to_location(location_hash)

assert_equal Google::Location, location.class
assert_equal location_hash[:latitude], location.latitude
assert_equal location_hash[:longitude], location.longitude

location_array = [18, 34]
location = Google::OptionsHelper.to_location(location_array)

assert_equal Google::Location, location.class
assert_equal location_array.first, location.latitude
assert_equal location_array.second, location.longitude
end

def test_to_google_control
assert_equal :GSmallMapControl, :small_map.to_google_control
Expand All @@ -87,27 +38,19 @@ def test_to_google_anchor

def test_to_google_options
assert_equal '{bounceGravity: 12, draggable: true, title: "My title!"}',
{:draggable => true, :bounce_gravity => 12, :title => "My title!"}.to_google_options
{:draggable => true, :bounce_gravity => 12, :title => "My title!"}.to_google_options

assert_equal '{dragCrossMove: true, icon: local_icon}',
{:drag_cross_move => true, :icon => :local_icon}.to_google_options
{:drag_cross_move => true, :icon => :local_icon}.to_google_options

assert_equal '{color: "red", opacity: 0.7, weight: 10}',
{:color => 'red', :weight => 10, :opacity => 0.7}.to_google_options
end

def test_to_marker
marker = Google::Marker.new(:location => {:latitude => -33.947, :longitude => 18.462})

assert_equal marker, Google::OptionsHelper.to_marker(marker)
assert_equal Google::Marker, Google::OptionsHelper.to_marker(:location => :existing).class
end
{:color => 'red', :weight => 10, :opacity => 0.7}.to_google_options

def test_to_line
line = Google::Line.new(:vertices => :first_location)
assert_equal '{color: "blue", offset: new GSize(10, 10)}',
{:color => 'blue', :offset => [10, 10].to_google_size}.to_google_options(:dont_convert => [:offset])

assert_equal line, Google::OptionsHelper.to_line(line)
assert_equal Google::Line, Google::OptionsHelper.to_line(:vertices => :first_location).class
assert_equal '{color: "blue", zone: zz1}',
{:color => 'blue', :zone => 'zz1'}.to_google_options(:dont_convert => [:zone])
end

end

0 comments on commit 57554c7

Please sign in to comment.