📄 searchservice.as
字号:
/** * Constructor. */ public function SearchService() { } //-------------------------------------------------------------------------- // // Public variables // //-------------------------------------------------------------------------- [Bindable] [Inspectable(enumeration="web,image,audio,video,local,artist,album,song,songDownload,news,context,termExtraction,related,spellingSuggestion")] /** * The type of search to perform * @default SearchService.WEB_SEARCH */ //TODO: Consider making this an array, allowing multiple types of searches in parallel public var type:String = WEB_SEARCH; /** * The query string to search for, in lieu of a request object * @see #request */ public var query:String; /** * The request object * @see com.yahoo.webapis.search.params.SearchParams */ public var request:SearchParams = new SearchParams(); /** * The result index to begin returning results at EXAMPLE: 50 would start returning results at 50 * @default 0 */ public var startAt:uint = 0; [Bindable] /** * The number of times this search has been performed; the "page" of results * @default 0 */ public var increment:uint = 0; [Bindable] /** * If the search has more results pending * */ public var hasResultsPending:Boolean = true; [Bindable] /** * The maximum number of results to return * @default 10 * maximum value is 100 */ public function get maximumResults():uint { return _maximumResults; } public function set maximumResults( value:uint ):void { //maximum that the Yahoo! API will allow is 100 //TODO: others may be less, this maybe should be lower if (value > 100) value = 100; _maximumResults = value; } [Bindable] /** * The total amount of results available */ public function get numResultsAvailable():uint { return _numResultsAvailable; } public function set numResultsAvailable( value:uint ):void { _numResultsAvailable = value; } [Bindable] /** * The position of the first result . */ public function get firstResultPosition():uint { return _firstResultPosition; } public function set firstResultPosition( value:uint ):void { _firstResultPosition = value; } [Bindable] /** * The position of the last result . */ public function get lastResultPosition():uint { return _lastResultPosition; } public function set lastResultPosition( value:uint ):void { _lastResultPosition = value; } [Bindable] /** * The amount of results of this page. */ public function get numResultsReturned():uint { return _numResultsReturned; } public function set numResultsReturned( value:uint ):void { _numResultsReturned = value; } [Bindable] /** * The total amount of results returned. */ public function get numTotalResultsReturned():uint { return _numTotalResultsReturned; } public function set numTotalResultsReturned( value:uint ):void { _numTotalResultsReturned = value; } [Bindable] /** * Whether to retrieve more results on the same search. */ public function get autoIncrement():Boolean { return _autoIncrement; } public function set autoIncrement( value:Boolean ):void { _autoIncrement = value; } [Bindable] /** * The result of the last service invocation * */ public function get lastResult():Array { return _lastResult; } //NOTE: these should probably be private or protected. //This usage is due to a compiler bug, which gives "Ambiguous reference to ..." errors //cf. http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=4a146409 public function set lastResult( value:Array ):void { _lastResult = value; } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** * Search based on type. * * This provides a way to use a standard method for search. * <p>Note: Invoking the <code>send()</code> method multiple times with the same parameters will retrieve more results for that search.</p> * @param parameters A String or SearchParams object to specify parameters * @see com.yahoo.webapis.search.params.SearchParams */ public function send(parameters:Object = null):void { //allow query that is set at the level of this service; useful for databinding if(parameters == null) { if(request == null) { request = new SearchParams(); } if(query != null) { request.query = query; } } if(parameters is String) { request.query = parameters as String; } if(parameters is SearchParams) { request = parameters as SearchParams; } //allow parameters that are set at the level of this service if(request.maximumResults==0) { request.maximumResults = _maximumResults; } var newRequest:String = request.toString(); //Take out the results and start parameters for comparison newRequest = newRequest.replace(/&start=\d+/,""); newRequest = newRequest.replace(/&results=\d+/,""); //trace(previousRequest, request) if((previousType == type) && (previousRequest == newRequest) && autoIncrement) { //continue to retrieve results for this same search increment ++; } else { //new query, so reset increment = 0; hasResultsPending = true; lastResult = []; //store the last query previousRequest = request.toString(); previousRequest = previousRequest.replace(/&start=\d+/,""); previousRequest = previousRequest.replace(/&results=\d+/,""); } //continue retrieving results for this same query, if there are any more if(!hasResultsPending) return; //hold the last type previousType = type; //Set the index to start at, based on paging (Yahoo! APIs are 1-based) request.start = startAt + ( increment * request.maximumResults ) + 1; switch(type) { case TERM_EXTRACTION: searchTermExtraction( request ); break; case CONTEXT_ANALYSIS: searchContextAnalysis( request ); break; case RELATED_SUGGESTION: searchRelatedSuggestions( request ); break; case SPELLING_SUGGESTION: searchSpellingSuggestions( request ); break; case VIDEO_SEARCH: searchVideos( request ); break; case NEWS_SEARCH: searchNews( request ); break; case IMAGE_SEARCH: searchImages(request ); break; case AUDIO_SEARCH: searchAudio(request ); break; case PODCAST_SEARCH: searchPodcasts( request ); break; case ARTIST_SEARCH: searchArtists( request ); break; case ALBUM_SEARCH: searchAlbums( request ); break; case SONG_SEARCH: searchSongs( request ); break; case SONG_DOWNLOAD_LOCATION_SEARCH: searchSongDownloads( request ); break; case LOCAL_SEARCH: searchLocal( request ); break; default: //default to Web Search searchWeb( request ); } } /** * Search pages based on a query. * @param params SearchParams passed for using search filters. */ public function searchWeb(params:SearchParams = null):void { handleQueryLoading( WEB_SEARCH_URL, SearchResultEvent.WEB_SEARCH_RESULT, params); } /** * Search local based on a query and location. * @param params SearchParams passed for using search filters. */ public function searchLocal(params:SearchParams = null):void { handleQueryLoading( LOCAL_SEARCH_URL, SearchResultEvent.LOCAL_SEARCH_RESULT, params); } /** * Search images based on a query. * @param params SearchParams passed for using search filters. */ public function searchImages(params:SearchParams = null):void { handleQueryLoading( IMAGE_SEARCH_URL, SearchResultEvent.IMAGE_SEARCH_RESULT, params); } /** * Search news based on a query. * @param params SearchParams passed for using search filters. */ public function searchNews(params:SearchParams = null):void { handleQueryLoading( NEWS_SEARCH_URL, SearchResultEvent.NEWS_SEARCH_RESULT, params); } /** * Search audio based on a query. * @param params SearchParams passed for using search filters. */ public function searchAudio(params:SearchParams = null):void { handleQueryLoading( AUDIO_SEARCH_URL, SearchResultEvent.AUDIO_SEARCH_RESULT, params); } /** * Search podcasts based on a query.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -