<commit_before>/* This file is part of Indico.
 * Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
 *
 * Indico is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Indico is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Indico; if not, see <http://www.gnu.org/licenses/>.
 */

(function($) {
    $.widget('indico.palettepicker', {

        options: {
            availableColors: [],
            onSelect: null,
            numColumns: 5
        },

        _create: function() {
            var self = this;
            var element = this.element;
            var paletteTable = $('<table>', {'class': 'palette-picker'});
            var availableColors = this.options.availableColors;
            var tr = this._createTableRow();

            for (var i = 0; i < availableColors.length; ++i) {
                var td = $('<td>', {
                    'css': {'background': '#' + availableColors[i].background},
                    'class': 'palette-color'
                });

                td.on('click', function(evt) {
                    evt.preventDefault();

                    var $this = $(this),
                        backgroundColor = $this.find('.background-box').data('value'),
                        textColor = $this.find('.text-box').data('value');

                    element.css({'background': backgroundColor, 'color': textColor + ' !important'});

                    if (self.options.onSelect) {
                        self.options.onSelect.call(element, backgroundColor, textColor);
                    }

                    element.qtip('hide');
                });

                var colorBox = $('<div>', {
                    'data-value': '#' + availableColors[i].background,
                    'class': 'background-box'
                });

                colorBox.append($('<div>', {
                    'css': {'background': '#' + availableColors[i].text},
                    'class': 'text-box',
                    'data-value': '#' + availableColors[i].text
                }));

                td.append(colorBox);
                tr.append(td);

                if ((i + 1) % this.options.numColumns == 0) {
                    paletteTable.append(tr);
                    tr = this._createTableRow();
                }
            }

            if (tr.children().length) {
                paletteTable.append(tr);
            }

            element.qtip({
                prerender: false,
                overwrite: false,
                style: {
                    classes: 'palette-picker-qtip'
                },
                position: {
                    my: 'top center',
                    at: 'bottom center',
                    target: element,
                    adjust: {
                        mouse: false,
                        scroll: false
                    }
                },
                content: {
                    text: $(paletteTable)
                },<commit_msg>Refactor the palettepicker<commit_after>/* This file is part of Indico.
 * Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
 *
 * Indico is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Indico is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Indico; if not, see <http://www.gnu.org/licenses/>.
 */

(function($) {
    $.widget('indico.palettepicker', {

        options: {
            availableColors: [],
            onSelect: null,
            numColumns: 5
        },

        _create: function() {
            var self = this;
            var element = this.element;
            var paletteTable = $('<table>', {'class': 'palette-picker'});
            var availableColors = this.options.availableColors;
            var tr = this._createTableRow();

            $.each(availableColors, function(index, color) {
                var td = $('<td>', {
                    'css': {'background': '#' + color.background},
                    'class': 'palette-color',
                    'data': {color: color}
                });

                var colorBox = $('<div>', {
                    'class': 'background-box'
                });

                colorBox.append($('<div>', {
                    'css': {'background': '#' + color.text},
                    'class': 'text-box',
                }));

                td.append(colorBox);
                tr.append(td);

                if ((index + 1) % self.options.numColumns == 0) {
                    paletteTable.append(tr);
                    tr = self._createTableRow();
                }
            });

            if (tr.children().length) {
                paletteTable.append(tr);
            }

            paletteTable.on('click', '.palette-color', function(evt) {
                var $this = $(this),
                    color = $this.data('color'),
                    backgroundColor = '#' + color.background,
                    textColor = '#' + color.text;

                element.css({'background': backgroundColor, 'color': textColor + ' !important'});

                if (self.options.onSelect) {
                    self.options.onSelect.call(element, backgroundColor, textColor);
                }

                element.qtip('hide');
            });

            element.qtip({
                prerender: false,
                overwrite: false,
                style: {
                    classes: 'palette-picker-qtip'
                },
                position: {
                    my: 'top center',
                    at: 'bottom center',
                    target: element,
                    adjust: {
                        mouse: false,
                        scroll: false
                    }
                },
                content: {
                    text: paletteTable
                },
{'fraction_non_alphanumeric': 0.11626872312027288, 'fraction_numerical': 0.0037075485688862525, 'mean_word_length': 1.343293954134816, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2004753', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1574, 'n_words': 457}