<commit_before>///<reference path="../typings/react/react.d.ts"/>
///<reference path="../typings/react/react-dom.d.ts"/>
///<reference path="../typings/lodash/lodash.d.ts"/>
///<reference path="../typings/react-vendor-prefix/react-vendor-prefix.d.ts"/>
///<reference path="../typings/react-bootstrap/react-bootstrap.d.ts"/>

import * as _ from "lodash";
import * as React from "react";
import * as ReactDOM from "react-dom";
import ui from "./react-ui/ui";
import * as VendorPrefix from "react-vendor-prefix";
import StandardLib from "./utils/StandardLib";
import {Glyphicon} from "react-bootstrap";
import {CSSProperties} from "react";
import {IVisTool, IVisToolProps, IVisToolState} from "./tools/IVisTool";
import ToolTip from "./tools/tooltip";
import {IToolTipProps, IToolTipState} from "./tools/tooltip";

const toolRegistry:{[name:string]: Function} = {};

declare type IToolTip = React.Component<IToolTipProps, IToolTipState>;

const grabberStyle:CSSProperties = {
    width: "16",
    height: "16",
    cursor: "move",
    background: "url(http://placehold.it/32x32)"
};

export function registerToolImplementation(asClassName:string, jsClass:Function) {
    toolRegistry[asClassName] = jsClass;
}

export function getToolImplementation(asClassName:string):Function {
    return toolRegistry[asClassName];
}

interface IWeaveToolProps extends React.Props<WeaveTool> {
    toolPath:WeavePath;
    toolClass?:string;
    style:CSSProperties;
    onDragStart:React.MouseEvent;
    onDragEnd:React.MouseEvent;
    onDragOver:React.MouseEvent;
}

interface IWeaveToolState {

}

export class WeaveTool extends React.Component<IWeaveToolProps, IWeaveToolState> {

    private toolPath:WeavePath;
    private ToolClass:any;
    private tool:IVisTool;
    private toolWidth:number;
    private toolHeight:number;
    private toolTip:IToolTip;
    private titleBarHeight: number;
    private titleBar:React.Component<ITitleBarProps, ITitleBarState>;

    constructor(props:IWeaveToolProps) {
        super(props);
        this.toolPath = this.props.toolPath;
        var toolType:string = this.toolPath ? this.toolPath.getType() : this.props.toolClass;
        if(toolType === "weave.visualization.tools::ExternalTool" && this.toolPath.getType("toolClass")) {
            toolType = this.toolPath.getState("toolClass");
        }
        if(toolType === "weavejs.core.LinkableHashMap" && this.toolPath.getType("class"))
            toolType = this.toolPath.getState("class");
        this.ToolClass = getToolImplementation(toolType);
        this.titleBarHeight = 25;
    }

    componentDidMount():void {
        // if(this.toolPath) {
        //     this.toolPath.addCallback(this, this.forceUpdate);
        // }
    }

    get title():string {
        return this.tool ? this.tool.title : "";
    }

    render() {
        var toolHeight:number = this.props.style ? this.props.style.height - this.titleBarHeight : 320;
        var toolWidth:number = this.props.style ? this.props.style.width : 320;

        //TODO - we shouldn't have to render twice to set the tooltip of the tool
        var reactTool:any;
        if (React.Component.isPrototypeOf(this.ToolClass)) {
            reactTool = React.createElement(this.ToolClass, {
                                key: "tool",
                                ref: (c:IVisTool) => { this.tool = c; },
                                toolPath: this.toolPath,
                                style: { height: toolHeight, width: toolWidth },
                                toolTip: this.toolTip
                            }
                        );
        }

        return (
            <ui.VBox style={this.props.style} onMouseEnter={() => { this.titleBar.setState({ showControls: true }); }} onMouseLeave={() => { this.titleBar.setState({ showControls: false }); }} onDragOver={this.props.onDragOver} onDragEnd={this.props.onDragEnd}>
                <TitleBar ref={(c:React.Component<ITitleBarProps, ITitleBarState>) => { this.titleBar = c; } }
                          onDragStart={this.props.onDragStart}
                          titleBarHeight={this.titleBarHeight}
                          title={this.title}
                          />
                {
                    <div style={{width: toolWidth, height: toolHeight}}>
                        <div style={{width: "100%", height: "100%", maxHeight: "100%"}}>
                            {<commit_msg>hide tooltip on mouse leaving the tool<commit_after>///<reference path="../typings/react/react.d.ts"/>
///<reference path="../typings/react/react-dom.d.ts"/>
///<reference path="../typings/lodash/lodash.d.ts"/>
///<reference path="../typings/react-vendor-prefix/react-vendor-prefix.d.ts"/>
///<reference path="../typings/react-bootstrap/react-bootstrap.d.ts"/>

import * as _ from "lodash";
import * as React from "react";
import * as ReactDOM from "react-dom";
import ui from "./react-ui/ui";
import * as VendorPrefix from "react-vendor-prefix";
import StandardLib from "./utils/StandardLib";
import {Glyphicon} from "react-bootstrap";
import {CSSProperties} from "react";
import {IVisTool, IVisToolProps, IVisToolState} from "./tools/IVisTool";
import ToolTip from "./tools/tooltip";
import {IToolTipProps, IToolTipState} from "./tools/tooltip";

const toolRegistry:{[name:string]: Function} = {};

declare type IToolTip = React.Component<IToolTipProps, IToolTipState>;

const grabberStyle:CSSProperties = {
    width: "16",
    height: "16",
    cursor: "move",
    background: "url(http://placehold.it/32x32)"
};

export function registerToolImplementation(asClassName:string, jsClass:Function) {
    toolRegistry[asClassName] = jsClass;
}

export function getToolImplementation(asClassName:string):Function {
    return toolRegistry[asClassName];
}

interface IWeaveToolProps extends React.Props<WeaveTool> {
    toolPath:WeavePath;
    toolClass?:string;
    style:CSSProperties;
    onDragStart:React.MouseEvent;
    onDragEnd:React.MouseEvent;
    onDragOver:React.MouseEvent;
}

interface IWeaveToolState {

}

export class WeaveTool extends React.Component<IWeaveToolProps, IWeaveToolState> {

    private toolPath:WeavePath;
    private ToolClass:any;
    private tool:IVisTool;
    private toolWidth:number;
    private toolHeight:number;
    private toolTip:IToolTip;
    private titleBarHeight: number;
    private titleBar:React.Component<ITitleBarProps, ITitleBarState>;

    constructor(props:IWeaveToolProps) {
        super(props);
        this.toolPath = this.props.toolPath;
        var toolType:string = this.toolPath ? this.toolPath.getType() : this.props.toolClass;
        if(toolType === "weave.visualization.tools::ExternalTool" && this.toolPath.getType("toolClass")) {
            toolType = this.toolPath.getState("toolClass");
        }
        if(toolType === "weavejs.core.LinkableHashMap" && this.toolPath.getType("class"))
            toolType = this.toolPath.getState("class");
        this.ToolClass = getToolImplementation(toolType);
        this.titleBarHeight = 25;
    }

    componentDidMount():void {
        // if(this.toolPath) {
        //     this.toolPath.addCallback(this, this.forceUpdate);
        // }
    }

    get title():string {
        return this.tool ? this.tool.title : "";
    }

    render() {
        var toolHeight:number = this.props.style ? this.props.style.height - this.titleBarHeight : 320;
        var toolWidth:number = this.props.style ? this.props.style.width : 320;

        //TODO - we shouldn't have to render twice to set the tooltip of the tool
        var reactTool:any;
        if (React.Component.isPrototypeOf(this.ToolClass)) {
            reactTool = React.createElement(this.ToolClass, {
                                key: "tool",
                                ref: (c:IVisTool) => { this.tool = c; },
                                toolPath: this.toolPath,
                                style: { height: toolHeight, width: toolWidth },
                                toolTip: this.toolTip
                            }
                        );
        }

        return (
            <ui.VBox style={this.props.style}
                    onMouseEnter={() => { this.titleBar.setState({ showControls: true }); }}
                    onMouseLeave={() => { this.titleBar.setState({ showControls: false }); this.toolTip.setState({ showToolTip: false }); }}
                    onDragOver={this.props.onDragOver}
                    onDragEnd={this.props.onDragEnd}>
                <TitleBar ref={(c:React.Component<ITitleBarProps, ITitleBarState>) => { this.titleBar = c; } }
                          onDragStart={this.props.onDragStart}
                          titleBarHeight={this.titleBarHeight}
                          title={this.title}
                          />
                {
                    <div style={{width: toolWidth, height: toolHeight}}>
                        <div style={{width: "100%", height: "100%", maxHeight: "100%"}}>
                            {
{'fraction_non_alphanumeric': 0.13174850033325927, 'fraction_numerical': 0.00555432126194179, 'mean_word_length': 2.4963106796116503, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '615902', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2571, 'n_words': 483}