"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Provides utilities for DOM manipulation
 */
var DomUtil = /** @class */ (function () {
    function DomUtil() {
    }
    /**
     * Adds a class to a HTMLElement.
     */
    DomUtil.addClass = function (el, className) {
        if (el.classList)
            el.classList.add(className);
        else
            el.className += ' ' + className;
    };
    /**
     * Checks whether a HTMLElement has a class.
     */
    DomUtil.hasClass = function (el, className) {
        if (el.classList)
            el.classList.contains(className);
        else
            new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
    };
    /**
     * Removes a class form a HTMLElement.
     */
    DomUtil.removeClass = function (el, className) {
        if (el.classList)
            el.classList.remove(className);
        else
            el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
    };
    /**
     * Removes a class from a HTMLElement when present, or adds it when not present.
     */
    DomUtil.toggleClass = function (el, className) {
        if (el.classList) {
            el.classList.toggle(className);
        }
        else {
            var classes = el.className.split(' ');
            var existingIndex = classes.indexOf(className);
            if (existingIndex >= 0)
                classes.splice(existingIndex, 1);
            else
                classes.push(className);
            el.className = classes.join(' ');
        }
    };
    /**
     * Removes a HTMLElement from the DOM.
     */
    DomUtil.remove = function (el) {
        el.parentNode.removeChild(el);
    };
    /**
     * Returns an object with all the attributes of a HTMLElement as key / value pairs.
     */
    DomUtil.getAttributes = function (el) {
        var attrs = el.attributes, obj = {}, i = 0;
        for (; i < attrs.length; i++) {
            obj[attrs[i].name] = attrs[i].value;
        }
        return obj;
    };
    /**
     * Sets attributes of a HTMLElement.
     */
    DomUtil.setAttributes = function (el, attributes) {
        Object.keys(attributes).forEach(function (k) {
            el.setAttribute(k, attributes[k]);
        });
    };
    /**
     * Get a style property of a HTMLElement.
     * @return {string} The value of the style property.
     */
    DomUtil.getStyle = function (el, styleProp) {
        var value;
        var defaultView = el.ownerDocument.defaultView;
        // W3C standard way:
        if (defaultView && defaultView.getComputedStyle) {
            // sanitize property name to css notation (hypen separated words eg. font-Size)
            styleProp = styleProp.replace(/([A-Z])/g, '-$1').toLowerCase();
            return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
        }
        else if (el['currentStyle']) { // IE
            // sanitize property name to camelCase
            styleProp = styleProp.replace(/\-(\w)/g, function (str, letter) {
                return letter.toUpperCase();
            });
            value = el['currentStyle'][styleProp];
            // convert other units to pixels on IE
            if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
                return (function (value) {
                    var oldLeft = el.style.left, oldRsLeft = el['runtimeStyle'].left;
                    el['runtimeStyle'].left = el['currentStyle'].left;
                    el.style.left = value || 0;
                    value = el.style['pixelLeft'] + 'px';
                    el.style.left = oldLeft;
                    el['runtimeStyle'].left = oldRsLeft;
                    return value;
                })(value);
            }
            return value;
        }
    };
    /**
     * Returns the width and height of the viewport.
     */
    DomUtil.viewportSize = function () {
        var obj = { width: undefined, height: undefined };
        if (typeof window !== 'undefined') {
            if (window.innerHeight) {
                obj.width = window.innerWidth;
                obj.height = window.innerHeight;
            }
            else {
                obj.width = document.documentElement.clientWidth;
                obj.height = document.documentElement.clientHeight;
            }
        }
        // todo: native
        if (typeof window === 'undefined') { }
        return obj;
    };
    DomUtil.copyToClipboard = function (str) {
        var el = document.createElement('textarea'); // Create a <textarea> element
        el.value = str; // Set its value to the string that you want copied
        el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
        el.style.position = 'absolute';
        el.style.left = '-9999px'; // Move outside the screen to make it invisible
        document.body.appendChild(el); // Append the <textarea> element to the HTML document
        var selected = document.getSelection().rangeCount > 0 // Check if there is any content selected previously
            ? document.getSelection().getRangeAt(0) // Store selection if found
            : false; // Mark as false to know no selection existed before
        el.select(); // Select the <textarea> content
        document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
        document.body.removeChild(el); // Remove the <textarea> element
        if (selected) { // If a selection existed before copying
            document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
            document.getSelection().addRange(selected); // Restore the original selection
        }
    };
    DomUtil.fadeIn = function (el, duration, delay, display) {
        if (duration === void 0) { duration = 300; }
        if (delay === void 0) { delay = 0; }
        if (display === void 0) { display = 'block'; }
        var step = (1 / duration) * 10;
        el.style.opacity = '0';
        el.style.display = display;
        setTimeout(function () {
            (function fade() {
                var val = parseFloat(el.style.opacity);
                if (!((val += step) > 1)) {
                    el.style.opacity = val.toString();
                    requestAnimationFrame(fade);
                }
            })();
        }, delay);
    };
    DomUtil.fadeOut = function (el, duration, delay, visible) {
        if (duration === void 0) { duration = 300; }
        if (delay === void 0) { delay = 0; }
        if (visible === void 0) { visible = false; }
        var step = (1 / duration) * 10;
        el.style.opacity = '1';
        setTimeout(function () {
            (function fade() {
                var opacity = Number(el.style.opacity);
                opacity = opacity - step;
                el.style.opacity = opacity.toString();
                if (opacity < 0) {
                    if (visible === false) {
                        el.style.display = 'none';
                        el.style.opacity = '1';
                    }
                }
                else {
                    requestAnimationFrame(fade);
                }
            })();
        }, delay);
    };
    DomUtil.getUrlParameterByName = function (name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    };
    return DomUtil;
}());
exports.DomUtil = DomUtil;
//# sourceMappingURL=dom-util.js.map