js_search_functions

NAML documentation   Watch a video
   Usages of this macro
... in javascript_library.naml
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<macro name="js_search_functions" requires="servlet">
    <![CDATA[
    Nabble.highlightSearchTerms = function(searchterms, elem) {
        if (elem.childNodes && elem.childNodes.length > 0) {
            for (var i=0; i<elem.childNodes.length; i++) {
                this.highlightSearchTerms(searchterms, elem.childNodes[i]);
            }
        } else if (elem.nodeType) {
            if (elem.nodeType == document.TEXT_NODE || elem.nodeType == 3) {
                var txt = elem.nodeValue;
                var rgx = new RegExp("\\b("+searchterms+")\\w*\\b", "gi");
                var result;
                var start = 0;
                var newFragment = document.createElement("span");
                while ((result = rgx.exec(txt)) != null) {
                    var end = result.index;
                    var textNode = document.createTextNode(txt.slice(start, end));
                    newFragment.appendChild(textNode);
                    var hlNode = document.createElement("span");
                    hlNode.className = "bold highlight search-highlight";
                    hlNode.appendChild(document.createTextNode(result[0]));
                    newFragment.appendChild(hlNode);
                    start = end + result[0].length;
                    Nabble.hasHighlightedTerms = true;
                }
                newFragment.appendChild(document.createTextNode(txt.slice(start)));
                elem.parentNode.replaceChild(newFragment, elem);
            }
        }
    };
 
    Nabble.getSearchTerms = function() {
        var searchterms = this.getCookie("searchterms");
        if (document.referrer) {
            var result = Nabble.getSearchTerms2(document.referrer);
            if (result!=null) {
                var query = decodeURIComponent(result);
                searchterms = query.replace(/\W+/g,"|").replace(/^\|/,"").replace(/\|$/,"");
                this.setCookie("searchterms", searchterms);
                Nabble.gquery = query.replace(/\+/g," ");
            }
        }
        return searchterms;
    };
 
    /* Logic from _uOrg() in urchin.js */
    Nabble.getSearchTerms2 = function(referrer) {
        if (typeof(_uOsr)=="undefined" || typeof(_uOkw)=="undefined")
            return null;
        var searchEngines = _uOsr;  /* from urchin */
        var searchQueries = _uOkw;  /* from urchin */
        var i;
        if (referrer==null || (i=referrer.indexOf("://")) < 0)
            return null;
        var h = referrer.substring(i+3);
        var p = h.indexOf("/");
        if (p > -1)
            h = h.substring(0,p);
        for (var j=0; j<searchEngines.length; j++) {
            var engine = searchEngines[j];
            var query = searchQueries[j];
            if (h.toLowerCase().indexOf(engine.toLowerCase()) > -1) {
                if ((i=referrer.indexOf("?"+query+"=")) > -1 || (i=referrer.indexOf("&"+query+"=")) > -1) {
                    var k = referrer.substring(i+query.length+2,referrer.length);
                    if ((i=k.indexOf("&")) > -1)
                        k = k.substring(0,i);
                    return k;
                }
            }
        }
        return null;
    };
    ]]>
</macro>