user-edit-profile.js
7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* globals modulejs */
modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoices', 'CreateInstitution'], function($, SelectElement, SelectFieldChoices, CreateInstitution) {
'use strict';
function set_form_count_custom_data() {
var divisor_option = SelectElement.generateOption("-1", "--------------------------------");
var default_option = SelectElement.generateOption("BR", "Brazil");
$('#profile_data_country').find("option[value='']").remove();
$('#profile_data_country').prepend(divisor_option);
$('#profile_data_country').prepend(default_option);
$('#profile_data_country').val("BR");
}
function set_initial_form_custom_data(selectFieldChoices) {
set_form_count_custom_data();
$("#password-balloon").html($("#user_password_menssage").val());
$("#profile_data_email").parent().append($("#email_public_message").remove());
if( $("#state_field").length !== 0 ) selectFieldChoices.replaceStateWithSelectElement();
}
function show_state_if_country_is_brazil() {
var selectFieldChoices = new SelectFieldChoices("#state_field", "#city_field", "/plugin/gov_user/get_brazil_states");
set_initial_form_custom_data(selectFieldChoices);
$("#profile_data_country").change(function(){
if( this.value === "-1" ) $(this).val("BR");
if( this.value === "BR" && selectFieldChoices.actualFieldIsInput() ) {
selectFieldChoices.replaceStateWithSelectElement();
selectFieldChoices.showCity();
} else if( this.value !== "BR" && !selectFieldChoices.actualFieldIsInput() ) {
selectFieldChoices.replaceStateWithInputElement();
selectFieldChoices.hideCity();
}
});
}
function show_or_hide_phone_mask() {
if($("#profile_data_country").val() === "BR") {
if( (typeof $("#profile_data_cell_phone").data("rawMaskFn") === 'undefined') ) {
// $("#profile_data_cell_phone").mask("(99) 9999?9-9999");
// $("#profile_data_comercial_phone").mask("(99) 9999?9-9999");
// $("#profile_data_contact_phone").mask("(99) 9999?9-9999");
}
} else {
// $("#profile_data_cell_phone").unmask();
// $("#profile_data_comercial_phone").unmask();
// $("#profile_data_contact_phone").unmask();
}
}
function fix_phone_mask_format(id) {
$(id).blur(function() {
var last = $(this).val().substr( $(this).val().indexOf("-") + 1 );
if( last.length === 3 ) {
var move = $(this).val().substr( $(this).val().indexOf("-") - 1, 1 );
var lastfour = move + last;
var first = $(this).val().substr( 0, 9 );
$(this).val( first + '-' + lastfour );
}
});
}
function show_plugin_error_message(field_selector, hidden_message_id ) {
var field = $(field_selector);
field.removeClass("validated").addClass("invalid");
if(!$("." + hidden_message_id)[0]) {
var message = $("#" + hidden_message_id).val();
field.parent().append("<div class='" + hidden_message_id + " errorExplanation'>"+message+"</span>");
} else {
$("." + hidden_message_id).show();
}
}
function hide_plugin_error_message(field_selector, hidden_message_id) {
$(field_selector).removeClass("invalid").addClass("validated");
$("." + hidden_message_id).hide();
}
function add_blur_fields(field_selector, hidden_message_id, validation_function, allow_blank) {
$(field_selector).blur(function(){
$(this).attr("class", "");
if( validation_function(this.value, !!allow_blank) ) {
show_plugin_error_message(field_selector, hidden_message_id);
} else {
hide_plugin_error_message(field_selector, hidden_message_id);
}
});
}
function invalid_email_validation(value, allow_blank) {
if( allow_blank && value.trim().length === 0 ) {
return false;
}
var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/);
return !correct_format_regex.test(value);
}
function invalid_site_validation(value) {
var correct_format_regex = new RegExp(/(^|)(http[s]{0,1})\:\/\/(\w+[.])\w+/g);
return !correct_format_regex.test(value);
}
function get_privacy_selector_parent_div(field_id, actual) {
if( actual === undefined ) actual = $(field_id);
if( actual.is("form") || actual.length === 0 ) return null; // Not allow recursion over form
if( actual.hasClass("field-with-privacy-selector") ) {
return actual;
} else {
return get_privacy_selector_parent_div(field_id, actual.parent());
}
}
function try_to_remove(list, field) {
try {
list.push(field.remove());
} catch(e) {
console.log("Cound not remove field");
}
}
function get_edit_fields_in_insertion_order() {
var containers = [];
try_to_remove(containers, get_privacy_selector_parent_div("#city_field"));
try_to_remove(containers, get_privacy_selector_parent_div("#state_field"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_country"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_birth_date"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_organization_website"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_personal_website"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_comercial_phone"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_contact_phone"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_cell_phone"));
try_to_remove(containers, $("#select_institution"));
try_to_remove(containers, $("#user_secondary_email").parent().parent());
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_email"));
try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_name"));
try_to_remove(containers, $(".pseudoformlabel").parent().parent());
try_to_remove(containers, $("h2")[0]);
return containers;
}
function change_edit_fields_order() {
var form = $("#profile-data");
if( form.length !== 0 ) {
var containers = get_edit_fields_in_insertion_order();
containers.forEach(function(container){
form.prepend(container);
});
}
}
function set_fields_validations() {
$("#profile_data_country").blur(show_or_hide_phone_mask);
// $("#profile_data_birth_date").mask("99/99/9999");
fix_phone_mask_format("#profile_data_cell_phone");
fix_phone_mask_format("#profile_data_comercial_phone");
fix_phone_mask_format("#profile_data_contact_phone");
add_blur_fields("#profile_data_email", "email_error", invalid_email_validation);
add_blur_fields("#user_secondary_email", "email_error", invalid_email_validation, true);
add_blur_fields("#profile_data_personal_website", "site_error", invalid_site_validation);
add_blur_fields("#profile_data_organization_website", "site_error", invalid_site_validation);
}
return {
isCurrentPage: function() {
return $('#profile_data_email').length === 1;
},
init: function() {
change_edit_fields_order(); // To change the fields order, it MUST be the first function executed
show_state_if_country_is_brazil();
show_or_hide_phone_mask();
set_fields_validations();
CreateInstitution.init();
}
}
});