From fbc0dde91f1fe33951aa5a2e57bb1a9f4b0fc4d3 Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Mon, 2 Mar 2015 20:48:49 -0300 Subject: [PATCH] Execute edit_software's javascript only when needed --- lib/software_communities_plugin.rb | 4 +++- public/app.js | 11 +++++++++++ public/initializer.js | 31 ++++++++++++++----------------- public/lib/auto-complete.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public/mpog-software-validations.js | 268 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public/views/control-panel.js | 2 +- public/views/edit-software.js | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 288 insertions(+), 287 deletions(-) create mode 100644 public/app.js create mode 100644 public/lib/auto-complete.js delete mode 100644 public/mpog-software-validations.js create mode 100644 public/views/edit-software.js diff --git a/lib/software_communities_plugin.rb b/lib/software_communities_plugin.rb index fbb5bbf..5fa7e7b 100644 --- a/lib/software_communities_plugin.rb +++ b/lib/software_communities_plugin.rb @@ -125,9 +125,11 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin vendor/jquery.js lib/noosfero-root.js lib/select-element.js + lib/auto-complete.js views/control-panel.js + views/edit-software.js initializer.js - mpog-software-validations.js + app.js mpog-user-validations.js mpog-institution-validations.js mpog-incomplete-registration.js diff --git a/public/app.js b/public/app.js new file mode 100644 index 0000000..7e98375 --- /dev/null +++ b/public/app.js @@ -0,0 +1,11 @@ +(function() { + 'use strict'; + + var $ = modulejs.require('jquery'); + var Initializer = modulejs.require('Initializer'); + + + $(document).ready(function() { + Initializer.init(); + }); +})(); diff --git a/public/initializer.js b/public/initializer.js index 49f38dc..d9fbc47 100644 --- a/public/initializer.js +++ b/public/initializer.js @@ -1,23 +1,20 @@ var dependencies = [ - 'ControlPanel' + 'ControlPanel', + 'EditSoftware' ]; -modulejs.define('Initializer', dependencies, function(cp) { - if( cp.isControlPanel() ) { - cp.init(); - } -}); - - -(function() { - 'use strict'; +modulejs.define('Initializer', dependencies, function(cp, es) { + return { + init: function() { + if( cp.isControlPanel() ) { + cp.init(); + } - var $ = modulejs.require('jquery'); - Initializer = modulejs.require('Initializer'); - - $(document).ready(function() { - Initializer(); - }); -})(); \ No newline at end of file + if( es.isEditSoftware() ) { + es.init(); + } + } + }; +}); diff --git a/public/lib/auto-complete.js b/public/lib/auto-complete.js new file mode 100644 index 0000000..0db1ac3 --- /dev/null +++ b/public/lib/auto-complete.js @@ -0,0 +1,61 @@ +modulejs.define('AutoComplete', ['jquery'], function($) { + function get_hidden_description_field(autocomplete_field, klass) { + var field = $(autocomplete_field); + field = field.parent().parent().find(klass); + return field; + } + + + function verify_autocomplete(field, klass) { + var field = $(field); + var selected = get_hidden_description_field(field, klass); + var message_error = $(field).parent().find(".autocomplete_validation_message"); + + if( field.length === 0 || selected.val().length === 0 ) { + message_error.removeClass("hide-field"); + selected.val(""); + + message_error.show(); + } else { + field.val(selected.attr("data-label")); + message_error.hide(); + } + } + + + function enable_autocomplete(field_name, field_value_class, autocomplete_class, ajax_url, select_callback) { + $(autocomplete_class).autocomplete({ + source : function(request, response){ + $.ajax({ + type: "GET", + url: ajax_url, + data: {query: request.term, field: field_name}, + success: function(result){ + response(result); + } + }); + }, + + minLength: 0, + + select : function (event, selected) { + var description = get_hidden_description_field(this, field_value_class); + description.val(selected.item.id); + description.attr("data-label", selected.item.label); + + if( select_callback !== undefined ) { + select_callback(selected); + } + } + }).blur(function(){ + verify_autocomplete(this, field_value_class); + }).click(function(){ + $(this).autocomplete("search", ""); + }); + } + + + return { + enable: enable_autocomplete + } +}); \ No newline at end of file diff --git a/public/mpog-software-validations.js b/public/mpog-software-validations.js deleted file mode 100644 index 001cfc8..0000000 --- a/public/mpog-software-validations.js +++ /dev/null @@ -1,268 +0,0 @@ -(function($){ - var AJAX_URL = { - get_field_data: - url_with_subdirectory("/plugin/software_communities/get_field_data"), - get_license_data: - url_with_subdirectory("/plugin/software_communities/get_license_data") - }; - - - function get_hidden_description_field(autocomplete_field, klass) { - var field = $(autocomplete_field); - field = field.parent().parent().find(klass); - return field; - } - - - function verify_autocomplete(field, klass) { - var field = $(field); - var selected = get_hidden_description_field(field, klass); - var message_error = $(field).parent().find(".autocomplete_validation_message"); - - if( field.length === 0 || selected.val().length === 0 ) { - message_error.removeClass("hide-field"); - selected.val(""); - - message_error.show(); - } else { - field.val(selected.attr("data-label")); - message_error.hide(); - } - } - - - function database_autocomplete() { - enable_autocomplete("database", ".database_description_id", ".database_autocomplete", AJAX_URL.get_field_data); - } - - - function language_autocomplete() { - enable_autocomplete("software_language", ".programming_language_id", ".language_autocomplete", AJAX_URL.get_field_data); - } - - - function enable_autocomplete(field_name, field_value_class, autocomplete_class, ajax_url, select_callback) { - $(autocomplete_class).autocomplete({ - source : function(request, response){ - $.ajax({ - type: "GET", - url: ajax_url, - data: {query: request.term, field: field_name}, - success: function(result){ - response(result); - } - }); - }, - - minLength: 0, - - select : function (event, selected) { - var description = get_hidden_description_field(this, field_value_class); - description.val(selected.item.id); - description.attr("data-label", selected.item.label); - - if( select_callback !== undefined ) { - select_callback(selected); - } - } - }).blur(function(){ - verify_autocomplete(this, field_value_class); - }).click(function(){ - $(this).autocomplete("search", ""); - }); - } - - - function delete_dynamic_table() { - var button = $(".delete-dynamic-table"); - - button.each(function(){ - var table = $(this).parent().parent().parent().parent(); - var color = table.css("background-color"); - - $(this).click(function(){ - table.remove(); - return false; - }).mouseover(function(){ - table.css("background-color", "#eee"); - }).mouseout(function(){ - table.css("background-color", color); - }); - }); - } - - - function has_more_than_one(table_class) { - return ($("."+table_class).length > 2); // One is always added by defaul and its hidden - } - - - function add_dynamic_table(element_id, content) { - Element.insert(element_id, {bottom: content}); - } - - - function show_another_license_on_page_load() { - $("#license_info_id").trigger("change"); - } - - - function hide_infos() { - $(".language-info").hide(); - $(".database-info").hide(); - $(".libraries-info").hide(); - $(".operating-system-info").hide(); - $(".language-button-hide").hide(); - $(".database-button-hide").hide(); - $(".libraries-button-hide").hide(); - $(".operating-system-button-hide").hide(); - } - - function hide_show_public_software_fields() { - if ($("#software_public_software").prop("checked")) - $(".public-software-fields").show(); - else - $(".public-software-fields").hide(); - } - - - function replace_software_creations_step() { - var software_creation_step = $("#software_creation_step").remove(); - - if( software_creation_step.size() > 0 ) { - $("#profile-data").parent().prepend(software_creation_step); - } - } - - - function display_another_license_fields(selected) { - if( selected == "Another" ) { - $("#another_license").removeClass("hide-field"); - $("#version_link").addClass("hide-field"); - console.log($("#version_link")); - } else { - $("#another_license").addClass("hide-field"); - $("#version_link").removeClass("hide-field"); - } - } - - - function display_license_link_on_autocomplete(selected) { - var link = $("#version_" + selected.item.id).val(); - $("#version_link").attr("href", link); - - display_another_license_fields(selected.item.label); - } - - - function license_info_autocomplete() { - enable_autocomplete( - "license_info", ".license_info_id", ".license_info_version", - AJAX_URL.get_license_data, display_license_link_on_autocomplete - ); - } - - - $(document).ready(function() { - var dynamic_tables = ["dynamic-databases", "dynamic-languages", "dynamic-libraries","dynamic-operating_systems"]; - - delete_dynamic_table(); - database_autocomplete(); - language_autocomplete(); - - $("#community_name_id").blur(function(){ - var community_name = $("#community_name_id").val(); - var domain = $("#software-hostname").text(); - - var slug_name = community_name.replace(/\s+/g, '-').toLowerCase(); - - var custom_domain = domain.concat('/'); - custom_domain = domain.concat(slug_name); - - $("#community_name").val(slug_name); - $("#software_info_repository_link").val(custom_domain); - }); - - $(".new-dynamic-table").click(function(){ - var link = $(this); - - dynamic_tables.each(function(value){ - if( link.hasClass(value) ) { - var table_id = value.split("-")[1]; - - var table_html = $("#table_structure_"+table_id).html(); - add_dynamic_table(table_id, table_html); - } - }); - - delete_dynamic_table(); - database_autocomplete(); - language_autocomplete(); - return false; - }); - - - $(".language-button-hide").click(function(event){ - event.preventDefault(); - $(".language-info").hide(); - $(".language-button-show").show(); - $(".language-button-hide").hide(); - }); - - $(".language-button-show").click(function(event){ - event.preventDefault(); - $(".language-info").show(); - $(".language-button-show").hide(); - $(".language-button-hide").show(); - }); - - $(".operating-system-button-hide").click(function(event){ - event.preventDefault(); - $(".operating-system-info").hide(); - $(".operating-system-button-show").show(); - $(".operating-system-button-hide").hide(); - }); - - $(".operating-system-button-show").click(function(event){ - event.preventDefault(); - $(".operating-system-info").show(); - $(".operating-system-button-show").hide(); - $(".operating-system-button-hide").show(); - }); - - $(".database-button-hide").click(function(event){ - event.preventDefault(); - $(".database-info").hide(); - $(".database-button-show").show(); - $(".database-button-hide").hide(); - }); - - $(".database-button-show").click(function(event){ - event.preventDefault(); - $(".database-info").show(); - $(".database-button-show").hide(); - $(".database-button-hide").show(); - }); - - $(".libraries-button-hide").click(function(event){ - event.preventDefault(); - $(".libraries-info").hide(); - $(".libraries-button-show").show(); - $(".libraries-button-hide").hide(); - }); - - $(".libraries-button-show").click(function(event){ - event.preventDefault(); - $(".libraries-info").show(); - $(".libraries-button-show").hide(); - $(".libraries-button-hide").show(); - }); - - hide_show_public_software_fields(); - $("#software_public_software").click(hide_show_public_software_fields); - - replace_software_creations_step(); - - license_info_autocomplete(); - }); -})(jQuery); diff --git a/public/views/control-panel.js b/public/views/control-panel.js index dfec878..deb2910 100644 --- a/public/views/control-panel.js +++ b/public/views/control-panel.js @@ -41,7 +41,7 @@ modulejs.define('ControlPanel', ['jquery'], function($) { return { isControlPanel: function() { - return $("#profile-editor-index").length == 1; + return $("#profile-editor-index").length === 1; }, diff --git a/public/views/edit-software.js b/public/views/edit-software.js new file mode 100644 index 0000000..022d37b --- /dev/null +++ b/public/views/edit-software.js @@ -0,0 +1,198 @@ +modulejs.define('EditSoftware', ['jquery', 'NoosferoRoot', 'AutoComplete'], function($, NoosferoRoot, AutoComplete) { + var AJAX_URL = { + get_field_data: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/get_field_data"), + get_license_data: + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/get_license_data") + }; + + + function database_autocomplete() { + AutoComplete.enable("database", ".database_description_id", ".database_autocomplete", AJAX_URL.get_field_data); + } + + + function language_autocomplete() { + AutoComplete.enable("software_language", ".programming_language_id", ".language_autocomplete", AJAX_URL.get_field_data); + } + + + function delete_dynamic_table() { + var button = $(".delete-dynamic-table"); + + button.each(function(){ + var table = $(this).parent().parent().parent().parent(); + var color = table.css("background-color"); + + $(this).click(function(){ + table.remove(); + return false; + }).mouseover(function(){ + table.css("background-color", "#eee"); + }).mouseout(function(){ + table.css("background-color", color); + }); + }); + } + + + function has_more_than_one(table_class) { + return ($("."+table_class).length > 2); // One is always added by defaul and its hidden + } + + + function add_dynamic_table(element_id, content) { + $("#"+element_id).append(content); + } + + + function show_another_license_on_page_load() { + $("#license_info_id").trigger("change"); + } + + + function hide_show_public_software_fields() { + if ($("#software_public_software").is(":checked")) { + $(".public-software-fields").show(); + } else { + $(".public-software-fields").hide(); + } + } + + + function replace_software_creations_step() { + var software_creation_step = $("#software_creation_step").remove(); + + if( software_creation_step.size() > 0 ) { + $("#profile-data").parent().prepend(software_creation_step); + } + } + + + function display_another_license_fields(selected) { + if( selected == "Another" ) { + $("#another_license").removeClass("hide-field"); + $("#version_link").addClass("hide-field"); + console.log($("#version_link")); + } else { + $("#another_license").addClass("hide-field"); + $("#version_link").removeClass("hide-field"); + } + } + + + function display_license_link_on_autocomplete(selected) { + var link = $("#version_" + selected.item.id).val(); + $("#version_link").attr("href", link); + + display_another_license_fields(selected.item.label); + } + + + function license_info_autocomplete() { + AutoComplete.enable( + "license_info", ".license_info_id", ".license_info_version", + AJAX_URL.get_license_data, display_license_link_on_autocomplete + ); + } + + + return { + isEditSoftware: function() { + return $("#especific-info").length === 1; + }, + + + init: function() { + var dynamic_tables = ["dynamic-databases", "dynamic-languages", "dynamic-libraries","dynamic-operating_systems"]; + + delete_dynamic_table(); + database_autocomplete(); + language_autocomplete(); + + $(".new-dynamic-table").click(function(){ + var link = $(this); + + dynamic_tables.forEach(function(value){ + if( link.hasClass(value) ) { + var table_id = value.split("-")[1]; + + var table_html = $("#table_structure_"+table_id).html(); + + add_dynamic_table(table_id, table_html); + } + }); + + delete_dynamic_table(); + database_autocomplete(); + language_autocomplete(); + + return false; + }); + + + $(".language-button-hide").click(function(event){ + event.preventDefault(); + $(".language-info").hide(); + $(".language-button-show").show(); + $(".language-button-hide").hide(); + }); + + $(".language-button-show").click(function(event){ + event.preventDefault(); + $(".language-info").show(); + $(".language-button-show").hide(); + $(".language-button-hide").show(); + }); + + $(".operating-system-button-hide").click(function(event){ + event.preventDefault(); + $(".operating-system-info").hide(); + $(".operating-system-button-show").show(); + $(".operating-system-button-hide").hide(); + }); + + $(".operating-system-button-show").click(function(event){ + event.preventDefault(); + $(".operating-system-info").show(); + $(".operating-system-button-show").hide(); + $(".operating-system-button-hide").show(); + }); + + $(".database-button-hide").click(function(event){ + event.preventDefault(); + $(".database-info").hide(); + $(".database-button-show").show(); + $(".database-button-hide").hide(); + }); + + $(".database-button-show").click(function(event){ + event.preventDefault(); + $(".database-info").show(); + $(".database-button-show").hide(); + $(".database-button-hide").show(); + }); + + $(".libraries-button-hide").click(function(event){ + event.preventDefault(); + $(".libraries-info").hide(); + $(".libraries-button-show").show(); + $(".libraries-button-hide").hide(); + }); + + $(".libraries-button-show").click(function(event){ + event.preventDefault(); + $(".libraries-info").show(); + $(".libraries-button-show").hide(); + $(".libraries-button-hide").show(); + }); + + hide_show_public_software_fields(); + $("#software_public_software").click(hide_show_public_software_fields); + + replace_software_creations_step(); + + license_info_autocomplete(); + } + } +}); -- libgit2 0.21.2