Commit 63305af96fda49b2cbfabc22570229d1ec97b725

Authored by Fabio Teixeira
1 parent 64b11b09
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

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 141 end
142 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 151 protected
146 152  
... ... @@ -207,5 +213,4 @@ class MpogSoftwarePluginController &lt; ApplicationController
207 213 end
208 214 software_list
209 215 end
210   -
211 216 end
... ...
public/mpog-user-validations.js
1 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 62 jQuery("#password-balloon").html(jQuery("#user_password_menssage").val());
  63 +
  64 + selectFieldChoices.setSelect();
6 65 }
7 66  
8 67 function check_reactivate_account(value, input_object){
... ... @@ -136,7 +195,8 @@
136 195 }
137 196  
138 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 201 jQuery('#secondary_email_field').blur(
142 202 validate_email_format
... ... @@ -150,6 +210,16 @@
150 210 jQuery("#user_pw").blur(verify_user_password_size);
151 211  
152 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 223 show_or_hide_phone_mask();
154 224  
155 225 fix_phone_mask_format("#profile_data_cell_phone");
... ...