all files / base/ utils.js

98.72% Statements 154/156
92.41% Branches 73/79
100% Functions 22/22
98.71% Lines 153/155
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 2561×   1× 1× 3894× 1859× 1034×     2860×   1× 1× 99× 99× 99× 99× 30× 2×     28×       69×   99×           1× 1× 4472× 5679× 5679× 9043× 9043× 11956× 11956× 21746× 21746× 1199×                 3273×   1× 1× 37× 46× 46× 62× 62× 81× 81× 34×               3×   1× 1× 948× 1015× 1015× 3167× 3167× 920×           28×   1× 1× 2015× 2015×   1× 1× 67× 67×   1× 1× 42× 42×   1× 1× 325× 146× 146× 5×     320× 320× 14×   306× 191× 190×   190×     1×       115×   115× 65×     65×   115×     1× 1× 2433×   1× 1× 11× 19× 11× 11×     11× 11× 11×       11×   11×     11× 11× 6×     6×   11× 11×       11×   11×         1× 1× 774×           774× 1× 11×   1× 11× 11× 11× 1×       1× 1× 598× 598×   1× 1× 3× 3×   1× 1× 2215× 123× 2× 2× 2×     121× 2× 2× 2×     119×       1× 1× 60× 7×   53× 37×     1× 1× 36× 36×     36× 36× 36×   1× 1× 153× 153×   1×    
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-popups", "./interface", "./constant", "@syncfusion/ej2-splitbuttons"], function (require, exports, ej2_base_1, ej2_popups_1, interface_1, constants, ej2_splitbuttons_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function getIndex(arr, condition) {
        for (var i = 0; i < arr.length; i++) {
            if (condition(arr[parseInt(i.toString(), 10)], i)) {
                return i;
            }
        }
        return -1;
    }
    exports.getIndex = getIndex;
    function getTemplateFunction(template) {
        Eif (typeof template === 'string') {
            var content = '';
            try {
                var tempEle = ej2_base_1.select(template);
                if (tempEle) {
                    content = tempEle.tagName === 'SCRIPT' ? tempEle.innerHTML : tempEle.outerHTML;
                }
                else {
                    content = template;
                }
            }
            catch (e) {
                content = template;
            }
            return ej2_base_1.compile(content);
        }
        else {
            return ej2_base_1.compile(template);
        }
    }
    exports.getTemplateFunction = getTemplateFunction;
    function getItem(tabs, id, type) {
        for (var i = 0; i < tabs.length; i++) {
            var tab = tabs[parseInt(i.toString(), 10)];
            for (var j = 0; j < tab.groups.length; j++) {
                var group = tab.groups[parseInt(j.toString(), 10)];
                for (var k = 0; k < group.collections.length; k++) {
                    var collection = group.collections[parseInt(k.toString(), 10)];
                    for (var l = 0; l < collection.items.length; l++) {
                        var item = collection.items[parseInt(l.toString(), 10)];
                        if ((id && item.id === id) || (type && item.type === type)) {
                            return {
                                item: item, collection: collection, group: group,
                                tabIndex: i, groupIndex: j, collectionIndex: k, itemIndex: l
                            };
                        }
                    }
                }
            }
        }
        return null;
    }
    exports.getItem = getItem;
    function getCollection(tabs, id) {
        for (var i = 0; i < tabs.length; i++) {
            var tab = tabs[parseInt(i.toString(), 10)];
            for (var j = 0; j < tab.groups.length; j++) {
                var group = tab.groups[parseInt(j.toString(), 10)];
                for (var k = 0; k < group.collections.length; k++) {
                    var collection = group.collections[parseInt(k.toString(), 10)];
                    if (collection.id === id) {
                        return {
                            collection: collection, group: group,
                            tabIndex: i, groupIndex: j, collectionIndex: k
                        };
                    }
                }
            }
        }
        return null;
    }
    exports.getCollection = getCollection;
    function getGroup(tabs, id) {
        for (var i = 0; i < tabs.length; i++) {
            var tab = tabs[parseInt(i.toString(), 10)];
            for (var j = 0; j < tab.groups.length; j++) {
                var group = tab.groups[parseInt(j.toString(), 10)];
                if (group.id === id) {
                    return {
                        group: group, tabIndex: i, groupIndex: j
                    };
                }
            }
        }
        return null;
    }
    exports.getGroup = getGroup;
    function destroyControl(element, moduleName) {
        var control = ej2_base_1.getComponent(element, moduleName);
        control.destroy();
    }
    exports.destroyControl = destroyControl;
    function updateCommonProperty(element, moduleName, commonProp) {
        var control = ej2_base_1.getComponent(element, moduleName);
        control.setProperties(commonProp);
    }
    exports.updateCommonProperty = updateCommonProperty;
    function updateControlDisabled(element, moduleName, disable) {
        var control = ej2_base_1.getComponent(element, moduleName);
        control.setProperties(moduleName === 'combobox' ? { enabled: !disable } : { disabled: disable });
    }
    exports.updateControlDisabled = updateControlDisabled;
    function getItemElement(parent, id, itemProp) {
        if (!itemProp) {
            itemProp = getItem(parent.tabs, id);
            if (!itemProp) {
                return null;
            }
        }
        var contentEle = parent.tabObj.items[itemProp.tabIndex].content;
        if (contentEle.innerHTML === '') {
            return null;
        }
        if (parent.activeLayout === interface_1.RibbonLayout.Classic) {
            if (itemProp.item.displayOptions & interface_1.DisplayMode.Classic) {
                contentEle = (itemProp.group.isCollapsed) ? parent.ribbonDropDownModule.getOverflowDropDownPopup(itemProp, contentEle)
                    : contentEle;
                return contentEle.querySelector('#' + id);
            }
            else {
                return null;
            }
        }
        else {
            var ele = (itemProp.item.displayOptions & interface_1.DisplayMode.Simplified) ?
                contentEle.querySelector('#' + itemProp.item.id) : null;
            if (!ele) {
                var dropdown = itemProp.group.enableGroupOverflow ?
                    ej2_base_1.getComponent(contentEle.querySelector('#' + itemProp.group.id + constants.GROUPOF_BUTTON_ID), ej2_splitbuttons_1.DropDownButton)
                    : parent.overflowDDB;
                ele = dropdown.target.querySelector('#' + itemProp.item.id);
            }
            return ele;
        }
    }
    exports.getItemElement = getItemElement;
    function isTooltipPresent(tooltip) {
        return (tooltip.content || tooltip.iconCss || tooltip.title || tooltip.id || tooltip.cssClass) ? true : false;
    }
    exports.isTooltipPresent = isTooltipPresent;
    function setToolTipContent(args, tooltip, tooltipData) {
        var targetId = args.target.getAttribute('id');
        var dataObj = tooltipData.filter(function (e) { return e.id === targetId; })[0];
        var data = dataObj.data;
        var content = tooltip.createElement('div', {
            id: data.id ? constants.RIBBON_TOOLTIP_CONTAINER + '_' + data.id : constants.RIBBON_TOOLTIP_CONTAINER
        });
        tooltip.element.append(content);
        Eif (data.title) {
            var header = tooltip.createElement('div', {
                innerHTML: data.title,
                className: constants.RIBBON_TOOLTIP_TITLE
            });
            content.appendChild(header);
        }
        var textContainer = tooltip.createElement('div', {
            className: constants.RIBBON_TEXT_CONTAINER
        });
        content.appendChild(textContainer);
        if (data.iconCss) {
            var customCss = tooltip.createElement('div', {
                className: data.iconCss + ' ' + constants.RIBBON_TOOLTIP_ICON
            });
            textContainer.appendChild(customCss);
        }
        Eif (data.content) {
            var tooltipContent = tooltip.createElement('div', {
                innerHTML: data.content,
                className: constants.RIBBON_TOOLTIP_CONTENT
            });
            textContainer.appendChild(tooltipContent);
        }
        tooltip.setProperties({
            content: content,
            cssClass: data.cssClass ? data.cssClass + ' ' + constants.RIBBON_TOOLTIP : constants.RIBBON_TOOLTIP
        });
    }
    exports.setToolTipContent = setToolTipContent;
    function createTooltip(element, ribbon) {
        var ribbonTooltip = new ej2_popups_1.Tooltip({
            target: '.' + constants.RIBBON_TOOLTIP_TARGET,
            beforeRender: beforeTooltipRender.bind(this),
            beforeOpen: beforeTooltipOpen.bind(this),
            windowCollision: true
        });
        ribbonTooltip.appendTo(element);
        function beforeTooltipRender(args) {
            setToolTipContent(args, ribbonTooltip, ribbon.tooltipData);
        }
        function beforeTooltipOpen(args) {
            var target = ribbon.getAppendToElement();
            var tooltipPopup = args.element;
            if (tooltipPopup && !target.contains(tooltipPopup)) {
                target.appendChild(tooltipPopup);
            }
        }
    }
    exports.createTooltip = createTooltip;
    function destroyTooltip(element) {
        var control = ej2_base_1.getComponent(element, ej2_popups_1.Tooltip);
        control.destroy();
    }
    exports.destroyTooltip = destroyTooltip;
    function updateTooltipProp(element, prop) {
        var control = ej2_base_1.getComponent(element, ej2_popups_1.Tooltip);
        control.setProperties(prop);
    }
    exports.updateTooltipProp = updateTooltipProp;
    function setCustomAttributes(element, attributes) {
        for (var key in attributes) {
            if (key === 'class') {
                var elementClass = attributes['class'].replace(/\s+/g, ' ').trim();
                Eif (elementClass) {
                    ej2_base_1.addClass([element], elementClass.split(' '));
                }
            }
            else if (key === 'style') {
                var prevStyles = element.getAttribute('style') || '';
                var value = "" + prevStyles + attributes["" + key];
                element.setAttribute("" + key, value);
            }
            else {
                element.setAttribute(key, attributes["" + key]);
            }
        }
    }
    exports.setCustomAttributes = setCustomAttributes;
    function publishPositiveWidth(target, property, width) {
        if (!target || width <= 0) {
            return;
        }
        if (target.style.getPropertyValue(property) !== width + 'px') {
            target.style.setProperty(property, width + 'px');
        }
    }
    exports.publishPositiveWidth = publishPositiveWidth;
    function observeElementSize(element, callback) {
        var observerCtor = window.ResizeObserver;
        Iif (typeof observerCtor === 'undefined' || !element) {
            return null;
        }
        var observer = new observerCtor(callback);
        observer.observe(element);
        return observer;
    }
    exports.observeElementSize = observeElementSize;
    function getLocaleText(parent, text) {
        var locale = parent.l10n.getConstant(text);
        return (locale === '') ? text : locale;
    }
    exports.getLocaleText = getLocaleText;
});