All files Helper.js

23.08% Statements 12/52
43.86% Branches 25/57
33.33% Functions 7/21
23.53% Lines 12/51
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            1x                                                                                                                                                                                               33x 11x                                                                                                                                         7x 22x 17x     7x                 8x             2x             6x             4x             3x    
/**
 * @author Michael Duve <mduve@designmail.net>
 * @file Helper for general purposes
 * @copyright Michael Duve 2016
 * @module Helper
 */
export const Helper = {
    /**
     * request json-data from given file and calls callback on success
     * @function
     * @memberof module:Helper
     * @param  {String} filename - path to file
     * @param  {Helper~requestJSONCallback} callback - function called when data is loaded successfully
     * @return {Helper} Helper object for chaining
     */
    requestJSON(filename, callback) {
        Helper.getFile(filename, (jsonFileData) => {
            if (callback) {
                callback(JSON.parse(jsonFileData));
            }
        });
        return this;
    },
    /**
     * find an element in DOM
     * @function
     * @memberof module:Helper
     * @param  {String} elementString = "*" - element to lookup
     * @param  {HTMLElement} element = null - element to start looking in for
     * @return {HTMLElement} reference to HTMLElement or null if not found
     */
    find(elementString = "*", element = null) {
        return (element || document).querySelector(elementString);
    },
    /**
     * find multiple elements in DOM
     * @function
     * @memberof module:Helper
     * @param  {String} elementString = "*" - element to lookup
     * @param  {HTMLElement} element = null - element to start looking in for
     * @return {NodeList} list of found elements
     */
    findAll(elementString = "*", element = null) {
        return (element || document).querySelectorAll(elementString);
    },
    /**
     * show a HTMLElement
     * @function
     * @memberof module:Helper
     * @param  {HTMLElement} elem - specified element
     * @return {Helper} reference of Helper for chaining
     */
    show(elem) {
        elem.style.display = "";
        return this;
    },
    /**
     * hide a HTMLElement
     * @function
     * @memberof module:Helper
     * @param  {HTMLElement} elem - specified element
     * @return {Helper} reference of Helper for chaining
     */
    hide(elem) {
        elem.style.display = "none";
        return this;
    },
    /**
     * applies css to given HTMLElement
     * @function
     * @memberof module:Helper
     * @param  {HTMLElement} elem - specified element to apply css to
     * @param  {Object} css - object containing css property and value
     * @return {Helper} reference of Helper for chaining
     */
    css(elem, css) {
        for (const property in css) {
            elem.style[property] = css[property];
        }
    },
    /**
     * adds a listener to given HTMLElement
     * @function
     * @memberof module:Helper
     * @param  {HTMLElement} elem - specified element to bind listener to
     * @param  {String} bindTo - names of events to bind to, seperated by spaces
     * @param  {Function} fn~addListenerCallback - callback-function for binding
     * @return {Helper} reference of Helper for chaining
     */
    addListener(elem, bindTo, fn) {
        bindTo.split(" ").forEach((e) => elem.addEventListener(e, fn, false));
        return this;
    },
    /**
     * clamps a value to specified min and max
     * @function
     * @memberof module:Helper
     * @param  {Number} v = 0 - specified value to clamp
     * @param  {Number} min = v - minimum value to clamp to
     * @param  {Number} max = v - maximum value to clamp to
     * @return {Number} clamped value
     */
    clamp(v = 0, min = v, max = v) {
        return Math.min(Math.max(v, min), max);
    },
    /**
     * loads an image and calls callback on success
     * @function
     * @memberof module:Helper
     * @param {Helper~loadImageCallback} cb - callback-function on success
     * @return {Helper} reference of Helper for chaining
     */
    loadImage(path, cb) {
        const img = new Image();
        img.onload = () => {
            if (cb && typeof cb === "function") {
                cb(img);
            }
        };
        img.src = path;
        return this;
    },
    /**
     * request data from given file and calls callback on success
     * @function
     * @memberof module:Helper
     * @param  {string} url - path to file
     * @param  {Helper~getFileCallback} callback - function called when data is loaded successfully
     * @return {Helper} reference of Helper for chaining
     */
    getFile(url, callback) {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = () => {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200 && callback) {
                    callback(xhr.responseText);
                } else {
                    throw new Error("The JSON submitted seems not valid", xhr);
                }
            }
        };
        xhr.open("GET", url, true);
        xhr.send();
        return this;
    },
    /**
     * loads a js script
     * @function
     * @memberof module:Helper
     * @param  {String} url - url to be loaded
     * @param  {Helper~loadScriptCallback} callback - function called when script is loaded successfully
     * @return {Helper} reference of Helper for chaining
     */
    loadScript(url, callback) {
        const head = Helper.find('head');
        const script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        script.onreadystatechange = callback;
        script.onload = callback;
        head.appendChild(script);
        return this;
    },
    /**
     * for each helper
     * @function
     * @memberof module:Helper
     * @param  {Object[]} a - array to iterate over each value
     * @param  {Helper~forEachCallback} cb - callback for each object
     * @return {Helper} reference of Helper for chaining
     */
    forEach(a, cb) {
        for (const i in a) {
            if (a[i] !== undefined && typeof cb === "function") {
                cb(a[i], i);
            }
        }
        return this;
    },
    /**
     * convert degree to radian
     * @function
     * @memberof module:Helper
     * @param {Number} degrees - specified degrees
     * @return {Number} converted radian
     */
    toRadians: degrees => degrees * Math.PI / 180,
    /**
     * checks if mouse is possible
     * @function
     * @memberof module:Helper
     * @return {Boolean} if true, mouse is possible
     */
    isMouse: () => ('onmousedown' in window),
    /**
     * checks if touch is possible
     * @function
     * @memberof module:Helper
     * @return {Boolean} if true, touch is possible
     */
    isTouch: () => (('ontouchstart' in window) || (navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)),
    /**
     * checks if IE is used
     * @function
     * @memberof module:Helper
     * @return {Boolean} if true, IE is used
     */
    isIE: () => ((navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)),
    /**
     * gets cross-browser scroll-event
     * @function
     * @memberof module:Helper
     * @return {String} name of scroll event
     */
    scrollEvent: () => "onwheel" in document.createElement("div") ? "wheel" : document.onmousewheel !== undefined ? "mousewheel" : "DOMMouseScroll"
};