Skip to content

Commit ef97cca

Browse files
committed
Merge pull request #535 from maciaszczykm/card-space-limit
Limit number of labels visible by default
2 parents d31587a + 9c706b1 commit ef97cca

File tree

4 files changed

+129
-3
lines changed

4 files changed

+129
-3
lines changed

src/app/frontend/common/components/labels/labels.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
limitations under the License.
1515
-->
1616

17-
<div flex="95" class="kd-labels" ng-repeat="(key, value) in ::labelsCtrl.labels">
18-
<kd-middle-ellipsis display-string="{{::key}}: {{::value}}">
19-
</kd-middle-ellipsis>
17+
<div flex="95" class="kd-labels" ng-repeat="(key, value) in ::labelsCtrl.labels"
18+
ng-if="labelsCtrl.isVisible($index)">
19+
<kd-middle-ellipsis display-string="{{::key}}: {{::value}}"></kd-middle-ellipsis>
20+
</div>
21+
<div class="kd-labels kd-labels-switch" ng-show="labelsCtrl.isMoreAvailable()"
22+
ng-click="labelsCtrl.switchLabelsView()">
23+
{{labelsCtrl.isShowingAll() ? "show less labels" : "show all labels"}}
2024
</div>

src/app/frontend/common/components/labels/labels.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ $label-margin: 0 $baseline-grid ($baseline-grid / 2) 0;
2626
padding: ($baseline-grid / 4) $baseline-grid;
2727
vertical-align: middle;
2828
}
29+
30+
.kd-labels-switch {
31+
color: $primary;
32+
cursor: pointer;
33+
outline: none;
34+
}

src/app/frontend/common/components/labels/labels_controller.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/**
16+
* Number of labels, that are always visible.
17+
* @type {number}
18+
*/
19+
const alwaysVisibleLabelsNumber = 8;
20+
1521
/**
1622
* @final
1723
*/
@@ -23,5 +29,41 @@ export default class LabelsController {
2329
constructor() {
2430
/** @export {!Object<string, string>} Initialized from the scope. */
2531
this.labels;
32+
33+
/** @private {boolean} */
34+
this.isShowingAllLabels_ = false;
2635
}
36+
37+
/**
38+
* Returns true if element at index position should be visible.
39+
*
40+
* @param index
41+
* @return {boolean}
42+
* @export
43+
*/
44+
isVisible(index) { return this.isShowingAllLabels_ || index < alwaysVisibleLabelsNumber; }
45+
46+
/**
47+
* Returns true if more labels than alwaysVisibleLabelsNumber is available.
48+
*
49+
* @return {boolean}
50+
* @export
51+
*/
52+
isMoreAvailable() { return Object.keys(this.labels).length > alwaysVisibleLabelsNumber; }
53+
54+
/**
55+
* Returns true if all labels are showed.
56+
*
57+
* @return {boolean}
58+
* @export
59+
*/
60+
isShowingAll() { return this.isShowingAllLabels_; }
61+
62+
/**
63+
* Switches labels view between two states, which are showing only alwaysVisibleLabelsNumber of
64+
* labels and showing all labels.
65+
*
66+
* @export
67+
*/
68+
switchLabelsView() { this.isShowingAllLabels_ = !this.isShowingAllLabels_; }
2769
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import LabelsController from 'common/components/labels/labels_controller';
16+
import componentsModule from 'common/components/components_module';
17+
18+
describe('Labels controller', () => {
19+
/**
20+
* @type {!LabelsController}
21+
*/
22+
let ctrl;
23+
24+
beforeEach(() => {
25+
angular.mock.module(componentsModule.name);
26+
angular.mock.inject(($controller) => { ctrl = $controller(LabelsController); });
27+
});
28+
29+
it('should display correct number of labels', () => {
30+
// given
31+
ctrl.labels = {
32+
'label-1': 'value-1',
33+
'label-2': 'value-2',
34+
'label-3': 'value-3',
35+
'label-4': 'value-4',
36+
'label-5': 'value-5',
37+
'label-6': 'value-6',
38+
'label-7': 'value-7',
39+
'label-8': 'value-8',
40+
'label-9': 'value-9',
41+
'label-10': 'value-10',
42+
};
43+
44+
expect(ctrl.isMoreAvailable()).toBe(true);
45+
expect(ctrl.isShowingAll()).toBe(false);
46+
expect(ctrl.isVisible(0)).toBe(true);
47+
expect(ctrl.isVisible(7)).toBe(true);
48+
expect(ctrl.isVisible(8)).toBe(false);
49+
50+
// when
51+
ctrl.switchLabelsView();
52+
53+
// then
54+
expect(ctrl.isShowingAll()).toBe(true);
55+
expect(ctrl.isVisible(0)).toBe(true);
56+
expect(ctrl.isVisible(7)).toBe(true);
57+
expect(ctrl.isVisible(8)).toBe(true);
58+
});
59+
60+
it('should not display switch if there are less than 8 labels', () => {
61+
// given
62+
ctrl.labels = {
63+
'label-1': 'value-1',
64+
'label-2': 'value-2',
65+
'label-3': 'value-3',
66+
'label-4': 'value-4',
67+
'label-5': 'value-5',
68+
'label-6': 'value-6',
69+
};
70+
71+
// then
72+
expect(ctrl.isMoreAvailable()).toBe(false);
73+
});
74+
});

0 commit comments

Comments
 (0)