|
| 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> |
0 commit comments