| 1 |
import { Dropdown } from "@web/core/dropdown/dropdown"; |
| 2 |
import { useBus } from "@web/core/utils/hooks"; |
| 3 |
|
| 4 |
import { |
| 5 |
Component, |
| 6 |
onMounted, |
| 7 |
onWillStart, |
| 8 |
onWillUpdateProps, |
| 9 |
reactive, |
| 10 |
useEffect, |
| 11 |
useRef, |
| 12 |
useState, |
| 13 |
} from "@odoo/owl"; |
| 14 |
import { browser } from "@web/core/browser/browser"; |
| 15 |
import { exprToBoolean } from "@web/core/utils/strings"; |
| 16 |
import { useSetupAction } from "@web/search/action_hook"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const isFilter = (s) => s.type === "filter"; |
| 23 |
const isActiveCategory = (s) => s.type === "category" && s.activeValueId; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
const nameOfCheckedValues = (values) => { |
| 30 |
const names = []; |
| 31 |
for (const [, value] of values) { |
| 32 |
if (value.checked) { |
| 33 |
names.push(value.display_name); |
| 34 |
} |
| 35 |
} |
| 36 |
return names; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
export class SearchPanel extends Component { |
| 49 |
static template = "web.SearchPanel"; |
| 50 |
static props = {}; |
| 51 |
static components = { |
| 52 |
Dropdown, |
| 53 |
}; |
| 54 |
static subTemplates = { |
| 55 |
section: "web.SearchPanel.Section", |
| 56 |
category: "web.SearchPanel.Category", |
| 57 |
filtersGroup: "web.SearchPanel.FiltersGroup", |
| 58 |
}; |
| 59 |
|
| 60 |
setup() { |
| 61 |
this.keyExpandSidebar = `search_panel_expanded,${this.env.config.viewId},${this.env.config.actionId}`; |
| 62 |
this.state = useState({ |
| 63 |
active: {}, |
| 64 |
expanded: {}, |
| 65 |
sidebarExpanded: true, |
| 66 |
}); |
| 67 |
this.hasImportedState = false; |
| 68 |
this.root = useRef("root"); |
| 69 |
this.scrollTop = 0; |
| 70 |
this.dropdownStates = {}; |
| 71 |
this.width = "10px"; |
| 72 |
|
| 73 |
this.importState(this.env.searchPanelState); |
| 74 |
const sidebarExpandedPreference = browser.localStorage.getItem(this.keyExpandSidebar); |
| 75 |
if (sidebarExpandedPreference !== null) { |
| 76 |
this.state.sidebarExpanded = exprToBoolean(sidebarExpandedPreference); |
| 77 |
} |
| 78 |
|
| 79 |
useBus(this.env.searchModel, "update", async () => { |
| 80 |
await this.env.searchModel.sectionsPromise; |
| 81 |
this.updateActiveValues(); |
| 82 |
this.render(); |
| 83 |
}); |
| 84 |
|
| 85 |
useEffect( |
| 86 |
(el) => { |
| 87 |
if (el && this.hasImportedState) { |
| 88 |
el.style["min-width"] = this.width; |
| 89 |
el.scroll({ top: this.scrollTop }); |
| 90 |
} |
| 91 |
}, |
| 92 |
() => [this.root.el] |
| 93 |
); |
| 94 |
|
| 95 |
useSetupAction({ |
| 96 |
getGlobalState: () => ({ |
| 97 |
searchPanel: this.exportState(), |
| 98 |
}), |
| 99 |
}); |
| 100 |
|
| 101 |
onWillStart(async () => { |
| 102 |
await this.env.searchModel.sectionsPromise; |
| 103 |
this.expandDefaultValue(); |
| 104 |
this.expandValues(); |
| 105 |
this.updateActiveValues(); |
| 106 |
}); |
| 107 |
|
| 108 |
onWillUpdateProps(async () => { |
| 109 |
await this.env.searchModel.sectionsPromise; |
| 110 |
this.updateActiveValues(); |
| 111 |
}); |
| 112 |
|
| 113 |
onMounted(() => { |
| 114 |
this.updateGroupHeadersChecked(); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
get sections() { |
| 123 |
return this.env.searchModel.getSections((s) => !s.empty); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
exportState() { |
| 131 |
const exported = { |
| 132 |
expanded: this.state.expanded, |
| 133 |
scrollTop: this.root.el?.scrollTop || 0, |
| 134 |
sidebarExpanded: this.state.sidebarExpanded, |
| 135 |
width: this.width, |
| 136 |
}; |
| 137 |
return JSON.stringify(exported); |
| 138 |
} |
| 139 |
|
| 140 |
importState(state) { |
| 141 |
this.hasImportedState = Boolean(state); |
| 142 |
if (this.hasImportedState) { |
| 143 |
this.state.expanded = state.expanded; |
| 144 |
this.scrollTop = state.scrollTop; |
| 145 |
this.state.sidebarExpanded = state.sidebarExpanded; |
| 146 |
this.width = state.width; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
getDropdownState(sectionId) { |
| 155 |
if (!this.dropdownStates[sectionId]) { |
| 156 |
const state = reactive({ |
| 157 |
isOpen: false, |
| 158 |
open: () => (state.isOpen = true), |
| 159 |
close: () => (state.isOpen = false), |
| 160 |
}); |
| 161 |
this.dropdownStates[sectionId] = reactive(state); |
| 162 |
} |
| 163 |
return this.dropdownStates[sectionId]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
expandDefaultValue() { |
| 170 |
if (this.hasImportedState) { |
| 171 |
return; |
| 172 |
} |
| 173 |
const categories = this.env.searchModel.getSections((s) => s.type === "category"); |
| 174 |
for (const category of categories) { |
| 175 |
this.state.expanded[category.id] = {}; |
| 176 |
if (category.activeValueId) { |
| 177 |
const ancestorIds = this.getAncestorValueIds(category, category.activeValueId); |
| 178 |
for (const ancestorId of ancestorIds) { |
| 179 |
this.state.expanded[category.id][ancestorId] = true; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
expandValues() { |
| 186 |
if (this.hasImportedState) { |
| 187 |
return; |
| 188 |
} |
| 189 |
const categories = this.env.searchModel.getSections((s) => s.type === "category"); |
| 190 |
for (const category of categories) { |
| 191 |
if (category.depth === 0) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
this.state.expanded[category.id] ||= {}; |
| 196 |
const expand = (id, level) => { |
| 197 |
if (!level) { |
| 198 |
return; |
| 199 |
} |
| 200 |
this.state.expanded[category.id][id] = true; |
| 201 |
const { childrenIds } = category.values.get(id); |
| 202 |
level -= 1; |
| 203 |
for (const childId of childrenIds) { |
| 204 |
expand(childId, level); |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
for (const rootId of category.rootIds) { |
| 209 |
expand(rootId, category.depth); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
getAncestorValueIds(category, categoryValueId) { |
| 221 |
const { parentId } = category.values.get(categoryValueId); |
| 222 |
return parentId ? [...this.getAncestorValueIds(category, parentId), parentId] : []; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
getCategorySelection() { |
| 231 |
const activeCategories = this.env.searchModel.getSections(isActiveCategory); |
| 232 |
const selection = []; |
| 233 |
for (const category of activeCategories) { |
| 234 |
const parentIds = this.getAncestorValueIds(category, category.activeValueId); |
| 235 |
const orderedCategoryNames = [...parentIds, category.activeValueId].map( |
| 236 |
(valueId) => category.values.get(valueId).display_name |
| 237 |
); |
| 238 |
selection.push({ |
| 239 |
values: orderedCategoryNames, |
| 240 |
icon: category.icon, |
| 241 |
color: category.color, |
| 242 |
}); |
| 243 |
} |
| 244 |
return selection; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
getFilterSelection() { |
| 253 |
const filters = this.env.searchModel.getSections(isFilter); |
| 254 |
const selection = []; |
| 255 |
for (const { groups, values, icon, color } of filters) { |
| 256 |
let filterValues; |
| 257 |
if (groups) { |
| 258 |
filterValues = Object.keys(groups) |
| 259 |
.map((groupId) => nameOfCheckedValues(groups[groupId].values)) |
| 260 |
.flat(); |
| 261 |
} else if (values) { |
| 262 |
filterValues = nameOfCheckedValues(values); |
| 263 |
} |
| 264 |
if (filterValues.length) { |
| 265 |
selection.push({ values: filterValues, icon, color }); |
| 266 |
} |
| 267 |
} |
| 268 |
return selection; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
hasSelection(sectionId = 0) { |
| 277 |
if (sectionId) { |
| 278 |
const sectionState = this.state.active[sectionId]; |
| 279 |
if (sectionState instanceof Object) { |
| 280 |
return Object.values(sectionState).some((val) => val); |
| 281 |
} |
| 282 |
return Boolean(sectionState); |
| 283 |
} |
| 284 |
return Object.keys(this.state.active).some((key) => this.hasSelection(key)); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
clearSelection(sectionId = 0) { |
| 293 |
const sectionIds = sectionId ? [sectionId] : Object.keys(this.state.active).map(Number); |
| 294 |
this.env.searchModel.clearSections(sectionIds); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
async toggleCategory(category, value) { |
| 304 |
if (value.childrenIds.length) { |
| 305 |
const categoryState = this.state.expanded[category.id]; |
| 306 |
if (categoryState[value.id] && category.activeValueId === value.id) { |
| 307 |
delete categoryState[value.id]; |
| 308 |
} else { |
| 309 |
categoryState[value.id] = true; |
| 310 |
} |
| 311 |
} else { |
| 312 |
this.getDropdownState(category.id).close(); |
| 313 |
} |
| 314 |
if (category.activeValueId !== value.id) { |
| 315 |
this.env.searchModel.toggleCategoryValue(category.id, value.id); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
toggleSidebar() { |
| 320 |
this.state.sidebarExpanded = !this.state.sidebarExpanded; |
| 321 |
browser.localStorage.setItem(this.keyExpandSidebar, this.state.sidebarExpanded); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
toggleFilterGroup(filterId, { values }) { |
| 329 |
const valueIds = []; |
| 330 |
const checked = [...values.values()].every( |
| 331 |
(value) => this.state.active[filterId][value.id] |
| 332 |
); |
| 333 |
values.forEach(({ id }) => { |
| 334 |
valueIds.push(id); |
| 335 |
this.state.active[filterId][id] = !checked; |
| 336 |
}); |
| 337 |
this.env.searchModel.toggleFilterValues(filterId, valueIds, !checked); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
toggleFilterValue(filterId, valueId, { currentTarget }) { |
| 347 |
this.state.active[filterId][valueId] = currentTarget.checked; |
| 348 |
this.updateGroupHeadersChecked(); |
| 349 |
this.env.searchModel.toggleFilterValues(filterId, [valueId]); |
| 350 |
} |
| 351 |
|
| 352 |
updateActiveValues() { |
| 353 |
if (this.sections.length === 0) { |
| 354 |
this.state.sidebarExpanded = false; |
| 355 |
} |
| 356 |
for (const section of this.sections) { |
| 357 |
if (section.type === "category") { |
| 358 |
this.state.active[section.id] = section.activeValueId; |
| 359 |
} else { |
| 360 |
this.state.active[section.id] = {}; |
| 361 |
if (section.groups) { |
| 362 |
for (const group of section.groups.values()) { |
| 363 |
for (const value of group.values.values()) { |
| 364 |
this.state.active[section.id][value.id] = value.checked; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
if (section && section.values) { |
| 369 |
for (const value of section.values.values()) { |
| 370 |
this.state.active[section.id][value.id] = value.checked; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
updateGroupHeadersChecked() { |
| 382 |
const groups = document.querySelectorAll(".o_search_panel_filter_group"); |
| 383 |
for (const group of groups) { |
| 384 |
const header = group.querySelector(":scope .o_search_panel_group_header input"); |
| 385 |
const vals = [...group.querySelectorAll(":scope .o_search_panel_filter_value input")]; |
| 386 |
header.checked = false; |
| 387 |
header.indeterminate = false; |
| 388 |
if (vals.every((v) => v.checked)) { |
| 389 |
header.checked = true; |
| 390 |
} else if (vals.some((v) => v.checked)) { |
| 391 |
header.indeterminate = true; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
_onStartResize(ev) { |
| 403 |
|
| 404 |
if (ev.button !== 0) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
const initialX = ev.pageX; |
| 409 |
const initialWidth = this.root.el.offsetWidth; |
| 410 |
const resizeStoppingEvents = ["keydown", "pointerdown", "pointerup"]; |
| 411 |
|
| 412 |
|
| 413 |
const resizePanel = (ev) => { |
| 414 |
ev.preventDefault(); |
| 415 |
ev.stopPropagation(); |
| 416 |
const maxWidth = Math.max(0.5 * window.innerWidth, initialWidth); |
| 417 |
const delta = ev.pageX - initialX; |
| 418 |
const newWidth = Math.min(maxWidth, Math.max(10, initialWidth + delta)); |
| 419 |
this.width = `${newWidth}px`; |
| 420 |
this.root.el.style["min-width"] = this.width; |
| 421 |
}; |
| 422 |
document.addEventListener("pointermove", resizePanel, true); |
| 423 |
|
| 424 |
|
| 425 |
const stopResize = (ev) => { |
| 426 |
|
| 427 |
|
| 428 |
if (ev.type === "pointerdown" && ev.button === 0) { |
| 429 |
return; |
| 430 |
} |
| 431 |
ev.preventDefault(); |
| 432 |
ev.stopPropagation(); |
| 433 |
|
| 434 |
document.removeEventListener("pointermove", resizePanel, true); |
| 435 |
resizeStoppingEvents.forEach((stoppingEvent) => { |
| 436 |
document.removeEventListener(stoppingEvent, stopResize, true); |
| 437 |
}); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
document.activeElement.blur(); |
| 442 |
}; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
resizeStoppingEvents.forEach((stoppingEvent) => { |
| 448 |
document.addEventListener(stoppingEvent, stopResize, true); |
| 449 |
}); |
| 450 |
} |
| 451 |
} |