<commit_before>/**
 * @author <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
 * @public
 */
/*global
 define, console
 */
/*jslint
 browser: true,
 white: true
 */
define([
    'jquery',
    'bluebird',
    'uuid',
    'kb/common/html',
    'kb/service/client/workspace',
    'kb/service/client/userAndJobState',
    'kb_dataview_easyTree',
    'kb/widget/legacy/authenticatedWidget'
],
    function ($, Promise, uuid, html,Workspace, UserAndJobState, EasyTree) {
        'use strict';
        $.KBWidget({
            name: 'kbaseTree',
            parent: 'kbaseAuthenticatedWidget',
            version: '0.0.1',
            options: {
                treeID: null,
                workspaceID: null,
                treeObjVer: null,
                jobID: null,
                token: null,
                width: 1045,
                height: 600
            },
            treeWsRef: null,
            pref: null,
            timer: null,
            init: function (options) {
                this._super(options);
                this.pref = uuid.v4();

                if (!this.options.treeID) {
                    this.renderError("No tree to render!");
                    return this;
                }

                if (!this.options.workspaceID) {
                    this.renderError("No workspace given!");
                    return this;
                }

                this.wsClient = new Workspace(this.runtime.getConfig('services.workspace.url'), {
                    token: this.runtime.service('session').getAuthToken()
                });
                this.$messagePane = $("<div/>")
                    .addClass("kbwidget-message-pane kbwidget-hide-message");
                this.$elem.append(this.$messagePane);

                this.render();

                return this;
            },
            render: function () {
                this.loading(false);
                if (this.treeWsRef || this.options.jobID === null) {
                    this.loadTree();
                } else {
                    var self = this;
                    var jobSrv = new UserAndJobState(this.runtime.getConfig('services.ujs.url'), {
                        token: this.runtime.service('session').getAuthToken()
                    });
                    self.$elem.empty();

                    var panel = $('<div class="loader-table"/>');
                    self.$elem.append(panel);
                    var table = $('<table class="table table-striped table-bordered" ' +
                        'style="margin-left: auto; margin-right: auto;" id="' + self.pref + 'overview-table"/>');
                    panel.append(table);
                    table.append('<tr><td>Job was created with id</td><td>' + self.options.jobID + '</td></tr>');
                    table.append('<tr><td>Output result will be stored as</td><td>' + self.options.treeID + '</td></tr>');
                    table.append('<tr><td>Current job state is</td><td id="' + self.pref + 'job"></td></tr>');
                    var timeLst = function (event) {
                        jobSrv.get_job_status(self.options.jobID, function (data) {
                            var status = data[2];
                            var complete = data[5];
                            var wasError = data[6];
                            var tdElem = $('#' + self.pref + 'job');
                            if (status === 'running') {
                                tdElem.html(html.loading(status));
                            } else {
                                tdElem.html(status);
                            }
                            if (complete === 1) {
                                clearInterval(self.timer);
                                if (this.treeWsRef) {
                                    // Just skip all this cause data was already showed through setState()
                                } else {
                                    if (wasError === 0) {
                                        self.loadTree();
                                    }
                                }
                            }
                        }, function (data) {
                            clearInterval(self.timer);
                            if (this.treeWsRef) {
                                // Just skip all this cause data was already showed through setState()
                            } else {
                                var tdElem = $('#' + self.pref + 'job');
                                tdElem.html("Error accessing job status: " + data.error.message);
                            }
                        });
                    };
                    timeLst();
                    self.timer = setInterval(timeLst, 5000);
                }
            },
            loadTree: function () {
                var objId = this.buildObjectIdentity(this.options.workspaceID, this.options.treeID, this.options.treeObjVer, this.treeWsRef);
                var self = this;
                this.wsClient.get_objects([objId])
                    .then(function (objArr) {
                        self.$elem.empty();

                        var canvasDivId = "knhx-canvas-div-" + self.pref;
                        self.canvasId = "knhx-canvas-" + self.pref;
                        self.$canvas = $('<div id="' + canvasDivId + '">')
                            .append($('<canvas id="' + self.canvasId + '">'));

                        if (self.options.height) {
                            self.$canvas.css({'max-height': self.options.height - 85, 'overflow': 'scroll'});
                        }
                        self.$elem.append(self.$canvas);

                        // SKIP FOR NOW
                        //watchForWidgetMaxWidthCorrection(canvasDivId);

                        if (!self.treeWsRef) {
                            var info = objArr[0].info;
                            self.treeWsRef = info[6] + "/" + info[0] + "/" + info[4];
                        }
                        var tree = objArr[0].data;

                        var refToInfoMap = {};
                        var objIdentityList = [];
                        if (tree.ws_refs) {
                            var key;
                            for (key in tree.ws_refs) {
                                objIdentityList.push({ref: tree.ws_refs[key]['g'][0]});
                            }
                        }
                        if (objIdentityList.length > 0) {
                            self.wsClient.get_object_info_new({objects: objIdentityList}, function (data) {
                                var i;
                                for (i in data) {
                                    var objInfo = data[i];
                                    refToInfoMap[objIdentityList[i].ref] = objInfo;
                                }
                            }, function (err) {
                                console.log("Error getting genomes info:");
                                console.log(err);
                            });
                        }
                        new EasyTree(self.canvasId, tree.tree, tree.default_node_labels, function (node) {
                            if ((!tree.ws_refs) || (!tree.ws_refs[node.id])) {
                                var node_name = tree.default_node_labels[node.id];
                                if (node_name.indexOf('/') > 0) {  // Gene label
                                    /* TODO: reroute #genes to #dataview */
                                    var url = "#genes/" + self.options.workspaceID + "/" + node_name;
                                    window.open(url, '_blank');<commit_msg>formatting edits<commit_after>/**
 * @author <NAME> <<EMAIL>>, <NAME> <<EMAIL>>
 * @public
 */
/*global
 define, console
 */
/*jslint
 browser: true,
 white: true
 */
define([
    'jquery',
    'bluebird',
    'uuid',
    'kb/common/html',
    'kb/service/client/workspace',
    'kb/service/client/userAndJobState',
    'kb_dataview_easyTree',
    'kb/widget/legacy/authenticatedWidget'
],
    function ($, Promise, uuid, html,Workspace, UserAndJobState, EasyTree) {
        'use strict';
        $.KBWidget({
            name: 'kbaseTree',
            parent: 'kbaseAuthenticatedWidget',
            version: '0.0.1',
            options: {
                treeID: null,
                workspaceID: null,
                treeObjVer: null,
                jobID: null,
                token: null,
                width: 1045,
                height: 600
            },
            treeWsRef: null,
            pref: null,
            timer: null,
            init: function (options) {
                this._super(options);
                this.pref = uuid.v4();

                if (!this.options.treeID) {
                    this.renderError("No tree to render!");
                    return this;
                }

                if (!this.options.workspaceID) {
                    this.renderError("No workspace given!");
                    return this;
                }

                this.wsClient = new Workspace(this.runtime.getConfig('services.workspace.url'), {
                    token: this.runtime.service('session').getAuthToken()
                });
                this.$messagePane = $("<div/>")
                    .addClass("kbwidget-message-pane kbwidget-hide-message");
                this.$elem.append(this.$messagePane);

                this.render();

                return this;
            },
            render: function () {
                this.loading(false);
                if (this.treeWsRef || this.options.jobID === null) {
                    this.loadTree();
                } else {
                    var self = this;
                    var jobSrv = new UserAndJobState(this.runtime.getConfig('services.ujs.url'), {
                        token: this.runtime.service('session').getAuthToken()
                    });
                    self.$elem.empty();

                    var panel = $('<div class="loader-table"/>');
                    self.$elem.append(panel);
                    var table = $('<table class="table table-striped table-bordered" ' +
                        'style="margin-left: auto; margin-right: auto;" id="' + self.pref + 'overview-table"/>');
                    panel.append(table);
                    table.append('<tr><td>Job was created with id</td><td>' + self.options.jobID + '</td></tr>');
                    table.append('<tr><td>Output result will be stored as</td><td>' + self.options.treeID + '</td></tr>');
                    table.append('<tr><td>Current job state is</td><td id="' + self.pref + 'job"></td></tr>');
                    var timeLst = function (event) {
                        jobSrv.get_job_status(self.options.jobID, function (data) {
                            var status = data[2];
                            var complete = data[5];
                            var wasError = data[6];
                            var tdElem = $('#' + self.pref + 'job');
                            if (status === 'running') {
                                tdElem.html(html.loading(status));
                            } else {
                                tdElem.html(status);
                            }
                            if (complete === 1) {
                                clearInterval(self.timer);
                                if (this.treeWsRef) {
                                    // Just skip all this cause data was already showed through setState()
                                } else {
                                    if (wasError === 0) {
                                        self.loadTree();
                                    }
                                }
                            }
                        }, function (data) {
                            clearInterval(self.timer);
                            if (this.treeWsRef) {
                                // Just skip all this cause data was already showed through setState()
                            } else {
                                var tdElem = $('#' + self.pref + 'job');
                                tdElem.html("Error accessing job status: " + data.error.message);
                            }
                        });
                    };
                    timeLst();
                    self.timer = setInterval(timeLst, 5000);
                }
            },
            loadTree: function () {
                var objId = this.buildObjectIdentity(this.options.workspaceID, this.options.treeID, this.options.treeObjVer, this.treeWsRef);
                var self = this;
                this.wsClient.get_objects([objId])
                    .then(function (objArr) {
                        self.$elem.empty();

                        var canvasDivId = "knhx-canvas-div-" + self.pref;
                        self.canvasId = "knhx-canvas-" + self.pref;
                        self.$canvas = $('<div id="' + canvasDivId + '">')
                            .append($('<canvas id="' + self.canvasId + '">'));

                        if (self.options.height) {
                            self.$canvas.css({'max-height': self.options.height - 85, 'overflow': 'scroll'});
                        }
                        self.$elem.append(self.$canvas);

                        // SKIP FOR NOW
                        //watchForWidgetMaxWidthCorrection(canvasDivId);

                        if (!self.treeWsRef) {
                            var info = objArr[0].info;
                            self.treeWsRef = info[6] + "/" + info[0] + "/" + info[4];
                        }
                        var tree = objArr[0].data;

                        var refToInfoMap = {};
                        var objIdentityList = [];
                        if (tree.ws_refs) {
                            var key;
                            for (key in tree.ws_refs) {
                                objIdentityList.push({ref: tree.ws_refs[key]['g'][0]});
                            }
                        }
                        
                        if (objIdentityList.length > 0) {
                            self.wsClient.get_object_info_new({objects: objIdentityList}, function (data) {
                                var i;
                                for (i in data) {
                                    var objInfo = data[i];
                                    refToInfoMap[objIdentityList[i].ref] = objInfo;
                                }
                            }, function (err) {
                                console.log("Error getting genomes info:");
                                console.log(err);
                            });
                        }
                        new EasyTree(self.canvasId, tree.tree, tree.default_node_labels, function (node) {
                            if ((!tree.ws_refs) || (!tree.ws_refs[node.id])) {
                                var node_name = tree.default_node_labels[node.id];
                                if (node_name.indexOf('/') > 0) {  // Gene label
                                    /* TODO: reroute #genes to #dataview */
                                    var url = "#genes/" + self.options.workspaceID + "/" + node_name;
                                    window.open(url, '_blank');
{'fraction_non_alphanumeric': 0.1182934712346477, 'fraction_numerical': 0.003878474466709761, 'mean_word_length': 0.969573520050923, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7141468', 'n_tokens_mistral': 3875, 'n_tokens_neox': 3575, 'n_words': 706}