ym4r 0.4.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,126 +0,0 @@
1
- module Ym4r
2
- module GoogleMaps
3
- #The module where all the Ruby-to-JavaScript conversion takes place. It is included by all the classes in the YM4R library.
4
- module MappingObject
5
- #The name of the variable in JavaScript space.
6
- attr_reader :variable
7
-
8
- #Creates javascript code for missing methods + takes care of listeners
9
- def method_missing(name,*args)
10
- str_name = name.to_s
11
- if str_name =~ /^on_(.*)/
12
- if args.length != 1
13
- raise ArgumentError("Only 1 argument is allowed on on_ methods");
14
- else
15
- Variable.new("GEvent.addListener(#{to_javascript},\"#{javascriptify_method($1)}\",#{args[0]})")
16
- end
17
- else
18
- args.collect! do |arg|
19
- MappingObject.javascriptify_variable(arg)
20
- end
21
- Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(str_name)}(#{args.join(",")})")
22
- end
23
- end
24
-
25
- #Creates javascript code for array or hash indexing
26
- def [](index) #index could be an integer or string
27
- return Variable.new("#{to_javascript}[#{MappingObject.javascriptify_variable(index)}]")
28
- end
29
-
30
- #Transforms a Ruby object into a JavaScript string : MAppingObject, String, Array, Hash and general case (using to_s)
31
- def self.javascriptify_variable(arg)
32
- if arg.is_a?(MappingObject)
33
- arg.to_javascript
34
- elsif arg.is_a?(String)
35
- "\"#{MappingObject.escape_javascript(arg)}\""
36
- elsif arg.is_a?(Array)
37
- "[" + arg.collect{ |a| MappingObject.javascriptify_variable(a)}.join(",") + "]"
38
- elsif arg.is_a?(Hash)
39
- "{" + arg.to_a.collect do |v|
40
- "#{MappingObject.javascriptify_method(v[0].to_s)} : #{MappingObject.javascriptify_variable(v[1])}"
41
- end.join(",") + "}"
42
- else
43
- arg.to_s
44
- end
45
- end
46
-
47
- #Escape string to be used in JavaScript. Lifted from rails.
48
- def self.escape_javascript(javascript)
49
- javascript.gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
50
- end
51
-
52
- #Transform a ruby-type method name (like add_overlay) to a JavaScript-style one (like addOverlay).
53
- def self.javascriptify_method(method_name)
54
- method_name.gsub(/_(\w)/){|s| $1.upcase}
55
- end
56
-
57
- #Declares a Mapping Object bound to a JavaScript variable of name +variable+.
58
- def declare(variable)
59
- @variable = variable
60
- "var #{@variable} = #{create};"
61
- end
62
-
63
- #declare with a random variable name
64
- def declare_random(init,size = 8)
65
- s = init.clone
66
- 6.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
67
- declare(s)
68
- end
69
-
70
- #Checks if the MappinObject has been declared
71
- def declared?
72
- !@variable.nil?
73
- end
74
-
75
- #Binds a Mapping object to a previously declared JavaScript variable of name +variable+.
76
- def assign_to(variable)
77
- @variable = variable
78
- "#{@variable} = #{create};"
79
- end
80
-
81
- #Assign the +value+ to the +property+ of the MappingObject
82
- def set_property(property, value)
83
- "#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)} = #{MappingObject.javascriptify_variable(value)}"
84
- end
85
-
86
- #Returns the code to get a +property+ from the MappingObject
87
- def get_property(property)
88
- Variable.new("#{to_javascript}.#{MappingObject.javascriptify_method(property.to_s)}")
89
- end
90
-
91
- #Returns a Javascript code representing the object
92
- def to_javascript
93
- unless @variable.nil?
94
- @variable
95
- else
96
- create
97
- end
98
- end
99
-
100
- #Creates a Mapping Object in JavaScript.
101
- #To be implemented by subclasses if needed
102
- def create
103
- end
104
- end
105
-
106
- #Used to bind a ruby variable to an already existing JavaScript one. It doesn't have to be a variable in the sense "var variable" but it can be any valid JavaScript expression that has a value.
107
- class Variable
108
- include MappingObject
109
-
110
- def initialize(variable)
111
- @variable = variable
112
- end
113
- #Returns the javascript expression contained in the object.
114
- def create
115
- @variable
116
- end
117
- #Returns the expression inside the Variable followed by a ";"
118
- def to_s
119
- @variable + ";"
120
- end
121
-
122
- UNDEFINED = Variable.new("undefined")
123
- end
124
- end
125
- end
126
-
@@ -1,187 +0,0 @@
1
- require 'ym4r/google_maps/mapping'
2
-
3
- module Ym4r
4
- module GoogleMaps
5
- #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.
6
- class GMarker
7
- include MappingObject
8
- attr_accessor :point, :options, :info_window, :info_window_tabs
9
- #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>. 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.
10
- def initialize(point, options = {})
11
- if point.is_a?(Array)
12
- @point = GLatLng.new(point)
13
- else
14
- @point = point
15
- end
16
- @info_window = options.delete(:info_window)
17
- @tab_info_window = options.delete(:info_window_tabs)
18
- @options = options
19
- end
20
- #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.
21
- def create
22
- if @options.empty?
23
- creation = "new GMarker(#{MappingObject.javascriptify_variable(@point)})"
24
- else
25
- creation = "new GMarker(#{MappingObject.javascriptify_variable(@point)},#{MappingObject.javascriptify_variable(@options)})"
26
- end
27
- if @info_window
28
- "addInfoWindowToMarker(#{creation},#{MappingObject.javascriptify_variable(@info_window)})"
29
- elsif @tab_info_window
30
- "addInfoWindowTabsToMarker(#{creation},#{MappingObject.javascriptify_variable(Array(@tab_info_window))})"
31
- else
32
- creation
33
- end
34
- end
35
- end
36
-
37
- #Represents a tab to be displayed in a bubble when a marker is clicked on.
38
- class GInfoWindowTab < Struct.new(:tab,:content)
39
- include MappingObject
40
- def create
41
- "new GInfoWindowTab(#{MappingObject.javascriptify_variable(tab)},#{MappingObject.javascriptify_variable(content)})"
42
- end
43
- end
44
-
45
- #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.
46
- class GIcon
47
- include MappingObject
48
- DEFAULT = Variable.new("G_DEFAULT_ICON")
49
- attr_accessor :options, :copy_base
50
-
51
- #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.
52
- def initialize(options = {})
53
- @copy_base = options.delete(:copy_base)
54
- @options = options
55
- end
56
- #Creates a GIcon.
57
- def create
58
- if @copy_base
59
- c = "new GIcon(#{MappingObject.javascriptify_variable(@copy_base)})"
60
- else
61
- c = "new GIcon()"
62
- end
63
- if !options.empty?
64
- "addOptionsToIcon(#{c},#{MappingObject.javascriptify_variable(@options)})"
65
- else
66
- c
67
- end
68
- end
69
- end
70
-
71
- #A polyline.
72
- class GPolyline
73
- include MappingObject
74
- attr_accessor :points,:color,:weight,:opacity
75
- #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.
76
- def initialize(points,color = nil,weight = nil,opacity = nil)
77
- if !points.empty? and points[0].is_a?(Array)
78
- @points = points.collect { |pt| GLatLng.new(pt) }
79
- else
80
- @points = points
81
- end
82
- @color = color
83
- @weight = weight
84
- @opacity = opacity
85
- end
86
- #Creates a new polyline.
87
- def create
88
- a = "new GPolyline([#{@points.collect{|pt| MappingObject.javascriptify_variable(pt)}.join(",")}]"
89
- a << ",#{MappingObject.javascriptify_variable(@color)}" if @color
90
- a << ",#{MappingObject.javascriptify_variable(@weight)}" if @weight
91
- a << ",#{MappingObject.javascriptify_variable(@opacity)}" if @opacity
92
- a << ")"
93
- end
94
- end
95
- #A basic Latitude/longitude point.
96
- class GLatLng
97
- include MappingObject
98
- attr_accessor :lat,:lng,:unbounded
99
-
100
- def initialize(latlng,unbounded = nil)
101
- @lat = latlng[0]
102
- @lng = latlng[1]
103
- @unbounded = unbounded
104
- end
105
- def create
106
- unless @unbounded
107
- "new GLatLng(#{@lat},#{@lng})"
108
- else
109
- "new GLatLng(#{@lat},#{@lng},#{@unbounded})"
110
- end
111
- end
112
- end
113
-
114
- #A rectangular bounding box, defined by its south-western and north-eastern corners.
115
- class GLatLngBounds < Struct.new(:sw,:ne)
116
- include MappingObject
117
- def create
118
- "new GLatLngBounds(#{sw},#{ne})"
119
- end
120
- end
121
-
122
- #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.
123
- class GMarkerGroup
124
- include MappingObject
125
- attr_accessor :active, :markers, :markers_by_id
126
-
127
- def initialize(active = true , markers = nil)
128
- @active = active
129
- @markers = []
130
- @markers_by_id = {}
131
- if markers.is_a?(Array)
132
- @markers = markers
133
- elsif markers.is_a?(Hash)
134
- @markers_by_id = markers
135
- end
136
- end
137
-
138
- def create
139
- "new GMarkerGroup(#{MappingObject.javascriptify_variable(@active)},#{MappingObject.javascriptify_variable(@markers)},#{MappingObject.javascriptify_variable(@markers_by_id)})"
140
- end
141
- end
142
-
143
- #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.
144
- class Clusterer
145
- include MappingObject
146
- attr_accessor :markers,:icon, :max_visible_markers, :grid_size, :min_markers_per_cluster , :max_lines_per_info_box
147
-
148
- def initialize(markers = [], options = {})
149
- @markers = markers
150
- @icon = options[:icon] || GIcon::DEFAULT
151
- @max_visible_markers = options[:max_visible_markers] || 150
152
- @grid_size = options[:grid_size] || 5
153
- @min_markers_per_cluster = options[:min_markers_per_cluster] || 5
154
- @max_lines_per_info_box = options[:max_lines_per_info_box] || 10
155
- end
156
-
157
- def create
158
- js_marker = '[' + @markers.collect do |marker|
159
- add_description(marker)
160
- end.join(",") + ']'
161
-
162
- "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)})"
163
- end
164
-
165
- private
166
- def add_description(marker)
167
- "addDescriptionToMarker(#{MappingObject.javascriptify_variable(marker)},#{MappingObject.javascriptify_variable(marker.options[:description] || marker.options[:title] || '')})"
168
- end
169
- end
170
-
171
- class GeoRssOverlay
172
- include MappingObject
173
- attr_accessor :url, :proxy, :icon
174
-
175
- def initialize(url, options = {})
176
- @url = url
177
- @icon = options[:icon] || GIcon::DEFAULT
178
- @proxy = options[:proxy] || Variable::UNDEFINED
179
- end
180
-
181
- def create
182
- "new GeoRssOverlay(#{MappingObject.javascriptify_variable(@url)},#{MappingObject.javascriptify_variable(@icon)},#{MappingObject.javascriptify_variable(@proxy)})"
183
- end
184
- end
185
-
186
- end
187
- end
@@ -1,36 +0,0 @@
1
- require 'ym4r/google_maps/mapping'
2
-
3
- module Ym4r
4
- module GoogleMaps
5
- #A point in pixel coordinates
6
- class GPoint < Struct.new(:x,:y)
7
- include MappingObject
8
- def create
9
- "new GPoint(#{x},#{y})"
10
- end
11
- end
12
- #A rectangular that contains all the pixel points passed as arguments
13
- class GBounds
14
- include MappingObject
15
- attr_accessor :points
16
- #Accepts both an array of GPoint and an array of 2-element arrays
17
- def initialize(points)
18
- if !points.empty? and points[0].is_a?(Array)
19
- @points = points.collect { |pt| GPoint.new(pt[0],pt[1]) }
20
- else
21
- @points = points
22
- end
23
- end
24
- def create
25
- "new GBounds([#{@points.map { |pt| pt.to_javascript}.join(",")}])"
26
- end
27
- end
28
- #A size object, in pixel space
29
- class GSize < Struct.new(:width,:height)
30
- include MappingObject
31
- def create
32
- "new GSize(#{width},#{height})"
33
- end
34
- end
35
- end
36
- end
@@ -1,7 +0,0 @@
1
- require 'ym4r/yahoo_maps/app_id'
2
- require 'ym4r/yahoo_maps/flash/mapping'
3
- require 'ym4r/yahoo_maps/flash/latlon'
4
- require 'ym4r/yahoo_maps/flash/marker'
5
- require 'ym4r/yahoo_maps/flash/widget'
6
- require 'ym4r/yahoo_maps/flash/tool'
7
- require 'ym4r/yahoo_maps/flash/overlay'
@@ -1,18 +0,0 @@
1
- module Ym4r
2
- module YahooMaps
3
- module Flash
4
- class LatLon < Struct.new(:lat,:lon)
5
- include MappingObject
6
- def create
7
- "new LatLon(#{lat},#{lon})"
8
- end
9
- end
10
- class LatLonRect < Struct.new(:min_lat, :min_lon, :max_lat, :max_lon)
11
- include MappingObject
12
- def create
13
- "new LatLonRect(#{min_lat},#{min_lon},#{max_lat},#{max_lon})"
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,74 +0,0 @@
1
- module Ym4r
2
- module YahooMaps
3
- module Flash
4
-
5
- module MapViews
6
- MAP = Variable.new("MapViews.MAP")
7
- HYBRID = Variable.new("MapViews.HYBRID")
8
- SATELLITE = Variable.new("MapViews.SATELLITE")
9
- end
10
-
11
- class Map
12
- include MappingObject
13
-
14
- attr_reader :container
15
- attr_accessor :zoom, :latlon, :location, :view_type
16
-
17
- EVENT_INITIALIZE = "Map.EVENT_INITIALIZE"
18
- EVENT_MAP_GEOCODE_ERROR = "Map.EVENT_MAP_GEOCODE_ERROR"
19
- EVENT_MAP_GEOCODE_SUCCESS = "Map.EVENT_MAP_GEOCODE_SUCCESS"
20
- EVENT_MARKER_GEOCODE_ERROR = "Map.EVENT_MARKER_GEOCODE_ERROR"
21
- EVENT_MARKER_GEOCODE_SUCCESS = "Map.EVENT_MARKER_GEOCODE_SUCCESS"
22
- EVENT_MOVE = "Map.EVENT_MOVE"
23
- EVENT_PAN_START = "Map.EVENT_PAN_START"
24
- EVENT_PAN_STOP = "Map.EVENT_PAN_STOP"
25
- EVENT_TOOL_ADDED = "Map.EVENT_TOOL_ADDED"
26
- EVENT_TOOL_CHANGE = "Map.EVENT_TOOL_CHANGE"
27
- EVENT_TOOL_REMOVED = "Map.EVENT_TOOL_REMOVED"
28
- EVENT_ZOOM = "Map.EVENT_ZOOM"
29
- EVENT_ZOOM_STOP = "Map.EVENT_ZOOM_STOP"
30
- EVENT_ZOOM_START = "Map.EVENT_ZOOM_START"
31
-
32
- #+container+ is the DIV element in the page that will host the SWF map
33
- def initialize(container, options={})
34
- @container = container
35
- @latlon = options[:latlon]
36
- @location = options[:location] || ""
37
- @zoom = options[:zoom] || 14
38
- @view_type = options[:view_type] || MapViews::MAP
39
- @init = ""
40
- end
41
-
42
- #returns HTML code to add the necessary include and css code to initialize the map
43
- def header
44
- "<script type='text/javascript' src='http://api.maps.yahoo.com/v3.0/fl/javascript/apiloader.js?appid=#{Ym4r::APP_ID}'></script>\n"
45
- end
46
-
47
- def header_width_height(width,height)
48
- "<style type='text/css'>\n##{@container} {\n height: #{@height}px;\n width: #{@width}px;\n}\n</style>\n"
49
- end
50
-
51
- def record_init(code)
52
- @init << code
53
- end
54
-
55
- #creates the map and add any initialization javascript code returned by the block (like addition of a set of initial markers or other custom code).
56
- def to_html(with_script_tag = true)
57
- html = ""
58
- html << "<script type=\"text/javascript\">\n" if with_script_tag
59
- html << @init
60
- html << "</script>\n" if with_script_tag
61
- html
62
- end
63
-
64
- def create
65
- unless @latlon.nil?
66
- "new Map('#{@container}','#{Ym4r::APP_ID}',#{@latlon.to_javascript},#{@zoom},#{@view_type.to_javascript})"
67
- else
68
- "new Map('#{@container}','#{Ym4r::APP_ID}','#{@location}',#{@zoom},#{@view_type.to_javascript})"
69
- end
70
- end
71
- end
72
- end
73
- end
74
- end