Sha256: 2d8ae8efe6ff45a39e8a12077522275f18f94af48a776e6b3f45c075ebf86254

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Jekyll
  module Maps
    class GoogleMapTag < Liquid::Tag
      JS_LIB_NAME        = "jekyllMaps".freeze
      DEFAULT_MAP_WIDTH  = 600
      DEFAULT_MAP_HEIGHT = 400

      def initialize(_, args, _)
        @args   = OptionsParser.parse(args)
        @finder = LocationFinder.new(@args)
        super
      end

      def render(context)
        locations = @finder.find(context.registers[:site], context.registers[:page])
        @args[:attributes][:id] ||= SecureRandom.uuid

        <<HTML
<div #{render_attributes}></div>
<script type='text/javascript'>
  #{JS_LIB_NAME}.register(
    '#{@args[:attributes][:id]}',
    #{locations.to_json},
    #{map_settings.to_json}
  );
</script>
HTML
      end

      private
      def render_attributes
        attributes = []
        attributes << "id='#{@args[:attributes][:id]}'"
        attributes << render_dimensions
        attributes << render_class if @args[:attributes][:class]
        attributes.join(" ")
      end

      private
      def render_dimensions
        width       = @args[:attributes][:width] || DEFAULT_MAP_WIDTH
        height      = @args[:attributes][:height] || DEFAULT_MAP_HEIGHT
        width_unit  = width.to_s.include?("%") ? "" : "px"
        height_unit = height.to_s.include?("%") ? "" : "px"
        %(style='width:#{width}#{width_unit};height:#{height}#{height_unit};')
      end

      private
      def render_class
        css = @args[:attributes][:class]
        css = css.join(" ") if css.is_a?(Array)
        %(class='#{css}')
      end

      private
      def map_settings
        {
          useCluster: !@args[:flags][:no_cluster],
          showMarkerPopup: @args[:attributes][:show_popup] != 'false'
        }
      end
    end
  end
end

Liquid::Template.register_tag("google_map", Jekyll::Maps::GoogleMapTag)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-maps-1.1.5 lib/jekyll-maps/google_map_tag.rb