Skip to content

EspoCRM – Open Source CRM Application

3.8 KB
Raw Blame
/************************************************************************
 * This file is part of EspoCRM.
 *
 * EspoCRM – Open Source CRM application.
 * Copyright (C) 2014-2026 EspoCRM, Inc.
 * Website: https://www.espocrm.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
 ************************************************************************/

/** @module views/fields/bool */

import BaseFieldView from 'views/fields/base';
import Select from 'ui/select';
import {BaseOptions as BaseOptions} from 'views/fields/base';
import {BaseParams as BaseParams} from 'views/fields/base';
import {BaseViewSchema} from 'views/fields/base';

export interface BoolParams extends BaseParams {}

export interface BoolOptions extends BaseOptions {}

/**
 * A boolean field (checkbox).
 */
export default class BoolFieldView<
    S extends BaseViewSchema = BaseViewSchema,
    O extends BoolOptions = BoolOptions,
    P extends BoolParams = BoolParams,
> extends BaseFieldView<S, O, P> {

    readonly type = 'bool'

    protected listTemplate = 'fields/bool/list'
    protected detailTemplate = 'fields/bool/detail'
    protected editTemplate = 'fields/bool/edit'
    protected searchTemplate = 'fields/bool/search'

    protected validations: BaseFieldView['validations'] = []
    initialSearchIsNotIdle = true

    data() {
        const data = super.data();

        data.valueIsSet = this.model.has(this.name);

        return data;
    }

    protected afterRender() {
        super.afterRender();

        if (this.mode === this.MODE_SEARCH && this.$element) {
            this.$element.on('change', () => {
                this.trigger('change');
            });

            Select.init(this.$element);
        }
    }

    fetch() {
        const element = this.mainInputElement as HTMLInputElement | null

        const value = element?.checked

        const data = {} as Record<string, any>;

        data[this.name] = value;

        return data;
    }

    fetchSearch() {
        const type = this.$element?.val();

        if (!type) {
            return null;
        }

        if (type === 'any') {
            return {
                type: 'or',
                value: [
                    {
                        type: 'isTrue',
                        attribute: this.name,
                    },
                    {
                        type: 'isFalse',
                        attribute: this.name,
                    },
                ],
                data: {
                    type: type,
                },
            };
        }

        return {
            type: type,
            data: {
                type: type,
            },
        };
    }

    protected getSearchType(): string {
        return this.getSearchParamsData().type ?? this.searchParams?.type ?? 'isTrue';
    }
}