Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 486fb3f

Browse files
authored
Merge pull request #1734 from adumesny/master
fix sizing from top/left
2 parents f3f81b5 + a13fad9 commit 486fb3f

File tree

8 files changed

+34
-32
lines changed

8 files changed

+34
-32
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,11 @@ you can init gridstack like:
264264
```js
265265
GridStack.init({
266266
resizable: {
267-
handles: 'e, se, s, sw, w'
267+
handles: 'e,se,s,sw,w'
268268
}
269269
});
270270
```
271271

272-
Note: It's not recommended to enable `nw`, `n`, `ne` resizing handles. Their behavior may be unexpected.
273-
274272
## Touch devices support
275273

276274
gridstack v3.2 jq version compiles touch support (html5 version does not yet support `touchmove` events. This will be added in a future release), so it works out of the box, no need for anything.

demo/float.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ <h1>Float grid demo</h1>
2222
</div>
2323
<script src="events.js"></script>
2424
<script type="text/javascript">
25-
let grid = GridStack.init({float: true});
25+
let grid = GridStack.init({
26+
float: true,
27+
resizable: { handles: 'all'} // do all sides for testing
28+
});
2629
addEvents(grid);
2730

2831
let items = [

demo/mobile.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</head>
1313
<body>
1414
<h1>Simple mobile demo</h1>
15-
<p>uses v3.2+ jquery version which now includes jquery.ui.touch-punch by default (small 2k)</p>
15+
<p>uses gridstack-jq.js which includes jquery.ui.touch-punch by default (minimum v3.2+, small 2k)</p>
1616
<div class="grid-stack"></div>
1717
<script type="text/javascript">
1818
let grid = GridStack.init({

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Change log
5959
- fix [#1700](https://github.com/gridstack/gridstack.js/issues/1700) JQ nested grid drag fix broken in 4.0.3 (but much older underlying issue)
6060
- fix [#1678](https://github.com/gridstack/gridstack.js/issues/1678) item gs-x:0 not animating fix
6161
- fix [#1678](https://github.com/gridstack/gridstack.js/pull/1678) resize-scroll issue when grid is not at top of page. Thanks [@Manfred-on-github](https://github.com/Manfred-on-github)
62+
- fix [#1728](https://github.com/gridstack/gridstack.js/issues/1728) fix sizing from top/left sides
6263

6364
## 4.2.0 (2021-4-11)
6465

doc/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ gridstack.js API
9797
* example `dragInOptions: { revert: 'invalid', scroll: false, appendTo: 'body', helper: 'clone', handle: '.grid-stack-item-content' }`
9898
* **Note**: if you have multiple grids, it's best to call `GridStack.setupDragIn()` with same params as it only need to be done once.
9999
* **Note2**: instead of 'clone' you can also pass your own function (get passed the event).
100-
- `draggable` - allows to override jQuery UI draggable options. (default: `{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body', containment: null}`)
100+
- `draggable` - allows to override draggable options. (default: `{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body', containment: null}`)
101101
- `dragOut` to let user drag nested grid items out of a parent or not (default false) See [example](http://gridstackjs.com/demo/nested.html)
102102
- `float` - enable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
103103
- `handle` - draggable handle selector (default: `'.grid-stack-item-content'`)
@@ -116,12 +116,12 @@ gridstack.js API
116116
- `oneColumnModeDomSort` - set to `true` if you want oneColumnMode to use the DOM order and ignore x,y from normal multi column layouts during sorting. This enables you to have custom 1 column layout that differ from the rest. (default?: `false`)
117117
- `placeholderClass` - class for placeholder (default: `'grid-stack-placeholder'`)
118118
- `placeholderText` - placeholder default content (default: `''`)
119-
- `resizable` - allows to override jQuery UI resizable options. (default: `{autoHide: true, handles: 'se'}`)
120-
- `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://gridstackjs.com/demo/two.html)
119+
- `resizable` - allows to override resizable options. (default: `{autoHide: true, handles: 'se'}`). `handles` can be any combo of `n,ne,e,se,s,sw,w,nw` or `all`.
120+
- `removable` - if `true` widgets could be removed by dragging outside of the grid. It could also be a selector string, in this case widgets will be removed by dropping them there (default: `false`) See [example](http://gridstackjs.com/demo/two.html)
121121
- `removeTimeout` - time in milliseconds before widget is being removed while dragging outside of the grid. (default: `2000`)
122122
- `row` - fix grid number of rows. This is a shortcut of writing `minRow:N, maxRow:N`. (default `0` no constrain)
123123
- `rtl` - if `true` turns grid to RTL. Possible values are `true`, `false`, `'auto'` (default: `'auto'`) See [example](http://gridstackjs.com/demo/rtl.html)
124-
- `staticGrid` - removes drag&drop&resize (default `false`). If `true` widgets are not movable/resizable by the user, but code can still move and oneColumnMode will still work. You don't even need jQueryUI draggable/resizable. A CSS class `grid-stack-static` is also added to the container.
124+
- `staticGrid` - removes drag|drop|resize (default `false`). If `true` widgets are not movable/resizable by the user, but code can still move and oneColumnMode will still work. You can use the smaller gridstack-static.js lib. A CSS class `grid-stack-static` is also added to the container.
125125
- `styleInHead` - if `true` will add style element to `<head>` otherwise will add it to element's parent node (default `false`).
126126

127127
## Grid attributes
@@ -144,7 +144,7 @@ The widget can still be dragged or resized by the user.
144144
You need to add `noResize` and `noMove` attributes to completely lock the widget.
145145
- `noResize` - disable element resizing
146146
- `noMove` - disable element moving
147-
- `resizeHandles` - sets resize handles for a specific widget.
147+
- `resizeHandles` - widgets can have their own custom resize handles. For example 'e,w' will make that particular widget only resize east and west. See `resizable: {handles: string}` option
148148
- `id`- (number | string) good for quick identification (for example in change event)
149149
- `content` - (string) html content to be added when calling `grid.load()/addWidget()` as content inside the item
150150
- `subGrid`: GridStackOptions - optional nested grid options and list of children
@@ -161,7 +161,7 @@ that is affected.
161161
You can call it on a single event name, or space separated list like:
162162
`grid.on('added removed change', ...)`
163163

164-
The Typescript `GridStackEvent` list all possible values, and nothing else is supported by the `grid.on()` method, though it's possible to register directly for other events generated by the drag&drop plugging implementation detail (currently jquery-ui based so you can still use `$(".grid-stack").on(...)`).
164+
The Typescript `GridStackEvent` list all possible values, and nothing else is supported by the `grid.on()` method, though it's possible to register directly for other events generated by the drag&drop plugging implementation detail (if jquery-ui based you can still use `$(".grid-stack").on(...)`).
165165

166166
### added(event, items)
167167

src/gridstack-dd.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,12 @@ GridStack.prototype._dragOrResize = function(el: GridItemHTMLElement, event: Eve
565565
if (node.w === p.w && node.h === p.h) return;
566566
if (node._lastTried && node._lastTried.w === p.w && node._lastTried.h === p.h) return; // skip one we tried (but failed)
567567

568-
// if we changed sizing on left side, move the item as well. Note: we don't support TOP resizing
569-
if (Math.round(ui.position.left) < Math.round(node._orig.x * cellWidth)) { // use round or we can get slightly off compare
570-
p.x = node._orig.x + node._orig.w - p.w;
571-
}
568+
// if we size on left/top side this might move us, so get possible new position as well
569+
let left = ui.position.left + this.opts.marginLeft;
570+
let top = ui.position.top + this.opts.marginTop;
571+
p.x = Math.round(left / cellWidth);
572+
p.y = Math.round(top / cellHeight);
573+
572574
resizing = true;
573575
}
574576

src/h5/dd-resizable.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,37 +234,35 @@ export class DDResizable extends DDBaseImplement implements HTMLElementExtendOpt
234234
const offsetY = event.clientY - oEvent.clientY;
235235

236236
if (dir.indexOf('e') > -1) {
237-
newRect.width += event.clientX - oEvent.clientX;
238-
}
239-
if (dir.indexOf('s') > -1) {
240-
newRect.height += event.clientY - oEvent.clientY;
241-
}
242-
if (dir.indexOf('w') > -1) {
237+
newRect.width += offsetX;
238+
} else if (dir.indexOf('w') > -1) {
243239
newRect.width -= offsetX;
244240
newRect.left += offsetX;
245241
}
246-
if (dir.indexOf('n') > -1) {
242+
if (dir.indexOf('s') > -1) {
243+
newRect.height += offsetY;
244+
} else if (dir.indexOf('n') > -1) {
247245
newRect.height -= offsetY;
248246
newRect.top += offsetY
249247
}
250-
const reshape = this._getReShapeSize(newRect.width, newRect.height);
251-
if (newRect.width !== reshape.width) {
248+
const constrain = this._constrainSize(newRect.width, newRect.height);
249+
if (Math.round(newRect.width) !== Math.round(constrain.width)) { // round to ignore slight round-off errors
252250
if (dir.indexOf('w') > -1) {
253-
newRect.left += newRect.width - reshape.width;
251+
newRect.left += newRect.width - constrain.width;
254252
}
255-
newRect.width = reshape.width;
253+
newRect.width = constrain.width;
256254
}
257-
if (newRect.height !== reshape.height) {
255+
if (Math.round(newRect.height) !== Math.round(constrain.height)) {
258256
if (dir.indexOf('n') > -1) {
259-
newRect.top += newRect.height - reshape.height;
257+
newRect.top += newRect.height - constrain.height;
260258
}
261-
newRect.height = reshape.height;
259+
newRect.height = constrain.height;
262260
}
263261
return newRect;
264262
}
265263

266-
/** @internal */
267-
private _getReShapeSize(oWidth: number, oHeight: number): Size {
264+
/** @internal constrain the size to the set min/max values */
265+
private _constrainSize(oWidth: number, oHeight: number): Size {
268266
const maxWidth = this.option.maxWidth || Number.MAX_SAFE_INTEGER;
269267
const minWidth = this.option.minWidth || oWidth;
270268
const maxHeight = this.option.maxHeight || Number.MAX_SAFE_INTEGER;

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export interface GridStackWidget extends GridStackPosition {
243243
noMove?: boolean;
244244
/** prevents moving and resizing (default?: undefined = un-constrained) */
245245
locked?: boolean;
246-
/** widgets can have their own resize handles. For example 'e,w' will make the particular widget only resize east and west. */
246+
/** widgets can have their own custom resize handles. For example 'e,w' will make that particular widget only resize east and west. See `resizable: {handles: string}` option */
247247
resizeHandles?: string;
248248
/** value for `gs-id` stored on the widget (default?: undefined) */
249249
id?: numberOrString;

0 commit comments

Comments
 (0)