Skip to content

Commit e61614d

Browse files
ENGCOM-7210: {ASI} : SortBy component added #26736
- Merge Pull Request #26736 from konarshankar07/magento2:Sortby-component - Merged commits: 1. d913e15 2. 4e65dc9 3. a483274 4. dd95f5e 5. bdfe729 6. 9404f53 7. 858756d 8. 7dc8227 9. 08aea80 10. a1b3c47 11. 6aaebb2 12. 9aa1cfc 13. a66fb6c 14. 5010692 15. aa09e38 16. 1f5bed9 17. 31926d6 18. 76c357b
2 parents 1116437 + 76c357b commit e61614d

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'uiElement'
7+
], function (Element) {
8+
'use strict';
9+
10+
return Element.extend({
11+
defaults: {
12+
template: 'ui/grid/sortBy',
13+
options: [],
14+
applied: {},
15+
sorting: 'asc',
16+
columnsProvider: 'ns = ${ $.ns }, componentType = columns',
17+
selectedOption: '',
18+
isVisible: true,
19+
listens: {
20+
'selectedOption': 'applyChanges'
21+
},
22+
statefull: {
23+
selectedOption: true,
24+
applied: true
25+
},
26+
exports: {
27+
applied: '${ $.provider }:params.sorting'
28+
},
29+
imports: {
30+
preparedOptions: '${ $.columnsProvider }:elems'
31+
},
32+
modules: {
33+
columns: '${ $.columnsProvider }'
34+
}
35+
},
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
initObservable: function () {
41+
return this._super()
42+
.observe([
43+
'applied',
44+
'selectedOption',
45+
'isVisible'
46+
]);
47+
},
48+
49+
/**
50+
* Prepared sort order options
51+
*/
52+
preparedOptions: function (columns) {
53+
if (columns && columns.length > 0) {
54+
columns.map(function (column) {
55+
if (column.sortable === true) {
56+
this.options.push({
57+
value: column.index,
58+
label: column.label
59+
});
60+
this.isVisible(true);
61+
} else {
62+
this.isVisible(false);
63+
}
64+
}.bind(this));
65+
}
66+
},
67+
68+
/**
69+
* Apply changes
70+
*/
71+
applyChanges: function () {
72+
this.applied({
73+
field: this.selectedOption(),
74+
direction: this.sorting
75+
});
76+
}
77+
});
78+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<div if="isVisible" class="masonry-image-sortby">
8+
<b><!-- ko i18n: 'Sort by' --><!-- /ko -->:</b>
9+
<select class="admin__control-select" data-bind="
10+
options: options,
11+
optionsValue: 'value',
12+
optionsText: 'label',
13+
value: selectedOption
14+
"></select>
15+
</div>

app/design/adminhtml/Magento/backend/Magento_Ui/web/css/source/module/_masonry-grid.less

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,16 @@
114114
}
115115
}
116116
}
117+
118+
&-sortby {
119+
select {
120+
background-image: url(../images/arrows-bg.svg);
121+
border: none;
122+
padding-right: 3.2rem;
123+
&:active {
124+
background-image+: url('../images/arrows-bg.svg');
125+
}
126+
}
127+
}
117128
}
118129
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/*eslint max-nested-callbacks: 0*/
7+
define([
8+
'Magento_Ui/js/grid/sortBy'
9+
], function (SortBy) {
10+
'use strict';
11+
12+
describe('Magento_Ui/js/grid/sortBy', function () {
13+
14+
var sortByObj;
15+
16+
beforeEach(function () {
17+
sortByObj = new SortBy({
18+
options: []
19+
});
20+
});
21+
22+
describe('"preparedOptions" method', function () {
23+
it('sort option will not available if sorting is disabled for the columns', function () {
24+
var columns = {
25+
sortable: false,
26+
label: 'magento',
27+
index: 'name'
28+
};
29+
30+
sortByObj.preparedOptions([columns]);
31+
expect(sortByObj.options[0]).toBeUndefined();
32+
expect(sortByObj.options[0]).toBeUndefined();
33+
});
34+
35+
it('sort option will available if sorting is enabled for the columns', function () {
36+
var columns = {
37+
sortable: true,
38+
label: 'magento',
39+
index: 'name'
40+
};
41+
42+
sortByObj.preparedOptions([columns]);
43+
expect(sortByObj.options[0].value).toEqual('name');
44+
expect(sortByObj.options[0].label).toEqual('magento');
45+
});
46+
47+
it('return "isVisible" method true if sorting is enabled for column', function () {
48+
var columns = {
49+
sortable: true,
50+
label: 'magento',
51+
index: 'name'
52+
};
53+
54+
sortByObj.preparedOptions([columns]);
55+
expect(sortByObj.isVisible()).toBeTruthy();
56+
});
57+
58+
it('return "isVisible" method false if sorting is disabled for column', function () {
59+
var columns = {
60+
sortable: false,
61+
label: 'magento',
62+
index: 'name'
63+
};
64+
65+
sortByObj.preparedOptions([columns]);
66+
expect(sortByObj.isVisible()).toBeFalsy();
67+
});
68+
});
69+
describe('"applyChanges" method', function () {
70+
it('return applied options for sorting column', function () {
71+
var applied = {
72+
field: 'selectedOption',
73+
direction: 'asc'
74+
};
75+
76+
spyOn(sortByObj, 'selectedOption').and.returnValue('selectedOption');
77+
sortByObj.applyChanges();
78+
expect(sortByObj.applied()).toEqual(applied);
79+
expect(sortByObj.selectedOption).toHaveBeenCalled();
80+
});
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)