Skip to content

Commit ec0803a

Browse files
author
Alain Dumesny
committed
define size of dragable elements from a sidebar
* you can now pre-define size of dragable elements from a sidebar using standard `data-gs-width` and `data-gs-height` * updated two.html to show that fix for #413 #914 #918 #922 (and others) and especially #933 which had the issue spelled out by @ermcgrat. Sorry it took so longer to add this!
1 parent ce1386c commit ec0803a

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

demo/two.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ <h1>Two grids demo</h1>
6262
<div class="row">
6363
<div class="col-md-3">
6464
<div class="sidebar">
65-
<div class="grid-stack-item"><div class="grid-stack-item-content">Drag me</div></div>
65+
66+
<!-- will size to fit content -->
67+
<div class="grid-stack-item">
68+
<div class="grid-stack-item-content">Drag me</div>
69+
</div>
70+
<!-- manually force a drop size of 2x1 -->
71+
<div class="grid-stack-item" data-gs-width="2" data-gs-height="1">
72+
<div class="grid-stack-item-content">Drag me 2x1</div>
73+
</div>
74+
6675
</div>
6776
</div>
6877
<div class="col-md-9">

doc/CHANGES.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,20 @@ Change log
3131
- add `compact()` to reclaim any empty space and relayout grid items [#1101](https://github.com/gridstack/gridstack.js/pull/1101)
3232
- add `options.dragOut` to let user drag nested grid items out of a parent or not (default false)
3333
and jQuery UI `draggable.containment` can now be specified in options. You can now drag&drop between 2 nested grids [#1105](https://github.com/gridstack/gridstack.js/pull/1105)
34-
- Allow percentage as a valid unit for height [#1093](https://github.com/gridstack/gridstack.js/pull/1093). thank you @trevisanweb @aureality @ZoolWay
34+
- Allow percentage as a valid unit for height [#1093](https://github.com/gridstack/gridstack.js/pull/1093). thank you
35+
[@trevisanweb](https://github.com/trevisanweb) [@aureality](https://github.com/aureality)
36+
[@ZoolWay](https://github.com/ZoolWay)
3537
- fixed callbacks to get either `added, removed, change` or combination if adding a node require also to change its (x,y) for example.
3638
Also you can now call `batchUpdate()` before calling a bunch of `addWidget()` and get a single event callback (more efficient).
3739
[#1096](https://github.com/gridstack/gridstack.js/pull/1096)
3840
- `removeAll()` is now much faster (no relayout) and calls `removed` event just once with a list [#1097](https://github.com/gridstack/gridstack.js/pull/1097)
3941
- `setColumn()` complete re-write and is no longer "Experimental". We now do a reasonable job at sizing/position the widgets (especially 1 column) and
4042
also now cache each column layout so you can go back to say 12 column and not loose original layout. [#1098](https://github.com/gridstack/gridstack.js/pull/1098)
4143
- fix bug where `addWidget(el)` (no data) would not render item at correct location, and overlap item at (0,0) [#1098](https://github.com/gridstack/gridstack.js/pull/1098)
44+
- you can now pre-define size of dragable elements from a sidebar using standard `data-gs-width` and `data-gs-height` - fix
45+
[#413](https://github.com/gridstack/gridstack.js/issues/413), [#914](https://github.com/gridstack/gridstack.js/issues/914), [#918](https://github.com/gridstack/gridstack.js/issues/918),
46+
[#922](https://github.com/gridstack/gridstack.js/issues/922), [#933](https://github.com/gridstack/gridstack.js/issues/933)
47+
thanks [@ermcgrat](https://github.com/ermcgrat) and others for pointing out code issue.
4248

4349
## v0.5.5 (2019-11-27)
4450

src/gridstack.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@
6262
width = Math.max.apply(Math, widths);
6363
}
6464

65-
dir = dir !== -1 ? 1 : -1;
66-
return Utils.sortBy(nodes, function(n) { return dir * (n.x + n.y * width); });
65+
if (dir === -1)
66+
return Utils.sortBy(nodes, function(n) { return -(n.x + n.y * width); });
67+
else
68+
return Utils.sortBy(nodes, function(n) { return (n.x + n.y * width); });
6769
},
6870

6971
createStylesheet: function(id) {
@@ -942,16 +944,25 @@
942944
}
943945
})
944946
.on(self.container, 'dropover', function(event, ui) {
945-
var offset = self.container.offset();
946947
var el = $(ui.draggable);
947-
var cellWidth = self.cellWidth();
948-
var cellHeight = self.cellHeight();
948+
var width, height;
949+
950+
// see if we already have a node with widget/height and check for attributes
949951
var origNode = el.data('_gridstack_node');
950-
var verticalMargin = self.opts.verticalMargin;
952+
if (!origNode || !origNode.width || !origNode.height) {
953+
var w = parseInt(el.attr('data-gs-width'));
954+
if (w > 0) { origNode = origNode || {}; origNode.width = w; }
955+
var h = parseInt(el.attr('data-gs-height'));
956+
if (h > 0) { origNode = origNode || {}; origNode.height = h; }
957+
}
951958

959+
// if not calculate the grid size based on element outer size
952960
// height: Each row is cellHeight + verticalMargin, until last one which has no margin below
953-
var width = origNode ? origNode.width : Math.ceil(el.outerWidth() / cellWidth);
954-
var height = origNode ? origNode.height : Math.round((el.outerHeight() + verticalMargin) / (cellHeight + verticalMargin));
961+
var cellWidth = self.cellWidth();
962+
var cellHeight = self.cellHeight();
963+
var verticalMargin = self.opts.verticalMargin;
964+
width = origNode && origNode.width ? origNode.width : Math.ceil(el.outerWidth() / cellWidth);
965+
height = origNode && origNode.height ? origNode.height : Math.round((el.outerHeight() + verticalMargin) / (cellHeight + verticalMargin));
955966

956967
draggingElement = el;
957968

0 commit comments

Comments
 (0)