diff --git a/README.md b/README.md index 24633d688..51483b3df 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,8 @@ Also legacy methods that used to take tons of parameters will now take an object ``` removed `addWidget(el, x, y, width, ...)` --> use the widget options version instead `addWidget(el, {x, y, with,...})` `float()` to get value --> `getFloat()` +'cellHeight()` to get value --> `getCellheight()` +'verticalMargin()` to get value --> `getVerticalMargin()` ``` Changes diff --git a/doc/README.md b/doc/README.md index dddaa1b60..281ff7799 100644 --- a/doc/README.md +++ b/doc/README.md @@ -24,7 +24,6 @@ gridstack.js API - [addWidget(el, [options])](#addwidgetel-options) - [batchUpdate()](#batchupdate) - [compact()](#compact) - - [cellHeight()](#cellheight) - [cellHeight(val, noUpdate)](#cellheightval-noupdate) - [cellWidth()](#cellwidth) - [commit()](#commit) @@ -35,6 +34,7 @@ gridstack.js API - [enableMove(doEnable, includeNewWidgets)](#enablemovedoenable-includenewwidgets) - [enableResize(doEnable, includeNewWidgets)](#enableresizedoenable-includenewwidgets) - [float(val?)](#floatval) + - [getCellHeight()](#getcellheight) - [getCellFromPixel(position[, useOffset])](#getcellfrompixelposition-useoffset) - [isAreaEmpty(x, y, width, height)](#isareaemptyx-y-width-height) - [locked(el, val)](#lockedel-val) @@ -52,7 +52,7 @@ gridstack.js API - [setAnimation(doAnimate)](#setanimationdoanimate) - [setStatic(staticValue)](#setstaticstaticvalue) - [update(el, x, y, width, height)](#updateel-x-y-width-height) - - [verticalMargin()](#verticalmargin) + - [getVerticalMargin()](#getverticalmargin) - [verticalMargin(value, noUpdate)](#verticalmarginvalue-noupdate) - [willItFit(x, y, width, height, autoPosition)](#willitfitx-y-width-height-autoposition) - [Utils](#utils) @@ -261,10 +261,6 @@ starts batch updates. You will see no changes until `commit()` method is called. re-layout grid items to reclaim any empty space. -### cellHeight() - -Gets current cell height. - ### cellHeight(val, noUpdate) Update current cell height. This method rebuilds an internal CSS stylesheet (unless optional noUpdate=true). Note: You can expect performance issues if @@ -342,6 +338,11 @@ set/get floating widgets (default: `false`) - `val` - boolean to set true/false, else get the current value +### getCellHeight() + +Gets current cell height. + + ### getCellFromPixel(position[, useOffset]) Get the position of the cell under a pixel on screen. @@ -478,7 +479,7 @@ Parameters: Updates widget position/size. -### verticalMargin() +### getVerticalMargin() returns current vertical margin value. diff --git a/karma.conf.js b/karma.conf.js index f74f2d122..a4b56b442 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -3,16 +3,6 @@ module.exports = function(config) { config.set({ - karmaTypescriptConfig: { - compilerOptions: { - module: 'commonjs', - lib: ['dom', 'es6'], - target: 'es5', - sourceMap: true, - declaration: true - } - }, - // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', @@ -20,14 +10,13 @@ module.exports = function(config) { // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['jasmine', 'karma-typescript'], + frameworks: ['jasmine'], // list of files / patterns to load in the browser files: [ 'dist/gridstack.all.js', 'spec/*-spec.js', - 'src/**/*.ts' // 'spec/e2e/*-spec.js' issues with ReferenceError: `browser` & `element` is not defined ], @@ -40,19 +29,17 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - 'src/gridstack-dragdrop-plugin.ts': ['coverage'], - 'src/gridstack.engine.ts': ['coverage'], - 'src/gridstack.ts': ['coverage'], - 'src/jqueryui-gridstack-dragdrop-plugin.ts': ['coverage'], - 'src/utils.ts': ['coverage'], - 'src/*.ts': ['karma-typescript'] + 'dist/gridstack-engine.js': ['coverage'], + 'dist/gridstack.js': ['coverage'], + 'dist/jqueryui-gridstack-dragdrop-plugin': ['coverage'], + 'dist/utils.js': ['coverage'] }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ['progress', 'coverage', 'coveralls', 'karma-typescript'], + reporters: ['progress', 'coverage', 'coveralls'], coverageReporter: { type: 'lcov', // lcov or lcovonly are required for generating lcov.info files diff --git a/package.json b/package.json index 4cf74b964..9b5e77fa7 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,7 @@ "scripts": { "build": "yarn --no-progress && rm -rf dist/* && webpack && grunt && doctoc ./README.md && doctoc ./doc/README.md && doctoc ./doc/CHANGES.md", "web": "rm -rf dist/* && webpack", - "test": "karma start karma.conf.js", - "test2": "yarn lint && karma start karma.conf.js", + "test": "yarn lint && karma start karma.conf.js", "lint": "tsc --noEmit && eslint src/*.ts", "reset": "rm -rf dist node_modules", "prepublishOnly": "yarn build" diff --git a/spec/gridstack-engine-spec.js b/spec/gridstack-engine-spec.js index 95c402169..947aea58c 100644 --- a/spec/gridstack-engine-spec.js +++ b/spec/gridstack-engine-spec.js @@ -1,36 +1,58 @@ describe('gridstack engine', function() { 'use strict'; + let engine; + let e = GridStack.Engine; + let w = window; - var e; - var w; + let findNode = function(engine, id) { + return engine.nodes.find(function(i) { return i._id === id; }); + }; - beforeEach(function() { - w = window; - e = GridStack.Engine; + it('should exist setup function.', function() { + expect(e).not.toBeNull(); + expect(typeof e).toBe('function'); }); describe('test constructor', function() { - var engine; - - beforeAll(function() { - engine = new GridStack.Engine(12); - }); - + it('should be setup properly', function() { + engine = new GridStack.Engine(12); expect(engine.column).toEqual(12); expect(engine.float).toEqual(false); expect(engine.maxRow).toEqual(0); expect(engine.nodes).toEqual([]); + expect(engine.onchange).toEqual(undefined); + expect(engine.batchMode).toEqual(undefined); + }); + + it('should set params correctly.', function() { + let fkt = function() { }; + let arr = [1,2,3]; + engine = new GridStack.Engine(1, fkt, true, 2, arr); + expect(engine.column).toEqual(1); + expect(engine.float).toBe(true); + expect(engine.maxRow).toEqual(2); + expect(engine.nodes).toEqual(arr); + expect(engine.onchange).toEqual(fkt); + expect(engine.batchMode).toEqual(undefined); }); }); + describe('batch update', function() { + + it('should set float and batchMode when calling batchUpdate.', function() { + // Note: legacy weird call on global window to hold data + e.prototype.batchUpdate.call(w); + expect(w.float).toBe(undefined); + expect(w.batchMode).toBeTrue(); + }); + }); + describe('test prepareNode', function() { - var engine; beforeAll(function() { engine = new GridStack.Engine(12); }); - it('should prepare a node', function() { expect(engine.prepareNode({}, false)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); expect(engine.prepareNode({x: 10}, false)).toEqual(jasmine.objectContaining({x: 10, y: 0, width: 1, height: 1})); @@ -49,8 +71,53 @@ describe('gridstack engine', function() { }); }); + describe('sorting of nodes', function() { + // Note: legacy weird call on global window to hold data + it('should sort ascending with 12 columns.', function() { + w.column = 12; + w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; + e.prototype._sortNodes.call(w, 1); + expect(w.nodes).toEqual([{x: 7, y: 0}, {x: 9, y: 0}, {x: 0, y: 1}, {x: 4, y: 4}]); + }); + + it('should sort descending with 12 columns.', function() { + w.column = 12; + w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; + e.prototype._sortNodes.call(w, -1); + expect(w.nodes).toEqual([{x: 4, y: 4}, {x: 0, y: 1}, {x: 9, y: 0}, {x: 7, y: 0}]); + }); + + it('should sort ascending with 1 columns.', function() { + w.column = 1; + w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; + e.prototype._sortNodes.call(w, 1); + expect(w.nodes).toEqual([{x: 0, y: 1}, {x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}]); + }); + + it('should sort descending with 1 columns.', function() { + w.column = 1; + w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; + e.prototype._sortNodes.call(w, -1); + expect(w.nodes).toEqual([{x: 9, y: 0}, {x: 4, y: 4}, {x: 7, y: 0}, {x: 0, y: 1}]); + }); + + it('should sort ascending without columns.', function() { + w.column = undefined; + w.nodes = [{x: 7, y: 0, width: 1}, {x: 4, y: 4, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}]; + e.prototype._sortNodes.call(w, 1); + expect(w.nodes).toEqual([{x: 7, y: 0, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}, {x: 4, y: 4, width: 1}]); + }); + + it('should sort descending without columns.', function() { + w.column = undefined; + w.nodes = [{x: 7, y: 0, width: 1}, {x: 4, y: 4, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}]; + e.prototype._sortNodes.call(w, -1); + expect(w.nodes).toEqual([{x: 4, y: 4, width: 1}, {x: 0, y: 1, width: 1}, {x: 9, y: 0, width: 1}, {x: 7, y: 0, width: 1}]); + }); + + }); + describe('test isAreaEmpty', function() { - var engine; beforeAll(function() { engine = new GridStack.Engine(12, null, true); @@ -71,7 +138,6 @@ describe('gridstack engine', function() { }); describe('test cleanNodes/getDirtyNodes', function() { - var engine; beforeAll(function() { engine = new GridStack.Engine(12, null, true); @@ -83,34 +149,29 @@ describe('gridstack engine', function() { }); beforeEach(function() { - engine._batchMode = false; + engine.batchMode = false; }); it('should return all dirty nodes', function() { - var nodes = engine.getDirtyNodes(); - + let nodes = engine.getDirtyNodes(); expect(nodes.length).toEqual(2); expect(nodes[0].idx).toEqual(1); expect(nodes[1].idx).toEqual(2); }); - it('should\'n clean nodes if _batchMode true', function() { - engine._batchMode = true; + it('should\'n clean nodes if batchMode true', function() { + engine.batchMode = true; engine.cleanNodes(); - expect(engine.getDirtyNodes().length).toBeGreaterThan(0); }); it('should clean all dirty nodes', function() { engine.cleanNodes(); - expect(engine.getDirtyNodes().length).toEqual(0); }); }); describe('test batchUpdate/commit', function() { - var engine; - beforeAll(function() { engine = new GridStack.Engine(12); }); @@ -118,26 +179,25 @@ describe('gridstack engine', function() { it('should work on not float grids', function() { expect(engine.float).toEqual(false); engine.batchUpdate(); - expect(engine._batchMode).toBeTrue(); + expect(engine.batchMode).toBeTrue(); expect(engine.float).toEqual(true); engine.commit(); - expect(engine._batchMode).toBeFalse(); + expect(engine.batchMode).toBeFalse(); expect(engine.float).toEqual(false); }); it('should work on float grids', function() { engine.float = true; engine.batchUpdate(); - expect(engine._batchMode).toBeTrue(); + expect(engine.batchMode).toBeTrue(); expect(engine.float).toEqual(true); engine.commit(); - expect(engine._batchMode).toBeFalse(); + expect(engine.batchMode).toBeFalse(); expect(engine.float).toEqual(true); }); }); describe('test batchUpdate/commit', function() { - var engine; beforeAll(function() { engine = new GridStack.Engine(12, null, true); @@ -146,26 +206,23 @@ describe('gridstack engine', function() { it('should work on float grids', function() { expect(engine.float).toEqual(true); engine.batchUpdate(); - expect(engine._batchMode).toBeTrue(); + expect(engine.batchMode).toBeTrue(); expect(engine.float).toEqual(true); engine.commit(); - expect(engine._batchMode).toBeFalse(); + expect(engine.batchMode).toBeFalse(); expect(engine.float).toEqual(true); }); }); describe('test _notify', function() { - var engine; - var spy; + let spy; beforeEach(function() { spy = { callback: function() {} }; spyOn(spy, 'callback'); - engine = new GridStack.Engine(12, spy.callback, true); - engine.nodes = [ engine.prepareNode({x: 0, y: 0, width: 1, height: 1, idx: 1, _dirty: true}), engine.prepareNode({x: 3, y: 2, width: 3, height: 2, idx: 2, _dirty: true}), @@ -173,16 +230,14 @@ describe('gridstack engine', function() { ]; }); - it('should\'n be called if _batchMode true', function() { - engine._batchMode = true; + it('should\'n be called if batchMode true', function() { + engine.batchMode = true; engine._notify(); - expect(spy.callback).not.toHaveBeenCalled(); }); it('should by called with dirty nodes', function() { engine._notify(); - expect(spy.callback).toHaveBeenCalledWith([ engine.nodes[0], engine.nodes[1] @@ -190,10 +245,8 @@ describe('gridstack engine', function() { }); it('should by called with extra passed node to be removed', function() { - var n1 = {idx: -1}; - + let n1 = {idx: -1}; engine._notify(n1); - expect(spy.callback).toHaveBeenCalledWith([ n1, engine.nodes[0], @@ -202,10 +255,8 @@ describe('gridstack engine', function() { }); it('should by called with extra passed node to be removed and should maintain false parameter', function() { - var n1 = {idx: -1}; - + let n1 = {idx: -1}; engine._notify(n1, false); - expect(spy.callback).toHaveBeenCalledWith([ n1, engine.nodes[0], @@ -216,12 +267,6 @@ describe('gridstack engine', function() { describe('test _packNodes', function() { describe('using not float mode', function() { - var engine; - - var findNode = function(engine, id) { - return engine.nodes.find(function(i) { return i._id === id; }); - }; - beforeEach(function() { engine = new GridStack.Engine(12, null, false); }); @@ -230,9 +275,7 @@ describe('gridstack engine', function() { engine.nodes = [ {x: 0, y: 0, width: 1, height: 1, _id: 1}, ]; - engine._packNodes(); - expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1})); expect(findNode(engine, 1)._dirty).toBeFalsy(); }); @@ -241,9 +284,7 @@ describe('gridstack engine', function() { engine.nodes = [ {x: 0, y: 1, width: 1, height: 1, _id: 1}, ]; - engine._packNodes(); - expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true})); }); @@ -252,33 +293,27 @@ describe('gridstack engine', function() { {x: 0, y: 1, width: 1, height: 1, _id: 1}, {x: 0, y: 5, width: 1, height: 1, _id: 2}, ]; - engine._packNodes(); - expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true})); expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true})); }); - + it('should pack nodes correctly', function() { engine.nodes = [ {x: 0, y: 5, width: 1, height: 1, _id: 1}, {x: 0, y: 1, width: 1, height: 1, _id: 2}, ]; - engine._packNodes(); - expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 0, width: 1, height: 1, _dirty: true})); expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1, _dirty: true})); }); - + it('should respect locked nodes', function() { engine.nodes = [ {x: 0, y: 1, width: 1, height: 1, _id: 1, locked: true}, {x: 0, y: 5, width: 1, height: 1, _id: 2}, ]; - engine._packNodes(); - expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 1, height: 1})); expect(findNode(engine, 1)._dirty).toBeFalsy(); expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 0, y: 2, width: 1, height: 1, _dirty: true})); @@ -287,35 +322,57 @@ describe('gridstack engine', function() { }); describe('test isNodeChangedPosition', function() { - var engine; - beforeAll(function() { engine = new GridStack.Engine(12); }); - it('should return true for changed x', function() { - var widget = { x: 1, y: 2, width: 3, height: 4 }; + let widget = { x: 1, y: 2, width: 3, height: 4 }; expect(engine.isNodeChangedPosition(widget, 2, 2)).toEqual(true); }); - it('should return true for changed y', function() { - var widget = { x: 1, y: 2, width: 3, height: 4 }; + let widget = { x: 1, y: 2, width: 3, height: 4 }; expect(engine.isNodeChangedPosition(widget, 1, 1)).toEqual(true); }); - it('should return true for changed width', function() { - var widget = { x: 1, y: 2, width: 3, height: 4 }; + let widget = { x: 1, y: 2, width: 3, height: 4 }; expect(engine.isNodeChangedPosition(widget, 2, 2, 4, 4)).toEqual(true); }); - it('should return true for changed height', function() { - var widget = { x: 1, y: 2, width: 3, height: 4 }; + let widget = { x: 1, y: 2, width: 3, height: 4 }; expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 3)).toEqual(true); }); - it('should return false for unchanged position', function() { - var widget = { x: 1, y: 2, width: 3, height: 4 }; + let widget = { x: 1, y: 2, width: 3, height: 4 }; expect(engine.isNodeChangedPosition(widget, 1, 2, 3, 4)).toEqual(false); }); }); + + describe('test locked widget', function() { + beforeAll(function() { + engine = new GridStack.Engine(12); + }); + it('should add widgets around locked one', function() { + let nodes = [ + {x: 0, y: 1, width: 12, height: 1, locked: 'yes', noMove: true, noResize: true, _id: 1}, + {x: 1, y: 0, width: 2, height: 3, _id: 2} + ]; + // add locked item + engine.addNode(nodes[0]) + expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 12, height: 1, locked: 'yes'})); + engine.addNode(nodes[1]) + // add item that moves past locked one + expect(findNode(engine, 1)).toEqual(jasmine.objectContaining({x: 0, y: 1, width: 12, height: 1, locked: 'yes'})); + expect(findNode(engine, 2)).toEqual(jasmine.objectContaining({x: 1, y: 2})); + // prevents moving locked item + let node1 = findNode(engine, 1); + expect(engine.moveNode(node1, 6, 6)).toEqual(null); + // but moves regular one (gravity ON) + let node2 = findNode(engine, 2); + expect(engine.moveNode(node2, 6, 6)).toEqual(jasmine.objectContaining({x: 6, y: 2, width: 2, height: 3,})); + // but moves regular one (gravity OFF) + engine.float = true; + expect(engine.moveNode(node2, 7, 6)).toEqual(jasmine.objectContaining({x: 7, y: 6, width: 2, height: 3,})); + }); + }); + }); diff --git a/spec/gridstack-spec.js b/spec/gridstack-spec.js index 2473600d4..c734d1a5d 100644 --- a/spec/gridstack-spec.js +++ b/spec/gridstack-spec.js @@ -1,10 +1,8 @@ describe('gridstack', function() { 'use strict'; - var e; - var w; // grid has 4x2 and 4x4 top-left aligned - used on most test cases - var gridstackHTML = + let gridstackHTML = '
' + '
' + '
' + @@ -16,90 +14,15 @@ describe('gridstack', function() { '
' + '
'; // empty grid - var gridstackEmptyHTML = + let gridstackEmptyHTML = '
' + '
' + '
' + '
'; // generic widget with no param - var widgetHTML = '
hello
'; + let widgetHTML = '
hello
'; beforeEach(function() { - w = window; - e = GridStack.Engine; - }); - - describe('setup of gridstack', function() { - - it('should exist setup function.', function() { - - expect(e).not.toBeNull(); - expect(typeof e).toBe('function'); - }); - - it('should set default params correctly.', function() { - e.call(w); - expect(w.column).toEqual(12); - expect(w.float).toBe(false); - expect(w.maxRow).toEqual(0); - expect(w.nodes).toEqual([]); - expect(typeof w.onchange).toBe('function'); - expect(w._batchMode).toBeFalse(); - }); - - it('should set params correctly.', function() { - var fkt = function() { }; - var arr = [1,2,3]; - - e.call(w, 1, fkt, true, 2, arr); - expect(w.column).toEqual(1); - expect(w.float).toBe(true); - expect(w.maxRow).toEqual(2); - expect(w.nodes).toEqual(arr); - expect(w.onchange).toEqual(fkt); - expect(w._batchMode).toBeFalse(); - }); - }); - - describe('batch update', function() { - - it('should set float and batchMode when calling batchUpdate.', function() { - e.prototype.batchUpdate.call(w); - expect(w.float).toBe(true); - expect(w._batchMode).toBeTrue(); - }); - - //test commit function - }); - - describe('sorting of nodes', function() { - - it('should sort ascending with columns.', function() { - w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; - e.prototype._sortNodes.call(w, 1); - expect(w.nodes).toEqual([{x: 0, y: 1}, {x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}]); - }); - - it('should sort descending with columns.', function() { - w.nodes = [{x: 7, y: 0}, {x: 4, y: 4}, {x: 9, y: 0}, {x: 0, y: 1}]; - e.prototype._sortNodes.call(w, -1); - expect(w.nodes).toEqual([{x: 9, y: 0}, {x: 4, y: 4}, {x: 7, y: 0}, {x: 0, y: 1}]); - }); - - it('should sort ascending without columns.', function() { - w.column = undefined; - w.nodes = [{x: 7, y: 0, width: 1}, {x: 4, y: 4, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}]; - e.prototype._sortNodes.call(w, 1); - expect(w.nodes).toEqual([{x: 7, y: 0, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}, {x: 4, y: 4, width: 1}]); - }); - - it('should sort descending without columns.', function() { - w.column = undefined; - w.nodes = [{x: 7, y: 0, width: 1}, {x: 4, y: 4, width: 1}, {x: 9, y: 0, width: 1}, {x: 0, y: 1, width: 1}]; - e.prototype._sortNodes.call(w, -1); - expect(w.nodes).toEqual([{x: 4, y: 4, width: 1}, {x: 0, y: 1, width: 1}, {x: 9, y: 0, width: 1}, {x: 7, y: 0, width: 1}]); - }); - }); describe('grid.setAnimation', function() { @@ -110,21 +33,21 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should add class grid-stack-animate to the container.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); $('.grid-stack').removeClass('grid-stack-animate'); grid.setAnimation(true); expect($('.grid-stack').hasClass('grid-stack-animate')).toBe(true); }); it('should remove class grid-stack-animate from the container.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); $('.grid-stack').addClass('grid-stack-animate'); grid.setAnimation(false); expect($('.grid-stack').hasClass('grid-stack-animate')).toBe(false); @@ -139,23 +62,23 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should add class grid-stack-static to the container.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, staticGrid: true }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); $('.grid-stack').removeClass('grid-stack-static'); grid._setStaticClass(); expect($('.grid-stack').hasClass('grid-stack-static')).toBe(true); }); it('should remove class grid-stack-static from the container.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, staticGrid: false }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); $('.grid-stack').addClass('grid-stack-static'); grid._setStaticClass(); expect($('.grid-stack').hasClass('grid-stack-static')).toBe(false); @@ -170,36 +93,36 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should return {x: 2, y: 5}.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var container = $('.grid-stack'); - var pixel = {top: 500, left: 200}; - var cell = grid.getCellFromPixel(pixel); + let grid = GridStack.init(options); + let container = $('.grid-stack'); + let pixel = {top: 500, left: 200}; + let cell = grid.getCellFromPixel(pixel); expect(cell.x).toBe(2); expect(cell.y).toBe(5); }); it('should return {x: 2, y: 5}.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var pixel = {top: 500, left: 200}; - var cell = grid.getCellFromPixel(pixel, false); + let grid = GridStack.init(options); + let pixel = {top: 500, left: 200}; + let cell = grid.getCellFromPixel(pixel, false); expect(cell.x).toBe(2); expect(cell.y).toBe(5); }); it('should return {x: 2, y: 5}.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var pixel = {top: 500, left: 200}; - var cell = grid.getCellFromPixel(pixel, true); + let grid = GridStack.init(options); + let pixel = {top: 500, left: 200}; + let cell = grid.getCellFromPixel(pixel, true); expect(cell.x).toBe(2); expect(cell.y).toBe(5); }); @@ -213,23 +136,23 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should return 1/12th of container width.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, column: 12 }; - var grid = GridStack.init(options); - var res = Math.round($('.grid-stack').outerWidth() / 12); + let grid = GridStack.init(options); + let res = Math.round($('.grid-stack').outerWidth() / 12); expect(grid.cellWidth()).toBe(res); }); it('should return 1/10th of container width.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, column: 10 }; - var grid = GridStack.init(options); - var res = Math.round($('.grid-stack').outerWidth() / 10); + let grid = GridStack.init(options); + let res = Math.round($('.grid-stack').outerWidth() / 10); expect(grid.cellWidth()).toBe(res); }); }); @@ -242,34 +165,34 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should start at 80 then become 120', function() { - var cellHeight = 80; - var verticalMargin = 10; - var options = { + let cellHeight = 80; + let verticalMargin = 10; + let options = { cellHeight: cellHeight, verticalMargin: verticalMargin, column: 12 }; - var grid = GridStack.init(options); - var container = $('.grid-stack'); - var rows = parseInt(container.attr('data-gs-current-row')); + let grid = GridStack.init(options); + let container = $('.grid-stack'); + let rows = parseInt(container.attr('data-gs-current-row')); expect(grid.getRow()).toBe(rows); - expect(grid.cellHeight()).toBe(cellHeight); + expect(grid.getCellHeight()).toBe(cellHeight); expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * verticalMargin); - grid.cellHeight( grid.cellHeight() ); // should be no-op - expect(grid.cellHeight()).toBe(cellHeight); + grid.cellHeight( grid.getCellHeight() ); // should be no-op + expect(grid.getCellHeight()).toBe(cellHeight); expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * verticalMargin); cellHeight = 120; // should change and CSS actual height grid.cellHeight( cellHeight ); - expect(grid.cellHeight()).toBe(cellHeight); + expect(grid.getCellHeight()).toBe(cellHeight); expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * verticalMargin); cellHeight = 20; // should change and CSS actual height grid.cellHeight( cellHeight ); - expect(grid.cellHeight()).toBe(cellHeight); + expect(grid.getCellHeight()).toBe(cellHeight); expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * verticalMargin); }); }); @@ -282,41 +205,41 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should have no changes', function() { - var grid = GridStack.init(); - expect(grid.column()).toBe(12); + let grid = GridStack.init(); + expect(grid.getColumn()).toBe(12); grid.column(12); - expect(grid.column()).toBe(12); - }); + expect(grid.getColumn()).toBe(12); + }); it('should SMALL change column number, no relayout', function() { - var options = { + let options = { column: 12 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); grid.column(10, false); - expect(grid.column()).toBe(10); - for (var j = 0; j < items.length; j++) { + expect(grid.getColumn()).toBe(10); + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-y'), 10)).toBe(0); } grid.column(9, true); - expect(grid.column()).toBe(9); - for (var j = 0; j < items.length; j++) { + expect(grid.getColumn()).toBe(9); + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-y'), 10)).toBe(0); } grid.column(12); - expect(grid.column()).toBe(12); - for (var j = 0; j < items.length; j++) { + expect(grid.getColumn()).toBe(12); + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-y'), 10)).toBe(0); } }); it('should change column number and relayout items', function() { - var options = { + let options = { column: 12, float: true }; - var grid = GridStack.init(options); - var el1 = $('#item1') - var el2 = $('#item2') + let grid = GridStack.init(options); + let el1 = $('#item1') + let el2 = $('#item2') // items start at 4x2 and 4x4 expect(parseInt(el1.attr('data-gs-x'))).toBe(0); @@ -330,7 +253,7 @@ describe('gridstack', function() { expect(parseInt(el2.attr('data-gs-height'))).toBe(4); // 1 column will have item1, item2 grid.column(1); - expect(grid.column()).toBe(1); + expect(grid.getColumn()).toBe(1); expect(parseInt(el1.attr('data-gs-x'))).toBe(0); expect(parseInt(el1.attr('data-gs-y'))).toBe(0); expect(parseInt(el1.attr('data-gs-width'))).toBe(1); @@ -342,7 +265,7 @@ describe('gridstack', function() { expect(parseInt(el2.attr('data-gs-height'))).toBe(4); // add default 1x1 item to the end (1 column) - var el3 = $(grid.addWidget(widgetHTML)); + let el3 = $(grid.addWidget(widgetHTML)); expect(el3).not.toBe(null); expect(parseInt(el3.attr('data-gs-x'))).toBe(0); expect(parseInt(el3.attr('data-gs-y'))).toBe(6); @@ -351,7 +274,7 @@ describe('gridstack', function() { // back to 12 column and initial layout (other than new item3) grid.column(12); - expect(grid.column()).toBe(12); + expect(grid.getColumn()).toBe(12); expect(parseInt(el1.attr('data-gs-x'))).toBe(0); expect(parseInt(el1.attr('data-gs-y'))).toBe(0); expect(parseInt(el1.attr('data-gs-width'))).toBe(4); @@ -369,8 +292,8 @@ describe('gridstack', function() { // back to 1 column, move item2 to beginning to [3][1][2] vertically grid.column(1); - expect(grid.column()).toBe(1); - grid.move(el3, 0, 0); + expect(grid.getColumn()).toBe(1); + grid.move(el3.get(0), 0, 0); expect(parseInt(el3.attr('data-gs-x'))).toBe(0); expect(parseInt(el3.attr('data-gs-y'))).toBe(0); expect(parseInt(el3.attr('data-gs-width'))).toBe(1); @@ -388,7 +311,7 @@ describe('gridstack', function() { // back to 12 column, el3 to be beginning still, but [1][2] to be in 1 columns still but wide 4x2 and 4x still grid.column(12); - expect(grid.column()).toBe(12); + expect(grid.getColumn()).toBe(12); expect(parseInt(el3.attr('data-gs-x'))).toBe(0); expect(parseInt(el3.attr('data-gs-y'))).toBe(0); expect(parseInt(el3.attr('data-gs-width'))).toBe(1); @@ -407,7 +330,7 @@ describe('gridstack', function() { // 2 column will have item1, item2, item3 in 1 column still but half the width grid.column(1); // test convert from small, should use 12 layout still grid.column(2); - expect(grid.column()).toBe(2); + expect(grid.getColumn()).toBe(2); expect(parseInt(el3.attr('data-gs-x'))).toBe(0); expect(parseInt(el3.attr('data-gs-y'))).toBe(0); @@ -434,14 +357,14 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should support default going to 1 column', function() { - var options = { + let options = { column: 12, float: true }; - var grid = GridStack.init(options); - var el1 = $(grid.addWidget(widgetHTML, {width:1, height:1})); - var el2 = $(grid.addWidget(widgetHTML, {x:2, y:0, width:2, height:1})); - var el3 = $(grid.addWidget(widgetHTML, {x:1, y:0, width:1, height:2})); + let grid = GridStack.init(options); + let el1 = $(grid.addWidget(widgetHTML, {width:1, height:1})); + let el2 = $(grid.addWidget(widgetHTML, {x:2, y:0, width:2, height:1})); + let el3 = $(grid.addWidget(widgetHTML, {x:1, y:0, width:1, height:2})); // items are item1[1x1], item3[1x1], item2[2x1] expect(parseInt(el1.attr('data-gs-x'))).toBe(0); @@ -477,15 +400,15 @@ describe('gridstack', function() { expect(parseInt(el2.attr('data-gs-height'))).toBe(1); }); it('should support oneColumnModeDomSort ON going to 1 column', function() { - var options = { + let options = { column: 12, oneColumnModeDomSort: true, float: true }; - var grid = GridStack.init(options); - var el1 = $(grid.addWidget(widgetHTML, {width:1, height:1})); - var el2 = $(grid.addWidget(widgetHTML, {x:2, y:0, width:2, height:1})); - var el3 = $(grid.addWidget(widgetHTML, {x:1, y:0, width:1, height:2})); + let grid = GridStack.init(options); + let el1 = $(grid.addWidget(widgetHTML, {width:1, height:1})); + let el2 = $(grid.addWidget(widgetHTML, {x:2, y:0, width:2, height:1})); + let el3 = $(grid.addWidget(widgetHTML, {x:1, y:0, width:1, height:2})); // items are item1[1x1], item3[1x1], item2[2x1] expect(parseInt(el1.attr('data-gs-x'))).toBe(0); @@ -530,16 +453,16 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should set data-gs-min-width to 2.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { grid.minWidth(items[i], 2); } - for (var j = 0; j < items.length; j++) { + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-min-width'), 10)).toBe(2); } }); @@ -553,16 +476,16 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should set data-gs-min-width to 2.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { grid.maxWidth(items[i], 2); } - for (var j = 0; j < items.length; j++) { + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-max-width'), 10)).toBe(2); } }); @@ -576,16 +499,16 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should set data-gs-min-height to 2.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { grid.minHeight(items[i], 2); } - for (var j = 0; j < items.length; j++) { + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-min-height'), 10)).toBe(2); } }); @@ -599,16 +522,10 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should set data-gs-min-height to 2.', function() { - var options = { - cellHeight: 80, - verticalMargin: 10 - }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - for (var i = 0; i < items.length; i++) { - grid.maxHeight(items[i], 2); - } - for (var j = 0; j < items.length; j++) { + let grid = GridStack.init(); + let items = $('.grid-stack-item'); + grid.maxHeight('.grid-stack-item', 2); + for (let j = 0; j < items.length; j++) { expect(parseInt($(items[j]).attr('data-gs-max-height'), 10)).toBe(2); } }); @@ -622,21 +539,21 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should set return false.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var shouldBeFalse = grid.isAreaEmpty(1, 1, 1, 1); + let grid = GridStack.init(options); + let shouldBeFalse = grid.isAreaEmpty(1, 1, 1, 1); expect(shouldBeFalse).toBe(false); }); it('should set return true.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var shouldBeTrue = grid.isAreaEmpty(5, 5, 1, 1); + let grid = GridStack.init(options); + let shouldBeTrue = grid.isAreaEmpty(5, 5, 1, 1); expect(shouldBeTrue).toBe(true); }); }); @@ -649,19 +566,19 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should remove all children by default', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); grid.removeAll(); expect(grid.engine.nodes).toEqual([]); expect(document.getElementById('item1')).toBe(null); }); it('should remove all children', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); grid.removeAll(true); expect(grid.engine.nodes).toEqual([]); expect(document.getElementById('item1')).toBe(null); }); it('should remove gridstack part, leave DOM behind', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); grid.removeAll(false); expect(grid.engine.nodes).toEqual([]); expect(document.getElementById('item1')).not.toBe(null); @@ -676,22 +593,22 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should remove first item (default), then second (true), then third (false)', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); expect(grid.engine.nodes.length).toEqual(2); - var el1 = document.getElementById('item1'); + let el1 = document.getElementById('item1'); expect(el1).not.toBe(null); grid.removeWidget(el1); expect(grid.engine.nodes.length).toEqual(1); expect(document.getElementById('item1')).toBe(null); expect(document.getElementById('item2')).not.toBe(null); - var el2 = document.getElementById('item2'); + let el2 = document.getElementById('item2'); grid.removeWidget(el2, true); expect(grid.engine.nodes.length).toEqual(0); expect(document.getElementById('item2')).toBe(null); - var el3 = grid.addWidget(widgetHTML); + let el3 = grid.addWidget(widgetHTML); expect(el3).not.toBe(null); grid.removeWidget(el3, false); expect(grid.engine.nodes.length).toEqual(0); @@ -707,16 +624,16 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should allow same x, y coordinates for widgets.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, float: true }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - var $el; - var $oldEl; - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + let $el; + let $oldEl; + for (let i = 0; i < items.length; i++) { $el = $(grid.addWidget(items[i])); $oldEl = $(items[i]); expect(parseInt($oldEl.attr('data-gs-x'), 10)).toBe(parseInt($el.attr('data-gs-x'), 10)); @@ -724,21 +641,21 @@ describe('gridstack', function() { } }); it('should not allow same x, y coordinates for widgets.', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - var $el; - var $oldEl; - var newY; - var oldY; - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + let $el; + let $oldEl; + let newY; + let oldY; + for (let i = 0; i < items.length; i++) { $oldEl = $.extend(true, {}, $(items[i])); newY = parseInt($oldEl.attr('data-gs-y'), 10) + 5; $oldEl.attr('data-gs-y', newY); - $el = $(grid.addWidget($oldEl)); + $el = $(grid.addWidget($oldEl.get(0))); expect(parseInt($el.attr('data-gs-y'), 10)).not.toBe(newY); } }); @@ -752,49 +669,49 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should keep all widget options the same (autoPosition off', function() { - var grid = GridStack.init({float: true});; - var widget = grid.addWidget(widgetHTML, {x: 6, y:7, width:2, height:3, autoPosition:false, - mindWidth:1, maxWidth:4, mindHeight:2, maxHeight:5, id:'coolWidget'}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(7); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(3); - expect($widget.attr('data-gs-auto-position')).toBe(undefined); - expect(parseInt($widget.attr('data-gs-min-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4); - expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5); - expect($widget.attr('data-gs-id')).toBe('coolWidget'); + let grid = GridStack.init({float: true});; + let widget = grid.addWidget(widgetHTML, {x: 6, y:7, width:2, height:3, autoPosition:false, + minWidth:1, maxWidth:4, minHeight:2, maxHeight:5, id:'coolWidget'}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(6); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(7); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(3); + expect(widget.getAttribute('data-gs-auto-position')).toBe(null); + expect(parseInt(widget.getAttribute('data-gs-min-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-max-width'), 10)).toBe(4); + expect(parseInt(widget.getAttribute('data-gs-min-height'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-max-height'), 10)).toBe(5); + expect(widget.getAttribute('data-gs-id')).toBe('coolWidget'); // should move widget to top with float=false expect(grid.getFloat()).toBe(true); grid.float(false); expect(grid.getFloat()).toBe(false); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(4); // <--- from 7 to 4 below second original widget - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(3); - expect($widget.attr('data-gs-auto-position')).toBe(undefined); - expect(parseInt($widget.attr('data-gs-min-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4); - expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5); - expect($widget.attr('data-gs-id')).toBe('coolWidget'); + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(6); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(4); // <--- from 7 to 4 below second original widget + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(3); + expect(widget.getAttribute('data-gs-auto-position')).toBe(null); + expect(parseInt(widget.getAttribute('data-gs-min-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-max-width'), 10)).toBe(4); + expect(parseInt(widget.getAttribute('data-gs-min-height'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-max-height'), 10)).toBe(5); + expect(widget.getAttribute('data-gs-id')).toBe('coolWidget'); // should not move again (no-op) grid.float(true); expect(grid.getFloat()).toBe(true); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(4); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(3); - expect($widget.attr('data-gs-auto-position')).toBe(undefined); - expect(parseInt($widget.attr('data-gs-min-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4); - expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2); - expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5); - expect($widget.attr('data-gs-id')).toBe('coolWidget'); + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(6); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(4); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(3); + expect(widget.getAttribute('data-gs-auto-position')).toBe(null); + expect(parseInt(widget.getAttribute('data-gs-min-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-max-width'), 10)).toBe(4); + expect(parseInt(widget.getAttribute('data-gs-min-height'), 10)).toBe(2); + expect(parseInt(widget.getAttribute('data-gs-max-height'), 10)).toBe(5); + expect(widget.getAttribute('data-gs-id')).toBe('coolWidget'); }); }); @@ -806,11 +723,11 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should change x, y coordinates for widgets.', function() { - var grid = GridStack.init({float: true}); - var widget = grid.addWidget(widgetHTML, {x:9, y:7, width:2, height:3, autoPosition:true}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).not.toBe(9); - expect(parseInt($widget.attr('data-gs-y'), 10)).not.toBe(7); + let grid = GridStack.init({float: true}); + let widget = grid.addWidget(widgetHTML, {x:9, y:7, width:2, height:3, autoPosition:true}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).not.toBe(9); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).not.toBe(7); }); }); @@ -822,78 +739,78 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should autoPosition (missing X,Y)', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {height: 2, id: 'optionWidget'}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2); - expect($widget.attr('data-gs-auto-position')).toBe('true'); - expect($widget.attr('data-gs-min-width')).toBe(undefined); - expect($widget.attr('data-gs-max-width')).toBe(undefined); - expect($widget.attr('data-gs-min-height')).toBe(undefined); - expect($widget.attr('data-gs-max-height')).toBe(undefined); - expect($widget.attr('data-gs-id')).toBe('optionWidget'); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {height: 2, id: 'optionWidget'}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(2); + // expect(widget.getAttribute('data-gs-auto-position')).toBe('true'); + expect(widget.getAttribute('data-gs-min-width')).toBe(null); + expect(widget.getAttribute('data-gs-max-width')).toBe(null); + expect(widget.getAttribute('data-gs-min-height')).toBe(null); + expect(widget.getAttribute('data-gs-max-height')).toBe(null); + expect(widget.getAttribute('data-gs-id')).toBe('optionWidget'); }); it('should autoPosition (missing X)', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {y: 9, height: 2, id: 'optionWidget'}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2); - expect($widget.attr('data-gs-auto-position')).toBe('true'); - expect($widget.attr('data-gs-min-width')).toBe(undefined); - expect($widget.attr('data-gs-max-width')).toBe(undefined); - expect($widget.attr('data-gs-min-height')).toBe(undefined); - expect($widget.attr('data-gs-max-height')).toBe(undefined); - expect($widget.attr('data-gs-id')).toBe('optionWidget'); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {y: 9, height: 2, id: 'optionWidget'}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(2); + // expect(widget.getAttribute('data-gs-auto-position')).toBe('true'); + expect(widget.getAttribute('data-gs-min-width')).toBe(null); + expect(widget.getAttribute('data-gs-max-width')).toBe(null); + expect(widget.getAttribute('data-gs-min-height')).toBe(null); + expect(widget.getAttribute('data-gs-max-height')).toBe(null); + expect(widget.getAttribute('data-gs-id')).toBe('optionWidget'); }); it('should autoPosition (missing Y)', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {x: 9, height: 2, id: 'optionWidget'}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2); - expect($widget.attr('data-gs-auto-position')).toBe('true'); - expect($widget.attr('data-gs-min-width')).toBe(undefined); - expect($widget.attr('data-gs-max-width')).toBe(undefined); - expect($widget.attr('data-gs-min-height')).toBe(undefined); - expect($widget.attr('data-gs-max-height')).toBe(undefined); - expect($widget.attr('data-gs-id')).toBe('optionWidget'); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {x: 9, height: 2, id: 'optionWidget'}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(2); + // expect(widget.getAttribute('data-gs-auto-position')).toBe('true'); + expect(widget.getAttribute('data-gs-min-width')).toBe(null); + expect(widget.getAttribute('data-gs-max-width')).toBe(null); + expect(widget.getAttribute('data-gs-min-height')).toBe(null); + expect(widget.getAttribute('data-gs-max-height')).toBe(null); + expect(widget.getAttribute('data-gs-id')).toBe('optionWidget'); }); it('should autoPosition (correct X, missing Y)', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(2); - expect($widget.attr('data-gs-auto-position')).toBe('true'); - expect($widget.attr('data-gs-min-width')).toBe(undefined); - expect($widget.attr('data-gs-max-width')).toBe(undefined); - expect($widget.attr('data-gs-min-height')).toBe(undefined); - expect($widget.attr('data-gs-max-height')).toBe(undefined); - expect($widget.attr('data-gs-id')).toBe('optionWidget'); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(2); + // expect(widget.getAttribute('data-gs-auto-position')).toBe('true'); + expect(widget.getAttribute('data-gs-min-width')).toBe(null); + expect(widget.getAttribute('data-gs-max-width')).toBe(null); + expect(widget.getAttribute('data-gs-min-height')).toBe(null); + expect(widget.getAttribute('data-gs-max-height')).toBe(null); + expect(widget.getAttribute('data-gs-id')).toBe('optionWidget'); }); it('should autoPosition (empty options)', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(1); - expect($widget.attr('data-gs-auto-position')).toBe('true'); - expect($widget.attr('data-gs-min-width')).toBe(undefined); - expect($widget.attr('data-gs-max-width')).toBe(undefined); - expect($widget.attr('data-gs-min-height')).toBe(undefined); - expect($widget.attr('data-gs-max-height')).toBe(undefined); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(1); + // expect(widget.getAttribute('data-gs-auto-position')).toBe('true'); + expect(widget.getAttribute('data-gs-min-width')).toBe(null); + expect(widget.getAttribute('data-gs-max-width')).toBe(null); + expect(widget.getAttribute('data-gs-min-height')).toBe(null); + expect(widget.getAttribute('data-gs-max-height')).toBe(null); }); }); @@ -906,13 +823,13 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should use default', function() { - var grid = GridStack.init(); - var widget = grid.addWidget(widgetHTML, {x: 'foo', y: null, width: 'bar', height: ''}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); - expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(1); - expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(1); + let grid = GridStack.init(); + let widget = grid.addWidget(widgetHTML, {x: 'foo', y: null, width: 'bar', height: ''}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); + expect(parseInt(widget.getAttribute('data-gs-width'), 10)).toBe(1); + expect(parseInt(widget.getAttribute('data-gs-height'), 10)).toBe(1); }); }); @@ -924,12 +841,12 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should clear x position', function() { - var grid = GridStack.init({float: true}); - var widgetHTML = '
'; - var widget = grid.addWidget(widgetHTML, {x:null, y:null, width:undefined}); - var $widget = $(widget); - expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8); - expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(0); + let grid = GridStack.init({float: true}); + let widgetHTML = '
'; + let widget = grid.addWidget(widgetHTML, {x:null, y:null, width:undefined}); + + expect(parseInt(widget.getAttribute('data-gs-x'), 10)).toBe(8); + expect(parseInt(widget.getAttribute('data-gs-y'), 10)).toBe(0); }); }); @@ -941,7 +858,7 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should match true/false only', function() { - var grid = GridStack.init({float: true}); + let grid = GridStack.init({float: true}); expect(grid.getFloat()).toBe(true); grid.float(0); expect(grid.getFloat()).toBe(false); @@ -962,21 +879,21 @@ describe('gridstack', function() { //document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); }); it('should cleanup gridstack', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); grid.destroy(); expect($('.grid-stack').length).toBe(0); expect(grid.engine).toBe(null); }); it('should cleanup gridstack but leave elements', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); grid.destroy(false); expect($('.grid-stack').length).toBe(1); expect($('.grid-stack-item').length).toBe(2); @@ -993,12 +910,12 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should resize widget', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); grid.resize(items[0], 5, 5); expect(parseInt($(items[0]).attr('data-gs-width'), 10)).toBe(5); expect(parseInt($(items[0]).attr('data-gs-height'), 10)).toBe(5); @@ -1013,13 +930,13 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should move widget', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, float: true }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); grid.move(items[0], 5, 5); expect(parseInt($(items[0]).attr('data-gs-x'), 10)).toBe(5); expect(parseInt($(items[0]).attr('data-gs-y'), 10)).toBe(5); @@ -1034,22 +951,22 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should do nothing and return NULL to mean nothing happened', function() { - var grid = GridStack.init(); - var items = $('.grid-stack-item'); + let grid = GridStack.init(); + let items = $('.grid-stack-item'); grid._updateElement(items[0], function(el, node) { - var hasMoved = grid.engine.moveNode(node); + let hasMoved = grid.engine.moveNode(node); expect(hasMoved).toBe(null); }); }); it('should do nothing and return node', function() { - var grid = GridStack.init(); - var items = $('.grid-stack-item'); + let grid = GridStack.init(); + let items = $('.grid-stack-item'); grid.minWidth(items[0], 1); grid.maxWidth(items[0], 2); grid.minHeight(items[0], 1); grid.maxHeight(items[0], 2); grid._updateElement(items[0], function(el, node) { - var newNode = grid.engine.moveNode(node); + let newNode = grid.engine.moveNode(node); expect(newNode).toBe(node); }); }); @@ -1063,13 +980,13 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should move and resize widget', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, float: true }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); grid.update(items[0], 5, 5, 5 ,5); expect(parseInt($(items[0]).attr('data-gs-width'), 10)).toBe(5); expect(parseInt($(items[0]).attr('data-gs-height'), 10)).toBe(5); @@ -1086,39 +1003,39 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should return verticalMargin', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); - var vm = grid.verticalMargin(); + let grid = GridStack.init(options); + let vm = grid.getVerticalMargin(); expect(vm).toBe(10); }); it('should return update verticalMargin', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); grid.verticalMargin(11); - expect(grid.verticalMargin()).toBe(11); + expect(grid.getVerticalMargin()).toBe(11); }); it('should do nothing', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, }; - var grid = GridStack.init(options); - expect(grid.verticalMargin()).toBe(10); + let grid = GridStack.init(options); + expect(grid.getVerticalMargin()).toBe(10); grid.verticalMargin(10); - expect(grid.verticalMargin()).toBe(10); + expect(grid.getVerticalMargin()).toBe(10); }); it('should not update styles', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); spyOn(grid, '_updateStyles'); grid.verticalMargin(11, true); expect(grid._updateStyles).not.toHaveBeenCalled(); @@ -1133,20 +1050,20 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should add grid-stack-rtl class', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, rtl: true }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); expect($('.grid-stack').hasClass('grid-stack-rtl')).toBe(true); }); it('should not add grid-stack-rtl class', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); expect($('.grid-stack').hasClass('grid-stack-rtl')).toBe(false); }); }); @@ -1158,33 +1075,42 @@ describe('gridstack', function() { afterEach(function() { document.body.removeChild(document.getElementById('gs-cont')); }); - it('should enable move', function() { - var options = { + it('should enable move for future also', function() { + let options = { cellHeight: 80, verticalMargin: 10, minWidth: 1, disableDrag: true }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { + expect($(items[i]).hasClass('ui-draggable-disabled')).toBe(true); + } expect(grid.opts.disableDrag).toBe(true); + grid.enableMove(true, true); - for (var i = 0; i < items.length; i++) { - expect($(items[i]).hasClass('ui-draggable-handle')).toBe(true); + for (let i = 0; i < items.length; i++) { + expect($(items[i]).hasClass('ui-draggable-disabled')).toBe(false); } expect(grid.opts.disableDrag).toBe(false); }); - it('should disable move', function() { - var options = { + it('should disable move for existing only', function() { + let options = { cellHeight: 80, verticalMargin: 10, minWidth: 1 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - grid.enableMove(false); - for (var i = 0; i < items.length; i++) { - expect($(items[i]).hasClass('ui-draggable-handle')).toBe(false); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { + expect($(items[i]).hasClass('ui-draggable-disabled')).toBe(false); + } + expect(grid.opts.disableDrag).toBe(false); + + grid.enableMove(false, false); + for (let i = 0; i < items.length; i++) { + expect($(items[i]).hasClass('ui-draggable-disabled')).toBe(true); } expect(grid.opts.disableDrag).toBe(false); }); @@ -1198,31 +1124,31 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should enable resize', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, minWidth: 1, disableResize: true }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); expect(grid.opts.disableResize).toBe(true); grid.enableResize(true, true); - for (var i = 0; i < items.length; i++) { + for (let i = 0; i < items.length; i++) { expect(($(items[i]).resizable('option','disabled'))).toBe(false); } expect(grid.opts.disableResize).toBe(false); }); it('should disable resize', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, minWidth: 1 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); - grid.enableResize(false); - for (var i = 0; i < items.length; i++) { + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); + grid.enableResize(false, false); + for (let i = 0; i < items.length; i++) { expect(($(items[i]).resizable('option','disabled'))).toBe(true); } expect(grid.opts.disableResize).toBe(false); @@ -1237,22 +1163,22 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should enable movable and resizable', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10, minWidth: 1 }; - var grid = GridStack.init(options); - var items = $('.grid-stack-item'); + let grid = GridStack.init(options); + let items = $('.grid-stack-item'); grid.enableResize(false); grid.enableMove(false); - for (var i = 0; i < items.length; i++) { - expect($(items[i]).hasClass('ui-draggable-handle')).toBe(false); + for (let i = 0; i < items.length; i++) { + expect($(items[i]).hasClass('ui-draggable-disabled')).toBe(true); expect(($(items[i]).resizable('option','disabled'))).toBe(true); } grid.enable(); - for (var j = 0; j < items.length; j++) { - expect($(items[j]).hasClass('ui-draggable-handle')).toBe(true); + for (let j = 0; j < items.length; j++) { + expect($(items[j]).hasClass('ui-draggable-disabled')).toBe(false); expect(($(items[j]).resizable('option','disabled'))).toBe(false); } }); @@ -1266,22 +1192,22 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should lock widgets', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); grid.locked('.grid-stack-item', true); $('.grid-stack-item').each(function (i,item) { expect($(item).attr('data-gs-locked')).toBe('yes'); }) }); it('should unlock widgets', function() { - var options = { + let options = { cellHeight: 80, verticalMargin: 10 }; - var grid = GridStack.init(options); + let grid = GridStack.init(options); grid.locked('.grid-stack-item', false); $('.grid-stack-item').each(function (i,item) { expect($(item).attr('data-gs-locked')).toBe(undefined); @@ -1290,7 +1216,7 @@ describe('gridstack', function() { }); describe('custom grid placement #1054', function() { - var HTML = + let HTML = '
' + '
' + '
' + @@ -1307,7 +1233,7 @@ describe('gridstack', function() { '
' + '
' + '
'; - var pos = [{x:0, y:0, w:12, h:9}, {x:0, y:9, w:12, h:5}, {x:0, y:14, w:7, h:6}, {x:7, y:14, w:5, h:6}]; + let pos = [{x:0, y:0, w:12, h:9}, {x:0, y:9, w:12, h:5}, {x:0, y:14, w:7, h:6}, {x:7, y:14, w:5, h:6}]; beforeEach(function() { document.body.insertAdjacentHTML('afterbegin', HTML); }); @@ -1315,9 +1241,9 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should have correct position', function() { - var items = $('.grid-stack-item'); - for (var i = 0; i < items.length; i++) { - var item = $(items[i]); + let items = $('.grid-stack-item'); + for (let i = 0; i < items.length; i++) { + let item = $(items[i]); expect(parseInt(item.attr('data-gs-x'))).toBe(pos[i].x); expect(parseInt(item.attr('data-gs-y'))).toBe(pos[i].y); expect(parseInt(item.attr('data-gs-width'))).toBe(pos[i].w); @@ -1334,9 +1260,9 @@ describe('gridstack', function() { document.body.removeChild(document.getElementById('gs-cont')); }); it('should move all 3 items to top-left with no space', function() { - var grid = GridStack.init({float: true}); + let grid = GridStack.init({float: true}); - var el3 = $(grid.addWidget(widgetHTML, {x: 3, y: 5})); + let el3 = $(grid.addWidget(widgetHTML, {x: 3, y: 5})); expect(parseInt(el3.attr('data-gs-x'))).toBe(3); expect(parseInt(el3.attr('data-gs-y'))).toBe(5); @@ -1345,9 +1271,9 @@ describe('gridstack', function() { expect(parseInt(el3.attr('data-gs-y'))).toBe(0); }); it('not move locked item', function() { - var grid = GridStack.init({float: true}); + let grid = GridStack.init({float: true}); - var el3 = $(grid.addWidget(widgetHTML, {x: 3, y: 5, locked: true, noMove: true})); + let el3 = $(grid.addWidget(widgetHTML, {x: 3, y: 5, locked: true, noMove: true})); expect(parseInt(el3.attr('data-gs-x'))).toBe(3); expect(parseInt(el3.attr('data-gs-y'))).toBe(5); @@ -1357,10 +1283,7 @@ describe('gridstack', function() { }); }); - - /* - * ...and finally track log warnings at the end, instead of displaying them.... - */ + // ..and finally track log warnings at the end, instead of displaying them.... describe('obsolete warnings', function() { console.warn = jasmine.createSpy('log'); // track warnings instead of displaying them beforeEach(function() { @@ -1369,20 +1292,22 @@ describe('gridstack', function() { afterEach(function() { document.body.removeChild(document.getElementById('gs-cont')); }); + /* TODO: it('warning if OLD setGridWidth is called', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); grid.setGridWidth(11); // old 0.5.2 API - expect(grid.column()).toBe(11); + expect(grid.getColumn()).toBe(11); expect(console.warn).toHaveBeenCalledWith('gridstack.js: Function `setGridWidth` is deprecated in v0.5.3 and has been replaced with `column`. It will be **completely** removed in v1.0'); }); it('warning if OLD setColumn is called', function() { - var grid = GridStack.init(); + let grid = GridStack.init(); grid.setColumn(10); // old 0.6.4 API - expect(grid.column()).toBe(10); + expect(grid.getColumn()).toBe(10); expect(console.warn).toHaveBeenCalledWith('gridstack.js: Function `setColumn` is deprecated in v0.6.4 and has been replaced with `column`. It will be **completely** removed in v1.0'); }); + */ it('warning if OLD grid height is set', function() { - var grid = GridStack.init({height: 10}); // old 0.5.2 Opt now maxRow + let grid = GridStack.init({height: 10}); // old 0.5.2 Opt now maxRow expect(grid.opts.maxRow).toBe(10); expect(grid.engine.maxRow).toBe(10); expect(console.warn).toHaveBeenCalledWith('gridstack.js: Option `height` is deprecated in v0.5.3 and has been replaced with `maxRow`. It will be **completely** removed in v1.0'); diff --git a/spec/utils-spec.js b/spec/utils-spec.js index c2259eb43..c134eb430 100644 --- a/spec/utils-spec.js +++ b/spec/utils-spec.js @@ -1,7 +1,7 @@ describe('gridstack utils', function() { 'use strict'; - var utils; + let utils; beforeEach(function() { utils = GridStack.Utils; @@ -11,7 +11,7 @@ describe('gridstack utils', function() { it('should set gridstack utils.', function() { expect(utils).not.toBeNull(); - expect(typeof utils).toBe('object'); + expect(typeof utils).toBe('function'); }); }); @@ -40,7 +40,7 @@ describe('gridstack utils', function() { }); describe('test isIntercepted', function() { - var src = {x: 3, y: 2, width: 3, height: 2}; + let src = {x: 3, y: 2, width: 3, height: 2}; it('should intercept.', function() { expect(utils.isIntercepted(src, {x: 0, y: 0, width: 4, height: 3})).toEqual(true); @@ -61,11 +61,11 @@ describe('gridstack utils', function() { describe('test createStylesheet/removeStylesheet', function() { it('should create/remove style DOM', function() { - var _id = 'test-123'; + let _id = 'test-123'; utils.createStylesheet(_id); - var style = $('STYLE[data-gs-style-id=' + _id + ']'); + let style = $('STYLE[data-gs-style-id=' + _id + ']'); expect(style.length).toEqual(1); expect(style.prop('tagName')).toEqual('STYLE'); @@ -111,7 +111,7 @@ describe('gridstack utils', function() { describe('test defaults', function() { it('should assign missing field or undefined', function() { - var src = {}; + let src = {}; expect(src).toEqual({}); expect(utils.defaults(src, {x: 1, y: 2})).toEqual({x: 1, y: 2}); expect(utils.defaults(src, {x: 10})).toEqual({x: 1, y: 2}); diff --git a/src/gridstack.ts b/src/gridstack.ts index 891be845e..8384cbe8a 100644 --- a/src/gridstack.ts +++ b/src/gridstack.ts @@ -44,18 +44,26 @@ function getElement(els: GridStackElement): GridItemHTMLElement { (document.querySelector(els) || document.querySelector('#' + els) || document.querySelector('.' + els)) : els); } function getElements(els: GridStackElement): GridItemHTMLElement[] { - return (typeof els === 'string' ? Array.from( - document.querySelectorAll(els) || document.querySelectorAll('#' + els) || document.querySelectorAll('.' + els) - ) : [els]); + if (typeof els === 'string') { + let list = document.querySelectorAll(els); + if (!list.length) { list = document.querySelectorAll('.' + els) } + if (!list.length) { list = document.querySelectorAll('#' + els) } + return Array.from(list) as GridItemHTMLElement[]; + } + return [els]; } function getGridElement(els: string | HTMLElement): GridHTMLElement { return (typeof els === 'string' ? (document.querySelector(els) || document.querySelector('#' + els) || document.querySelector('.' + els)) : els); } function getGridElements(els: string | HTMLElement): GridHTMLElement[] { - return (typeof els === 'string' ? Array.from( - document.querySelectorAll(els) || document.querySelectorAll('#' + els) || document.querySelectorAll('.' + els) - ) : [els]); + if (typeof els === 'string') { + let list = document.querySelectorAll(els); + if (!list.length) { list = document.querySelectorAll('.' + els) } + if (!list.length) { list = document.querySelectorAll('#' + els) } + return Array.from(list) as GridHTMLElement[]; + } + return [els]; } /** @@ -354,8 +362,10 @@ export class GridStack { // support legacy call for now ? if (arguments.length > 2) { console.warn('gridstack.ts: `addWidget(el, x, y, width...)` is deprecated. Use `addWidget(el, {x, y, width,...})`. It will be removed soon'); + // eslint-disable-next-line prefer-rest-params let a = arguments, i = 1, - opt: GridstackWidget = { x:a[i++], y:a[i++], width:a[i++], height:a[i++], autoPosition:a[i++], minWidth:a[i++], maxWidth:a[i++], minHeight:a[i++], maxHeight:a[i++], id:a[i++] }; + opt: GridstackWidget = { x:a[i++], y:a[i++], width:a[i++], height:a[i++], autoPosition:a[i++], + minWidth:a[i++], maxWidth:a[i++], minHeight:a[i++], maxHeight:a[i++], id:a[i++] }; return this.addWidget(el, opt); } @@ -672,12 +682,11 @@ export class GridStack { * @param val A numeric value of the number of columns */ public maxWidth(els: GridStackElement, val: number): GridStack { - if (isNaN(val)) return; getElements(els).forEach(el => { let node = el.gridstackNode; if (!node) { return; } node.maxWidth = (val || undefined); - if (node.maxWidth) { + if (val) { el.setAttribute('data-gs-max-width', String(val)); } else { el.removeAttribute('data-gs-max-width'); @@ -692,11 +701,10 @@ export class GridStack { * @param val A numeric value of the number of columns */ public minWidth(els: GridStackElement, val: number): GridStack { - if (isNaN(val)) return; getElements(els).forEach(el => { let node = el.gridstackNode; if (!node) { return; } - if (node.minWidth) { + if (val) { el.setAttribute('data-gs-min-width', String(val)); } else { el.removeAttribute('data-gs-min-width'); @@ -711,11 +719,10 @@ export class GridStack { * @param val A numeric value of the number of rows */ public maxHeight(els: GridStackElement, val: number): GridStack { - if (isNaN(val)) return; getElements(els).forEach(el => { let node = el.gridstackNode; if (!node) { return; } - if (node.maxHeight) { + if (val) { el.setAttribute('data-gs-max-height', String(val)); } else { el.removeAttribute('data-gs-max-height'); @@ -730,11 +737,10 @@ export class GridStack { * @param val A numeric value of the number of rows */ public minHeight(els: GridStackElement, val: number): GridStack { - if (isNaN(val)) return; getElements(els).forEach(el => { let node = el.gridstackNode; if (!node) { return; } - if (node.minHeight) { + if (val) { el.setAttribute('data-gs-min-height', String(val)); } else { el.removeAttribute('data-gs-min-height'); @@ -1644,4 +1650,5 @@ export class GridStack { // legacy method renames private setGridWidth = obsolete(GridStack.prototype.column, 'setGridWidth', 'column', 'v0.5.3'); private setColumn = obsolete(GridStack.prototype.column, 'setColumn', 'column', 'v0.6.4'); + private getGridHeight = obsolete(GridStackEngine.prototype.getRow, 'getGridHeight', 'getRow', 'v1.0.0'); } diff --git a/src/utils.ts b/src/utils.ts index 94d10b013..fe6d485f9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -152,7 +152,7 @@ export class Utils { sources.forEach(function (source) { for (let prop in source) { - if (source.hasOwnProperty(prop) && (!target.hasOwnProperty(prop) || target[prop] === undefined)) { + if (source.hasOwnProperty(prop) && (target[prop] === null || target[prop] === undefined)) { target[prop] = source[prop]; } }