2022-01-28 21:00:11 +00:00
|
|
|
import $ from 'jquery';
|
2021-10-16 17:28:04 +00:00
|
|
|
import {stripTags} from '../utils.js';
|
2023-02-19 04:06:14 +00:00
|
|
|
import {hideElem, showElem} from '../utils/dom.js';
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
const {appSubUrl, csrfToken} = window.config;
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
export function initRepoTopicBar() {
|
|
|
|
const mgrBtn = $('#manage_topic');
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
if (!mgrBtn.length) return;
|
2021-10-16 17:28:04 +00:00
|
|
|
const editDiv = $('#topic_edit');
|
|
|
|
const viewDiv = $('#repo-topics');
|
|
|
|
const saveBtn = $('#save_topic');
|
|
|
|
const topicDropdown = $('#topic_edit .dropdown');
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
const topicForm = editDiv; // the old logic, editDiv is topicForm
|
|
|
|
const topicDropdownSearch = topicDropdown.find('input.search');
|
|
|
|
const topicPrompts = {
|
|
|
|
countPrompt: topicDropdown.attr('data-text-count-prompt'),
|
|
|
|
formatPrompt: topicDropdown.attr('data-text-format-prompt'),
|
|
|
|
};
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
mgrBtn.on('click', () => {
|
2023-02-19 04:06:14 +00:00
|
|
|
hideElem(viewDiv);
|
|
|
|
showElem(editDiv);
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
topicDropdownSearch.focus();
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
|
|
|
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
$('#cancel_topic_edit').on('click', () => {
|
|
|
|
hideElem(editDiv);
|
|
|
|
showElem(viewDiv);
|
|
|
|
mgrBtn.focus();
|
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
saveBtn.on('click', () => {
|
|
|
|
const topics = $('input[name=topics]').val();
|
|
|
|
|
2022-06-06 04:58:53 +00:00
|
|
|
$.post(saveBtn.attr('data-link'), {
|
2021-10-21 07:37:43 +00:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 17:28:04 +00:00
|
|
|
topics
|
|
|
|
}, (_data, _textStatus, xhr) => {
|
|
|
|
if (xhr.responseJSON.status === 'ok') {
|
|
|
|
viewDiv.children('.topic').remove();
|
|
|
|
if (topics.length) {
|
|
|
|
const topicArray = topics.split(',');
|
2023-04-14 19:29:05 +00:00
|
|
|
topicArray.sort();
|
2021-10-16 17:28:04 +00:00
|
|
|
for (let i = 0; i < topicArray.length; i++) {
|
|
|
|
const link = $('<a class="ui repo-topic large label topic"></a>');
|
2021-10-21 07:37:43 +00:00
|
|
|
link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topicArray[i])}&topic=1`);
|
2021-10-16 17:28:04 +00:00
|
|
|
link.text(topicArray[i]);
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
link.insertBefore(mgrBtn); // insert all new topics before manage button
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-19 04:06:14 +00:00
|
|
|
hideElem(editDiv);
|
|
|
|
showElem(viewDiv);
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
}).fail((xhr) => {
|
|
|
|
if (xhr.status === 422) {
|
|
|
|
if (xhr.responseJSON.invalidTopics.length > 0) {
|
|
|
|
topicPrompts.formatPrompt = xhr.responseJSON.message;
|
|
|
|
|
|
|
|
const {invalidTopics} = xhr.responseJSON;
|
2023-04-14 19:29:05 +00:00
|
|
|
const topicLabels = topicDropdown.children('a.ui.label');
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2021-11-22 08:19:01 +00:00
|
|
|
for (const [index, value] of topics.split(',').entries()) {
|
2021-10-16 17:28:04 +00:00
|
|
|
for (let i = 0; i < invalidTopics.length; i++) {
|
|
|
|
if (invalidTopics[i] === value) {
|
2023-04-14 19:29:05 +00:00
|
|
|
topicLabels.eq(index).removeClass('green').addClass('red');
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 08:19:01 +00:00
|
|
|
}
|
2021-10-16 17:28:04 +00:00
|
|
|
} else {
|
|
|
|
topicPrompts.countPrompt = xhr.responseJSON.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).always(() => {
|
|
|
|
topicForm.form('validate form');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
topicDropdown.dropdown({
|
|
|
|
allowAdditions: true,
|
|
|
|
forceSelection: false,
|
|
|
|
fullTextSearch: 'exact',
|
|
|
|
fields: {name: 'description', value: 'data-value'},
|
|
|
|
saveRemoteData: false,
|
|
|
|
label: {
|
|
|
|
transition: 'horizontal flip',
|
|
|
|
duration: 200,
|
|
|
|
variation: false,
|
|
|
|
},
|
|
|
|
apiSettings: {
|
2022-04-07 18:59:56 +00:00
|
|
|
url: `${appSubUrl}/explore/topics/search?q={query}`,
|
2021-10-16 17:28:04 +00:00
|
|
|
throttle: 500,
|
|
|
|
cache: false,
|
|
|
|
onResponse(res) {
|
|
|
|
const formattedResponse = {
|
|
|
|
success: false,
|
|
|
|
results: [],
|
|
|
|
};
|
|
|
|
const query = stripTags(this.urlData.query.trim());
|
|
|
|
let found_query = false;
|
|
|
|
const current_topics = [];
|
Fine tune more downdrop settings, use SVG for labels, improve Repo Topic Edit form (#23626)
Although it seems that some different purposes are mixed in this PR,
however, they are all related, and can be tested together, so I put them
together to save everyone's time.
Diff: `+79 −84`, everything becomes much better.
### Improve the dropdown settings.
Move all fomantic-init related code into our `fomantic.js`
Fine-tune some dropdown global settings, see the comments.
Also help to fix the first problem in #23625 , cc: @yp05327
The "language" menu has been simplified, and it works with small-height
window better.
### Use SVG instead of `<i class="delete icon">`
It's also done by `$.fn.dropdown.settings.templates.label` , cc:
@silverwind
### Remove incorrect `tabable` CSS class
It doesn't have CSS styles, and it was only in Vue. So it's totally
unnecessary, remove it by the way.
### Improve the Repo Topic Edit form
* Simplify the code
* Add a "Cancel" button
* Align elements
Before:
<details>
![image](https://user-images.githubusercontent.com/2114189/223325782-f09532de-0c38-4742-ba86-ed35cc9a858d.png)
</details>
After:
![image](https://user-images.githubusercontent.com/2114189/226796347-207feb0a-b3cd-4820-8a3e-01930bab1069.png)
2023-03-26 11:31:26 +00:00
|
|
|
topicDropdown.find('a.label.visible').each((_, el) => {
|
2021-11-22 08:19:01 +00:00
|
|
|
current_topics.push(el.getAttribute('data-value'));
|
|
|
|
});
|
2021-10-16 17:28:04 +00:00
|
|
|
|
|
|
|
if (res.topics) {
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < res.topics.length; i++) {
|
|
|
|
// skip currently added tags
|
|
|
|
if (current_topics.includes(res.topics[i].topic_name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.topics[i].topic_name.toLowerCase() === query.toLowerCase()) {
|
|
|
|
found_query = true;
|
|
|
|
}
|
|
|
|
formattedResponse.results.push({description: res.topics[i].topic_name, 'data-value': res.topics[i].topic_name});
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
formattedResponse.success = found;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.length > 0 && !found_query) {
|
|
|
|
formattedResponse.success = true;
|
|
|
|
formattedResponse.results.unshift({description: query, 'data-value': query});
|
|
|
|
} else if (query.length > 0 && found_query) {
|
|
|
|
formattedResponse.results.sort((a, b) => {
|
|
|
|
if (a.description.toLowerCase() === query.toLowerCase()) return -1;
|
|
|
|
if (b.description.toLowerCase() === query.toLowerCase()) return 1;
|
|
|
|
if (a.description > b.description) return -1;
|
|
|
|
if (a.description < b.description) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedResponse;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onLabelCreate(value) {
|
|
|
|
value = value.toLowerCase().trim();
|
|
|
|
this.attr('data-value', value).contents().first().replaceWith(value);
|
|
|
|
return $(this);
|
|
|
|
},
|
|
|
|
onAdd(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) {
|
|
|
|
const topics = topicDropdown.children('a.ui.label');
|
|
|
|
const 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
topicForm.form({
|
|
|
|
on: 'change',
|
|
|
|
inline: true,
|
|
|
|
fields: {
|
|
|
|
topics: {
|
|
|
|
identifier: 'topics',
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
type: 'validateTopic',
|
|
|
|
value: /^[a-z0-9][a-z0-9-]{0,35}$/,
|
|
|
|
prompt: topicPrompts.formatPrompt
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'maxCount[25]',
|
|
|
|
prompt: topicPrompts.countPrompt
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|