From b9c916554e28a9f56c4eaf723f1cb6288c6a70f7 Mon Sep 17 00:00:00 2001 From: d Date: Thu, 25 Feb 2016 15:48:25 -0500 Subject: [PATCH 1/2] Added parseInt(x, 10) to cellWidth and added some unit tests. --- dist/gridstack.js | 2 +- dist/gridstack.min.js | 2 +- dist/gridstack.min.map | 2 +- spec/gridstack-spec.js | 315 ++++++++++++++++++++++++++++++++++++++++- src/gridstack.js | 2 +- 5 files changed, 318 insertions(+), 5 deletions(-) diff --git a/dist/gridstack.js b/dist/gridstack.js index 1535571..0909f97 100644 --- a/dist/gridstack.js +++ b/dist/gridstack.js @@ -1360,7 +1360,7 @@ GridStack.prototype.cellWidth = function() { var o = this.container.children('.' + this.opts.itemClass).first(); - return Math.ceil(o.outerWidth() / o.attr('data-gs-width')); + return Math.ceil(o.outerWidth() / parseInt(o.attr('data-gs-width'), 10)); }; GridStack.prototype.getCellFromPixel = function(position, useOffset) { diff --git a/dist/gridstack.min.js b/dist/gridstack.min.js index 0a7a8e5..d1110e6 100644 --- a/dist/gridstack.min.js +++ b/dist/gridstack.min.js @@ -16,5 +16,5 @@ h.prototype.getNodeDataByDOMEl=function(a){return b.find(this.nodes,function(b){ // jscs:enable requireCamelCaseOrUpperCaseIdentifiers return i.prototype._triggerChangeEvent=function(a){var b=this.grid.getDirtyNodes(),c=!1,d=[];b&&b.length&&(d.push(b),c=!0),(c||a===!0)&&this.container.trigger("change",d)},i.prototype._triggerAddEvent=function(){this.grid._addedNodes&&this.grid._addedNodes.length>0&&(this.container.trigger("added",[b.map(this.grid._addedNodes,b.clone)]),this.grid._addedNodes=[])},i.prototype._triggerRemoveEvent=function(){this.grid._removedNodes&&this.grid._removedNodes.length>0&&(this.container.trigger("removed",[b.map(this.grid._removedNodes,b.clone)]),this.grid._removedNodes=[])},i.prototype._initStyles=function(){this._stylesId&&f.removeStylesheet(this._stylesId),this._stylesId="gridstack-style-"+(1e5*Math.random()).toFixed(),this._styles=f.createStylesheet(this._stylesId),null!==this._styles&&(this._styles._max=0)},i.prototype._updateStyles=function(a){if(null!==this._styles&&"undefined"!=typeof this._styles){var b,c="."+this.opts._class+" ."+this.opts.itemClass,d=this;if("undefined"==typeof a&&(a=this._styles._max,this._initStyles(),this._updateContainerHeight()),this.opts.cellHeight&&!(0!==this._styles._max&&a<=this._styles._max)&&(b=this.opts.verticalMargin&&this.opts.cellHeightUnit!==this.opts.verticalMarginUnit?function(a,b){return a&&b?"calc("+(d.opts.cellHeight*a+d.opts.cellHeightUnit)+" + "+(d.opts.verticalMargin*b+d.opts.verticalMarginUnit)+")":d.opts.cellHeight*a+d.opts.verticalMargin*b+d.opts.cellHeightUnit}:function(a,b){return d.opts.cellHeight*a+d.opts.verticalMargin*b+d.opts.cellHeightUnit},0===this._styles._max&&f.insertCSSRule(this._styles,c,"min-height: "+b(1,0)+";",0),a>this._styles._max)){for(var e=this._styles._max;a>e;++e)f.insertCSSRule(this._styles,c+'[data-gs-height="'+(e+1)+'"]',"height: "+b(e+1,e)+";",e),f.insertCSSRule(this._styles,c+'[data-gs-min-height="'+(e+1)+'"]',"min-height: "+b(e+1,e)+";",e),f.insertCSSRule(this._styles,c+'[data-gs-max-height="'+(e+1)+'"]',"max-height: "+b(e+1,e)+";",e),f.insertCSSRule(this._styles,c+'[data-gs-y="'+e+'"]',"top: "+b(e,e)+";",e);this._styles._max=a}}},i.prototype._updateContainerHeight=function(){if(!this.grid._updateCounter){var a=this.grid.getGridHeight();this.container.attr("data-gs-current-height",a),this.opts.cellHeight&&(this.opts.verticalMargin?this.opts.cellHeightUnit===this.opts.verticalMarginUnit?this.container.css("height",a*(this.opts.cellHeight+this.opts.verticalMargin)-this.opts.verticalMargin+this.opts.cellHeightUnit):this.container.css("height","calc("+(a*this.opts.cellHeight+this.opts.cellHeightUnit)+" + "+(a*(this.opts.verticalMargin-1)+this.opts.verticalMarginUnit)+")"):this.container.css("height",a*this.opts.cellHeight+this.opts.cellHeightUnit))}},i.prototype._isOneColumnMode=function(){return(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)<=this.opts.minWidth},i.prototype._setupRemovingTimeout=function(b){var c=this,d=a(b).data("_gridstack_node");!d._removeTimeout&&c.opts.removable&&(d._removeTimeout=setTimeout(function(){b.addClass("grid-stack-item-removing"),d._isAboutToRemove=!0},c.opts.removeTimeout))},i.prototype._clearRemovingTimeout=function(b){var c=a(b).data("_gridstack_node");c._removeTimeout&&(clearTimeout(c._removeTimeout),c._removeTimeout=null,b.removeClass("grid-stack-item-removing"),c._isAboutToRemove=!1)},i.prototype._prepareElement=function(c,d){d="undefined"!=typeof d?d:!1;var e=this;c=a(c),c.addClass(this.opts.itemClass);var g=e.grid.addNode({x:c.attr("data-gs-x"),y:c.attr("data-gs-y"),width:c.attr("data-gs-width"),height:c.attr("data-gs-height"),maxWidth:c.attr("data-gs-max-width"),minWidth:c.attr("data-gs-min-width"),maxHeight:c.attr("data-gs-max-height"),minHeight:c.attr("data-gs-min-height"),autoPosition:f.toBool(c.attr("data-gs-auto-position")),noResize:f.toBool(c.attr("data-gs-no-resize")),noMove:f.toBool(c.attr("data-gs-no-move")),locked:f.toBool(c.attr("data-gs-locked")),el:c,id:c.attr("data-gs-id"),_grid:e},d);c.data("_gridstack_node",g);var h,i,j=function(a,b){var d,f,j=Math.round(b.position.left/h),k=Math.floor((b.position.top+i/2)/i);if("drag"!=a.type&&(d=Math.round(b.size.width/h),f=Math.round(b.size.height/i)),"drag"==a.type)0>j||j>=e.grid.width||0>k?(e.opts.removable===!0&&e._setupRemovingTimeout(c),j=g._beforeDragX,k=g._beforeDragY,e.placeholder.detach(),e.placeholder.hide(),e.grid.removeNode(g),e._updateContainerHeight(),g._temporaryRemoved=!0):(e._clearRemovingTimeout(c),g._temporaryRemoved&&(e.grid.addNode(g),e.placeholder.attr("data-gs-x",j).attr("data-gs-y",k).attr("data-gs-width",d).attr("data-gs-height",f).show(),e.container.append(e.placeholder),g.el=e.placeholder,g._temporaryRemoved=!1));else if("resize"==a.type&&0>j)return;e.grid.canMoveNode(g,j,k,d,f)&&(e.grid.moveNode(g,j,k,d,f),e._updateContainerHeight())},k=function(b,d){e.container.append(e.placeholder);var f=a(this);e.grid.cleanNodes(),e.grid.beginUpdate(g),h=Math.ceil(f.outerWidth()/f.attr("data-gs-width"));var j=Math.ceil(f.outerHeight()/f.attr("data-gs-height"));i=e.container.height()/parseInt(e.container.attr("data-gs-current-height")),e.placeholder.attr("data-gs-x",f.attr("data-gs-x")).attr("data-gs-y",f.attr("data-gs-y")).attr("data-gs-width",f.attr("data-gs-width")).attr("data-gs-height",f.attr("data-gs-height")).show(),g.el=e.placeholder,g._beforeDragX=g.x,g._beforeDragY=g.y,c.resizable("option","minWidth",h*(g.minWidth||1)),c.resizable("option","minHeight",j*(g.minHeight||1)),"resizestart"==b.type&&f.find(".grid-stack-item").trigger("resizestart")},l=function(b,d){var f=!1;e.placeholder.detach();var h=a(this);g.el=h,e.placeholder.hide(),g._isAboutToRemove?(f=!0,c.removeData("_gridstack_node"),c.remove()):(e._clearRemovingTimeout(c),g._temporaryRemoved?(h.attr("data-gs-x",g._beforeDragX).attr("data-gs-y",g._beforeDragY).attr("data-gs-width",g.width).attr("data-gs-height",g.height).removeAttr("style"),g.x=g._beforeDragX,g.y=g._beforeDragY,e.grid.addNode(g)):h.attr("data-gs-x",g.x).attr("data-gs-y",g.y).attr("data-gs-width",g.width).attr("data-gs-height",g.height).removeAttr("style")),e._updateContainerHeight(),e._triggerChangeEvent(f),e.grid.endUpdate();var i=h.find(".grid-stack");i.length&&"resizestop"==b.type&&(i.each(function(b,c){a(c).data("gridstack").onResizeHandler()}),h.find(".grid-stack-item").trigger("resizestop"))};c.draggable(b.extend(this.opts.draggable,{containment:this.opts.isNested?this.container.parent():null,start:k,stop:l,drag:j})).resizable(b.extend(this.opts.resizable,{start:k,stop:l,resize:j})),(g.noMove||this._isOneColumnMode()||this.opts.staticGrid||this.opts.disableDrag)&&c.draggable("disable"),(g.noResize||this._isOneColumnMode()||this.opts.staticGrid||this.opts.disableResize)&&c.resizable("disable"),c.attr("data-gs-locked",g.locked?"yes":null)},i.prototype.setAnimation=function(a){a?this.container.addClass("grid-stack-animate"):this.container.removeClass("grid-stack-animate")},i.prototype.addWidget=function(b,c,d,e,f,g,h,i,j,k,l){return b=a(b),"undefined"!=typeof c&&b.attr("data-gs-x",c),"undefined"!=typeof d&&b.attr("data-gs-y",d),"undefined"!=typeof e&&b.attr("data-gs-width",e),"undefined"!=typeof f&&b.attr("data-gs-height",f),"undefined"!=typeof g&&b.attr("data-gs-auto-position",g?"yes":null),"undefined"!=typeof h&&b.attr("data-gs-min-width",h),"undefined"!=typeof i&&b.attr("data-gs-max-width",i),"undefined"!=typeof j&&b.attr("data-gs-min-height",j),"undefined"!=typeof k&&b.attr("data-gs-max-height",k),"undefined"!=typeof l&&b.attr("data-gs-id",l),this.container.append(b),this._prepareElement(b,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),b},i.prototype.makeWidget=function(b){return b=a(b),this._prepareElement(b,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),b},i.prototype.willItFit=function(a,b,c,d,e){var f={x:a,y:b,width:c,height:d,autoPosition:e};return this.grid.canBePlacedWithRespectToHeight(f)},i.prototype.removeWidget=function(b,c){c="undefined"==typeof c?!0:c,b=a(b);var d=b.data("_gridstack_node"); // For Meteor support: https://github.com/troolee/gridstack.js/pull/272 -d||(d=this.grid.getNodeDataByDOMEl(b)),this.grid.removeNode(d,c),b.removeData("_gridstack_node"),this._updateContainerHeight(),c&&b.remove(),this._triggerChangeEvent(!0),this._triggerRemoveEvent()},i.prototype.removeAll=function(a){b.each(this.grid.nodes,b.bind(function(b){this.removeWidget(b.el,a)},this)),this.grid.nodes=[],this._updateContainerHeight()},i.prototype.destroy=function(b){a(window).off("resize",this.onResizeHandler),this.disable(),"undefined"==typeof b||b?this.container.remove():this.removeAll(!1),f.removeStylesheet(this._stylesId),this.grid&&(this.grid=null)},i.prototype.resizable=function(b,c){var d=this;return b=a(b),b.each(function(b,e){e=a(e);var f=e.data("_gridstack_node");"undefined"!=typeof f&&null!==f&&(f.noResize=!c,f.noResize||d._isOneColumnMode()?e.resizable("disable"):e.resizable("enable"))}),this},i.prototype.movable=function(b,c){var d=this;return b=a(b),b.each(function(b,e){e=a(e);var f=e.data("_gridstack_node");"undefined"!=typeof f&&null!==f&&(f.noMove=!c,f.noMove||d._isOneColumnMode()?(e.draggable("disable"),e.removeClass("ui-draggable-handle")):(e.draggable("enable"),e.addClass("ui-draggable-handle")))}),this},i.prototype.enableMove=function(a,b){this.movable(this.container.children("."+this.opts.itemClass),a),b&&(this.opts.disableDrag=!a)},i.prototype.enableResize=function(a,b){this.resizable(this.container.children("."+this.opts.itemClass),a),b&&(this.opts.disableResize=!a)},i.prototype.disable=function(){this.movable(this.container.children("."+this.opts.itemClass),!1),this.resizable(this.container.children("."+this.opts.itemClass),!1),this.container.trigger("disable")},i.prototype.enable=function(){this.movable(this.container.children("."+this.opts.itemClass),!0),this.resizable(this.container.children("."+this.opts.itemClass),!0),this.container.trigger("enable")},i.prototype.locked=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!==e&&(e.locked=c||!1,d.attr("data-gs-locked",e.locked?"yes":null))}),this},i.prototype.maxHeight=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!==e&&(isNaN(c)||(e.maxHeight=c||!1,d.attr("data-gs-max-height",c)))}),this},i.prototype.minHeight=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!==e&&(isNaN(c)||(e.minHeight=c||!1,d.attr("data-gs-min-height",c)))}),this},i.prototype.maxWidth=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!==e&&(isNaN(c)||(e.maxWidth=c||!1,d.attr("data-gs-max-width",c)))}),this},i.prototype.minWidth=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!==e&&(isNaN(c)||(e.minWidth=c||!1,d.attr("data-gs-min-width",c)))}),this},i.prototype._updateElement=function(b,c){b=a(b).first();var d=b.data("_gridstack_node");if("undefined"!=typeof d&&null!==d){var e=this;e.grid.cleanNodes(),e.grid.beginUpdate(d),c.call(this,b,d),e._updateContainerHeight(),e._triggerChangeEvent(),e.grid.endUpdate()}},i.prototype.resize=function(a,b,c){this._updateElement(a,function(a,d){b=null!==b&&"undefined"!=typeof b?b:d.width,c=null!==c&&"undefined"!=typeof c?c:d.height,this.grid.moveNode(d,d.x,d.y,b,c)})},i.prototype.move=function(a,b,c){this._updateElement(a,function(a,d){b=null!==b&&"undefined"!=typeof b?b:d.x,c=null!==c&&"undefined"!=typeof c?c:d.y,this.grid.moveNode(d,b,c,d.width,d.height)})},i.prototype.update=function(a,b,c,d,e){this._updateElement(a,function(a,f){b=null!==b&&"undefined"!=typeof b?b:f.x,c=null!==c&&"undefined"!=typeof c?c:f.y,d=null!==d&&"undefined"!=typeof d?d:f.width,e=null!==e&&"undefined"!=typeof e?e:f.height,this.grid.moveNode(f,b,c,d,e)})},i.prototype.verticalMargin=function(a,b){if("undefined"==typeof a)return this.opts.verticalMargin;var c=f.parseHeight(a);this.opts.verticalMarginUnit===c.unit&&this.opts.height===c.height||(this.opts.verticalMarginUnit=c.unit,this.opts.verticalMargin=c.height,b||this._updateStyles())},i.prototype.cellHeight=function(a,b){if("undefined"==typeof a){if(this.opts.cellHeight)return this.opts.cellHeight;var c=this.container.children("."+this.opts.itemClass).first();return Math.ceil(c.outerHeight()/c.attr("data-gs-height"))}var d=f.parseHeight(a);this.opts.cellHeightUnit===d.heightUnit&&this.opts.height===d.height||(this.opts.cellHeightUnit=d.unit,this.opts.cellHeight=d.height,b||this._updateStyles())},i.prototype.cellWidth=function(){var a=this.container.children("."+this.opts.itemClass).first();return Math.ceil(a.outerWidth()/a.attr("data-gs-width"))},i.prototype.getCellFromPixel=function(a,b){var c="undefined"!=typeof b&&b?this.container.offset():this.container.position(),d=a.left-c.left,e=a.top-c.top,f=Math.floor(this.container.width()/this.opts.width),g=Math.floor(this.container.height()/parseInt(this.container.attr("data-gs-current-height")));return{x:Math.floor(d/f),y:Math.floor(e/g)}},i.prototype.batchUpdate=function(){this.grid.batchUpdate()},i.prototype.commit=function(){this.grid.commit(),this._updateContainerHeight()},i.prototype.isAreaEmpty=function(a,b,c,d){return this.grid.isAreaEmpty(a,b,c,d)},i.prototype.setStatic=function(a){this.opts.staticGrid=a===!0,this.enableMove(!a),this.enableResize(!a),this._setStaticClass()},i.prototype._setStaticClass=function(){var a="grid-stack-static";this.opts.staticGrid===!0?this.container.addClass(a):this.container.removeClass(a)},i.prototype._updateNodeWidths=function(a,b){this.grid._sortNodes(),this.grid.batchUpdate();for(var c={},d=0;d' + + '
' + + '
' + + '
' + + '
' + + '
' + + '
' + + ''; beforeEach(function() { w = window; @@ -87,6 +100,306 @@ describe('gridstack', function() { }); + describe('grid.setAnimation', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should add class grid-stack-animate to the container.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + $('.grid-stack').removeClass('grid-stack-animate'); + var grid = $('.grid-stack').data('gridstack'); + 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 = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + $('.grid-stack').addClass('grid-stack-animate'); + var grid = $('.grid-stack').data('gridstack'); + grid.setAnimation(false); + expect($('.grid-stack').hasClass('grid-stack-animate')).toBe(false); + }); + }); + + describe('grid._setStaticClass', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should add class grid-stack-static to the container.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10, + staticGrid: true + }; + $('.grid-stack').gridstack(options); + $('.grid-stack').removeClass('grid-stack-static'); + var grid = $('.grid-stack').data('gridstack'); + grid._setStaticClass(); + expect($('.grid-stack').hasClass('grid-stack-static')).toBe(true); + }); + it('should remove class grid-stack-static from the container.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10, + staticGrid: false + }; + $('.grid-stack').gridstack(options); + $('.grid-stack').addClass('grid-stack-static'); + var grid = $('.grid-stack').data('gridstack'); + grid._setStaticClass(); + expect($('.grid-stack').hasClass('grid-stack-static')).toBe(false); + }); + }); + + describe('grid.getCellFromPixel', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should return {x: 2, y: 1}.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var pixel = {top: 100, left: 72}; + var cell = grid.getCellFromPixel(pixel); + expect(cell.x).toBe(2); + expect(cell.y).toBe(1); + }); + it('should return {x: 2, y: 1}.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var pixel = {top: 100, left: 72}; + var cell = grid.getCellFromPixel(pixel, false); + expect(cell.x).toBe(2); + expect(cell.y).toBe(1); + }); + it('should return {x: 2, y: 1}.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var pixel = {top: 100, left: 72}; + var cell = grid.getCellFromPixel(pixel, true); + expect(cell.x).toBe(2); + expect(cell.y).toBe(1); + }); + }); + + describe('grid.cellWidth', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should return 96.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + expect(grid.cellWidth()).toBe(96); + }); + }); + + describe('grid.minWidth', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should set data-gs-min-width to 2.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var items = $('.grid-stack-item'); + for (var i = 0; i < items.length; i++) { + grid.minWidth(items[i], 2); + } + for (var j = 0; j < items.length; j++) { + expect(parseInt($(items[j]).attr('data-gs-min-width'), 10)).toBe(2); + } + }); + }); + + describe('grid.maxWidth', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should set data-gs-min-width to 2.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var items = $('.grid-stack-item'); + for (var i = 0; i < items.length; i++) { + grid.maxWidth(items[i], 2); + } + for (var j = 0; j < items.length; j++) { + expect(parseInt($(items[j]).attr('data-gs-max-width'), 10)).toBe(2); + } + }); + }); + + describe('grid.minHeight', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should set data-gs-min-height to 2.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + var items = $('.grid-stack-item'); + for (var i = 0; i < items.length; i++) { + grid.minHeight(items[i], 2); + } + for (var j = 0; j < items.length; j++) { + expect(parseInt($(items[j]).attr('data-gs-min-height'), 10)).toBe(2); + } + }); + }); + + describe('grid.maxHeight', function() { + beforeEach(function() { + document.body.insertAdjacentHTML( + 'afterbegin', gridstackHTML); + }); + afterEach(function() { + document.body.removeChild(document.getElementsByClassName('grid-stack')[0]); + }); + it('should set data-gs-min-height to 2.', function() { + var options = { + cellHeight: 80, + verticalMargin: 10 + }; + $('.grid-stack').gridstack(options); + var grid = $('.grid-stack').data('gridstack'); + 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++) { + expect(parseInt($(items[j]).attr('data-gs-max-height'), 10)).toBe(2); + } + }); + }); +}); + +/* + GridStack.prototype.minWidth = function(el, val) { + el = $(el); + el.each(function(index, el) { + el = $(el); + var node = el.data('_gridstack_node'); + if (typeof node === 'undefined' || node === null) { + return; + } + + if (!isNaN(val)) { + node.minWidth = (val || false); + el.attr('data-gs-min-width', val); + } + }); + return this; + }; -}); \ No newline at end of file + GridStack.prototype.maxHeight = function(el, val) { + el = $(el); + el.each(function(index, el) { + el = $(el); + var node = el.data('_gridstack_node'); + if (typeof node === 'undefined' || node === null) { + return; + } + + if (!isNaN(val)) { + node.maxHeight = (val || false); + el.attr('data-gs-max-height', val); + } + }); + return this; + }; + + GridStack.prototype.minHeight = function(el, val) { + el = $(el); + el.each(function(index, el) { + el = $(el); + var node = el.data('_gridstack_node'); + if (typeof node === 'undefined' || node === null) { + return; + } + + if (!isNaN(val)) { + node.minHeight = (val || false); + el.attr('data-gs-min-height', val); + } + }); + return this; + }; + + GridStack.prototype.maxWidth = function(el, val) { + el = $(el); + el.each(function(index, el) { + el = $(el); + var node = el.data('_gridstack_node'); + if (typeof node === 'undefined' || node === null) { + return; + } + + if (!isNaN(val)) { + node.maxWidth = (val || false); + el.attr('data-gs-max-width', val); + } + }); + return this; + }; + +*/ \ No newline at end of file diff --git a/src/gridstack.js b/src/gridstack.js index 1535571..0909f97 100644 --- a/src/gridstack.js +++ b/src/gridstack.js @@ -1360,7 +1360,7 @@ GridStack.prototype.cellWidth = function() { var o = this.container.children('.' + this.opts.itemClass).first(); - return Math.ceil(o.outerWidth() / o.attr('data-gs-width')); + return Math.ceil(o.outerWidth() / parseInt(o.attr('data-gs-width'), 10)); }; GridStack.prototype.getCellFromPixel = function(position, useOffset) { From 51d7a2011682ff603d4fccfd980072a76607d7a7 Mon Sep 17 00:00:00 2001 From: d Date: Thu, 25 Feb 2016 15:49:41 -0500 Subject: [PATCH 2/2] Comments left in. Whoops. --- spec/gridstack-spec.js | 72 ------------------------------------------ 1 file changed, 72 deletions(-) diff --git a/spec/gridstack-spec.js b/spec/gridstack-spec.js index 41c7845..e909e5b 100644 --- a/spec/gridstack-spec.js +++ b/spec/gridstack-spec.js @@ -331,75 +331,3 @@ describe('gridstack', function() { }); }); }); - -/* - GridStack.prototype.minWidth = function(el, val) { - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node === 'undefined' || node === null) { - return; - } - - if (!isNaN(val)) { - node.minWidth = (val || false); - el.attr('data-gs-min-width', val); - } - }); - return this; - }; - - - GridStack.prototype.maxHeight = function(el, val) { - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node === 'undefined' || node === null) { - return; - } - - if (!isNaN(val)) { - node.maxHeight = (val || false); - el.attr('data-gs-max-height', val); - } - }); - return this; - }; - - GridStack.prototype.minHeight = function(el, val) { - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node === 'undefined' || node === null) { - return; - } - - if (!isNaN(val)) { - node.minHeight = (val || false); - el.attr('data-gs-min-height', val); - } - }); - return this; - }; - - GridStack.prototype.maxWidth = function(el, val) { - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node === 'undefined' || node === null) { - return; - } - - if (!isNaN(val)) { - node.maxWidth = (val || false); - el.attr('data-gs-max-width', val); - } - }); - return this; - }; - -*/ \ No newline at end of file