scaffold.libs_as.feathers.data.LocalAutoCompleteSource.as Maven / Gradle / Ivy
/*
Feathers
Copyright 2012-2015 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.data
{
import starling.events.Event;
import starling.events.EventDispatcher;
/**
* Dispatched when the suggestions finish loading.
*
* The properties of the event object have the following values:
*
* Property Value
* bubbles
false
* currentTarget
The Object that defines the
* event listener that handles the event. For example, if you use
* myButton.addEventListener()
to register an event listener,
* myButton is the value of the currentTarget
.
* data
A ListCollection
containing
* the suggestions to display.
* target
The Object that dispatched the event;
* it is not always the Object listening for the event. Use the
* currentTarget
property to always access the Object
* listening for the event.
*
*
* @eventType starling.events.Event.COMPLETE
*/
[Event(name="complete",type="starling.events.Event")]
/**
* Creates a list of suggestions for an AutoComplete
component
* by searching through items in a ListCollection
.
*
* @see feathers.controls.AutoComplete
* @see feathers.data.ListCollection
*/
public class LocalAutoCompleteSource extends EventDispatcher implements IAutoCompleteSource
{
/**
* @private
*/
protected static function defaultCompareFunction(item:Object, textToMatch:String):Boolean
{
return item.toString().toLowerCase().indexOf(textToMatch.toLowerCase()) >= 0;
}
/**
* Constructor.
*/
public function LocalAutoCompleteSource(source:ListCollection = null)
{
this._dataProvider = source;
}
/**
* @private
*/
private var _dataProvider:ListCollection;
/**
* A collection of items to be used as a source for auto-complete
* results.
*/
public function get dataProvider():ListCollection
{
return this._dataProvider;
}
/**
* @private
*/
public function set dataProvider(value:ListCollection):void
{
this._dataProvider = value;
}
/**
* @private
*/
protected var _compareFunction:Function = defaultCompareFunction;
/**
* A function used to compare items from the data provider with the
* string passed to the load()
function in order to
* generate a list of suggestions. The function should return
* true
if the item should be included in the list of
* suggestions.
*
* The function is expected to have the following signature:
* function( item:Object, textToMatch:String ):Boolean
*/
public function get compareFunction():Function
{
return this._compareFunction;
}
/**
* @private
*/
public function set compareFunction(value:Function):void
{
if(value === null)
{
value = defaultCompareFunction;
}
this._compareFunction = value;
}
/**
* @copy feathers.data.IAutoCompleteSource#load()
*/
public function load(textToMatch:String, result:ListCollection = null):void
{
if(result)
{
result.removeAll();
}
else
{
result = new ListCollection();
}
if(!this._dataProvider || textToMatch.length == 0)
{
this.dispatchEventWith(Event.COMPLETE, false, result);
return;
}
var compareFunction:Function = this._compareFunction;
for(var i:int = 0; i < this._dataProvider.length; i++)
{
var item:Object = this._dataProvider.getItemAt(i);
if(compareFunction(item, textToMatch))
{
result.addItem(item);
}
}
this.dispatchEventWith(Event.COMPLETE, false, result);
}
}
}