[fixes] [enhancement] autocomplete in tags and other fixes frappe/erpnext#3049

This commit is contained in:
Rushabh Mehta 2015-04-15 14:54:21 +05:30
parent 14432af06d
commit eecb2a3129
3 changed files with 41 additions and 7 deletions

View file

@ -55,6 +55,19 @@ def remove_tag():
DocTags(dt).remove(dn, tag)
@frappe.whitelist()
def get_tags(doctype, txt):
tags = []
try:
for _user_tags in frappe.db.sql_list("""select `_user_tags`
from `tab{0}`
where _user_tags like '%{1}%'
limit 1""".format(doctype, frappe.db.escape(txt))):
tags.extend(_user_tags.split(","))
except Exception, e:
if e.args[0]!=1054: raise
return sorted(filter(lambda t: t and txt in t, list(set(tags))))
class DocTags:
"""Tags for a particular doctype"""

View file

@ -46,7 +46,7 @@ frappe.ui.form.Sidebar = Class.extend({
this.frm.assign_to.refresh();
this.frm.attachments.refresh();
this.frm.shared.refresh();
this.frm.tags && this.frm.tags.refresh();
this.frm.tags && this.frm.tags.refresh(this.frm.doc._user_tags);
this.sidebar.find(".modified-by").html(__("{0} edited this {1}",
["<strong>" + frappe.user.full_name(this.frm.doc.modified_by) + "</strong>",
"<br>" + comment_when(this.frm.doc.modified)]));
@ -64,6 +64,7 @@ frappe.ui.form.Sidebar = Class.extend({
},
make_tags: function() {
var me = this;
if (this.frm.meta.issingle) {
this.sidebar.find(".form-tags").toggle(false);
return;
@ -71,7 +72,10 @@ frappe.ui.form.Sidebar = Class.extend({
this.frm.tags = new frappe.ui.TagEditor({
parent: this.sidebar.find(".tag-area"),
frm: this.frm
frm: this.frm,
on_change: function(user_tags) {
me.frm.doc._user_tags = user_tags;
}
});
},
make_attachments: function() {

View file

@ -53,8 +53,28 @@ frappe.ui.TagEditor = Class.extend({
if (!this.user_tags) {
this.user_tags = "";
}
this.refresh(this.user_tags);
this.initialized = true;
this.refresh(this.user_tags);
this.setup_autocomplete();
},
setup_autocomplete: function() {
var me = this;
this.$w.find("input").autocomplete({
minLength: 0,
minChars: 0,
source: function(request, response) {
frappe.call({
method:"frappe.desk.tags.get_tags",
args:{
doctype: me.frm.doctype,
txt: request.term
},
callback: function(r) {
response(r.message);
}
});
},
});
},
get_args: function(tag) {
return {
@ -73,12 +93,9 @@ frappe.ui.TagEditor = Class.extend({
try {
me.$tags.tagit("removeAll");
if(!user_tags && this.frm)
user_tags = frappe.model.get_value(this.frm.doctype, this.frm.docname, "_user_tags");
if(user_tags) {
$.each(user_tags.split(','), function(i, v) {
me.$tags.tagit("createTag", v);
if(v) { me.$tags.tagit("createTag", v); }
});
}
} catch(e) {