diff --git a/lib/software_communities_plugin.rb b/lib/software_communities_plugin.rb index 6c26dca..f2b2f68 100644 --- a/lib/software_communities_plugin.rb +++ b/lib/software_communities_plugin.rb @@ -119,19 +119,22 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin end def js_files + #mpog-user-validations.js %w( vendor/jquery.maskedinput.min.js vendor/modulejs-1.5.0.min.js vendor/jquery.js lib/noosfero-root.js lib/select-element.js + lib/select-field-choices lib/auto-complete.js views/control-panel.js views/edit-software.js views/new-software.js + views/user-edit-profile.js initializer.js app.js - mpog-user-validations.js + mpog-institution-validations.js mpog-incomplete-registration.js mpog-search.js diff --git a/public/initializer.js b/public/initializer.js index 8783c37..00b55ec 100644 --- a/public/initializer.js +++ b/public/initializer.js @@ -1,11 +1,12 @@ var dependencies = [ 'ControlPanel', 'EditSoftware', - 'NewSoftware' + 'NewSoftware', + 'UserEditProfile' ]; -modulejs.define('Initializer', dependencies, function(cp, es, ns) { +modulejs.define('Initializer', dependencies, function(cp, es, ns, uep) { 'use strict'; @@ -24,6 +25,11 @@ modulejs.define('Initializer', dependencies, function(cp, es, ns) { if( ns.isNewSoftware() ) { ns.init(); } + + + if( uep.isUserEditProfile() ) { + uep.init(); + } } }; }); diff --git a/public/lib/select-field-choices.js b/public/lib/select-field-choices.js new file mode 100644 index 0000000..095d4e1 --- /dev/null +++ b/public/lib/select-field-choices.js @@ -0,0 +1,81 @@ +modulejs.define('SelectFieldChoices', ['jquery', 'SelectElement'], function($, SelectElement) { + 'use strict'; + + + function SelectFieldChoices(state_id, city_id, state_url) { + this.state_id = state_id; + this.input_html = $(state_id).parent().html(); + this.old_value = $(state_id).val(); + this.city_parent_div = $(city_id).parent().parent().parent(); + this.state_url = state_url; + } + + + SelectFieldChoices.prototype.getCurrentStateElement = function() { + return $(this.state_id); + }; + + + SelectFieldChoices.prototype.replaceWith = function(html) { + var parent_div = this.getCurrentStateElement().parent(); + parent_div.html(html); + }; + + + SelectFieldChoices.prototype.generateSelect = function(state_list) { + var select_element, option; + + select_element = new SelectElement(); + select_element.setAttr("name", "profile_data[state]"); + select_element.setAttr("id", "state_field"); + select_element.setAttr("class", "type-select valid"); + + state_list.forEach(function(state) { + option = SelectElement.generateOption(state, state); + select_element.addOption(option); + }); + + return select_element.getSelect(); + }; + + + SelectFieldChoices.prototype.replaceStateWithSelectElement = function() { + var klass = this; + + $.get(this.state_url, function(response) { + var select_html; + + if (response.length > 0) { + select_html = klass.generateSelect(response); + klass.replaceWith(select_html); + + if (klass.old_value.length !== 0 && response.include(klass.old_value)) { + klass.getCurrentStateElement().val(klass.old_value); + } + } + }); + }; + + + SelectFieldChoices.prototype.replaceStateWithInputElement = function() { + this.replaceWith(this.input_html); + }; + + + SelectFieldChoices.prototype.hideCity = function() { + this.city_parent_div.addClass("mpog_hidden_field"); + }; + + + SelectFieldChoices.prototype.showCity = function() { + this.city_parent_div.removeClass("mpog_hidden_field"); + }; + + + SelectFieldChoices.prototype.actualFieldIsInput = function() { + return this.getCurrentStateElement().attr("type") === "text"; + }; + + + return SelectFieldChoices; +}); diff --git a/public/mpog-user-validations.js b/public/mpog-user-validations.js index 48f9699..eb92ded 100644 --- a/public/mpog-user-validations.js +++ b/public/mpog-user-validations.js @@ -220,7 +220,7 @@ return false; } - var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/); + var correct_format_regex = new RegExp(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/); return !correct_format_regex.test(value); } diff --git a/public/views/user-edit-profile.js b/public/views/user-edit-profile.js new file mode 100644 index 0000000..51a78d6 --- /dev/null +++ b/public/views/user-edit-profile.js @@ -0,0 +1,284 @@ +modulejs.define('UserEditProfile', ['jquery', 'NoosferoRoot', 'SelectElement', 'SelectFieldChoices'], function($, NoosferoRoot, SelectElement, SelectFieldChoices) { + 'use strict'; + + var AJAX_URL = { + check_reactivate_account: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/check_reactivate_account") + }; + + + 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 check_reactivate_account(value, input_object){ + $.ajax({ + url : AJAX_URL.check_reactivate_account, + type: "GET", + data: { "email": value }, + success: function(response) { + if( $("#forgot_link").length === 0 ) + $(input_object).parent().append(response); + else + $("#forgot_link").html(response); + }, + error: function(type, err, message) { + console.log(type+" -- "+err+" -- "+message); + } + }); + } + + + function put_brazil_based_on_email(){ + var suffixes = ['gov.br', 'jus.br', 'leg.br', 'mp.br']; + var value = this.value; + var input_object = this; + var gov_suffix = false; + + suffixes.each(function(suffix){ + var has_suffix = new RegExp("(.*)"+suffix+"$", "i"); + + if( has_suffix.test(value) ) { + gov_suffix = true; + $("#profile_data_country").val("BR"); + } + }); + + $("#profile_data_country").find(':not(:selected)').css('display', (gov_suffix?'none':'block')); + + check_reactivate_account(value, input_object); + } + + + function validate_email_format(){ + var correct_format_regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; + + if( this.value.length > 0 ) { + if(correct_format_regex.test(this.value)) { + this.className = "validated"; + } else { + this.className = "invalid"; + } + } else { + this.className = ""; + } + } + + + function verify_user_password_size() { + if( this.value.length < 6 ) { + $(this).switchClass("validated", "invalid"); + } else { + $(this).switchClass("invalid", "validated"); + } + } + + + 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("