<commit_before>odoo.define('web_editor.rte.summernote', function (require) {
'use strict';

var core = require('web.core');
var ajax = require('web.ajax');
var base = require('web_editor.base');
var widgets = require('web_editor.widget');
var rte = require('web_editor.rte');

var QWeb = core.qweb;
var _t = core._t;

//////////////////////////////////////////////////////////////////////////////////////////////////////////

ajax.jsonRpc('/web/dataset/call', 'call', {
    'model': 'ir.ui.view',
    'method': 'read_template',
    'args': ['web_editor.colorpicker', base.get_context()]
}).done(function (data) {
    QWeb.add_template(data);
});

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Summernote Lib (neek change to make accessible: method and object) */

var dom = $.summernote.core.dom;
var range = $.summernote.core.range;
var eventHandler = $.summernote.eventHandler;
var renderer = $.summernote.renderer;
var options = $.summernote.options;

var tplButton = renderer.getTemplate().button;
var tplIconButton = renderer.getTemplate().iconButton;
var tplDropdown = renderer.getTemplate().dropdown;

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/* update and change the popovers content, and add history button */

var fn_createPalette = renderer.createPalette;
renderer.createPalette = function ($container, options) {
    fn_createPalette.call(this, $container, options);

    if (!QWeb.has_template('web_editor.colorpicker')) {
        return;
    }

    var $clpicker = $(QWeb.render('web_editor.colorpicker'));

    var groups;
    if ($clpicker.is("colorpicker")) {
        groups = _.map($clpicker.children(), function (el) {
            return $(el).find("button").empty();
        });
    } else {
        groups = [$clpicker.find("button").empty()];
    }

    var html = "<h6>" + _t("Theme colors") + "</h6>" + _.map(groups, function ($group) {
        var $row = $("<div/>", {"class": "note-color-row mb8"}).append($group);
        var $after_breaks = $row.find(".o_small + :not(.o_small)");
        if ($after_breaks.length === 0) {
            $after_breaks = $row.find(":nth-child(8n+9)");
        }
        $after_breaks.addClass("o_clear");
        return $row[0].outerHTML;
    }).join("") + "<h6>" + _t("Common colors") + "</h6>";
    var $palettes = $container.find(".note-color .note-color-palette");
    $palettes.prepend(html);

    var $bg = $palettes.first().find("button:not(.note-color-btn)").addClass("note-color-btn");
    var $fore = $palettes.last().find("button:not(.note-color-btn)").addClass("note-color-btn");
    $bg.each(function () {
        var $el = $(this);
        var className = 'bg-' + $el.data('color');
        $el.attr('data-event', 'backColor').attr('data-value', className).addClass(className);
    });
    $fore.each(function () {
        var $el = $(this);
        var className = 'text-' + $el.data('color');
        $el.attr('data-event', 'foreColor').attr('data-value', className).addClass('bg-' + $el.data('color'));
    });
};

var fn_tplPopovers = renderer.tplPopovers;
renderer.tplPopovers = function (lang, options) {
    var $popover = $(fn_tplPopovers.call(this, lang, options));

    var $imagePopover = $popover.find('.note-image-popover');
    var $linkPopover = $popover.find('.note-link-popover');
    var $airPopover = $popover.find('.note-air-popover');

    if (window === window.top) {
        $popover.children().addClass("hidden-xs");
    }

    //////////////// image popover

    // add center button for images
    var $centerbutton = $(tplIconButton('fa fa-align-center', {
            title: _t('Center'),
            event: 'floatMe',
            value: 'center'
        })).insertAfter($imagePopover.find('[data-event="floatMe"][data-value="left"]'));
    $imagePopover.find('button[data-event="removeMedia"]').parent().remove();
    $imagePopover.find('button[data-event="floatMe"][data-value="none"]').remove();

    // padding button
    var $padding = $('<div class="btn-group"/>');
    $padding.insertBefore($imagePopover.find('.btn-group:first'));
    var dropdown_content = [
        '<li><a data-event="padding" href="#" data-value="">'+_t('None')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="small">'+_t('Small')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="medium">'+_t('Medium')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="large">'+_t('Large')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="xl">'+_t('Xl')+'</a></li>',
    ];
    var $button = $(tplIconButton('fa fa-plus-square-o', {
            title: _t('Padding'),
            dropdown: tplDropdown(dropdown_content)
        })).appendTo($padding);

    // circle, boxed... options became toggled
    $imagePopover.find('[data-event="imageShape"]:not([data-value])').remove();
    var $button = $(tplIconButton('fa fa-sun-o', {
            title: _t('Shadow'),
            event: 'imageShape',
            value: 'shadow'
        })).insertAfter($imagePopover.find('[data-event="imageShape"][data-value="img-circle"]'));

    // add spin for fa
    var $spin = $('<div class="btn-group hidden only_fa"/>').insertAfter($button.parent());
    $(tplIconButton('fa fa-refresh', {
            title: _t('Spin'),
            event: 'imageShape',
            value: 'fa-spin'
        })).appendTo($spin);

    // resize for fa
    var $resizefa = $('<div class="btn-group hidden only_fa"/>')
        .insertAfter($imagePopover.find('.btn-group:has([data-event="resize"])'));
    for (var size=1; size<=5; size++) {
        $(tplButton('<span class="note-fontsize-10">'+size+'x</span>', {
          title: size+"x",
          event: 'resizefa',
          value: size+''
        })).appendTo($resizefa);
    }
    var $colorfa = $airPopover.find('.note-color').clone();
    $colorfa.find(".btn-group:first").remove();
    $colorfa.find("ul.dropdown-menu").css('min-width', '172px');
    $colorfa.find('button[data-event="color"]').attr('data-value', '{"foreColor": "#f00"}')
        .find("i").css({'background': '', 'color': '#f00'});
    $resizefa.after($colorfa);

    // show dialog box and delete
    var $imageprop = $('<div class="btn-group"/>');
    $imageprop.appendTo($imagePopover.find('.popover-content'));
    $(tplIconButton('fa fa-picture-o', {
            title: _t('Edit'),
            event: 'showImageDialog'
        })).appendTo($imageprop);
    $(tplIconButton('fa fa-trash-o', {
            title: _t('Remove'),
            event: 'delete'
        })).appendTo($imageprop);

    $imagePopover.find('.popover-content').append($airPopover.find(".note-history").clone());

    $imagePopover.find('[data-event="showImageDialog"]').before($airPopover.find('[data-event="showLinkDialog"]').clone());

    var $alt = $('<div class="btn-group"/>');
    $alt.appendTo($imagePopover.find('.popover-content'));
    $alt.append('<button class="btn btn-default btn-sm btn-small" data-event="alt"><strong>' + _t('Description') + ': </strong><span class="o_image_alt"/></button>');

    //////////////// link popover

    $linkPopover.find('.popover-content').append($airPopover.find(".note-history").clone());

    $linkPopover.find('button[data-event="showLinkDialog"] i').attr("class", "fa fa-link");
    $linkPopover.find('button[data-event="unlink"]').before($airPopover.find('button[data-event="showImageDialog"]').clone());

    //////////////// text/air popover

    //// highlight the text format
    $airPopover.find('.note-style').on('mousedown', function () {
        var $format = $airPopover.find('[data-event="formatBlock"]');
        var node = range.create().sc;
        var formats = $format.map(function () { return $(this).data("value"); }).get();
        while (node && (!node.tagName || (!node.tagName || formats.indexOf(node.tagName.toLowerCase()) === -1))) {
            node = node.parentNode;
        }
        $format.parent().removeClass('active');
        $format.filter('[data-value="'+(node ? node.tagName.toLowerCase() : "p")+'"]')
            .parent().addClass("active");
    });

    //////////////// tooltip

    setTimeout(function () {
        $airPopover.add($linkPopover).add($imagePopover).find("button")
            .tooltip('destroy')
            .tooltip({<commit_msg>[FIX] web_editor: properly bind event to the editor style dropdown<commit_after>odoo.define('web_editor.rte.summernote', function (require) {
'use strict';

var core = require('web.core');
var ajax = require('web.ajax');
var base = require('web_editor.base');
var widgets = require('web_editor.widget');
var rte = require('web_editor.rte');

var QWeb = core.qweb;
var _t = core._t;

//////////////////////////////////////////////////////////////////////////////////////////////////////////

ajax.jsonRpc('/web/dataset/call', 'call', {
    'model': 'ir.ui.view',
    'method': 'read_template',
    'args': ['web_editor.colorpicker', base.get_context()]
}).done(function (data) {
    QWeb.add_template(data);
});

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Summernote Lib (neek change to make accessible: method and object) */

var dom = $.summernote.core.dom;
var range = $.summernote.core.range;
var eventHandler = $.summernote.eventHandler;
var renderer = $.summernote.renderer;
var options = $.summernote.options;

var tplButton = renderer.getTemplate().button;
var tplIconButton = renderer.getTemplate().iconButton;
var tplDropdown = renderer.getTemplate().dropdown;

//////////////////////////////////////////////////////////////////////////////////////////////////////////
/* update and change the popovers content, and add history button */

var fn_createPalette = renderer.createPalette;
renderer.createPalette = function ($container, options) {
    fn_createPalette.call(this, $container, options);

    if (!QWeb.has_template('web_editor.colorpicker')) {
        return;
    }

    var $clpicker = $(QWeb.render('web_editor.colorpicker'));

    var groups;
    if ($clpicker.is("colorpicker")) {
        groups = _.map($clpicker.children(), function (el) {
            return $(el).find("button").empty();
        });
    } else {
        groups = [$clpicker.find("button").empty()];
    }

    var html = "<h6>" + _t("Theme colors") + "</h6>" + _.map(groups, function ($group) {
        var $row = $("<div/>", {"class": "note-color-row mb8"}).append($group);
        var $after_breaks = $row.find(".o_small + :not(.o_small)");
        if ($after_breaks.length === 0) {
            $after_breaks = $row.find(":nth-child(8n+9)");
        }
        $after_breaks.addClass("o_clear");
        return $row[0].outerHTML;
    }).join("") + "<h6>" + _t("Common colors") + "</h6>";
    var $palettes = $container.find(".note-color .note-color-palette");
    $palettes.prepend(html);

    var $bg = $palettes.first().find("button:not(.note-color-btn)").addClass("note-color-btn");
    var $fore = $palettes.last().find("button:not(.note-color-btn)").addClass("note-color-btn");
    $bg.each(function () {
        var $el = $(this);
        var className = 'bg-' + $el.data('color');
        $el.attr('data-event', 'backColor').attr('data-value', className).addClass(className);
    });
    $fore.each(function () {
        var $el = $(this);
        var className = 'text-' + $el.data('color');
        $el.attr('data-event', 'foreColor').attr('data-value', className).addClass('bg-' + $el.data('color'));
    });
};

var fn_tplPopovers = renderer.tplPopovers;
renderer.tplPopovers = function (lang, options) {
    var $popover = $(fn_tplPopovers.call(this, lang, options));

    var $imagePopover = $popover.find('.note-image-popover');
    var $linkPopover = $popover.find('.note-link-popover');
    var $airPopover = $popover.find('.note-air-popover');

    if (window === window.top) {
        $popover.children().addClass("hidden-xs");
    }

    //////////////// image popover

    // add center button for images
    var $centerbutton = $(tplIconButton('fa fa-align-center', {
            title: _t('Center'),
            event: 'floatMe',
            value: 'center'
        })).insertAfter($imagePopover.find('[data-event="floatMe"][data-value="left"]'));
    $imagePopover.find('button[data-event="removeMedia"]').parent().remove();
    $imagePopover.find('button[data-event="floatMe"][data-value="none"]').remove();

    // padding button
    var $padding = $('<div class="btn-group"/>');
    $padding.insertBefore($imagePopover.find('.btn-group:first'));
    var dropdown_content = [
        '<li><a data-event="padding" href="#" data-value="">'+_t('None')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="small">'+_t('Small')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="medium">'+_t('Medium')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="large">'+_t('Large')+'</a></li>',
        '<li><a data-event="padding" href="#" data-value="xl">'+_t('Xl')+'</a></li>',
    ];
    var $button = $(tplIconButton('fa fa-plus-square-o', {
            title: _t('Padding'),
            dropdown: tplDropdown(dropdown_content)
        })).appendTo($padding);

    // circle, boxed... options became toggled
    $imagePopover.find('[data-event="imageShape"]:not([data-value])').remove();
    var $button = $(tplIconButton('fa fa-sun-o', {
            title: _t('Shadow'),
            event: 'imageShape',
            value: 'shadow'
        })).insertAfter($imagePopover.find('[data-event="imageShape"][data-value="img-circle"]'));

    // add spin for fa
    var $spin = $('<div class="btn-group hidden only_fa"/>').insertAfter($button.parent());
    $(tplIconButton('fa fa-refresh', {
            title: _t('Spin'),
            event: 'imageShape',
            value: 'fa-spin'
        })).appendTo($spin);

    // resize for fa
    var $resizefa = $('<div class="btn-group hidden only_fa"/>')
        .insertAfter($imagePopover.find('.btn-group:has([data-event="resize"])'));
    for (var size=1; size<=5; size++) {
        $(tplButton('<span class="note-fontsize-10">'+size+'x</span>', {
          title: size+"x",
          event: 'resizefa',
          value: size+''
        })).appendTo($resizefa);
    }
    var $colorfa = $airPopover.find('.note-color').clone();
    $colorfa.find(".btn-group:first").remove();
    $colorfa.find("ul.dropdown-menu").css('min-width', '172px');
    $colorfa.find('button[data-event="color"]').attr('data-value', '{"foreColor": "#f00"}')
        .find("i").css({'background': '', 'color': '#f00'});
    $resizefa.after($colorfa);

    // show dialog box and delete
    var $imageprop = $('<div class="btn-group"/>');
    $imageprop.appendTo($imagePopover.find('.popover-content'));
    $(tplIconButton('fa fa-picture-o', {
            title: _t('Edit'),
            event: 'showImageDialog'
        })).appendTo($imageprop);
    $(tplIconButton('fa fa-trash-o', {
            title: _t('Remove'),
            event: 'delete'
        })).appendTo($imageprop);

    $imagePopover.find('.popover-content').append($airPopover.find(".note-history").clone());

    $imagePopover.find('[data-event="showImageDialog"]').before($airPopover.find('[data-event="showLinkDialog"]').clone());

    var $alt = $('<div class="btn-group"/>');
    $alt.appendTo($imagePopover.find('.popover-content'));
    $alt.append('<button class="btn btn-default btn-sm btn-small" data-event="alt"><strong>' + _t('Description') + ': </strong><span class="o_image_alt"/></button>');

    //////////////// link popover

    $linkPopover.find('.popover-content').append($airPopover.find(".note-history").clone());

    $linkPopover.find('button[data-event="showLinkDialog"] i').attr("class", "fa fa-link");
    $linkPopover.find('button[data-event="unlink"]').before($airPopover.find('button[data-event="showImageDialog"]').clone());

    //////////////// text/air popover

    //// highlight the text format
    $airPopover.find('.note-style .dropdown-toggle').on('mousedown', function () {
        var $format = $airPopover.find('[data-event="formatBlock"]');
        var node = range.create().sc;
        var formats = $format.map(function () { return $(this).data("value"); }).get();
        while (node && (!node.tagName || (!node.tagName || formats.indexOf(node.tagName.toLowerCase()) === -1))) {
            node = node.parentNode;
        }
        $format.parent().removeClass('active');
        $format.filter('[data-value="'+(node ? node.tagName.toLowerCase() : "p")+'"]')
            .parent().addClass("active");
    });

    //////////////// tooltip

    setTimeout(function () {
        $airPopover.add($linkPopover).add($imagePopover).find("button")
            .tooltip('destroy')
            .tooltip({
{'fraction_non_alphanumeric': 0.25112719506407216, 'fraction_numerical': 0.0024916943521594683, 'mean_word_length': 2.814663951120163, 'pattern_counts': {'":': 6, '<': 79, '<?xml version=': 0, '>': 77, 'https://': 0, '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': '3307307', 'n_tokens_mistral': 5228, 'n_tokens_neox': 5207, 'n_words': 847}