openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

jquery-plugin-orSearch.js (1775B)


      1 /**
      2  * Suche mit Dropdown
      3  */
      4 jQuery.fn.orSearch = function( options )
      5 {
      6     // Create some defaults, extending them with any options that were provided
      7     var settings = $.extend( {
      8       'dropdown': $(), // empty element
      9       'select'  : function( obj ) {}
     10     }, options);
     11 	
     12 	
     13 	return $(this).on('input', function()
     14 	{
     15 		let searchArgument = $(this).val();
     16 		let dropdownEl     = $( settings.dropdown );
     17 
     18 		if	( searchArgument.length > 3 )
     19 		{
     20 			$(dropdownEl).empty(); // Leeren.
     21 
     22 			$.ajax( { 'type':'GET',url:'./api/?action=search&subaction=quicksearch&output=json&search='+searchArgument, data:null, success:function(data, textStatus, jqXHR)
     23 				{
     24 					for( id in data.output.result )
     25 					{
     26 						let result = data.output.result[id];
     27 						
     28 						// Suchergebnis-Zeile in das Ergebnis schreiben.
     29 
     30 						let div = $('<div class="entry or-search-result" title="'+result.desc+'"></div>');
     31 						div.data('object',{
     32 							'name':result.name,
     33 						    'action':result.type,
     34 							'id':result.id
     35 						} );
     36 						let link = $('<a />').attr('href',Openrat.Navigator.createShortUrl(result.type, result.id));
     37 						link.click( function(e) {
     38 							e.preventDefault();
     39 						});
     40 						$(link).append('<i class="image-icon image-icon--action-'+result.type+'" />');
     41 						$(link).append('<span>'+result.name+'</span>');
     42 
     43 						$(div).append(link);
     44 						$(dropdownEl).append(div);
     45 					}
     46 
     47 					// Open the menu
     48                     $(dropdownEl).closest('.or-menu').addClass('open');
     49 
     50 					// Register clickhandler for search results.
     51 					$(dropdownEl).find('.or-search-result').click( function(e) {
     52 						settings.select( $(this).data('object') );
     53 					} );
     54 
     55 				} } );
     56 
     57 			
     58 		}
     59 		else
     60 		{
     61 			// No search argument.
     62             $(dropdownEl).empty(); // Leeren.
     63 		}
     64 	});
     65 };