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.

126 lines
4.3 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.color', 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 AbstractField = require('web.AbstractField');
  26. var _t = core._t;
  27. var QWeb = core.qweb;
  28. var FieldColor = fields.InputField.extend({
  29. events: _.extend({}, fields.InputField.prototype.events, {
  30. "click .mk_field_color_button": "_onCustomColorButtonClick",
  31. }),
  32. template: "muk_web_utils.FieldColor",
  33. supportedFieldTypes: ['char'],
  34. start: function() {
  35. this.$input = this.$('.mk_field_color_input');
  36. return this._super.apply(this, arguments);
  37. },
  38. _renderEdit: function () {
  39. this.$('.mk_field_color_input').val(
  40. this._formatValue(this.value)
  41. );
  42. this.$('.mk_field_color_input').css({
  43. 'background-color': this._formatValue(this.value),
  44. });
  45. },
  46. _renderReadonly: function () {
  47. this.$el.text(this._formatValue(this.value));
  48. this.$el.css({'color': this._formatValue(this.value)});
  49. },
  50. _doAction: function() {
  51. this._super.apply(this, arguments);
  52. this.$('.mk_field_color_input').css({
  53. 'background-color': this._getValue(),
  54. });
  55. },
  56. _formatValue: function (value) {
  57. return value;
  58. },
  59. _parseValue: function (value) {
  60. if((/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i).test(value)) {
  61. return value;
  62. } else {
  63. throw new Error(_.str.sprintf(_t("'%s' is not a correct color value"), value));
  64. }
  65. },
  66. _onCustomColorButtonClick: function () {
  67. var ColorpickerDialog = new colorpicker(this, {
  68. dialogClass: 'mk_field_color_picker',
  69. defaultColor: this._getValue(),
  70. });
  71. ColorpickerDialog.on('colorpicker:saved', this, function (event) {
  72. this.$input.val(event.data.hex);
  73. this._doAction();
  74. });
  75. ColorpickerDialog.open();
  76. },
  77. });
  78. var FieldColorIndex = AbstractField.extend({
  79. events: _.extend({}, AbstractField.prototype.events, {
  80. 'change': '_onChange',
  81. }),
  82. template: 'muk_web_utils.FieldColorIndex',
  83. supportedFieldTypes: ['integer'],
  84. isSet: function () {
  85. return this.value === 0 || this._super.apply(this, arguments);
  86. },
  87. getFocusableElement: function () {
  88. return this.$el.is('select') ? this.$el : $();
  89. },
  90. _renderEdit: function () {
  91. this.$el.addClass('mk_color_index_' + this.value);
  92. this.$('option[value="' + this.value + '"]').prop('selected', true);
  93. },
  94. _renderReadonly: function () {
  95. this.$el.addClass('mk_color_index_' + this.value);
  96. this.$el.empty().text('Color ' + this._formatValue(this.value));
  97. },
  98. _onChange: function (event) {
  99. this.$el.removeClass(function (index, className) {
  100. return (className.match (/(^|\s)mk_color_index_\S+/g) || []).join(' ');
  101. });
  102. this.$el.addClass('mk_color_index_' + this.$el.val());
  103. this._setValue(this.$el.val());
  104. },
  105. _parseValue: function (value) {
  106. if(0 > value || value > 12) {
  107. throw new Error(_.str.sprintf(_t("'%s' is not a correct color index (0-12)"), value));
  108. }
  109. return value;
  110. },
  111. });
  112. registry.add('color', FieldColor);
  113. registry.add('color_index', FieldColorIndex);
  114. return {
  115. FieldColor: FieldColor,
  116. FieldColorIndex: FieldColorIndex,
  117. };
  118. });