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.

103 lines
3.6 KiB

  1. /* Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. * License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). */
  3. odoo.define("web_editor_background_color.colorpicker", function (require) {
  4. "use strict";
  5. var options = require("web_editor.snippets.options");
  6. var colorpicker = options.registry.colorpicker;
  7. colorpicker.include({
  8. events: _.extend({}, colorpicker.prototype.events, {
  9. "changeColor [data-name=custom_color]":
  10. "set_inline_background_color",
  11. // Remove inline background-color for normal class-based buttons
  12. "click .o_colorpicker_section button[data-color]":
  13. "remove_inline_background_color",
  14. "click [data-name=custom_color] input": "input_select",
  15. "click [data-name=custom_color]": "custom_abort_event",
  16. "keydown [data-name=custom_color]": "custom_abort_event",
  17. "keypress [data-name=custom_color]": "custom_abort_event",
  18. "keyup [data-name=custom_color]": "custom_abort_event",
  19. }),
  20. xmlDependencies: colorpicker.prototype.xmlDependencies.concat([
  21. "/web_editor_background_color/static/src/xml/colorpicker.xml",
  22. ]),
  23. /**
  24. * @override
  25. */
  26. start: function () {
  27. this._super();
  28. // Enable custom color picker
  29. this.$custom = this.$el.find('[data-name="custom_color"]');
  30. this.$custom.colorpicker({
  31. color: this.$target.css("background-color"),
  32. container: true,
  33. inline: true,
  34. sliders: {
  35. saturation: {
  36. maxLeft: 118,
  37. maxTop: 118,
  38. },
  39. hue: {
  40. maxTop: 118,
  41. },
  42. alpha: {
  43. maxTop: 118,
  44. },
  45. },
  46. });
  47. // Activate border color changes if it matches background's
  48. var style = this.$target.prop("style");
  49. this.change_border =
  50. style["border-color"] &&
  51. style["background-color"] === style["border-color"];
  52. },
  53. /**
  54. * A HACK to avoid dropdown disappearing when picking colors
  55. *
  56. * @param {Event} event
  57. */
  58. custom_abort_event: function (event) {
  59. event.stopPropagation();
  60. },
  61. /**
  62. * Select the color picker input
  63. *
  64. * @param {Event} event
  65. */
  66. input_select: function (event) {
  67. $(event.target).focus().select();
  68. },
  69. /**
  70. * Undo the inline background color, besides upstream color classes
  71. *
  72. * @override
  73. */
  74. _onColorResetButtonClick: function (event) {
  75. this._super.apply(this, arguments);
  76. this.$target.css("background-color", "");
  77. if (this.change_border) {
  78. this.$target.css("border-color", "");
  79. }
  80. this.$target.trigger("background-color-event", event.type);
  81. },
  82. /**
  83. * Apply the chosen color as an inline style
  84. *
  85. * @param {Event} event
  86. */
  87. set_inline_background_color: function (event) {
  88. var color = String(event.color);
  89. this.$target.css("background-color", color);
  90. if (this.change_border) {
  91. this.$target.css("border-color", color);
  92. }
  93. this.$target.trigger("background-color-event", event.type);
  94. },
  95. });
  96. });