<commit_before>/**
https://openclassrooms.com/courses/une-palette-de-couleurs
http://htmlcolorcodes.com/fr/selecteur-de-couleur/
 */


class Color {
    constructor(parameters) {
        // Init attributes
        this.colors = this.currentColors = [];
        this.parentElement = (parameters.parentElement !== undefined) ? parameters.parentElement : 'toolbar';
        this.rootElement = (parameters.rootElement !== undefined) ? parameters.rootElement : 'color';
        this.wrapperHtml = (parameters.wrapperHtml !== undefined) ? parameters.wrapperHtml : 'Colors: <ul>%s</ul>';
        this.childHtml = (parameters.childHtml !== undefined) ? parameters.childHtml : '<li><a href="#" style="background-color:%s" data-color="%s"></a></li>';

        this.setColors(parameters.colors);
    }
    getColors() {
        return this.colors;
    }
    setColors(colors) {
        if (typeof colors === 'function') {
            /* http://krazydad.com/tutorials/makecolors.php*/
            this.colors = colors();
        } else if (colors instanceof Object) {
            this.colors = Utils.objectToArray(colors);
        } else if (colors instanceof Array) {
            this.colors = colors;
        }
    }
    objectToArray(obj) {
        var arr = [];

        if (typeof obj !== 'object') {
            throw new Exception('Not an object');
        }

        for (var i in obj) {
            arr[i] = obj[i];
        }
        return arr;
    }
    getColor(idx) {
        if (idx !== undefined || this.currentColors.indexOf(idx) === -1) {
            idx = 0;
        }
        return this.currentColors.indexOf(idx);
    }
    setColor(idx, colorValue) {
        this.currentColors[idx] = colorValue;
    }
    setColor(colorValue) {
        return this.setColor(0, colorValue);
    }
    render() {
        var render = content = '';

        this.colors.forEach(function(element) {
            render += this.childHtml.replace(/%s/g, element);
        }, this);
        render = this.wrapperHtml.replace(/%s/g, render);

        content = document.createElement('div');
        content.setAttribute('id', this.rootElement);

        content.innerHTML = render;
        document.getElementById(this.parentElement).appendChild(content);

        this.initEvents();
    }
    initEvents() {
        document.getElementById(this.rootElement).addEventListener('click', function(event) {
            if (event.target !== undefined && event.target.getAttribute('data-color') !== undefined) {
                event.preventDefault();
                //this.config.colorPallet.setColor(event.target.getAttribute('data-color'));
                document.dispatchEvent(
                    new CustomEvent('change-color', {detail: {selectedColor: event.target.getAttribute('data-color')}})
                );
            }
        });
    }<commit_msg>Update color.js<commit_after>/**
https://openclassrooms.com/courses/une-palette-de-couleurs
http://htmlcolorcodes.com/fr/selecteur-de-couleur/
 */


class Color {
    constructor(parameters) {
        // Init attributes
        this.colors = this.currentColors = [];
        this.parentElement = (parameters.parentElement !== undefined) ? parameters.parentElement : 'toolbar';
        this.rootElement = (parameters.rootElement !== undefined) ? parameters.rootElement : 'color';
        this.wrapperHtml = (parameters.wrapperHtml !== undefined) ? parameters.wrapperHtml : 'Colors: <ul>%s</ul>';
        this.childHtml = (parameters.childHtml !== undefined) ? parameters.childHtml : '<li><a href="#" style="background-color:%s" data-color="%s"></a></li>';

        this.setColors(parameters.colors);
    }
    getColors() {
        return this.colors;
    }
    setColors(colors) {
        if (typeof colors === 'function') {
            /* http://krazydad.com/tutorials/makecolors.php*/
            this.colors = colors();
        } else if (colors instanceof Object) {
            this.colors = Utils.objectToArray(colors);
        } else if (colors instanceof Array) {
            this.colors = colors;
        }
    }
    objectToArray(obj) {
        var arr = [];

        if (typeof obj !== 'object') {
            throw new Exception('Not an object');
        }

        for (var i in obj) {
            arr[i] = obj[i];
        }
        return arr;
    }
    getColor(idx) {
        if (idx !== undefined || this.currentColors.indexOf(idx) === -1) {
            idx = 0;
        }
        return this.currentColors.indexOf(idx);
    }
    setColor(idx, colorValue) {
        this.currentColors[idx] = colorValue;
    }
    setColor(colorValue) {
        return this.setColor(0, colorValue);
    }
    render() {
        var render = '',
            content = '';

        this.colors.forEach(function(element) {
            render += this.childHtml.replace(/%s/g, element);
        }, this);
        render = this.wrapperHtml.replace(/%s/g, render);

        content = document.createElement('div');
        content.setAttribute('id', this.rootElement);

        content.innerHTML = render;
        document.getElementById(this.parentElement).appendChild(content);

        this.initEvents();
    }
    initEvents() {
        document.getElementById(this.rootElement).addEventListener('click', function(event) {
            if (event.target !== undefined && event.target.getAttribute('data-color') !== undefined) {
                event.preventDefault();
                //this.config.colorPallet.setColor(event.target.getAttribute('data-color'));
                document.dispatchEvent(
                    new CustomEvent('change-color', {detail: {selectedColor: event.target.getAttribute('data-color')}})
                );
            }
        });
    }
{'fraction_non_alphanumeric': 0.1463073153657683, 'fraction_numerical': 0.0010500525026251313, 'mean_word_length': 2.236126840317101, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703409', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1561, 'n_words': 271}