-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Changes from 1 commit
82f6df1
c0e1869
cb11761
0018e80
fd7a376
723d722
9302fe2
87b2a43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"), | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(","); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -2360,22 +2389,63 @@ function initTopicbar() { | |
throttle: 500, | ||
cache: false, | ||
onResponse: function(res) { | ||
var formattedResponse = { | ||
let formattedResponse = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you keep |
||
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); | ||
|
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
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
tovar
?