<commit_before>"use strict";

(function( $, undefined ) {
    $.KBWidget({
        name: 'kbaseNarrativeJobsPanel',
        parent: 'kbaseNarrativeControlPanel',
        version: '0.0.1',
        options: {
            loadingImage: 'static/kbase/images/ajax-loader.gif',
            autopopulate: true,
            title: 'Jobs',
        },
        // these are the elements that contain running apps and methods
        $appsList: null,
        $methodsList: null,

        refreshTimer: null,
        refreshInterval: 5000,

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

            $(document).on('registerMethod.Narrative', $.proxy(
                function(e, jobInfo) {
                    this.registerJob(jobInfo, false);
                }, this)
            );

            $(document).on('registerApp.Narrative', $.proxy(
                function(e, jobInfo) {
                    this.registerJob(jobInfo, true);
                }, this)
            );

            $(document).on('refreshJobs.Narrative', $.proxy(
                function(e) {
                    this.refresh();
                }, this)
            );

            // DOM structure setup here.
            // After this, just need to update the function list

            /* There's a few bits here.
             * 1. It's all in a Bootstrap Panel scaffold.
             * 2. The panel-body section contains the core of the widget:
             *    a. loading panel (just a blank thing with a spinning gif)
             *    b. error panel
             *    c. actual function widget setup.
             *
             * So, initialize the scaffold, bind the three core pieces in the
             * panel-body, make sure the right one is being shown at the start,
             * and off we go.
             */

            var $refreshBtn = $('<button>')
                              .addClass('btn btn-xs btn-default')
                              .click($.proxy(function(event) { this.refresh(); }, this))
                              .append($('<span>')
                                      .addClass('glyphicon glyphicon-refresh'));

            var $headerDiv = $('<div>')
                              .append('Jobs')
                              .append($('<button>')
                                      .addClass('btn btn-xs btn-default kb-ws-refresh-btn')
                                      .css({'margin-top': '-4px',
                                            'margin-right': '4px'})
                                      .click($.proxy(function(event) { this.refresh(); }, this))
                                      .append($('<span>')
                                              .addClass('glyphicon glyphicon-refresh')));

            this.$methodsList = $('<div>');
            this.$appsList = $('<div>');

            this.$jobsAccordion = $('<div>');
            // Make a function panel for everything to sit inside.
            this.$jobsPanel = $('<div>')
                              .addClass('kb-function-body')
                              .append(this.$jobsAccordion);
            
            this.$jobsAccordion.kbaseAccordion({'elements' : [{ 'title' : 'Apps', 'body' : this.$appsList },
                                                              { 'title' : 'Methods', 'body' : this.$methodsList }]});

            // The 'loading' panel should just have a spinning gif in it.
            this.$loadingPanel = $('<div>')
                                 .addClass('kb-data-loading')
                                 .append('<img src="' + this.options.loadingImage + '">')
                                 .append($('<div>')
                                         .attr('id', 'message'))
                                 .hide();

            // The error panel should be empty for now.
            this.$errorPanel = $('<div>')
                               .addClass('kb-error')
                               .hide();

            this.addButton($refreshBtn);

            this.body().append(this.$jobsPanel)
                       .append(this.$loadingPanel)
                       .append(this.$errorPanel);

            this.refresh();

            if (this.options.autopopulate === true) {
                this.refresh();
            }

            return this;
        },

        /**
         * Shows a loading spinner or message on top of the panel.
         * @private
         */
        showMessage: function(message, loading) {
            this.$loadingPanel.find('#message').empty();
            if (message) 
                this.$loadingPanel.find('#message').html(message);
            if (loading)
                this.$loadingPanel.find('img').show();
            else
                this.$loadingPanel.find('img').hide();
            this.$jobsPanel.hide();
            this.$errorPanel.hide();
            this.$loadingPanel.show();
        },

        showLoadingMessage: function(message) {
            this.showMessage(message, true);
        },

        /**
         * Shows the main jobs panel, hiding all others.
         * @private
         */
        showJobsPanel: function() {
            this.$errorPanel.hide();
            this.$loadingPanel.hide();
            this.$jobsPanel.show();
        },

        registerJob: function(jobInfo, isApp) {
            // Check to make sure the Narrative has been instantiated to begin with
            if (!IPython || !IPython.notebook || !IPython.notebook.kernel || !IPython.notebook.metadata)
                return;
            if (!IPython.notebook.metadata.job_ids)
                IPython.notebook.metadata.job_ids = {
                    'methods' : [],
                    'apps' : []
                };
            if (!IPython.notebook.metadata.job_ids['methods'])
                IPython.notebook.metadata.job_ids['methods'] = [];
            if (!IPython.notebook.metadata.job_ids['apps'])
                IPython.notebook.metadata.job_ids['apps'] = [];

            var type = isApp ? 'apps' : 'methods';
            IPython.notebook.metadata.job_ids[type].push(jobInfo);
            this.refresh();
        },

        /**
         * @method
         */
        refresh: function() {
            // if there's no timer, set one up.
            // if (this.refreshTimer === null) {<commit_msg>*Really* fixed Job metadata initialization (checks for array, replaces with object).<commit_after>"use strict";

(function( $, undefined ) {
    $.KBWidget({
        name: 'kbaseNarrativeJobsPanel',
        parent: 'kbaseNarrativeControlPanel',
        version: '0.0.1',
        options: {
            loadingImage: 'static/kbase/images/ajax-loader.gif',
            autopopulate: true,
            title: 'Jobs',
        },
        // these are the elements that contain running apps and methods
        $appsList: null,
        $methodsList: null,

        refreshTimer: null,
        refreshInterval: 5000,

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

            $(document).on('registerMethod.Narrative', $.proxy(
                function(e, jobInfo) {
                    this.registerJob(jobInfo, false);
                }, this)
            );

            $(document).on('registerApp.Narrative', $.proxy(
                function(e, jobInfo) {
                    this.registerJob(jobInfo, true);
                }, this)
            );

            $(document).on('refreshJobs.Narrative', $.proxy(
                function(e) {
                    this.refresh();
                }, this)
            );

            // DOM structure setup here.
            // After this, just need to update the function list

            /* There's a few bits here.
             * 1. It's all in a Bootstrap Panel scaffold.
             * 2. The panel-body section contains the core of the widget:
             *    a. loading panel (just a blank thing with a spinning gif)
             *    b. error panel
             *    c. actual function widget setup.
             *
             * So, initialize the scaffold, bind the three core pieces in the
             * panel-body, make sure the right one is being shown at the start,
             * and off we go.
             */

            var $refreshBtn = $('<button>')
                              .addClass('btn btn-xs btn-default')
                              .click($.proxy(function(event) { this.refresh(); }, this))
                              .append($('<span>')
                                      .addClass('glyphicon glyphicon-refresh'));

            var $headerDiv = $('<div>')
                              .append('Jobs')
                              .append($('<button>')
                                      .addClass('btn btn-xs btn-default kb-ws-refresh-btn')
                                      .css({'margin-top': '-4px',
                                            'margin-right': '4px'})
                                      .click($.proxy(function(event) { this.refresh(); }, this))
                                      .append($('<span>')
                                              .addClass('glyphicon glyphicon-refresh')));

            this.$methodsList = $('<div>');
            this.$appsList = $('<div>');

            this.$jobsAccordion = $('<div>');
            // Make a function panel for everything to sit inside.
            this.$jobsPanel = $('<div>')
                              .addClass('kb-function-body')
                              .append(this.$jobsAccordion);
            
            this.$jobsAccordion.kbaseAccordion({'elements' : [{ 'title' : 'Apps', 'body' : this.$appsList },
                                                              { 'title' : 'Methods', 'body' : this.$methodsList }]});

            // The 'loading' panel should just have a spinning gif in it.
            this.$loadingPanel = $('<div>')
                                 .addClass('kb-data-loading')
                                 .append('<img src="' + this.options.loadingImage + '">')
                                 .append($('<div>')
                                         .attr('id', 'message'))
                                 .hide();

            // The error panel should be empty for now.
            this.$errorPanel = $('<div>')
                               .addClass('kb-error')
                               .hide();

            this.addButton($refreshBtn);

            this.body().append(this.$jobsPanel)
                       .append(this.$loadingPanel)
                       .append(this.$errorPanel);

            this.refresh();

            if (this.options.autopopulate === true) {
                this.refresh();
            }

            return this;
        },

        /**
         * Shows a loading spinner or message on top of the panel.
         * @private
         */
        showMessage: function(message, loading) {
            this.$loadingPanel.find('#message').empty();
            if (message) 
                this.$loadingPanel.find('#message').html(message);
            if (loading)
                this.$loadingPanel.find('img').show();
            else
                this.$loadingPanel.find('img').hide();
            this.$jobsPanel.hide();
            this.$errorPanel.hide();
            this.$loadingPanel.show();
        },

        showLoadingMessage: function(message) {
            this.showMessage(message, true);
        },

        /**
         * Shows the main jobs panel, hiding all others.
         * @private
         */
        showJobsPanel: function() {
            this.$errorPanel.hide();
            this.$loadingPanel.hide();
            this.$jobsPanel.show();
        },

        registerJob: function(jobInfo, isApp) {
            // Check to make sure the Narrative has been instantiated to begin with.
            if (!IPython || !IPython.notebook || !IPython.notebook.kernel || !IPython.notebook.metadata)
                return;
            // If the job ids hasn't been inited yet, or it was done in the old way (as an array) then do it.
            if (!IPython.notebook.metadata.job_ids || 
                Object.prototype.toString.call(IPython.notebook.metadata.job_ids) === '[object Array]') {
                IPython.notebook.metadata.job_ids = {
                    'methods' : [],
                    'apps' : []
                };
            }
            // Double-check that it has the right properties
            if (!IPython.notebook.metadata.job_ids['methods'])
                IPython.notebook.metadata.job_ids['methods'] = [];
            if (!IPython.notebook.metadata.job_ids['apps'])
                IPython.notebook.metadata.job_ids['apps'] = [];

            var type = isApp ? 'apps' : 'methods';
            IPython.notebook.metadata.job_ids[type].push(jobInfo);
            this.refresh();
        },

        /**
         * @method
         */
        refresh: function() {
            // if there's no timer, set one up.
            // if (this.refreshTimer === null) {
{'fraction_non_alphanumeric': 0.1273953702284225, 'fraction_numerical': 0.0016863406408094434, 'mean_word_length': 1.2287324906047148, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5533761', 'n_tokens_mistral': 3289, 'n_tokens_neox': 3122, 'n_words': 722}