{"size": 2774, "ext": "js", "max_stars_repo_path": "packages/perspective-viewer-datagrid/src/js/index.js", "max_stars_repo_name": "MisterAwesome23/perspective", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/perspective-viewer-datagrid/src/js/index.js", "max_issues_repo_name": "MisterAwesome23/perspective", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/perspective-viewer-datagrid/src/js/index.js", "max_forks_repo_name": "MisterAwesome23/perspective", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.6730769231, "max_line_length": 79, "alphanum_fraction": 0.6095890411}
/******************************************************************************
 *
 * Copyright (c) 2017, the Perspective Authors.
 *
 * This file is part of the Perspective library, distributed under the terms of
 * the Apache License 2.0.  The full license can be found in the LICENSE file.
 *
 */

import {registerPlugin} from "@finos/perspective-viewer/dist/esm/utils.js";
import {_start_profiling_loop} from "./utils";
import {DatagridViewModel} from "./datagrid";
import {VIEWER_MAP} from "./constants";
import MATERIAL_STYLE from "../less/material.less";

/**
 * Initializes a new datagrid renderer if needed, or returns an existing one
 * associated with a rendering `<div>` from a cache.
 *
 * @param {*} element
 * @param {*} div
 * @returns
 */
function get_or_create_datagrid(element, div) {
    let datagrid;
    if (!VIEWER_MAP.has(div)) {
        datagrid = document.createElement("perspective-datagrid");
        datagrid.appendChild(document.createElement("slot"));
        datagrid.set_element(element);
        datagrid.register_listeners();
        div.innerHTML = "";
        div.appendChild(datagrid);
        VIEWER_MAP.set(div, datagrid);
    } else {
        datagrid = VIEWER_MAP.get(div);
    }

    if (!datagrid.isConnected) {
        datagrid.clear();
        div.innerHTML = "";
        div.appendChild(datagrid);
    }

    return datagrid;
}

/**
 * <perspective-viewer> plugin.
 *
 * @class DatagridPlugin
 */
class DatagridPlugin {
    static name = "Datagrid";
    static selectMode = "toggle";
    static deselectMode = "pivots";

    static async update(div) {
        try {
            const datagrid = VIEWER_MAP.get(div);
            await datagrid.draw({invalid_viewport: true});
        } catch (e) {
            return;
        }
    }

    static async create(div, view) {
        const datagrid = get_or_create_datagrid(this, div);
        const options = await datagrid.set_view(view);
        await datagrid.draw(options);
    }

    static async resize() {
        if (this.view && VIEWER_MAP.has(this._datavis)) {
            const datagrid = VIEWER_MAP.get(this._datavis);
            datagrid.reset_size();
            await datagrid.draw({invalid_viewport: true});
        }
    }
}

/**
 * Appends the default tbale CSS to `<head>`, should be run once on module
 *  import.
 *
 */
function _register_global_styles() {
    const style = document.createElement("style");
    style.textContent = MATERIAL_STYLE;
    document.head.appendChild(style);
}

/******************************************************************************
 *
 * Main
 *
 */

window.customElements.define("perspective-datagrid", DatagridViewModel);

registerPlugin("datagrid", DatagridPlugin);

_start_profiling_loop();
_register_global_styles();
