Skip to content

Commit 1cbf673

Browse files
committed
Merge pull request #2064 from mathiasdose/Issue#2042
Fix #2042 (pinning): Pinning column menu item duplication on resize
2 parents 944db59 + c92f8fe commit 1cbf673

File tree

6 files changed

+104
-23
lines changed

6 files changed

+104
-23
lines changed

misc/demo/grid-directive.html

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<link href="/dist/release/ui-grid.css" rel="stylesheet">
1111

1212
<!--<script src="https://code.jquery.com/jquery-1.11.1.js"></script>-->
13-
<script src="/lib/test/angular/1.2.21/angular.js"></script>
13+
<script src="/lib/test/angular/1.2.26/angular.js"></script>
1414
<script src="/dist/release/ui-grid.js"></script>
1515

1616
<style>
@@ -19,8 +19,8 @@
1919
min-height: 600px;
2020
}
2121
.grid {
22-
height: 50%;
23-
width: auto;
22+
width: 500px;
23+
height: 400px;
2424
}
2525
.placeholder {
2626
height: 50%;
@@ -35,29 +35,36 @@
3535

3636
<!-- <div class="row main"> -->
3737
<h2>Grid</h2>
38-
<div ui-grid="gridOptions" class="grid" ui-grid-selection></div>
38+
<div ui-grid="gridOptions" class="grid" ui-grid-pinning ui-grid-resize-columns></div>
3939
<!-- <div class="placeholder"> -->
4040
<!-- </div> -->
4141

4242
<br>
4343
<br>
4444

4545
<script>
46-
var app = angular.module('test', ['ui.grid', 'ui.grid.selection']);
46+
var app = angular.module('test', ['ui.grid', 'ui.grid.pinning', 'ui.grid.resizeColumns']);
4747
app.controller('Main', function($scope, $http) {
48-
// $scope.gridOptions = { data: 'data' };
49-
$scope.gridOptions = {
50-
enableFiltering: true
51-
};
48+
$scope.gridOptions = {};
5249
$scope.gridOptions.columnDefs = [
53-
{
54-
name : "Name",
55-
field: "name",
56-
cellTemplate: '/misc/demo/cellTemplate.html'
57-
}
50+
{ name:'id', width:50 },
51+
{ name:'name', width:100, pinnedLeft:true },
52+
{ name:'age', width:100, pinnedRight:true },
53+
{ name:'address.street', width:150 },
54+
{ name:'address.city', width:150 },
55+
{ name:'address.state', width:50 },
56+
{ name:'address.zip', width:50 },
57+
{ name:'company', width:100 },
58+
{ name:'email', width:100 },
59+
{ name:'phone', width:200 },
60+
{ name:'about', width:300 },
61+
{ name:'friends[0].name', displayName:'1st friend', width:150 },
62+
{ name:'friends[1].name', displayName:'2nd friend', width:150 },
63+
{ name:'friends[2].name', displayName:'3rd friend', width:150 }
5864
];
59-
$http.get('/misc/site/data/100.json')
60-
.success(function (data) {
65+
66+
$http.get('https://rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
67+
.success(function(data) {
6168
$scope.gridOptions.data = data;
6269
});
6370
});

misc/tutorial/401_AllFeatures.ngdoc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ All features are enabled to get an idea of performance
8383
</file>
8484
<file name="index.html">
8585
<div ng-controller="MainCtrl">
86-
<button type="button" class="btn btn-success" ng-click="refreshData()">Refresh Data</button> <strong>Calls Pending:</strong> <span ng-bind="callsPending"></span>
86+
<button id="refreshButton" type="button" class="btn btn-success" ng-click="refreshData()">Refresh Data</button> <strong>Calls Pending:</strong> <span ng-bind="callsPending"></span>
8787
<br>
8888
<br>
8989
<strong>{{ myData.length }} rows</strong>
9090
<br>
91-
<div ui-grid="gridOptions" ui-grid-cellNav ui-grid-edit ui-grid-resize-columns ui-grid-pinning ui-grid-selection class="grid"></div>
91+
<div id="grid1" ui-grid="gridOptions" ui-grid-cellNav ui-grid-edit ui-grid-resize-columns ui-grid-pinning ui-grid-selection class="grid"></div>
9292
</div>
9393
</file>
9494
<file name="main.css">
@@ -97,4 +97,15 @@ All features are enabled to get an idea of performance
9797
height: 500px;
9898
}
9999
</file>
100+
<file name="scenario.js">
101+
var gridTestUtils = require('../../test/e2e/gridTestUtils.spec.js');
102+
103+
describe('a grid with all features', function () {
104+
it('should not duplicate the menu options for pinning when resizing a column', function () {
105+
element( by.id('refreshButton') ).click();
106+
gridTestUtils.resizeHeaderCell( 'grid1', 1 );
107+
gridTestUtils.expectVisibleColumnMenuItems( 'grid1', 1, 5)
108+
});
109+
});
110+
</file>
100111
</example>

src/features/pinning/js/pinning.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,15 @@
181181
}
182182
};
183183

184-
col.menuItems.push(pinColumnLeftAction);
185-
col.menuItems.push(pinColumnRightAction);
186-
col.menuItems.push(removePinAction);
184+
if (!gridUtil.arrayContainsObjectWithProperty(col.menuItems, 'title', 'Pin Left')) {
185+
col.menuItems.push(pinColumnLeftAction);
186+
}
187+
if (!gridUtil.arrayContainsObjectWithProperty(col.menuItems, 'title', 'Pin Right')) {
188+
col.menuItems.push(pinColumnRightAction);
189+
}
190+
if (!gridUtil.arrayContainsObjectWithProperty(col.menuItems, 'title', 'Unpin')) {
191+
col.menuItems.push(removePinAction);
192+
}
187193
}
188194
};
189195

src/js/core/services/ui-grid-util.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,16 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
600600
return str.indexOf(suffix, str.length - suffix.length) !== -1;
601601
},
602602

603+
arrayContainsObjectWithProperty: function(array, propertyName, propertyValue) {
604+
var found = false;
605+
angular.forEach(array, function (object) {
606+
if (object[propertyName] === propertyValue) {
607+
found = true;
608+
}
609+
});
610+
return found;
611+
},
612+
603613
// Shim requestAnimationFrame
604614
requestAnimationFrame: $window.requestAnimationFrame && $window.requestAnimationFrame.bind($window) ||
605615
$window.webkitRequestAnimationFrame && $window.webkitRequestAnimationFrame.bind($window) ||

test/e2e/gridTestUtils.spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,37 @@ module.exports = {
293293
headerCell.click();
294294
},
295295

296+
/**
297+
* @ngdoc method
298+
* @methodOf ui.grid.e2eTestLibrary.api:gridTest
299+
* @name resizeHeaderCell
300+
* @description Drags the left resizer border towards the column menu button,
301+
* which will perform a column resizing.
302+
* @param {string} gridId the id of the grid that you want to adjust
303+
* @param {integer} colNumber the number of the column (within the visible columns)
304+
* which left resizer border you wish to drag (this will increase the size of colNumber-1).
305+
*
306+
* @example
307+
* <pre>
308+
* gridTestUtils.resizeHeaderCell('myGrid', 1);
309+
* </pre>
310+
*
311+
*/
312+
resizeHeaderCell: function( gridId, colNumber ) {
313+
var headerCell = this.headerCell(gridId, colNumber);
314+
315+
var resizer = headerCell.all( by.css( '.ui-grid-column-resizer' )).first();
316+
var menuButton = headerCell.element( by.css( '.ui-grid-column-menu-button' ));
317+
318+
protractor.getInstance().actions()
319+
.mouseDown(resizer)
320+
.mouseMove(menuButton)
321+
.mouseUp()
322+
.perform();
323+
324+
},
325+
326+
296327

297328
/**
298329
* @ngdoc method
@@ -312,13 +343,13 @@ module.exports = {
312343
*/
313344
shiftClickHeaderCell: function( gridId, colNumber ) {
314345
var headerCell = this.headerCell( gridId, colNumber);
315-
346+
316347
protractor.getInstance().actions()
317348
.keyDown(protractor.Key.SHIFT)
318349
.click(headerCell)
319350
.keyUp(protractor.Key.SHIFT)
320351
.perform();
321-
},
352+
},
322353

323354
/**
324355
* @ngdoc method

test/unit/core/services/ui-grid-util.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,22 @@ describe('ui.grid.utilService', function() {
404404
});
405405
});
406406

407+
describe('arrayContainsObjectWithProperty', function () {
408+
it('should return true if the array contains an object with the provided key and value, and false if not', function () {
409+
var array = [
410+
{ name: 'Jonah', age: 12},
411+
{ name: 'Alice', age: 33},
412+
{ name: 'Joseph', age: 22}
413+
];
414+
415+
var contains = gridUtil.arrayContainsObjectWithProperty(array, 'name', 'Alice');
416+
var notContains = gridUtil.arrayContainsObjectWithProperty(array, 'name', 'Mathias');
417+
expect(contains).toEqual(true);
418+
expect(notContains).toEqual(false);
419+
});
420+
});
421+
422+
407423
describe('postProcessTemplate', function () {
408424
it('should return unmodified template when interpolation symbols are the default values ( {{ / }} )', function () {
409425
var tmpl;

0 commit comments

Comments
 (0)