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.

258 lines
9.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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.FieldList", function(require) {
  4. "use strict";
  5. var core = require("web.core");
  6. var qweb = core.qweb;
  7. var Widget = require("web.Widget");
  8. var mixins = require("web.mixins");
  9. var FieldListContextMenu = Widget.extend(
  10. _.extend({}, mixins.EventDispatcherMixin, {
  11. start: function() {
  12. var res = this._super.apply(this, arguments);
  13. this.$el.mouseleave(function() {
  14. $(this).addClass("d-none");
  15. });
  16. return res;
  17. },
  18. open: function(x, y) {
  19. this.$el.css({
  20. left: x + "px",
  21. top: y + "px",
  22. });
  23. this.$el.removeClass("d-none");
  24. return this;
  25. },
  26. })
  27. );
  28. var FieldListFieldContextMenu = FieldListContextMenu.extend({
  29. template: "bi_view_editor.FieldList.FieldContextMenu",
  30. open: function(x, y, $item) {
  31. var field = $item.data("field");
  32. this.$el.find(".checkbox-column").prop("checked", field.column);
  33. this.$el.find(".checkbox-row").prop("checked", field.row);
  34. this.$el.find(".checkbox-measure").prop("checked", field.measure);
  35. this.$el.find(".checkbox-list").prop("checked", field.list);
  36. var measureable =
  37. field.type === "float" ||
  38. field.type === "integer" ||
  39. field.type === "monetary";
  40. this.$el.find(".checkbox-column").attr("disabled", measureable);
  41. this.$el.find(".checkbox-row").attr("disabled", measureable);
  42. this.$el.find(".checkbox-measure").attr("disabled", !measureable);
  43. this.$el.find(".checkbox-list").attr("disabled", false);
  44. var events = this._super(x, y, field);
  45. this.$el.find("input").unbind("change");
  46. this.$el.find("input").change(function() {
  47. var $checkbox = $(this);
  48. var property = $checkbox.attr("data-for");
  49. field[property] = $checkbox.is(":checked");
  50. events.trigger("change", field, $item);
  51. });
  52. return events;
  53. },
  54. });
  55. var FieldListJoinContextMenu = FieldListContextMenu.extend({
  56. template: "bi_view_editor.FieldList.JoinContextMenu",
  57. open: function(x, y, $item) {
  58. var node = $item.data("field");
  59. this.$el.find(".checkbox-join-left").prop("checked", node.join_left);
  60. var events = this._super(x, y, node);
  61. this.$el.find("input").unbind("change");
  62. this.$el.find("input").change(function() {
  63. var $checkbox = $(this);
  64. var property = $checkbox.attr("data-for");
  65. node[property] = $checkbox.is(":checked");
  66. events.trigger("change", node);
  67. });
  68. return events;
  69. },
  70. });
  71. var FieldList = Widget.extend({
  72. template: "bi_view_editor.FieldList",
  73. events: {
  74. "click .delete-button": "removeClicked",
  75. 'keyup input[name="description"]': "keyupDescription",
  76. },
  77. start: function() {
  78. var res = this._super.apply(this, arguments);
  79. this.contextmenu = new FieldListFieldContextMenu(this);
  80. this.contextmenu.appendTo(this.$el);
  81. this.contextmenu.on(
  82. "change",
  83. this,
  84. function(f, $item) {
  85. $item.data("field", f);
  86. this.refreshItem($item);
  87. this.trigger("updated");
  88. }.bind(this)
  89. );
  90. this.contextmenu_join = new FieldListJoinContextMenu(this);
  91. this.contextmenu_join.appendTo(this.$el);
  92. this.contextmenu_join.on(
  93. "change",
  94. this,
  95. function(f, $item) {
  96. $item.data("field", f);
  97. this.refreshItem($item);
  98. this.trigger("updated");
  99. }.bind(this)
  100. );
  101. this.$table = this.$el.find("tbody");
  102. this.mode = null;
  103. return res;
  104. },
  105. setMode: function(mode) {
  106. if (mode === "readonly") {
  107. this.$el.find('input[type="text"]').attr("disabled", true);
  108. this.$el.find(".delete-button").addClass("d-none");
  109. } else {
  110. this.$el.find('input[type="text"]').removeAttr("disabled");
  111. this.$el.find(".delete-button").removeClass("d-none");
  112. }
  113. this.mode = mode;
  114. },
  115. get: function() {
  116. return $.makeArray(
  117. this.$el.find("tbody tr").map(function() {
  118. var field = $(this).data("field");
  119. field.description = $(this)
  120. .find('input[name="description"]')
  121. .val();
  122. return field;
  123. })
  124. );
  125. },
  126. getModelIds: function() {
  127. var model_ids = {};
  128. this.$el.find("tbody tr").each(function() {
  129. var data = $(this).data("field");
  130. model_ids[data.table_alias] = data.model_id;
  131. });
  132. return model_ids;
  133. },
  134. getModelData: function() {
  135. var model_data = {};
  136. this.$el.find("tbody tr").each(function() {
  137. var data = $(this).data("field");
  138. model_data[data.table_alias] = {
  139. model_id: data.model_id,
  140. model_name: data.model_name,
  141. };
  142. });
  143. return model_data;
  144. },
  145. add: function(field) {
  146. var self = this;
  147. field.row = typeof field.row === "undefined" ? false : field.row;
  148. field.column = typeof field.column === "undefined" ? false : field.column;
  149. field.measure =
  150. typeof field.measure === "undefined" ? false : field.measure;
  151. field.list = typeof field.list === "undefined" ? true : field.list;
  152. field._id =
  153. typeof field._id === "undefined" ? _.uniqueId("node_") : field._id;
  154. if (field.join_node) {
  155. field.join_left =
  156. typeof field.join_left === "undefined" ? false : field.join_left;
  157. }
  158. var i = 0;
  159. var name = field.name;
  160. while (
  161. this.get().filter(function(item) {
  162. return item.name === field.name;
  163. }).length > 0
  164. ) {
  165. field.name = name + "_" + i;
  166. i++;
  167. }
  168. // Render table row
  169. var $html = $(
  170. qweb.render(
  171. field.join_node
  172. ? "bi_view_editor.JoinListItem"
  173. : "bi_view_editor.FieldListItem",
  174. {
  175. field: field,
  176. }
  177. )
  178. )
  179. .data("field", field)
  180. .contextmenu(function(e) {
  181. var $item = $(this);
  182. if (self.mode === "readonly") {
  183. return;
  184. }
  185. e.preventDefault();
  186. self.openContextMenu($item, e.pageX, e.pageY);
  187. });
  188. this.$el.find("tbody").append($html);
  189. },
  190. remove: function(id) {
  191. var $item = this.$el.find('tr[data-id="' + id + '"]');
  192. $item.remove();
  193. this.trigger("removed", id);
  194. },
  195. set: function(fields) {
  196. var set_fields = fields;
  197. if (!set_fields) {
  198. set_fields = [];
  199. }
  200. this.$el.find("tbody tr").remove();
  201. for (var i = 0; i < set_fields.length; i++) {
  202. this.add(set_fields[i]);
  203. }
  204. },
  205. openContextMenu: function($item, x, y) {
  206. var field = $item.data("field");
  207. var contextmenu = field.join_node
  208. ? this.contextmenu_join
  209. : this.contextmenu;
  210. // Temporary disable contextmenu for join node (until left join is implemented)
  211. if (field.join_node) {
  212. return;
  213. }
  214. contextmenu.open(x - 20, y - 20, $item);
  215. },
  216. refreshItem: function($item) {
  217. var data = $item.data("field");
  218. var $attributes = $item.find("span[data-for], img[data-for]");
  219. $.each($attributes, function() {
  220. var $attribute = $(this);
  221. var value = data[$attribute.attr("data-for")];
  222. if (value) {
  223. $attribute.removeClass("d-none");
  224. } else {
  225. $attribute.addClass("d-none");
  226. }
  227. });
  228. },
  229. removeClicked: function(e) {
  230. var $button = $(e.currentTarget);
  231. var id = $button.attr("data-id");
  232. this.remove(id);
  233. },
  234. keyupDescription: function() {
  235. this.trigger("updated");
  236. },
  237. });
  238. return {
  239. FieldList: FieldList,
  240. FieldListContextMenu: FieldListContextMenu,
  241. FieldListFieldContextMenu: FieldListFieldContextMenu,
  242. FieldListJoinContextMenu: FieldListJoinContextMenu,
  243. };
  244. });