From eecb2a3129193a0b823dc3a433e6ce2eb09af378 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 15 Apr 2015 14:54:21 +0530 Subject: [PATCH] [fixes] [enhancement] autocomplete in tags and other fixes frappe/erpnext#3049 --- frappe/desk/tags.py | 13 ++++++++++++ frappe/public/js/frappe/form/sidebar.js | 8 ++++++-- frappe/public/js/frappe/ui/tags.js | 27 ++++++++++++++++++++----- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/frappe/desk/tags.py b/frappe/desk/tags.py index a0b7c8ce92..174277314b 100644 --- a/frappe/desk/tags.py +++ b/frappe/desk/tags.py @@ -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""" diff --git a/frappe/public/js/frappe/form/sidebar.js b/frappe/public/js/frappe/form/sidebar.js index 5884de1628..a80bdec7f1 100644 --- a/frappe/public/js/frappe/form/sidebar.js +++ b/frappe/public/js/frappe/form/sidebar.js @@ -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}", ["" + frappe.user.full_name(this.frm.doc.modified_by) + "", "
" + 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() { diff --git a/frappe/public/js/frappe/ui/tags.js b/frappe/public/js/frappe/ui/tags.js index 05eb9e421e..31699a3c67 100644 --- a/frappe/public/js/frappe/ui/tags.js +++ b/frappe/public/js/frappe/ui/tags.js @@ -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) {