refactor: Move mention list generation logic to server-side
- Moved mention list generation logic to server-side to get latest mention list everytime
- To indicate group option, added a users icon.
(cherry picked from commit 1af59ce16c)
This commit is contained in:
parent
691b2689d9
commit
8cd8f32993
4 changed files with 49 additions and 24 deletions
|
|
@ -221,3 +221,37 @@ def validate_and_sanitize_search_inputs(fn, instance, args, kwargs):
|
|||
return []
|
||||
|
||||
return fn(**kwargs)
|
||||
|
||||
|
||||
@frappe.whitelist()
|
||||
def get_names_for_mentions(search_term):
|
||||
users_for_mentions = frappe.cache().get_value('users_for_mentions', get_users_for_mentions)
|
||||
user_groups = frappe.cache().get_value('users_groups', get_user_groups)
|
||||
|
||||
filtered_mentions = []
|
||||
for mention_data in users_for_mentions + user_groups:
|
||||
if search_term.lower() not in mention_data.value.lower():
|
||||
continue
|
||||
|
||||
mention_data['link'] = frappe.utils.get_url_to_form(
|
||||
'User Group' if mention_data.get('is_group') else 'User',
|
||||
mention_data['id']
|
||||
)
|
||||
|
||||
filtered_mentions.append(mention_data)
|
||||
|
||||
return sorted(filtered_mentions, key=lambda d: d['value'])
|
||||
|
||||
def get_users_for_mentions():
|
||||
return frappe.get_all('User',
|
||||
fields=['name as id', 'full_name as value'],
|
||||
filters={
|
||||
'name': ['not in', ('Administrator', 'Guest')],
|
||||
'allowed_in_mentions': True,
|
||||
'user_type': 'System User',
|
||||
})
|
||||
|
||||
def get_user_groups():
|
||||
return frappe.get_all('User Group', fields=['name as id', 'name as value'], update={
|
||||
'is_group': True
|
||||
})
|
||||
|
|
|
|||
|
|
@ -78,35 +78,26 @@ frappe.ui.form.ControlComment = frappe.ui.form.ControlTextEditor.extend({
|
|||
},
|
||||
|
||||
get_mention_options() {
|
||||
if (!(this.mentions && this.mentions.length)) {
|
||||
if (!this.enable_mentions) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const at_values = this.mentions.slice();
|
||||
|
||||
let me = this;
|
||||
return {
|
||||
allowedChars: /^[A-Za-z0-9_]*$/,
|
||||
mentionDenotationChars: ["@"],
|
||||
isolateCharacter: true,
|
||||
source: function (searchTerm, renderList, mentionChar) {
|
||||
let values;
|
||||
source: frappe.utils.debounce(async function(search_term, renderList) {
|
||||
let method = me.mention_search_method || 'frappe.desk.search.get_names_for_mentions';
|
||||
let values = await frappe.xcall(method, {
|
||||
search_term
|
||||
});
|
||||
renderList(values, search_term);
|
||||
}, 300),
|
||||
renderItem(item) {
|
||||
let value = item.value;
|
||||
return `${value} ${item.is_group ? frappe.utils.icon('users') : ''}`;
|
||||
|
||||
if (mentionChar === "@") {
|
||||
values = at_values;
|
||||
}
|
||||
|
||||
if (searchTerm.length === 0) {
|
||||
renderList(values, searchTerm);
|
||||
} else {
|
||||
const matches = [];
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
if (~values[i].value.toLowerCase().indexOf(searchTerm.toLowerCase())) {
|
||||
matches.push(values[i]);
|
||||
}
|
||||
}
|
||||
renderList(matches, searchTerm);
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ frappe.ui.form.Footer = Class.extend({
|
|||
parent: this.wrapper.find(".comment-box"),
|
||||
render_input: true,
|
||||
only_input: true,
|
||||
mentions: frappe.utils.get_names_for_mentions(),
|
||||
enable_mentions: true,
|
||||
df: {
|
||||
fieldtype: 'Comment',
|
||||
fieldname: 'comment'
|
||||
|
|
|
|||
|
|
@ -492,7 +492,7 @@ class FormTimeline extends BaseTimeline {
|
|||
fieldname: 'comment',
|
||||
label: 'Comment'
|
||||
},
|
||||
mentions: frappe.utils.get_names_for_mentions(),
|
||||
enable_mentions: true,
|
||||
render_input: true,
|
||||
only_input: true,
|
||||
no_wrapper: true
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue