From 7eac9970ae98e1d2a3ef1b1fe1e8722a94a3ab90 Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Tue, 3 Mar 2015 15:46:55 -0300 Subject: [PATCH] Execute create institution javascript only when needed --- lib/software_communities_plugin.rb | 2 +- public/initializer.js | 10 ++++++++-- public/mpog-institution-validations.js | 273 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public/views/create-institution.js | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/views/user-edit-profile.js | 4 +++- 5 files changed, 311 insertions(+), 277 deletions(-) delete mode 100644 public/mpog-institution-validations.js create mode 100644 public/views/create-institution.js diff --git a/lib/software_communities_plugin.rb b/lib/software_communities_plugin.rb index 48ae52c..bb7624e 100644 --- a/lib/software_communities_plugin.rb +++ b/lib/software_communities_plugin.rb @@ -131,9 +131,9 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin views/edit-software.js views/new-software.js views/user-edit-profile.js + views/create-institution.js initializer.js app.js - mpog-institution-validations.js mpog-incomplete-registration.js mpog-search.js software-catalog.js diff --git a/public/initializer.js b/public/initializer.js index 00b55ec..713f622 100644 --- a/public/initializer.js +++ b/public/initializer.js @@ -2,11 +2,12 @@ var dependencies = [ 'ControlPanel', 'EditSoftware', 'NewSoftware', - 'UserEditProfile' + 'UserEditProfile', + 'CreateInstitution' ]; -modulejs.define('Initializer', dependencies, function(cp, es, ns, uep) { +modulejs.define('Initializer', dependencies, function(cp, es, ns, uep, ci) { 'use strict'; @@ -30,6 +31,11 @@ modulejs.define('Initializer', dependencies, function(cp, es, ns, uep) { if( uep.isUserEditProfile() ) { uep.init(); } + + + if( ci.isCreateInstitution() ) { + ci.init(); + } } }; }); diff --git a/public/mpog-institution-validations.js b/public/mpog-institution-validations.js deleted file mode 100644 index e05e7eb..0000000 --- a/public/mpog-institution-validations.js +++ /dev/null @@ -1,273 +0,0 @@ -(function(){ - var AJAX_URL = { - create_institution_modal: - url_with_subdirectory("/plugin/software_communities/create_institution"), - new_institution: - url_with_subdirectory("/plugin/software_communities/new_institution"), - institution_already_exists: - url_with_subdirectory("/plugin/software_communities/institution_already_exists"), - get_institutions: - url_with_subdirectory("/plugin/software_communities/get_institutions") - }; - - - function open_create_institution_modal(evt) { - evt.preventDefault(); - - jQuery.get(AJAX_URL.create_institution_modal, function(response){ - jQuery("#institution_dialog").html(response); - - set_form_count_custom_data(); - set_events(); - - jQuery("#institution_dialog").dialog({ - modal: true, - width: 500, - height: 530, - position: 'center', - close: function() { - jQuery("#institution_dialog").html(""); - jQuery('#institution_empty_ajax_message').switchClass("show-field", "hide-field"); - } - }); - }); - } - - function show_public_institutions_fields() { - jQuery(".public-institutions-fields").show(); - } - - function show_private_institutions_fields() { - jQuery(".public-institutions-fields").hide(); - - jQuery("#institutions_governmental_power option").selected(0); - jQuery("#institutions_governmental_sphere option").selected(0); - } - - function get_post_data() { - return { - community : { - name : jQuery("#community_name").val(), - country : jQuery("#community_country").val(), - state : jQuery("#community_state").val(), - city : jQuery("#community_city").val() - }, - institutions : { - cnpj: jQuery("#institutions_cnpj").val(), - type: jQuery("input[name='institutions[type]']:checked").val(), - acronym : jQuery("#institutions_acronym").val(), - governmental_power: jQuery("#institutions_governmental_power").selected().val(), - governmental_sphere: jQuery("#institutions_governmental_sphere").selected().val(), - juridical_nature: jQuery("#institutions_juridical_nature").selected().val(), - corporate_name: jQuery("#institutions_corporate_name").val() - }, - } - } - - function success_ajax_response(response) { - close_loading(); - - if(response.success){ - var institution_name = response.institution_data.name; - var institution_id = response.institution_data.id; - - jQuery("#institution_dialog").html("

"+response.message+"

"); - jQuery("#create_institution_errors").switchClass("show-field", "hide-field"); - - jQuery(".institution_container").append(get_clone_institution_data(institution_id)); - add_selected_institution_to_list(institution_id, institution_name); - - jQuery(".remove-institution").click(remove_institution); - } else { - var errors = ""; - - jQuery("#create_institution_errors").switchClass("hide-field", "show-field").html("

"+response.message+"

"+errors); - } - } - - function save_institution(evt) { - evt.preventDefault(); - - open_loading(jQuery("#loading_message").val()); - jQuery.ajax({ - url: AJAX_URL.new_institution, - data : get_post_data(), - type: "POST", - success: success_ajax_response, - error: function() { - close_loading(); - var error_message = jQuery("#institution_error_message").val(); - jQuery("#create_institution_errors").switchClass("hide-field", "show-field").html("

"+error_message+"

"); - } - }); - } - - function institution_already_exists(){ - if( this.value.length >= 3 ) { - jQuery.get(AJAX_URL.institution_already_exists, {name:this.value}, function(response){ - if( response == true ) { - jQuery("#already_exists_text").switchClass("hide-field", "show-field"); - } else { - jQuery("#already_exists_text").switchClass("show-field", "hide-field"); - } - }); - } - } - - function get_clone_institution_data(value) { - var user_institutions = jQuery(".user_institutions").first().clone(); - user_institutions.val(value); - - return user_institutions; - } - - function institution_autocomplete() { - jQuery("#input_institution").autocomplete({ - source : function(request, response){ - jQuery.ajax({ - type: "GET", - url: AJAX_URL.get_institutions, - data: {query: request.term}, - success: function(result){ - response(result); - - if( result.length == 0 ) { - jQuery('#institution_empty_ajax_message').switchClass("hide-field", "show-field"); - } else { - jQuery('#institution_empty_ajax_message').switchClass("show-field", "hide-field"); - } - }, - error: function(ajax, stat, errorThrown) { - console.log('Link not found : ' + errorThrown); - } - }); - }, - - minLength: 2, - - select : function (event, selected) { - jQuery("#institution_selected").val(selected.item.id).attr("data-name", selected.item.label); - } - }); - } - - function add_selected_institution_to_list(id, name) { - var selected_institution = "
  • "+name; - selected_institution += "
  • "; - - jQuery(".institutions_added").append(selected_institution); - } - - function add_new_institution(evt) { - evt.preventDefault(); - var selected = jQuery("#institution_selected"); - var institution_already_added = jQuery(".institutions_added li[data-institution='"+selected.val()+"']").length; - - if(selected.val().length > 0 && institution_already_added == 0) { - //field that send the institutions to the server - jQuery(".institution_container").append(get_clone_institution_data(selected.val())); - - // Visualy add the selected institution to the list - add_selected_institution_to_list(selected.val(), selected.attr("data-name")); - - // clean the institution flag - selected.val("").attr("data-name", ""); - jQuery("#input_institution").val(""); - - jQuery(".remove-institution").click(remove_institution); - } - } - - function remove_institution(evt) { - evt.preventDefault(); - var code = jQuery(this).parent().attr("data-institution"); - - jQuery(".user_institutions[value="+code+"]").remove(); - jQuery(this).parent().remove(); - } - - function add_mask_to_form_items() { - jQuery(".intitution_cnpj_field").mask("99.999.999/9999-99"); - } - - function show_hide_cnpj_city(country) { - var cnpj = jQuery("#institutions_cnpj").parent().parent(); - var city = jQuery("#community_city").parent().parent(); - var state = jQuery("#community_state").parent().parent(); - - if( country == "-1" ) jQuery("#community_country").val("BR"); - - if( country != "BR" ) { - cnpj.hide(); - city.hide(); - state.hide(); - } else { - cnpj.show(); - city.show(); - state.show(); - } - } - - function institution_type_actions(type) { - if( type == "PublicInstitution" ) - show_public_institutions_fields(); - else - show_private_institutions_fields(); - } - - function set_form_count_custom_data() { - var divisor_option = SelectElement.generateOption("-1", "--------------------------------"); - var default_option = SelectElement.generateOption("BR", "Brazil"); - - var inst_type = jQuery("input[name='institutions[type]']:checked").val(); - var country = jQuery("#community_country").val(); - - institution_type_actions(inst_type); - show_hide_cnpj_city(country); - - if( jQuery('#community_country').find("option[value='']").length == 1 ) { - jQuery('#community_country').find("option[value='']").remove(); - jQuery('#community_country').prepend(divisor_option); - jQuery('#community_country').prepend(default_option); - - if(jQuery("#edit_institution_page").val() == "false"){ - jQuery('#community_country').val("BR"); - show_hide_cnpj_city(jQuery('#community_country').val()); - } - } - } - - function set_events() { - jQuery("#create_institution_link").click(open_create_institution_modal); - - jQuery("input[name='institutions[type]']").click(function(){ - institution_type_actions(this.value); - }); - - jQuery('#save_institution_button').click(save_institution); - - jQuery("#community_name").keyup(institution_already_exists); - - jQuery("#add_new_institution").click(add_new_institution); - - jQuery(".remove-institution").click(remove_institution); - - jQuery("#community_country").change(function(){ - show_hide_cnpj_city(this.value); - }); - - add_mask_to_form_items(); - - institution_autocomplete(); - } - - jQuery(document).ready(function(){ - set_form_count_custom_data(); - set_events(); - }); -})(); diff --git a/public/views/create-institution.js b/public/views/create-institution.js new file mode 100644 index 0000000..b2853b8 --- /dev/null +++ b/public/views/create-institution.js @@ -0,0 +1,299 @@ +modulejs.define('CreateInstitution', ['jquery', 'NoosferoRoot', 'SelectElement'], function($, NoosferoRoot, SelectElement) { + 'use strict'; + + var AJAX_URL = { + create_institution_modal: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/create_institution"), + new_institution: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/new_institution"), + institution_already_exists: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/institution_already_exists"), + get_institutions: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/get_institutions") + }; + + + function open_create_institution_modal(evt) { + evt.preventDefault(); + + $.get(AJAX_URL.create_institution_modal, function(response){ + $("#institution_dialog").html(response); + + set_form_count_custom_data(); + set_events(); + + $("#institution_dialog").dialog({ + modal: true, + width: 500, + height: 530, + position: 'center', + close: function() { + $("#institution_dialog").html(""); + $('#institution_empty_ajax_message').switchClass("show-field", "hide-field"); + } + }); + }); + } + + + function show_public_institutions_fields() { + $(".public-institutions-fields").show(); + } + + + function show_private_institutions_fields() { + $(".public-institutions-fields").hide(); + + $("#institutions_governmental_power option").selected(0); + $("#institutions_governmental_sphere option").selected(0); + } + + + function get_post_data() { + return { + community : { + name : $("#community_name").val(), + country : $("#community_country").val(), + state : $("#community_state").val(), + city : $("#community_city").val() + }, + institutions : { + cnpj: $("#institutions_cnpj").val(), + type: $("input[name='institutions[type]']:checked").val(), + acronym : $("#institutions_acronym").val(), + governmental_power: $("#institutions_governmental_power").selected().val(), + governmental_sphere: $("#institutions_governmental_sphere").selected().val(), + juridical_nature: $("#institutions_juridical_nature").selected().val(), + corporate_name: $("#institutions_corporate_name").val() + }, + } + } + + + function success_ajax_response(response) { + close_loading(); + + if(response.success){ + var institution_name = response.institution_data.name; + var institution_id = response.institution_data.id; + + $("#institution_dialog").html("

    "+response.message+"

    "); + $("#create_institution_errors").switchClass("show-field", "hide-field"); + + $(".institution_container").append(get_clone_institution_data(institution_id)); + add_selected_institution_to_list(institution_id, institution_name); + + $(".remove-institution").click(remove_institution); + } else { + var errors = ""; + + $("#create_institution_errors").switchClass("hide-field", "show-field").html("

    "+response.message+"

    "+errors); + } + } + + + function save_institution(evt) { + evt.preventDefault(); + + open_loading($("#loading_message").val()); + $.ajax({ + url: AJAX_URL.new_institution, + data : get_post_data(), + type: "POST", + success: success_ajax_response, + error: function() { + close_loading(); + var error_message = $("#institution_error_message").val(); + $("#create_institution_errors").switchClass("hide-field", "show-field").html("

    "+error_message+"

    "); + } + }); + } + + + function institution_already_exists(){ + if( this.value.length >= 3 ) { + $.get(AJAX_URL.institution_already_exists, {name:this.value}, function(response){ + if( response == true ) { + $("#already_exists_text").switchClass("hide-field", "show-field"); + } else { + $("#already_exists_text").switchClass("show-field", "hide-field"); + } + }); + } + } + + + function get_clone_institution_data(value) { + var user_institutions = $(".user_institutions").first().clone(); + user_institutions.val(value); + + return user_institutions; + } + + + function institution_autocomplete() { + $("#input_institution").autocomplete({ + source : function(request, response){ + $.ajax({ + type: "GET", + url: AJAX_URL.get_institutions, + data: {query: request.term}, + success: function(result){ + response(result); + + if( result.length == 0 ) { + $('#institution_empty_ajax_message').switchClass("hide-field", "show-field"); + } else { + $('#institution_empty_ajax_message').switchClass("show-field", "hide-field"); + } + }, + error: function(ajax, stat, errorThrown) { + console.log('Link not found : ' + errorThrown); + } + }); + }, + + minLength: 2, + + select : function (event, selected) { + $("#institution_selected").val(selected.item.id).attr("data-name", selected.item.label); + } + }); + } + + + function add_selected_institution_to_list(id, name) { + var selected_institution = "
  • "+name; + selected_institution += "
  • "; + + $(".institutions_added").append(selected_institution); + } + + + function add_new_institution(evt) { + evt.preventDefault(); + var selected = $("#institution_selected"); + var institution_already_added = $(".institutions_added li[data-institution='"+selected.val()+"']").length; + + if(selected.val().length > 0 && institution_already_added == 0) { + //field that send the institutions to the server + $(".institution_container").append(get_clone_institution_data(selected.val())); + + // Visualy add the selected institution to the list + add_selected_institution_to_list(selected.val(), selected.attr("data-name")); + + // clean the institution flag + selected.val("").attr("data-name", ""); + $("#input_institution").val(""); + + $(".remove-institution").click(remove_institution); + } + } + + + function remove_institution(evt) { + evt.preventDefault(); + var code = $(this).parent().attr("data-institution"); + + $(".user_institutions[value="+code+"]").remove(); + $(this).parent().remove(); + } + + + function add_mask_to_form_items() { + $(".intitution_cnpj_field").mask("99.999.999/9999-99"); + } + + + function show_hide_cnpj_city(country) { + var cnpj = $("#institutions_cnpj").parent().parent(); + var city = $("#community_city").parent().parent(); + var state = $("#community_state").parent().parent(); + + if( country == "-1" ) $("#community_country").val("BR"); + + if( country != "BR" ) { + cnpj.hide(); + city.hide(); + state.hide(); + } else { + cnpj.show(); + city.show(); + state.show(); + } + } + + + function institution_type_actions(type) { + if( type == "PublicInstitution" ) + show_public_institutions_fields(); + else + show_private_institutions_fields(); + } + + + function set_form_count_custom_data() { + var divisor_option = SelectElement.generateOption("-1", "--------------------------------"); + var default_option = SelectElement.generateOption("BR", "Brazil"); + + var inst_type = $("input[name='institutions[type]']:checked").val(); + var country = $("#community_country").val(); + + institution_type_actions(inst_type); + show_hide_cnpj_city(country); + + if( $('#community_country').find("option[value='']").length == 1 ) { + $('#community_country').find("option[value='']").remove(); + $('#community_country').prepend(divisor_option); + $('#community_country').prepend(default_option); + + if($("#edit_institution_page").val() == "false"){ + $('#community_country').val("BR"); + show_hide_cnpj_city($('#community_country').val()); + } + } + } + + + function set_events() { + $("#create_institution_link").click(open_create_institution_modal); + + $("input[name='institutions[type]']").click(function(){ + institution_type_actions(this.value); + }); + + $('#save_institution_button').click(save_institution); + + $("#community_name").keyup(institution_already_exists); + + $("#add_new_institution").click(add_new_institution); + + $(".remove-institution").click(remove_institution); + + $("#community_country").change(function(){ + show_hide_cnpj_city(this.value); + }); + + add_mask_to_form_items(); + + institution_autocomplete(); + } + + + return { + isCreateInstitution: function() { + return $("#institution_form").length === 1; + }, + + + init: function() { + set_form_count_custom_data(); + set_events(); + } + } +}); diff --git a/public/views/user-edit-profile.js b/public/views/user-edit-profile.js index 1e70c43..eaedc8b 100644 --- a/public/views/user-edit-profile.js +++ b/public/views/user-edit-profile.js @@ -1,4 +1,4 @@ -modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoices'], function($, SelectElement, SelectFieldChoices) { +modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoices', 'CreateInstitution'], function($, SelectElement, SelectFieldChoices, CreateInstitution) { 'use strict'; function set_form_count_custom_data() { @@ -209,6 +209,8 @@ modulejs.define('UserEditProfile', ['jquery', 'SelectElement', 'SelectFieldChoic show_or_hide_phone_mask(); set_fields_validations(); + + CreateInstitution.init(); } } }); -- libgit2 0.21.2