Permalink
Browse files

Converts plugin to do multi-trigger multi-source autocomplete functio…

…nality.
  • Loading branch information...
irobinson committed Jan 15, 2012
1 parent 3bd52a4 commit a15db38624c047fa2df63f90ef829eaee75d1902
Showing with 67 additions and 36 deletions.
  1. +19 −0 demo.html
  2. +48 −36 jquery-ui.autocompleteTrigger.js → jquery-ui.autocompleteMultiTrigger.js
View
@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang=en>
+<meta charset=utf-8>
+<title>test</title>
+<body>
+ <textarea class="ac_input"></textarea>
+</body>
+</html>
+
+<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/base/jquery-ui.css"/>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>
+<script type="text/javascript" src="jquery-ui.autocompleteMultiTrigger.js"></script>
+
+<script type="text/javascript">
+$(document).ready(function(){
+ $('textarea').autocompleteMultiTrigger();
+});
+</script>
@@ -49,15 +49,15 @@
;
(function($, window, document, undefined) {
- $.widget("ui.autocompleteTrigger", {
+ $.widget("ui.autocompleteMultiTrigger", {
//Options to be used as defaults
options:{
- triggerStart:"%{",
- triggerEnd:"}"
+ triggerStart:['@','#'],
+ triggerEnd:' ',
+ sources: [ ['sample', 'first', 'source'],['second','test','data','set'] ]
},
-
_create:function() {
this.triggered = false;
@@ -67,23 +67,21 @@
/**
* @description only make a request and suggest items if acTrigger.triggered is true
*/
- var acTrigger = $(this).data("autocompleteTrigger");
-
+ var acTrigger = $(this).data("autocompleteMultiTrigger");
return acTrigger.triggered;
},
select:function(event, ui) {
/**
* @description if a item is selected, insert the value between triggerStart and triggerEnd
*/
- acTrigger = $(this).data("autocompleteTrigger");
+ acTrigger = $(this).data("autocompleteMultiTrigger");
var text = this.value;
- var trigger = acTrigger.options.triggerStart;
+ var trigger = acTrigger.value;
var cursorPosition = acTrigger.getCursorPosition();
- var lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(trigger);
- var firstTextPart = text.substring(0, lastTriggerPosition + trigger.length) +
- ui.item.value +
- acTrigger.options.triggerEnd;
+
+ var lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(acTrigger.value);
+ var firstTextPart = text.substring(0, lastTriggerPosition + acTrigger.value.length) + ui.item.value + acTrigger.options.triggerEnd;
this.value = firstTextPart + text.substring(cursorPosition, text.length);
acTrigger.triggered = false;
@@ -109,29 +107,43 @@
* @description Bind to keyup-events to detect text changes.
* If the trigger is found before the cursor, autocomplete will be called
*/
- var acTrigger = $(this).data("autocompleteTrigger");
-
- if (event.keyCode != $.ui.keyCode.UP && event.keyCode != $.ui.keyCode.DOWN) {
- var text = this.value;
- var textLength = text.length;
- var cursorPosition = acTrigger.getCursorPosition();
- var lastString;
- var query;
- var lastTriggerPosition;
- var trigger = acTrigger.options.triggerStart;
-
- if (acTrigger.triggered) {
- // call autocomplete with the string after the trigger
- // Example: triggerStart = @, string is '@foo' -> query string is 'foo'
- lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(trigger);
- query = text.substring(lastTriggerPosition + trigger.length, cursorPosition);
- $(this).autocomplete("search", query);
- }
- else if (textLength >= trigger.length) {
- // set trigged to true, if the string before the cursor is triggerStart
- lastString = text.substring(cursorPosition - trigger.length, cursorPosition);
- acTrigger.triggered = (lastString === trigger);
- }
+ var acTrigger = $(this).data("autocompleteMultiTrigger");
+
+ if (event.keyCode == $.ui.keyCode.UP || event.keyCode == $.ui.keyCode.DOWN) {
+ return;
+ }
+
+ if (event.keyCode == $.ui.keyCode.ESCAPE) {
+ acTrigger.triggered = false;
+ return;
+ }
+
+ var text = this.value;
+ var textLength = text.length;
+ var cursorPosition = acTrigger.getCursorPosition();
+ var lastString;
+ var query;
+ var lastTriggerPosition;
+ var trigger = acTrigger.options.triggerStart;
+
+ if (textLength === 0) {
+ acTrigger.triggered = false;
+ }
+
+ if (acTrigger.triggered) {
+ lastTriggerPosition = text.substring(0, cursorPosition).lastIndexOf(acTrigger.value);
+ query = text.substring(lastTriggerPosition + acTrigger.value.length, cursorPosition);
+ $(this).autocomplete("search", query);
+ }
+ else if (textLength >= 1) {
+ lastString = text.substring(cursorPosition - 1, cursorPosition);
+ var triggerLocation = $.inArray(lastString, trigger);
+ var isTriggered = (triggerLocation > -1);
+ acTrigger.triggered = isTriggered;
+ acTrigger.index = triggerLocation;
+ acTrigger.value = (isTriggered ? trigger[triggerLocation] : '');
+ var triggerSource = acTrigger.options.sources[acTrigger.index];
+ $(this).autocomplete('option','source', triggerSource);
}
});
},
@@ -175,4 +187,4 @@
return position;
}
});
-})(jQuery, window, document);
+})(jQuery, window, document);

0 comments on commit a15db38

Please sign in to comment.