Add Google Map of voters
[selectricity-live] / vendor / plugins / ym4r_gm / lib / gm_plugin / map.rb
diff --git a/vendor/plugins/ym4r_gm/lib/gm_plugin/map.rb b/vendor/plugins/ym4r_gm/lib/gm_plugin/map.rb
new file mode 100644 (file)
index 0000000..82d1374
--- /dev/null
@@ -0,0 +1,268 @@
+module Ym4r\r
+  module GmPlugin \r
+    #Representing the Google Maps API class GMap2.\r
+    class GMap\r
+      include MappingObject\r
+      \r
+      #A constant containing the declaration of the VML namespace, necessary to display polylines under IE.\r
+      VML_NAMESPACE = "xmlns:v=\"urn:schemas-microsoft-com:vml\""\r
+      \r
+      #The id of the DIV that will contain the map in the HTML page. \r
+      attr_reader :container\r
+      \r
+      #By default the map in the HTML page will be globally accessible with the name +map+.\r
+      def initialize(container, variable = "map")\r
+        @container = container\r
+        @variable = variable\r
+        @init = []\r
+        @init_end = [] #for stuff that must be initialized at the end (controls)\r
+        @init_begin = [] #for stuff that must be initialized at the beginning (center + zoom)\r
+        @global_init = []\r
+      end\r
+\r
+      #Deprecated. Use the static version instead.\r
+      def header(with_vml = true)\r
+        GMap.header(:with_vml => with_vml)\r
+      end\r
+\r
+      #Outputs the header necessary to use the Google Maps API, by including the JS files of the API, as well as a file containing YM4R/GM helper functions. By default, it also outputs a style declaration for VML elements. This default can be overriddent by passing <tt>:with_vml => false</tt> as option to the method. You can also pass a <tt>:host</tt> option in order to select the correct API key for the location where your app is currently running, in case the current environment has multiple possible keys. Usually, in this case, you should pass it <tt>@request.host</tt>. If you have defined only one API key for the current environment, the <tt>:host</tt> option is ignored. Finally you can override all the key settings in the configuration by passing a value to the <tt>:key</tt> key. Finally, you can pass a language for the map type buttons with the <tt>:hl</tt> option (possible values are: Japanese (ja), French (fr), German (de), Italian (it), Spanish (es), Catalan (ca), Basque (eu) and Galician (gl): no values means english)\r
+      def self.header(options = {})\r
+        options[:with_vml] = true unless options.has_key?(:with_vml)\r
+        options[:hl] ||= ''\r
+        api_key = ApiKey.get(options)\r
+        a = "<script src=\"http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=#{api_key}&amp;hl=#{options[:hl]}\" type=\"text/javascript\"></script>\n"\r
+        a << "<script src=\"/javascripts/ym4r-gm.js\" type=\"text/javascript\"></script>\n" unless options[:without_js]\r
+        a << "<style type=\"text/css\">\n v\:* { behavior:url(#default#VML);}\n</style>" if options[:with_vml]\r
+        a\r
+      end\r
+     \r
+      #Outputs the <div id=...></div> which has been configured to contain the map. You can pass <tt>:width</tt> and <tt>:height</tt> as options to output this in the style attribute of the DIV element (you could also achieve the same effect by putting the dimension info into a CSS or using the instance method GMap#header_width_height). You can aslo pass <tt>:class</tt> to set the classname of the div.\r
+      def div(options = {})\r
+        attributes = "id=\"#{@container}\" "\r
+        if options.has_key?(:height) && options.has_key?(:width)\r
+          attributes += "style=\"width:#{options.delete(:width)}px;height:#{options.delete(:height)}px\" "\r
+        end\r
+        if options.has_key?(:class)\r
+          attributes += options.keys.map {|opt| "#{opt}=\"#{options[opt]}\"" }.join(" ")\r
+        end\r
+        "<div #{attributes}></div>"\r
+      end\r
+\r
+      #Outputs a style declaration setting the dimensions of the DIV container of the map. This info can also be set manually in a CSS.\r
+      def header_width_height(width,height)\r
+        "<style type=\"text/css\">\n##{@container} { height: #{height}px;\n  width: #{width}px;\n}\n</style>"\r
+      end\r
+\r
+      #Records arbitrary JavaScript code and outputs it during initialization inside the +load+ function.\r
+      def record_init(code)\r
+        @init << code\r
+      end\r
+\r
+      #Initializes the controls: you can pass a hash with keys <tt>:small_map</tt>, <tt>:large_map</tt>, <tt>:small_zoom</tt>, <tt>:scale</tt>, <tt>:map_type</tt> and <tt>:overview_map</tt> and a boolean value as the value (usually true, since the control is not displayed by default)\r
+      def control_init(controls = {})\r
+        @init_end << add_control(GSmallMapControl.new) if controls[:small_map]\r
+        @init_end << add_control(GLargeMapControl.new) if controls[:large_map]\r
+        @init_end << add_control(GSmallZoomControl.new) if controls[:small_zoom]\r
+        @init_end << add_control(GScaleControl.new) if controls[:scale]\r
+        @init_end << add_control(GMapTypeControl.new) if controls[:map_type]\r
+        @init_end << add_control(GOverviewMapControl.new) if controls[:overview_map]\r
+      end\r
+      \r
+      #Initializes the interface configuration: double-click zoom, dragging, continuous zoom,... You can pass a hash with keys <tt>:dragging</tt>, <tt>:info_window</tt>, <tt>:double_click_zoom</tt>, <tt>:continuous_zoom</tt> and <tt>:scroll_wheel_zoom</tt>. The values should be true or false. Check the google maps API doc to know what the default values are.\r
+      def interface_init(interface = {})\r
+        if !interface[:dragging].nil?\r
+          if interface[:dragging]\r
+             @init << enableDragging() \r
+          else\r
+            @init << disableDragging() \r
+          end\r
+        end\r
+        if !interface[:info_window].nil?\r
+          if interface[:info_window]\r
+            @init << enableInfoWindow()\r
+          else\r
+            @init << disableInfoWindow()\r
+          end\r
+        end\r
+        if !interface[:double_click_zoom].nil?\r
+          if interface[:double_click_zoom]\r
+            @init << enableDoubleClickZoom()\r
+          else\r
+            @init << disableDoubleClickZoom()\r
+          end\r
+        end\r
+        if !interface[:continuous_zoom].nil?\r
+          if interface[:continuous_zoom]\r
+            @init << enableContinuousZoom()\r
+          else\r
+            @init << disableContinuousZoom()\r
+          end\r
+        end\r
+        if !interface[:scroll_wheel_zoom].nil?\r
+          if interface[:scroll_wheel_zoom]\r
+            @init << enableScrollWheelZoom()\r
+          else\r
+            @init << disableScrollWheelZoom()\r
+          end\r
+        end\r
+      end\r
+\r
+      #Initializes the initial center and zoom of the map. +center+ can be both a GLatLng object or a 2-float array.\r
+      def center_zoom_init(center, zoom)\r
+        if center.is_a?(GLatLng)\r
+          @init_begin << set_center(center,zoom)\r
+        else\r
+          @init_begin << set_center(GLatLng.new(center),zoom)\r
+        end\r
+      end\r
+\r
+      #Center and zoom based on the coordinates passed as argument (either 2D arrays or GLatLng objects)\r
+      def center_zoom_on_points_init(*points)\r
+        if(points.length > 0)\r
+          if(points[0].is_a?(Array))\r
+            points = points.collect { |point| GLatLng.new(point) }\r
+          end\r
+          @init_begin << center_and_zoom_on_points(points)\r
+        end\r
+      end\r
+\r
+      #Center and zoom based on the bbox corners. Pass a GLatLngBounds object, an array of 2D coordinates (sw and ne) or an array of GLatLng objects (sw and ne).\r
+      def center_zoom_on_bounds_init(latlngbounds)\r
+        if(latlngbounds.is_a?(Array))\r
+          if latlngbounds[0].is_a?(Array)\r
+            latlngbounds = GLatLngBounds.new(GLatLng.new(latlngbounds[0]),GLatLng.new(latlngbounds[1]))\r
+          elsif latlngbounds[0].is_a?(GLatLng)\r
+            latlngbounds = GLatLngBounds.new(*latlngbounds)\r
+          end\r
+        end\r
+        #else it is already a latlngbounds object\r
+\r
+        @init_begin << center_and_zoom_on_bounds(latlngbounds)\r
+      end\r
+\r
+      #Initializes the map by adding an overlay (marker or polyline).\r
+      def overlay_init(overlay)\r
+        @init << add_overlay(overlay)\r
+      end\r
+\r
+      #Sets up a new map type. If +add+ is false, all the other map types of the map are wiped out. If you want to access the map type in other methods, you should declare the map type first (with +declare_init+).\r
+      def add_map_type_init(map_type, add = true)\r
+        unless add\r
+          @init << get_map_types.set_property(:length,0)\r
+        end\r
+        @init << add_map_type(map_type)\r
+      end\r
+      #for legacy purpose\r
+      alias :map_type_init :add_map_type_init\r
+\r
+      #Sets the map type displayed by default after the map is loaded. It should be known from the map (ie either the default map types or a user-defined map type added with <tt>add_map_type_init</tt>). Use <tt>set_map_type_init(GMapType::G_SATELLITE_MAP)</tt> or <tt>set_map_type_init(GMapType::G_HYBRID_MAP)</tt> to initialize the map with repsecitvely the Satellite view and the hybrid view.\r
+      def set_map_type_init(map_type)\r
+        @init << set_map_type(map_type)\r
+      end\r
+\r
+      #Locally declare a MappingObject with variable name "name"\r
+      def declare_init(variable, name)\r
+        @init << variable.declare(name)\r
+      end\r
+\r
+      #Records arbitrary JavaScript code and outputs it during initialization outside the +load+ function (ie globally).\r
+      def record_global_init(code)\r
+        @global_init << code\r
+      end\r
+      \r
+      #Deprecated. Use icon_global_init instead.\r
+      def icon_init(icon , name)\r
+        icon_global_init(icon , name)\r
+      end\r
+      \r
+      #Initializes an icon  and makes it globally accessible through the JavaScript variable of name +variable+.\r
+      def icon_global_init(icon , name, options = {})\r
+        declare_global_init(icon,name,options)\r
+      end\r
+\r
+      #Registers an event\r
+      def event_init(object,event,callback)\r
+        @init << "GEvent.addListener(#{object.to_javascript},\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback});"\r
+      end\r
+\r
+      #Registers an event globally\r
+      def event_global_init(object,event,callback)\r
+        @global_init << "GEvent.addListener(#{object.to_javascript},\"#{MappingObject.javascriptify_method(event.to_s)}\",#{callback});"\r
+      end\r
+      \r
+      #Declares the overlay globally with name +name+\r
+      def overlay_global_init(overlay,name, options = {})\r
+        declare_global_init(overlay,name, options)\r
+        @init << add_overlay(overlay)\r
+      end\r
+\r
+      #Globally declare a MappingObject with variable name "name". Option <tt>:local_construction</tt> should be passed if the construction has to be done inside the onload callback method (for exsample if it depends on the GMap to be initialized)\r
+      def declare_global_init(variable,name, options = {})\r
+        unless options[:local_construction]\r
+          @global_init << "var #{variable.assign_to(name)}"\r
+        else\r
+          @global_init << "var #{name};"\r
+          @init << variable.assign_to(name)\r
+        end\r
+      end\r
+      \r
+      #Outputs the initialization code for the map. By default, it outputs the script tags, performs the initialization in response to the onload event of the window and makes the map globally available. If you pass +true+ to the option key <tt>:full</tt>, the map will be setup in full screen, in which case it is not necessary (but not harmful) to set a size for the map div.\r
+      def to_html(options = {})\r
+        no_load = options[:no_load]\r
+        no_script_tag = options[:no_script_tag]\r
+        no_declare = options[:no_declare]\r
+        no_global = options[:no_global]\r
+        fullscreen = options[:full]\r
+        load_pr = options[:proto_load] #to prevent some problems when the onload event callback from Prototype is used\r
+        \r
+        html = ""\r
+        html << "<script type=\"text/javascript\">\n" if !no_script_tag\r
+        #put the functions in a separate javascript file to be included in the page\r
+        html << @global_init * "\n"\r
+        html << "var #{@variable};\n" if !no_declare and !no_global\r
+        if !no_load\r
+          if load_pr\r
+            html << "Event.observe(window,'load',"\r
+          else\r
+            html << "window.onload = addCodeToFunction(window.onload,"\r
+          end\r
+          html << "function() {\n"\r
+        end\r
+\r
+        html << "if (GBrowserIsCompatible()) {\n" \r
+        \r
+        if fullscreen\r
+          #Adding the initial resizing and setting up the event handler for\r
+          #future resizes\r
+          html << "setWindowDims(document.getElementById('#{@container}'));\n"\r
+          html << "if (window.attachEvent) { window.attachEvent(\"onresize\", function() {setWindowDims(document.getElementById('#{@container}'));})} else {window.addEventListener(\"resize\", function() {setWindowDims(document.getElementById('#{@container}')); } , false);}\n"\r
+        end\r
+      \r
+        if !no_declare and no_global \r
+          html << "#{declare(@variable)}\n"\r
+        else\r
+          html << "#{assign_to(@variable)}\n"\r
+        end\r
+        html << @init_begin * "\n"\r
+        html << @init * "\n"\r
+        html << @init_end * "\n"\r
+        html << "\n}\n"\r
+        html << "});\n" if !no_load\r
+        html << "</script>" if !no_script_tag\r
+        \r
+        if fullscreen\r
+          #setting up the style in case of full screen\r
+          html << "<style>html, body {width: 100%; height: 100%} body {margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 0px} ##{@container} {margin:  0px;} </style>"\r
+        end\r
+        \r
+        html\r
+      end\r
+      \r
+      #Outputs in JavaScript the creation of a GMap2 object \r
+      def create\r
+        "new GMap2(document.getElementById(\"#{@container}\"))"\r
+      end\r
+    end\r
+  end\r
+end\r
+\r

Benjamin Mako Hill || Want to submit a patch?