You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.7 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.snippet_options', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var colorpicker = require('web.colorpicker');
  23. var options = require('web_editor.snippets.options');
  24. var _t = core._t;
  25. var QWeb = core.qweb;
  26. options.registry.colorpicker.include({
  27. events: _.extend({}, options.registry.colorpicker.prototype.events || {}, {
  28. 'click .mk_add_custom_color': '_onCustomColorButtonClick',
  29. }),
  30. start: function () {
  31. var res = this._super.apply(this, arguments);
  32. this._renderPickedColors();
  33. return res;
  34. },
  35. _renderPickedColors: function () {
  36. var $editable = window.__EditorMenuBar_$editable;
  37. if (this.$el.find('.colorpicker').length && $editable.length) {
  38. var $snippets = $editable.find('.mk_custom_background');
  39. var colors = _.map($snippets, function ($el) {
  40. return $el.style.backgroundColor;
  41. });
  42. this.$el.find('.colorpicker .mk_custom_color').remove();
  43. this.$el.find('.colorpicker button.selected').removeClass('selected');
  44. _.each( _.uniq(colors), function (color) {
  45. var classes = ['mk_custom_color'];
  46. if (this.$target.css('background-color') === color) {
  47. classes.push('selected');
  48. }
  49. var $colorButton = $('<button/>', {
  50. style: 'background-color:' + color + ';',
  51. class: classes.join(' '),
  52. });
  53. this.$el.find('.mk_add_custom_color').before($colorButton);
  54. }, this);
  55. }
  56. },
  57. _onColorButtonEnter: function (event) {
  58. if ($(event.target).hasClass('mk_custom_color')) {
  59. var color = event.currentTarget.style.backgroundColor;
  60. this.$target.removeClass(this.classes);
  61. this.$target.css('background-color', color);
  62. this.$target.trigger('background-color-event', true);
  63. } else {
  64. this._super.apply(this, arguments);
  65. }
  66. },
  67. _onColorButtonClick: function (event) {
  68. this._super.apply(this, arguments);
  69. var isCustomColor = $(event.target).hasClass('mk_custom_color');
  70. this.$target.toggleClass('mk_custom_background', isCustomColor);
  71. },
  72. _onCustomColorButtonClick: function () {
  73. var ColorpickerDialog = new colorpicker(this, {});
  74. ColorpickerDialog.on('colorpicker:saved', this, function (event) {
  75. this.$target.removeClass(this.classes);
  76. this.$target.addClass('mk_custom_background');
  77. this.$target.css('background-color', event.data.cssColor);
  78. this.$target.closest('.o_editable').trigger('content_changed');
  79. this.$target.trigger('background-color-event', false);
  80. this._renderPickedColors();
  81. });
  82. ColorpickerDialog.open();
  83. },
  84. });
  85. });