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 1 commit
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
122 changes: 96 additions & 26 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")
let mgrBtn = $("#manage_topic"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this let to var?

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() {
let 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();
let topics = $("input[name=topics]").val();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above re: let/var


$.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(",");
let topicArray = topics.split(",");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


var last = viewDiv.children("a").last();
for (var i=0;i < topicArray.length; i++) {
$('<div class="ui green basic label topic" style="cursor:pointer;">'+topicArray[i]+'</div>').insertBefore(last)
let last = viewDiv.children("a").last();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

for (let topic of topicArray) {
$('<div class="ui green basic label topic" style="cursor:pointer;">'+topic+'</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;

let invalidTopics = xhr.responseJSON.invalidTopics,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

topicLables = topicDropdown.children('a.ui.label');

topics.split(',').forEach((value, index) => {
for (let val of invalidTopics) {
if (val === 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 @@ -2360,22 +2389,63 @@ function initTopicbar() {
throttle: 500,
cache: false,
onResponse: function(res) {
var formattedResponse = {
let formattedResponse = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you keep var? The versions of IE we support don't support newer ECMAscript

success: false,
results: new Array(),
results: [],
};

if (res.topics) {
formattedResponse.success = true;
for (var i=0;i < res.topics.length;i++) {
for (let i=0;i < res.topics.length;i++) {
formattedResponse.results.push({"description": res.topics[i].Name, "data-value":res.topics[i].Name})
}
}

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) {
let 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.2

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.2.tar.gz">semantic-UI-2.3.2.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
100644 → 100755

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