All files / web/bundles/pimui/js/job/common/edit launch.js

100% Statements 21/21
100% Branches 2/2
100% Functions 10/10
100% Lines 21/21

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                335x                           335x                   559x   559x             382x 382x 382x 16x     366x         382x   382x             313x 313x 313x   312x     1x     313x                   329x 329x         329x                 350x          
'use strict';
/**
 * Launch button
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @copyright 2016 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',
        'routing',
        'pim/router',
        'pim/common/property',
        'oro/messenger',
        'oro/loading-mask',
        'pim/template/export/common/edit/launch'
    ],
    function ($, _, __, BaseForm, Routing, router, propertyAccessor, messenger, LoadingMask, template) {
        return BaseForm.extend({
            template: _.template(template),
            events: {
                'click .AknButton': 'launch'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.isVisible().then(function (isVisible) {
                    this.$el.empty();
                    if (!isVisible) {
                        return this;
                    }
 
                    this.$el.html(this.template({
                        label: __(this.config.label)
                    }));
                }.bind(this));
 
                this.delegateEvents();
 
                return this;
            },
 
            /**
             * Launch the job
             */
            launch: function () {
                var loadingMask = new LoadingMask();
                loadingMask.render().$el.appendTo(this.getRoot().$el).show();
                $.post(this.getUrl())
                    .then(function (response) {
                        router.redirect(response.redirectUrl);
                    })
                    .fail(function () {
                        messenger.notify('error', __('pim_import_export.form.job_instance.fail.launch'));
                    })
                    .always(function () {
                        loadingMask.hide().$el.remove();
                    });
            },
 
            /**
             * Get the route to launch the job
             *
             * @return {string}
             */
            getUrl: function () {
                var params = {};
                params[this.config.identifier.name] = propertyAccessor.accessProperty(
                    this.getFormData(),
                    this.config.identifier.path
                );
 
                return Routing.generate(this.config.route, params);
            },
 
            /**
             * Should this extension render
             *
             * @return {Promise}
             */
            isVisible: function () {
                return $.Deferred().resolve(true).promise();
            }
        });
    }
);