Skip to content

Commit d0bc03d

Browse files
Portugal, Marcelomportuga
Portugal, Marcelo
authored andcommitted
fix(5007): Reduced the amount of digest cycles initiated by the grid.
Part 1: Reduce digest cylces in ui-grid-utils #5007
1 parent 721d0ec commit d0bc03d

File tree

2 files changed

+73
-25
lines changed

2 files changed

+73
-25
lines changed

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,7 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
619619
},
620620

621621
isNullOrUndefined: function(obj) {
622-
if (obj === undefined || obj === null) {
623-
return true;
624-
}
625-
return false;
622+
return (obj === undefined || obj === null);
626623
},
627624

628625
endsWith: function(str, suffix) {
@@ -831,7 +828,7 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
831828
} else {
832829
s.logWarn('[focus.byId] Element id ' + elementID + ' was not found.');
833830
}
834-
});
831+
}, 0, false);
835832
this.queue.push(promise);
836833
return promise;
837834
},
@@ -856,7 +853,7 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
856853
if (element){
857854
element[0].focus();
858855
}
859-
});
856+
}, 0, false);
860857
this.queue.push(promise);
861858
return promise;
862859
},
@@ -886,8 +883,8 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
886883
};
887884
this._purgeQueue();
888885
if (aSync){ //Do this asynchronysly
889-
var promise = $timeout(focusBySelector);
890-
this.queue.push($timeout(focusBySelector));
886+
var promise = $timeout(focusBySelector, 0, false);
887+
this.queue.push(promise);
891888
return promise;
892889
} else {
893890
return focusBySelector();

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

+68-17
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@ describe('ui.grid.utilService', function() {
66
$q,
77
$interpolateProvider;
88

9-
beforeEach(module('ui.grid', function (_$interpolateProvider_) {
10-
$interpolateProvider = _$interpolateProvider_;
11-
}));
12-
13-
beforeEach(inject(function(_$rootScope_, _$q_, _gridUtil_, _$window_, _Grid_) {
14-
gridUtil = _gridUtil_;
15-
$window = _$window_;
16-
Grid = _Grid_;
17-
$rootScope = _$rootScope_;
18-
$q = _$q_;
19-
}));
9+
beforeEach(function() {
10+
module('ui.grid', function (_$interpolateProvider_) {
11+
$interpolateProvider = _$interpolateProvider_;
12+
});
13+
14+
inject(function(_$rootScope_, _$q_, _gridUtil_, _$window_, _Grid_) {
15+
gridUtil = _gridUtil_;
16+
$window = _$window_;
17+
Grid = _Grid_;
18+
$rootScope = _$rootScope_;
19+
$q = _$q_;
20+
});
21+
});
2022

2123
describe('newId()', function() {
2224
it('creates a unique id each time it is called', function() {
23-
var id1 = gridUtil.newId();
24-
var id2 = gridUtil.newId();
25+
var id1 = gridUtil.newId(),
26+
id2 = gridUtil.newId();
2527

2628
expect(id1).not.toEqual(id2);
2729
});
@@ -532,12 +534,14 @@ describe('ui.grid.utilService', function() {
532534
/* Create Button1 */
533535
button1 = document.createElement('button');
534536
aButton1 = angular.element(button1);
537+
aButton1.attr('id', 'aButton1');
535538
aButton1.attr('type', 'button');
536539
// The class is not set here because it is set inside of tests if needed
537540

538541
/* Create Button2 */
539542
button2 = document.createElement('button');
540543
aButton2 = angular.element(button1);
544+
aButton2.attr('id', 'aButton2');
541545
aButton2.attr('type', 'button');
542546
aButton2.addClass(button2class);
543547

@@ -557,14 +561,61 @@ describe('ui.grid.utilService', function() {
557561
expect(element.innerHTML).toEqual(document.activeElement.innerHTML);
558562
}
559563

564+
describe('byId', function() {
565+
describe('when the grid is not defined', function() {
566+
it('should focus on the element with the id passed', function(){
567+
gridUtil.focus.byId('aButton2');
568+
$timeout.flush();
569+
570+
expectFocused(button2);
571+
});
572+
});
573+
describe('when the grid is defined', function() {
574+
it('should focus on the element with the grid id and the id passed', function(){
575+
var gridId = 'gridId';
576+
577+
aButton2.attr('id', 'gridId-aButton2');
578+
gridUtil.focus.byId('aButton2', {id: gridId});
579+
$timeout.flush();
580+
581+
expectFocused(button2);
582+
});
583+
});
584+
describe('when the id passed in is not in the dom', function() {
585+
it('should keep focus on the body', function() {
586+
gridUtil.focus.byId('notAnElement');
587+
$timeout.flush();
588+
589+
expectFocused(document.body);
590+
});
591+
});
592+
});
560593
describe('byElement', function(){
561-
it('should focus on the element passed', function(){
562-
gridUtil.focus.byElement(button1);
563-
$timeout.flush();
564-
expectFocused(button1);
594+
describe('when argument passed is an element', function() {
595+
it('should focus on the element passed', function(){
596+
gridUtil.focus.byElement(button1);
597+
$timeout.flush();
598+
expectFocused(button1);
599+
});
600+
});
601+
describe('when argument passed is not an element', function() {
602+
it('should keep focus on the body', function(){
603+
gridUtil.focus.byElement('');
604+
expectFocused(document.body);
605+
});
565606
});
566607
});
567608
describe('bySelector', function(){
609+
describe('when argument passed is not an element', function() {
610+
it('should throw an error', function(done){
611+
try {
612+
gridUtil.focus.bySelector('');
613+
} catch (error) {
614+
expect(error.message).toEqual('The parent element is not an element.');
615+
done();
616+
}
617+
});
618+
});
568619
it('should focus on an elment using a selector', function(){
569620
gridUtil.focus.bySelector(elm, '.' + button2class);
570621
$timeout.flush();

0 commit comments

Comments
 (0)