All files / web/bundles/pimui/js/grid view-selector-create-view.js

75.76% Statements 25/33
58.33% Branches 7/12
70% Functions 7/10
75.76% Lines 25/33

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                    299x                                                     299x                           495x           495x       495x   495x             6x   6x                           6x   6x   6x 6x     6x 67x   67x     67x     6x 67x                         6x       6x 6x           6x   6x 6x 6x                      
'use strict';
 
/**
 * Create extension for the Datagrid View Selector.
 * It displays a button near the selector to allow the user to create a new view.
 *
 * @author    Adrien Pétremann <adrien.petremann@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',
        'backbone',
        'pim/form',
        'pim/template/grid/view-selector/create-view',
        'pim/template/form/creation/modal',
        'pim/template/grid/view-selector/create-view-label-input',
        'pim/datagrid/state',
        'pim/saver/datagrid-view',
        'oro/messenger'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        template,
        templateModalContent,
        templateInput,
        DatagridState,
        DatagridViewSaver,
        messenger
    ) {
        return BaseForm.extend({
            template: _.template(template),
            templateModalContent: _.template(templateModalContent),
            templateInput: _.template(templateInput),
            tagName: 'span',
            className: 'create-button',
            events: {
                'click .create': 'promptCreateView'
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif ('view' !== this.getRoot().currentViewType) {
                    this.$el.html('');
 
                    return this;
                }
 
                this.$el.html(this.template({
                    label: __('pim_datagrid.view_selector.create_view')
                }));
 
                this.$('[data-toggle="tooltip"]').tooltip();
 
                return this;
            },
 
            /**
             * Prompt the view creation modal.
             */
            promptCreateView: function () {
                this.getRoot().trigger('grid:view-selector:close-selector');
 
                let modal = new Backbone.BootstrapModal({
                    subtitle: __('pim_datagrid.view_selector.view'),
                    title: __('pim_common.create'),
                    picture: 'illustrations/Views.svg',
                    okText: __('pim_common.save'),
                    okCloses: false,
                    content: this.templateModalContent({
                        fields: this.templateInput({
                            placeholder: __('pim_datagrid.view_selector.placeholder'),
                            label: __('pim_datagrid.view_selector.choose_label')
                        })
                    }),
                });
 
                modal.open();
 
                const $submitButton = modal.$el.find('.ok').addClass('AknButton--disabled');
 
                modal.on('ok', this.saveView.bind(this, modal));
                modal.on('cancel', function () {
                    modal.remove();
                }.bind(this));
                modal.$('input[name="new-view-label"]').on('input', function (event) {
                    var label = event.target.value;
 
                    Iif (!label.length) {
                        $submitButton.addClass('AknButton--disabled');
                    } else {
                        $submitButton.removeClass('AknButton--disabled');
                    }
                });
                modal.$('input[name="new-view-label"]').on('keypress', function (event) {
                    Iif (13 === (event.keyCode || event.which) && event.target.value.length) {
                        $submitButton.trigger('click');
                    }
                });
            },
 
            /**
             * Save the current Datagrid view in database and triggers an event to the parent
             * to select it.
             *
             * @param {object} modal
             */
            saveView: function (modal) {
                Iif ($('.modal .ok').hasClass('AknButton--disabled')) {
                    return;
                }
 
                var gridState = DatagridState.get(this.getRoot().gridAlias, ['filters', 'columns']);
                var newView = {
                    filters: gridState.filters,
                    columns: gridState.columns,
                    label: modal.$('input[name="new-view-label"]').val()
                };
 
                DatagridViewSaver.save(newView, this.getRoot().gridAlias)
                    .done(function (response) {
                        this.getRoot().trigger('grid:view-selector:view-created', response.id);
                        modal.close();
                        modal.remove();
                    }.bind(this))
                    .fail(function (response) {
                        _.each(response.responseJSON, function (error) {
                            messenger.notify('error', error);
                        });
                    });
            }
        });
    }
);