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.

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