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.

85 lines
2.9 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.ColorChar', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var fields = require('web.basic_fields');
  23. var registry = require('web.field_registry');
  24. var colorpicker = require('web.colorpicker');
  25. var _t = core._t;
  26. var QWeb = core.qweb;
  27. var FieldColor = fields.InputField.extend({
  28. events: _.extend({}, fields.InputField.prototype.events, {
  29. "click .mk_field_color_button": "_onCustomColorButtonClick",
  30. }),
  31. template: "muk_web_utils.FieldColor",
  32. supportedFieldTypes: ['char'],
  33. start: function() {
  34. this.$input = this.$('.mk_field_color_input');
  35. return this._super.apply(this, arguments);
  36. },
  37. _renderEdit: function () {
  38. this.$('.mk_field_color_input').val(
  39. this._formatValue(this.value)
  40. );
  41. this.$('.mk_field_color_input').css({
  42. 'background-color': this._formatValue(this.value),
  43. });
  44. },
  45. _renderReadonly: function () {
  46. this.$el.text(this._formatValue(this.value));
  47. this.$el.css({'color': this._formatValue(this.value)});
  48. },
  49. _doAction: function() {
  50. this._super.apply(this, arguments);
  51. this.$('.mk_field_color_input').css({
  52. 'background-color': this._getValue(),
  53. });
  54. },
  55. _formatValue: function (value) {
  56. return value;
  57. },
  58. _parseValue: function (value) {
  59. if((/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(value)) {
  60. return value;
  61. } else {
  62. throw new Error(_.str.sprintf(core._t("'%s' is not a correct color value"), value));
  63. }
  64. },
  65. _onCustomColorButtonClick: function () {
  66. var ColorpickerDialog = new colorpicker(this, {
  67. dialogClass: 'mk_field_color_picker',
  68. defaultColor: this._getValue(),
  69. });
  70. ColorpickerDialog.on('colorpicker:saved', this, function (event) {
  71. this.$input.val(event.data.hex);
  72. this._doAction();
  73. });
  74. ColorpickerDialog.open();
  75. },
  76. });
  77. registry.add('color', FieldColor);
  78. return FieldColor;
  79. });