lib/ropenlayer/openlayer/feature.rb in ropenlayer-0.3.3 vs lib/ropenlayer/openlayer/feature.rb in ropenlayer-0.3.4
- old
+ new
@@ -1,15 +1,48 @@
module Ropenlayer
module Openlayer
class Feature
- DEFAULT_ICON_SIZE = [ 40, 34 ]
- DEFAULT_ICON_URL = "/images/ropenlayer/default_marker.png"
- DEFAULT_POPUP_SIZE = [ 600, 400 ]
-
attr_reader :map
+ def self.default_attributes
+ { # Style Attributes
+ :style => {
+ :cursor => 'pointer',
+ :graphic_width => 32,
+ :graphic_height => 32,
+ :stroke_color => '#008600',
+ :stroke_opacity => 0.9,
+ :stroke_width => 2,
+ :fill_color => '#008600',
+ :fill_opacity => 0.9,
+ :point_radius => 5,
+ :pointer_events => 'visiblePainted',
+ :font_size => '12px',
+ :font_weight => 'bold'
+ },
+ :render => {
+ # Render Attributes
+ :name => 'Another Feature',
+ :icon_url => '/images/ropenlayer/default_marker.png',
+ :icon_size => [ 40, 34 ],
+ # iccon offsset attribute must be writen as js code.
+ # x offset (first value) and y offset (last value) should maybe fixed values or mathematical operations with size var who is actually icon_size x and y values...
+ # for example, to offset half of icon dimensions, just type
+ # [ '-(size.w/2)', '-(size.h/2)' ]
+ :icon_offset => ['-(size.w/2)', '-(size.h/2)'],
+ :popup_size => [ 600, 400 ],
+ :popup_bgcolor => '#16b87d',
+ :popup_content => false
+ }
+ }
+ end
+
+ def self.all_attributes
+ default_attributes.map{|attr| attr.last.keys }.flatten
+ end
+
def self.draw_collection(map_object)
features = map_object.draw_features
feature_objects = features.inject([]) do |objects, feature_data|
objects << new(map_object, feature_data)
objects
@@ -20,34 +53,37 @@
def initialize(map, attributes)
@map = map
@id = attributes[:id] || raise("No defined id for feature #{ attributes.inspect }")
@js_id = "#{ @map.js_id }node#{ @id }"
- @name = attributes[:name] || 'Another Feature'
- @popup_content = attributes[:popup_content] || false
@geometry = attributes[:geometry] || raise("No defined geometry for feature #{ attributes.inspect }")
@longitude = attributes[:longitude] || raise("No defined longitude for feature #{ attributes.inspect }")
@latitude = attributes[:latitude] || raise("No defined latitude for feature #{ attributes.inspect }")
- @icon_url = attributes[:icon_url]
- @icon_size = attributes[:icon_size]
- @popup_size = attributes[:popup_size]
- @color = attributes[:color]
@localizations = attributes[:localizations]
+
+ @attributes = attributes
+ @name = attributes[:name] || 'Another Feature'
+
+ [:icon_url, :popup_content, :popup_bgcolor ].each do |render_attribute|
+ build_render_attribute(render_attribute, String)
+ end
+ [:icon_size, :popup_size, :icon_offset ].each do |render_attribute|
+ build_render_attribute(render_attribute, Array)
+ end
+
end
-
+
def to_js
%( #{ build_main_geometry }
#{ build_vector_feature }
#{ build_main_icon }
#{ build_main_feature }
#{ build_marker_click_function if @popup_content }
#{ Ropenlayer::Openlayer::Js.new("#{ @map.markers_layer.js_id }.addMarker(#{ @js_id }MainMarker)").to_js }
#{ Ropenlayer::Openlayer::Js.new("#{ @map.vectors_layer.js_id }.addFeatures([#{ @js_id }VectorFeature, #{ @js_id }MainFeature])").to_js }
-
-
)
end
private
@@ -76,21 +112,18 @@
end
def build_vector_feature
vector_feature_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Feature.Vector", :args => [ "#{ @js_id }MainGeometry" ]).to_s
%( #{ Ropenlayer::Openlayer::Js.new_var("#{ @js_id }VectorFeature", vector_feature_method).to_js }
- #{ Ropenlayer::Openlayer::Js.new("#{ @js_id }VectorFeature.attributes = { name: '#{ @name }', favColor: 'red', fillColor: '#{ @color }' }").to_js } )
+ #{ Ropenlayer::Openlayer::Js.new("#{ @js_id }VectorFeature.attributes = #{ build_vector_style_attributes_name.to_json }").to_js } )
end
-
- def build_main_icon
- icon_size = (@icon_size and @icon_size.is_a?(Array)) ? @icon_size : DEFAULT_ICON_SIZE
- icon_url = (@icon_url and @icon_url.is_a?(String)) ? @icon_url : DEFAULT_ICON_URL
- size_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Size", :args => icon_size).to_s
- offset_method = Ropenlayer::Openlayer::Js.new("function(size){ return new OpenLayers.Pixel(-(size.w/2), -(size.h*2)) }").to_s
-
- icon_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Icon", :args => [ "'#{ icon_url }'", size_method, 'null', offset_method ]).to_s
+ def build_main_icon
+ size_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Size", :args => @icon_size).to_s
+ pixel_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Pixel", :args => @icon_offset).to_s
+ offset_method = Ropenlayer::Openlayer::Js.new("function(size){ return #{ pixel_method } }").to_s
+ icon_method = Ropenlayer::Openlayer::Js.new_method("OpenLayers.Icon", :args => [ "'#{ @icon_url }'", size_method, 'null', offset_method ]).to_s
%( #{ Ropenlayer::Openlayer::Js.new_var("#{ @js_id }Icon", icon_method).to_js } )
end
def build_main_feature
main_feature_lonlat = Ropenlayer::Openlayer::Js.new_method("OpenLayers.LonLat", :args => [ @longitude, @latitude ]).to_s
@@ -99,25 +132,37 @@
#{ Ropenlayer::Openlayer::Js.new("#{ @js_id }MainFeature.data.icon = #{ @js_id }Icon").to_js }
#{ Ropenlayer::Openlayer::Js.new_var("#{ @js_id }MainMarker", "#{ @js_id }MainFeature.createMarker()").to_js } )
end
def build_marker_click_function
- popup_size = (@popup_size and @popup_size.is_a?(Array)) ? @popup_size : DEFAULT_POPUP_SIZE
function_content = %(
if (this.popup == null) {
this.popup = this.createPopup(true);
- this.popup.setSize = function(size) { return new OpenLayers.Size(#{ popup_size.join)(',') }); };
+ this.popup.setSize = function(size) { return new OpenLayers.Size(#{ @popup_size.join(',') }); };
this.popup.setBackgroundColor('#16b87d');
this.popup.setContentHTML('#{ @popup_content.gsub("'","") }');
#{ @map.js_id }.addPopup(this.popup);
this.popup.show();
} else {
this.popup.toggle();
}
- OpenLayers.Event.stop(evt);
+ //OpenLayers.Event.stop(evt);
)
%( #{ Ropenlayer::Openlayer::Js.new_var("#{ @js_id}MarkerClickFunction", "function(event) {#{ function_content }}").to_js }
#{ Ropenlayer::Openlayer::Js.new("#{ @js_id }MainMarker.events.register('mousedown', #{ @js_id }MainFeature, #{ @js_id}MarkerClickFunction)").to_js } )
+ end
+
+ def build_vector_style_attributes_name
+ style_attributes = self.class.default_attributes[:style].keys
+ style_attributes.inject({}) do |hash, attribute|
+ hash[attribute] = @attributes[attribute] || self.class.default_attributes[:style][attribute]
+ hash
+ end
+ end
+
+ def build_render_attribute(attribute, should_be_klass)
+ value = (@attributes[attribute] and @attributes[attribute].is_a?(should_be_klass)) ? @attributes[attribute] : self.class.default_attributes[:render][attribute]
+ instance_variable_set("@#{ attribute }", value)
end
end
end
end
\ No newline at end of file