OCA reporting engine fork for dev and update.
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.

155 lines
5.5 KiB

  1. /* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
  2. * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
  3. odoo.define('bi_view_editor.ModelList', function (require) {
  4. "use strict";
  5. var Widget = require('web.Widget');
  6. var core = require('web.core');
  7. var qweb = core.qweb;
  8. var ModelList = Widget.extend({
  9. template: 'bi_view_editor.ModelList',
  10. events: {
  11. 'keyup .search-bar > input': 'filterChanged',
  12. },
  13. init: function (parent) {
  14. var res = this._super(parent);
  15. this.active_models = [];
  16. this.cache_fields = {};
  17. this.current_filter = '';
  18. this.mode = null;
  19. return res;
  20. },
  21. setMode: function (mode) {
  22. if (mode === 'readonly') {
  23. this.$el.find('.search-bar').attr('disabled', true);
  24. this.$el.find('.class-list, .class').addClass('readonly');
  25. } else {
  26. this.$el.find('.search-bar').attr('disabled', false);
  27. this.$el.find('.class-list, .class').removeClass('readonly');
  28. }
  29. this.mode = mode;
  30. },
  31. isActive: function (id) {
  32. return this.active_models.indexOf(id) !== -1;
  33. },
  34. removeAsActive: function (id) {
  35. var i = this.active_models.indexOf(id);
  36. this.active_models.splice(i, 1);
  37. },
  38. addAsActive: function (id) {
  39. this.active_models.push(id);
  40. },
  41. loadModels: function (model_ids) {
  42. return this._rpc({
  43. model: 'ir.model',
  44. method: 'get_models',
  45. args: model_ids ? [model_ids] : [],
  46. });
  47. },
  48. loadFields: function (model_id) {
  49. if (!(model_id in this.cache_fields)) {
  50. var deferred = this._rpc({
  51. model: 'ir.model',
  52. method: 'get_fields',
  53. args: [model_id],
  54. });
  55. this.cache_fields[model_id] = deferred;
  56. }
  57. return this.cache_fields[model_id];
  58. },
  59. populateModels: function (models) {
  60. var self = this;
  61. this.$el.find(".class-list").html('');
  62. _.each(models, function (model) {
  63. var $html = $(qweb.render('bi_view_editor.ModelListItem', {
  64. 'id': model.id,
  65. 'model': model.model,
  66. 'name': model.name,
  67. }));
  68. $html.find('.class').data('model', model).click(function () {
  69. self.modelClicked($(this));
  70. });
  71. self.$el.find(".class-list").append($html);
  72. if (self.isActive(model.id)) {
  73. self.loadFields(model.id).done(function (fields) {
  74. self.populateFields(fields, model.id);
  75. });
  76. }
  77. });
  78. },
  79. populateFields: function (fields, model_id) {
  80. var self = this;
  81. if (!model_id && fields.length === 0) {
  82. return;
  83. }
  84. var data_model_id = model_id;
  85. if (!data_model_id) {
  86. data_model_id = fields[0].model_id;
  87. }
  88. var $model_item = this.$el.find(".class[data-id='" + data_model_id + "']");
  89. _.each(fields, function (field) {
  90. var $field = $(qweb.render('bi_view_editor.ModelListFieldItem', {
  91. name: field.name,
  92. description: field.description,
  93. })).data('field', field).click(function () {
  94. self.fieldClicked($(this));
  95. }).draggable({
  96. 'revert': 'invalid',
  97. 'scroll': false,
  98. 'helper': 'clone',
  99. 'appendTo': 'body',
  100. 'containment': 'window',
  101. });
  102. $model_item.after($field);
  103. });
  104. },
  105. modelClicked: function ($el) {
  106. if (this.mode === 'readonly') {
  107. return;
  108. }
  109. var model = $el.data('model');
  110. $el.parent().find('.field').remove();
  111. if (this.isActive(model.id)) {
  112. this.removeAsActive(model.id);
  113. } else {
  114. this.addAsActive(model.id);
  115. this.loadFields(model.id).done(function (fields) {
  116. this.populateFields(fields, model.id);
  117. }.bind(this));
  118. }
  119. },
  120. fieldClicked: function ($el) {
  121. if (this.mode === 'readonly') {
  122. return;
  123. }
  124. this.trigger('field_clicked', $el.data('field'));
  125. },
  126. filterChanged: function (e) {
  127. var $input = $(e.target);
  128. this.filter($input.val());
  129. },
  130. filter: function (value) {
  131. this.active_models = [];
  132. this.$el.find('.field').remove();
  133. var val = typeof value === 'undefined' ? this.current_filter : value.toLowerCase();
  134. this.$el.find(".class").each(function () {
  135. var data = $(this).data('model');
  136. if (data.name.toLowerCase().indexOf(val) === -1 &&
  137. data.model.toLowerCase().indexOf(val) === -1) {
  138. $(this).addClass('d-none');
  139. } else {
  140. $(this).removeClass('d-none');
  141. }
  142. });
  143. this.current_filter = val;
  144. },
  145. });
  146. return ModelList;
  147. });