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.

83 lines
3.1 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 ajax = require("web.ajax");
  6. var core = require("web.core");
  7. var options = require("web_editor.snippets.options");
  8. ajax.loadXML(
  9. "/web_editor_background_color/static/src/xml/colorpicker.xml",
  10. core.qweb
  11. );
  12. return options.registry.colorpicker.include({
  13. bind_events: function () {
  14. this._super();
  15. // Remove inline background-color for normal class-based buttons
  16. this.$el.find(".o_colorpicker_section button[data-color]").on(
  17. "click",
  18. $.proxy(this.remove_inline_background_color, this)
  19. );
  20. // Enable custom color picker
  21. this.$custom = this.$el.find('[data-name="custom_color"]');
  22. this.$custom.colorpicker({
  23. color: this.$target.css("background-color"),
  24. container: true,
  25. inline: true,
  26. sliders: {
  27. saturation: {
  28. maxLeft: 118,
  29. maxTop: 118,
  30. },
  31. hue: {
  32. maxTop: 118,
  33. },
  34. alpha: {
  35. maxTop: 118,
  36. },
  37. },
  38. });
  39. this.$custom.on(
  40. "changeColor",
  41. $.proxy(this.set_inline_background_color, this));
  42. this.$custom.on(
  43. "click keypress keyup keydown",
  44. $.proxy(this.custom_abort_event, this));
  45. this.$custom.on(
  46. "click", "input",
  47. $.proxy(this.input_select, this));
  48. this.$el.find(".note-color-reset").on(
  49. "click",
  50. $.proxy(this.remove_inline_background_color, this));
  51. // Activate border color changes if it matches background's
  52. var style = this.$target.prop("style");
  53. this.change_border =
  54. style["border-color"] &&
  55. style["background-color"] === style["border-color"];
  56. },
  57. custom_abort_event: function (event) {
  58. // HACK Avoid dropdown disappearing when picking colors
  59. event.stopPropagation();
  60. },
  61. input_select: function (event) {
  62. $(event.target).focus().select();
  63. },
  64. remove_inline_background_color: function (event) {
  65. this.$target.css("background-color", "");
  66. if (this.change_border) {
  67. this.$target.css("border-color", "");
  68. }
  69. this.$target.trigger("background-color-event", event.type);
  70. },
  71. set_inline_background_color: function (event) {
  72. var color = String(event.color);
  73. this.$target.css("background-color", color);
  74. if (this.change_border) {
  75. this.$target.css("border-color", color);
  76. }
  77. this.$target.trigger("background-color-event", event.type);
  78. },
  79. });
  80. });