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

91.67% Statements 22/24
100% Branches 10/10
75% Functions 6/8
91.67% Lines 22/24

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                      299x                                                     299x                       496x   496x             534x     492x   492x     42x         42x                 1049x 1049x   1049x 39x 39x   39x 39x                 2x   2x 2x 2x   2x   2x                      
'use strict';
 
/**
 * Save extension for the Datagrid View Selector.
 * It displays a button near the selector to allow the user to save the current changes
 * to the current 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',
        'pim/form',
        'pim/template/grid/view-selector/save-view',
        'pim/datagrid/state',
        'pim/dialog',
        'routing',
        'pim/user-context',
        'pim/saver/datagrid-view',
        'oro/messenger'
    ],
    function (
        $,
        _,
        __,
        BaseForm,
        template,
        DatagridState,
        Dialog,
        Routing,
        UserContext,
        DatagridViewSaver,
        messenger
    ) {
        return BaseForm.extend({
            template: _.template(template),
            tagName: 'span',
            className: 'save-button',
            events: {
                'click .save': 'saveView'
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'grid:view-selector:state-changed', this.onDatagridStateChange);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                if ('view' !== this.getRoot().currentViewType ||
                    UserContext.get('meta').id !== this.getRoot().currentView.owner_id
                ) {
                    this.$el.html('');
 
                    return;
                }
 
                this.$el.html(this.template({
                    dirty: this.dirty,
                    label: __('pim_datagrid.view_selector.save_changes')
                }));
 
                this.$('[data-toggle="tooltip"]').tooltip();
            },
 
            /**
             * Method called on datagrid state change (when columns or filters are modified)
             *
             * @param {Object} datagridState
             */
            onDatagridStateChange: function (datagridState) {
                var initialView = this.getRoot().initialView;
                var initialViewExists = null !== initialView && 0 !== initialView.id;
 
                if (initialViewExists) {
                    var filtersModified = initialView.filters !== datagridState.filters;
                    var columnsModified = !_.isEqual(initialView.columns, datagridState.columns.split(','));
 
                    this.dirty = filtersModified || columnsModified;
                    this.render();
                }
            },
 
            /**
             * Save the current Datagrid view in database and triggers an event to the parent
             * to select it.
             */
            saveView: function () {
                var gridState = DatagridState.get(this.getRoot().gridAlias, ['filters', 'columns']);
 
                var currentView = $.extend(true, {}, this.getRoot().currentView);
                currentView.filters = gridState.filters;
                currentView.columns = gridState.columns;
 
                DatagridViewSaver.save(currentView, this.getRoot().gridAlias)
                    .done(function (response) {
                        this.getRoot().trigger('grid:view-selector:view-saved', response.id);
                    }.bind(this))
                    .fail(function (response) {
                        _.each(response.responseJSON, function (error) {
                            messenger.notify('error', error);
                        });
                    });
            }
        });
    }
);