Skip to Content
systray data_display

Switch Company Menu

Odoo 19 systray — Switch Company Menu (webclient)

Live preview Interactive
Source excerpt web/static/src/webclient/switch_company_menu/switch_company_menu.js
import { Dropdown } from "@web/core/dropdown/dropdown";
import { DropdownGroup } from "@web/core/dropdown/dropdown_group";
import { DropdownItem } from "@web/core/dropdown/dropdown_item";
import { registry } from "@web/core/registry";

import { Component, useChildSubEnv, useRef, useState } from "@odoo/owl";
import { useCommand } from "@web/core/commands/command_hook";
import { _t } from "@web/core/l10n/translation";
import { symmetricalDifference } from "@web/core/utils/arrays";
import { useBus, useChildRef, useService } from "@web/core/utils/hooks";
import { SwitchCompanyItem } from "@web/webclient/switch_company_menu/switch_company_item";
import { useHotkey } from "@web/core/hotkeys/hotkey_hook";
import { useDropdownState } from "@web/core/dropdown/dropdown_hooks";
import { user, userBus } from "@web/core/user";
import { router } from "@web/core/browser/router";

function getCompany(cid) {
    return user.allowedCompaniesWithAncestors.find((c) => c.id === cid);
}

export class CompanySelector {
    constructor(actionService, dropdownState) {
        this.actionService = actionService;
        this.dropdownState = dropdownState;
        this.selectedCompaniesIds = user.activeCompanies.map((c) => c.id);
    }

    get hasSelectionChanged() {
        return (
            symmetricalDifference(
                this.selectedCompaniesIds,
                user.activeCompanies.map((c) => c.id)
            ).length > 0
        );
    }

    isCompanySelected(companyId) {
        return this.selectedCompaniesIds.includes(companyId);
    }

    switchCompany(mode, companyId) {
        if (mode === "toggle") {
            if (this.selectedCompaniesIds.includes(companyId)) {
                this._deselectCompany(companyId);
            } else {
                this._selectCompany(companyId);
            }
        } else if (mode === "loginto") {
            if (this._isSingleCompanyMode()) {
                this.selectedCompaniesIds.splice(0, this.selectedCompaniesIds.length);
            }
            this._selectCompany(companyId, true);
            this.apply();

            this.dropdownState.close?.();
        }
    }

    async apply() {
        user.activateCompanies(this.selectedCompaniesIds, {
            includeChildCompanies: false,
            reload: false,
        });

        const controller = this.actionService.currentController;
        const state = {};
        const options = { reload: true };
        if (controller?.props.resId && controller?.props.resModel) {
            const hasReadRights = await user.checkAccessRight(
                controller.props.resModel,
                "read",
                controller.props.resId
            );

            if (!hasReadRights) {
                options.replace = true;
                state.actionStack = router.current.actionStack.slice(0, -1);
            }
        }

        router.pushState(state, options);
    }

    reset() {
        this.selectedCompaniesIds = user.activeCompanies.map((c) => c.id);
    }

    selectAll(companyIds) {
        let shouldSelectAll = true;

        // If any company is selected, just unselect all
        for (let i = this.selectedCompaniesIds.length - 1; i >= 0; i--) {
            if (companyIds.includes(this.selectedCompaniesIds[i])) {
                this.selectedCompaniesIds.splice(i, 1);
                shouldSelectAll = false;
            }
        }

        // If no company is selected, select all
        if (shouldSelectAll) {
            for (const companyId of companyIds) {
                if (!this.selectedCompaniesIds.includes(companyId)) {
                    this.selectedCompaniesIds.push(companyId);
                }
            }
        }
    }

    _selectCompany(companyId, unshift = false) {
        if (this._isCompanyAllowed(companyId)) {
            if (!this.selectedCompaniesIds.includes(companyId)) {
                if (unshift) {
                    this.selectedCompaniesIds.unshift(companyId);
                } else {
                    this.selectedCompaniesIds.push(companyId);
                }
            } else if (unshift) {
                const index = this.selectedCompaniesIds.findIndex((c) => c === companyId);
                this.selectedCompaniesIds.splice(index, 1);
                this.selectedCompaniesIds.unshift(companyId);
Registry / API
Registry name
SwitchCompanyMenu
Category
systray
Module
web
Slug
switchcompanymenu
Nav group
data_display