Skip to content

Commit be7f0d6

Browse files
authored
Merge pull request #1324 from adumesny/typescript
TS v2.0 release candidate
2 parents 1ab30b4 + ab9c283 commit be7f0d6

File tree

11 files changed

+105
-90
lines changed

11 files changed

+105
-90
lines changed

Gruntfile.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = function(grunt) {
22
grunt.loadNpmTasks('grunt-sass');
3-
// grunt.loadNpmTasks('grunt-contrib-cssmin');
3+
grunt.loadNpmTasks('grunt-contrib-cssmin');
44
grunt.loadNpmTasks('grunt-contrib-copy');
55
grunt.loadNpmTasks('grunt-contrib-uglify');
66
grunt.loadNpmTasks('grunt-eslint');
@@ -24,7 +24,6 @@ module.exports = function(grunt) {
2424
}
2525
}
2626
},
27-
/* very little gain
2827
cssmin: {
2928
dist: {
3029
options: {
@@ -37,7 +36,6 @@ module.exports = function(grunt) {
3736
}
3837
}
3938
},
40-
*/
4139
copy: {
4240
dist: {
4341
files: {
@@ -58,8 +56,8 @@ module.exports = function(grunt) {
5856
},
5957
dist: {
6058
files: {
61-
'dist/jq/jquery.min.js': 'src/jq/jquery.js',
62-
'dist/jq/jquery-ui.min.js': 'src/jq/jquery-ui.js',
59+
'dist/jq/jquery.js': 'src/jq/jquery.js',
60+
'dist/jq/jquery-ui.js': 'src/jq/jquery-ui.js',
6361
/*
6462
'dist/jq/gridstack-dd-jqueryui.min.js': 'dist/jq/gridstack-dd-jqueryui.js',
6563
'dist/gridstack-dd.min.js': 'dist/gridstack-dd.js',
@@ -84,7 +82,7 @@ module.exports = function(grunt) {
8482
},
8583
styles: {
8684
files: ['src/*.scss'],
87-
tasks: ['sass'/*, 'cssmin'*/],
85+
tasks: ['sass', 'cssmin'],
8886
options: {
8987
},
9088
}
@@ -118,6 +116,6 @@ module.exports = function(grunt) {
118116
});
119117

120118
grunt.registerTask('lint', ['eslint']);
121-
grunt.registerTask('default', ['sass', /*'cssmin', 'eslint',*/ 'copy', 'uglify']);
119+
grunt.registerTask('default', ['sass', 'cssmin', /*'eslint',*/ 'copy', 'uglify']);
122120
grunt.registerTask('e2e-test', ['connect', 'protractor_webdriver', 'protractor']);
123121
};

README.md

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ Join us on Slack: https://gridstackjs.troolee.com
3737
- [Custom columns CSS](#custom-columns-css)
3838
- [Override resizable/draggable options](#override-resizabledraggable-options)
3939
- [Touch devices support](#touch-devices-support)
40-
- [Migrating to v0.6.x](#migrating-to-v06x)
41-
- [Migrating to v1.0.0](#migrating-to-v100)
40+
- [Migrating to v0.6](#migrating-to-v06)
41+
- [Migrating to v1](#migrating-to-v1)
4242
- [jQuery Application](#jquery-application)
43-
- [Migrating to v2.0.0](#migrating-to-v200)
43+
- [Migrating to v2](#migrating-to-v2)
4444
- [Changes](#changes)
4545
- [The Team](#the-team)
4646

@@ -70,44 +70,66 @@ npm install --save gridstack
7070

7171
## Include
7272

73-
after you install:
73+
ES6 or Typescript
74+
75+
```js
76+
import { GridStack } from 'gridstack';
77+
import 'gridstack/dist/gridstack.css';
78+
```
79+
80+
legacy javascript
7481

7582
```js
7683
import 'gridstack/dist/gridstack.all.js';
7784
import 'gridstack/dist/gridstack.css';
7885
```
79-
* alternatively in html
86+
87+
alternatively in html
8088

8189
```html
8290
<link rel="stylesheet" href="node_modules/gridstack/dist/gridstack.min.css" />
8391
<script src="node_modules/gridstack/dist/gridstack.all.js"></script>
8492
```
8593

86-
* or using CDN (minimized):
94+
or using CDN (minimized):
8795

8896
```html
89-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gridstack@1.2.0/dist/gridstack.min.css" />
90-
<script src="https://cdn.jsdelivr.net/npm/gridstack@1.2.0/dist/gridstack.all.js"></script>
97+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gridstack@2.0.0-rc/dist/gridstack.min.css" />
98+
<script src="https://cdn.jsdelivr.net/npm/gridstack@2.0.0-rc/dist/gridstack.all.js"></script>
9199
```
92100

93-
if you need to debug, look at the git demo/ examples for non min includes.
101+
.map files are included for debugging purposes.
94102

95103
## Basic usage
96104

97105
creating items dynamically...
98106

99-
```html
107+
```js
108+
// ...in your HTML
100109
<div class="grid-stack"></div>
101110

102-
<script type="text/javascript">
103-
let grid = GridStack.init();
104-
grid.addWidget('<div><div class="grid-stack-item-content">Item 1</div></div>', {width: 2});
105-
</script>
111+
// ...in your script
112+
var grid = GridStack.init();
113+
grid.addWidget('<div><div class="grid-stack-item-content">Item 1</div></div>', {width: 2});
114+
```
115+
116+
... or creating from list
117+
118+
```js
119+
// using serialize data instead of .addWidget()
120+
const serializedData = [
121+
{x: 0, y: 0, width: 2, height: 2},
122+
{x: 2, y: 3, width: 3, height: 1},
123+
{x: 1, y: 3, width: 1, height: 1}
124+
];
125+
126+
grid.load(serializedData);
106127
```
107128

108129
... or DOM created items
109130

110-
```html
131+
```js
132+
// ...in your HTML
111133
<div class="grid-stack">
112134
<div class="grid-stack-item">
113135
<div class="grid-stack-item-content">Item 1</div>
@@ -117,9 +139,8 @@ creating items dynamically...
117139
</div>
118140
</div>
119141

120-
<script type="text/javascript">
121-
GridStack.init();
122-
</script>
142+
// ...in your script
143+
GridStack.init();
123144
```
124145

125146
see [jsfiddle sample](https://jsfiddle.net/adumesny/jqhkry7g) as running example too.
@@ -276,24 +297,24 @@ GridStack.init(options);
276297
If you're still experiencing issues on touch devices please check [#444](https://github.com/gridstack/gridstack.js/issues/444)
277298

278299

279-
## Migrating to v0.6.x
300+
## Migrating to v0.6
280301

281302
starting in 0.6.x `change` event are no longer sent (for pretty much most nodes!) when an item is just added/deleted unless it also changes other nodes (was incorrect and causing inefficiencies). You may need to track `added|removed` [events](https://github.com/gridstack/gridstack.js/tree/develop/doc#events) if you didn't and relied on the old broken behavior.
282303

283-
## Migrating to v1.0.0
304+
## Migrating to v1
284305

285306
v1.0.0 removed Jquery from the API and external dependencies, which will require some code changes. Here is a list of the changes:
286307

287-
1. see [Migrating to v0.6.x](#migrating-to-v06x) if you didn't already
308+
1. see [Migrating to v0.6](#migrating-to-v06) if you didn't already
288309

289-
2. your code only needs to include `gridstack.all.js` and `gristack.css` (don't include other JS) and is recommended you do that as internal dependencies will change over time. If you are jquery based, also see note below.
310+
2. your code only needs to include `gridstack.all.js` and `gristack.css` (don't include other JS) and is recommended you do that as internal dependencies will change over time. If you are jquery based, see [jquery app](#jquery-application) below.
290311

291312
3. code change:
292313

293314
**OLD** initializing code + adding a widget + adding an event:
294315
```js
295316
// initialization returned Jquery element, requiring second call to get GridStack var
296-
let grid = $('.grid-stack').gridstack(opts?).data('gridstack');
317+
var grid = $('.grid-stack').gridstack(opts?).data('gridstack');
297318

298319
// returned Jquery element
299320
grid.addWidget($('<div><div class="grid-stack-item-content"> test </div></div>'), undefined, undefined, 2, undefined, true);
@@ -302,13 +323,13 @@ grid.addWidget($('<div><div class="grid-stack-item-content"> test </div></div>')
302323
$('.grid-stack').on('added', function(e, items) {/* items contains info */});
303324

304325
// grid access after init
305-
let grid = $('.grid-stack').data('gridstack');
326+
var grid = $('.grid-stack').data('gridstack');
306327
```
307328
**NEW**
308329
```js
309330
// element identifier defaults to '.grid-stack', returns the grid
310-
// Note: for Typescript use window.GridStack.init() until next native TS version
311-
let grid = GridStack.init(opts?, element?);
331+
// Note: for Typescript use window.GridStack.init() until next native 2.x TS version
332+
var grid = GridStack.init(opts?, element?);
312333

313334
// returns DOM element
314335
grid.addWidget('<div><div class="grid-stack-item-content"> test </div></div>', {width: 2});
@@ -317,9 +338,9 @@ grid.addWidget('<div><div class="grid-stack-item-content"> test </div></div>', {
317338
grid.on('added', function(e, items) {/* items contains info */});
318339

319340
// grid access after init
320-
let grid = el.gridstack; // where el = document.querySelector('.grid-stack') or other ways...
341+
var grid = el.gridstack; // where el = document.querySelector('.grid-stack') or other ways...
321342
```
322-
Other vars/global changes
343+
Other changes
323344
```
324345
`GridStackUI` --> `GridStack`
325346
`GridStackUI.GridStackEngine` --> `GridStack.Engine`
@@ -332,22 +353,23 @@ Recommend looking at the [many samples](./demo) for more code examples.
332353
333354
### jQuery Application
334355
335-
We're working on implementing HTML5 drag'n'drop through the plugin system. Right now it is still jquery-ui based. Because of that we are still bundling `jquery` (3.5.1) + `jquery-ui` (1.12.1 minimal drag|drop|resize) internally in `gridstack.all.js`. IFF your app needs to bring your own version instead, you should **instead** include `gridstack-poly.min.js` (optional IE support) + `gridstack.min.js` + `gridstack.jQueryUI.min.js` after you import your JQ libs. But note that there are issue with jQuery and ES6 import (see [1306](https://github.com/gridstack/gridstack.js/issues/1306))
356+
We're working on implementing HTML5 drag'n'drop through the plugin system. Right now it is still jquery-ui based. Because of that we are still bundling `jquery` (3.5.1) + `jquery-ui` (1.12.1 minimal drag|drop|resize) internally in `gridstack.all.js`. IFF your app needs to bring your own version instead, you should **instead** include `gridstack-poly.min.js` (optional IE support) + `gridstack.min.js` + `gridstack.jQueryUI.min.js` after you import your JQ libs. But note that there are issue with jQuery and ES6 import (see [1306](https://github.com/gridstack/gridstack.js/issues/1306)).
357+
358+
Note: v2.0.-rc does not currently support importing GridStack Drag&Drop without also including our jquery + jqueryui. Still trying to figure how to make that bundle possible.
336359
337-
## Migrating to v2.0.0
360+
## Migrating to v2
338361
339-
make sure to read v1.0.0 migration first!
362+
make sure to read v1 migration first!
340363
341-
v2.x is a Typescript rewrite of 1.x, removing all jquery events, using classes and overall code cleanup. Your code might need to change from 1.x
364+
v2.x is a Typescript rewrite of 1.x, removing all jquery events, using classes and overall code cleanup to support ES6 modules. Your code might need to change from 1.x
342365
343-
1. In general methods that used no args (getter) vs setter are not used in Typescript when the arguments differ.
344-
Also legacy methods that used to take many parameters will now take a single object (typically `GridstackOptions` or `GridStackWidget`).
366+
1. In general methods that used no args (getter) vs setter can't be used in TS when the arguments differ (set/get are also not function calls so API would have changed). Instead we decided to have <b>all set methods return</b> `GridStack` to they can be chain-able (ex: `grid.float(true).cellHeight(10).column(6)`). Also legacy methods that used to take many parameters will now take a single object (typically `GridstackOptions` or `GridStackWidget`).
345367
346368
```
347-
removed `addWidget(el, x, y, width, ...)` --> use the widget options version instead `addWidget(el, {with, ...})`
369+
`addWidget(el, x, y, width, height)` --> `addWidget(el, {with: 2})`
348370
`float()` to get value --> `getFloat()`
349371
'cellHeight()` to get value --> `getCellHeight()`
350-
'verticalMargin' is now 'margin' grid options and applies to all 4 sides.
372+
'verticalMargin' is now 'margin' grid options and API that applies to all 4 sides.
351373
'verticalMargin()` to get value --> `getMargin()`
352374
```
353375

demo/two.html

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
<link rel="stylesheet" href="../dist/gridstack-extra.css"/>
1212

1313
<script src="../dist/gridstack.all.js"></script>
14-
<!--
15-
<script src="../dist/jq/jquery.min.js"></script>
16-
<script src="../dist/jq/jquery-ui.min.js"></script>
17-
<script src="../dist/gridstack.js"></script>
18-
<script src="../dist/jq/gridstack-dd-jqueryui.js"></script>
19-
-->
2014

2115
<style type="text/css">
2216
.grid-stack {
@@ -100,6 +94,7 @@ <h1>Two grids demo</h1>
10094
<script type="text/javascript">
10195
let options = {
10296
column: 6,
97+
cellHeight: 70,
10398
disableOneColumnMode: true,
10499
float: false,
105100
removable: '.trash',

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Change log
3737

3838
## 2.0.0-dev (upcoming)
3939

40-
- re-write to native Typescript, removing all JQuery from main code and API (drag&drop plugin still using for now which use latest v3.5.1)
40+
- re-write to native Typescript, removing all JQuery from main code and API (drag&drop plugin still using jqueryui for now)
4141
- add `getGridItems()` to return list of HTML grid items
4242
- add `{dragIn | dragInOptions}` grid attributes to handle external drag&drop items
4343
- add `save()` and `load()` to serialize grids from JSON, saving all attributes (not just w,h,x,y) [1286](https://github.com/gridstack/gridstack.js/issues/1286)

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"url": "git+https://github.com/gridstack/gridstack.js.git"
1010
},
1111
"scripts": {
12-
"build": "yarn --no-progress && rm -rf dist/* && grunt && webpack && doctoc ./README.md && doctoc ./doc/README.md && doctoc ./doc/CHANGES.md",
12+
"build": "yarn --no-progress && rm -rf dist/* && grunt && webpack && tsc --stripInternal && doctoc ./README.md && doctoc ./doc/README.md && doctoc ./doc/CHANGES.md",
1313
"w": "rm -rf dist/* && grunt && webpack",
14-
"t": "rm -rf dist/* && tsc --stripInternal",
14+
"t": "rm -rf dist/* && grunt && tsc --stripInternal",
1515
"test": "yarn lint && karma start karma.conf.js",
1616
"lint": "tsc --noEmit && eslint src/*.ts",
1717
"reset": "rm -rf dist node_modules",
@@ -43,9 +43,11 @@
4343
},
4444
"homepage": "http://gridstack.github.io/gridstack.js/",
4545
"dependencies": {
46+
},
47+
"devDependencies": {
4648
"@types/jasmine": "^3.5.9",
47-
"@types/jquery": "^3.3.33",
48-
"@types/jqueryui": "^1.12.10",
49+
"@types/jquery": "^3.5.1",
50+
"@types/jqueryui": "^1.12.13",
4951
"@typescript-eslint/eslint-plugin": "^2.23.0",
5052
"@typescript-eslint/parser": "^2.23.0",
5153
"connect": "^3.7.0",

src/gridstack.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
* (c) 2014-2020 Alain Dumesny, Dylan Weiss, Pavel Reznikov
66
* gridstack.js may be freely distributed under the MIT license.
77
*/
8+
import './gridstack-poly.js';
89

910
import { GridStackEngine } from './gridstack-engine';
1011
import { obsoleteOpts, obsoleteOptsDel, obsoleteAttr, obsolete, Utils } from './utils';
1112
import { GridItemHTMLElement, GridStackWidget, GridStackNode, GridstackOptions, numberOrString } from './types';
1213
import { GridStackDD } from './gridstack-dd';
1314

15+
// export all dependent file as well to make it easier for users to just import the main file
16+
export * from './types';
17+
export * from './utils';
18+
export * from './gridstack-engine';
19+
export * from './gridstack-dd';
20+
21+
// TEMPORARY import the jquery-ui drag&drop since we don't have alternative yet and don't expect users to create their own yet
22+
import './jq/gridstack-dd-jqueryui';
23+
export * from './jq/gridstack-dd-jqueryui';
24+
1425
export type GridStackElement = string | HTMLElement | GridItemHTMLElement;
1526

1627
export interface GridHTMLElement extends HTMLElement {

src/index.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/jq/gridstack-dd-jqueryui.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import { GridItemHTMLElement, DDDragInOpt } from '../types';
1212

1313
// TODO: TEMPORARY until can remove jquery-ui drag&drop and this class and use HTML5 instead !
1414
// see https://stackoverflow.com/questions/35345760/importing-jqueryui-with-typescript-and-requirejs
15-
import * as $ from './jquery.js';
16-
import './jquery-ui.js';
15+
import * as $ from './jquery';
16+
export { $ };
17+
export * from './jquery-ui';
1718

1819
/**
1920
* Jquery-ui based drag'n'drop plugin.
@@ -67,7 +68,7 @@ export class GridStackDDJQueryUI extends GridStackDD {
6768
}
6869

6970
public dragIn(el: GridStackElement, opts: DDDragInOpt): GridStackDD {
70-
let $el: JQuery = $(el);
71+
let $el: JQuery = $(el as any); // workaround Type 'string' is not assignable to type 'PlainObject<any>' - see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29312
7172
$el.draggable(opts);
7273
return this;
7374
}
@@ -89,7 +90,7 @@ export class GridStackDDJQueryUI extends GridStackDD {
8990
}
9091

9192
public isDraggable(el: GridStackElement): boolean {
92-
let $el: JQuery = $(el);
93+
let $el: JQuery = $(el as any); // workaround Type 'string' is not assignable to type 'PlainObject<any>' - see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/29312
9394
return Boolean($el.data('ui-draggable'));
9495
}
9596

tsconfig.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
"experimentalDecorators": true,
1111
"inlineSources": true,
1212
"lib": [ "es6", "es2015", "dom" ],
13-
"module": "commonjs",
13+
"module": "CommonJS",
1414
"noImplicitAny": false,
1515
"outDir": "./dist",
1616
"sourceMap": true,
1717
"strict": false,
18-
"target": "es5"
18+
"target": "ES6"
1919
},
2020
"exclude": [
21-
"./src/**/*.spec.ts"
21+
"./src/**/*.spec.ts",
22+
"./src/index.ts" // used for webpack all.js
23+
2224
],
2325
"include": [
2426
"./src/**/*.ts"

0 commit comments

Comments
 (0)