<commit_before>/*global
 define
 */
/*jslint
 browser: true,
 white: true
 */
define([
    'jquery',
    'kb/service/client/narrativeMethodStore',
    'kb/service/client/catalog',
    'plugins/catalog/modules/widgets/kbaseCatalogRegistration',
    'kb/widget/legacy/authenticatedWidget',
    'bootstrap',
],
    function ($, NarrativeMethodStore, Catalog) {
        $.KBWidget({
            name: "KBaseCatalogModuleViewer",
            parent: "kbaseAuthenticatedWidget",  // todo: do we still need th
            options: {
                module_name: null
            },
            $mainPanel: null,
            $errorPanel: null,

            // clients to the catalog service and the NarrativeMethodStore
            catalog: null,
            nms: null,

            // 
            module_name: null,
            moduleDetails: null,
            isGithub: null,

            // main panel and elements
            $mainPanel: null,
            $appsPanel: null,
            $loadingPanel: null,

            init: function (options) {
                this._super(options);

                var self = this;

                self.module_name = options.module_name;
                self.isGithub = false;

                // new style we have a runtime object that gives us everything in the options
                self.runtime = options.runtime;
                self.setupClients();

                console.log(options);
                console.log(this.runtime.service('session').getUsername());

                // initialize and add the main panel
                self.$loadingPanel = self.initLoadingPanel();
                self.$elem.append(self.$loadingPanel);
                var mainPanelElements = self.initMainPanel();
                //[$mainPanel, $header, $adminPanel, $appsPanel, $descriptionPanel, $versionsPanel];
                self.$mainPanel = mainPanelElements[0];
                self.$headerPanel = mainPanelElements[1];
                self.$adminPanel = mainPanelElements[2];
                self.$appsPanel = mainPanelElements[3];
                self.$descriptionPanel = mainPanelElements[4];
                self.$versionsPanel = mainPanelElements[5];

                self.$elem.append(self.$mainPanel);
                self.showLoading();

                // get the module information
                var loadingCalls = [];
                self.moduleDetails = { info:null, versions:null };
                loadingCalls.push(self.getModuleInfo());
                loadingCalls.push(self.getModuleVersions());
                loadingCalls.push(self.getModuleStatus());

                // when we have it all, then render the list
                Promise.all(loadingCalls).then(function() {
                    self.render();
                    self.hideLoading();
                });


                return this;
            },

            render: function() {
                var self = this;

                var info = self.moduleDetails.info;
                var versions = self.moduleDetails.versions;


                // HEADER
                var $header = $('<div>');
                $header.append($('<h1>').append(info.module_name));
                $header.append($('<h4>').append(
                    '<a href="'+info.git_url+'" target="_blank">'+info.git_url+'<a>'));

                var isOwner = false;
                var $owners = $('<div>').append('<i>Developed by:</i> ');
                for(var k=0; k<info.owners.length; k++) {
                    // todo: get nice name
                    var username = info.owners[k];
                    $owners.append('<a href="#people/'+username+'">'+username+"</a> ");
                    if(self.runtime.service('session').getUsername() === info.owners[k]) {
                        isOwner = true;
                    }
                }
                $header.append($owners);

                self.$headerPanel.append($header);


                // ADMIN PANEL IF OWNER
                if(isOwner) {
                    self.$adminPanel.append('<b>You are a module owner</b><br>');
                    self.$adminPanel.append(self.renderModuleAdminDiv());
                }


                //DESCRIPTION PANEL
                self.$descriptionPanel.append($('<div>').html(info.description));



                
                //VERSIONS PANEL

                var $versionDiv = $('<div>');

                $versionDiv.append('<h3>Stable Released Version</h3>');
                if(info.release) {
                    $versionDiv.append(self.renderVersion('release',info.release));
                } else {
                    $versionDiv.append('<i>This module has not been released.</i>');
                }

                $versionDiv.append('<h3>Beta Version</h3>');
                if(info.beta) {
                    $versionDiv.append(self.renderVersion('beta',info.beta));
                } else {
                    $versionDiv.append('<i>This module has not been released to beta.</i>');
                }

                $versionDiv.append('<h3>Development Version</h3>');
                if(info.dev) {
                    $versionDiv.append('<a href="#appcatalog/status/'+info.module_name+'">View recent registrations</a><br>')
                    $versionDiv.append(self.renderVersion('dev',info.dev));
                } else {
                    $versionDiv.append('<i>This module has not been registered properly.</i>');
                }

                self.$versionsPanel.append($versionDiv);
                

                if(versions) {
                    if(versions.length>0) {
                        $versionDiv.append('<hr>');
                        $versionDiv.append('<h3>Old Releases</h3>');
                        for(var v=0; v<versions.length; v++) {
                            $versionDiv.append('<h4>'+versions[v].version+'</h4>');
                            $versionDiv.append(self.renderVersion(versions[v].version,versions[v]));
                        }
                    }
                }

                console.debug(self.moduleDetails);
            },

            // tag=dev/beta/release/version number, version=the actual info
            renderVersion: function(tag, version) {
                var self = this;
                var git_url = this.moduleDetails.info.git_url;
                var $verDiv = $('<div>');

                // Check state here, it may be registering currently

                $verDiv.append('<b>Version:</b> ' + version.version + '<br>');
                $verDiv.append('<b>Registration Timestamp:</b> ' + new Date(version.timestamp).toLocaleString() + ' - ' + version.timestamp + '<br>');
                if(self.isGithub) {
                    $verDiv.append('<b>Commit:</b> <a href="'+git_url+'/tree/' + version.git_commit_hash+
                        '" target="_blank">'+version.git_commit_hash+'</a><br>');
                } else {
                    $verDiv.append('<b>Commit:</b> ' + version.git_commit_hash+'<br>');
                }
                $verDiv.append('<b>Commit Mssg:</b> ' + version.git_commit_message+'<br>');
                $verDiv.append('<b>Narrative Apps/Methods:</b> ');
<commit_msg>made version info collapsable<commit_after>/*global
 define
 */
/*jslint
 browser: true,
 white: true
 */
define([
    'jquery',
    'kb/service/client/narrativeMethodStore',
    'kb/service/client/catalog',
    'plugins/catalog/modules/widgets/kbaseCatalogRegistration',
    'kb/widget/legacy/authenticatedWidget',
    'bootstrap',
],
    function ($, NarrativeMethodStore, Catalog) {
        $.KBWidget({
            name: "KBaseCatalogModuleViewer",
            parent: "kbaseAuthenticatedWidget",  // todo: do we still need th
            options: {
                module_name: null
            },
            $mainPanel: null,
            $errorPanel: null,

            // clients to the catalog service and the NarrativeMethodStore
            catalog: null,
            nms: null,

            // 
            module_name: null,
            moduleDetails: null,
            isGithub: null,

            // main panel and elements
            $mainPanel: null,
            $appsPanel: null,
            $loadingPanel: null,

            init: function (options) {
                this._super(options);

                var self = this;

                self.module_name = options.module_name;
                self.isGithub = false;

                // new style we have a runtime object that gives us everything in the options
                self.runtime = options.runtime;
                self.setupClients();

                console.log(options);
                console.log(this.runtime.service('session').getUsername());

                // initialize and add the main panel
                self.$loadingPanel = self.initLoadingPanel();
                self.$elem.append(self.$loadingPanel);
                var mainPanelElements = self.initMainPanel();
                //[$mainPanel, $header, $adminPanel, $appsPanel, $descriptionPanel, $versionsPanel];
                self.$mainPanel = mainPanelElements[0];
                self.$headerPanel = mainPanelElements[1];
                self.$adminPanel = mainPanelElements[2];
                self.$appsPanel = mainPanelElements[3];
                self.$descriptionPanel = mainPanelElements[4];
                self.$versionsPanel = mainPanelElements[5];

                self.$elem.append(self.$mainPanel);
                self.showLoading();

                // get the module information
                var loadingCalls = [];
                self.moduleDetails = { info:null, versions:null };
                loadingCalls.push(self.getModuleInfo());
                loadingCalls.push(self.getModuleVersions());
                loadingCalls.push(self.getModuleStatus());

                // when we have it all, then render the list
                Promise.all(loadingCalls).then(function() {
                    self.render();
                    self.hideLoading();
                });


                return this;
            },

            renderCollapsableVersionDiv: function(title, $content) {
                // todo: replace this cut/paste with a function
                var $div = $('<div>').css('margin-top','1em');
                var $content = $('<div>').css('margin','1em').append($content).hide();
                var $toggle = $('<i>').addClass('fa fa-chevron-right').css('margin-left','15px');
                $div.append(
                        $('<div>').css('cursor','pointer')
                            .append($('<h4>').append(title).css('display','inline'))
                            .append($toggle)
                            .on('click', function() {
                                        if($toggle.hasClass('fa-chevron-right')) {
                                            // hidden, so show
                                            $toggle.removeClass('fa-chevron-right');
                                            $toggle.addClass('fa-chevron-down');
                                            $content.slideDown();
                                        } else {
                                            $toggle.removeClass('fa-chevron-down');
                                            $toggle.addClass('fa-chevron-right');
                                            $content.slideUp();
                                        }
                                    }));
                $div.append($content);
                return $div;
            },


            render: function() {
                var self = this;

                var info = self.moduleDetails.info;
                var versions = self.moduleDetails.versions;


                // HEADER
                var $header = $('<div>');
                $header.append($('<h1>').append(info.module_name));
                $header.append($('<h4>').append(
                    '<a href="'+info.git_url+'" target="_blank">'+info.git_url+'<a>'));

                var isOwner = false;
                var $owners = $('<div>').append('<i>Developed by:</i> ');
                for(var k=0; k<info.owners.length; k++) {
                    // todo: get nice name
                    var username = info.owners[k];
                    if(k>0) { $owners.append(', '); }
                    $owners.append('<a href="#people/'+username+'">'+username+"</a>");
                    if(self.runtime.service('session').getUsername() === info.owners[k]) {
                        isOwner = true;
                    }
                }
                $header.append($owners);

                self.$headerPanel.append($header);


                // ADMIN PANEL IF OWNER
                if(isOwner) {
                    self.$adminPanel.append('<b>You are a module owner</b><br>');
                    self.$adminPanel.append(self.renderModuleAdminDiv());
                }


                //DESCRIPTION PANEL
                self.$descriptionPanel.append($('<div>').html(info.description));



                
                //VERSIONS PANEL
                var $versionDiv = $('<div>');

                if(info.release) {
                    $versionDiv.append(self.renderCollapsableVersionDiv(
                        'Latest Release Version',
                        self.renderVersion('release',info.release)
                        ));
                } else {
                    $versionDiv.append('<i>This module has not been released.</i><br>');
                }

                if(info.beta) {
                    $versionDiv.append(self.renderCollapsableVersionDiv(
                        'Beta Version',
                        self.renderVersion('beta',info.beta)
                        ));
                } else {
                    $versionDiv.append('<i>This module has no beta version.</i><br>');
                }

                if(info.dev) {
                    $versionDiv.append(self.renderCollapsableVersionDiv(
                        'Development Version',
                        $('<div>')
                            .append('<a href="#appcatalog/status/'+info.module_name+'">View recent registrations</a><br><br>')
                            .append(self.renderVersion('dev',info.dev))
                        ));
                } else {
                    $versionDiv.append('<i>This module has not yet completed a successful registration.</i>');
                }

                self.$versionsPanel.append($versionDiv);
                

                if(versions) {
                    if(versions.length>0) {
                        var $oldReleaseDiv = $('<div>');
                        for(var v=0; v<versions.length; v++) {
                            $oldReleaseDiv.append(self.renderCollapsableVersionDiv(
                                versions[v].version,
                                self.renderVersion(versions[v].version,versions[v])
                            ));
                        }
                        $versionDiv.append(self.renderCollapsableVersionDiv(
                            'Old Releases',
                            $oldReleaseDiv));
                    }
                }

                console.debug(self.moduleDetails);
            },

            // tag=dev/beta/release/version number, version=the actual info
            renderVersion: function(tag, version) {
                var self = this;
                var git_url = this.moduleDetails.info.git_url;
                var $verDiv = $('<div>');

                // Check state here, it may be registering currently

                $verDiv.append('<b>Version:</b> ' + version.version + '<br>');
                $verDiv.append('<b>Registration Timestamp:</b> ' + new Date(version.timestamp).toLocaleString() + ' - ' + version.timestamp + '<br>');
                if(self.isGithub) {
                    $verDiv.append('<b>Commit:</b> <a href="'+git_url+'/tree/' + version.git_commit_hash+
                        '" target="_blank">'+version.git_commit_hash+'</a><br>');
                } else {
                    $verDiv.append('<b>Commit:</b> ' + version.git_commit_hash+'<br>');
                }
                $verDiv.append('<b>Commit Mssg:</b> ' + version.git_commit_message+'<br>');
                $verDiv.append('<b>Narrative Apps/Methods:</b> ');

{'fraction_non_alphanumeric': 0.13076314026432792, 'fraction_numerical': 0.0023143918630854497, 'mean_word_length': 1.3460494356336619, 'pattern_counts': {'":': 0, '<': 116, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6349065', 'n_tokens_mistral': 4220, 'n_tokens_neox': 3895, 'n_words': 690}