Template:SearchSuggestion

    Table of contents
    No headers
    // PURPOSE:  Insert a search box that will help narrow down search results
    // WRITTEN BY:  Original creator: Howleyah 
    //              Edited by: rberinger
    //              Edited by: carles.coll
    // VERSION: 1.5
    // VERSION HISTORY: 1.0 - Initial creation
    //                  1.0 - Reworked to be non-site specific and allow for a max results parameter
    //                  1.5 - Added Parameters(path, showpages, showfiles, showcomments, sortby)
    //                        Added ability to press ENTER to perform default wiki search within Path contraints.
    //                  1.6 - Added Parameters(inputid,constrained)
    //         
    // PARAMETERS:  max - Limits Ajax search results to this number (default = 10)
    //              path - Limits Ajax search to a particular path (default = entire site)
    //                     NOTE:  if a search of subpages is desired you must end the path with /*
    //              showpages - Will Show or Hide Page results (default = true)
    //              showfiles - Will Show or Hide Files results (default = true)
    //              showcomments - Will Show or Hide Comment results (default = true)
    //              sortby - Currently not working (Default sort is by score only at this time)
    //              inputid - The Search Box Input ID , by default "search-input"
    //              constrained - Contraint to apply on the search suggestion
    
    // KNOWN BUGS:  Sortby parameter currently has no effect
    
    // USE:  Must be used within a DekiScript block or wrapped in double curly braces
    //       EXAMPLE:  {{ SearchSuggestion{max: 15, path:'path/to/some/page/*'}
    // If the dynamic results are not what you were searching for you can just hit the ENTER key
    // and a default search will be performed limited within the path constraint if one was desired.
    // When ENTER key is applied only the query text and path arguments will be honored.
    
    
    // USER PARAMETERS
    var maxresults = ($max ?? $0 ?? 10);
    var searchpath = ($path ?? $1 ?? '');
    var sortorder = ($sortby ?? $2 ?? '');
    var showpages = ($showpages ?? $3 ?? true);
    var showfiles = ($showfiles ?? $4 ?? true);
    var showcmts = ($showcomments ?? $5 ?? true);
    var inputid = ($inputid?? $6 ?? "search-input");
    var constrained = ($constrained ?? $7 ?? '');
    
    // Account for user folders
    var searchpath = string.replace(string.tolower(searchpath),'user:','User\\:');
    
    // Make this template site in-specific
    var siteAPI = site.api;
    var siteURI = site.uri;
    
    <input type="text" id=(inputid) />
    <div id="search-suggest"><a class="close" href="#">'x'</a>
    <div class="search-pages"><strong>'pages'</strong></div>
    <div class="search-files"><strong>'files'</strong></div>
    <div class="search-comments"><strong>'comments'</strong></div>
    </div>
    
    
    <div><style type="text/css">'
    #search-suggest strong{
    	margin:0 0 10px 0;
    	color:#999;
    	display:block;
    }
    #search-suggest {
    	border:1px solid #ccc;
    	display:none;
    	padding:0;
    	position:absolute;
    	overflow:hidden;
    	background:#fff;
    	font-size:11px;
    	min-width:150px;
    	z-index:50;
    }
    #search-suggest .search-pages{
    	float:left;
    	padding:5px 20px;
    	max-width:300px;
    }
    #search-suggest .search-files{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest .search-comments{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest a {
    	display:block;
    	padding:0 0 0 20px;
    	height:16px;
    	overflow:hidden;
    	margin:0 0 10px 0;
    	background-image:url(/skins/common/icons/icons.gif);
    	background-repeat:no-repeat;
    }
    #search-suggest .search-pages a {
    	background-position:0 -768px;
    	margin:0;
    	
    }
    #search-suggest .search-files a {
    	background-position:0 -1040px;
    }
    #search-suggest .search-comments .comment-user {
    	display:block;
    	font-weight:bold;
    	margin-bottom:0px;
    	padding:0;
    }
    #search-suggest .search-comments .comment-text {
    	display:block;
    	font-weight:100;
    	margin-bottom:10px;
    	color:#999;
    }
    #search-suggest .search-pages .page-path {
    	display:block;
    	color:#999;
    	font-size:9px;
    	margin:0 0 10px 0;
    }
    .no-show {
    	display:none;
    }
    #search-suggest a.close{
    	float:right;
    	display:block;
    	font-size:14px;
    	width:15px;
    	padding:0 0 1px 0;
    	line-height:1;
    	-moz-border-radius:7px;
    	border:2px solid #fff;
    	font-weight:bold;
    	text-align:center;
    	background:#ccc;
    	color:#fff;
    	text-decoration:none;
    }
    #search-suggest .close:hover{
    	background:#9f1313;
    	text-decoration:none;
    	color:#fff;
    }
    '</style>
    </div>
    
    <script type="text/javascript">'
    	$("body").ready( function() {
    		$("#search-suggest a.close").click( function() {
    			$("#search-suggest").hide();
    			return false;
    		});
    	
    		$("#'..inputid..'").keyup( function(e) {
    
    			if ($(this).val()==\'\'){
    				$("#search-suggest").hide();
    				return false;
    			} 
    	
    			var q = $(this).val();
    
                            // Get the Code the the key that was press (we are looking for the enter key)
                            var code = (e.keycode ? e.keycode : e.which);
    
                            // Was Enter Pressed?
                            if(code == 13) {
                                // Listen for an Enter KeyPress just incase our reduced results dont return
                                // The stuff our user is looking for.  NOTE: This defaults to an all site search.
    
                                var searchpath='..json.emit(searchpath)..';
                                var constrained ='..json.emit(constrained)..';
                                var sortorder='..json.emit(sortorder)..';
        
                                // Lets build the Query string with our parameters
                                var qrystr = "";
            
                                searchpath = searchpath.replace("\\:","\:");
    
                                if(searchpath != "") { qrystr += "+AND+path%3A" + searchpath }
                                if(constrained != "") { qrystr += "+AND+" + constrained }                                
                                qrystr = Deki.url.encode(q) + qrystr;
    
                                var SiteURI = '..json.emit(siteURI)..';
                                var SearchURI = SiteURI + "Special:Search?search=" + qrystr + "&type=fulltext&go=Find&ns=all";
                                location.href = SearchURI;
                                return false;
                            }
    			
                        clearTimeout($.data(this, "timer"));
    		    var ms = 400; //milliseconds
    		    var wait = setTimeout(function() {
    		      loadsearch(q);
    		    }, ms);
    	    
    	
    			$.data(this, "timer", wait);
    		});
    	});
    	
    function loadsearch(q) {
        var mySite='..json.emit(siteAPI)..';
    
        var maxreturns='..json.emit(maxresults)..';
        var searchpath='..json.emit(searchpath)..';
        var constrained='..json.emit(constrained)..';
        var sortorder='..json.emit(sortorder)..';
        var showpages='..json.emit(showpages)..';
        var showfiles='..json.emit(showfiles)..';
        var showcmts='..json.emit(showcmts)..';
    
        // Lets build the Query string with our parameters
        var qrystr = "";
    
        if(searchpath != "") { qrystr += " AND path:" + searchpath; }
        qrystr = Deki.url.encode(q + "*" + qrystr);
        if(constrained != "") { qrystr += " AND (" + constrained + ")"; }
        if(sortorder != "") { qrystr += "&sortby:" + sortorder }
    
        var qurl=mySite+"/site/search?limit="+maxreturns+"&q="+qrystr;
    
    	var cntpage=0;
    	var cntfile=0;
    	var cntcomments=0;
    	jQuery.get(qurl, function(xml) {
    		$("#search-suggest .search-pages a").remove();
    		$("#search-suggest .search-pages span").remove();
    		$("#search-suggest .search-files a").remove();
    		$("#search-suggest .search-comments a").remove();
    		$("#search-suggest .search-comments span").remove();
    	
                    if(showpages) {
    		$(xml).find("search > page").each(function(){
    			var qpath = $(this).find(" > path").text();
    			var qtitle = $(this).find("> title").text();
    		$("#search-suggest .search-pages ").append(\'<a class="page" href="/\' + qpath + \'">\' + qtitle.substr(0,30) + \'</a><span class="page-path">\' + qpath + \'</span>\');
    			cntpage++;
    		});
                    } else {
                        cntpage = 0;
                    }
    
                    if(showfiles) {		
    		$(xml).find("search > file").each(function(){
    			var quri = $(this).find(" > contents").attr("href");
    			var qtitle = $(this).find(" > filename").text();
    			$("#search-suggest .search-files").append(\'<a class="file" href="\' + quri + \'">\' + qtitle + \'</a>\');
    			cntfile++;
    		});
                    } else {
                        cntfile = 0;
                    }
    		
                    if(showcmts) {
    		$(xml).find("search > comment").each(function(){
    			var quser = $(this).find("username").text();
    			var qcomment = $(this).find(" > content").text();
    			var quri = $(this).find("path").text();
    			$("#search-suggest .search-comments").append(\'<a href="/\' + quri + \'" class="comment-user">\' + quser + \'</a><span class="comment-text">\' + qcomment.substr(0,40) + \'...</span>\');
    			cntcomments++;
    		});
                    } else {
                        cntcomments = 0;
                    }
    		
    		if(cntpage>0){
    			$("#search-suggest .search-pages").show();
    		} else {
    			$("#search-suggest .search-pages").hide();
    		}
    		
    		if(cntfile>0){
    			$("#search-suggest .search-files").show();
    		} else {
    			$("#search-suggest .search-files").hide();
    		}
    		
    		if(cntcomments>0){
    			$("#search-suggest .search-comments").show();
    		} else {
    			$("#search-suggest .search-comments").hide();
    		}
    		
    		if(cntpage>0 || cntfile>0 || cntcomments>0){
    			$("#search-suggest").show();
    		} else {
    			$("#search-suggest").hide();
    		}
    	});
    }
    '</script>
    
    
    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by