Test Setup Failed
Push — master ( d00a46...d73e35 )
by
unknown
03:25
created

breadcrumbs-navigation-block.js ➔ ... ➔ iterator   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
nop 2
1
define(function(require) {
2
    'use strict';
3
4
    var BreadcrumbsNavigationBlock;
5
    var mediator = require('oroui/js/mediator');
6
    var BaseComponent = require('oroui/js/app/components/base/component');
7
    var $ = require('jquery');
8
    var __ = require('orotranslation/js/translator');
9
10
    BreadcrumbsNavigationBlock = BaseComponent.extend({
11
        /**
12
         * @property
13
         */
14
        $element: null,
15
16
        /**
17
         * {@inheritDoc}
18
         */
19
        initialize: function(options) {
20
            this.$element = options._sourceElement;
21
22
            mediator.on('datagrid_filters:update', $.proxy(this, 'updateFiltersInfo'));
23
            mediator.on('datagrid_filters:update', $.proxy(this, 'updateSortingInfo'));
24
            mediator.on('datagrid_filters:update', $.proxy(this, 'updatePaginationInfo'));
25
26
            BreadcrumbsNavigationBlock.__super__.initialize.apply(this, arguments);
27
        },
28
29
        /**
30
         * {@inheritDoc}
31
         */
32
        dispose: function() {
33
            if (this.disposed) {
34
                return;
35
            }
36
37
            BreadcrumbsNavigationBlock.__super__.dispose.call(this);
38
        },
39
40
        /**
41
         * Updates the components inner content,
42
         * presenting categories path current filters state.
43
         *
44
         * @param {object} datagrid
45
         */
46
        updateFiltersInfo: function(datagrid) {
47
            var currentFilters = [];
48
            var iterator = function(filterName, filterDefinition) {
49
                if (filterDefinition.name === filterName && filterDefinition.visible) {
50
                    var hint = datagrid.filterManager.filters[filterDefinition.name].getState().hint;
51
52
                    currentFilters.push({
53
                        hint: hint,
54
                        label: filterDefinition.label
55
                    });
56
                }
57
            };
58
59
            for (var filterName in datagrid.collection.state.filters) {
60
                if (datagrid.collection.state.filters.hasOwnProperty(filterName)) {
61
                    datagrid.metadata.filters.forEach(iterator.bind(null, filterName));
62
                }
63
            }
64
65
            if (currentFilters.length === 0) {
66
                $('.filters-info', this.$element).html('');
67
68
                return;
69
            }
70
71
            var buildFilterString = function(filter) {
72
                return filter.label + ' ' + filter.hint;
73
            };
74
75
            var filtersStrings = [];
76
77
            currentFilters.forEach(function(filter) {
78
                filtersStrings.push(buildFilterString(filter));
79
            });
80
81
            var filtersString = '[' + filtersStrings.join(', ') + ']';
82
83
            $('.filters-info', this.$element).text(filtersString);
84
        },
85
86
        /**
87
         * Updates the components inner content,
88
         * presenting sorting information.
89
         *
90
         * @param {object} datagrid
91
         */
92
        updateSortingInfo: function(datagrid) {
93
            var info = __('oro.product.grid.navigation_bar.sorting.label');
94
95
            var sorter = datagrid.collection.state.sorters;
96
            var sorterLabel = '';
97
            var sorterDirection = '';
98
99
            for (var k in sorter) {
100
                if (sorter.hasOwnProperty(k)) {
101
                    sorterLabel = k;
102
                    sorterDirection = __('oro.product.grid.navigation_bar.sorting.' + (sorter[k] > 0 ? 'desc' : 'asc'));
103
104
                    break;
105
                }
106
            }
107
108
            info = info.replace('%column%', sorterLabel).replace('%direction%', sorterDirection);
109
110
            $('.sorting-info', this.$element).html(info);
111
        },
112
113
        /**
114
         * Updates the components inner content,
115
         * presenting pagination information.
116
         *
117
         * @param {object} datagrid
118
         */
119
        updatePaginationInfo: function(datagrid) {
120
            var info = __('oro.product.grid.navigation_bar.pagination.label');
121
            var state = datagrid.collection.state;
122
123
            var start = (state.currentPage - 1) * state.pageSize + 1;
124
            var end = state.totalRecords < state.pageSize ? state.totalRecords : (state.currentPage) * state.pageSize;
125
126
            info = info.replace('%start%', start).replace('%end%', end).replace('%total%', state.totalRecords);
127
128
            $('.pagination-info', this.$element).html(info);
129
        }
130
    });
131
132
    return BreadcrumbsNavigationBlock;
133
});
134