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

45% Statements 27/60
17.86% Branches 10/56
57.14% Functions 4/7
46.55% Lines 27/58

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225  554x                     554x                                                                                                                             2415x   2415x       2415x 2415x 2415x 2415x   2415x   2415x                                                               4x   4x       4x 4x   4x   4x 4x 4x 4x                   4x 4x 4x                                                                                                                       228x   228x   228x           228x       228x        
/* global define */
define(['jquery', 'underscore', 'backbone', 'pim/form'],
function($, _, Backbone, BaseForm) {
    'use strict';
 
    /**
     * Datagrid pagination widget
     *
     * @export  oro/datagrid/pagination
     * @class   oro.datagrid.Pagination
     * @extends BaseForm
     */
    return BaseForm.extend({
        /** @property */
        tagName: 'div',
 
        /** @property */
        className: 'AknGridToolbar-center',
 
        /** @property */
        windowSize: 10,
 
        /** @property */
        enabled: true,
 
        /** @property */
        hidden: false,
 
        /** @property */
        template: _.template(
            '<label>Page:</label>' +
            '<ul class="AknPagination-icons">' +
                '<% _.each(handles, function (handle) { %>' +
                    '<li class="AknPagination-item <% if (handle.className) { %><%= handle.className %><% } %>">' +
                        '<a href="#" class="AknPagination-link" <% if (handle.title) {%> title="<%= handle.title %>"<% } %>>' +
                            '<% if (handle.wrapClass) {%>' +
                                '<i <% if (handle.wrapClass) { %>class="<%= handle.wrapClass %>"<% } %>>' +
                                    '<%= handle.label %>' +
                                '</i>' +
                            '<% } else { %>' +
                                '<%= handle.label %>' +
                            '<% } %>' +
                        '</a>' +
                    '</li>' +
                '<% }); %>' +
            '</ul>' +
            '<label>of <%= state.totalPages ? state.totalPages : 1 %> | <%= state.totalRecords %> records</label>'
        ),
 
        /** @property */
        events: {
            "click a": "onChangePage"
        },
 
        /** @property */
        fastForwardHandleConfig: {
            prev: {
                label: '',
                wrapClass: 'icon-chevron-left hide-text'
            },
            next: {
                label: '',
                wrapClass: 'icon-chevron-right hide-text'
            }
        },
 
        /**
         * Initializer.
         *
         * @param {Object} options
         * @param {Backbone.Collection} options.collection
         * @param {Object} options.fastForwardHandleConfig
         * @param {Number} options.windowSize
         */
        initialize: function (options) {
            options = options || {};
 
            Iif (!options.collection) {
                throw new TypeError("'collection' is required");
            }
 
            this.collection = options.collection;
            this.listenTo(this.collection, "add", this.render);
            this.listenTo(this.collection, "remove", this.render);
            this.listenTo(this.collection, "reset", this.render);
 
            this.hidden = options.hide == true;
 
            BaseForm.prototype.initialize.call(this, options);
        },
 
        /**
         * Disable pagination
         *
         * @return {*}
         */
        disable: function() {
            this.enabled = false;
            this.render();
            return this;
        },
 
        /**
         * Enable pagination
         *
         * @return {*}
         */
        enable: function() {
            this.enabled = true;
            this.render();
            return this;
        },
 
        /**
         * jQuery event handler for the page handlers. Goes to the right page upon clicking.
         *
         * @param {Event} e
         * @protected
         */
        onChangePage: function (e) {
            e.preventDefault();
 
            Iif (!this.enabled) {
                return;
            }
 
            const label = $(e.target).text();
            const ffConfig = this.fastForwardHandleConfig;
 
            const collection = this.collection;
 
            Eif (ffConfig) {
                const prevLabel = _.has(ffConfig.prev, 'label') ? ffConfig.prev.label : undefined;
                const nextLabel = _.has(ffConfig.next, 'label') ? ffConfig.next.label : undefined;
                switch (label) {
                    case prevLabel:
                        if (collection.hasPrevious()) collection.getPreviousPage();
                        return;
                    case nextLabel:
                        if (collection.hasNext()) collection.getNextPage();
                        return;
                }
            }
 
            const state = collection.state;
            const pageIndex = label * 1 - state.firstPage;
            collection.getPage(state.firstPage === 0 ? pageIndex : pageIndex + 1);
        },
 
        /**
         * Internal method to create a list of page handle objects for the template
         * to render them.
         *
         * @return {Array.<Object>} an array of page handle objects hashes
         */
        makeHandles: function (handles) {
            handles = handles || [];
 
            var collection = this.collection;
            var state = collection.state;
 
            // convert all indices to 0-based here
            var lastPage = state.lastPage ? state.lastPage : state.firstPage;
            lastPage = state.firstPage === 0 ? lastPage : lastPage - 1;
            var currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1;
            var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
            var windowEnd = windowStart + this.windowSize;
            windowEnd = windowEnd <= lastPage ? windowEnd : lastPage + 1;
 
            if (collection.mode !== "infinite") {
                for (var i = windowStart; i < windowEnd; i++) {
                    handles.push({
                        label: i + 1,
                        title: "No. " + (i + 1),
                        className: currentPage === i ? "active" : undefined
                    });
                }
            }
 
            var ffConfig = this.fastForwardHandleConfig || {};
 
            if (ffConfig.prev) {
                handles.unshift({
                    label: _.has(ffConfig.prev, 'label') ? ffConfig.prev.label : undefined,
                    wrapClass: _.has(ffConfig.prev, 'wrapClass') ? ffConfig.prev.wrapClass : undefined,
                    className: collection.hasPrevious() ? undefined : 'AknActionButton--disabled'
                });
            }
 
            if (ffConfig.next) {
                handles.push({
                    label: _.has(ffConfig.next, 'label') ? ffConfig.next.label : undefined,
                    wrapClass: _.has(ffConfig.next, 'wrapClass') ? ffConfig.next.wrapClass : undefined,
                    className: collection.hasNext() ? void 0 : 'AknActionButton--disabled'
                });
            }
 
            return handles;
        },
 
        /**
         * Render pagination
         *
         * @return {*}
         */
        renderPagination: function() {
            this.$el.empty();
 
            var state = this.collection.state;
 
            this.$el.append($(this.template({
                disabled: !this.enabled || !state.totalRecords,
                handles: this.makeHandles(),
                state: state
            })));
 
            Iif (this.hidden) {
                this.$el.hide();
            }
 
            return this;
        }
    });
});