mpog-software-validations.js
7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(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);