Skip to content

Commit e1602ed

Browse files
committed
Merge commit '8faecc91769cd43431524065ea35bac10ee33cb5'
2 parents 68ebd3e + 8faecc9 commit e1602ed

File tree

7 files changed

+237
-53
lines changed

7 files changed

+237
-53
lines changed

demo/two.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@
4242
.grid-stack-item-removing {
4343
opacity: 0.5;
4444
}
45+
46+
.trash {
47+
height: 150px;
48+
margin-bottom: 20px;
49+
background: rgba(255, 0, 0, 0.1) center center url(data:image/svg+xml;utf8;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTYuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjY0cHgiIGhlaWdodD0iNjRweCIgdmlld0JveD0iMCAwIDQzOC41MjkgNDM4LjUyOSIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDM4LjUyOSA0MzguNTI5OyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+CjxnPgoJPGc+CgkJPHBhdGggZD0iTTQxNy42ODksNzUuNjU0Yy0xLjcxMS0xLjcwOS0zLjkwMS0yLjU2OC02LjU2My0yLjU2OGgtODguM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5056

57+
<div class="trash">
58+
</div>
59+
5160
<div class="row">
5261
<div class="col-md-6">
5362
<div class="grid-stack grid-stack-6" id="grid1">
@@ -66,7 +75,8 @@ <h1>Two grids demo</h1>
6675
var options = {
6776
width: 6,
6877
float: true,
69-
removable: true
78+
removable: '.trash',
79+
removeTimeout: 100
7080
};
7181
$('#grid1').gridstack(options);
7282
$('#grid2').gridstack(options);

dist/gridstack.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,30 @@
664664

665665
$(window).resize(this.onResizeHandler);
666666
this.onResizeHandler();
667+
668+
if (typeof self.opts.removable === 'string') {
669+
var trashZone = $(self.opts.removable);
670+
if (!trashZone.data('droppable')) {
671+
trashZone.droppable({});
672+
}
673+
trashZone
674+
.on('dropover', function(event, ui) {
675+
var el = $(ui.draggable);
676+
var node = el.data('_gridstack_node');
677+
if (node._grid !== self) {
678+
return;
679+
}
680+
self._setupRemovingTimeout(el);
681+
})
682+
.on('dropout', function(event, ui) {
683+
var el = $(ui.draggable);
684+
var node = el.data('_gridstack_node');
685+
if (node._grid !== self) {
686+
return;
687+
}
688+
self._clearRemovingTimeout(el);
689+
});
690+
}
667691
};
668692

669693
GridStack.prototype._triggerChangeEvent = function(forceTrigger) {
@@ -799,6 +823,32 @@
799823
this.opts.minWidth;
800824
};
801825

826+
GridStack.prototype._setupRemovingTimeout = function(el) {
827+
var self = this;
828+
var node = $(el).data('_gridstack_node');
829+
830+
if (node._removeTimeout || !self.opts.removable) {
831+
return;
832+
}
833+
node._removeTimeout = setTimeout(function() {
834+
el.addClass('grid-stack-item-removing');
835+
node._isAboutToRemove = true;
836+
}, self.opts.removeTimeout);
837+
};
838+
839+
GridStack.prototype._clearRemovingTimeout = function(el) {
840+
var self = this;
841+
var node = $(el).data('_gridstack_node');
842+
843+
if (!node._removeTimeout) {
844+
return;
845+
}
846+
clearTimeout(node._removeTimeout);
847+
node._removeTimeout = null;
848+
el.removeClass('grid-stack-item-removing');
849+
node._isAboutToRemove = false;
850+
};
851+
802852
GridStack.prototype._prepareElement = function(el, triggerAddEvent) {
803853
triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false;
804854
var self = this;
@@ -819,32 +869,13 @@
819869
noMove: Utils.toBool(el.attr('data-gs-no-move')),
820870
locked: Utils.toBool(el.attr('data-gs-locked')),
821871
el: el,
822-
id: el.attr('data-gs-id')
872+
id: el.attr('data-gs-id'),
873+
_grid: self
823874
}, triggerAddEvent);
824875
el.data('_gridstack_node', node);
825876

826877
var cellWidth;
827878
var cellHeight;
828-
var removeTimeout;
829-
830-
var setupRemovingTimeout = function() {
831-
if (removeTimeout || !self.opts.removable) {
832-
return;
833-
}
834-
removeTimeout = setTimeout(function() {
835-
el.addClass('grid-stack-item-removing');
836-
node._isAboutToRemove = true;
837-
}, self.opts.removeTimeout);
838-
};
839-
var clearRemovingTimeout = function() {
840-
if (!removeTimeout) {
841-
return;
842-
}
843-
clearTimeout(removeTimeout);
844-
removeTimeout = null;
845-
el.removeClass('grid-stack-item-removing');
846-
node._isAboutToRemove = false;
847-
};
848879

849880
var dragOrResize = function(event, ui) {
850881
var x = Math.round(ui.position.left / cellWidth);
@@ -859,7 +890,9 @@
859890

860891
if (event.type == 'drag') {
861892
if (x < 0 || x >= self.grid.width || y < 0) {
862-
setupRemovingTimeout();
893+
if (self.opts.removable === true) {
894+
self._setupRemovingTimeout(el);
895+
}
863896

864897
x = node._beforeDragX;
865898
y = node._beforeDragY;
@@ -871,7 +904,7 @@
871904

872905
node._temporaryRemoved = true;
873906
} else {
874-
clearRemovingTimeout();
907+
self._clearRemovingTimeout(el);
875908

876909
if (node._temporaryRemoved) {
877910
self.grid.addNode(node);
@@ -937,7 +970,7 @@
937970
el.removeData('_gridstack_node');
938971
el.remove();
939972
} else {
940-
clearRemovingTimeout();
973+
self._clearRemovingTimeout(el);
941974
if (!node._temporaryRemoved) {
942975
o
943976
.attr('data-gs-x', node.x)

dist/gridstack.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/gridstack.min.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ gridstack.js API
7777
- `placeholderClass` - class for placeholder (default: `'grid-stack-placeholder'`)
7878
- `placeholderText` - placeholder default content (default: `''`)
7979
- `resizable` - allows to override jQuery UI resizable options. (default: `{autoHide: true, handles: 'se'}`)
80-
- `removable` - if `true` widgets could be removed by dragging outside of the grid (default: `false`)
80+
- `removable` - if `true` widgets could be removed by dragging outside of the grid. It could also be a jQuery selector string, in this case widgets will be removed by dropping them there (default: `false`) See [example](http://troolee.github.io/gridstack.js/demo/two.html)
8181
- `removeTimeout` - time in milliseconds before widget is being removed while dragging outside of the grid. (default: `2000`)
8282
- `rtl` - if `true` turns grid to RTL. Possible values are `true`, `false`, `'auto'` (default: `'auto'`) See [example](http://troolee.github.io/gridstack.js/demo/rtl.html)
8383
- `staticGrid` - makes grid static (default `false`). If true widgets are not movable/resizable. You don't even need jQueryUI draggable/resizable. A CSS class `grid-stack-static` is also added to the container.

spec/gridstack-engine-spec.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ describe('gridstack engine', function() {
108108
});
109109
});
110110

111+
describe('test batchUpdate/commit', function () {
112+
var engine;
113+
114+
beforeAll(function () {
115+
engine = new GridStackUI.Engine(12)
116+
});
117+
118+
it('should work on not float grids', function () {
119+
expect(engine.float).toEqual(false);
120+
engine.batchUpdate();
121+
expect(engine._updateCounter).toBeGreaterThan(0);
122+
expect(engine.float).toEqual(true);
123+
engine.commit();
124+
expect(engine._updateCounter).toEqual(0);
125+
expect(engine.float).toEqual(false);
126+
});
127+
});
128+
129+
describe('test batchUpdate/commit', function () {
130+
var engine;
131+
132+
beforeAll(function () {
133+
engine = new GridStackUI.Engine(12, null, true)
134+
});
135+
136+
it('should work on float grids', function () {
137+
expect(engine.float).toEqual(true);
138+
engine.batchUpdate();
139+
expect(engine._updateCounter).toBeGreaterThan(0);
140+
expect(engine.float).toEqual(true);
141+
engine.commit();
142+
expect(engine._updateCounter).toEqual(0);
143+
expect(engine.float).toEqual(true);
144+
});
145+
});
146+
111147
describe('test _notify', function() {
112148
var engine;
113149
var spy;
@@ -157,4 +193,76 @@ describe('gridstack engine', function() {
157193
]);
158194
});
159195
});
196+
197+
describe('test _packNodes', function () {
198+
describe('using not float mode', function () {
199+
var engine;
200+
201+
var findNode = function (engine, id) {
202+
return _.find(engine.nodes, function(i) { return i._id === id });
203+
}
204+
205+
beforeEach(function () {
206+
engine = new GridStackUI.Engine(12, null, false);
207+
});
208+
209+
it('shouldn\'t pack one node with y coord eq 0', function () {
210+
engine.nodes = [
211+
{x: 0, y: 0, width: 1, height: 1, _id: 1},
212+
];
213+
214+
engine._packNodes();
215+
216+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1}));
217+
expect(findNode(engine, 1)._dirty).toBeFalsy();
218+
});
219+
220+
it('should pack one node correctly', function () {
221+
engine.nodes = [
222+
{x: 0, y: 1, width: 1, height: 1, _id: 1},
223+
];
224+
225+
engine._packNodes();
226+
227+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true}));
228+
});
229+
230+
it('should pack nodes correctly', function () {
231+
engine.nodes = [
232+
{x: 0, y: 1, width: 1, height: 1, _id: 1},
233+
{x: 0, y: 5, width: 1, height: 1, _id: 2},
234+
];
235+
236+
engine._packNodes();
237+
238+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true}));
239+
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true}));
240+
});
241+
242+
it('should pack nodes correctly', function () {
243+
engine.nodes = [
244+
{x: 0, y: 5, width: 1, height: 1, _id: 1},
245+
{x: 0, y: 1, width: 1, height: 1, _id: 2},
246+
];
247+
248+
engine._packNodes();
249+
250+
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true}));
251+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true}));
252+
});
253+
254+
it('should respect locked nodes', function () {
255+
engine.nodes = [
256+
{x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true},
257+
{x: 0, y: 5, width: 1, height: 1, _id: 2},
258+
];
259+
260+
engine._packNodes();
261+
262+
expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1}));
263+
expect(findNode(engine, 1)._dirty).toBeFalsy();
264+
expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 2, width: 1, height: 1, _dirty: true}));
265+
});
266+
});
267+
});
160268
});

0 commit comments

Comments
 (0)