Commit 6de8cf433d34aca538ca11c03e3fbea0ff212aef

Authored by Rafael Martins
1 parent 2f584417

File edit_location javascript and some fixes

app/controllers/my_profile/maps_controller.rb
@@ -26,6 +26,10 @@ class MapsController < MyProfileController @@ -26,6 +26,10 @@ class MapsController < MyProfileController
26 end 26 end
27 end 27 end
28 28
  29 + def google_map
  30 + render :partial => 'google_map.js'
  31 + end
  32 +
29 def search_city 33 def search_city
30 34
31 term = params[:term]; 35 term = params[:term];
app/views/maps/_google_map.js.erb 0 → 100644
@@ -0,0 +1,246 @@ @@ -0,0 +1,246 @@
  1 +
  2 +var geocoder;
  3 +var map;
  4 +var marker;
  5 +var center;
  6 +var move = true;
  7 +var previousCenter;
  8 +
  9 +function getAddress(latlng) {
  10 + $('location-fields').addClassName("loading");
  11 +
  12 + if (latlng != null) {
  13 + geocoder.geocode( {'latLng': latlng}, showAddress);
  14 + }
  15 +}
  16 +
  17 +function codeAddress() {
  18 + $('location-fields').addClassName("loading");
  19 +
  20 + var country_option = $('profile_data_country').value;
  21 + var address = $('profile_data_address').value + "-" + $('profile_data_zip_code').value + "," + $('profile_data_city').value+ "-" + $('profile_data_state').value + "," + country_option;
  22 +
  23 + if (geocoder) {
  24 + geocoder.geocode( { 'address': address}, function(results, status) {
  25 + if (status == google.maps.GeocoderStatus.OK) {
  26 + map.setCenter(results[0].geometry.location);
  27 + marker.setPosition(results[0].geometry.location);
  28 + $('profile_data_lat').value = results[0].geometry.location.lat();
  29 + $('profile_data_lng').value = results[0].geometry.location.lng();
  30 + $('location-fields').removeClassName("loading");
  31 + enable_save();
  32 + } else {
  33 + $('location-fields').removeClassName("loading");
  34 + alert('<%=_("Address not found, reason:")%>' + translate_status(status));
  35 + }
  36 + });
  37 + }
  38 +
  39 + map.setZoom(11);
  40 +
  41 + return false;
  42 +}
  43 +
  44 +function translate_status(status)
  45 +{
  46 + var translated_status = '';
  47 +
  48 + if (google.maps.GeocoderStatus.INVALID_REQUEST == status)
  49 + translated_status = '<%= _('Invalid address') %>';
  50 + else if (google.maps.GeocoderStatus.REQUEST_DENIED == status)
  51 + translated_status = '<%= _('Request denied') %>';
  52 + else if (google.maps.GeocoderStatus.OVER_QUERY_LIMIT == status)
  53 + translated_status = '<%= _('Over query limit') %>';
  54 + else if (google.maps.GeocoderStatus.ZERO_RESULTS == status)
  55 + translated_status = "<%= _('Address do not exist') %>";
  56 +
  57 + return translated_status;
  58 +}
  59 +
  60 +function getAddressData() {
  61 + var text = '';
  62 + var fields = [
  63 + 'profile_data_country',
  64 + 'profile_data_state',
  65 + 'profile_data_city',
  66 + 'profile_data_address',
  67 + 'profile_data_zip_code'
  68 + ];
  69 + for (var i = 0; i < fields.length; i++) {
  70 + var field = fields[i];
  71 + if ($(field)) {
  72 + text += $(field).value + " ";
  73 + }
  74 + }
  75 + return text;
  76 +}
  77 +
  78 +function showAddress(results, status) {
  79 +
  80 + if (status == google.maps.GeocoderStatus.OK) {
  81 + map.setCenter(results[0].geometry.location);
  82 + updateFields(results[0]);
  83 +
  84 + } else {
  85 + alert("<%=_("Address not found, reason:")%>" + translate_status(status));
  86 + }
  87 +
  88 +}
  89 +
  90 +function updateFields(place) {
  91 + var position = marker.getPosition();
  92 + $('profile_data_lat').value = position.lat();
  93 + $('profile_data_lng').value = position.lng();
  94 + $('location-fields').removeClassName("loading");
  95 +
  96 + var components_len = place.address_components.size();
  97 +
  98 + if(components_len < 2)
  99 + {
  100 + return false;
  101 + }
  102 +
  103 + var components = place.address_components;
  104 + var address = "";
  105 + var zip_code = "";
  106 + var city = "";
  107 + var state = "";
  108 + var country_code = "";
  109 + var i = 0;
  110 +
  111 + for( i =0 ; i < components_len; i ++)
  112 + {
  113 +
  114 + if (components[i].types[0] == 'country')
  115 + country_code = components[i].short_name;
  116 + else if (components[i].types[0] == 'administrative_area_level_1')
  117 + state = components[i].long_name;
  118 + else if (components[i].types[0] == 'locality')
  119 + city = components[i].long_name;
  120 + else if (components[i].types[0] == 'sublocality')
  121 + address = components[i].long_name + "-" + address;
  122 + else if (components[i].types[0] == "route")
  123 + address = components[i].long_name + address;
  124 + else if (components[i].types[0] == "street_number")
  125 + address = address + "," + components[i].short_name ;
  126 + else if (components[i].types[0] == 'postal_code')
  127 + zip_code = components[i].short_name;
  128 + }
  129 +
  130 + $('profile_data_country').value = country_code;
  131 + $('profile_data_state').value = state;
  132 + $('profile_data_address').value = address;
  133 + $('profile_data_city').value = city;
  134 + $('profile_data_zip_code').value = zip_code;
  135 +}
  136 +
  137 +
  138 +function initialize_map() {
  139 + geocoder = new google.maps.Geocoder();
  140 +
  141 + var lat = <%= profile.lat || 'false' %>;
  142 + var lng = <%= profile.lng || 'false' %>;
  143 +
  144 + if ( !(lat && lng) ) {
  145 + lat = -15.7605361485013;
  146 + lng = -47.933349609375;
  147 + }
  148 +
  149 + var latlng = new google.maps.LatLng(lat,lng);
  150 +
  151 + var myOptions = {
  152 + zoom: 8,
  153 + center: latlng,
  154 + mapTypeId: google.maps.MapTypeId.ROADMAP
  155 + }
  156 +
  157 + center = latlng;
  158 +
  159 + map = new google.maps.Map(document.getElementById("location-map"), myOptions);
  160 +
  161 + continueLoadMapV3()
  162 +}
  163 +
  164 +function continueLoadMapV3() {
  165 +
  166 + marker = new google.maps.Marker({
  167 + position: center,
  168 + map: map,
  169 + draggable: true
  170 + });
  171 +
  172 + google.maps.event.addListener(marker, "dragend", function() {
  173 + move = false;
  174 + getAddress(marker.getPosition());
  175 + enable_save();
  176 + });
  177 +
  178 +}
  179 +
  180 +window.onload = initialize_map;
  181 +
  182 +var delay_autocomplete = 500;
  183 +
  184 +jQuery.noConflict();
  185 +jQuery(document).ready(function (){
  186 +
  187 + jQuery.widget( "custom.catcomplete",jQuery.ui.autocomplete, {
  188 + _renderMenu: function( ul, items ) {
  189 + var self = this,
  190 + currentCategory = "";
  191 + jQuery.each( items, function( index, item ) {
  192 + if ( item.category != currentCategory ) {
  193 + ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
  194 + currentCategory = item.category;
  195 + }
  196 + self._renderItem( ul, item );
  197 + });
  198 + }
  199 + });
  200 +
  201 +
  202 + jQuery("#profile_data_city").catcomplete({
  203 + source: "../maps/search_city",
  204 + minLength: 3,
  205 + delay: delay_autocomplete,
  206 + select: function( event, ui ) { $('profile_data_state').value =( ui.item ? ui.item.category : this.value ); }
  207 + });
  208 +
  209 + jQuery("#profile_data_state").autocomplete({
  210 + source: "../maps/search_state",
  211 + minLength: 3,
  212 + delay: delay_autocomplete
  213 + });
  214 +
  215 + jQuery("#profile_data_city").keyup(function(){
  216 +
  217 + disable_save();
  218 +
  219 + });
  220 +
  221 + jQuery("#profile_data_state").keyup(function(){
  222 +
  223 + disable_save();
  224 +
  225 + });
  226 +
  227 + jQuery("#profile_data_country").change(function(){
  228 +
  229 + disable_save();
  230 +
  231 + });
  232 +
  233 +});
  234 +
  235 +function disable_save()
  236 +{
  237 + jQuery('input[type="submit"]').attr("disabled", "true");
  238 + jQuery('input[type="submit"]').val('<%=_("Localize before save")%>');
  239 + jQuery('input[type="submit"]').addClass('disabled');
  240 +}
  241 +function enable_save()
  242 +{
  243 + jQuery('input[type="submit"]').removeAttr("disabled");
  244 + jQuery('input[type="submit"]').val('<%=_("Save")%>');
  245 + jQuery('input[type="submit"]').removeClass('disabled');
  246 +}
app/views/maps/_google_map.rhtml
@@ -1,282 +0,0 @@ @@ -1,282 +0,0 @@
1 -<%= content_tag('script', '', :src => "http://maps.googleapis.com/maps/api/js?sensor=false", :type => 'text/javascript') %>  
2 -  
3 -<script type="text/javascript" >  
4 -  
5 - var geocoder;  
6 - var map;  
7 - var marker;  
8 - var center;  
9 - var move = true;  
10 - var previousCenter;  
11 -  
12 - function getAddress(latlng) {  
13 - $('location-fields').addClassName("loading");  
14 -  
15 - if (latlng != null) {  
16 - geocoder.geocode( {'latLng': latlng}, showAddress);  
17 - }  
18 - }  
19 -  
20 - function codeAddress() {  
21 - $('location-fields').addClassName("loading");  
22 -  
23 - var country_option = $('profile_data_country').value;  
24 - var address = $('profile_data_address').value + "-" + $('profile_data_zip_code').value + "," + $('profile_data_city').value+ "-" + $('profile_data_state').value + "," + country_option;  
25 -  
26 - if (geocoder) {  
27 - geocoder.geocode( { 'address': address}, function(results, status) {  
28 - if (status == google.maps.GeocoderStatus.OK) {  
29 - map.setCenter(results[0].geometry.location);  
30 - marker.setPosition(results[0].geometry.location);  
31 - $('profile_data_lat').value = results[0].geometry.location.lat();  
32 - $('profile_data_lng').value = results[0].geometry.location.lng();  
33 - $('location-fields').removeClassName("loading");  
34 - enable_save();  
35 - } else {  
36 - $('location-fields').removeClassName("loading");  
37 - alert('<%=_("Address not found, reason:")%>' + translate_status(status));  
38 - }  
39 - });  
40 - }  
41 -  
42 - return false;  
43 - }  
44 -  
45 - function translate_status(status)  
46 - {  
47 - var translated_status = '';  
48 -  
49 - if(google.maps.GeocoderStatus.INVALID_REQUEST == status)  
50 - {  
51 - translated_status = '<%= _('Invalid address') %>';  
52 - }  
53 -  
54 - if(google.maps.GeocoderStatus.REQUEST_DENIED == status)  
55 - {  
56 - translated_status = '<%= _('Request denied') %>';  
57 - }  
58 -  
59 - if(google.maps.GeocoderStatus.OVER_QUERY_LIMIT == status)  
60 - {  
61 - translated_status = '<%= _('Over query limit') %>';  
62 - }  
63 -  
64 - if(google.maps.GeocoderStatus.ZERO_RESULTS == status)  
65 - {  
66 - translated_status = "<%= _('Address do not exist') %>";  
67 - }  
68 -  
69 - return translated_status;  
70 - }  
71 -  
72 - function getAddressData() {  
73 - var text = '';  
74 - var fields = [  
75 - 'profile_data_country',  
76 - 'profile_data_state',  
77 - 'profile_data_city',  
78 - 'profile_data_address',  
79 - 'profile_data_zip_code'  
80 - ];  
81 - for (var i = 0; i < fields.length; i++) {  
82 - var field = fields[i];  
83 - if ($(field)) {  
84 - text += $(field).value + " ";  
85 - }  
86 - }  
87 - return text;  
88 - }  
89 -  
90 - function showAddress(results, status) {  
91 -  
92 - if (status == google.maps.GeocoderStatus.OK) {  
93 - map.setCenter(results[0].geometry.location);  
94 - updateFields(results[0]);  
95 -  
96 - } else {  
97 - alert("<%=_("Address not found, reason:")%>" + translate_status(status));  
98 - }  
99 -  
100 - }  
101 -  
102 - function updateFields(place) {  
103 - var position = marker.getPosition();  
104 - $('profile_data_lat').value = position.lat();  
105 - $('profile_data_lng').value = position.lng();  
106 - $('location-fields').removeClassName("loading");  
107 -  
108 - var components_len = place.address_components.size();  
109 -  
110 - if(components_len < 2)  
111 - {  
112 - return false;  
113 - }  
114 -  
115 - var components = place.address_components;  
116 - var address = "";  
117 - var zip_code = "";  
118 - var city = "";  
119 - var state = "";  
120 - var country_code = "";  
121 - var i = 0;  
122 -  
123 - for( i =0 ; i < components_len; i ++)  
124 - {  
125 -  
126 - if(components[i].types[0] == 'country')  
127 - {  
128 - country_code = components[i].short_name;  
129 - }  
130 -  
131 - if(components[i].types[0] == 'administrative_area_level_1')  
132 - {  
133 - state = components[i].long_name;  
134 - }  
135 -  
136 - if(components[i].types[0] == 'locality')  
137 - {  
138 - city = components[i].long_name;  
139 - }  
140 -  
141 - if(components[i].types[0] == 'sublocality')  
142 - {  
143 - address = components[i].long_name + "-" + address;  
144 - }  
145 -  
146 - if(components[i].types[0] == "route")  
147 - {  
148 - address = components[i].long_name + address;  
149 - }  
150 -  
151 -  
152 - if(components[i].types[0] == "street_number")  
153 - {  
154 - address = address + "," + components[i].short_name ;  
155 - }  
156 -  
157 - if(components[i].types[0] == 'postal_code')  
158 - {  
159 - zip_code = components[i].short_name;  
160 - }  
161 -  
162 - }  
163 -  
164 - $('profile_data_country').value = country_code;  
165 - $('profile_data_state').value = state;  
166 - $('profile_data_address').value = address;  
167 - $('profile_data_city').value = city;  
168 - $('profile_data_zip_code').value = zip_code;  
169 - }  
170 -  
171 -  
172 - function initialize_map() {  
173 - geocoder = new google.maps.Geocoder();  
174 -  
175 - var lat = <%= profile.lat || 'false' %>;  
176 - var lng = <%= profile.lng || 'false' %>;  
177 -  
178 - if ( !(lat && lng) ) {  
179 - lat = -15.7605361485013;  
180 - lng = -47.933349609375;  
181 - }  
182 -  
183 - var latlng = new google.maps.LatLng(lat,lng);  
184 -  
185 - var myOptions = {  
186 - zoom: 8,  
187 - center: latlng,  
188 - mapTypeId: google.maps.MapTypeId.ROADMAP  
189 - }  
190 -  
191 - center = latlng;  
192 -  
193 - map = new google.maps.Map(document.getElementById("location-map"), myOptions);  
194 -  
195 - continueLoadMapV3()  
196 - }  
197 -  
198 - function continueLoadMapV3() {  
199 -  
200 - marker = new google.maps.Marker({  
201 - position: center,  
202 - map: map,  
203 - draggable: true  
204 - });  
205 -  
206 - google.maps.event.addListener(marker, "dragend", function() {  
207 - move = false;  
208 - getAddress(marker.getPosition());  
209 - });  
210 -  
211 - }  
212 -  
213 - window.onload = initialize_map;  
214 -  
215 - var delay_autocomplete = 500;  
216 -  
217 - jQuery.noConflict();  
218 - jQuery(document).ready(function (){  
219 -  
220 - jQuery.widget( "custom.catcomplete",jQuery.ui.autocomplete, {  
221 - _renderMenu: function( ul, items ) {  
222 - var self = this,  
223 - currentCategory = "";  
224 - jQuery.each( items, function( index, item ) {  
225 - if ( item.category != currentCategory ) {  
226 - ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );  
227 - currentCategory = item.category;  
228 - }  
229 - self._renderItem( ul, item );  
230 - });  
231 - }  
232 - });  
233 -  
234 -  
235 - jQuery("#profile_data_city").catcomplete({  
236 - source: "../maps/search_city",  
237 - minLength: 3,  
238 - delay: delay_autocomplete,  
239 - select: function( event, ui ) { $('profile_data_state').value =( ui.item ? ui.item.category : this.value ); }  
240 - });  
241 -  
242 - jQuery("#profile_data_state").autocomplete({  
243 - source: "../maps/search_state",  
244 - minLength: 3,  
245 - delay: delay_autocomplete  
246 - });  
247 -  
248 - jQuery("#profile_data_city").keyup(function(){  
249 -  
250 - disable_save();  
251 -  
252 - });  
253 -  
254 - jQuery("#profile_data_state").keyup(function(){  
255 -  
256 - disable_save();  
257 -  
258 - });  
259 -  
260 - jQuery("#profile_data_country").change(function(){  
261 -  
262 - disable_save();  
263 -  
264 - });  
265 -  
266 - });  
267 -  
268 - function disable_save()  
269 - {  
270 - jQuery('input[type="submit"]').attr("disabled", "true");  
271 - jQuery('input[type="submit"]').attr("value", '<%=_("Localize before save")%>');  
272 - jQuery('input[type="submit"]').attr("class", "disabled button icon-save submit");  
273 - }  
274 - function enable_save()  
275 - {  
276 - jQuery('input[type="submit"]').attr("value", '<%=_("Save")%>');  
277 - jQuery('input[type="submit"]').removeAttr("disabled");  
278 - jQuery('input[type="submit"]').attr("class", "button icon-save submit");  
279 - }  
280 -</script>  
281 -  
282 -  
app/views/maps/edit_location.rhtml
@@ -17,18 +17,17 @@ @@ -17,18 +17,17 @@
17 <% end %> 17 <% end %>
18 </div> 18 </div>
19 19
  20 + <p><%= _('Drag the balloon to find the exact location.') %> </p>
  21 +
20 <div style='overflow: hidden'> 22 <div style='overflow: hidden'>
21 <p><div id="location-map"></div></p> 23 <p><div id="location-map"></div></p>
22 </div> 24 </div>
23 25
24 <%= f.hidden_field(:lat) %> 26 <%= f.hidden_field(:lat) %>
25 <%= f.hidden_field(:lng) %> 27 <%= f.hidden_field(:lng) %>
26 -  
27 - <% button_bar do %>  
28 - <%= submit_button 'save', _('Save') %>  
29 - <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %>  
30 - <% end %>  
31 28
32 <% end %> 29 <% end %>
33 30
34 -<%= render :partial => 'google_map'%> 31 +<%= content_tag('script', '', :src => "http://maps.googleapis.com/maps/api/js?sensor=false", :type => 'text/javascript') %>
  32 +<%= content_tag('script', '', :src => url_for(:controller => :maps, :action => :google_map), :type => 'text/javascript') %>
  33 +