Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Instructions:
- *
- * Just copy the 'textFields' Object and the addAutoComplete() function in your code, and call it
- * passing as arguments the textfield and a list of keywords to match (Object, Array or Vector.<int or String>).
- *
- * E.g:
- *
- * addAutoComplete(txt1, new <int>[12311, 1232, 1233]);
- * addAutoComplete(txt2, new <String>["Angel", "Annie", "Animals"]);
- * addAutoComplete(txt3, ["Angel", "Annie", "Animals"]);
- * addAutoComplete(txt4, {"1":Angel", "2":"Annie", "3":"Animals"});
- *
- */
- //textFields
- var textFields:Object = {};
- //addAutoComplete
- function addAutoComplete(textField:TextField, keywords:Object):void{
- textFields[textField.name] = {"textField": textField, "keywords": keywords};
- //Create new container of type TextField to append the results
- //and set its appearance and size according to the textfield's size.
- textFields[textField.name].container = new TextField();
- textFields[textField.name].container.autoSize = "center";
- textFields[textField.name].container.defaultTextFormat = new TextFormat("Verdana", 13, 0, null, null, null, null, null, "center");
- textFields[textField.name].container.multiline = true;
- textFields[textField.name].container.wordWrap = true;
- textFields[textField.name].container.border = true;
- textFields[textField.name].container.borderColor = 0x999999;
- textFields[textField.name].container.width = textField.width;
- textFields[textField.name].container.y = textField.y + textField.height;
- textFields[textField.name].container.x = textField.x;
- //Declare event listeners callbacks
- //onLink
- textFields[textField.name].onLink = function(event:TextEvent):void{
- //Split the string from the clicked hyperlink by its "&" symbols
- //to get all the key/value pairs available.
- var parse:Array = event.text.split("&");
- var obj:Object = {};
- //Get the keys and values and add them to obj Object.
- for (var i:String in parse)
- {
- var o:Array = parse[i].split("=");
- var key:String = o[0];
- var value:String = o[1];
- obj[key] = value;
- }
- var txt:TextField = textFields[obj.textField].textField;
- //Remove the container
- var container:TextField = textFields[txt.name].container;
- if (contains(container)) removeChild(container);
- //Add the clicked hyperlink text to the actual textfield
- txt.text = obj.clickedItem;
- };
- //onChange
- textFields[textField.name].onChange = function(event:Event):void{
- var txt:TextField = event.currentTarget as TextField;
- var obj:Object = textFields[txt.name];
- var container:TextField = obj.container as TextField;
- //If typed text length is higher than 1 character,
- if (txt.length > 1){
- var results:Array = [];
- //Iterate through the keywords to match
- for (var i:String in obj.keywords){
- var result:String = String(obj.keywords[i]);
- var query:String = String(txt.text);
- //If there is a match with the typed text,
- if (result.toLowerCase().indexOf(query.toLowerCase()) >= 0){
- //append the match to the results array.
- results.push(result);
- }
- //Garbage collection
- result = query = null;
- }
- //Check if there are results.
- if (results.length > 0){
- //Add the container to the stage if it's not already added.
- if (!contains(container)) addChild(container);
- //Set the width of the container to the textfield's width, in case it changed.
- container.width = textField.width;
- //Clear the container
- container.htmlText = "";
- //Iterate through all the results.
- for (var j:String in results){
- //Append the current result as hyperlink HTML text to the texfield.
- container.htmlText += "<a href=\"event:"+"textField="+txt.name+"&clickedItem="+results[j]+"\">"+results[j]+" </a></br > ";
- }
- //Garbage collection
- results = null;
- } else{
- if(contains(container)) removeChild(container);
- }
- //else, clear and remove the container from the stage.
- } else{
- container.htmlText = "";
- if(contains(container)) removeChild(container);
- }
- };
- //Add event listeners
- textFields[textField.name].container.addEventListener(TextEvent.LINK, textFields[textField.name].onLink);
- textField.addEventListener(Event.CHANGE, textFields[textField.name].onChange);
- };
- //End addAutoComplete
Add Comment
Please, Sign In to add comment