Commit 8c5d6d8b1d2b69451c1a0356ebe774ef9aaf0060
Committed by
Gabriela Navarro
1 parent
2b0ca56f
Exists in
master
and in
79 other branches
Execute user profile_editor javascript only when needed
Showing
5 changed files
with
378 additions
and
4 deletions
Show diff stats
lib/software_communities_plugin.rb
@@ -119,19 +119,22 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | @@ -119,19 +119,22 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin | ||
119 | end | 119 | end |
120 | 120 | ||
121 | def js_files | 121 | def js_files |
122 | + #mpog-user-validations.js | ||
122 | %w( | 123 | %w( |
123 | vendor/jquery.maskedinput.min.js | 124 | vendor/jquery.maskedinput.min.js |
124 | vendor/modulejs-1.5.0.min.js | 125 | vendor/modulejs-1.5.0.min.js |
125 | vendor/jquery.js | 126 | vendor/jquery.js |
126 | lib/noosfero-root.js | 127 | lib/noosfero-root.js |
127 | lib/select-element.js | 128 | lib/select-element.js |
129 | + lib/select-field-choices | ||
128 | lib/auto-complete.js | 130 | lib/auto-complete.js |
129 | views/control-panel.js | 131 | views/control-panel.js |
130 | views/edit-software.js | 132 | views/edit-software.js |
131 | views/new-software.js | 133 | views/new-software.js |
134 | + views/user-edit-profile.js | ||
132 | initializer.js | 135 | initializer.js |
133 | app.js | 136 | app.js |
134 | - mpog-user-validations.js | 137 | + |
135 | mpog-institution-validations.js | 138 | mpog-institution-validations.js |
136 | mpog-incomplete-registration.js | 139 | mpog-incomplete-registration.js |
137 | mpog-search.js | 140 | mpog-search.js |
public/initializer.js
1 | var dependencies = [ | 1 | var dependencies = [ |
2 | 'ControlPanel', | 2 | 'ControlPanel', |
3 | 'EditSoftware', | 3 | 'EditSoftware', |
4 | - 'NewSoftware' | 4 | + 'NewSoftware', |
5 | + 'UserEditProfile' | ||
5 | ]; | 6 | ]; |
6 | 7 | ||
7 | 8 | ||
8 | -modulejs.define('Initializer', dependencies, function(cp, es, ns) { | 9 | +modulejs.define('Initializer', dependencies, function(cp, es, ns, uep) { |
9 | 'use strict'; | 10 | 'use strict'; |
10 | 11 | ||
11 | 12 | ||
@@ -24,6 +25,11 @@ modulejs.define('Initializer', dependencies, function(cp, es, ns) { | @@ -24,6 +25,11 @@ modulejs.define('Initializer', dependencies, function(cp, es, ns) { | ||
24 | if( ns.isNewSoftware() ) { | 25 | if( ns.isNewSoftware() ) { |
25 | ns.init(); | 26 | ns.init(); |
26 | } | 27 | } |
28 | + | ||
29 | + | ||
30 | + if( uep.isUserEditProfile() ) { | ||
31 | + uep.init(); | ||
32 | + } | ||
27 | } | 33 | } |
28 | }; | 34 | }; |
29 | }); | 35 | }); |
@@ -0,0 +1,81 @@ | @@ -0,0 +1,81 @@ | ||
1 | +modulejs.define('SelectFieldChoices', ['jquery', 'SelectElement'], function($, SelectElement) { | ||
2 | + 'use strict'; | ||
3 | + | ||
4 | + | ||
5 | + function SelectFieldChoices(state_id, city_id, state_url) { | ||
6 | + this.state_id = state_id; | ||
7 | + this.input_html = $(state_id).parent().html(); | ||
8 | + this.old_value = $(state_id).val(); | ||
9 | + this.city_parent_div = $(city_id).parent().parent().parent(); | ||
10 | + this.state_url = state_url; | ||
11 | + } | ||
12 | + | ||
13 | + | ||
14 | + SelectFieldChoices.prototype.getCurrentStateElement = function() { | ||
15 | + return $(this.state_id); | ||
16 | + }; | ||
17 | + | ||
18 | + | ||
19 | + SelectFieldChoices.prototype.replaceWith = function(html) { | ||
20 | + var parent_div = this.getCurrentStateElement().parent(); | ||
21 | + parent_div.html(html); | ||
22 | + }; | ||
23 | + | ||
24 | + | ||
25 | + SelectFieldChoices.prototype.generateSelect = function(state_list) { | ||
26 | + var select_element, option; | ||
27 | + | ||
28 | + select_element = new SelectElement(); | ||
29 | + select_element.setAttr("name", "profile_data[state]"); | ||
30 | + select_element.setAttr("id", "state_field"); | ||
31 | + select_element.setAttr("class", "type-select valid"); | ||
32 | + | ||
33 | + state_list.forEach(function(state) { | ||
34 | + option = SelectElement.generateOption(state, state); | ||
35 | + select_element.addOption(option); | ||
36 | + }); | ||
37 | + | ||
38 | + return select_element.getSelect(); | ||
39 | + }; | ||
40 | + | ||
41 | + | ||
42 | + SelectFieldChoices.prototype.replaceStateWithSelectElement = function() { | ||
43 | + var klass = this; | ||
44 | + | ||
45 | + $.get(this.state_url, function(response) { | ||
46 | + var select_html; | ||
47 | + | ||
48 | + if (response.length > 0) { | ||
49 | + select_html = klass.generateSelect(response); | ||
50 | + klass.replaceWith(select_html); | ||
51 | + | ||
52 | + if (klass.old_value.length !== 0 && response.include(klass.old_value)) { | ||
53 | + klass.getCurrentStateElement().val(klass.old_value); | ||
54 | + } | ||
55 | + } | ||
56 | + }); | ||
57 | + }; | ||
58 | + | ||
59 | + | ||
60 | + SelectFieldChoices.prototype.replaceStateWithInputElement = function() { | ||
61 | + this.replaceWith(this.input_html); | ||
62 | + }; | ||
63 | + | ||
64 | + | ||
65 | + SelectFieldChoices.prototype.hideCity = function() { | ||
66 | + this.city_parent_div.addClass("mpog_hidden_field"); | ||
67 | + }; | ||
68 | + | ||
69 | + | ||
70 | + SelectFieldChoices.prototype.showCity = function() { | ||
71 | + this.city_parent_div.removeClass("mpog_hidden_field"); | ||
72 | + }; | ||
73 | + | ||
74 | + | ||
75 | + SelectFieldChoices.prototype.actualFieldIsInput = function() { | ||
76 | + return this.getCurrentStateElement().attr("type") === "text"; | ||
77 | + }; | ||
78 | + | ||
79 | + | ||
80 | + return SelectFieldChoices; | ||
81 | +}); |
public/mpog-user-validations.js
@@ -220,7 +220,7 @@ | @@ -220,7 +220,7 @@ | ||
220 | return false; | 220 | return false; |
221 | } | 221 | } |
222 | 222 | ||
223 | - var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/); | 223 | + var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/); |
224 | 224 | ||
225 | return !correct_format_regex.test(value); | 225 | return !correct_format_regex.test(value); |
226 | } | 226 | } |
@@ -0,0 +1,284 @@ | @@ -0,0 +1,284 @@ | ||
1 | +modulejs.define('UserEditProfile', ['jquery', 'NoosferoRoot', 'SelectElement', 'SelectFieldChoices'], function($, NoosferoRoot, SelectElement, SelectFieldChoices) { | ||
2 | + 'use strict'; | ||
3 | + | ||
4 | + var AJAX_URL = { | ||
5 | + check_reactivate_account: | ||
6 | + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/check_reactivate_account") | ||
7 | + }; | ||
8 | + | ||
9 | + | ||
10 | + function set_form_count_custom_data() { | ||
11 | + var divisor_option = SelectElement.generateOption("-1", "--------------------------------"); | ||
12 | + var default_option = SelectElement.generateOption("BR", "Brazil"); | ||
13 | + | ||
14 | + $('#profile_data_country').find("option[value='']").remove(); | ||
15 | + $('#profile_data_country').prepend(divisor_option); | ||
16 | + $('#profile_data_country').prepend(default_option); | ||
17 | + $('#profile_data_country').val("BR"); | ||
18 | + } | ||
19 | + | ||
20 | + | ||
21 | + function set_initial_form_custom_data(selectFieldChoices) { | ||
22 | + set_form_count_custom_data(); | ||
23 | + | ||
24 | + $("#password-balloon").html($("#user_password_menssage").val()); | ||
25 | + $("#profile_data_email").parent().append($("#email_public_message").remove()); | ||
26 | + | ||
27 | + if( $("#state_field").length !== 0 ) selectFieldChoices.replaceStateWithSelectElement(); | ||
28 | + } | ||
29 | + | ||
30 | + | ||
31 | + function check_reactivate_account(value, input_object){ | ||
32 | + $.ajax({ | ||
33 | + url : AJAX_URL.check_reactivate_account, | ||
34 | + type: "GET", | ||
35 | + data: { "email": value }, | ||
36 | + success: function(response) { | ||
37 | + if( $("#forgot_link").length === 0 ) | ||
38 | + $(input_object).parent().append(response); | ||
39 | + else | ||
40 | + $("#forgot_link").html(response); | ||
41 | + }, | ||
42 | + error: function(type, err, message) { | ||
43 | + console.log(type+" -- "+err+" -- "+message); | ||
44 | + } | ||
45 | + }); | ||
46 | + } | ||
47 | + | ||
48 | + | ||
49 | + function put_brazil_based_on_email(){ | ||
50 | + var suffixes = ['gov.br', 'jus.br', 'leg.br', 'mp.br']; | ||
51 | + var value = this.value; | ||
52 | + var input_object = this; | ||
53 | + var gov_suffix = false; | ||
54 | + | ||
55 | + suffixes.each(function(suffix){ | ||
56 | + var has_suffix = new RegExp("(.*)"+suffix+"$", "i"); | ||
57 | + | ||
58 | + if( has_suffix.test(value) ) { | ||
59 | + gov_suffix = true; | ||
60 | + $("#profile_data_country").val("BR"); | ||
61 | + } | ||
62 | + }); | ||
63 | + | ||
64 | + $("#profile_data_country").find(':not(:selected)').css('display', (gov_suffix?'none':'block')); | ||
65 | + | ||
66 | + check_reactivate_account(value, input_object); | ||
67 | + } | ||
68 | + | ||
69 | + | ||
70 | + function validate_email_format(){ | ||
71 | + var correct_format_regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; | ||
72 | + | ||
73 | + if( this.value.length > 0 ) { | ||
74 | + if(correct_format_regex.test(this.value)) { | ||
75 | + this.className = "validated"; | ||
76 | + } else { | ||
77 | + this.className = "invalid"; | ||
78 | + } | ||
79 | + } else { | ||
80 | + this.className = ""; | ||
81 | + } | ||
82 | + } | ||
83 | + | ||
84 | + | ||
85 | + function verify_user_password_size() { | ||
86 | + if( this.value.length < 6 ) { | ||
87 | + $(this).switchClass("validated", "invalid"); | ||
88 | + } else { | ||
89 | + $(this).switchClass("invalid", "validated"); | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + | ||
94 | + function show_or_hide_phone_mask() { | ||
95 | + if($("#profile_data_country").val() == "BR") { | ||
96 | + if( (typeof $("#profile_data_cell_phone").data("rawMaskFn") === 'undefined') ) { | ||
97 | + $("#profile_data_cell_phone").mask("(99) 9999?9-9999"); | ||
98 | + $("#profile_data_comercial_phone").mask("(99) 9999?9-9999"); | ||
99 | + $("#profile_data_contact_phone").mask("(99) 9999?9-9999"); | ||
100 | + } | ||
101 | + } else { | ||
102 | + $("#profile_data_cell_phone").unmask(); | ||
103 | + $("#profile_data_comercial_phone").unmask(); | ||
104 | + $("#profile_data_contact_phone").unmask(); | ||
105 | + } | ||
106 | + } | ||
107 | + | ||
108 | + | ||
109 | + function fix_phone_mask_format(id) { | ||
110 | + $(id).blur(function() { | ||
111 | + var last = $(this).val().substr( $(this).val().indexOf("-") + 1 ); | ||
112 | + | ||
113 | + if( last.length == 3 ) { | ||
114 | + var move = $(this).val().substr( $(this).val().indexOf("-") - 1, 1 ); | ||
115 | + var lastfour = move + last; | ||
116 | + var first = $(this).val().substr( 0, 9 ); | ||
117 | + | ||
118 | + $(this).val( first + '-' + lastfour ); | ||
119 | + } | ||
120 | + }); | ||
121 | + } | ||
122 | + | ||
123 | + | ||
124 | + function show_plugin_error_message(field_selector, hidden_message_id ) { | ||
125 | + var field = $(field_selector); | ||
126 | + | ||
127 | + field.removeClass("validated").addClass("invalid"); | ||
128 | + | ||
129 | + if(!$("." + hidden_message_id)[0]) { | ||
130 | + var message = $("#" + hidden_message_id).val(); | ||
131 | + field.parent().append("<div class='" + hidden_message_id + " errorExplanation'>"+message+"</span>"); | ||
132 | + } else { | ||
133 | + $("." + hidden_message_id).show(); | ||
134 | + } | ||
135 | + } | ||
136 | + | ||
137 | + | ||
138 | + function hide_plugin_error_message(field_selector, hidden_message_id) { | ||
139 | + $(field_selector).removeClass("invalid").addClass("validated"); | ||
140 | + $("." + hidden_message_id).hide(); | ||
141 | + } | ||
142 | + | ||
143 | + | ||
144 | + function add_blur_fields(field_selector, hidden_message_id, validation_function, allow_blank) { | ||
145 | + $(field_selector).blur(function(){ | ||
146 | + $(this).attr("class", ""); | ||
147 | + | ||
148 | + if( validation_function(this.value, !!allow_blank) ) { | ||
149 | + show_plugin_error_message(field_selector, hidden_message_id); | ||
150 | + } else { | ||
151 | + hide_plugin_error_message(field_selector, hidden_message_id); | ||
152 | + } | ||
153 | + }); | ||
154 | + } | ||
155 | + | ||
156 | + | ||
157 | + function invalid_email_validation(value, allow_blank) { | ||
158 | + if( allow_blank && value.trim().length === 0 ) { | ||
159 | + return false; | ||
160 | + } | ||
161 | + | ||
162 | + var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/); | ||
163 | + | ||
164 | + return !correct_format_regex.test(value); | ||
165 | + } | ||
166 | + | ||
167 | + | ||
168 | + function invalid_site_validation(value) { | ||
169 | + var correct_format_regex = new RegExp(/(^|)(http[s]{0,1})\:\/\/(\w+[.])\w+/g); | ||
170 | + | ||
171 | + return !correct_format_regex.test(value); | ||
172 | + } | ||
173 | + | ||
174 | + | ||
175 | + function get_privacy_selector_parent_div(field_id, actual) { | ||
176 | + if( actual === undefined ) actual = $(field_id); | ||
177 | + | ||
178 | + if( actual.is("form") || actual.length === 0 ) return null; // Not allow recursion over form | ||
179 | + | ||
180 | + if( actual.hasClass("field-with-privacy-selector") ) { | ||
181 | + return actual; | ||
182 | + } else { | ||
183 | + return get_privacy_selector_parent_div(field_id, actual.parent()); | ||
184 | + } | ||
185 | + } | ||
186 | + | ||
187 | + | ||
188 | + function try_to_remove(list, field) { | ||
189 | + try { | ||
190 | + list.push(field.remove()); | ||
191 | + } catch(e) { | ||
192 | + console.log("Cound not remove field"); | ||
193 | + } | ||
194 | + } | ||
195 | + | ||
196 | + | ||
197 | + function get_edit_fields_in_insertion_order() { | ||
198 | + var containers = []; | ||
199 | + | ||
200 | + try_to_remove(containers, get_privacy_selector_parent_div("#city_field")); | ||
201 | + try_to_remove(containers, get_privacy_selector_parent_div("#state_field")); | ||
202 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_country")); | ||
203 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_birth_date")); | ||
204 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_organization_website")); | ||
205 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_personal_website")); | ||
206 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_comercial_phone")); | ||
207 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_contact_phone")); | ||
208 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_cell_phone")); | ||
209 | + try_to_remove(containers, $("#select_institution")); | ||
210 | + try_to_remove(containers, $("#user_secondary_email").parent().parent()); | ||
211 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_email")); | ||
212 | + try_to_remove(containers, get_privacy_selector_parent_div("#profile_data_name")); | ||
213 | + try_to_remove(containers, $(".pseudoformlabel").parent().parent()); | ||
214 | + try_to_remove(containers, $("h2")[0]); | ||
215 | + | ||
216 | + return containers; | ||
217 | + } | ||
218 | + | ||
219 | + | ||
220 | + function change_edit_fields_order() { | ||
221 | + var form = $("#profile-data"); | ||
222 | + | ||
223 | + if( form.length !== 0 ) { | ||
224 | + var containers = get_edit_fields_in_insertion_order(); | ||
225 | + | ||
226 | + containers.forEach(function(container){ | ||
227 | + form.prepend(container); | ||
228 | + }); | ||
229 | + } | ||
230 | + } | ||
231 | + | ||
232 | + | ||
233 | + function set_fields_validations() { | ||
234 | + $('#secondary_email_field').blur(validate_email_format); | ||
235 | + | ||
236 | + $("#user_email").blur(put_brazil_based_on_email); | ||
237 | + | ||
238 | + $("#user_pw").blur(verify_user_password_size); | ||
239 | + | ||
240 | + $("#profile_data_country").blur(show_or_hide_phone_mask); | ||
241 | + } | ||
242 | + | ||
243 | + | ||
244 | + return { | ||
245 | + isUserEditProfile: function() { | ||
246 | + return $('#profile_data_email').length === 1; | ||
247 | + }, | ||
248 | + | ||
249 | + | ||
250 | + init: function() { | ||
251 | + change_edit_fields_order(); // To change the fields order, it MUST be the first function executed | ||
252 | + | ||
253 | + var selectFieldChoices = new SelectFieldChoices("#state_field", "#city_field", "/plugin/software_communities/get_brazil_states"); | ||
254 | + set_initial_form_custom_data(selectFieldChoices); | ||
255 | + | ||
256 | + | ||
257 | + | ||
258 | + // Event that calls the "Class" to siwtch state field types | ||
259 | + $("#profile_data_country").change(function(){ | ||
260 | + if( this.value == "-1" ) $(this).val("BR"); | ||
261 | + | ||
262 | + if( this.value == "BR" && selectFieldChoices.actualFieldIsInput() ) { | ||
263 | + selectFieldChoices.replaceStateWithSelectElement(); | ||
264 | + selectFieldChoices.showCity(); | ||
265 | + } else if( this.value != "BR" && !selectFieldChoices.actualFieldIsInput() ) { | ||
266 | + selectFieldChoices.replaceStateWithInputElement(); | ||
267 | + selectFieldChoices.hideCity(); | ||
268 | + } | ||
269 | + }); | ||
270 | + | ||
271 | + show_or_hide_phone_mask(); | ||
272 | + $("#profile_data_birth_date").mask("99/99/9999"); | ||
273 | + | ||
274 | + fix_phone_mask_format("#profile_data_cell_phone"); | ||
275 | + fix_phone_mask_format("#profile_data_comercial_phone"); | ||
276 | + fix_phone_mask_format("#profile_data_contact_phone"); | ||
277 | + | ||
278 | + add_blur_fields("#profile_data_email", "email_error", invalid_email_validation); | ||
279 | + add_blur_fields("#user_secondary_email", "email_error", invalid_email_validation, true); | ||
280 | + add_blur_fields("#profile_data_personal_website", "site_error", invalid_site_validation); | ||
281 | + add_blur_fields("#profile_data_organization_website", "site_error", invalid_site_validation); | ||
282 | + } | ||
283 | + } | ||
284 | +}); |