all files / common/ incremental-search.js

95.41% Statements 104/109
88.89% Branches 88/99
100% Functions 11/11
95.41% Lines 104/109
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154   26× 25×         25×       26×         23× 23×     26× 26× 26×                   21× 21× 21× 21× 21× 21× 21× 21× 280× 21×   280× 21×     259×   280× 280× 280× 28×   280×   21× 21× 21×         18×     578× 578× 578× 578× 500× 500× 500× 500× 2818× 2818× 2818× 1016× 1016× 1016× 15827× 15830×   771×         2818× 2818× 360× 360× 360×     500× 2818× 2818× 360×   140×   78×   500×   355×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var queryString = '';
    var prevString = '';
    var tempQueryString = '';
    var matches = [];
    var activeClass = 'e-active';
    var prevElementId = '';
    function incrementalSearch(keyCode, items, selectedIndex, ignoreCase, elementId, queryStringUpdated, currentValue, isVirtual, refresh) {
        if (!queryStringUpdated || queryString === '') {
            Iif (tempQueryString !== '') {
                queryString = tempQueryString + String.fromCharCode(keyCode);
                tempQueryString = '';
            }
            else {
                queryString += String.fromCharCode(keyCode);
            }
        }
        else Eif (queryString === prevString) {
            tempQueryString = String.fromCharCode(keyCode);
        }
        if (isVirtual) {
            setTimeout(function () {
                tempQueryString = '';
            }, 700);
            setTimeout(function () {
                queryString = '';
            }, 3000);
        }
        else {
            setTimeout(function () {
                queryString = '';
            }, 1000);
        }
        var index;
        queryString = ignoreCase ? queryString.toLowerCase() : queryString;
        if (prevElementId === elementId && prevString === queryString && !refresh) {
            for (var i = 0; i < matches.length; i++) {
                if (matches[i].classList.contains(activeClass)) {
                    index = i;
                    break;
                }
                Iif (currentValue && matches[i].textContent.toLowerCase() === currentValue.toLowerCase()) {
                    index = i;
                    break;
                }
            }
            index = index + 1;
            Iif (isVirtual) {
                return matches[index] && matches.length - 1 !== index ? matches[index] : matches[matches.length];
            }
            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;
            if (isVirtual) {
                var indexUpdated = false;
                for (var i_1 = 0; i_1 < matches.length; i_1++) {
                    Eif (currentValue && matches[i_1].textContent.toLowerCase() === currentValue.toLowerCase()) {
                        index = i_1;
                        indexUpdated = true;
                        break;
                    }
                }
                if (currentValue && indexUpdated) {
                    index = index + 1;
                }
                return matches[index] ? matches[index] : matches[0];
            }
            return matches[0];
        }
    }
    exports.incrementalSearch = incrementalSearch;
    function Search(inputVal, items, searchType, ignoreCase, dataSource, fields, type) {
        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);
            var _loop_1 = function (i, itemsData) {
                var item = itemsData[i];
                var filterValue;
                if (items && dataSource) {
                    var checkField_1 = item;
                    var fieldValue_1 = fields.text.split('.');
                    dataSource.filter(function (data) {
                        Array.prototype.slice.call(fieldValue_1).forEach(function (value) {
                            if ((type === 'object' && !data.isHeader && checkField_1.textContent.toString().indexOf(data[value]) !== -1 && checkField_1.getAttribute('data-value') === data[fields.value].toString()) ||
                                (type === 'string' && checkField_1.textContent.toString().indexOf(data) !== -1)) {
                                filterValue = type === 'object' ? data[value] : data;
                            }
                        });
                    });
                }
                var text = dataSource && filterValue ? (ignoreCase ? filterValue.toString().toLocaleLowerCase() : filterValue).replace(/^\s+|\s+$/g, '') : (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 { value: { item: item, index: i } };
                }
            };
            for (var i = 0, itemsData = listItems; i < itemsData.length; i++) {
                var state_1 = _loop_1(i, itemsData);
                if (typeof state_1 === "object")
                    return state_1.value;
            }
            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;
});