<commit_before>/*
 * Copyright 2015-2016 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {html, LitElement} from "/web_modules/lit-element.js";
import UtilsNew from "../../../utilsNew.js";
import "../simple-chart.js";
import "../json-viewer.js";
import "../../tree-viewer.js";
import "../../download-button.js";
import "../../commons/filters/text-field-filter.js";
import "../../../form/controls/toggle-switch.js";
import "../../../form/controls/toggle-buttons.js";

export default class DataForm extends LitElement {

    constructor() {
        super();

        this._init();
    }

    createRenderRoot() {
        return this;
    }

    static get properties() {
        return {
            data: {
                type: Object
            },
            mode: {
                type: String
            },
            config: {
                type: Object
            }
        };
    }

    _init() {
        this._prefix = UtilsNew.randomString(8);
        //this.data = {};
    }

    firstUpdated(_changedProperties) {
        $("#" + this._prefix + "DuePickerDate").datetimepicker({
            format: "DD/MM/YYYY"
        });
        $("#" + this._prefix + "DuePickerDate").on("dp.change", e => {
            this.onFilterChange(e.currentTarget.dataset.field, e.date.format("YYYYMMDDHHmmss"));
        });
    }

    updated(changedProperties) {
        if (changedProperties.has("data")) {
            // this.requestUpdate();
        }
    }

    getValue(field, object, defaultValue, format) {
        let value = object;
        if (field) {
            const _object = object ? object : this.data;
            // optional chaining is needed when "res" is undefined
            value = field.split(".").reduce((res, prop) => res?.[prop], _object);

            // needed for handling falsy values
            if (value !== undefined && value !== "") {
                if (format) {
                    if (format.classes || format.style) {
                        value = html`<span class="${format.classes}" style="${format.style}">${value}</span>`;
                    }
                    if (format.link) {
                        value = html`<a href="${format.link.replace(field.toUpperCase(), value)}" target="_blank">${value}</a>`;
                    }<commit_msg>wc: make data-form to init data object in case is undefined or null<commit_after>/*
 * Copyright 2015-2016 OpenCB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {html, LitElement} from "/web_modules/lit-element.js";
import UtilsNew from "../../../utilsNew.js";
import "../simple-chart.js";
import "../json-viewer.js";
import "../../tree-viewer.js";
import "../../download-button.js";
import "../../commons/filters/text-field-filter.js";
import "../../../form/controls/toggle-switch.js";
import "../../../form/controls/toggle-buttons.js";

export default class DataForm extends LitElement {

    constructor() {
        super();

        this._init();
    }

    createRenderRoot() {
        return this;
    }

    static get properties() {
        return {
            data: {
                type: Object
            },
            mode: {
                type: String
            },
            config: {
                type: Object
            }
        };
    }

    _init() {
        this._prefix = UtilsNew.randomString(8);

        // We need to initialise 'data' in case undefined value is passed
        this.data = {};
    }

    firstUpdated(_changedProperties) {
        $("#" + this._prefix + "DuePickerDate").datetimepicker({
            format: "DD/MM/YYYY"
        });
        $("#" + this._prefix + "DuePickerDate").on("dp.change", e => {
            this.onFilterChange(e.currentTarget.dataset.field, e.date.format("YYYYMMDDHHmmss"));
        });
    }

    update(changedProperties) {
        if (changedProperties.has("data")) {
            // Check if 'data' passed is undefined or null and initialised to empty object
            this.data = this.data ?? {};
        }
        super.update(changedProperties);
    }

    getValue(field, object, defaultValue, format) {
        let value = object;
        if (field) {
            const _object = object ? object : this.data;
            // optional chaining is needed when "res" is undefined
            value = field.split(".").reduce((res, prop) => res?.[prop], _object);

            // needed for handling falsy values
            if (value !== undefined && value !== "") {
                if (format) {
                    if (format.classes || format.style) {
                        value = html`<span class="${format.classes}" style="${format.style}">${value}</span>`;
                    }
                    if (format.link) {
                        value = html`<a href="${format.link.replace(field.toUpperCase(), value)}" target="_blank">${value}</a>`;
                    }
{'fraction_non_alphanumeric': 0.14319566689234936, 'fraction_numerical': 0.004400812457684496, 'mean_word_length': 1.9383391347588264, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4146088', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1537, 'n_words': 438}