all files / common/ incremental-search.js

100% Statements 70/70
97.73% Branches 43/44
100% Functions 6/6
100% Lines 70/70
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93   20× 20× 20×   20× 20× 20×         17× 17× 17× 17× 17× 17× 17× 17× 143× 17×   143× 17×     126×   143× 143× 143× 24×   143×   17× 17× 17×     568× 568× 568× 568× 506× 506× 506× 506× 2153× 2153× 2153× 381× 381× 381×     125×   62×   506×   215×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var queryString = '';
    var prevString = '';
    var matches = [];
    var activeClass = 'e-active';
    var prevElementId = '';
    function incrementalSearch(keyCode, items, selectedIndex, ignoreCase, elementId) {
        queryString += String.fromCharCode(keyCode);
        setTimeout(function () {
            queryString = '';
        }, 1000);
        var index;
        queryString = ignoreCase ? queryString.toLowerCase() : queryString;
        if (prevElementId === elementId && prevString === queryString) {
            for (var i = 0; i < matches.length; i++) {
                if (matches[i].classList.contains(activeClass)) {
                    index = i;
                    break;
                }
            }
            index = index + 1;
            return matches[index] ? matches[index] : matches[0];
        }
        else {
            var listItems = items;
            var strLength = queryString.length;
            var text = void 0;
            var item = void 0;
            selectedIndex = selectedIndex ? selectedIndex + 1 : 0;
            var i = selectedIndex;
            matches = [];
            do {
                if (i === listItems.length) {
                    i = -1;
                }
                if (i === -1) {
                    index = 0;
                }
                else {
                    index = i;
                }
                item = listItems[index];
                text = ignoreCase ? item.innerText.toLowerCase() : item.innerText;
                if (text.substr(0, strLength) === queryString) {
                    matches.push(listItems[index]);
                }
                i++;
            } while (i !== selectedIndex);
            prevString = queryString;
            prevElementId = elementId;
            return matches[0];
        }
    }
    exports.incrementalSearch = incrementalSearch;
    function Search(inputVal, items, searchType, ignoreCase) {
        var listItems = items;
        ignoreCase = ignoreCase !== undefined && ignoreCase !== null ? ignoreCase : true;
        var itemData = { item: null, index: null };
        if (inputVal && inputVal.length) {
            var strLength = inputVal.length;
            var queryStr = ignoreCase ? inputVal.toLocaleLowerCase() : inputVal;
            queryStr = escapeCharRegExp(queryStr);
            for (var i = 0, itemsData = listItems; i < itemsData.length; i++) {
                var item = itemsData[i];
                var text = (ignoreCase ? item.textContent.toLocaleLowerCase() : item.textContent).replace(/^\s+|\s+$/g, '');
                if ((searchType === 'Equal' && text === queryStr) || (searchType === 'StartsWith' && text.substr(0, strLength) === queryStr) || (searchType === 'EndsWith' && text.substr(text.length - queryStr.length) === queryStr) || (searchType === 'Contains' && new RegExp(queryStr, "g").test(text))) {
                    itemData.item = item;
                    itemData.index = i;
                    return { item: item, index: i };
                }
            }
            return itemData;
        }
        return itemData;
    }
    exports.Search = Search;
    function escapeCharRegExp(value) {
        return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    }
    exports.escapeCharRegExp = escapeCharRegExp;
    function resetIncrementalSearchValues(elementId) {
        if (prevElementId === elementId) {
            prevElementId = '';
            prevString = '';
            queryString = '';
            matches = [];
        }
    }
    exports.resetIncrementalSearchValues = resetIncrementalSearchValues;
});