summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/views/search_panel.js
blob: 715378471c2d31c42c2af7314b5ad4ee4d379be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
odoo.define("web/static/src/js/views/search_panel.js", function (require) {
    "use strict";

    const { Model, useModel } = require("web/static/src/js/model.js");
    const patchMixin = require("web.patchMixin");

    const { Component, hooks } = owl;
    const { useState, useSubEnv } = hooks;

    /**
     * Search panel
     *
     * Represent an extension of the search interface located on the left side of
     * the view. It is divided in sections defined in a "<searchpanel>" node located
     * inside of a "<search>" arch. Each section is represented by a list of different
     * values (categories or ungrouped filters) or groups of values (grouped filters).
     * Its state is directly affected by its model (@see SearchPanelModelExtension).
     * @extends Component
     */
    class SearchPanel extends Component {
        constructor() {
            super(...arguments);

            useSubEnv({ searchModel: this.props.searchModel });

            this.state = useState({
                active: {},
                expanded: {},
            });
            this.model = useModel("searchModel");
            this.scrollTop = 0;
            this.hasImportedState = false;

            this.importState(this.props.importedState);
        }

        async willStart() {
            this._expandDefaultValue();
            this._updateActiveValues();
        }

        mounted() {
            this._updateGroupHeadersChecked();
            if (this.hasImportedState) {
                this.el.scroll({ top: this.scrollTop });
            }
        }

        async willUpdateProps() {
            this._updateActiveValues();
        }

        //---------------------------------------------------------------------
        // Public
        //---------------------------------------------------------------------

        exportState() {
            const exported = {
                expanded: this.state.expanded,
                scrollTop: this.el.scrollTop,
            };
            return JSON.stringify(exported);
        }

        importState(stringifiedState) {
            this.hasImportedState = Boolean(stringifiedState);
            if (this.hasImportedState) {
                const state = JSON.parse(stringifiedState);
                this.state.expanded = state.expanded;
                this.scrollTop = state.scrollTop;
            }
        }

        //---------------------------------------------------------------------
        // Private
        //---------------------------------------------------------------------

        /**
         * Expands category values holding the default value of a category.
         * @private
         */
        _expandDefaultValue() {
            if (this.hasImportedState) {
                return;
            }
            const categories = this.model.get("sections", s => s.type === "category");
            for (const category of categories) {
                this.state.expanded[category.id] = {};
                if (category.activeValueId) {
                    const ancestorIds = this._getAncestorValueIds(category, category.activeValueId);
                    for (const ancestorId of ancestorIds) {
                        this.state.expanded[category.id][ancestorId] = true;
                    }
                }
            }
        }

        /**
         * @private
         * @param {Object} category
         * @param {number} categoryValueId
         * @returns {number[]} list of ids of the ancestors of the given value in
         *   the given category.
         */
        _getAncestorValueIds(category, categoryValueId) {
            const { parentId } = category.values.get(categoryValueId);
            return parentId ? [...this._getAncestorValueIds(category, parentId), parentId] : [];
        }

        /**
         * Prevent unnecessary calls to the model by ensuring a different category
         * is clicked.
         * @private
         * @param {Object} category
         * @param {Object} value
         */
        async _toggleCategory(category, value) {
            if (value.childrenIds.length) {
                const categoryState = this.state.expanded[category.id];
                if (categoryState[value.id] && category.activeValueId === value.id) {
                    delete categoryState[value.id];
                } else {
                    categoryState[value.id] = true;
                }
            }
            if (category.activeValueId !== value.id) {
                this.state.active[category.id] = value.id;
                this.model.dispatch("toggleCategoryValue", category.id, value.id);
            }
        }

        /**
         * @private
         * @param {number} filterId
         * @param {{ values: Map<Object> }} group
         */
        _toggleFilterGroup(filterId, { values }) {
            const valueIds = [];
            const checked = [...values.values()].every(
                (value) => this.state.active[filterId][value.id]
            );
            values.forEach(({ id }) => {
                valueIds.push(id);
                this.state.active[filterId][id] = !checked;
            });
            this.model.dispatch("toggleFilterValues", filterId, valueIds, !checked);
        }

        /**
         * @private
         * @param {number} filterId
         * @param {Object} [group]
         * @param {number} valueId
         * @param {MouseEvent} ev
         */
        _toggleFilterValue(filterId, valueId, { currentTarget }) {
            this.state.active[filterId][valueId] = currentTarget.checked;
            this._updateGroupHeadersChecked();
            this.model.dispatch("toggleFilterValues", filterId, [valueId]);
        }

        _updateActiveValues() {
            for (const section of this.model.get("sections")) {
                if (section.type === "category") {
                    this.state.active[section.id] = section.activeValueId;
                } else {
                    this.state.active[section.id] = {};
                    if (section.groups) {
                        for (const group of section.groups.values()) {
                            for (const value of group.values.values()) {
                                this.state.active[section.id][value.id] = value.checked;
                            }
                        }
                    }
                    if (section && section.values) {
                        for (const value of section.values.values()) {
                            this.state.active[section.id][value.id] = value.checked;
                        }
                    }
                }
            }
        }

        /**
         * Updates the "checked" or "indeterminate" state of each of the group
         * headers according to the state of their values.
         * @private
         */
        _updateGroupHeadersChecked() {
            const groups = this.el.querySelectorAll(":scope .o_search_panel_filter_group");
            for (const group of groups) {
                const header = group.querySelector(":scope .o_search_panel_group_header input");
                const vals = [...group.querySelectorAll(":scope .o_search_panel_filter_value input")];
                header.checked = false;
                header.indeterminate = false;
                if (vals.every((v) => v.checked)) {
                    header.checked = true;
                } else if (vals.some((v) => v.checked)) {
                    header.indeterminate = true;
                }
            }
        }
    }
    SearchPanel.modelExtension = "SearchPanel";

    SearchPanel.props = {
        className: { type: String, optional: 1 },
        importedState: { type: String, optional: 1 },
        searchModel: Model,
    };
    SearchPanel.template = "web.SearchPanel";

    return patchMixin(SearchPanel);
});