Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added front-end topics validation #4316

Merged
merged 8 commits into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 90 additions & 20 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2302,46 +2302,75 @@ function initNavbarContentToggle() {
}

function initTopicbar() {
var mgrBtn = $("#manage_topic")
var editDiv = $("#topic_edit")
var viewDiv = $("#repo-topic")
var saveBtn = $("#save_topic")
var mgrBtn = $("#manage_topic"),
editDiv = $("#topic_edit"),
viewDiv = $("#repo-topic"),
saveBtn = $("#save_topic"),
topicDropdown = $('#topic_edit .dropdown'),
topicForm = $('#topic_edit.ui.form'),
topicPrompts;

mgrBtn.click(function() {
viewDiv.hide();
editDiv.show();
})
});

function getPrompts() {
var hidePrompt = $("div.hide#validate_prompt"),
prompts = {
countPrompt: hidePrompt.children('#count_prompt').text(),
formatPrompt: hidePrompt.children('#format_prompt').text()
};
hidePrompt.remove();
return prompts;
}

saveBtn.click(function() {
var topics = $("input[name=topics]").val();

$.post($(this).data('link'), {
$.post(saveBtn.data('link'), {
"_csrf": csrf,
"topics": topics
}).success(function(res){
if (res["status"] != "ok") {
alert(res.message);
} else {
}, function(data, textStatus, xhr){
if (xhr.responseJSON.status === 'ok') {
viewDiv.children(".topic").remove();
if (topics.length == 0) {
if (topics.length === 0) {
return
}
var topicArray = topics.split(",");

var last = viewDiv.children("a").last();
for (var i=0;i < topicArray.length; i++) {
for (var i=0; i < topicArray.length; i++) {
$('<div class="ui green basic label topic" style="cursor:pointer;">'+topicArray[i]+'</div>').insertBefore(last)
}
editDiv.hide();
viewDiv.show();
}
}).done(function() {
editDiv.hide();
viewDiv.show();
}).fail(function(xhr) {
alert(xhr.responseJSON.message)
})
}).fail(function(xhr){
if (xhr.status === 422) {
if (xhr.responseJSON.invalidTopics.length > 0) {
topicPrompts.formatPrompt = xhr.responseJSON.message;

var invalidTopics = xhr.responseJSON.invalidTopics,
topicLables = topicDropdown.children('a.ui.label');

topics.split(',').forEach(function(value, index) {
for (var i=0; i < invalidTopics.length; i++) {
if (invalidTopics[i] === value) {
topicLables.eq(index).removeClass("green").addClass("red");
}
}
});
} else {
topicPrompts.countPrompt = xhr.responseJSON.message;
}
}
}).always(function() {
topicForm.form('validate form');
});
});

$('#topic_edit .dropdown').dropdown({
topicDropdown.dropdown({
allowAdditions: true,
fields: { name: "description", value: "data-value" },
saveRemoteData: false,
Expand All @@ -2362,7 +2391,7 @@ function initTopicbar() {
onResponse: function(res) {
var formattedResponse = {
success: false,
results: new Array(),
results: [],
};

if (res.topics) {
Expand All @@ -2375,7 +2404,48 @@ function initTopicbar() {
return formattedResponse;
},
},
onLabelCreate: function(value) {
value = value.toLowerCase().trim();
this.attr("data-value", value).contents().first().replaceWith(value);
return $(this);
},
onAdd: function(addedValue, addedText, $addedChoice) {
addedValue = addedValue.toLowerCase().trim();
$($addedChoice).attr('data-value', addedValue);
$($addedChoice).attr('data-text', addedValue);
}
});

$.fn.form.settings.rules.validateTopic = function(values, regExp) {
var topics = topicDropdown.children('a.ui.label'),
status = topics.length === 0 || topics.last().attr("data-value").match(regExp);
if (!status) {
topics.last().removeClass("green").addClass("red");
}
return status && topicDropdown.children('a.ui.label.red').length === 0;
};

topicPrompts = getPrompts();
topicForm.form({
on: 'change',
inline : true,
fields: {
topics: {
identifier: 'topics',
rules: [
{
type: 'validateTopic',
value: /^[a-z0-9][a-z0-9-]{1,35}$/,
prompt: topicPrompts.formatPrompt
},
{
type: 'maxCount[25]',
prompt: topicPrompts.countPrompt
}
]
},
}
});
}
function toggleDuedateForm() {
$('#add_deadline_form').fadeToggle(150);
Expand Down
2 changes: 1 addition & 1 deletion public/vendor/VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ File(s): /vendor/plugins/jquery/jquery.min.js
Version: 1.11.3

File(s): /vendor/plugins/semantic/semantic.min.js
Version: 2.2.1
Version: 2.3.1

File(s): /vendor/plugins/clipboard/clipboard.min.js
Version: 1.5.9
Expand Down
4 changes: 2 additions & 2 deletions public/vendor/librejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</tr>
<tr>
<td><a href="/vendor/plugins/semantic/semantic.min.js">semantic.min.js</a></td>
<td><a href="http://www.freebsd.org/copyright/freebsd-license.html">Expat</a></td>
<td><a href="https://github.com/Semantic-Org/Semantic-UI/archive/2.2.1.tar.gz">semantic-UI-2.2.1.tar.gz</a></td>
<td><a href="https://semantic-ui.mit-license.org/">Expat</a></td>
<td><a href="https://github.com/Semantic-Org/Semantic-UI/archive/2.3.1.tar.gz">semantic-UI-2.3.1.tar.gz</a></td>
</tr>
<tr>
<td><a href="/js/index.js">index.js</a></td>
Expand Down
357 changes: 355 additions & 2 deletions public/vendor/plugins/semantic/semantic.min.css

Large diffs are not rendered by default.

12 changes: 2 additions & 10 deletions public/vendor/plugins/semantic/semantic.min.js

Large diffs are not rendered by default.

Binary file not shown.
1,008 changes: 1,008 additions & 0 deletions public/vendor/plugins/semantic/themes/default/assets/fonts/brand-icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading