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.

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