Commit 2ec7dec9bc3ad176ee4b9d109ff931e0d7991a95

Authored by Fabio Teixeira
1 parent 03d6fe1b

correcoes_aderencia: Refactoty some code on mpog-user-validations.js

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Showing 1 changed file with 63 additions and 68 deletions   Show diff stats
public/mpog-user-validations.js
... ... @@ -35,79 +35,75 @@
35 35 * If the Country if Brazil, set state to select field
36 36 * else set it as a input field
37 37 */
38   - function SelectFieldChoices() {
39   - // Get the initial state html
40   - var input_select = jQuery("#state_field").parent().html();
41   - var old_value = jQuery("#state_field").val();
42   - var city_parent_div = jQuery("#city_field").parent().parent().parent();
43   -
44   - function replace_with(html) {
45   - var parent_div = jQuery("#state_field").parent();
46   - parent_div.html(html);
  38 + var SelectFieldChoices = (function() {
  39 + function SelectFieldChoices(state_id, city_id, state_url) {
  40 + this.state_id = state_id;
  41 + this.input_html = jQuery(state_id).parent().html();
  42 + this.old_value = jQuery(state_id).val();
  43 + this.city_parent_div = jQuery(city_id).parent().parent().parent();
  44 + this.state_url = state_url;
47 45 }
48 46  
49   - function generate_select(state_list) {
50   - var select_element = new SelectElement();
  47 + SelectFieldChoices.prototype.getCurrentStateElement = function() {
  48 + return jQuery(this.state_id);
  49 + };
  50 +
  51 + SelectFieldChoices.prototype.replaceWith = function(html) {
  52 + var parent_div = this.getCurrentStateElement().parent();
  53 + parent_div.html(html);
  54 + };
  55 +
  56 + SelectFieldChoices.prototype.generateSelect = function(state_list) {
  57 + var select_element, option;
51 58  
  59 + select_element = new SelectElement();
52 60 select_element.setAttr("name", "profile_data[state]");
53 61 select_element.setAttr("id", "state_field");
54 62 select_element.setAttr("class", "type-select valid");
55 63  
56   - state_list.forEach(function(state){
57   - var option = SelectElement.generateOption(state, state);
  64 + state_list.forEach(function(state) {
  65 + option = SelectElement.generateOption(state, state);
58 66 select_element.addOption(option);
59 67 });
60 68  
61 69 return select_element.getSelect();
62   - }
  70 + };
63 71  
64   - function replace_state_with_select() {
65   - jQuery.get("/plugin/mpog_software/get_brazil_states", function(response){
66   - if( response.length > 0 ) {
67   - var select_html = generate_select(response);
68   - replace_with(select_html);
  72 + SelectFieldChoices.prototype.replaceStateWithSelectElement = function() {
  73 + var klass = this;
69 74  
70   - if( old_value.length != 0 && response.include(old_value) ) {
71   - jQuery("#state_field").val(old_value);
  75 + jQuery.get(this.state_url, function(response) {
  76 + var select_html;
  77 +
  78 + if (response.length > 0) {
  79 + select_html = klass.generateSelect(response);
  80 + klass.replaceWith(select_html);
  81 +
  82 + if (klass.old_value.length !== 0 && response.include(klass.old_value)) {
  83 + klass.getCurrentStateElement().val(klass.old_value);
72 84 }
73 85 }
74 86 });
75   - }
76   -
77   - function hide_city(){
78   - city_parent_div.addClass("mpog_hidden_field");
79   - }
80   -
81   - function show_city(){
82   - city_parent_div.removeClass("mpog_hidden_field");
83   - }
  87 + };
84 88  
85   - function replace_state_with_input() {
86   - replace_with(input_select);
87   - }
  89 + SelectFieldChoices.prototype.replaceStateWithInputElement = function() {
  90 + this.replaceWith(this.input_html);
  91 + };
88 92  
89   - return {
90   - actualFieldIsInput : function() {
91   - return jQuery("#state_field").attr("type") == "text";
92   - },
  93 + SelectFieldChoices.prototype.hideCity = function() {
  94 + this.city_parent_div.addClass("mpog_hidden_field");
  95 + };
93 96  
94   - setSelect : function() {
95   - replace_state_with_select();
96   - },
  97 + SelectFieldChoices.prototype.showCity = function() {
  98 + this.city_parent_div.removeClass("mpog_hidden_field");
  99 + };
97 100  
98   - setInput : function() {
99   - replace_state_with_input();
100   - },
101   -
102   - setHideCity : function(){
103   - hide_city();
104   - },
  101 + SelectFieldChoices.prototype.actualFieldIsInput = function() {
  102 + return this.getCurrentStateElement().attr("type") === "text";
  103 + };
105 104  
106   - setShowCity : function(){
107   - show_city();
108   - }
109   - }
110   - }
  105 + return SelectFieldChoices;
  106 + })();
111 107  
112 108 function set_form_count_custom_data() {
113 109 var divisor_option = SelectElement.generateOption("-1", "--------------------------------");
... ... @@ -125,7 +121,7 @@
125 121 jQuery("#password-balloon").html(jQuery("#user_password_menssage").val());
126 122 jQuery("#profile_data_email").parent().append(jQuery("#email_public_message").remove());
127 123  
128   - if( jQuery("#state_field").length != 0 ) selectFieldChoices.setSelect();
  124 + if( jQuery("#state_field").length !== 0 ) selectFieldChoices.replaceStateWithSelectElement();
129 125 }
130 126  
131 127 function check_reactivate_account(value, input_object){
... ... @@ -134,7 +130,7 @@
134 130 type: "GET",
135 131 data: { "email": value },
136 132 success: function(response) {
137   - if( jQuery("#forgot_link").length == 0 )
  133 + if( jQuery("#forgot_link").length === 0 )
138 134 jQuery(input_object).parent().append(response);
139 135 else
140 136 jQuery("#forgot_link").html(response);
... ... @@ -162,7 +158,7 @@
162 158  
163 159 jQuery("#profile_data_country").find(':not(:selected)').css('display', (gov_suffix?'none':'block'));
164 160  
165   - check_reactivate_account(value, input_object)
  161 + check_reactivate_account(value, input_object);
166 162 }
167 163  
168 164 function validate_email_format(){
... ... @@ -233,7 +229,7 @@
233 229 }
234 230  
235 231 function invalid_name_validation(text) {
236   - if( text.trim().length == 0 ) {
  232 + if( text.trim().length === 0 ) {
237 233 return true;
238 234 }
239 235  
... ... @@ -288,7 +284,7 @@
288 284 }
289 285  
290 286 function invalid_email_validation(value, allow_blank) {
291   - if( allow_blank && value.trim().length == 0 ) {
  287 + if( allow_blank && value.trim().length === 0 ) {
292 288 return false;
293 289 }
294 290  
... ... @@ -305,9 +301,9 @@
305 301 //End generic
306 302  
307 303 function get_privacy_selector_parent_div(field_id, actual) {
308   - if( actual == undefined ) actual = jQuery(field_id);
  304 + if( actual === undefined ) actual = jQuery(field_id);
309 305  
310   - if( actual.is("form") || actual.length == 0 ) return null; // Not allow recursion over form
  306 + if( actual.is("form") || actual.length === 0 ) return null; // Not allow recursion over form
311 307  
312 308 if( actual.hasClass("field-with-privacy-selector") ) {
313 309 return actual;
... ... @@ -346,7 +342,7 @@
346 342  
347 343 function change_edit_fields_order() {
348 344 var form = jQuery("#profile-data");
349   - if( form.length != 0 ) {
  345 + if( form.length !== 0 ) {
350 346 var containers = get_edit_fields_in_insertion_order();
351 347  
352 348 containers.reverse();
... ... @@ -360,12 +356,10 @@
360 356 jQuery(document).ready(function(){
361 357 change_edit_fields_order(); // To change the fields order, it MUST be the first function executed
362 358  
363   - var selectFieldChoices = new SelectFieldChoices();
  359 + var selectFieldChoices = new SelectFieldChoices("#state_field", "#city_field", "/plugin/mpog_software/get_brazil_states");
364 360 set_initial_form_custom_data(selectFieldChoices);
365 361  
366   - jQuery('#secondary_email_field').blur(
367   - validate_email_format
368   - );
  362 + jQuery('#secondary_email_field').blur(validate_email_format);
369 363  
370 364 jQuery("#user_email").blur(put_brazil_based_on_email);
371 365  
... ... @@ -381,15 +375,16 @@
381 375 if( this.value == "-1" ) jQuery(this).val("BR");
382 376  
383 377 if( this.value == "BR" && selectFieldChoices.actualFieldIsInput() ) {
384   - selectFieldChoices.setSelect();
385   - selectFieldChoices.setShowCity();
  378 + selectFieldChoices.replaceStateWithSelectElement();
  379 + selectFieldChoices.showCity();
386 380 } else if( this.value != "BR" && !selectFieldChoices.actualFieldIsInput() ) {
387   - selectFieldChoices.setInput();
388   - selectFieldChoices.setHideCity();
  381 + selectFieldChoices.replaceStateWithInputElement();
  382 + selectFieldChoices.hideCity();
389 383 }
390 384 });
391 385  
392 386 show_or_hide_phone_mask();
  387 + jQuery("#profile_data_birth_date").mask("99/99/9999");
393 388  
394 389 fix_phone_mask_format("#profile_data_cell_phone");
395 390 fix_phone_mask_format("#profile_data_comercial_phone");
... ...