Skip to content

Commit

Permalink
google-map: implement google map tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ayastreb committed Jul 17, 2016
1 parent 1f586cd commit 0431356
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 2 deletions.
58 changes: 58 additions & 0 deletions lib/jekyll-maps/google_map.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<div id='google-map'></div>

<script type='text/javascript'>
var jekyllMaps = (function () {
'use strict';

return {
loadScript: function (url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.body.appendChild(script);
},
initialize: function () {
this.options = {
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 3
}
this.map = new google.maps.Map(document.getElementById('google-map'), this.options);
this.showMarkers({{ locations }});
},
showMarkers: function (locations) {
var map = this.map,
bounds = new google.maps.LatLngBounds(),
markers = locations.map(function (location) {
var position = new google.maps.LatLng(location.latitude, location.longitude);
var marker = new google.maps.Marker({
position: position,
map: map,
title: location.title,
url: location.url
});

marker.addListener('click', function () {
if (this.url) {
window.location.href = this.url;
}
});
bounds.extend(position);

return marker;
});

new MarkerClusterer(map, markers, {
gridSize: 25,
imagePath: 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
});
map.fitBounds(bounds);
},
};
}());

window.onload = function () {
jekyllMaps.loadScript('https://googlemaps.github.io/js-marker-clusterer/src/markerclusterer.js');
jekyllMaps.loadScript('http://maps.googleapis.com/maps/api/js?callback=jekyllMaps.initialize');
};
</script>
57 changes: 55 additions & 2 deletions lib/jekyll-maps/google_map_tag.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
module Jekyll
module Maps
class GoogleMapTag < Liquid::Tag
def initialize(_tag_name, text, _tokens)
attr_accessor :context

def initialize(_, args, _)
unless args.empty?
@filter_key = args.split(":").first.strip
@filter_value = args.split(":").last.strip
end

super
end

def render(_context)
def render(context)
@context = context
template.render!({ "locations" => locations.to_json })
end

private
def locations
filter_posts.map do |post|
{
:latitude => post["location"]["latitude"],
:longitude => post["location"]["longitude"],
:title => post["title"],
:url => post.url
}
end
end

def filter_posts
posts = context.registers[:site].posts.docs.reject do |post|
post["location"].nil? || post["location"].empty?
end
if @filter_key
posts.reject do |post|
post[@filter_key].nil? || post[@filter_key] != @filter_value
end
else
posts
end
end

private
def template
@template ||= Liquid::Template.parse template_contents
end

private
def template_contents
@template_contents ||= begin
File.read(template_path)
end
end

private
def template_path
@template_path ||= begin
File.expand_path "./google_map.html", File.dirname(__FILE__)
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/fixtures/_posts/2016-07-01-london.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: London
country: gb
location:
latitude: 51.5285582
longitude: -0.2416807
---

London is a capital of Great Britain.
9 changes: 9 additions & 0 deletions spec/fixtures/_posts/2016-07-02-berlin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Berlin
country: de
location:
latitude: 52.5072111
longitude: 13.1449604
---

Berlin is in Germany.
5 changes: 5 additions & 0 deletions spec/fixtures/_posts/2016-07-03-no-location.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: post without location
---

Nothing to see here.
7 changes: 7 additions & 0 deletions spec/fixtures/page_filter_attribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Only include posts with matching attribute
---

**With Filters**

{% google_map country:de %}
7 changes: 7 additions & 0 deletions spec/fixtures/page_no_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Simple map without options
---

**Jekyll Google Maps Plugin**

{% google_map %}
35 changes: 35 additions & 0 deletions spec/jekyll_google_map_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "spec_helper"

describe Jekyll::Maps::GoogleMapTag do
let(:site) { make_site }

before do
site.process
end

context "tag without options" do
let(:content) { File.read(dest_dir("page_no_options.html")) }

it "builds javascript" do
expect(content).to match(%r!jekyllMaps!)
end

it "finds posts with location" do
expect(content).to match(%r!Berlin!)
expect(content).to match(%r!London!)
end

it "skips posts without location" do
expect(content).not_to match(%r!post without location!)
end
end

context "tag with attribute filter" do
let(:content) { File.read(dest_dir("page_filter_attribute.html")) }

it "finds only post with matching attribute" do
expect(content).to match(%r!Berlin!)
expect(content).not_to match(%r!London!)
end
end
end
33 changes: 33 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "jekyll"
require "jekyll-maps"

Jekyll.logger.log_level = :error

RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.order = "random"

SOURCE_DIR = File.expand_path("../fixtures", __FILE__)
DEST_DIR = File.expand_path("../dest", __FILE__)

def source_dir(*files)
File.join(SOURCE_DIR, *files)
end

def dest_dir(*files)
File.join(DEST_DIR, *files)
end

CONFIG_DEFAULTS = {
"source" => source_dir,
"destination" => dest_dir,
"gems" => ["jekyll-maps"]
}.freeze

def make_site(options = {})
site_config = Jekyll.configuration CONFIG_DEFAULTS.merge(options)
Jekyll::Site.new(site_config)
end
end

0 comments on commit 0431356

Please sign in to comment.