Skip to content

Commit

Permalink
final touch before release
Browse files Browse the repository at this point in the history
  • Loading branch information
apneadiving committed Aug 30, 2011
1 parent fd6d1a1 commit 06c9bbf
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 54 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ To make Rails serve the assets (javascripts, stylesheets and marker images) you

* //= require gmaps4rails/openlayers.js

* //= require gmaps4rails/all_apis.js

You even don't need the `yield :head` if you include `require gmaps4rails` in your CSS manifest.

- if you are using Rails 3.0:
Expand Down
19 changes: 10 additions & 9 deletions lib/gmaps4rails/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def Gmaps4rails.create_js_from_hash(hash, edit_map_with_id = false)
# - "map_options" and "scripts" don't contain interesting data for the view
# - "direction" has a hash as a data and waypoints options must be processed properly
#
#TODO: clean up this method
result = Array.new
map_id = edit_map_with_id || Gmaps4rails.get_map_id(hash[:map_options])
map_id = "Gmaps." + map_id
Expand All @@ -212,7 +213,7 @@ def Gmaps4rails.create_js_from_hash(hash, edit_map_with_id = false)
#extract map_options
unless hash[:map_options].nil?
hash[:map_options].each do |option_k, option_v|
if option_k == "bounds" #particular case
if option_k.to_sym == :bounds #particular case
result << "#{map_id}.map_options.#{option_k} = #{option_v};"
else
result << "#{map_id}.map_options.#{option_k} = #{Gmaps4rails.filter option_v};"
Expand All @@ -224,12 +225,9 @@ def Gmaps4rails.create_js_from_hash(hash, edit_map_with_id = false)
end

hash.each do |category, content| #loop through options hash
case category
when "map_options"
#already taken into account above => nothing to do here
when "scripts"
#nothing to do
when "direction"
if [:map_options, :last_map, :scripts].include? category.to_sym
#nothing to do
elsif category.to_sym == :direction
result << "#{map_id}.direction_conf.origin = '#{content["data"]["from"]}';"
result << "#{map_id}.direction_conf.destination = '#{content["data"]["to"]}';"

Expand All @@ -246,7 +244,7 @@ def Gmaps4rails.create_js_from_hash(hash, edit_map_with_id = false)
end
end #end .each
result << "#{map_id}.create_direction();"
else #default behaviour in case condition
else #default behaviour
result << "#{map_id}.#{category} = #{content[:data]};"
content[:options] ||= Array.new
content[:options].each do |option_k, option_v|
Expand All @@ -258,7 +256,10 @@ def Gmaps4rails.create_js_from_hash(hash, edit_map_with_id = false)
result << "#{map_id}.callback();"

if edit_map_with_id == false
result << "};\nwindow.onload = #{Gmaps4rails.js_function_name(hash)};"
result << "};\nGmaps.mapsToLoad.push('#{Gmaps4rails.js_function_name(hash)}');"
if hash[:last_map].nil? || hash[:last_map] == true
result << "window.onload = Gmaps.loadMaps;"
end
end

result * ('
Expand Down
5 changes: 5 additions & 0 deletions public/javascripts/gmaps4rails/all_apis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//= require ./gmaps4rails.base.js
//= require ./gmaps4rails.googlemaps.js
//= require ./gmaps4rails.mapquest.js
//= require ./gmaps4rails.openlayers.js
//= require ./gmaps4rails.bing.js
9 changes: 7 additions & 2 deletions public/javascripts/gmaps4rails/gmaps4rails.base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
var Gmaps = {};
var Gmaps = {mapsToLoad: []};

Gmaps.loadMaps = function(){
for (var i = 0; i < Gmaps.mapsToLoad.length; ++i) {
window[Gmaps.mapsToLoad[i]]();
}
};

function Gmaps4Rails() {

Expand Down Expand Up @@ -431,7 +437,6 @@ function Gmaps4Rails() {
if (this.map_options.auto_adjust) {
//from markers
this.extendBoundsWithMarkers();

//from polylines:
for (var i = 0; i < this.polylines.length; ++i) {
var polyline_points = this.polylines[i].serviceObject.latLngs.getArray()[0].getArray();
Expand Down
60 changes: 32 additions & 28 deletions test/dummy/app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,19 @@ var builder3 = [{
"markers" => {"data" => @json}
}) %>
<%#= gmaps(@options) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => true } },
:map_options => { :auto_adjust => true, :zoom => 5 }) %>
<%#= gmaps(:markers => {:data => '[{ "lng": "5.9311119", "lat": "43.1251606"} ,{ "lng": "2.3509871", "lat": "48.8566667" },{ "lng": "0", "lat": "0" } ]', "options" => {"do_clustering" => true, "draggable" => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5 },
:scripts => "none" ) %>
<%#= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => true, :draggable => true } },
:map_options => { :auto_adjust => true, :zoom => 5 },
:last_map => false) %>
<%= gmaps(:markers => {:data => '[{ "lng": "5.9311119", "lat": "43.1251606"} ,{ "lng": "2.3509871", "lat": "48.8566667" },{ "lng": "0", "lat": "0" } ]',
"options" => {"do_clustering" => true, "draggable" => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "ed" },
:scripts => "none",
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :provider => "openlayers", :id => "joe" },
:scripts => "api" ) %>
<%#= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:scripts => "api",
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe3", :provider => "mapquest", :provider_key => MAPQUEST_KEY },
:scripts => "api" ) %>
<%#= gmaps(:markers => {"data" => @json, :options => {:do_clustering => false, :draggable => true } },
Expand Down Expand Up @@ -164,26 +168,26 @@ var builder3 = [{
// };
</script>
<script type="text/javascript" charset="utf-8">
//
// Gmaps.map.HandleDragend = function(pos) {
// var geocoder = new google.maps.Geocoder();
// geocoder.geocode({
// latLng: pos
// }, function(responses) {
// if (responses && responses.length > 0) {
// alert(responses[0].formatted_address);
// } else {
// alert('Cannot determine address at this location.');
// }
// });
// };
//
//
// Gmaps.map.callback = function() {
// for (var i = 0; i < this.markers.length; ++i) {
// google.maps.event.addListener(Gmaps.map.markers[i].serviceObject, 'dragend', function() { Gmaps.map.HandleDragend(this.getPosition()) });
// }
// };
Gmaps.map.HandleDragend = function(pos) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
alert(responses[0].formatted_address);
} else {
alert('Cannot determine address at this location.');
}
});
};


Gmaps.map.callback = function() {
for (var i = 0; i < this.markers.length; ++i) {
google.maps.event.addListener(Gmaps.map.markers[i].serviceObject, 'dragend', function() { Gmaps.map.HandleDragend(this.getPosition()); });
}
};
</script>
<% end %>
<div> style="margin-top: 500px;"
Expand Down
11 changes: 10 additions & 1 deletion test/dummy/app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
<p>
<script type="text/javascript" charset="utf-8">
function testMe(){alert('first')};

window.onload = testMe;

function second(){alert('second')};

window.onload = second;

</script><p>
<strong>Name:</strong>
<%= @user.name %>
</p>
Expand Down
14 changes: 14 additions & 0 deletions test/dummy/spec/base/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
result.should include "Gmaps.map.kml = [{ url: \"http://www.searcharoo.net/SearchKml/newyork.kml\"}, { url: \"http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml\", options: {clickable: false } }];\nGmaps.map.create_kml();"
#callback
result.should include "Gmaps.map.callback();"
#launch array
result.should include "Gmaps.mapsToLoad.push('load_map');"
end


Expand All @@ -123,6 +125,18 @@
result.should include "Gmaps.id_foo"
result.should_not include "Gmaps.map"
end

it "should not call map builder if not last_map" do
hash = {:last_map => false}
hash.to_gmaps4rails.should_not include "window.onload = Gmaps.loadMaps;"
end

it "should call map builder if last_map" do
hash = {:last_map => true}
hash.to_gmaps4rails.should include "window.onload = Gmaps.loadMaps;"
hash = {}
hash.to_gmaps4rails.should include "window.onload = Gmaps.loadMaps;"
end
end

describe "Destination" do
Expand Down
14 changes: 14 additions & 0 deletions test/dummy/spec/javascripts/basic_methods_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,18 @@ describe("Gmaps4Rails", function() {
});
});
});
});

function test1(){};
function test2() {};
describe("Gmaps", function() {
it("should initialize all requested maps", function() {
spyOn(window, "test1");
spyOn(window, "test2");
Gmaps.mapsToLoad = ["test1", "test2"];
Gmaps.loadMaps();
expect(window.test1).toHaveBeenCalled();
expect(window.test2).toHaveBeenCalled();
});

});
1 change: 1 addition & 0 deletions test/dummy31/.rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm 1.9.2@rails31
2 changes: 1 addition & 1 deletion test/dummy31/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ gem 'jquery-rails'
# gem 'capistrano'

# To use debugger
gem 'ruby-debug'
#gem 'ruby-debug'

12 changes: 1 addition & 11 deletions test/dummy31/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../../
specs:
gmaps4rails (0.11.0)
gmaps4rails (0.11.1)
crack

GEM
Expand Down Expand Up @@ -48,7 +48,6 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.1.2)
columnize (0.3.4)
crack (0.1.8)
erubis (2.7.0)
execjs (1.2.4)
Expand All @@ -59,8 +58,6 @@ GEM
railties (~> 3.0)
thor (~> 0.14)
json (1.5.3)
linecache (0.46)
rbx-require-relative (> 0.0.4)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand Down Expand Up @@ -93,13 +90,7 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
rbx-require-relative (0.0.5)
rdoc (3.9.1)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
sass (3.1.7)
sass-rails (3.1.0.rc.5)
actionpack (~> 3.1.0.rc1)
Expand Down Expand Up @@ -131,7 +122,6 @@ DEPENDENCIES
jquery-rails
json
rails (= 3.1.0.rc5)
ruby-debug
sass-rails (~> 3.1.0.rc)
sqlite3
uglifier
2 changes: 1 addition & 1 deletion test/dummy31/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require gmaps4rails/googlemaps.js
//= require gmaps4rails/all_apis.js
21 changes: 20 additions & 1 deletion test/dummy31/app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,23 @@

<%= link_to 'New User', new_user_path %>

<%= gmaps4rails(@json) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => true } },
:map_options => { :auto_adjust => true, :zoom => 5 },
:last_map => false) %>
<%= gmaps(:markers => {:data => '[{ "lng": "5.9311119", "lat": "43.1251606"} ,{ "lng": "2.3509871", "lat": "48.8566667" },{ "lng": "0", "lat": "0" } ]', "options" => {"do_clustering" => true, "draggable" => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "ed" },
:scripts => "none",
:last_map => false ) %>
<%= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :provider => "openlayers", :id => "joe" },
:scripts => "api" ) %>
<%#= gmaps(:markers => {:data => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe3", :provider => "mapquest", :provider_key => MAPQUEST_KEY },
:scripts => "api" ) %>
<%#= gmaps(:markers => {"data" => @json, :options => {:do_clustering => false, :draggable => true } },
:map_options => { :auto_adjust => true, :center_on_user => true, :zoom => 5, :id => "joe2", :provider => "bing", :provider_key => BING_KEY },
:scripts => "api" ) %>
<%#= gmaps({
"map_options" => { "auto_adjust" => false, "zoom" => 0, "libraries" => ["places"] },
"markers" => { "data" => @json }
}, true, true) %>

0 comments on commit 06c9bbf

Please sign in to comment.