From dae22f9d68c67e4fbb0b0202ec0db0e0bf99c2c1 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 6 May 2015 21:28:31 -0500
Subject: [PATCH] variable: Auto expand if user clicks `>`

---
 js/redactor-plugins.js        | 15 ++++++++++++---
 scp/js/bootstrap-typeahead.js |  9 +++++----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/js/redactor-plugins.js b/js/redactor-plugins.js
index 19544786b..33edff821 100644
--- a/js/redactor-plugins.js
+++ b/js/redactor-plugins.js
@@ -1803,11 +1803,13 @@ RedactorPlugins.contexttypeahead = function() {
       }
     },
 
-    select: function(item) {
+    select: function(item, event) {
       var current = this.selection.getCurrent(),
           sel     = this.selection.get(),
           range   = this.sel.getRangeAt(0),
           cursorAt = range.endOffset,
+          // TODO: Consume immediately following `}` symbols
+          plugin  = this.contexttypeahead,
           search  = new RegExp(/%\{([^}]*)(\}?)$/);
 
       // FIXME: ENTER will end up here, but current will be empty
@@ -1818,7 +1820,9 @@ RedactorPlugins.contexttypeahead = function() {
       // Set cursor at the end of the expanded text
       var left = current.textContent.substring(0, cursorAt),
           right = current.textContent.substring(cursorAt),
-          newLeft = left.replace(search, '%{' + item.variable + '}');
+          autoExpand = event.target.nodeName == 'I',
+          selected = item.variable + (autoExpand ? '.' : '')
+          newLeft = left.replace(search, '%{' + selected + '}');
 
       current.textContent = newLeft
         // Drop the remaining part of a variable block, if any
@@ -1827,7 +1831,12 @@ RedactorPlugins.contexttypeahead = function() {
       this.range.setStart(current, newLeft.length - 1);
       this.range.setEnd(current, newLeft.length - 1);
       this.selection.addRange();
-      return this.contexttypeahead.destroy();
+      if (!autoExpand)
+          return plugin.destroy();
+
+      plugin.typeahead.val(selected);
+      plugin.typeahead.typeahead('lookup');
+      return false;
     }
   };
 };
diff --git a/scp/js/bootstrap-typeahead.js b/scp/js/bootstrap-typeahead.js
index 6c8238ad1..7ddc01f61 100644
--- a/scp/js/bootstrap-typeahead.js
+++ b/scp/js/bootstrap-typeahead.js
@@ -41,7 +41,7 @@
 
     constructor: Typeahead
 
-  , select: function () {
+  , select: function (e) {
       var val = JSON.parse(this.$menu.find('.active').attr('data-value'))
         , text
 
@@ -51,7 +51,8 @@
       this.$element.val(text)
 
       if (typeof this.onselect == "function")
-          this.onselect(val)
+          if (false === this.onselect(val, e))
+              return;
 
       return this.hide()
     }
@@ -252,7 +253,7 @@
         case 9: // tab
         case 13: // enter
           if (!this.shown) return
-          this.select()
+          this.select(e)
           break
 
         case 27: // escape
@@ -298,7 +299,7 @@
   , click: function (e) {
       e.stopPropagation()
       e.preventDefault()
-      this.select()
+      this.select(e)
     }
 
   , mouseenter: function (e) {
-- 
GitLab