Commit fbc0dde91f1fe33951aa5a2e57bb1a9f4b0fc4d3

Authored by Fabio Teixeira
Committed by Gabriela Navarro
1 parent 05fc8a5c

Execute edit_software's javascript only when needed

lib/software_communities_plugin.rb
... ... @@ -125,9 +125,11 @@ class SoftwareCommunitiesPlugin < Noosfero::Plugin
125 125 vendor/jquery.js
126 126 lib/noosfero-root.js
127 127 lib/select-element.js
  128 + lib/auto-complete.js
128 129 views/control-panel.js
  130 + views/edit-software.js
129 131 initializer.js
130   - mpog-software-validations.js
  132 + app.js
131 133 mpog-user-validations.js
132 134 mpog-institution-validations.js
133 135 mpog-incomplete-registration.js
... ...
public/app.js 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +(function() {
  2 + 'use strict';
  3 +
  4 + var $ = modulejs.require('jquery');
  5 + var Initializer = modulejs.require('Initializer');
  6 +
  7 +
  8 + $(document).ready(function() {
  9 + Initializer.init();
  10 + });
  11 +})();
... ...
public/initializer.js
1 1 var dependencies = [
2   - 'ControlPanel'
  2 + 'ControlPanel',
  3 + 'EditSoftware'
3 4 ];
4 5  
5 6  
6   -modulejs.define('Initializer', dependencies, function(cp) {
7   - if( cp.isControlPanel() ) {
8   - cp.init();
9   - }
10   -});
11   -
12   -
13   -(function() {
14   - 'use strict';
  7 +modulejs.define('Initializer', dependencies, function(cp, es) {
  8 + return {
  9 + init: function() {
  10 + if( cp.isControlPanel() ) {
  11 + cp.init();
  12 + }
15 13  
16   - var $ = modulejs.require('jquery');
17   - Initializer = modulejs.require('Initializer');
18 14  
19   -
20   - $(document).ready(function() {
21   - Initializer();
22   - });
23   -})();
24 15 \ No newline at end of file
  16 + if( es.isEditSoftware() ) {
  17 + es.init();
  18 + }
  19 + }
  20 + };
  21 +});
... ...
public/lib/auto-complete.js 0 → 100644
... ... @@ -0,0 +1,61 @@
  1 +modulejs.define('AutoComplete', ['jquery'], function($) {
  2 + function get_hidden_description_field(autocomplete_field, klass) {
  3 + var field = $(autocomplete_field);
  4 + field = field.parent().parent().find(klass);
  5 + return field;
  6 + }
  7 +
  8 +
  9 + function verify_autocomplete(field, klass) {
  10 + var field = $(field);
  11 + var selected = get_hidden_description_field(field, klass);
  12 + var message_error = $(field).parent().find(".autocomplete_validation_message");
  13 +
  14 + if( field.length === 0 || selected.val().length === 0 ) {
  15 + message_error.removeClass("hide-field");
  16 + selected.val("");
  17 +
  18 + message_error.show();
  19 + } else {
  20 + field.val(selected.attr("data-label"));
  21 + message_error.hide();
  22 + }
  23 + }
  24 +
  25 +
  26 + function enable_autocomplete(field_name, field_value_class, autocomplete_class, ajax_url, select_callback) {
  27 + $(autocomplete_class).autocomplete({
  28 + source : function(request, response){
  29 + $.ajax({
  30 + type: "GET",
  31 + url: ajax_url,
  32 + data: {query: request.term, field: field_name},
  33 + success: function(result){
  34 + response(result);
  35 + }
  36 + });
  37 + },
  38 +
  39 + minLength: 0,
  40 +
  41 + select : function (event, selected) {
  42 + var description = get_hidden_description_field(this, field_value_class);
  43 + description.val(selected.item.id);
  44 + description.attr("data-label", selected.item.label);
  45 +
  46 + if( select_callback !== undefined ) {
  47 + select_callback(selected);
  48 + }
  49 + }
  50 + }).blur(function(){
  51 + verify_autocomplete(this, field_value_class);
  52 + }).click(function(){
  53 + $(this).autocomplete("search", "");
  54 + });
  55 + }
  56 +
  57 +
  58 + return {
  59 + enable: enable_autocomplete
  60 + }
  61 +});
0 62 \ No newline at end of file
... ...
public/mpog-software-validations.js
... ... @@ -1,268 +0,0 @@
1   -(function($){
2   - var AJAX_URL = {
3   - get_field_data:
4   - url_with_subdirectory("/plugin/software_communities/get_field_data"),
5   - get_license_data:
6   - url_with_subdirectory("/plugin/software_communities/get_license_data")
7   - };
8   -
9   -
10   - function get_hidden_description_field(autocomplete_field, klass) {
11   - var field = $(autocomplete_field);
12   - field = field.parent().parent().find(klass);
13   - return field;
14   - }
15   -
16   -
17   - function verify_autocomplete(field, klass) {
18   - var field = $(field);
19   - var selected = get_hidden_description_field(field, klass);
20   - var message_error = $(field).parent().find(".autocomplete_validation_message");
21   -
22   - if( field.length === 0 || selected.val().length === 0 ) {
23   - message_error.removeClass("hide-field");
24   - selected.val("");
25   -
26   - message_error.show();
27   - } else {
28   - field.val(selected.attr("data-label"));
29   - message_error.hide();
30   - }
31   - }
32   -
33   -
34   - function database_autocomplete() {
35   - enable_autocomplete("database", ".database_description_id", ".database_autocomplete", AJAX_URL.get_field_data);
36   - }
37   -
38   -
39   - function language_autocomplete() {
40   - enable_autocomplete("software_language", ".programming_language_id", ".language_autocomplete", AJAX_URL.get_field_data);
41   - }
42   -
43   -
44   - function enable_autocomplete(field_name, field_value_class, autocomplete_class, ajax_url, select_callback) {
45   - $(autocomplete_class).autocomplete({
46   - source : function(request, response){
47   - $.ajax({
48   - type: "GET",
49   - url: ajax_url,
50   - data: {query: request.term, field: field_name},
51   - success: function(result){
52   - response(result);
53   - }
54   - });
55   - },
56   -
57   - minLength: 0,
58   -
59   - select : function (event, selected) {
60   - var description = get_hidden_description_field(this, field_value_class);
61   - description.val(selected.item.id);
62   - description.attr("data-label", selected.item.label);
63   -
64   - if( select_callback !== undefined ) {
65   - select_callback(selected);
66   - }
67   - }
68   - }).blur(function(){
69   - verify_autocomplete(this, field_value_class);
70   - }).click(function(){
71   - $(this).autocomplete("search", "");
72   - });
73   - }
74   -
75   -
76   - function delete_dynamic_table() {
77   - var button = $(".delete-dynamic-table");
78   -
79   - button.each(function(){
80   - var table = $(this).parent().parent().parent().parent();
81   - var color = table.css("background-color");
82   -
83   - $(this).click(function(){
84   - table.remove();
85   - return false;
86   - }).mouseover(function(){
87   - table.css("background-color", "#eee");
88   - }).mouseout(function(){
89   - table.css("background-color", color);
90   - });
91   - });
92   - }
93   -
94   -
95   - function has_more_than_one(table_class) {
96   - return ($("."+table_class).length > 2); // One is always added by defaul and its hidden
97   - }
98   -
99   -
100   - function add_dynamic_table(element_id, content) {
101   - Element.insert(element_id, {bottom: content});
102   - }
103   -
104   -
105   - function show_another_license_on_page_load() {
106   - $("#license_info_id").trigger("change");
107   - }
108   -
109   -
110   - function hide_infos() {
111   - $(".language-info").hide();
112   - $(".database-info").hide();
113   - $(".libraries-info").hide();
114   - $(".operating-system-info").hide();
115   - $(".language-button-hide").hide();
116   - $(".database-button-hide").hide();
117   - $(".libraries-button-hide").hide();
118   - $(".operating-system-button-hide").hide();
119   - }
120   -
121   - function hide_show_public_software_fields() {
122   - if ($("#software_public_software").prop("checked"))
123   - $(".public-software-fields").show();
124   - else
125   - $(".public-software-fields").hide();
126   - }
127   -
128   -
129   - function replace_software_creations_step() {
130   - var software_creation_step = $("#software_creation_step").remove();
131   -
132   - if( software_creation_step.size() > 0 ) {
133   - $("#profile-data").parent().prepend(software_creation_step);
134   - }
135   - }
136   -
137   -
138   - function display_another_license_fields(selected) {
139   - if( selected == "Another" ) {
140   - $("#another_license").removeClass("hide-field");
141   - $("#version_link").addClass("hide-field");
142   - console.log($("#version_link"));
143   - } else {
144   - $("#another_license").addClass("hide-field");
145   - $("#version_link").removeClass("hide-field");
146   - }
147   - }
148   -
149   -
150   - function display_license_link_on_autocomplete(selected) {
151   - var link = $("#version_" + selected.item.id).val();
152   - $("#version_link").attr("href", link);
153   -
154   - display_another_license_fields(selected.item.label);
155   - }
156   -
157   -
158   - function license_info_autocomplete() {
159   - enable_autocomplete(
160   - "license_info", ".license_info_id", ".license_info_version",
161   - AJAX_URL.get_license_data, display_license_link_on_autocomplete
162   - );
163   - }
164   -
165   -
166   - $(document).ready(function() {
167   - var dynamic_tables = ["dynamic-databases", "dynamic-languages", "dynamic-libraries","dynamic-operating_systems"];
168   -
169   - delete_dynamic_table();
170   - database_autocomplete();
171   - language_autocomplete();
172   -
173   - $("#community_name_id").blur(function(){
174   - var community_name = $("#community_name_id").val();
175   - var domain = $("#software-hostname").text();
176   -
177   - var slug_name = community_name.replace(/\s+/g, '-').toLowerCase();
178   -
179   - var custom_domain = domain.concat('/');
180   - custom_domain = domain.concat(slug_name);
181   -
182   - $("#community_name").val(slug_name);
183   - $("#software_info_repository_link").val(custom_domain);
184   - });
185   -
186   - $(".new-dynamic-table").click(function(){
187   - var link = $(this);
188   -
189   - dynamic_tables.each(function(value){
190   - if( link.hasClass(value) ) {
191   - var table_id = value.split("-")[1];
192   -
193   - var table_html = $("#table_structure_"+table_id).html();
194   - add_dynamic_table(table_id, table_html);
195   - }
196   - });
197   -
198   - delete_dynamic_table();
199   - database_autocomplete();
200   - language_autocomplete();
201   - return false;
202   - });
203   -
204   -
205   - $(".language-button-hide").click(function(event){
206   - event.preventDefault();
207   - $(".language-info").hide();
208   - $(".language-button-show").show();
209   - $(".language-button-hide").hide();
210   - });
211   -
212   - $(".language-button-show").click(function(event){
213   - event.preventDefault();
214   - $(".language-info").show();
215   - $(".language-button-show").hide();
216   - $(".language-button-hide").show();
217   - });
218   -
219   - $(".operating-system-button-hide").click(function(event){
220   - event.preventDefault();
221   - $(".operating-system-info").hide();
222   - $(".operating-system-button-show").show();
223   - $(".operating-system-button-hide").hide();
224   - });
225   -
226   - $(".operating-system-button-show").click(function(event){
227   - event.preventDefault();
228   - $(".operating-system-info").show();
229   - $(".operating-system-button-show").hide();
230   - $(".operating-system-button-hide").show();
231   - });
232   -
233   - $(".database-button-hide").click(function(event){
234   - event.preventDefault();
235   - $(".database-info").hide();
236   - $(".database-button-show").show();
237   - $(".database-button-hide").hide();
238   - });
239   -
240   - $(".database-button-show").click(function(event){
241   - event.preventDefault();
242   - $(".database-info").show();
243   - $(".database-button-show").hide();
244   - $(".database-button-hide").show();
245   - });
246   -
247   - $(".libraries-button-hide").click(function(event){
248   - event.preventDefault();
249   - $(".libraries-info").hide();
250   - $(".libraries-button-show").show();
251   - $(".libraries-button-hide").hide();
252   - });
253   -
254   - $(".libraries-button-show").click(function(event){
255   - event.preventDefault();
256   - $(".libraries-info").show();
257   - $(".libraries-button-show").hide();
258   - $(".libraries-button-hide").show();
259   - });
260   -
261   - hide_show_public_software_fields();
262   - $("#software_public_software").click(hide_show_public_software_fields);
263   -
264   - replace_software_creations_step();
265   -
266   - license_info_autocomplete();
267   - });
268   -})(jQuery);
public/views/control-panel.js
... ... @@ -41,7 +41,7 @@ modulejs.define('ControlPanel', ['jquery'], function($) {
41 41  
42 42 return {
43 43 isControlPanel: function() {
44   - return $("#profile-editor-index").length == 1;
  44 + return $("#profile-editor-index").length === 1;
45 45 },
46 46  
47 47  
... ...
public/views/edit-software.js 0 → 100644
... ... @@ -0,0 +1,198 @@
  1 +modulejs.define('EditSoftware', ['jquery', 'NoosferoRoot', 'AutoComplete'], function($, NoosferoRoot, AutoComplete) {
  2 + var AJAX_URL = {
  3 + get_field_data:
  4 + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/get_field_data"),
  5 + get_license_data:
  6 + NoosferoRoot.urlWithSubDirectory("/plugin/software_communities/get_license_data")
  7 + };
  8 +
  9 +
  10 + function database_autocomplete() {
  11 + AutoComplete.enable("database", ".database_description_id", ".database_autocomplete", AJAX_URL.get_field_data);
  12 + }
  13 +
  14 +
  15 + function language_autocomplete() {
  16 + AutoComplete.enable("software_language", ".programming_language_id", ".language_autocomplete", AJAX_URL.get_field_data);
  17 + }
  18 +
  19 +
  20 + function delete_dynamic_table() {
  21 + var button = $(".delete-dynamic-table");
  22 +
  23 + button.each(function(){
  24 + var table = $(this).parent().parent().parent().parent();
  25 + var color = table.css("background-color");
  26 +
  27 + $(this).click(function(){
  28 + table.remove();
  29 + return false;
  30 + }).mouseover(function(){
  31 + table.css("background-color", "#eee");
  32 + }).mouseout(function(){
  33 + table.css("background-color", color);
  34 + });
  35 + });
  36 + }
  37 +
  38 +
  39 + function has_more_than_one(table_class) {
  40 + return ($("."+table_class).length > 2); // One is always added by defaul and its hidden
  41 + }
  42 +
  43 +
  44 + function add_dynamic_table(element_id, content) {
  45 + $("#"+element_id).append(content);
  46 + }
  47 +
  48 +
  49 + function show_another_license_on_page_load() {
  50 + $("#license_info_id").trigger("change");
  51 + }
  52 +
  53 +
  54 + function hide_show_public_software_fields() {
  55 + if ($("#software_public_software").is(":checked")) {
  56 + $(".public-software-fields").show();
  57 + } else {
  58 + $(".public-software-fields").hide();
  59 + }
  60 + }
  61 +
  62 +
  63 + function replace_software_creations_step() {
  64 + var software_creation_step = $("#software_creation_step").remove();
  65 +
  66 + if( software_creation_step.size() > 0 ) {
  67 + $("#profile-data").parent().prepend(software_creation_step);
  68 + }
  69 + }
  70 +
  71 +
  72 + function display_another_license_fields(selected) {
  73 + if( selected == "Another" ) {
  74 + $("#another_license").removeClass("hide-field");
  75 + $("#version_link").addClass("hide-field");
  76 + console.log($("#version_link"));
  77 + } else {
  78 + $("#another_license").addClass("hide-field");
  79 + $("#version_link").removeClass("hide-field");
  80 + }
  81 + }
  82 +
  83 +
  84 + function display_license_link_on_autocomplete(selected) {
  85 + var link = $("#version_" + selected.item.id).val();
  86 + $("#version_link").attr("href", link);
  87 +
  88 + display_another_license_fields(selected.item.label);
  89 + }
  90 +
  91 +
  92 + function license_info_autocomplete() {
  93 + AutoComplete.enable(
  94 + "license_info", ".license_info_id", ".license_info_version",
  95 + AJAX_URL.get_license_data, display_license_link_on_autocomplete
  96 + );
  97 + }
  98 +
  99 +
  100 + return {
  101 + isEditSoftware: function() {
  102 + return $("#especific-info").length === 1;
  103 + },
  104 +
  105 +
  106 + init: function() {
  107 + var dynamic_tables = ["dynamic-databases", "dynamic-languages", "dynamic-libraries","dynamic-operating_systems"];
  108 +
  109 + delete_dynamic_table();
  110 + database_autocomplete();
  111 + language_autocomplete();
  112 +
  113 + $(".new-dynamic-table").click(function(){
  114 + var link = $(this);
  115 +
  116 + dynamic_tables.forEach(function(value){
  117 + if( link.hasClass(value) ) {
  118 + var table_id = value.split("-")[1];
  119 +
  120 + var table_html = $("#table_structure_"+table_id).html();
  121 +
  122 + add_dynamic_table(table_id, table_html);
  123 + }
  124 + });
  125 +
  126 + delete_dynamic_table();
  127 + database_autocomplete();
  128 + language_autocomplete();
  129 +
  130 + return false;
  131 + });
  132 +
  133 +
  134 + $(".language-button-hide").click(function(event){
  135 + event.preventDefault();
  136 + $(".language-info").hide();
  137 + $(".language-button-show").show();
  138 + $(".language-button-hide").hide();
  139 + });
  140 +
  141 + $(".language-button-show").click(function(event){
  142 + event.preventDefault();
  143 + $(".language-info").show();
  144 + $(".language-button-show").hide();
  145 + $(".language-button-hide").show();
  146 + });
  147 +
  148 + $(".operating-system-button-hide").click(function(event){
  149 + event.preventDefault();
  150 + $(".operating-system-info").hide();
  151 + $(".operating-system-button-show").show();
  152 + $(".operating-system-button-hide").hide();
  153 + });
  154 +
  155 + $(".operating-system-button-show").click(function(event){
  156 + event.preventDefault();
  157 + $(".operating-system-info").show();
  158 + $(".operating-system-button-show").hide();
  159 + $(".operating-system-button-hide").show();
  160 + });
  161 +
  162 + $(".database-button-hide").click(function(event){
  163 + event.preventDefault();
  164 + $(".database-info").hide();
  165 + $(".database-button-show").show();
  166 + $(".database-button-hide").hide();
  167 + });
  168 +
  169 + $(".database-button-show").click(function(event){
  170 + event.preventDefault();
  171 + $(".database-info").show();
  172 + $(".database-button-show").hide();
  173 + $(".database-button-hide").show();
  174 + });
  175 +
  176 + $(".libraries-button-hide").click(function(event){
  177 + event.preventDefault();
  178 + $(".libraries-info").hide();
  179 + $(".libraries-button-show").show();
  180 + $(".libraries-button-hide").hide();
  181 + });
  182 +
  183 + $(".libraries-button-show").click(function(event){
  184 + event.preventDefault();
  185 + $(".libraries-info").show();
  186 + $(".libraries-button-show").hide();
  187 + $(".libraries-button-hide").show();
  188 + });
  189 +
  190 + hide_show_public_software_fields();
  191 + $("#software_public_software").click(hide_show_public_software_fields);
  192 +
  193 + replace_software_creations_step();
  194 +
  195 + license_info_autocomplete();
  196 + }
  197 + }
  198 +});
... ...