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.

171 lines
6.0 KiB

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