From a3d921c47a2cb6afa9398de21e34563af4983ab5 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 12 Nov 2013 21:56:36 +0800 Subject: [PATCH] implementd maps with category filteration and custom color markers. --- .../app/assets/javascripts/gprs/.gitkeep | 0 .../app/assets/javascripts/location.js | 132 ++++++++++++++++++ .../panel/gprs/back_end/locations.js | 2 - .../location/back_end/locations_controller.rb | 8 +- .../back_end/locations/index.html.erb | 40 ++++++ 5 files changed, 179 insertions(+), 3 deletions(-) delete mode 100644 vendor/built_in_modules/location/app/assets/javascripts/gprs/.gitkeep create mode 100644 vendor/built_in_modules/location/app/assets/javascripts/location.js delete mode 100644 vendor/built_in_modules/location/app/assets/javascripts/panel/gprs/back_end/locations.js diff --git a/vendor/built_in_modules/location/app/assets/javascripts/gprs/.gitkeep b/vendor/built_in_modules/location/app/assets/javascripts/gprs/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/vendor/built_in_modules/location/app/assets/javascripts/location.js b/vendor/built_in_modules/location/app/assets/javascripts/location.js new file mode 100644 index 00000000..685ac6df --- /dev/null +++ b/vendor/built_in_modules/location/app/assets/javascripts/location.js @@ -0,0 +1,132 @@ +var Locations = function(ls){ + + l = this; + this.locations = ls; + this.markerCounts = 0; + var map = null, + infowindow = new google.maps.InfoWindow(), + default_location = new google.maps.LatLng( 24.8043107, 121.03042159999995 ), + infowindow_template = "
", + markers = []; + + + var initialize = function() { + + google.maps.visualRefresh = true; + var location = ( l.locations.length ? new google.maps.LatLng( l.locations[0].latitude, l.locations[0].longitude ) : default_location ), + mapOptions = { + zoom: 16, + center: location, + mapTypeId: google.maps.MapTypeId.ROADMAP + }; + + map = new google.maps.Map( document.getElementById('map'), mapOptions ); + + for( i = 0; i < l.locations.length; i++ ){ + markers.push( addMarker( new google.maps.LatLng(l.locations[i].latitude, l.locations[i].longitude), l.locations[i].color, i, function( index ){ + + }) ); + } + + } + + var addMarker = function( location, pinColor, index, markerCallback ){ + + var pin = getPin(( pinColor.substr( 0, 1 ) == "#" ? pinColor.replace("#","") : pinColor )), + marker = new google.maps.Marker({ + map: map, + icon: pin.image, + shadow: pin.shadow, + position: location, + zIndex: 10 + }); + + google.maps.event.addListener( marker, 'click', function() { + + infowindow.setContent( getTemplate( index ) ); + infowindow.open( map, marker ); + if( typeof markerCallback == "function" ) + markerCallback.call( this, index ); + + }); + + l.markerCounts++; + return {"marker" : marker, "category_id" : l.locations[index].category_id}; + } + + + + var getTemplate = function( obj ){ + + var object = ( typeof obj == "number" ? l.locations[obj] : ( typeof obj == "object" ? obj : null ) ); + + if ( object ){ + + var template = $( "
" ); + template.html( infowindow_template ); + template.find( '.title' ).text( object.name ); + template.find( "img.thumbnail" ).attr( "src", object.file.thumb.url ); + return template.html(); + + }else{ + + throw new InvalidObjectError(); + } + + } + + var getPin = function( color ){ + + var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + color, + new google.maps.Size(21, 34), + new google.maps.Point(0,0), + new google.maps.Point(10, 34)); + + var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow", + new google.maps.Size(40, 37), + new google.maps.Point(0, 0), + new google.maps.Point(12, 35)); + + return {"image":pinImage,"shadow":pinShadow}; + } + + this.getMarker = function( index ){ + return markers[index]; + } + this.getAllMarkers = function( ){ + return markers; + } + + this.clearFilter = function(){ + var m = l.getAllMarkers(); + for( i = 0; i < m.length; i++ ){ + m[i].marker.setVisible(true); + } + } + + this.filterMarkers = function( categories ){ + var filtered = [], + m = l.getAllMarkers(); + + for( i = 0; i < m.length; i++ ){ + m[i].marker.setVisible(false); + } + for( i = 0; i < categories.length; i++ ){ + filtered = filtered.concat(m.filter(function(a){return a.category_id == categories[i]})); + } + for( i = 0; i < filtered.length; i++ ){ + filtered[i].marker.setVisible(true); + } + return filtered; + } + +google.maps.event.addDomListener(window, 'load', initialize); + +} +var InvalidObjectError = function(){ + this.name = "InvalidObjectError"; + this.message = "Object not valid"; +} + +InvalidObjectError.prototype = new Error(); +InvalidObjectError.prototype.constructor = InvalidObjectError; diff --git a/vendor/built_in_modules/location/app/assets/javascripts/panel/gprs/back_end/locations.js b/vendor/built_in_modules/location/app/assets/javascripts/panel/gprs/back_end/locations.js deleted file mode 100644 index dee720fa..00000000 --- a/vendor/built_in_modules/location/app/assets/javascripts/panel/gprs/back_end/locations.js +++ /dev/null @@ -1,2 +0,0 @@ -// Place all the behaviors and hooks related to the matching controller here. -// All this logic will automatically be available in application.js. diff --git a/vendor/built_in_modules/location/app/controllers/panel/location/back_end/locations_controller.rb b/vendor/built_in_modules/location/app/controllers/panel/location/back_end/locations_controller.rb index b5d1c064..b2a13516 100644 --- a/vendor/built_in_modules/location/app/controllers/panel/location/back_end/locations_controller.rb +++ b/vendor/built_in_modules/location/app/controllers/panel/location/back_end/locations_controller.rb @@ -2,8 +2,13 @@ class Panel::Location::BackEnd::LocationsController < OrbitBackendController include AdminHelper def index - @location_infos = LocationInfo.all + @locations = LocationInfo.all @categories = get_categories_for_index + @location_infos = []; + @locations.each do |loc| + loc['color'] = Category.find(loc.category_id).custom_value + @location_infos << loc + end respond_to do |format| format.html # new.html.erb format.json { render json: @locations } @@ -36,6 +41,7 @@ class Panel::Location::BackEnd::LocationsController < OrbitBackendController def create @location_info = LocationInfo.new(params[:location_info]) + @categories = get_categories_for_index if @location_info.save flash[:success] = "Success!!" redirect_to panel_location_back_end_locations_url diff --git a/vendor/built_in_modules/location/app/views/panel/location/back_end/locations/index.html.erb b/vendor/built_in_modules/location/app/views/panel/location/back_end/locations/index.html.erb index 3190012b..cbd1c516 100644 --- a/vendor/built_in_modules/location/app/views/panel/location/back_end/locations/index.html.erb +++ b/vendor/built_in_modules/location/app/views/panel/location/back_end/locations/index.html.erb @@ -1,3 +1,5 @@ + +<%= javascript_include_tag "location" %> @@ -28,6 +30,44 @@ <% end %>
+
+ Categories : +
+
+ <% @categories.each do |category| %> + + <% end %> +
+Clear Filter +
<%if is_manager? %> <%= link_to 'Add', new_panel_location_back_end_location_path, :class => "btn btn-primary pull-right", :id=>"create_event_btn", :ref=>"add-btn"%> <% end %> +