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.

129 lines
4.9 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_searchpanel.KanbanView', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var config = require('web.config');
  23. var pyUtils = require('web.py_utils');
  24. var utils = require('web.utils');
  25. var KanbanView = require('web.KanbanView');
  26. var SearchPanel = require('web.SearchPanel');
  27. var _t = core._t;
  28. var QWeb = core.qweb;
  29. KanbanView.include({
  30. config: _.extend({}, KanbanView.prototype.config, {
  31. SearchPanel: SearchPanel,
  32. }),
  33. init: function (viewInfo, params) {
  34. this.searchPanelSections = Object.create(null);
  35. this._super.apply(this, arguments);
  36. this.hasSearchPanel = !_.isEmpty(this.searchPanelSections);
  37. },
  38. getController: function (parent) {
  39. var self = this;
  40. var def = undefined;
  41. if (this.hasSearchPanel) {
  42. def = this._createSearchPanel(parent);
  43. }
  44. var _super = this._super.bind(this);
  45. return $.when(def).then(function (searchPanel) {
  46. return _super(parent).done(function (controller) {
  47. if (self.hasSearchPanel) {
  48. self.controllerParams.searchPanel.setParent(controller);
  49. }
  50. return controller
  51. });
  52. });
  53. },
  54. _createSearchPanel: function (parent) {
  55. var self = this;
  56. var defaultCategoryValues = {};
  57. Object.keys(this.loadParams.context).forEach(function (key) {
  58. var match = /^searchpanel_default_(.*)$/.exec(key);
  59. if (match) {
  60. defaultCategoryValues[match[1]] = self.loadParams.context[key];
  61. }
  62. });
  63. var controlPanelDomain = this.loadParams.domain;
  64. var searchPanel = new this.config.SearchPanel(parent, {
  65. defaultCategoryValues: defaultCategoryValues,
  66. fields: this.fields,
  67. model: this.loadParams.modelName,
  68. searchDomain: controlPanelDomain,
  69. sections: this.searchPanelSections,
  70. });
  71. this.controllerParams.searchPanel = searchPanel;
  72. this.controllerParams.controlPanelDomain = controlPanelDomain;
  73. return searchPanel.appendTo(document.createDocumentFragment()).then(function () {
  74. var searchPanelDomain = searchPanel.getDomain();
  75. self.loadParams.domain = controlPanelDomain.concat(searchPanelDomain);
  76. });
  77. },
  78. _processNode: function (node, fv) {
  79. if (node.tag === 'searchpanel') {
  80. if (!config.device.isMobile) {
  81. this._processSearchPanelNode(node, fv);
  82. }
  83. return false;
  84. }
  85. return this._super.apply(this, arguments);
  86. },
  87. _processSearchPanelNode: function (node, fv) {
  88. var self = this;
  89. node.children.forEach(function (childNode, index) {
  90. if (childNode.tag !== 'field') {
  91. return;
  92. }
  93. if (childNode.attrs.invisible === "1") {
  94. return;
  95. }
  96. var fieldName = childNode.attrs.name;
  97. var type = childNode.attrs.select === 'multi' ? 'filter' : 'category';
  98. var sectionId = _.uniqueId('section_');
  99. var section = {
  100. color: childNode.attrs.color,
  101. description: childNode.attrs.string || fv.fields[fieldName].string,
  102. fieldName: fieldName,
  103. icon: childNode.attrs.icon,
  104. id: sectionId,
  105. index: index,
  106. type: type,
  107. };
  108. if (section.type === 'category') {
  109. section.icon = section.icon || 'fa-folder';
  110. } else if (section.type === 'filter') {
  111. section.disableCounters = !!pyUtils.py_eval(childNode.attrs.disable_counters || '0');
  112. section.domain = childNode.attrs.domain || '[]';
  113. section.groupBy = childNode.attrs.groupby;
  114. section.icon = section.icon || 'fa-filter';
  115. }
  116. self.searchPanelSections[sectionId] = section;
  117. });
  118. },
  119. });
  120. });