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

Skip to content

Commit b4f8966

Browse files
authored
Merge pull request gridstack#1932 from adumesny/master
add `GridStack.registerEngine()`
2 parents 4359dcc + e92f96d commit b4f8966

File tree

7 files changed

+121
-8
lines changed

7 files changed

+121
-8
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Join us on Slack: https://gridstackjs.troolee.com
3333
- [Requirements](#requirements)
3434
- [API Documentation](#api-documentation)
3535
- [Extend Library](#extend-library)
36+
- [Extend Engine](#extend-engine)
3637
- [Change grid columns](#change-grid-columns)
3738
- [Custom columns CSS](#custom-columns-css)
3839
- [Override resizable/draggable options](#override-resizabledraggable-options)
@@ -186,6 +187,27 @@ let grid = GridStack.init();
186187
grid.printCount();
187188
```
188189

190+
## Extend Engine
191+
192+
You can now (5.1+) easily create your own layout engine to further customize you usage. Here is a typescript example
193+
194+
```ts
195+
import { GridStack, GridStackEngine, GridStackNod, GridStackMoveOpts } from 'gridstack';
196+
197+
class CustomEngine extends GridStackEngine {
198+
199+
/** refined this to move the node to the given new location */
200+
public moveNode(node: GridStackNode, o: GridStackMoveOpts): boolean {
201+
// keep the same original X and Width and let base do it all...
202+
o.x = node.x;
203+
o.w = node.w;
204+
return super.moveNode(node, o);
205+
}
206+
}
207+
208+
GridStack.registerEngine(CustomEngine); // globally set our custom class
209+
```
210+
189211
## Change grid columns
190212

191213
GridStack makes it very easy if you need [1-12] columns out of the box (default is 12), but you always need **2 things** if you need to customize this:

demo/custom-engine.html

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Custom Engine</title>
8+
9+
<link rel="stylesheet" href="demo.css"/>
10+
<!-- <script src="../dist/gridstack-h5.js"></script> -->
11+
<script src="events.js"></script>
12+
</head>
13+
<body>
14+
<div class="container-fluid">
15+
<h1>Custom Engine</h1>
16+
<p>shows a custom engine subclass in Typescript that only allows objects to move vertically.</p>
17+
<div>
18+
<a class="btn btn-primary" onClick="addNewWidget()" href="#">Add Widget</a>
19+
<a class="btn btn-primary" onclick="toggleFloat()" id="float" href="#">float: true</a>
20+
</div>
21+
<br><br>
22+
<div class="grid-stack"></div>
23+
</div>
24+
25+
<script type="module" > // so we can use import
26+
// get CORS error in Chrome...need to have http://localhost/ URL - see https://stackoverflow.com/questions/50197495/javascript-modules-and-cors
27+
import { GridStack, GridStackEngine } from '../dist/gridstack-h5.js';
28+
29+
/**
30+
* Custom engine class that only allows vertical movement and resizing
31+
*/
32+
class CustomEngine extends GridStackEngine {
33+
/** refined this to move the node to the given new location */
34+
moveNode(node, o) {
35+
// keep the same original X and Width and let base do it all...
36+
o.x = node.x;
37+
o.w = node.w;
38+
return super.moveNode(node, o);
39+
}
40+
}
41+
GridStack.registerEngine(CustomEngine); // globally set our custom class
42+
43+
let count = 0;
44+
let items = [
45+
{x: 0, y: 0},
46+
{x: 1, y: 0},
47+
{x: 1, y: 2, w: 3},
48+
];
49+
items.forEach(n => n.content = String(count++));
50+
51+
let grid = GridStack.init({
52+
float: true,
53+
disableOneColumnMode: true,
54+
cellHeight: 70
55+
}).load(items);
56+
addEvents(grid);
57+
58+
let addNewWidget = function() {
59+
let n = items[count] || {
60+
x: Math.round(12 * Math.random()),
61+
y: Math.round(5 * Math.random()),
62+
w: Math.round(1 + 3 * Math.random()),
63+
h: Math.round(1 + 3 * Math.random())
64+
};
65+
n.content = n.content || String(count++);
66+
grid.addWidget(n);
67+
};
68+
69+
let toggleFloat = function() {
70+
grid.float(! grid.getFloat());
71+
document.querySelector('#float').innerHTML = 'float: ' + grid.getFloat();
72+
};
73+
</script>
74+
</body>
75+
</html>

doc/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Change log
6969

7070

7171
## 5.0.0-dev (TBD)
72+
* add `GridStack.registerEngine()` to let user use their own custom layout engine subclass. Thank you [Thomas] for sponsoring it.
7273

7374
## 5.0.0 (2022-01-10)
7475
* add [#992](https://github.com/gridstack/gridstack.js/issues/992) support dragging into and out of nested grids from parents! Thank you [@arclogos132](https://github.com/arclogos132) for sponsoring it.

doc/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ gridstack.js API
2727
- [`initAll(options: GridStackOptions = {}, selector = '.grid-stack'): GridStack[]`](#initalloptions-gridstackoptions---selector--grid-stack-gridstack)
2828
- [`addGrid(parent: HTMLElement, opt: GridStackOptions = {}): GridStack `](#addgridparent-htmlelement-opt-gridstackoptions---gridstack-)
2929
- [`setupDragIn(dragIn?: string, dragInOptions?: DDDragInOpt)`](#setupdragindragin-string-draginoptions-dddraginopt)
30+
- [`GridStack.registerEngine(engineClass: typeof GridStackEngine)`](#gridstackregisterengineengineclass-typeof-gridstackengine)
3031
- [API](#api)
3132
- [`addWidget(el?: GridStackWidget | GridStackElement, options?: GridStackWidget)`](#addwidgetel-gridstackwidget--gridstackelement-options-gridstackwidget)
3233
- [`batchUpdate()`](#batchupdate)
@@ -100,6 +101,7 @@ gridstack.js API
100101
* **Note2**: instead of 'clone' you can also pass your own function (get passed the event).
101102
- `draggable` - allows to override draggable options. (default: `{handle: '.grid-stack-item-content', scroll: false, appendTo: 'body', containment: null}`)
102103
- `dragOut` to let user drag nested grid items out of a parent or not (default false) See [example](http://gridstackjs.com/demo/nested.html)
104+
- `engineClass` - the type of engine to create (so you can subclass) default to GridStackEngine
103105
- `float` - enable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
104106
- `handle` - draggable handle selector (default: `'.grid-stack-item-content'`)
105107
- `handleClass` - draggable handle class (e.g. `'grid-stack-item-content'`). If set `handle` is ignored (default: `null`)
@@ -321,6 +323,10 @@ Called during `GridStack.init()` as options, but can also be called directly (la
321323
* @param dragInOptions options - see `DDDragInOpt`. (default: `{revert: 'invalid', handle: '.grid-stack-item-content', scroll: false, appendTo: 'body'}`
322324
but you will probably also want `helper: 'clone'` or your own callback function).
323325

326+
327+
### `GridStack.registerEngine(engineClass: typeof GridStackEngine)`
328+
329+
* call to specify global custom engine subclass - see instead `GridStackOptions.engineClass` if you only need to replace just one instance.
324330
## API
325331

326332
### `addWidget(el?: GridStackWidget | GridStackElement, options?: GridStackWidget)`

src/gridstack-engine.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -622,14 +622,8 @@ export class GridStackEngine {
622622
if (Utils.samePos(node, o)) return false;
623623
let prevPos: GridStackPosition = Utils.copyPos({}, node);
624624

625-
// during while() collisions make sure to check entire row so larger items don't leap frog small ones (push them all down)
626-
let area = nn;
627-
// if (this._useEntireRowArea(node, nn)) {
628-
// area = {x: 0, w: this.column, y: nn.y, h: nn.h};
629-
// }
630-
631625
// check if we will need to fix collision at our new location
632-
let collides = this.collideAll(node, area, o.skip);
626+
let collides = this.collideAll(node, nn, o.skip);
633627
let needToMove = true;
634628
if (collides.length) {
635629
// now check to make sure we actually collided over 50% surface area while dragging

src/gridstack.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ export class GridStack {
179179
return grid;
180180
}
181181

182+
/** call this method to register your engine instead of the default one.
183+
* See instead `GridStackOptions.engineClass` if you only need to
184+
* replace just one instance.
185+
*/
186+
static registerEngine(engineClass: typeof GridStackEngine) {
187+
GridStack.engineClass = engineClass;
188+
}
189+
182190
/** scoping so users can call GridStack.Utils.sort() for example */
183191
public static Utils = Utils;
184192

@@ -194,6 +202,8 @@ export class GridStack {
194202
/** grid options - public for classes to access, but use methods to modify! */
195203
public opts: GridStackOptions;
196204

205+
protected static engineClass: typeof GridStackEngine;
206+
197207
/** @internal create placeholder DIV as needed */
198208
public get placeholder(): HTMLElement {
199209
if (!this._placeholder) {
@@ -321,7 +331,8 @@ export class GridStack {
321331

322332
this._setStaticClass();
323333

324-
this.engine = new GridStackEngine({
334+
let engineClass = this.opts.engineClass || GridStack.engineClass || GridStackEngine;
335+
this.engine = new engineClass({
325336
column: this.getColumn(),
326337
float: this.opts.float,
327338
maxRow: this.opts.maxRow,

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { GridStack } from './gridstack';
7+
import { GridStackEngine } from './gridstack-engine';
78

89

910
/** different layout options when changing # of columns,
@@ -105,6 +106,9 @@ export interface GridStackOptions {
105106
/** let user drag nested grid items out of a parent or not (default false) */
106107
dragOut?: boolean;
107108

109+
/** the type of engine to create (so you can subclass) default to GridStackEngine */
110+
engineClass?: typeof GridStackEngine;
111+
108112
/** enable floating widgets (default?: false) See example (http://gridstack.github.io/gridstack.js/demo/float.html) */
109113
float?: boolean;
110114

0 commit comments

Comments
 (0)