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.

129 lines
4.3 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Web Utils
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_utils.color', function (require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var fields = require('web.basic_fields');
  26. var registry = require('web.field_registry');
  27. var colorpicker = require('web.colorpicker');
  28. var AbstractField = require('web.AbstractField');
  29. var _t = core._t;
  30. var QWeb = core.qweb;
  31. var FieldColor = fields.InputField.extend({
  32. events: _.extend({}, fields.InputField.prototype.events, {
  33. "click .mk_field_color_button": "_onCustomColorButtonClick",
  34. }),
  35. template: "muk_web_utils.FieldColor",
  36. supportedFieldTypes: ['char'],
  37. start: function() {
  38. this.$input = this.$('.mk_field_color_input');
  39. return this._super.apply(this, arguments);
  40. },
  41. _renderEdit: function () {
  42. this.$('.mk_field_color_input').val(
  43. this._formatValue(this.value)
  44. );
  45. this.$('.mk_field_color_input').css({
  46. 'background-color': this._formatValue(this.value),
  47. });
  48. },
  49. _renderReadonly: function () {
  50. this.$el.text(this._formatValue(this.value));
  51. this.$el.css({'color': this._formatValue(this.value)});
  52. },
  53. _doAction: function() {
  54. this._super.apply(this, arguments);
  55. this.$('.mk_field_color_input').css({
  56. 'background-color': this._getValue(),
  57. });
  58. },
  59. _formatValue: function (value) {
  60. return value;
  61. },
  62. _parseValue: function (value) {
  63. if((/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(value)) {
  64. return value;
  65. } else {
  66. throw new Error(_.str.sprintf(_t("'%s' is not a correct color value"), value));
  67. }
  68. },
  69. _onCustomColorButtonClick: function () {
  70. var ColorpickerDialog = new colorpicker(this, {
  71. dialogClass: 'mk_field_color_picker',
  72. defaultColor: this._getValue(),
  73. });
  74. ColorpickerDialog.on('colorpicker:saved', this, function (event) {
  75. this.$input.val(event.data.hex);
  76. this._doAction();
  77. });
  78. ColorpickerDialog.open();
  79. },
  80. });
  81. var FieldColorIndex = AbstractField.extend({
  82. events: _.extend({}, AbstractField.prototype.events, {
  83. 'change': '_onChange',
  84. }),
  85. template: 'muk_web_utils.FieldColorIndex',
  86. supportedFieldTypes: ['integer'],
  87. isSet: function () {
  88. return this.value === 0 || this._super.apply(this, arguments);
  89. },
  90. getFocusableElement: function () {
  91. return this.$el.is('select') ? this.$el : $();
  92. },
  93. _renderEdit: function () {
  94. this.$el.addClass('mk_color_index_' + this.value);
  95. this.$('option[value="' + this.value + '"]').prop('selected', true);
  96. },
  97. _renderReadonly: function () {
  98. this.$el.addClass('mk_color_index_' + this.value);
  99. this.$el.empty().text('Color ' + this._formatValue(this.value));
  100. },
  101. _onChange: function (event) {
  102. this.$el.removeClass(function (index, className) {
  103. return (className.match (/(^|\s)mk_color_index_\S+/g) || []).join(' ');
  104. });
  105. this.$el.addClass('mk_color_index_' + this.$el.val());
  106. this._setValue(this.$el.val());
  107. },
  108. _parseValue: function (value) {
  109. if(0 > value || value > 12) {
  110. throw new Error(_.str.sprintf(_t("'%s' is not a correct color index (0-12)"), value));
  111. }
  112. return value;
  113. },
  114. });
  115. registry.add('color', FieldColor);
  116. registry.add('color_index', FieldColorIndex);
  117. return {
  118. FieldColor: FieldColor,
  119. FieldColorIndex: FieldColorIndex,
  120. };
  121. });