Commit 63305af96fda49b2cbfabc22570229d1ec97b725

Authored by Fabio Teixeira
1 parent 64b11b09

correcoes_aderencia: Switch between input and select field on state based on country

Signed-off-by: David Carlos <ddavidcarlos1392@gmail.com>
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
controllers/mpog_software_plugin_controller.rb
@@ -141,6 +141,12 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -141,6 +141,12 @@ class MpogSoftwarePluginController &lt; ApplicationController
141 end 141 end
142 end 142 end
143 143
  144 + def get_brazil_states
  145 + redirect_to "/" unless request.xhr?
  146 +
  147 + state_list = NationalRegion.find(:all, :conditions=>["national_region_type_id = ?", 2], :order=>"name")
  148 + render :json=>state_list.collect {|state| state.name }.to_json
  149 + end
144 150
145 protected 151 protected
146 152
@@ -207,5 +213,4 @@ class MpogSoftwarePluginController &lt; ApplicationController @@ -207,5 +213,4 @@ class MpogSoftwarePluginController &lt; ApplicationController
207 end 213 end
208 software_list 214 software_list
209 end 215 end
210 -  
211 end 216 end
public/mpog-user-validations.js
1 (function(){ 1 (function(){
2 - function set_initial_form_custom_data() {  
3 - jQuery('#profile_data_country').val("BR"); 2 + /*
  3 + * "Class" that switch state field between input and select
  4 + * If the Country if Brazil, set state to select field
  5 + * else set it as a input field
  6 + */
  7 + function SelectFieldChoices() {
  8 + // Get the initial state html
  9 + var input_select = jQuery("#state_field").parent().html();
  10 + var old_value = jQuery("#state_field").val();
  11 +
  12 + function replace_with(html) {
  13 + var parent_div = jQuery("#state_field").parent();
  14 + parent_div.html(html);
  15 + }
  16 +
  17 + function generate_select(state_list) {
  18 + var html = "<select class='type-select valid' id='state_field' name='profile_data[state]'>";
  19 +
  20 + state_list.forEach(function(state){
  21 + html += "<option value='"+state+"'>"+state+"</option>";
  22 + });
  23 +
  24 + html += "</select>";
  25 + return html;
  26 + }
  27 +
  28 + function replace_state_with_select() {
  29 + jQuery.get("/plugin/mpog_software/get_brazil_states", function(response){
  30 + if( response.length > 0 ) {
  31 + var select_html = generate_select(response);
  32 + replace_with(select_html);
  33 +
  34 + if( old_value.length != 0 && response.include(old_value) ) {
  35 + jQuery("#state_field").val(old_value);
  36 + }
  37 + }
  38 + });
  39 + }
  40 +
  41 + function replace_state_with_input() {
  42 + replace_with(input_select);
  43 + }
  44 +
  45 + return {
  46 + actualFieldIsInput : function() {
  47 + return jQuery("#state_field").attr("type") == "text";
  48 + },
4 49
  50 + setSelect : function() {
  51 + replace_state_with_select();
  52 + },
  53 +
  54 + setInput : function() {
  55 + replace_state_with_input();
  56 + }
  57 + }
  58 + }
  59 +
  60 + function set_initial_form_custom_data(selectFieldChoices) {
  61 + jQuery('#profile_data_country').val("BR");
5 jQuery("#password-balloon").html(jQuery("#user_password_menssage").val()); 62 jQuery("#password-balloon").html(jQuery("#user_password_menssage").val());
  63 +
  64 + selectFieldChoices.setSelect();
6 } 65 }
7 66
8 function check_reactivate_account(value, input_object){ 67 function check_reactivate_account(value, input_object){
@@ -136,7 +195,8 @@ @@ -136,7 +195,8 @@
136 } 195 }
137 196
138 jQuery(document).ready(function(){ 197 jQuery(document).ready(function(){
139 - set_initial_form_custom_data(); 198 + var selectFieldChoices = new SelectFieldChoices();
  199 + set_initial_form_custom_data(selectFieldChoices);
140 200
141 jQuery('#secondary_email_field').blur( 201 jQuery('#secondary_email_field').blur(
142 validate_email_format 202 validate_email_format
@@ -150,6 +210,16 @@ @@ -150,6 +210,16 @@
150 jQuery("#user_pw").blur(verify_user_password_size); 210 jQuery("#user_pw").blur(verify_user_password_size);
151 211
152 jQuery("#profile_data_country").blur(show_or_hide_phone_mask); 212 jQuery("#profile_data_country").blur(show_or_hide_phone_mask);
  213 +
  214 + // Event that calls the "Class" to siwtch state field types
  215 + jQuery("#profile_data_country").change(function(){
  216 + if( this.value == "BR" && selectFieldChoices.actualFieldIsInput() ) {
  217 + selectFieldChoices.setSelect();
  218 + } else {
  219 + selectFieldChoices.setInput();
  220 + }
  221 + });
  222 +
153 show_or_hide_phone_mask(); 223 show_or_hide_phone_mask();
154 224
155 fix_phone_mask_format("#profile_data_cell_phone"); 225 fix_phone_mask_format("#profile_data_cell_phone");