Add Google Map of voters
[selectricity-live] / vendor / plugins / ym4r_gm / lib / gm_plugin / overlay.rb
diff --git a/vendor/plugins/ym4r_gm/lib/gm_plugin/overlay.rb b/vendor/plugins/ym4r_gm/lib/gm_plugin/overlay.rb
new file mode 100644 (file)
index 0000000..5e0953a
--- /dev/null
@@ -0,0 +1,386 @@
+module Ym4r\r
+  module GmPlugin\r
+    #A graphical marker positionned through geographic coordinates (in the WGS84 datum). An HTML info window can be set to be displayed when the marker is clicked on.\r
+    class GMarker\r
+      include MappingObject\r
+      attr_accessor :point, :options, :info_window, :info_window_tabs, :address\r
+      #The +points+ argument can be either a GLatLng object or an array of 2 floats. The +options+ keys can be: <tt>:icon</tt>, <tt>:clickable</tt>, <tt>:title</tt>, <tt>:info_window</tt> and <tt>:info_window_tabs</tt>, as well as <tt>:max_width</tt>. The value of the +info_window+ key is a string of HTML code that will be displayed when the markers is clicked on. The value of the +info_window_tabs+ key is an array of GInfoWindowTab objects or a hash directly, in which case it will be transformed to an array of GInfoWindowTabs, with the keys as the tab headers and the values as the content.\r
+      def initialize(position, options = {})\r
+        if position.is_a?(Array)\r
+          @point = GLatLng.new(position)\r
+        elsif position.is_a?(String)\r
+          @point = Variable.new("INVISIBLE") #default coordinates: won't appear anyway\r
+          @address = position\r
+        else\r
+          @point = position\r
+        end\r
+        @info_window = options.delete(:info_window)\r
+        @info_window_tabs = options.delete(:info_window_tabs)\r
+        if options.has_key?(:max_url)\r
+          @info_window_options = {:max_url => options.delete(:max_url) } \r
+        else\r
+          @info_window_options = {}\r
+        end\r
+        @options = options\r
+      end\r
+      #Creates a marker: If an info_window or info_window_tabs is present, the response to the click action from the user is setup here.\r
+      def create\r
+        if @options.empty?\r
+          creation = "new GMarker(#{MappingObject.javascriptify_variable(@point)})"\r
+        else\r
+          creation = "new GMarker(#{MappingObject.javascriptify_variable(@point)},#{MappingObject.javascriptify_variable(@options)})"\r
+        end\r
+        if @info_window && @info_window.is_a?(String)\r
+          creation = "addInfoWindowToMarker(#{creation},#{MappingObject.javascriptify_variable(@info_window)},#{MappingObject.javascriptify_variable(@info_window_options)})"\r
+        elsif @info_window_tabs && @info_window_tabs.is_a?(Hash)\r
+          creation = "addInfoWindowTabsToMarker(#{creation},#{MappingObject.javascriptify_variable(@info_window_tabs.to_a.collect{|kv| GInfoWindowTab.new(kv[0],kv[1] ) })},#{MappingObject.javascriptify_variable(@info_window_options)})"\r
+        elsif @info_window_tabs \r
+          creation = "addInfoWindowTabsToMarker(#{creation},#{MappingObject.javascriptify_variable(Array(@info_window_tabs))},#{MappingObject.javascriptify_variable(@info_window_options)})"\r
+        end\r
+        if @address.nil?\r
+          creation\r
+        else\r
+          "addGeocodingToMarker(#{creation},#{MappingObject.javascriptify_variable(@address)})"\r
+        end\r
+      end\r
+    end\r
+    \r
+    #Represents a tab to be displayed in a bubble when a marker is clicked on.\r
+    class GInfoWindowTab < Struct.new(:tab,:content)\r
+      include MappingObject\r
+      def create\r
+        "new GInfoWindowTab(#{MappingObject.javascriptify_variable(tab)},#{MappingObject.javascriptify_variable(content)})"\r
+      end\r
+    end\r
+        \r
+    #Represents a definition of an icon. You can pass rubyfied versions of the attributes detailed in the Google Maps API documentation. You can initialize global icons to be used in the application by passing a icon object, along with a variable name, to GMap#icon_init. If you want to declare an icon outside this, you will need to declare it first, since the JavaScript constructor does not accept any argument.\r
+    class GIcon\r
+      include MappingObject\r
+      DEFAULT = Variable.new("G_DEFAULT_ICON")\r
+      attr_accessor :options, :copy_base\r
+\r
+      #Options can contain all the attributes (in rubyfied format) of a GIcon object (see Google's doc), as well as <tt>:copy_base</tt>, which indicates if the icon is copied from another one.\r
+      def initialize(options = {})\r
+        @copy_base = options.delete(:copy_base)\r
+        @options = options\r
+      end\r
+      #Creates a GIcon.\r
+      def create\r
+        if @copy_base\r
+          c = "new GIcon(#{MappingObject.javascriptify_variable(@copy_base)})"\r
+        else\r
+          c = "new GIcon()"\r
+        end\r
+        if !options.empty?\r
+          "addOptionsToIcon(#{c},#{MappingObject.javascriptify_variable(@options)})"\r
+        else\r
+          c\r
+        end\r
+      end\r
+    end\r
+     \r
+    #A polyline.\r
+    class GPolyline\r
+      include MappingObject\r
+      attr_accessor :points,:color,:weight,:opacity\r
+      #Can take an array of +GLatLng+ or an array of 2D arrays. A method to directly build a polyline from a GeoRuby linestring is provided in the helper.rb file.\r
+      def initialize(points,color = nil,weight = nil,opacity = nil)\r
+        if !points.empty? and points[0].is_a?(Array)\r
+          @points = points.collect { |pt| GLatLng.new(pt) }\r
+        else\r
+          @points = points\r
+        end\r
+        @color = color\r
+        @weight = weight\r
+        @opacity = opacity\r
+      end\r
+      #Creates a new polyline.\r
+      def create\r
+        a = "new GPolyline(#{MappingObject.javascriptify_variable(points)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@color)}" if @color\r
+        a << ",#{MappingObject.javascriptify_variable(@weight)}" if @weight\r
+        a << ",#{MappingObject.javascriptify_variable(@opacity)}" if @opacity\r
+        a << ")"\r
+      end\r
+    end\r
+\r
+    #Encoded GPolyline class\r
+    class GPolylineEncoded \r
+      include MappingObject\r
+      attr_accessor :points,:color,:weight,:opacity,:levels,:zoom_factor,:num_levels\r
+\r
+      def initialize(options={})\r
+        #points = options[:points]\r
+        #if !points.empty? and points[0].is_a?(Array)\r
+        #  @points = points.collect { |pt| GLatLng.new(pt) }\r
+        #else\r
+        #@points = points\r
+          #end\r
+        @points = options[:points]\r
+        @color = options[:color]\r
+        @weight = options[:weight]\r
+        @opacity = options[:opacity]\r
+        @levels = options[:levels] || "BBBBBBBBBBBB"\r
+        @zoom_factor = options[:zoom_factor] || 32\r
+        @num_levels = options[:num_levels] || 4\r
+      end\r
+      def create\r
+        a = "new GPolyline.fromEncoded({points: #{MappingObject.javascriptify_variable(points)},\n" \r
+        a << "levels: #{MappingObject.javascriptify_variable(@levels)},"\r
+        a << "zoomFactor: #{MappingObject.javascriptify_variable(@zoom_factor)},"\r
+        a << "numLevels: #{MappingObject.javascriptify_variable(@num_levels)}"\r
+        a << ",color: #{MappingObject.javascriptify_variable(@color)}" if @color\r
+        a << ",weight: #{MappingObject.javascriptify_variable(@weight)}" if @weight\r
+        a << ",opacity: #{MappingObject.javascriptify_variable(@opacity)}" if @opacity\r
+        a << "})"\r
+      end\r
+    end\r
+\r
+    #A basic Latitude/longitude point.\r
+    class GLatLng \r
+      include MappingObject\r
+      attr_accessor :lat,:lng,:unbounded\r
+      \r
+      def initialize(latlng,unbounded = nil)\r
+        @lat = latlng[0]\r
+        @lng = latlng[1]\r
+        @unbounded = unbounded\r
+      end\r
+      def create\r
+        unless @unbounded\r
+          "new GLatLng(#{MappingObject.javascriptify_variable(@lat)},#{MappingObject.javascriptify_variable(@lng)})"\r
+        else\r
+          "new GLatLng(#{MappingObject.javascriptify_variable(@lat)},#{MappingObject.javascriptify_variable(@lng)},#{MappingObject.javascriptify_variable(@unbounded)})"\r
+        end\r
+      end\r
+    end\r
+    \r
+    #A rectangular bounding box, defined by its south-western and north-eastern corners.\r
+    class GLatLngBounds < Struct.new(:sw,:ne)\r
+      include MappingObject\r
+      def create\r
+        "new GLatLngBounds(#{MappingObject.javascriptify_variable(sw)},#{MappingObject.javascriptify_variable(ne)})"\r
+      end\r
+    end\r
+\r
+    #Polygon. Not documented yet in the Google Maps API\r
+    class GPolygon\r
+      include MappingObject\r
+      \r
+      attr_accessor :points,:stroke_color,:stroke_weight,:stroke_opacity,:color,:opacity\r
+      \r
+      #Can take an array of +GLatLng+ or an array of 2D arrays. A method to directly build a polygon from a GeoRuby polygon is provided in the helper.rb file.\r
+      def initialize(points,stroke_color="#000000",stroke_weight=1,stroke_opacity=1.0,color="#ff0000",opacity=1.0,encoded=false)\r
+        if !points.empty? and points[0].is_a?(Array)\r
+          @points = points.collect { |pt| GLatLng.new(pt) }\r
+        else\r
+          @points = points\r
+        end\r
+        @stroke_color = stroke_color\r
+        @stroke_weight = stroke_weight\r
+        @stroke_opacity = stroke_opacity\r
+        @color = color\r
+        @opacity = opacity\r
+      end\r
+      \r
+      #Creates a new polygon\r
+      def create\r
+        a = "new GPolygon(#{MappingObject.javascriptify_variable(points)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@stroke_color)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@stroke_weight)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@stroke_opacity)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@color)}"\r
+        a << ",#{MappingObject.javascriptify_variable(@opacity)}"\r
+        a << ")"\r
+      end\r
+    end\r
+\r
+    class GPolygonEncoded \r
+      include MappingObject\r
+      \r
+      attr_accessor :polyline, :color, :opacity, :outline, :fill\r
+      \r
+      def initialize(polylines,fill=true,color="#000000",opacity=0.5,outline=false)\r
+        #force polylines to be an array\r
+        if polylines.is_a? Array\r
+          @polylines = polylines\r
+        else\r
+          @polylines = [polylines]\r
+        end\r
+        @color = color\r
+        @fill = fill \r
+        @opacity = opacity\r
+        @outline = outline\r
+      end\r
+      \r
+      #Creates a new polygon.\r
+      def create\r
+        polylines_for_polygon= []\r
+        @polylines.each do |p|\r
+          x = "{points: #{MappingObject.javascriptify_variable(p.points)}," \r
+          x << "levels: #{MappingObject.javascriptify_variable(p.levels)},"\r
+          x << "zoomFactor: #{MappingObject.javascriptify_variable(p.zoom_factor)},"\r
+          x << "numLevels: #{MappingObject.javascriptify_variable(p.num_levels)} "\r
+          x << "}"\r
+          polylines_for_polygon << x\r
+        end\r
+\r
+        polylines_for_polygon = "[" + polylines_for_polygon.join(",") + "]"\r
+\r
+        a = "new GPolygon.fromEncoded({polylines: #{polylines_for_polygon},"\r
+        a << "fill: #{MappingObject.javascriptify_variable(@fill)},"\r
+        a << "color: #{MappingObject.javascriptify_variable(@color)},"\r
+        a << "opacity: #{MappingObject.javascriptify_variable(@opacity)},"\r
+        a << "outline: #{MappingObject.javascriptify_variable(@outline)}"\r
+        a << "})"\r
+      end\r
+    end\r
+\r
+    class ELabel\r
+      attr_accessor :point, :text, :style\r
+      include MappingObject\r
+\r
+      def initialize(point, text=nil, style=nil)\r
+        @point = point\r
+        @text = text\r
+        @style = style\r
+      end\r
+\r
+      def create\r
+          a = "new ELabel(#{MappingObject.javascriptify_variable(@point)}"\r
+          a << ",#{MappingObject.javascriptify_variable(@text)}" if @text\r
+          a << ",#{MappingObject.javascriptify_variable(@style)}" if @style\r
+          a << ")"\r
+      end\r
+    end\r
+\r
+\r
+    #A GGeoXml object gets data from a GeoRSS or KML feed and displays it. Use <tt>overlay_init</tt> to add it to a map at initialization time.\r
+    class GGeoXml\r
+      include MappingObject\r
+      \r
+      attr_accessor :url\r
+\r
+      def initialize(url)\r
+        @url = url\r
+      end\r
+\r
+      def create\r
+        "new GGeoXml(#{MappingObject.javascriptify_variable(@url)})"\r
+      end\r
+\r
+    end\r
+    \r
+    #A GOverlay representing a group of GMarkers. The GMarkers can be identified with an id, which can be used to show the info window of a specific marker, in reponse, for example, to a click on a link. The whole group can be shown on and off at once. It should be declared global at initialization time to be useful.\r
+    class GMarkerGroup\r
+      include MappingObject\r
+      attr_accessor :active, :markers, :markers_by_id\r
+\r
+      def initialize(active = true , markers = nil)\r
+        @active = active\r
+        @markers = []\r
+        @markers_by_id = {}\r
+        if markers.is_a?(Array)\r
+          @markers = markers\r
+        elsif markers.is_a?(Hash)\r
+          @markers_by_id = markers\r
+        end\r
+      end\r
+      \r
+      def create\r
+        "new GMarkerGroup(#{MappingObject.javascriptify_variable(@active)},#{MappingObject.javascriptify_variable(@markers)},#{MappingObject.javascriptify_variable(@markers_by_id)})"\r
+      end\r
+    end\r
+\r
+    #Can be used to implement a clusterer, similar to the clusterer below, except that there is more stuff to manage explicitly byt the programmer (but this is also more flexible). See the README for usage esamples.\r
+    class GMarkerManager\r
+      include MappingObject\r
+      \r
+      attr_accessor :map,:options,:managed_markers\r
+            \r
+      #options can be <tt>:border_padding</tt>, <tt>:max_zoom</tt>, <tt>:track_markers</tt> and <tt>:managed_markers</tt>: managed_markers must be an array of ManagedMarker objects\r
+      def initialize(map, options = {})\r
+        @map = map\r
+        @managed_markers = Array(options.delete(:managed_markers)) #[] if nil\r
+        @options = options\r
+      end\r
+\r
+      def create\r
+        puts @options.inspect\r
+        "addMarkersToManager(new GMarkerManager(#{MappingObject.javascriptify_variable(@map)},#{MappingObject.javascriptify_variable(@options)}),#{MappingObject.javascriptify_variable(@managed_markers)})"\r
+      end\r
+\r
+    end\r
+\r
+    #A set of similarly managed markers: They share the same minZoom and maxZoom.\r
+    class ManagedMarker\r
+      include MappingObject\r
+      \r
+      attr_accessor :markers,:min_zoom, :max_zoom\r
+      \r
+      def initialize(markers,min_zoom,max_zoom = nil)\r
+        @markers = markers\r
+        @min_zoom = min_zoom\r
+        @max_zoom = max_zoom\r
+      end\r
+\r
+      def create\r
+        "new ManagedMarker(#{MappingObject.javascriptify_variable(@markers)},#{MappingObject.javascriptify_variable(@min_zoom)},#{MappingObject.javascriptify_variable(@max_zoom)})"\r
+      end\r
+      \r
+    end\r
+\r
+    #Makes the link with the Clusterer2 library by Jef Poskanzer (slightly modified though). Is a GOverlay making clusters out of its GMarkers, so that GMarkers very close to each other appear as one when the zoom is low. When the zoom gets higher, the individual markers are drawn.\r
+    class Clusterer\r
+      include MappingObject\r
+      attr_accessor :markers,:icon, :max_visible_markers, :grid_size, :min_markers_per_cluster , :max_lines_per_info_box\r
+\r
+      def initialize(markers = [], options = {})\r
+        @markers = markers\r
+        @icon = options[:icon] || GIcon::DEFAULT\r
+        @max_visible_markers = options[:max_visible_markers] || 150\r
+        @grid_size = options[:grid_size] || 5\r
+        @min_markers_per_cluster = options[:min_markers_per_cluster] || 5\r
+        @max_lines_per_info_box = options[:max_lines_per_info_box] || 10\r
+      end\r
+\r
+      def create \r
+        js_marker = '[' + @markers.collect do |marker|\r
+          add_description(marker)\r
+        end.join(",") + ']'\r
+\r
+        "new Clusterer(#{js_marker},#{MappingObject.javascriptify_variable(@icon)},#{MappingObject.javascriptify_variable(@max_visible_markers)},#{MappingObject.javascriptify_variable(@grid_size)},#{MappingObject.javascriptify_variable(@min_markers_per_cluster)},#{MappingObject.javascriptify_variable(@max_lines_per_info_box)})"\r
+      end\r
+            \r
+      private\r
+      def add_description(marker)\r
+        "addDescriptionToMarker(#{MappingObject.javascriptify_variable(marker)},#{MappingObject.javascriptify_variable(marker.options[:description] || marker.options[:title] || '')})"\r
+      end\r
+    end\r
+    \r
+    #Makes the link with the MGeoRSS extension by Mikel Maron (a bit modified though). It lets you overlay on top of Google Maps the items present in a RSS feed that has GeoRss data. This data can be either in W3C Geo vocabulary or in the GeoRss Simple format. See http://georss.org to know more about GeoRss.\r
+    class GeoRssOverlay\r
+      include MappingObject\r
+      attr_accessor :url, :proxy, :icon, :options\r
+      \r
+      #You can pass the following options:\r
+      #- <tt>:icon</tt>: An icon for the items of the feed. Defaults to the classic red balloon icon.\r
+      #- <tt>:proxy</tt>: An URL on your server where fetching the RSS feed will be taken care of.\r
+      #- <tt>:list_div</tt>: In case you want a list of all the markers, with a link on which you can click in order to display the info on the marker, use this option to indicate the ID of the div (that you must place yourself).\r
+      #- <tt>:list_item_class</tt>: class of the DIV containing each item of the list. Ignored if option <tt>:list_div</tt> is not set.\r
+      #- <tt>:limit</tt>: Maximum number of items to display on the map.\r
+      #- <tt>:content_div</tt>: Instead of having an info window appear, indicates the ID of the DIV where this info should be displayed.\r
+      def initialize(url, options = {})\r
+        @url = url\r
+        @icon = options.delete(:icon) || GIcon::DEFAULT\r
+        @proxy = options.delete(:proxy) || Variable::UNDEFINED\r
+        @options = options \r
+      end\r
+\r
+      def create \r
+        "new GeoRssOverlay(#{MappingObject.javascriptify_variable(@url)},#{MappingObject.javascriptify_variable(@icon)},#{MappingObject.javascriptify_variable(@proxy)},#{MappingObject.javascriptify_variable(@options)})"\r
+      end\r
+    end\r
+\r
+  end\r
+end\r

Benjamin Mako Hill || Want to submit a patch?