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

95.65% Statements 22/23
83.33% Branches 5/6
100% Functions 6/6
95.65% Lines 22/23

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                    297x                             297x                   499x           499x             1550x           1550x   1550x                   1051x       1051x 1051x   1051x 1051x   1051x 32x 32x   1019x 1019x   1019x 1019x     1051x                 499x                       1051x          
'use strict';
 
/**
 * Module to display the current view in the Datagrid View Selector.
 * This module accepts extensions to display more info beside the view.
 *
 * @author    Adrien Petremann <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',
        'backbone',
        'pim/form',
        'pim/template/grid/view-selector/current'
    ],
    function (
        $,
        _,
        Backbone,
        BaseForm,
        template
    ) {
        return BaseForm.extend({
            template: _.template(template),
            datagridView: null,
            dirtyColumns: false,
            dirtyFilters: false,
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(
                    this.getRoot(),
                    'grid:view-selector:state-changed',
                    this.onDatagridStateChange.bind(this)
                );
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template({
                    view: this.datagridView,
                    dirtyFilters: this.dirtyFilters,
                    dirtyColumns: this.dirtyColumns
                }));
 
                this.renderExtensions();
 
                return this;
            },
 
            /**
             * Method called on datagrid state change (when columns or filters are modified).
             * Set the state to dirty if it's the case then re-render this extension.
             *
             * @param {Object} datagridState
             */
            onDatagridStateChange: function (datagridState) {
                Iif (null === datagridState.columns) {
                    datagridState.columns = '';
                }
 
                var initialView = this.getRoot().initialView;
                var initialViewExists = null !== initialView && 0 !== initialView.id;
 
                var filtersModified = this.areFiltersModified(initialView.filters, datagridState.filters);
                var columnsModified = !_.isEqual(initialView.columns, datagridState.columns.split(','));
 
                if (initialViewExists) {
                    this.dirtyFilters = filtersModified;
                    this.dirtyColumns = columnsModified;
                } else {
                    var isDefaultFilters = ('' === datagridState.filters);
                    var isDefaultColumns = _.isEqual(this.getRoot().defaultColumns, datagridState.columns.split(','));
 
                    this.dirtyFilters = !isDefaultFilters;
                    this.dirtyColumns = !isDefaultColumns;
                }
 
                this.render();
            },
 
            /**
             * Set the view of this module.
             *
             * @param {Object} view
             */
            setView: function (view) {
                this.datagridView = view;
            },
 
            /**
             * Check if current datagrid state filters are modified regarding the initial view
             *
             * @param {Object} initialViewFilters
             * @param {Object} datagridStateFilters
             *
             * @return {boolean}
             */
            areFiltersModified: function (initialViewFilters, datagridStateFilters) {
                return initialViewFilters !== datagridStateFilters;
            }
        });
    }
);