angelhdz12

Actionscript 3.0 Autocomplete For Textfields (free)

Jan 18th, 2017
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Instructions:
  3.  *
  4.  * Just copy the 'textFields' Object and the addAutoComplete() function in your code, and call it
  5.  * passing as arguments the textfield and a list of keywords to match (Object, Array or Vector.<int or String>).
  6.  *
  7.  * E.g:
  8.  *
  9.  * addAutoComplete(txt1, new <int>[12311, 1232, 1233]);
  10.  * addAutoComplete(txt2, new <String>["Angel", "Annie", "Animals"]);
  11.  * addAutoComplete(txt3, ["Angel", "Annie", "Animals"]);
  12.  * addAutoComplete(txt4, {"1":Angel", "2":"Annie", "3":"Animals"});
  13.  *
  14.  */
  15.  
  16.  //textFields
  17.  var textFields:Object = {};
  18.  
  19. //addAutoComplete
  20. function addAutoComplete(textField:TextField, keywords:Object):void{
  21.     textFields[textField.name] = {"textField": textField, "keywords": keywords};
  22.    
  23.     //Create new container of type TextField to append the results
  24.     //and set its appearance and size according to the textfield's size.
  25.     textFields[textField.name].container = new TextField();
  26.     textFields[textField.name].container.autoSize = "center";
  27.     textFields[textField.name].container.defaultTextFormat = new TextFormat("Verdana", 13, 0, null, null, null, null, null, "center");
  28.     textFields[textField.name].container.multiline = true;
  29.     textFields[textField.name].container.wordWrap = true;
  30.     textFields[textField.name].container.border = true;
  31.     textFields[textField.name].container.borderColor = 0x999999;
  32.     textFields[textField.name].container.width = textField.width;
  33.     textFields[textField.name].container.y = textField.y + textField.height;
  34.     textFields[textField.name].container.x = textField.x;
  35.    
  36.     //Declare event listeners callbacks
  37.    
  38.     //onLink
  39.     textFields[textField.name].onLink = function(event:TextEvent):void{
  40.         //Split the string from the clicked hyperlink by its "&" symbols
  41.         //to get all the key/value pairs available.
  42.         var parse:Array = event.text.split("&");
  43.         var obj:Object = {};
  44.        
  45.         //Get the keys and values and add them to obj Object.
  46.         for (var i:String in parse)
  47.         {
  48.             var o:Array = parse[i].split("=");
  49.             var key:String = o[0];
  50.             var value:String = o[1];
  51.             obj[key] = value;
  52.         }
  53.         var txt:TextField = textFields[obj.textField].textField;
  54.         //Remove the container
  55.         var container:TextField = textFields[txt.name].container;
  56.         if (contains(container)) removeChild(container);
  57.         //Add the clicked hyperlink text to the actual textfield
  58.         txt.text = obj.clickedItem;
  59.     };
  60.     //onChange
  61.     textFields[textField.name].onChange = function(event:Event):void{
  62.         var txt:TextField = event.currentTarget as TextField;
  63.         var obj:Object = textFields[txt.name];
  64.         var container:TextField = obj.container as TextField;
  65.         //If typed text length is higher than 1 character,
  66.         if (txt.length > 1){
  67.             var results:Array = [];
  68.            
  69.             //Iterate through the keywords to match
  70.             for (var i:String in obj.keywords){
  71.                 var result:String = String(obj.keywords[i]);
  72.                 var query:String = String(txt.text);
  73.                
  74.                 //If there is a match with the typed text,
  75.                 if (result.toLowerCase().indexOf(query.toLowerCase()) >= 0){
  76.                     //append the match to the results array.
  77.                     results.push(result);
  78.                 }      
  79.                 //Garbage collection
  80.                 result = query = null;
  81.             }
  82.             //Check if there are results.
  83.             if (results.length > 0){
  84.                 //Add the container to the stage if it's not already added.
  85.                 if (!contains(container)) addChild(container);
  86.                 //Set the width of the container to the textfield's width, in case it changed.
  87.                 container.width = textField.width;
  88.                 //Clear the container
  89.                 container.htmlText = "";
  90.                 //Iterate through all the results.
  91.                 for (var j:String in results){
  92.                     //Append the current result as hyperlink HTML text to the texfield.
  93.                     container.htmlText += "<a href=\"event:"+"textField="+txt.name+"&clickedItem="+results[j]+"\">"+results[j]+" </a></br > ";
  94.                 }
  95.                 //Garbage collection
  96.                 results = null;
  97.             } else{
  98.                 if(contains(container)) removeChild(container);
  99.             }
  100.         //else, clear and remove the container from the stage.
  101.         } else{
  102.             container.htmlText = "";
  103.             if(contains(container)) removeChild(container);
  104.         }
  105.     };
  106.     //Add event listeners
  107.     textFields[textField.name].container.addEventListener(TextEvent.LINK, textFields[textField.name].onLink);
  108.     textField.addEventListener(Event.CHANGE, textFields[textField.name].onChange);
  109. };
  110. //End addAutoComplete
Add Comment
Please, Sign In to add comment