summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/control_panel/groupby_menu.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/web/static/src/js/control_panel/groupby_menu.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/web/static/src/js/control_panel/groupby_menu.js')
-rw-r--r--addons/web/static/src/js/control_panel/groupby_menu.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/addons/web/static/src/js/control_panel/groupby_menu.js b/addons/web/static/src/js/control_panel/groupby_menu.js
new file mode 100644
index 00000000..546952ad
--- /dev/null
+++ b/addons/web/static/src/js/control_panel/groupby_menu.js
@@ -0,0 +1,98 @@
+odoo.define('web.GroupByMenu', function (require) {
+ "use strict";
+
+ const CustomGroupByItem = require('web.CustomGroupByItem');
+ const DropdownMenu = require('web.DropdownMenu');
+ const { FACET_ICONS, GROUPABLE_TYPES } = require('web.searchUtils');
+ const { useModel } = require('web/static/src/js/model.js');
+
+ /**
+ * 'Group by' menu
+ *
+ * Simple rendering of the filters of type `groupBy` given by the control panel
+ * model. It uses most of the behaviours implemented by the dropdown menu Component,
+ * with the addition of a groupBy filter generator (@see CustomGroupByItem).
+ * @see DropdownMenu for additional details.
+ * @extends DropdownMenu
+ */
+ class GroupByMenu extends DropdownMenu {
+
+ constructor() {
+ super(...arguments);
+
+ this.fields = Object.values(this.props.fields)
+ .filter(field => this._validateField(field))
+ .sort(({ string: a }, { string: b }) => a > b ? 1 : a < b ? -1 : 0);
+
+ this.model = useModel('searchModel');
+ }
+
+ //---------------------------------------------------------------------
+ // Getters
+ //---------------------------------------------------------------------
+
+ /**
+ * @override
+ */
+ get icon() {
+ return FACET_ICONS.groupBy;
+ }
+
+ /**
+ * @override
+ */
+ get items() {
+ return this.model.get('filters', f => f.type === 'groupBy');
+ }
+
+ /**
+ * @override
+ */
+ get title() {
+ return this.env._t("Group By");
+ }
+
+ //---------------------------------------------------------------------
+ // Private
+ //---------------------------------------------------------------------
+
+ /**
+ * @private
+ * @param {Object} field
+ * @returns {boolean}
+ */
+ _validateField(field) {
+ return field.sortable &&
+ field.name !== "id" &&
+ GROUPABLE_TYPES.includes(field.type);
+ }
+
+ //---------------------------------------------------------------------
+ // Handlers
+ //---------------------------------------------------------------------
+
+ /**
+ * @private
+ * @param {OwlEvent} ev
+ */
+ _onItemSelected(ev) {
+ ev.stopPropagation();
+ const { item, option } = ev.detail;
+ if (option) {
+ this.model.dispatch('toggleFilterWithOptions', item.id, option.id);
+ } else {
+ this.model.dispatch('toggleFilter', item.id);
+ }
+ }
+ }
+
+ GroupByMenu.components = Object.assign({}, DropdownMenu.components, {
+ CustomGroupByItem,
+ });
+ GroupByMenu.props = Object.assign({}, DropdownMenu.props, {
+ fields: Object,
+ });
+ GroupByMenu.template = 'web.GroupByMenu';
+
+ return GroupByMenu;
+});