All files / web/bundles/pimdatagrid/js/datagrid pagination-input.js

93.65% Statements 59/63
71.05% Branches 27/38
100% Functions 7/7
93.55% Lines 58/62

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                  554x                                       554x                                             911x 911x 911x   911x       911x 185x     911x 911x                 2983x   2415x 2415x   2415x                   228x   228x 228x 228x   228x 228x   228x 661x 20x           661x 661x             228x                       228x             2643x 2643x   2643x 2643x 2643x 2643x 2643x 2643x 2643x 2643x   2643x 2643x 866x     2643x 2643x     2643x             4x   4x       4x             2415x 2187x   228x   228x 228x 228x             228x 17x       2415x       554x 185x         554x      
/* global define */
 
/**
 * Datagrid pagination with input field
 *
 * @export  oro/datagrid/pagination-input
 * @class   oro.datagrid.PaginationInput
 * @extends oro.datagrid.Pagination
 */
define(
    [
        'jquery',
        'oro/mediator',
        'underscore',
        'oro/translator',
        'oro/datagrid/pagination',
        'pim/template/datagrid/pagination',
        'oro/messenger'
    ], function(
        $,
        mediator,
        _,
        __,
        Pagination,
        template,
        Messenger
    ) {
    'use strict';
 
    const PaginationInput = Pagination.extend({
        collection: {},
 
        /** @property */
        template: _.template(template),
 
        /** @property */
        windowSize: 3,
 
        /** @property */
        fastForwardHandleConfig: {
            gap: {
                label: '...'
            }
        },
 
        /** @property */
        maxRescoreWindow: 10000,
 
        /**
         * @inheritDoc
         */
        initialize: function (options) {
            this.appendToGrid = options.appendToGrid;
            this.gridElement = options.gridElement;
            this.gridName = options.config.gridName;
 
            Iif (null === this.gridName || undefined === this.gridName) {
                throw Error('You must set the gridName in the form_extensions config for the oro/datagrid/pagination-input');
            }
 
            if (this.appendToGrid) {
                mediator.on('datagrid_collection_set_after', this.setupPagination.bind(this));
            }
 
            mediator.once('grid_load:start', this.setupPagination.bind(this));
            mediator.on('grid_load:complete', this.setupPagination.bind(this));
        },
 
        /**
         * Initialize the pagination with the collection
         *
         * @param collection
         */
        setupPagination(collection) {
            if (collection.inputName !== this.gridName) return;
 
            this.collection = collection;
            this.renderPagination();
 
            return Pagination.prototype.initialize.call(this, {
                collection: this.collection,
                enabled: true
            });
        },
 
        /**
         * {@inheritdoc}
         */
        makeHandles: function () {
            let handles = [];
 
            const state = this.collection.state;
            const currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
            const pageIds = this.getPages();
 
            Eif (this.collection.mode !== 'infinite') {
                let previousId = _.first(pageIds);
 
                pageIds.forEach((id) => {
                    if (id - previousId > 1) {
                        handles.push({
                            label: this.fastForwardHandleConfig.gap.label,
                            title: this.fastForwardHandleConfig.gap.label,
                            className: 'AknActionButton--unclickable'
                        });
                    }
                    previousId = id;
                    handles.push({
                        label: id + 1,
                        title: 'No. ' + (id + 1),
                        className: currentPage === id ? 'active AknActionButton--highlight' : undefined
                    });
                });
 
                Iif (
                    state.totalRecords > this.maxRescoreWindow &&
                    (previousId + 1) * state.pageSize < this.maxRescoreWindow
                ) {
                    handles.push({
                        label: this.fastForwardHandleConfig.gap.label,
                        title: this.fastForwardHandleConfig.gap.label,
                        className: 'AknActionButton--unclickable'
                    });
                }
            }
 
            return handles;
        },
 
        /**
         * Returns the list of pages to display
         */
        getPages() {
            const collection = this.collection;
            const state = collection.state;
 
            let lastPage = state.lastPage ? state.lastPage : state.firstPage;
            lastPage = state.firstPage === 0 ? lastPage : lastPage - 1;
            const lastAccessiblePage = Math.floor(this.maxRescoreWindow / state.pageSize);
            const currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
            let windowStart = currentPage - (this.windowSize - 1) / 2;
            windowStart = Math.max(Math.min(windowStart, lastPage - this.windowSize + 1), 0);
            const windowEnd = Math.min(windowStart + this.windowSize, lastPage, lastAccessiblePage);
            let ids = [];
 
            ids.push(state.firstPage - 1);
            for (let i = windowStart; i < windowEnd; i++) {
                ids.push(i);
            }
 
            Eif (state.totalRecords < this.maxRescoreWindow) {
                ids.push(lastPage);
            }
 
            return _.uniq(ids);
        },
 
        /**
         * {@inheritdoc}
         */
        onChangePage: function (e) {
            const label = $(e.target).text().trim();
 
            Iif (label === this.fastForwardHandleConfig.gap.label) {
                return false;
            }
 
            return Pagination.prototype.onChangePage.apply(this, arguments);
        },
 
        /**
         * Render pagination view and add validation for input with positive integer value
         */
        renderPagination: function() {
            if (this.getPages().length <= 1) {
                this.$el.empty();
            } else {
                Pagination.prototype.renderPagination.apply(this, arguments);
 
                const state = this.collection.state;
                const currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
                Iif ((currentPage + 1) === Math.floor(this.maxRescoreWindow / state.pageSize)) {
                    Messenger.notify(
                        'warning',
                        __('oro.datagrid.pagination.limit_warning', { limit: this.maxRescoreWindow }),
                    );
                }
 
                if (this.options.appendToGrid) {
                    this.gridElement.prepend(this.$el);
                }
            }
 
            return this;
        }
    });
 
    PaginationInput.init = function(gridContainer, gridName) {
        return new PaginationInput({ appendToGrid: true, gridElement: $(gridContainer).find('.grid-container'), config: {
            gridName
        } });
    };
 
    return PaginationInput;
 
});