From bd38d1436e22744bf52fc3ef79a87a23671e0840 Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Wed, 9 Feb 2011 15:51:28 -0800
Subject: [PATCH] Today we learned about the order of operations

---
 .../javascripts/jquery.autocomplete-custom.js | 20 ++++++++-----------
 public/javascripts/keycodes.js                |  1 +
 public/javascripts/publisher.js               | 18 ++++++++++-------
 3 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/public/javascripts/jquery.autocomplete-custom.js b/public/javascripts/jquery.autocomplete-custom.js
index b9171888ac..e10b9de6d4 100644
--- a/public/javascripts/jquery.autocomplete-custom.js
+++ b/public/javascripts/jquery.autocomplete-custom.js
@@ -52,18 +52,7 @@ $.fn.extend({
 
 $.Autocompleter = function(input, options) {
 
-	var KEY = {
-		UP: 38,
-		DOWN: 40,
-		DEL: 46,
-		TAB: 9,
-		RETURN: 13,
-		ESC: 27,
-		COMMA: 188,
-		PAGEUP: 33,
-		PAGEDOWN: 34,
-		BACKSPACE: 8
-	};
+	var KEY = KEYCODES;
 
 	// Create $ object for input element
 	var $input = $(input).attr("autocomplete", "off").addClass(options.inputClass);
@@ -94,6 +83,12 @@ $.Autocompleter = function(input, options) {
 		lastKeyPressCode = event.keyCode;
 		switch(event.keyCode) {
 
+			case KEY.LEFT:
+      case KEY.RIGHT:
+        if( options.disableRightAndLeft && select.visible()){
+          event.preventDefault();
+        }
+        break;
 			case KEY.UP:
 				if ( select.visible() ) {
           event.preventDefault();
@@ -408,6 +403,7 @@ $.Autocompleter.defaults = {
 	width: 0,
 	multiple: false,
 	multipleSeparator: ", ",
+  disableRightAndLeft: false,
 	highlight: function(value, term) {
 		return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
 	},
diff --git a/public/javascripts/keycodes.js b/public/javascripts/keycodes.js
index 6138b08acd..ca275d1ce5 100644
--- a/public/javascripts/keycodes.js
+++ b/public/javascripts/keycodes.js
@@ -10,6 +10,7 @@ PAUSE : 19,
 BREAK : 19,
 CAPSLOCK : 20,
 ESCAPE : 27,
+ESC : 27,
 SPACEBAR : 32,
 PAGEUP : 33,
 PAGEDOWN : 34,
diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js
index 1b3f30a044..ac5bd9c6d4 100644
--- a/public/javascripts/publisher.js
+++ b/public/javascripts/publisher.js
@@ -53,7 +53,8 @@ var Publisher = {
       },
       formatResult: function(row) {
           return row.name;
-      }
+      },
+      disableRightAndLeft : true
     };},
     hiddenMentionFromPerson : function(personData){
       return "@{" + personData.name + "; " + personData.handle + "}";
@@ -104,7 +105,9 @@ var Publisher = {
       },
 
       insertionAt : function(insertionStartIndex, selectionEnd, keyCode){
-        this.selectionDeleted(insertionStartIndex, selectionEnd);
+        if(insertionStartIndex != selectionEnd){
+          this.selectionDeleted(insertionStartIndex, selectionEnd);
+        }
         this.updateMentionLocations(insertionStartIndex, 1);
         this.destroyMentionAt(insertionStartIndex);
       },
@@ -188,26 +191,27 @@ var Publisher = {
     keyDownHandler : function(event){
       var input = Publisher.input();
       var selectionStart = input[0].selectionStart;
-      var isDeletion = (event.keyCode == KEYCODES.DEL && selectionStart < input.val().length) || (event.keyCode == KEYCODES.BACKSPACE && selectionStart > 0)
+      var selectionEnd = input[0].selectionEnd;
+      var isDeletion = (event.keyCode == KEYCODES.DEL && selectionStart < input.val().length) || (event.keyCode == KEYCODES.BACKSPACE && (selectionStart > 0 || selectionStart != selectionEnd))
       var isInsertion = (KEYCODES.isInsertion(event.keyCode) && event.keyCode != KEYCODES.RETURN )
 
       if(isDeletion){
-        Publisher.autocompletion.mentionList.deletionAt(selectionStart, input[0].selectionEnd, event.keyCode);
+        Publisher.autocompletion.mentionList.deletionAt(selectionStart, selectionEnd, event.keyCode);
       }else if(isInsertion){
-        Publisher.autocompletion.mentionList.insertionAt(selectionStart, input[0].selectionEnd, event.keyCode);
+        Publisher.autocompletion.mentionList.insertionAt(selectionStart, selectionEnd, event.keyCode);
       }
     },
 
     addMentionToInput: function(input, cursorIndex, formatted){
       var inputContent = input.val();
 
-      var stringLoc = Publisher.autocompletion.findStringToReplace(input.val(), cursorIndex);
+      var stringLoc = Publisher.autocompletion.findStringToReplace(inputContent, cursorIndex);
 
       var stringStart = inputContent.slice(0, stringLoc[0]);
       var stringEnd = inputContent.slice(stringLoc[1]);
 
       input.val(stringStart + formatted + stringEnd);
-      var offset = formatted.length - stringLoc[1] - stringLoc[0]
+      var offset = formatted.length - (stringLoc[1] - stringLoc[0])
       Publisher.autocompletion.mentionList.updateMentionLocations(stringStart.length, offset);
       return [stringStart.length, stringStart.length + formatted.length]
     },
-- 
GitLab