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.

118 lines
4.0 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.path', 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 FieldPathNames = fields.FieldChar.extend({
  32. init: function(parent, name, record) {
  33. this._super.apply(this, arguments);
  34. this.max_width = this.nodeOptions.width || 500;
  35. },
  36. _renderReadonly: function() {
  37. var show_value = this._formatValue(this.value);
  38. var text_witdh = $.fn.textWidth(show_value);
  39. if(text_witdh >= this.max_width) {
  40. var ratio_start = (1 - (this.max_width / text_witdh)) * show_value.length;
  41. show_value = ".." + show_value.substring(ratio_start, show_value.length);
  42. }
  43. this.$el.text(show_value);
  44. },
  45. });
  46. var FieldPathJson = fields.FieldText.extend({
  47. events: _.extend({}, fields.FieldText.prototype.events, {
  48. 'click a' : '_onNodeClicked',
  49. }),
  50. init: function(parent, name, record) {
  51. this._super.apply(this, arguments);
  52. this.max_width = this.nodeOptions.width || 500;
  53. this.seperator = this.nodeOptions.seperator || "/";
  54. this.prefix = this.nodeOptions.prefix || false;
  55. this.suffix = this.nodeOptions.suffix || false;
  56. },
  57. _renderReadonly: function() {
  58. this.$el.empty();
  59. this._renderPath();
  60. },
  61. _renderPath: function() {
  62. var text_width_measure = "";
  63. var path = JSON.parse(this.value || "[]");
  64. $.each(_.clone(path).reverse(), function(index, element) {
  65. text_width_measure += element.name + "/";
  66. if($.fn.textWidth(text_width_measure) >= this.max_width) {
  67. this.$el.prepend($('<span/>').text(".."));
  68. } else {
  69. if (index == 0) {
  70. if(this.suffix) {
  71. this.$el.prepend($('<span/>').text(this.seperator));
  72. }
  73. this.$el.prepend($('<span/>').text(element.name));
  74. this.$el.prepend($('<span/>').text(this.seperator));
  75. } else {
  76. this.$el.prepend($('<a/>', {
  77. 'class': 'oe_form_uri',
  78. 'data-model': element.model,
  79. 'data-id': element.id,
  80. 'href': "javascript:void(0);",
  81. 'text': element.name,
  82. }));
  83. if (index != path.length - 1) {
  84. this.$el.prepend($('<span/>').text(this.seperator));
  85. } else if (this.prefix) {
  86. this.$el.prepend($('<span/>').text(this.seperator));
  87. }
  88. }
  89. }
  90. return ($.fn.textWidth(text_width_measure) < this.max_width);
  91. }.bind(this));
  92. },
  93. _onNodeClicked : function(event) {
  94. this.do_action({
  95. type: 'ir.actions.act_window',
  96. res_model: $(event.currentTarget).data('model'),
  97. res_id: $(event.currentTarget).data('id'),
  98. views: [[false, 'form']],
  99. target: 'current',
  100. context: {},
  101. });
  102. }
  103. });
  104. registry.add('path_names', FieldPathNames);
  105. registry.add('path_json', FieldPathJson);
  106. return {
  107. FieldPathNames: FieldPathNames,
  108. FieldPathJson: FieldPathJson,
  109. };
  110. });