Skip to content

Commit 425ef52

Browse files
author
Tobias Wolff
committed
adding size-changed event
1 parent 71b4ce3 commit 425ef52

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
@@ -1826,8 +1826,8 @@
18261826
/**
18271827
* GridsterItem directive
18281828
*/
1829-
.directive('gridsterItem', ['$parse', 'GridsterDraggable', 'GridsterResizable',
1830-
function($parse, GridsterDraggable, GridsterResizable) {
1829+
.directive('gridsterItem', ['$parse', '$rootScope', 'GridsterDraggable', 'GridsterResizable',
1830+
function($parse, $rootScope, GridsterDraggable, GridsterResizable) {
18311831
return {
18321832
restrict: 'EA',
18331833
controller: 'GridsterItemCtrl',
@@ -1931,6 +1931,8 @@
19311931
item.gridster.moveOverlappingItems(item);
19321932
gridster.layoutChanged();
19331933
}
1934+
1935+
$rootScope.$broadcast('gridster-item-size-changed', item.sizeX, item.sizeY);
19341936
}
19351937
scope.$watch(function() {
19361938
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)