Typeahead Search

Wordpress Plugin
Contractor: H1 Web Oy
Client: Munstadi
Website: munstadi.fi
Close X

Not available for public use yet. I will publish it on Github at some point

/**
 * Typeahead Search WordPress Plugin
 * Author: Federico Di Rosa
 * Version: 1.0
 * License: MIT
 */

if (!class_exists('TypeaheadSearchPublic')) {

	class TypeaheadSearchPublic {

		public function __construct() {
			if (!is_admin()){
				$this->actions();
			}
		}

		public function actions() {
			add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
		}

		public function load_scripts() {
			wp_enqueue_script( 'typeahead.js', plugin_dir_url( TYPEAHEADSEARCH_BASENAME ) . 'js/typeahead.js/typeahead.jquery.min.js', array( 'jquery' ), '1.0', true );
			wp_register_script( 'typeahead-search', plugin_dir_url( TYPEAHEADSEARCH_BASENAME ) . 'js/typeahead-search.min.js', array( 'typeahead.js' ), '1.0', true );
			$data = array(
				'plugins_url' => plugins_url(),
				'post_types' => $this->get_dataset_post_types(),
				'form_classes' => $this->get_search_form_classes()
			);
			wp_localize_script( 'typeahead-search', 'typeahead_search_data', $data );
			wp_enqueue_script( 'typeahead-search' );
		}

		public static function get_dataset_post_types() {
			$post_types = array();
			$settings = get_option( 'typeahead_search_settings');
			if (!empty($settings['post_types'])) {
				foreach ($settings['post_types'] as $key => $value) {
					$post_types[] = $key;
				}
			}
			return $post_types;
		}

		public static function get_dataset_posts_per_page() {
			$post_types = array();
			$settings = get_option( 'typeahead_search_settings');
			if (!empty($settings['posts_per_page'])) {
				return $settings['posts_per_page'];
			} else {
				return 500;
			}
		}

		public function get_search_form_classes() {
			$form_classes = array();
			$settings = get_option( 'typeahead_search_settings');
			if (!empty($settings['form_class'])) {
				$form_classes['form_class'] = $settings['form_class'];
			}
			if (!empty($settings['field_class'])) {
				$form_classes['field_class'] = $settings['field_class'];
			}
			return $form_classes;
		}

		public static function typeahead_search_filter() {
			$post_types = self::get_dataset_post_types();
			if (in_array(get_post_type(), $post_types))
				include(dirname(plugin_dir_path( __FILE__ )) . '/templates/typeahead-search-filter.php');
		}

	}

	$typeahead_search_public = new TypeaheadSearchPublic;

}

 

/*!
 * Typeahead Search WordPress Plugin
 * Author: Federico Di Rosa
 * Version: 1.0
 * License: MIT
 */

jQuery(document).ready(function($) {

	var TYPEAHEAD_SEARCH = {
		init: function() {
			TYPEAHEAD_SEARCH.get_data();
			TYPEAHEAD_SEARCH.search_filter();
			TYPEAHEAD_SEARCH.on_suggestion_selected();
		},
		options: {
			hint: true,
			highlight: true,
			minLength: 1
		},
		substring_matcher: function( strs ) {
			return function findMatches(q, cb) {
				var matches, substrRegex;
				// an array that will be populated with substring matches
				matches = [];
				// regex used to determine if a string contains the substring `q`
				substrRegex = new RegExp(q, 'i');
				// iterate through the pool of strings and for any string that
				// contains the substring `q`, add it to the `matches` array
				$.each(strs, function(i, str) {
					if (substrRegex.test(str)) {
						// the typeahead jQuery plugin expects suggestions to a
						// JavaScript object, refer to typeahead docs for more info
						matches.push({ value: str });
					}
				});
				cb(matches);
			};
		},
		get_data: function() {
			if (window.Modernizr !== undefined && Modernizr.touch)
				return;
			// pass an empty value to post_type parameter to retrieve all post types
			var post_type = $(typeahead_search_data.form_classes.form_class + ' input[name="post_type"]').val();
			$.get( typeahead_search_data.plugins_url + '/typeahead-search/datasets/dataset.php?pt=' + post_type, function( data ) {
				if (data) TYPEAHEAD_SEARCH.initiate_typeahead( data );
			});
		},
		initiate_typeahead: function( data ) {
			var dataset = {
				name: 'posts',
				displayKey: 'value',
				source: TYPEAHEAD_SEARCH.substring_matcher(data)
			};
			$(typeahead_search_data.form_classes.form_class + ' ' + typeahead_search_data.form_classes.field_class).typeahead( TYPEAHEAD_SEARCH.options, dataset );
		},
		regenerate_typeahead: function(){
			$(typeahead_search_data.form_classes.form_class + ' ' + typeahead_search_data.form_classes.field_class).typeahead('destroy');
			TYPEAHEAD_SEARCH.get_data();
		},
		search_filter: function() {
			$('.typeahead-search-filter').hide();
			$('.typeahead-search-filter-toggle').on('click', function(){
				$(this).next('.typeahead-search-filter').slideToggle( 200 );
			});
			$('.typeahead-search-filter li').on('click', function(){
				if ($('.typeahead-search-filter').is(':visible')) {
					$('.typeahead-search-filter').slideUp( 200 );
					$('.typeahead-search-filter-toggle').text( $(this).text() );
					$(typeahead_search_data.form_classes.form_class + ' input[name="post_type"]').val( $(this).attr('data-value') );
					TYPEAHEAD_SEARCH.regenerate_typeahead();
				}
			});
			$(document).keyup(function( event ) {
				if (event.keyCode == 27) {
					if ($('.typeahead-search-filter').is(':visible'))
						$('.typeahead-search-filter').slideUp( 200 );
				}
			});
			$(document).on('click', function( event ) {
				if (!$(event.target).hasClass('typeahead-search-filter') &&
					!$(event.target).parents().hasClass('typeahead-search-filter') &&
					!$(event.target).hasClass('typeahead-search-filter-toggle')) {
					if ($('.typeahead-search-filter').is(':visible'))
						$('.typeahead-search-filter').slideUp( 200 );
				}
			});
		},
		on_suggestion_selected: function() {
			$(typeahead_search_data.form_classes.form_class + ' ' + typeahead_search_data.form_classes.field_class).on('typeahead:selected', function( event, suggestion, dataset ){
				$(typeahead_search_data.form_classes.form_class).submit();
			});
		}
	};

	TYPEAHEAD_SEARCH.init();

});

 

Screen Shot 2015-03-21 at 17.07.14