All files / web/bundles/pimui/js/job/execution auto-refresh.js

85.71% Statements 30/35
71.43% Branches 5/7
83.33% Functions 10/12
85.71% Lines 30/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                383x                     383x                                 643x 643x   643x             605x 605x     605x   605x                 1423x 1423x                   604x     604x 604x                         1116x 819x 819x 819x   819x 819x 819x       297x 297x                               1247x 1247x             3182x             3182x               1759x     1759x 1759x          
'use strict';
/**
 * Auto refresh
 *
 * @author    Alban Alnot <alban.alnot@consertotech.pro>
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/fetcher-registry',
        'pim/template/job-execution/auto-refresh',
        'backbone'
    ],
    function ($, _, __, BaseForm, FetcherRegistry, template, Backbone) {
        return BaseForm.extend({
            template: _.template(template),
            events: {
                'click a': 'startAutoUpdateOnClick'
            },
            autoRefreshDelay: 1000, //1 second
            autoRefreshTimeout: null,
 
            stopAutoRefreshDelay: 2 * 60 * 1000, //2 minutes
            stopAutoUpdateTimeout: null,
 
            status: null, //3 status: isLoading | isFinished | isNotFinished
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
                this.setStatus('isLoading');
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim-job-execution-form:start-auto-update', this.startAutoUpdate);
                this.listenTo(this.getRoot(), 'pim-job-execution-form:stop-auto-update', this.stopAll);
 
                // Clear interval/timeout when changing the page
                Backbone.Router.prototype.on('route', this.stopAll.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Restart the auto refresh timeout
             */
            restartAutoRefreshTimeout: function () {
                //We do not want a setInterval here,
                //in order to avoid to fetch every second even if a fetch is already in progress...
                clearTimeout(this.autoRefreshTimeout);
                this.autoRefreshTimeout =
                    setTimeout(this.fetchData.bind(this, this.getFormData()), this.autoRefreshDelay);
            },
 
            /**
             * Start the auto update
             */
            startAutoUpdate: function () {
 
                //Refreshing data every seconds
                this.restartAutoRefreshTimeout();
 
                //After 2 minutes, stop the auto refresh and display the button 'Refresh' (only if the job is not done!)
                clearTimeout(this.stopAutoUpdateTimeout);
                this.stopAutoUpdateTimeout =
                    setTimeout(function () {
                        this.stopAll();
                        this.setStatus('isNotFinished');
                    }.bind(this), this.stopAutoRefreshDelay);
            },
 
            /**
             * Fetch the data
             * @param jobExecution
             */
            fetchData: function (jobExecution) {
 
                if (jobExecution.isRunning) {
                    this.setStatus('isLoading');
                    var jobId = jobExecution.meta.id;
                    FetcherRegistry.getFetcher('job-execution').fetch(jobId, {id: jobId, cached: false})
                        .then(function (newJobExecution) {
                            this.setData(newJobExecution);
                            this.render();
                            this.restartAutoRefreshTimeout();
                        }.bind(this));
                } else {
                    ///Data are up to date!
                    this.stopAll();
                    this.setStatus('isFinished');
                }
            },
 
            /**
             * Called when clicking on 'Refresh' button
             */
            startAutoUpdateOnClick: function () {
                this.setStatus('isLoading');
                this.startAutoUpdate();
            },
 
            /**
             * Stop all timeout
             */
            stopAll: function () {
                clearTimeout(this.autoRefreshTimeout);
                clearTimeout(this.stopAutoUpdateTimeout);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template({
                    __: __,
                    status: this.status,
                    loadingShown: this.status === 'isLoading',
                    refreshBtnShown: this.status === 'isNotFinished'
                }));
 
                return this;
            },
 
            /**
             * Change the status of the extension
             * @param status (isLoading | isFinished | isNotFinished)
             */
            setStatus: function (status) {
                Iif (status !== 'isLoading' && status !== 'isFinished' && status !== 'isNotFinished') {
                    throw new Error('Status equal isLoading | isFinished | isNotFinished but === [' + status + ']');
                }
                this.status = status;
                this.render();
            }
        });
    }
);