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.

72 lines
2.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 ajax = require("web.ajax");
  6. var core = require("web.core");
  7. var options = require("web_editor.snippets.options");
  8. var ready = ajax.loadXML(
  9. "/web_editor_background_color/static/src/xml/colorpicker.xml",
  10. core.qweb
  11. );
  12. 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(".colorpicker button").on(
  17. "click",
  18. $.proxy(this.remove_inline_background_color, this)
  19. );
  20. // Enable custom color picker
  21. this.$custom = this.$el.find(".bg-custom");
  22. this.$custom.colorpicker({
  23. color: this.$target.css("background-color"),
  24. container: true,
  25. });
  26. this.$custom.on(
  27. "changeColor",
  28. $.proxy(this.set_inline_background_color, this)
  29. );
  30. this.$custom.on("click", $.proxy(this.custom_click, this));
  31. this.$custom.on(
  32. "click",
  33. "input",
  34. $.proxy(this.input_select, this)
  35. );
  36. // Activate border color changes if it matches background's
  37. var style = this.$target.prop("style");
  38. this.change_border =
  39. style["border-color"] &&
  40. style["background-color"] === style["border-color"];
  41. },
  42. custom_click: function (event) {
  43. // HACK Avoid dropdown disappearing when picking colors
  44. event.stopPropagation();
  45. },
  46. input_select: function (event) {
  47. $(event.target).focus().select();
  48. this.$custom.colorpicker("show");
  49. },
  50. remove_inline_background_color: function (event) {
  51. this.$target.css("background-color", "");
  52. if (this.change_border) {
  53. this.$target.css("border-color", "");
  54. }
  55. },
  56. set_inline_background_color: function (event) {
  57. var color = String(event.color);
  58. this.$target.css("background-color", color);
  59. if (this.change_border) {
  60. this.$target.css("border-color", color);
  61. }
  62. },
  63. });
  64. return {
  65. ready: ready,
  66. colorpicker: options.registry.colorpicker,
  67. };
  68. });