Skip to content

Commit f25090f

Browse files
committed
Merge pull request #242 from twolff-iow/master
adding size-changed event
2 parents e650ad2 + 425ef52 commit f25090f

2 files changed

Lines changed: 45 additions & 20 deletions

File tree

src/angular-gridster.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,8 +1838,8 @@
18381838
* @param GridsterDraggable
18391839
* @param GridsterResizable
18401840
*/
1841-
.directive('gridsterItem', ['$parse', 'GridsterDraggable', 'GridsterResizable',
1842-
function($parse, GridsterDraggable, GridsterResizable) {
1841+
.directive('gridsterItem', ['$parse', '$rootScope', 'GridsterDraggable', 'GridsterResizable',
1842+
function($parse, $rootScope, GridsterDraggable, GridsterResizable) {
18431843
return {
18441844
restrict: 'EA',
18451845
controller: 'GridsterItemCtrl',
@@ -1958,6 +1958,8 @@
19581958
item.gridster.moveOverlappingItems(item);
19591959
gridster.layoutChanged();
19601960
}
1961+
1962+
$rootScope.$broadcast('gridster-item-size-changed', item.sizeX, item.sizeY);
19611963
}
19621964
scope.$watch(function() {
19631965
return item.sizeY + ',' + item.sizeX + ',' + item.minSizeX + ',' + item.maxSizeX + ',' + item.minSizeY + ',' + item.maxSizeY;

test/spec/gridster-directive.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe('gridster directive', function() {
1010
var startCount;
1111
var resizeCount;
1212
var stopCount;
13+
var rootScope;
14+
var broadcastOnRootScope;
1315

1416
var dragHelper = function(el, dx, dy) {
1517
el.simulate('mouseover').simulate('drag', {
@@ -20,6 +22,9 @@ describe('gridster directive', function() {
2022
};
2123

2224
beforeEach(inject(function($rootScope, $compile) {
25+
rootScope = $rootScope;
26+
broadcastOnRootScope = spyOn(rootScope, '$broadcast').and.callThrough();
27+
2328
$scope = $rootScope.$new();
2429
startCount = resizeCount = stopCount = 0;
2530

@@ -89,31 +94,49 @@ describe('gridster directive', function() {
8994

9095
it('should initialize resizable', function() {
9196
var $widget = $el.find('li:first-child');
92-
setTimeout(function() {
93-
expect($widget.find('.handle-n').length).toBe(1);
94-
});
97+
98+
expect($widget.find('.handle-n').length).toBe(1);
9599
});
96100

97101
it('should update widget dimensions on resize & trigger custom resize events', function() {
102+
var $widget = $el.find('li:first-child');
103+
var handle = $widget.find('.handle-e');
98104

99-
setTimeout(function() {
100-
var $widget = $el.find('li:first-child');
101-
var handle = $widget.find('.handle-e');
105+
expect($widget.width()).toBe(155);
106+
expect($scope.dashboard.widgets[0].sizeX).toBe(1);
107+
expect(startCount).toBe(0);
108+
expect(resizeCount).toBe(0);
109+
expect(stopCount).toBe(0);
102110

103-
expect($widget.width()).toBe(155);
104-
expect($scope.dashboard.widgets[0].sizeX).toBe(1);
105-
expect(startCount).toBe(0);
106-
expect(resizeCount).toBe(0);
107-
expect(stopCount).toBe(0);
111+
dragHelper(handle, 50); // should resize to next width step
108112

109-
dragHelper(handle, 50); // should resize to next width step
113+
expect($widget.width()).toBe(320);
114+
expect($scope.dashboard.widgets[0].sizeX).toBe(2);
115+
expect(startCount).toBe(1);
116+
expect(resizeCount).toBe(1);
117+
expect(stopCount).toBe(1);
118+
});
110119

111-
expect($widget.width()).toBe(320);
112-
expect($scope.dashboard.widgets[0].sizeX).toBe(2);
113-
expect(startCount).toBe(1);
114-
expect(resizeCount).toBe(1);
115-
expect(stopCount).toBe(1);
116-
});
120+
it('should broadcast "size changed" event on resize', function() {
121+
// arrange
122+
var eHandle = $el.find('li:first-child').find('.handle-e');
123+
var sHandle = $el.find('li:first-child').find('.handle-s');
124+
broadcastOnRootScope.calls.reset();
125+
126+
// act
127+
dragHelper(eHandle, 50);
128+
129+
// assert
130+
expect(broadcastOnRootScope).toHaveBeenCalledWith('gridster-item-size-changed', 2, 1);
131+
132+
// arrange
133+
broadcastOnRootScope.calls.reset();
134+
135+
// act
136+
dragHelper(sHandle, 0, 50);
137+
138+
// assert
139+
expect(broadcastOnRootScope).toHaveBeenCalledWith('gridster-item-size-changed', 2, 2);
117140
});
118141

119142
});

0 commit comments

Comments
 (0)