diff --git a/README.md b/README.md index 142e17aac..7f5af6a88 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,8 @@ Usage ## Requirements -* [lodash.js](https://lodash.com) (>= 4.17.10, full build) * [jQuery](http://jquery.com) (>= 3.1.0) +* `Array.prototype.find` for IE and older browsers ([core.js](https://github.com/zloirock/core-js#ecmascript-6-array) allows to include specific polyfills) #### Using gridstack.js with jQuery UI @@ -154,7 +154,7 @@ Please use [jQuery UI Touch Punch](https://github.com/furf/jquery-ui-touch-punch working on touch-based devices. ```html - + diff --git a/bower.json b/bower.json index b1cb6cddd..b400ead99 100644 --- a/bower.json +++ b/bower.json @@ -15,7 +15,6 @@ "amd" ], "dependencies": { - "lodash": ">= 4.17.10", "jquery": ">= 3.1.0", "jquery-ui": ">= 1.12.0" }, diff --git a/demo/anijs.html b/demo/anijs.html index e11dc4eb1..d8898493c 100644 --- a/demo/anijs.html +++ b/demo/anijs.html @@ -18,7 +18,7 @@ - + @@ -86,4 +86,4 @@

Widget added

}); - \ No newline at end of file + diff --git a/demo/float.html b/demo/float.html index e8a753ff5..040f9b999 100644 --- a/demo/float.html +++ b/demo/float.html @@ -16,7 +16,7 @@ - + @@ -75,7 +75,7 @@

Float grid demo

width: 1 + 3 * Math.random(), height: 1 + 3 * Math.random() }; - this.grid.addWidget($('
'), + this.grid.addWidget($('
'), node.x, node.y, node.width, node.height); return false; }.bind(this); diff --git a/demo/knockout.html b/demo/knockout.html index 1a41371fe..a7677333e 100644 --- a/demo/knockout.html +++ b/demo/knockout.html @@ -16,7 +16,7 @@ - + @@ -63,7 +63,7 @@

knockout.js Demo

}).data('gridstack'); } - var item = _.find(items, function (i) { return i.nodeType == 1 }); + var item = items.find(function (i) { return i.nodeType == 1 }); grid.addWidget(item); ko.utils.domNodeDisposal.addDisposeCallback(item, function () { grid.removeWidget(item); diff --git a/demo/knockout2.html b/demo/knockout2.html index cd4cfe025..f7beb9287 100644 --- a/demo/knockout2.html +++ b/demo/knockout2.html @@ -16,7 +16,7 @@ - + @@ -63,7 +63,7 @@

knockout.js Demo

}).data('gridstack'); } - var item = _.find(items, function (i) { return i.nodeType == 1 }); + var item = items.find(function (i) { return i.nodeType == 1 }); grid.addWidget(item); ko.utils.domNodeDisposal.addDisposeCallback(item, function () { grid.removeWidget(item); diff --git a/demo/nested.html b/demo/nested.html index 13287e3bf..607d1093d 100644 --- a/demo/nested.html +++ b/demo/nested.html @@ -16,7 +16,7 @@ - + diff --git a/demo/responsive.html b/demo/responsive.html index 1a40301a6..88f645ed9 100644 --- a/demo/responsive.html +++ b/demo/responsive.html @@ -17,7 +17,7 @@ - + @@ -106,7 +106,7 @@

Responsive grid demo

this.loadGrid = function () { this.grid.removeAll(); var items = GridStackUI.Utils.sort(this.serializedData); - _.each(items, function (node, i) { + items.forEach(function (node, i) { this.grid.addWidget($('
' + i + '
'), node.x, node.y, node.width, node.height); }.bind(this)); diff --git a/demo/rtl.html b/demo/rtl.html index 69bd2f203..564cfbab0 100644 --- a/demo/rtl.html +++ b/demo/rtl.html @@ -16,7 +16,7 @@ - + @@ -62,7 +62,7 @@

RTL Demo

}).data('gridstack'); } - var item = _.find(items, function (i) { return i.nodeType == 1 }); + var item = items.find(function (i) { return i.nodeType == 1 }); grid.addWidget(item); ko.utils.domNodeDisposal.addDisposeCallback(item, function () { grid.removeWidget(item); @@ -95,7 +95,8 @@

RTL Demo

y: 0, width: Math.floor(1 + 3 * Math.random()), height: Math.floor(1 + 3 * Math.random()), - auto_position: true + auto_position: true, + index: 'auto' }); return false; }; diff --git a/demo/serialization.html b/demo/serialization.html index 77a7fecc5..594784a98 100644 --- a/demo/serialization.html +++ b/demo/serialization.html @@ -16,7 +16,7 @@ - + @@ -76,15 +76,15 @@

Serialization demo

this.loadGrid = function () { this.grid.removeAll(); var items = GridStackUI.Utils.sort(this.serializedData); - _.each(items, function (node) { - this.grid.addWidget($('
'), - node.x, node.y, node.width, node.height); - }.bind(this)); + items.forEach(function (node) { + this.grid.addWidget($('
'), + node.x, node.y, node.width, node.height); + }, this); return false; }.bind(this); this.saveGrid = function () { - this.serializedData = _.map($('.grid-stack > .grid-stack-item:visible'), function (el) { + this.serializedData = $('.grid-stack > .grid-stack-item:visible').map(function (i, el) { el = $(el); var node = el.data('_gridstack_node'); return { @@ -93,7 +93,7 @@

Serialization demo

width: node.width, height: node.height }; - }); + }).toArray(); $('#saved-data').val(JSON.stringify(this.serializedData, null, ' ')); return false; }.bind(this); diff --git a/demo/two.html b/demo/two.html index 35f6dc653..8c71e8904 100644 --- a/demo/two.html +++ b/demo/two.html @@ -17,7 +17,7 @@ - + @@ -113,9 +113,9 @@

Two grids demo

acceptWidgets: '.grid-stack-item' }; $('#grid1').gridstack(options); - $('#grid2').gridstack(_.defaults({ + $('#grid2').gridstack($.extend({}, options, { float: true - }, options)); + })); var items = [ {x: 0, y: 0, width: 2, height: 2}, @@ -128,8 +128,8 @@

Two grids demo

$('.grid-stack').each(function () { var grid = $(this).data('gridstack'); - _.each(items, function (node) { - grid.addWidget($('
'), + items.forEach(function (node) { + grid.addWidget($('
'), node.x, node.y, node.width, node.height); }); }); diff --git a/dist/gridstack.all.js b/dist/gridstack.all.js deleted file mode 100644 index 8c8cb1145..000000000 --- a/dist/gridstack.all.js +++ /dev/null @@ -1,2 +0,0 @@ -!function(t){if("function"==typeof define&&define.amd)define(["jquery","lodash","exports"],t);else if("undefined"!=typeof exports){try{jQuery=require("jquery")}catch(t){}try{_=require("lodash")}catch(t){}t(jQuery,_,exports)}else t(jQuery,_,window)}(function(u,g,t){var e=function(t,e,i){var o=function(){return console.warn("gridstack.js: Function `"+e+"` is deprecated as of v0.2.5 and has been replaced with `"+i+"`. It will be **completely** removed in v1.0."),t.apply(this,arguments)};return o.prototype=t.prototype,o},d=function(t,e){console.warn("gridstack.js: Option `"+t+"` is deprecated as of v0.2.5 and has been replaced with `"+e+"`. It will be **completely** removed in v1.0.")},_={isIntercepted:function(t,e){return!(t.x+t.width<=e.x||e.x+e.width<=t.x||t.y+t.height<=e.y||e.y+e.height<=t.y)},sort:function(t,e,i){return i=i||g.chain(t).map(function(t){return t.x+t.width}).max().value(),e=-1!=e?1:-1,g.sortBy(t,[function(t){return e*(t.x+t.y*i)}])},createStylesheet:function(t){var e=document.createElement("style");return e.setAttribute("type","text/css"),e.setAttribute("data-gs-style-id",t),e.styleSheet?e.styleSheet.cssText="":e.appendChild(document.createTextNode("")),document.getElementsByTagName("head")[0].appendChild(e),e.sheet},removeStylesheet:function(t){u("STYLE[data-gs-style-id="+t+"]").remove()},insertCSSRule:function(t,e,i,o){"function"==typeof t.insertRule?t.insertRule(e+"{"+i+"}",o):"function"==typeof t.addRule&&t.addRule(e,i,o)},toBool:function(t){return"boolean"==typeof t?t:"string"==typeof t?!(""===(t=t.toLowerCase())||"no"==t||"false"==t||"0"==t):Boolean(t)},_collisionNodeCheck:function(t){return t!=this.node&&_.isIntercepted(t,this.nn)},_didCollide:function(t){return _.isIntercepted({x:this.n.x,y:this.newY,width:this.n.width,height:this.n.height},t)},_isAddNodeIntercepted:function(t){return _.isIntercepted({x:this.x,y:this.y,width:this.node.width,height:this.node.height},t)},parseHeight:function(t){var e=t,i="px";if(e&&g.isString(e)){var o=e.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);if(!o)throw new Error("Invalid height");i=o[2]||"px",e=parseFloat(o[1])}return{height:e,unit:i}},removePositioningStyles:function(t){var e=t[0].style;e.position&&e.removeProperty("position"),e.left&&e.removeProperty("left"),e.top&&e.removeProperty("top"),e.width&&e.removeProperty("width"),e.height&&e.removeProperty("height")},getScrollParent:function(t){return null==t?null:t.scrollHeight>t.clientHeight?t:_.getScrollParent(t.parentNode)},updateScrollPosition:function(t,e,i){var o=t.getBoundingClientRect(),a=window.innerHeight||document.documentElement.clientHeight;if(o.top<0||o.bottom>a){var r=o.bottom-a,s=o.top,n=_.getScrollParent(t);if(null!=n){var d=n.scrollTop;o.top<0&&i<0?t.offsetHeight>a?n.scrollTop+=i:n.scrollTop+=Math.abs(s)>Math.abs(i)?i:s:0a?n.scrollTop+=i:n.scrollTop+=i=t._origY;){g.chain(this.nodes).take(e).find(g.bind(_._didCollide,{n:t,newY:i})).value()||(t._dirty=!0,t.y=i),--i}},this)):g.each(this.nodes,g.bind(function(t,e){if(!t.locked)for(;0this.width?t.width=this.width:t.width<1&&(t.width=1),t.height<1&&(t.height=1),t.x<0&&(t.x=0),t.x+t.width>this.width&&(e?t.width=this.width-t.x:t.x=this.width-t.width),t.y<0&&(t.y=0),t},y.prototype._notify=function(){var t=Array.prototype.slice.call(arguments,0);if(t[0]=void 0===t[0]?[]:[t[0]],t[1]=void 0===t[1]||t[1],!this._updateCounter){var e=t[0].concat(this.getDirtyNodes());this.onchange(e,t[1])}},y.prototype.cleanNodes=function(){this._updateCounter||g.each(this.nodes,function(t){t._dirty=!1})},y.prototype.getDirtyNodes=function(){return g.filter(this.nodes,function(t){return t._dirty})},y.prototype.addNode=function(t,e){if(void 0!==(t=this._prepareNode(t)).maxWidth&&(t.width=Math.min(t.width,t.maxWidth)),void 0!==t.maxHeight&&(t.height=Math.min(t.height,t.maxHeight)),void 0!==t.minWidth&&(t.width=Math.max(t.width,t.minWidth)),void 0!==t.minHeight&&(t.height=Math.max(t.height,t.minHeight)),t._id=++r,t._dirty=!0,t.autoPosition){this._sortNodes();for(var i=0;;++i){var o=i%this.width,a=Math.floor(i/this.width);if(!(o+t.width>this.width)&&!g.find(this.nodes,g.bind(_._isAddNodeIntercepted,{x:o,y:a,node:t}))){t.x=o,t.y=a;break}}}return this.nodes.push(t),void 0!==e&&e&&this._addedNodes.push(g.clone(t)),this._fixCollisions(t),this._packNodes(),this._notify(),t},y.prototype.removeNode=function(t,e){e=void 0===e||e,this._removedNodes.push(g.clone(t)),t._id=null,this.nodes=g.without(this.nodes,t),this._packNodes(),this._notify(t,e)},y.prototype.canMoveNode=function(e,t,i,o,a){if(!this.isNodeChangedPosition(e,t,i,o,a))return!1;var r,s=Boolean(g.find(this.nodes,function(t){return t.locked}));if(!this.height&&!s)return!0;var n=new y(this.width,null,this.float,0,g.map(this.nodes,function(t){return t==e?r=u.extend({},t):u.extend({},t)}));if(void 0===r)return!0;n.moveNode(r,t,i,o,a);var d=!0;return s&&(d&=!Boolean(g.find(n.nodes,function(t){return t!=r&&Boolean(t.locked)&&Boolean(t._dirty)}))),this.height&&(d&=n.getGridHeight()<=this.height),d},y.prototype.canBePlacedWithRespectToHeight=function(t){if(!this.height)return!0;var e=new y(this.width,null,this.float,0,g.map(this.nodes,function(t){return u.extend({},t)}));return e.addNode(t),e.getGridHeight()<=this.height},y.prototype.isNodeChangedPosition=function(t,e,i,o,a){return"number"!=typeof e&&(e=t.x),"number"!=typeof i&&(i=t.y),"number"!=typeof o&&(o=t.width),"number"!=typeof a&&(a=t.height),void 0!==t.maxWidth&&(o=Math.min(o,t.maxWidth)),void 0!==t.maxHeight&&(a=Math.min(a,t.maxHeight)),void 0!==t.minWidth&&(o=Math.max(o,t.minWidth)),void 0!==t.minHeight&&(a=Math.max(a,t.minHeight)),t.x!=e||t.y!=i||t.width!=o||t.height!=a},y.prototype.moveNode=function(t,e,i,o,a,r){if(!this.isNodeChangedPosition(t,e,i,o,a))return t;if("number"!=typeof e&&(e=t.x),"number"!=typeof i&&(i=t.y),"number"!=typeof o&&(o=t.width),"number"!=typeof a&&(a=t.height),void 0!==t.maxWidth&&(o=Math.min(o,t.maxWidth)),void 0!==t.maxHeight&&(a=Math.min(a,t.maxHeight)),void 0!==t.minWidth&&(o=Math.max(o,t.minWidth)),void 0!==t.minHeight&&(a=Math.max(a,t.minHeight)),t.x==e&&t.y==i&&t.width==o&&t.height==a)return t;var s=t.width!=o;return t._dirty=!0,t.x=e,t.y=i,t.width=o,t.height=a,t.lastTriedX=e,t.lastTriedY=i,t.lastTriedWidth=o,t.lastTriedHeight=a,t=this._prepareNode(t,s),this._fixCollisions(t),r||(this._packNodes(),this._notify()),t},y.prototype.getGridHeight=function(){return g.reduce(this.nodes,function(t,e){return Math.max(t,e.y+e.height)},0)},y.prototype.beginUpdate=function(t){g.each(this.nodes,function(t){t._origY=t.y}),t._updating=!0},y.prototype.endUpdate=function(){g.each(this.nodes,function(t){t._origY=t.y});var t=g.find(this.nodes,function(t){return t._updating});t&&(t._updating=!1)};var i=function(t,e){var i,o,h=this;e=e||{},this.container=u(t),void 0!==e.handle_class&&(e.handleClass=e.handle_class,d("handle_class","handleClass")),void 0!==e.item_class&&(e.itemClass=e.item_class,d("item_class","itemClass")),void 0!==e.placeholder_class&&(e.placeholderClass=e.placeholder_class,d("placeholder_class","placeholderClass")),void 0!==e.placeholder_text&&(e.placeholderText=e.placeholder_text,d("placeholder_text","placeholderText")),void 0!==e.cell_height&&(e.cellHeight=e.cell_height,d("cell_height","cellHeight")),void 0!==e.vertical_margin&&(e.verticalMargin=e.vertical_margin,d("vertical_margin","verticalMargin")),void 0!==e.min_width&&(e.minWidth=e.min_width,d("min_width","minWidth")),void 0!==e.static_grid&&(e.staticGrid=e.static_grid,d("static_grid","staticGrid")),void 0!==e.is_nested&&(e.isNested=e.is_nested,d("is_nested","isNested")),void 0!==e.always_show_resize_handle&&(e.alwaysShowResizeHandle=e.always_show_resize_handle,d("always_show_resize_handle","alwaysShowResizeHandle")),e.itemClass=e.itemClass||"grid-stack-item";var a=0
'+this.opts.placeholderText+"
").hide(),this._updateContainerHeight(),this._updateHeightsOnResize=g.throttle(function(){h.cellHeight(h.cellWidth(),!1)},100),this.onResizeHandler=function(){if(o&&h._updateHeightsOnResize(),h._isOneColumnMode()&&!h.opts.disableOneColumnMode){if(i)return;h.container.addClass(h.opts.oneColumnModeClass),i=!0,h.grid._sortNodes(),g.each(h.grid.nodes,function(t){h.container.append(t.el),h.opts.staticGrid||(h.dd.draggable(t.el,"disable"),h.dd.resizable(t.el,"disable"),t.el.trigger("resize"))})}else{if(!i)return;if(h.container.removeClass(h.opts.oneColumnModeClass),i=!1,h.opts.staticGrid)return;g.each(h.grid.nodes,function(t){t.noMove||h.opts.disableDrag||h.dd.draggable(t.el,"enable"),t.noResize||h.opts.disableResize||h.dd.resizable(t.el,"enable"),t.el.trigger("resize")})}},u(window).resize(this.onResizeHandler),this.onResizeHandler(),!h.opts.staticGrid&&"string"==typeof h.opts.removable){var n=u(h.opts.removable);this.dd.isDroppable(n)||this.dd.droppable(n,h.opts.removableOptions),this.dd.on(n,"dropover",function(t,e){var i=u(e.draggable);i.data("_gridstack_node")._grid===h&&(i.data("inTrashZone",!0),h._setupRemovingTimeout(i))}).on(n,"dropout",function(t,e){var i=u(e.draggable);i.data("_gridstack_node")._grid===h&&(i.data("inTrashZone",!1),h._clearRemovingTimeout(i))})}if(!h.opts.staticGrid&&h.opts.acceptWidgets){var l=null,p=function(t,e){var i=l,o=i.data("_gridstack_node"),a=h.getCellFromPixel({left:t.pageX,top:t.pageY},!0),r=Math.max(0,a.x),s=Math.max(0,a.y);o._added||(o._added=!0,o.el=i,o.autoPosition=!0,o.x=r,o.y=s,h.grid.cleanNodes(),h.grid.beginUpdate(o),h.grid.addNode(o),h.container.append(h.placeholder),h.placeholder.attr("data-gs-x",o.x).attr("data-gs-y",o.y).attr("data-gs-width",o.width).attr("data-gs-height",o.height).show(),o.el=h.placeholder,o._beforeDragX=o.x,o._beforeDragY=o.y,h._updateContainerHeight()),h.grid.canMoveNode(o,r,s)&&(h.grid.moveNode(o,r,s),h._updateContainerHeight())};this.dd.droppable(h.container,{accept:function(t){var e=(t=u(t)).data("_gridstack_node");return(!e||e._grid!==h)&&t.is(!0===h.opts.acceptWidgets?".grid-stack-item":h.opts.acceptWidgets)}}).on(h.container,"dropover",function(t,e){h.container.offset();var i=u(e.draggable),o=h.cellWidth(),a=h.cellHeight(),r=i.data("_gridstack_node"),s=r?r.width:Math.ceil(i.outerWidth()/o),n=r?r.height:Math.ceil(i.outerHeight()/a);l=i;var d=h.grid._prepareNode({width:s,height:n,_added:!1,_temporary:!0});i.data("_gridstack_node",d),i.data("_gridstack_node_orig",r),i.on("drag",p)}).on(h.container,"dropout",function(t,e){var i=u(e.draggable);if(i.data("_gridstack_node")){i.unbind("drag",p);var o=i.data("_gridstack_node");o.el=null,h.grid.removeNode(o),h.placeholder.detach(),h._updateContainerHeight(),i.data("_gridstack_node",i.data("_gridstack_node_orig"))}}).on(h.container,"drop",function(t,e){h.placeholder.detach();var i=u(e.draggable).data("_gridstack_node");i._grid=h;var o=u(e.draggable).clone(!1);o.data("_gridstack_node",i);var a=u(e.draggable).data("_gridstack_node_orig");void 0!==a&&void 0!==a._grid&&a._grid._triggerRemoveEvent(),u(e.helper).remove(),i.el=o,h.placeholder.hide(),_.removePositioningStyles(o),o.find("div.ui-resizable-handle").remove(),o.attr("data-gs-x",i.x).attr("data-gs-y",i.y).attr("data-gs-width",i.width).attr("data-gs-height",i.height).addClass(h.opts.itemClass).enableSelection().removeData("draggable").removeClass("ui-draggable ui-draggable-dragging ui-draggable-disabled").unbind("drag",p),h.container.append(o),h._prepareElementsByNode(o,i),h._updateContainerHeight(),h.grid._addedNodes.push(i),h._triggerAddEvent(),h._triggerChangeEvent(),h.grid.endUpdate(),u(e.draggable).unbind("drag",p),u(e.draggable).removeData("_gridstack_node"),u(e.draggable).removeData("_gridstack_node_orig"),h.container.trigger("dropped",[a,i])})}};return i.prototype._triggerChangeEvent=function(t){var e=this.grid.getDirtyNodes(),i=!1,o=[];e&&e.length&&(o.push(e),i=!0),(i||!0===t)&&this.container.trigger("change",o)},i.prototype._triggerAddEvent=function(){this.grid._addedNodes&&0this._styles._max)){for(var a=this._styles._max;a=c.grid.width||r<0||!c.grid.float&&r>c.grid.getGridHeight()){if(l._temporaryRemoved)return;!0===c.opts.removable&&c._setupRemovingTimeout(h),a=l._beforeDragX,r=l._beforeDragY,c.placeholder.detach(),c.placeholder.hide(),c.grid.removeNode(l),c._updateContainerHeight(),l._temporaryRemoved=!0}else c._clearRemovingTimeout(h),l._temporaryRemoved&&(c.grid.addNode(l),c.placeholder.attr("data-gs-x",a).attr("data-gs-y",r).attr("data-gs-width",i).attr("data-gs-height",o).show(),c.container.append(c.placeholder),l.el=c.placeholder,l._temporaryRemoved=!1)}else if("resize"==t.type&&a<0)return;var n=void 0!==i?i:l.lastTriedWidth,d=void 0!==o?o:l.lastTriedHeight;!c.grid.canMoveNode(l,a,r,i,o)||l.lastTriedX===a&&l.lastTriedY===r&&l.lastTriedWidth===n&&l.lastTriedHeight===d||(l.lastTriedX=a,l.lastTriedY=r,l.lastTriedWidth=i,l.lastTriedHeight=o,c.grid.moveNode(l,a,r,i,o),c._updateContainerHeight(),"resize"==t.type&&u(t.target).trigger("gsresize",l))},e=function(t,e){c.container.append(c.placeholder);var i=u(this);c.grid.cleanNodes(),c.grid.beginUpdate(l),p=c.cellWidth();var o=Math.ceil(i.outerHeight()/i.attr("data-gs-height"));g=c.container.height()/parseInt(c.container.attr("data-gs-current-height")),c.placeholder.attr("data-gs-x",i.attr("data-gs-x")).attr("data-gs-y",i.attr("data-gs-y")).attr("data-gs-width",i.attr("data-gs-width")).attr("data-gs-height",i.attr("data-gs-height")).show(),l.el=c.placeholder,l._beforeDragX=l.x,l._beforeDragY=l.y,l._prevYPix=e.position.top,c.dd.resizable(h,"option","minWidth",p*(l.minWidth||1)),c.dd.resizable(h,"option","minHeight",o*(l.minHeight||1)),"resizestart"==t.type&&i.find(".grid-stack-item").trigger("resizestart")},i=function(t,e){var i=u(this);if(i.data("_gridstack_node")){var o=!1;if(c.placeholder.detach(),l.el=i,c.placeholder.hide(),l._isAboutToRemove)o=!0,h.data("_gridstack_node")._grid._triggerRemoveEvent(),h.removeData("_gridstack_node"),h.remove();else c._clearRemovingTimeout(h),l._temporaryRemoved?(_.removePositioningStyles(i),i.attr("data-gs-x",l._beforeDragX).attr("data-gs-y",l._beforeDragY).attr("data-gs-width",l.width).attr("data-gs-height",l.height),l.x=l._beforeDragX,l.y=l._beforeDragY,l._temporaryRemoved=!1,c.grid.addNode(l)):(_.removePositioningStyles(i),i.attr("data-gs-x",l.x).attr("data-gs-y",l.y).attr("data-gs-width",l.width).attr("data-gs-height",l.height));c._updateContainerHeight(),c._triggerChangeEvent(o),c.grid.endUpdate();var a=i.find(".grid-stack");a.length&&"resizestop"==t.type&&(a.each(function(t,e){u(e).data("gridstack").onResizeHandler()}),i.find(".grid-stack-item").trigger("resizestop"),i.find(".grid-stack-item").trigger("gsresizestop")),"resizestop"==t.type&&c.container.trigger("gsresizestop",i)}};this.dd.draggable(h,{start:e,stop:i,drag:t}).resizable(h,{start:e,stop:i,resize:t}),(l.noMove||this._isOneColumnMode()&&!c.opts.disableOneColumnMode||this.opts.disableDrag||this.opts.staticGrid)&&this.dd.draggable(h,"disable"),(l.noResize||this._isOneColumnMode()&&!c.opts.disableOneColumnMode||this.opts.disableResize||this.opts.staticGrid)&&this.dd.resizable(h,"disable"),h.attr("data-gs-locked",l.locked?"yes":null)},i.prototype._prepareElement=function(t,e){e=void 0!==e&&e;(t=u(t)).addClass(this.opts.itemClass);var i=this.grid.addNode({x:parseInt(t.attr("data-gs-x"),10),y:parseInt(t.attr("data-gs-y"),10),width:t.attr("data-gs-width"),height:t.attr("data-gs-height"),maxWidth:t.attr("data-gs-max-width"),minWidth:t.attr("data-gs-min-width"),maxHeight:t.attr("data-gs-max-height"),minHeight:t.attr("data-gs-min-height"),autoPosition:_.toBool(t.attr("data-gs-auto-position")),noResize:_.toBool(t.attr("data-gs-no-resize")),noMove:_.toBool(t.attr("data-gs-no-move")),locked:_.toBool(t.attr("data-gs-locked")),resizeHandles:t.attr("data-gs-resize-handles"),el:t,id:t.attr("data-gs-id"),_grid:this},e);t.data("_gridstack_node",i),this._prepareElementsByNode(t,i)},i.prototype.setAnimation=function(t){t?this.container.addClass("grid-stack-animate"):this.container.removeClass("grid-stack-animate")},i.prototype.addWidget=function(t,e,i,o,a,r,s,n,d,h,l){return t=u(t),void 0!==e&&t.attr("data-gs-x",e),void 0!==i&&t.attr("data-gs-y",i),void 0!==o&&t.attr("data-gs-width",o),void 0!==a&&t.attr("data-gs-height",a),void 0!==r&&t.attr("data-gs-auto-position",r?"yes":null),void 0!==s&&t.attr("data-gs-min-width",s),void 0!==n&&t.attr("data-gs-max-width",n),void 0!==d&&t.attr("data-gs-min-height",d),void 0!==h&&t.attr("data-gs-max-height",h),void 0!==l&&t.attr("data-gs-id",l),this.container.append(t),this._prepareElement(t,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),t},i.prototype.makeWidget=function(t){return t=u(t),this._prepareElement(t,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),t},i.prototype.willItFit=function(t,e,i,o,a){var r={x:t,y:e,width:i,height:o,autoPosition:a};return this.grid.canBePlacedWithRespectToHeight(r)},i.prototype.removeWidget=function(t,e){e=void 0===e||e;var i=(t=u(t)).data("_gridstack_node");i||(i=this.grid.getNodeDataByDOMEl(t)),this.grid.removeNode(i,e),t.removeData("_gridstack_node"),this._updateContainerHeight(),e&&t.remove(),this._triggerChangeEvent(!0),this._triggerRemoveEvent()},i.prototype.removeAll=function(e){g.each(this.grid.nodes,g.bind(function(t){this.removeWidget(t.el,e)},this)),this.grid.nodes=[],this._updateContainerHeight()},i.prototype.destroy=function(t){u(window).off("resize",this.onResizeHandler),this.disable(),void 0===t||t?this.container.remove():(this.removeAll(!1),this.container.removeData("gridstack")),_.removeStylesheet(this._stylesId),this.grid&&(this.grid=null)},i.prototype.resizable=function(t,o){var a=this;return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(i.noResize=!o,i.noResize||a._isOneColumnMode()&&!a.opts.disableOneColumnMode?a.dd.resizable(e,"disable"):a.dd.resizable(e,"enable"))}),this},i.prototype.movable=function(t,o){var a=this;return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(i.noMove=!o,i.noMove||a._isOneColumnMode()&&!a.opts.disableOneColumnMode?(a.dd.draggable(e,"disable"),e.removeClass("ui-draggable-handle")):(a.dd.draggable(e,"enable"),e.addClass("ui-draggable-handle")))}),this},i.prototype.enableMove=function(t,e){this.movable(this.container.children("."+this.opts.itemClass),t),e&&(this.opts.disableDrag=!t)},i.prototype.enableResize=function(t,e){this.resizable(this.container.children("."+this.opts.itemClass),t),e&&(this.opts.disableResize=!t)},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(t,o){return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(i.locked=o||!1,e.attr("data-gs-locked",i.locked?"yes":null))}),this},i.prototype.maxHeight=function(t,o){return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.maxHeight=o||!1,e.attr("data-gs-max-height",o)))}),this},i.prototype.minHeight=function(t,o){return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.minHeight=o||!1,e.attr("data-gs-min-height",o)))}),this},i.prototype.maxWidth=function(t,o){return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.maxWidth=o||!1,e.attr("data-gs-max-width",o)))}),this},i.prototype.minWidth=function(t,o){return(t=u(t)).each(function(t,e){var i=(e=u(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.minWidth=o||!1,e.attr("data-gs-min-width",o)))}),this},i.prototype._updateElement=function(t,e){var i=(t=u(t).first()).data("_gridstack_node");if(null!=i){var o=this;o.grid.cleanNodes(),o.grid.beginUpdate(i),e.call(this,t,i),o._updateContainerHeight(),o._triggerChangeEvent(),o.grid.endUpdate()}},i.prototype.resize=function(t,i,o){this._updateElement(t,function(t,e){i=null!=i?i:e.width,o=null!=o?o:e.height,this.grid.moveNode(e,e.x,e.y,i,o)})},i.prototype.move=function(t,i,o){this._updateElement(t,function(t,e){i=null!=i?i:e.x,o=null!=o?o:e.y,this.grid.moveNode(e,i,o,e.width,e.height)})},i.prototype.update=function(t,i,o,a,r){this._updateElement(t,function(t,e){i=null!=i?i:e.x,o=null!=o?o:e.y,a=null!=a?a:e.width,r=null!=r?r:e.height,this.grid.moveNode(e,i,o,a,r)})},i.prototype.verticalMargin=function(t,e){if(void 0===t)return this.opts.verticalMargin;var i=_.parseHeight(t);this.opts.verticalMarginUnit===i.unit&&this.opts.height===i.height||(this.opts.verticalMarginUnit=i.unit,this.opts.verticalMargin=i.height,e||this._updateStyles())},i.prototype.cellHeight=function(t,e){if(void 0===t){if(this.opts.cellHeight)return this.opts.cellHeight;var i=this.container.children("."+this.opts.itemClass).first();return Math.ceil(i.outerHeight()/i.attr("data-gs-height"))}var o=_.parseHeight(t);this.opts.cellHeightUnit===o.unit&&this.opts.cellHeight===o.height||(this.opts.cellHeightUnit=o.unit,this.opts.cellHeight=o.height,e||this._updateStyles())},i.prototype.cellWidth=function(){return Math.round(this.container.outerWidth()/this.opts.width)},i.prototype.getCellFromPixel=function(t,e){var i=void 0!==e&&e?this.container.offset():this.container.position(),o=t.left-i.left,a=t.top-i.top,r=Math.floor(this.container.width()/this.opts.width),s=Math.floor(this.container.height()/parseInt(this.container.attr("data-gs-current-height")));return{x:Math.floor(o/r),y:Math.floor(a/s)}},i.prototype.batchUpdate=function(){this.grid.batchUpdate()},i.prototype.commit=function(){this.grid.commit(),this._updateContainerHeight()},i.prototype.isAreaEmpty=function(t,e,i,o){return this.grid.isAreaEmpty(t,e,i,o)},i.prototype.setStatic=function(t){this.opts.staticGrid=!0===t,this.enableMove(!t),this.enableResize(!t),this._setStaticClass()},i.prototype._setStaticClass=function(){var t="grid-stack-static";!0===this.opts.staticGrid?this.container.addClass(t):this.container.removeClass(t)},i.prototype._updateNodeWidths=function(t,e){this.grid._sortNodes(),this.grid.batchUpdate();for(var i={},o=0;o el.clientHeight) { - returnEl = el; - } else { - returnEl = Utils.getScrollParent(el.parentNode); - } - return returnEl; - }, - updateScrollPosition: function(el, ui, distance) { - // is widget in view? - var rect = el.getBoundingClientRect(); - var innerHeightOrClientHeight = (window.innerHeight || document.documentElement.clientHeight); - if (rect.top < 0 || - rect.bottom > innerHeightOrClientHeight - ) { - // set scrollTop of first parent that scrolls - // if parent is larger than el, set as low as possible - // to get entire widget on screen - var offsetDiffDown = rect.bottom - innerHeightOrClientHeight; - var offsetDiffUp = rect.top; - var scrollEl = Utils.getScrollParent(el); - if (scrollEl != null) { - var prevScroll = scrollEl.scrollTop; - if (rect.top < 0 && distance < 0) { - // moving up - if (el.offsetHeight > innerHeightOrClientHeight) { - scrollEl.scrollTop += distance; - } else { - scrollEl.scrollTop += Math.abs(offsetDiffUp) > Math.abs(distance) ? distance : offsetDiffUp; - } - } else if (distance > 0) { - // moving down - if (el.offsetHeight > innerHeightOrClientHeight) { - scrollEl.scrollTop += distance; - } else { - scrollEl.scrollTop += offsetDiffDown > distance ? distance : offsetDiffDown; - } - } - // move widget y by amount scrolled - ui.position.top += scrollEl.scrollTop - prevScroll; - } - } - } - }; - - // jscs:disable requireCamelCaseOrUpperCaseIdentifiers - Utils.is_intercepted = obsolete(Utils.isIntercepted, 'is_intercepted', 'isIntercepted'); - - Utils.create_stylesheet = obsolete(Utils.createStylesheet, 'create_stylesheet', 'createStylesheet'); - - Utils.remove_stylesheet = obsolete(Utils.removeStylesheet, 'remove_stylesheet', 'removeStylesheet'); - - Utils.insert_css_rule = obsolete(Utils.insertCSSRule, 'insert_css_rule', 'insertCSSRule'); - // jscs:enable requireCamelCaseOrUpperCaseIdentifiers - - /** - * @class GridStackDragDropPlugin - * Base class for drag'n'drop plugin. - */ - function GridStackDragDropPlugin(grid) { - this.grid = grid; - } - - GridStackDragDropPlugin.registeredPlugins = []; - - GridStackDragDropPlugin.registerPlugin = function(pluginClass) { - GridStackDragDropPlugin.registeredPlugins.push(pluginClass); - }; - - GridStackDragDropPlugin.prototype.resizable = function(el, opts) { - return this; - }; - - GridStackDragDropPlugin.prototype.draggable = function(el, opts) { - return this; - }; - - GridStackDragDropPlugin.prototype.droppable = function(el, opts) { - return this; - }; - - GridStackDragDropPlugin.prototype.isDroppable = function(el) { - return false; - }; - - GridStackDragDropPlugin.prototype.on = function(el, eventName, callback) { - return this; - }; - - - var idSeq = 0; - - var GridStackEngine = function(width, onchange, floatMode, height, items) { - this.width = width; - this.float = floatMode || false; - this.height = height || 0; - - this.nodes = items || []; - this.onchange = onchange || function() {}; - - this._updateCounter = 0; - this._float = this.float; - - this._addedNodes = []; - this._removedNodes = []; - }; - - GridStackEngine.prototype.batchUpdate = function() { - this._updateCounter = 1; - this.float = true; - }; - - GridStackEngine.prototype.commit = function() { - if (this._updateCounter !== 0) { - this._updateCounter = 0; - this.float = this._float; - this._packNodes(); - this._notify(); - } - }; - - // For Meteor support: https://github.com/troolee/gridstack.js/pull/272 - GridStackEngine.prototype.getNodeDataByDOMEl = function(el) { - return _.find(this.nodes, function(n) { return el.get(0) === n.el.get(0); }); - }; - - GridStackEngine.prototype._fixCollisions = function(node) { - var self = this; - this._sortNodes(-1); - - var nn = node; - var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; })); - if (!this.float && !hasLocked) { - nn = {x: 0, y: node.y, width: this.width, height: node.height}; - } - while (true) { - var collisionNode = _.find(this.nodes, _.bind(Utils._collisionNodeCheck, {node: node, nn: nn})); - if (typeof collisionNode == 'undefined') { - return; - } - this.moveNode(collisionNode, collisionNode.x, node.y + node.height, - collisionNode.width, collisionNode.height, true); - } - }; - - GridStackEngine.prototype.isAreaEmpty = function(x, y, width, height) { - var nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1}; - var collisionNode = _.find(this.nodes, _.bind(function(n) { - return Utils.isIntercepted(n, nn); - }, this)); - return collisionNode === null || typeof collisionNode === 'undefined'; - }; - - GridStackEngine.prototype._sortNodes = function(dir) { - this.nodes = Utils.sort(this.nodes, dir, this.width); - }; - - GridStackEngine.prototype._packNodes = function() { - this._sortNodes(); - - if (this.float) { - _.each(this.nodes, _.bind(function(n, i) { - if (n._updating || typeof n._origY == 'undefined' || n.y == n._origY) { - return; - } - - var newY = n.y; - while (newY >= n._origY) { - var collisionNode = _.chain(this.nodes) - .take(i) - .find(_.bind(Utils._didCollide, {n: n, newY: newY})) - .value(); - - if (!collisionNode) { - n._dirty = true; - n.y = newY; - } - --newY; - } - }, this)); - } else { - _.each(this.nodes, _.bind(function(n, i) { - if (n.locked) { - return; - } - while (n.y > 0) { - var newY = n.y - 1; - var canBeMoved = i === 0; - - if (i > 0) { - var collisionNode = _.chain(this.nodes) - .take(i) - .find(_.bind(Utils._didCollide, {n: n, newY: newY})) - .value(); - canBeMoved = typeof collisionNode == 'undefined'; - } - - if (!canBeMoved) { - break; - } - n._dirty = n.y != newY; - n.y = newY; - } - }, this)); - } - }; - - GridStackEngine.prototype._prepareNode = function(node, resizing) { - node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0}); - - node.x = parseInt('' + node.x); - node.y = parseInt('' + node.y); - node.width = parseInt('' + node.width); - node.height = parseInt('' + node.height); - node.autoPosition = node.autoPosition || false; - node.noResize = node.noResize || false; - node.noMove = node.noMove || false; - - if (node.width > this.width) { - node.width = this.width; - } else if (node.width < 1) { - node.width = 1; - } - - if (node.height < 1) { - node.height = 1; - } - - if (node.x < 0) { - node.x = 0; - } - - if (node.x + node.width > this.width) { - if (resizing) { - node.width = this.width - node.x; - } else { - node.x = this.width - node.width; - } - } - - if (node.y < 0) { - node.y = 0; - } - - return node; - }; - - GridStackEngine.prototype._notify = function() { - var args = Array.prototype.slice.call(arguments, 0); - args[0] = typeof args[0] === 'undefined' ? [] : [args[0]]; - args[1] = typeof args[1] === 'undefined' ? true : args[1]; - if (this._updateCounter) { - return; - } - var deletedNodes = args[0].concat(this.getDirtyNodes()); - this.onchange(deletedNodes, args[1]); - }; - - GridStackEngine.prototype.cleanNodes = function() { - if (this._updateCounter) { - return; - } - _.each(this.nodes, function(n) {n._dirty = false; }); - }; - - GridStackEngine.prototype.getDirtyNodes = function() { - return _.filter(this.nodes, function(n) { return n._dirty; }); - }; - - GridStackEngine.prototype.addNode = function(node, triggerAddEvent) { - node = this._prepareNode(node); - - if (typeof node.maxWidth != 'undefined') { node.width = Math.min(node.width, node.maxWidth); } - if (typeof node.maxHeight != 'undefined') { node.height = Math.min(node.height, node.maxHeight); } - if (typeof node.minWidth != 'undefined') { node.width = Math.max(node.width, node.minWidth); } - if (typeof node.minHeight != 'undefined') { node.height = Math.max(node.height, node.minHeight); } - - node._id = ++idSeq; - node._dirty = true; - - if (node.autoPosition) { - this._sortNodes(); - - for (var i = 0;; ++i) { - var x = i % this.width; - var y = Math.floor(i / this.width); - if (x + node.width > this.width) { - continue; - } - if (!_.find(this.nodes, _.bind(Utils._isAddNodeIntercepted, {x: x, y: y, node: node}))) { - node.x = x; - node.y = y; - break; - } - } - } - - this.nodes.push(node); - if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) { - this._addedNodes.push(_.clone(node)); - } - - this._fixCollisions(node); - this._packNodes(); - this._notify(); - return node; - }; - - GridStackEngine.prototype.removeNode = function(node, detachNode) { - detachNode = typeof detachNode === 'undefined' ? true : detachNode; - this._removedNodes.push(_.clone(node)); - node._id = null; - this.nodes = _.without(this.nodes, node); - this._packNodes(); - this._notify(node, detachNode); - }; - - GridStackEngine.prototype.canMoveNode = function(node, x, y, width, height) { - if (!this.isNodeChangedPosition(node, x, y, width, height)) { - return false; - } - var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; })); - - if (!this.height && !hasLocked) { - return true; - } - - var clonedNode; - var clone = new GridStackEngine( - this.width, - null, - this.float, - 0, - _.map(this.nodes, function(n) { - if (n == node) { - clonedNode = $.extend({}, n); - return clonedNode; - } - return $.extend({}, n); - })); - - if (typeof clonedNode === 'undefined') { - return true; - } - - clone.moveNode(clonedNode, x, y, width, height); - - var res = true; - - if (hasLocked) { - res &= !Boolean(_.find(clone.nodes, function(n) { - return n != clonedNode && Boolean(n.locked) && Boolean(n._dirty); - })); - } - if (this.height) { - res &= clone.getGridHeight() <= this.height; - } - - return res; - }; - - GridStackEngine.prototype.canBePlacedWithRespectToHeight = function(node) { - if (!this.height) { - return true; - } - - var clone = new GridStackEngine( - this.width, - null, - this.float, - 0, - _.map(this.nodes, function(n) { return $.extend({}, n); })); - clone.addNode(node); - return clone.getGridHeight() <= this.height; - }; - - GridStackEngine.prototype.isNodeChangedPosition = function(node, x, y, width, height) { - if (typeof x != 'number') { x = node.x; } - if (typeof y != 'number') { y = node.y; } - if (typeof width != 'number') { width = node.width; } - if (typeof height != 'number') { height = node.height; } - - if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); } - if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); } - if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); } - if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); } - - if (node.x == x && node.y == y && node.width == width && node.height == height) { - return false; - } - return true; - }; - - GridStackEngine.prototype.moveNode = function(node, x, y, width, height, noPack) { - if (!this.isNodeChangedPosition(node, x, y, width, height)) { - return node; - } - if (typeof x != 'number') { x = node.x; } - if (typeof y != 'number') { y = node.y; } - if (typeof width != 'number') { width = node.width; } - if (typeof height != 'number') { height = node.height; } - - if (typeof node.maxWidth != 'undefined') { width = Math.min(width, node.maxWidth); } - if (typeof node.maxHeight != 'undefined') { height = Math.min(height, node.maxHeight); } - if (typeof node.minWidth != 'undefined') { width = Math.max(width, node.minWidth); } - if (typeof node.minHeight != 'undefined') { height = Math.max(height, node.minHeight); } - - if (node.x == x && node.y == y && node.width == width && node.height == height) { - return node; - } - - var resizing = node.width != width; - node._dirty = true; - - node.x = x; - node.y = y; - node.width = width; - node.height = height; - - node.lastTriedX = x; - node.lastTriedY = y; - node.lastTriedWidth = width; - node.lastTriedHeight = height; - - node = this._prepareNode(node, resizing); - - this._fixCollisions(node); - if (!noPack) { - this._packNodes(); - this._notify(); - } - return node; - }; - - GridStackEngine.prototype.getGridHeight = function() { - return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0); - }; - - GridStackEngine.prototype.beginUpdate = function(node) { - _.each(this.nodes, function(n) { - n._origY = n.y; - }); - node._updating = true; - }; - - GridStackEngine.prototype.endUpdate = function() { - _.each(this.nodes, function(n) { - n._origY = n.y; - }); - var n = _.find(this.nodes, function(n) { return n._updating; }); - if (n) { - n._updating = false; - } - }; - - var GridStack = function(el, opts) { - var self = this; - var oneColumnMode, isAutoCellHeight; - - opts = opts || {}; - - this.container = $(el); - - // jscs:disable requireCamelCaseOrUpperCaseIdentifiers - if (typeof opts.handle_class !== 'undefined') { - opts.handleClass = opts.handle_class; - obsoleteOpts('handle_class', 'handleClass'); - } - if (typeof opts.item_class !== 'undefined') { - opts.itemClass = opts.item_class; - obsoleteOpts('item_class', 'itemClass'); - } - if (typeof opts.placeholder_class !== 'undefined') { - opts.placeholderClass = opts.placeholder_class; - obsoleteOpts('placeholder_class', 'placeholderClass'); - } - if (typeof opts.placeholder_text !== 'undefined') { - opts.placeholderText = opts.placeholder_text; - obsoleteOpts('placeholder_text', 'placeholderText'); - } - if (typeof opts.cell_height !== 'undefined') { - opts.cellHeight = opts.cell_height; - obsoleteOpts('cell_height', 'cellHeight'); - } - if (typeof opts.vertical_margin !== 'undefined') { - opts.verticalMargin = opts.vertical_margin; - obsoleteOpts('vertical_margin', 'verticalMargin'); - } - if (typeof opts.min_width !== 'undefined') { - opts.minWidth = opts.min_width; - obsoleteOpts('min_width', 'minWidth'); - } - if (typeof opts.static_grid !== 'undefined') { - opts.staticGrid = opts.static_grid; - obsoleteOpts('static_grid', 'staticGrid'); - } - if (typeof opts.is_nested !== 'undefined') { - opts.isNested = opts.is_nested; - obsoleteOpts('is_nested', 'isNested'); - } - if (typeof opts.always_show_resize_handle !== 'undefined') { - opts.alwaysShowResizeHandle = opts.always_show_resize_handle; - obsoleteOpts('always_show_resize_handle', 'alwaysShowResizeHandle'); - } - // jscs:enable requireCamelCaseOrUpperCaseIdentifiers - - opts.itemClass = opts.itemClass || 'grid-stack-item'; - var isNested = this.container.closest('.' + opts.itemClass).length > 0; - - this.opts = _.defaults(opts || {}, { - width: parseInt(this.container.attr('data-gs-width')) || 12, - height: parseInt(this.container.attr('data-gs-height')) || 0, - itemClass: 'grid-stack-item', - placeholderClass: 'grid-stack-placeholder', - placeholderText: '', - handle: '.grid-stack-item-content', - handleClass: null, - cellHeight: 60, - verticalMargin: 20, - auto: true, - minWidth: 768, - float: false, - staticGrid: false, - _class: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0), - animate: Boolean(this.container.attr('data-gs-animate')) || false, - alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false, - resizable: _.defaults(opts.resizable || {}, { - autoHide: !(opts.alwaysShowResizeHandle || false), - handles: 'se' - }), - draggable: _.defaults(opts.draggable || {}, { - handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) || - '.grid-stack-item-content', - scroll: false, - appendTo: 'body' - }), - disableDrag: opts.disableDrag || false, - disableResize: opts.disableResize || false, - rtl: 'auto', - removable: false, - removableOptions: _.defaults(opts.removableOptions || {}, { - accept: '.' + opts.itemClass - }), - removeTimeout: 2000, - verticalMarginUnit: 'px', - cellHeightUnit: 'px', - disableOneColumnMode: opts.disableOneColumnMode || false, - oneColumnModeClass: opts.oneColumnModeClass || 'grid-stack-one-column-mode', - ddPlugin: null - }); - - if (this.opts.ddPlugin === false) { - this.opts.ddPlugin = GridStackDragDropPlugin; - } else if (this.opts.ddPlugin === null) { - this.opts.ddPlugin = _.first(GridStackDragDropPlugin.registeredPlugins) || GridStackDragDropPlugin; - } - - this.dd = new this.opts.ddPlugin(this); - - if (this.opts.rtl === 'auto') { - this.opts.rtl = this.container.css('direction') === 'rtl'; - } - - if (this.opts.rtl) { - this.container.addClass('grid-stack-rtl'); - } - - this.opts.isNested = isNested; - - isAutoCellHeight = this.opts.cellHeight === 'auto'; - if (isAutoCellHeight) { - self.cellHeight(self.cellWidth(), true); - } else { - this.cellHeight(this.opts.cellHeight, true); - } - this.verticalMargin(this.opts.verticalMargin, true); - - this.container.addClass(this.opts._class); - - this._setStaticClass(); - - if (isNested) { - this.container.addClass('grid-stack-nested'); - } - - this._initStyles(); - - this.grid = new GridStackEngine(this.opts.width, function(nodes, detachNode) { - detachNode = typeof detachNode === 'undefined' ? true : detachNode; - var maxHeight = 0; - _.each(this.nodes, function(n) { - maxHeight = Math.max(maxHeight, n.y + n.height); - }); - _.each(nodes, function(n) { - if (detachNode && n._id === null) { - if (n.el) { - n.el.remove(); - } - } else { - n.el - .attr('data-gs-x', n.x) - .attr('data-gs-y', n.y) - .attr('data-gs-width', n.width) - .attr('data-gs-height', n.height); - } - }); - self._updateStyles(maxHeight + 10); - }, this.opts.float, this.opts.height); - - if (this.opts.auto) { - var elements = []; - var _this = this; - this.container.children('.' + this.opts.itemClass + ':not(.' + this.opts.placeholderClass + ')') - .each(function(index, el) { - el = $(el); - elements.push({ - el: el, - i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width - }); - }); - _.chain(elements).sortBy(function(x) { return x.i; }).each(function(i) { - self._prepareElement(i.el); - }).value(); - } - - this.setAnimation(this.opts.animate); - - this.placeholder = $( - '
' + - '
' + this.opts.placeholderText + '
').hide(); - - this._updateContainerHeight(); - - this._updateHeightsOnResize = _.throttle(function() { - self.cellHeight(self.cellWidth(), false); - }, 100); - - this.onResizeHandler = function() { - if (isAutoCellHeight) { - self._updateHeightsOnResize(); - } - - if (self._isOneColumnMode() && !self.opts.disableOneColumnMode) { - if (oneColumnMode) { - return; - } - self.container.addClass(self.opts.oneColumnModeClass); - oneColumnMode = true; - - self.grid._sortNodes(); - _.each(self.grid.nodes, function(node) { - self.container.append(node.el); - - if (self.opts.staticGrid) { - return; - } - self.dd.draggable(node.el, 'disable'); - self.dd.resizable(node.el, 'disable'); - - node.el.trigger('resize'); - }); - } else { - if (!oneColumnMode) { - return; - } - - self.container.removeClass(self.opts.oneColumnModeClass); - oneColumnMode = false; - - if (self.opts.staticGrid) { - return; - } - - _.each(self.grid.nodes, function(node) { - if (!node.noMove && !self.opts.disableDrag) { - self.dd.draggable(node.el, 'enable'); - } - if (!node.noResize && !self.opts.disableResize) { - self.dd.resizable(node.el, 'enable'); - } - - node.el.trigger('resize'); - }); - } - }; - - $(window).resize(this.onResizeHandler); - this.onResizeHandler(); - - if (!self.opts.staticGrid && typeof self.opts.removable === 'string') { - var trashZone = $(self.opts.removable); - if (!this.dd.isDroppable(trashZone)) { - this.dd.droppable(trashZone, self.opts.removableOptions); - } - this.dd - .on(trashZone, 'dropover', function(event, ui) { - var el = $(ui.draggable); - var node = el.data('_gridstack_node'); - if (node._grid !== self) { - return; - } - el.data('inTrashZone', true); - self._setupRemovingTimeout(el); - }) - .on(trashZone, 'dropout', function(event, ui) { - var el = $(ui.draggable); - var node = el.data('_gridstack_node'); - if (node._grid !== self) { - return; - } - el.data('inTrashZone', false); - self._clearRemovingTimeout(el); - }); - } - - if (!self.opts.staticGrid && self.opts.acceptWidgets) { - var draggingElement = null; - - var onDrag = function(event, ui) { - var el = draggingElement; - var node = el.data('_gridstack_node'); - var pos = self.getCellFromPixel({left: event.pageX, top: event.pageY}, true); - var x = Math.max(0, pos.x); - var y = Math.max(0, pos.y); - if (!node._added) { - node._added = true; - - node.el = el; - node.autoPosition = true; - node.x = x; - node.y = y; - self.grid.cleanNodes(); - self.grid.beginUpdate(node); - self.grid.addNode(node); - - self.container.append(self.placeholder); - self.placeholder - .attr('data-gs-x', node.x) - .attr('data-gs-y', node.y) - .attr('data-gs-width', node.width) - .attr('data-gs-height', node.height) - .show(); - node.el = self.placeholder; - node._beforeDragX = node.x; - node._beforeDragY = node.y; - - self._updateContainerHeight(); - } - if (!self.grid.canMoveNode(node, x, y)) { - return; - } - self.grid.moveNode(node, x, y); - self._updateContainerHeight(); - }; - - this.dd - .droppable(self.container, { - accept: function(el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (node && node._grid === self) { - return false; - } - return el.is(self.opts.acceptWidgets === true ? '.grid-stack-item' : self.opts.acceptWidgets); - } - }) - .on(self.container, 'dropover', function(event, ui) { - var offset = self.container.offset(); - var el = $(ui.draggable); - var cellWidth = self.cellWidth(); - var cellHeight = self.cellHeight(); - var origNode = el.data('_gridstack_node'); - - var width = origNode ? origNode.width : (Math.ceil(el.outerWidth() / cellWidth)); - var height = origNode ? origNode.height : (Math.ceil(el.outerHeight() / cellHeight)); - - draggingElement = el; - - var node = self.grid._prepareNode({width: width, height: height, _added: false, _temporary: true}); - el.data('_gridstack_node', node); - el.data('_gridstack_node_orig', origNode); - - el.on('drag', onDrag); - }) - .on(self.container, 'dropout', function(event, ui) { - var el = $(ui.draggable); - if (!el.data('_gridstack_node')) { - return; - } - el.unbind('drag', onDrag); - var node = el.data('_gridstack_node'); - node.el = null; - self.grid.removeNode(node); - self.placeholder.detach(); - self._updateContainerHeight(); - el.data('_gridstack_node', el.data('_gridstack_node_orig')); - }) - .on(self.container, 'drop', function(event, ui) { - self.placeholder.detach(); - - var node = $(ui.draggable).data('_gridstack_node'); - node._grid = self; - var el = $(ui.draggable).clone(false); - el.data('_gridstack_node', node); - var originalNode = $(ui.draggable).data('_gridstack_node_orig'); - if (typeof originalNode !== 'undefined' && typeof originalNode._grid !== 'undefined') { - originalNode._grid._triggerRemoveEvent(); - } - $(ui.helper).remove(); - node.el = el; - self.placeholder.hide(); - Utils.removePositioningStyles(el); - el.find('div.ui-resizable-handle').remove(); - - el - .attr('data-gs-x', node.x) - .attr('data-gs-y', node.y) - .attr('data-gs-width', node.width) - .attr('data-gs-height', node.height) - .addClass(self.opts.itemClass) - .enableSelection() - .removeData('draggable') - .removeClass('ui-draggable ui-draggable-dragging ui-draggable-disabled') - .unbind('drag', onDrag); - self.container.append(el); - self._prepareElementsByNode(el, node); - self._updateContainerHeight(); - self.grid._addedNodes.push(node); - self._triggerAddEvent(); - self._triggerChangeEvent(); - - self.grid.endUpdate(); - $(ui.draggable).unbind('drag', onDrag); - $(ui.draggable).removeData('_gridstack_node'); - $(ui.draggable).removeData('_gridstack_node_orig'); - self.container.trigger('dropped', [originalNode, node]); - }); - } - }; - - GridStack.prototype._triggerChangeEvent = function(forceTrigger) { - var elements = this.grid.getDirtyNodes(); - var hasChanges = false; - - var eventParams = []; - if (elements && elements.length) { - eventParams.push(elements); - hasChanges = true; - } - - if (hasChanges || forceTrigger === true) { - this.container.trigger('change', eventParams); - } - }; - - GridStack.prototype._triggerAddEvent = function() { - if (this.grid._addedNodes && this.grid._addedNodes.length > 0) { - this.container.trigger('added', [_.map(this.grid._addedNodes, _.clone)]); - this.grid._addedNodes = []; - } - }; - - GridStack.prototype._triggerRemoveEvent = function() { - if (this.grid._removedNodes && this.grid._removedNodes.length > 0) { - this.container.trigger('removed', [_.map(this.grid._removedNodes, _.clone)]); - this.grid._removedNodes = []; - } - }; - - GridStack.prototype._initStyles = function() { - if (this._stylesId) { - Utils.removeStylesheet(this._stylesId); - } - this._stylesId = 'gridstack-style-' + (Math.random() * 100000).toFixed(); - this._styles = Utils.createStylesheet(this._stylesId); - if (this._styles !== null) { - this._styles._max = 0; - } - }; - - GridStack.prototype._updateStyles = function(maxHeight) { - if (this._styles === null || typeof this._styles === 'undefined') { - return; - } - - var prefix = '.' + this.opts._class + ' .' + this.opts.itemClass; - var self = this; - var getHeight; - - if (typeof maxHeight == 'undefined') { - maxHeight = this._styles._max; - } - this._initStyles(); - this._updateContainerHeight(); - if (!this.opts.cellHeight) { // The rest will be handled by CSS - return ; - } - if (this._styles._max !== 0 && maxHeight <= this._styles._max) { // Keep this._styles._max increasing - return ; - } - - if (!this.opts.verticalMargin || this.opts.cellHeightUnit === this.opts.verticalMarginUnit) { - getHeight = function(nbRows, nbMargins) { - return (self.opts.cellHeight * nbRows + self.opts.verticalMargin * nbMargins) + - self.opts.cellHeightUnit; - }; - } else { - getHeight = function(nbRows, nbMargins) { - if (!nbRows || !nbMargins) { - return (self.opts.cellHeight * nbRows + self.opts.verticalMargin * nbMargins) + - self.opts.cellHeightUnit; - } - return 'calc(' + ((self.opts.cellHeight * nbRows) + self.opts.cellHeightUnit) + ' + ' + - ((self.opts.verticalMargin * nbMargins) + self.opts.verticalMarginUnit) + ')'; - }; - } - - if (this._styles._max === 0) { - Utils.insertCSSRule(this._styles, prefix, 'min-height: ' + getHeight(1, 0) + ';', 0); - } - - if (maxHeight > this._styles._max) { - for (var i = this._styles._max; i < maxHeight; ++i) { - Utils.insertCSSRule(this._styles, - prefix + '[data-gs-height="' + (i + 1) + '"]', - 'height: ' + getHeight(i + 1, i) + ';', - i - ); - Utils.insertCSSRule(this._styles, - prefix + '[data-gs-min-height="' + (i + 1) + '"]', - 'min-height: ' + getHeight(i + 1, i) + ';', - i - ); - Utils.insertCSSRule(this._styles, - prefix + '[data-gs-max-height="' + (i + 1) + '"]', - 'max-height: ' + getHeight(i + 1, i) + ';', - i - ); - Utils.insertCSSRule(this._styles, - prefix + '[data-gs-y="' + i + '"]', - 'top: ' + getHeight(i, i) + ';', - i - ); - } - this._styles._max = maxHeight; - } - }; - - GridStack.prototype._updateContainerHeight = function() { - if (this.grid._updateCounter) { - return; - } - var height = this.grid.getGridHeight(); - // check for css min height. Each row is cellHeight + verticalMargin, until last one which has no margin below - var cssMinHeight = parseInt(this.container.css('min-height')); - if (cssMinHeight > 0) { - var minHeight = (cssMinHeight + this.opts.verticalMargin) / (this.cellHeight() + this.opts.verticalMargin); - if (height < minHeight) { - height = minHeight; - } - } - this.container.attr('data-gs-current-height', height); - if (!this.opts.cellHeight) { - return ; - } - if (!this.opts.verticalMargin) { - this.container.css('height', (height * (this.opts.cellHeight)) + this.opts.cellHeightUnit); - } else if (this.opts.cellHeightUnit === this.opts.verticalMarginUnit) { - this.container.css('height', (height * (this.opts.cellHeight + this.opts.verticalMargin) - - this.opts.verticalMargin) + this.opts.cellHeightUnit); - } else { - this.container.css('height', 'calc(' + ((height * (this.opts.cellHeight)) + this.opts.cellHeightUnit) + - ' + ' + ((height * (this.opts.verticalMargin - 1)) + this.opts.verticalMarginUnit) + ')'); - } - }; - - GridStack.prototype._isOneColumnMode = function() { - return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) <= - this.opts.minWidth; - }; - - GridStack.prototype._setupRemovingTimeout = function(el) { - var self = this; - var node = $(el).data('_gridstack_node'); - - if (node._removeTimeout || !self.opts.removable) { - return; - } - node._removeTimeout = setTimeout(function() { - el.addClass('grid-stack-item-removing'); - node._isAboutToRemove = true; - }, self.opts.removeTimeout); - }; - - GridStack.prototype._clearRemovingTimeout = function(el) { - var node = $(el).data('_gridstack_node'); - - if (!node._removeTimeout) { - return; - } - clearTimeout(node._removeTimeout); - node._removeTimeout = null; - el.removeClass('grid-stack-item-removing'); - node._isAboutToRemove = false; - }; - - GridStack.prototype._prepareElementsByNode = function(el, node) { - var self = this; - - var cellWidth; - var cellHeight; - - var dragOrResize = function(event, ui) { - var x = Math.round(ui.position.left / cellWidth); - var y = Math.floor((ui.position.top + cellHeight / 2) / cellHeight); - var width; - var height; - - if (event.type != 'drag') { - width = Math.round(ui.size.width / cellWidth); - height = Math.round(ui.size.height / cellHeight); - } - - if (event.type == 'drag') { - var distance = ui.position.top - node._prevYPix; - node._prevYPix = ui.position.top; - Utils.updateScrollPosition(el[0], ui, distance); - if (el.data('inTrashZone') || x < 0 || x >= self.grid.width || y < 0 || - (!self.grid.float && y > self.grid.getGridHeight())) { - if (!node._temporaryRemoved) { - if (self.opts.removable === true) { - self._setupRemovingTimeout(el); - } - - x = node._beforeDragX; - y = node._beforeDragY; - - self.placeholder.detach(); - self.placeholder.hide(); - self.grid.removeNode(node); - self._updateContainerHeight(); - - node._temporaryRemoved = true; - } else { - return; - } - } else { - self._clearRemovingTimeout(el); - - if (node._temporaryRemoved) { - self.grid.addNode(node); - self.placeholder - .attr('data-gs-x', x) - .attr('data-gs-y', y) - .attr('data-gs-width', width) - .attr('data-gs-height', height) - .show(); - self.container.append(self.placeholder); - node.el = self.placeholder; - node._temporaryRemoved = false; - } - } - } else if (event.type == 'resize') { - if (x < 0) { - return; - } - } - // width and height are undefined if not resizing - var lastTriedWidth = typeof width !== 'undefined' ? width : node.lastTriedWidth; - var lastTriedHeight = typeof height !== 'undefined' ? height : node.lastTriedHeight; - if (!self.grid.canMoveNode(node, x, y, width, height) || - (node.lastTriedX === x && node.lastTriedY === y && - node.lastTriedWidth === lastTriedWidth && node.lastTriedHeight === lastTriedHeight)) { - return; - } - node.lastTriedX = x; - node.lastTriedY = y; - node.lastTriedWidth = width; - node.lastTriedHeight = height; - self.grid.moveNode(node, x, y, width, height); - self._updateContainerHeight(); - - if (event.type == 'resize') { - $(event.target).trigger('gsresize', node); - } - }; - - var onStartMoving = function(event, ui) { - self.container.append(self.placeholder); - var o = $(this); - self.grid.cleanNodes(); - self.grid.beginUpdate(node); - cellWidth = self.cellWidth(); - var strictCellHeight = Math.ceil(o.outerHeight() / o.attr('data-gs-height')); - cellHeight = self.container.height() / parseInt(self.container.attr('data-gs-current-height')); - self.placeholder - .attr('data-gs-x', o.attr('data-gs-x')) - .attr('data-gs-y', o.attr('data-gs-y')) - .attr('data-gs-width', o.attr('data-gs-width')) - .attr('data-gs-height', o.attr('data-gs-height')) - .show(); - node.el = self.placeholder; - node._beforeDragX = node.x; - node._beforeDragY = node.y; - node._prevYPix = ui.position.top; - - self.dd.resizable(el, 'option', 'minWidth', cellWidth * (node.minWidth || 1)); - self.dd.resizable(el, 'option', 'minHeight', strictCellHeight * (node.minHeight || 1)); - - if (event.type == 'resizestart') { - o.find('.grid-stack-item').trigger('resizestart'); - } - }; - - var onEndMoving = function(event, ui) { - var o = $(this); - if (!o.data('_gridstack_node')) { - return; - } - - var forceNotify = false; - self.placeholder.detach(); - node.el = o; - self.placeholder.hide(); - - if (node._isAboutToRemove) { - forceNotify = true; - var gridToNotify = el.data('_gridstack_node')._grid; - gridToNotify._triggerRemoveEvent(); - el.removeData('_gridstack_node'); - el.remove(); - } else { - self._clearRemovingTimeout(el); - if (!node._temporaryRemoved) { - Utils.removePositioningStyles(o); - o - .attr('data-gs-x', node.x) - .attr('data-gs-y', node.y) - .attr('data-gs-width', node.width) - .attr('data-gs-height', node.height); - } else { - Utils.removePositioningStyles(o); - o - .attr('data-gs-x', node._beforeDragX) - .attr('data-gs-y', node._beforeDragY) - .attr('data-gs-width', node.width) - .attr('data-gs-height', node.height); - node.x = node._beforeDragX; - node.y = node._beforeDragY; - node._temporaryRemoved = false; - self.grid.addNode(node); - } - } - self._updateContainerHeight(); - self._triggerChangeEvent(forceNotify); - - self.grid.endUpdate(); - - var nestedGrids = o.find('.grid-stack'); - if (nestedGrids.length && event.type == 'resizestop') { - nestedGrids.each(function(index, el) { - $(el).data('gridstack').onResizeHandler(); - }); - o.find('.grid-stack-item').trigger('resizestop'); - o.find('.grid-stack-item').trigger('gsresizestop'); - } - if (event.type == 'resizestop') { - self.container.trigger('gsresizestop', o); - } - }; - - this.dd - .draggable(el, { - start: onStartMoving, - stop: onEndMoving, - drag: dragOrResize - }) - .resizable(el, { - start: onStartMoving, - stop: onEndMoving, - resize: dragOrResize - }); - - if (node.noMove || (this._isOneColumnMode() && !self.opts.disableOneColumnMode) || this.opts.disableDrag || - this.opts.staticGrid) { - this.dd.draggable(el, 'disable'); - } - - if (node.noResize || (this._isOneColumnMode() && !self.opts.disableOneColumnMode) || this.opts.disableResize || - this.opts.staticGrid) { - this.dd.resizable(el, 'disable'); - } - - el.attr('data-gs-locked', node.locked ? 'yes' : null); - }; - - GridStack.prototype._prepareElement = function(el, triggerAddEvent) { - triggerAddEvent = typeof triggerAddEvent != 'undefined' ? triggerAddEvent : false; - var self = this; - el = $(el); - - el.addClass(this.opts.itemClass); - var node = self.grid.addNode({ - x: parseInt(el.attr('data-gs-x'), 10), - y: parseInt(el.attr('data-gs-y'), 10), - width: el.attr('data-gs-width'), - height: el.attr('data-gs-height'), - maxWidth: el.attr('data-gs-max-width'), - minWidth: el.attr('data-gs-min-width'), - maxHeight: el.attr('data-gs-max-height'), - minHeight: el.attr('data-gs-min-height'), - autoPosition: Utils.toBool(el.attr('data-gs-auto-position')), - noResize: Utils.toBool(el.attr('data-gs-no-resize')), - noMove: Utils.toBool(el.attr('data-gs-no-move')), - locked: Utils.toBool(el.attr('data-gs-locked')), - resizeHandles: el.attr('data-gs-resize-handles'), - el: el, - id: el.attr('data-gs-id'), - _grid: self - }, triggerAddEvent); - el.data('_gridstack_node', node); - - this._prepareElementsByNode(el, node); - }; - - GridStack.prototype.setAnimation = function(enable) { - if (enable) { - this.container.addClass('grid-stack-animate'); - } else { - this.container.removeClass('grid-stack-animate'); - } - }; - - GridStack.prototype.addWidget = function(el, x, y, width, height, autoPosition, minWidth, maxWidth, - minHeight, maxHeight, id) { - el = $(el); - if (typeof x != 'undefined') { el.attr('data-gs-x', x); } - if (typeof y != 'undefined') { el.attr('data-gs-y', y); } - if (typeof width != 'undefined') { el.attr('data-gs-width', width); } - if (typeof height != 'undefined') { el.attr('data-gs-height', height); } - if (typeof autoPosition != 'undefined') { el.attr('data-gs-auto-position', autoPosition ? 'yes' : null); } - if (typeof minWidth != 'undefined') { el.attr('data-gs-min-width', minWidth); } - if (typeof maxWidth != 'undefined') { el.attr('data-gs-max-width', maxWidth); } - if (typeof minHeight != 'undefined') { el.attr('data-gs-min-height', minHeight); } - if (typeof maxHeight != 'undefined') { el.attr('data-gs-max-height', maxHeight); } - if (typeof id != 'undefined') { el.attr('data-gs-id', id); } - this.container.append(el); - this._prepareElement(el, true); - this._triggerAddEvent(); - this._updateContainerHeight(); - this._triggerChangeEvent(true); - - return el; - }; - - GridStack.prototype.makeWidget = function(el) { - el = $(el); - this._prepareElement(el, true); - this._triggerAddEvent(); - this._updateContainerHeight(); - this._triggerChangeEvent(true); - - return el; - }; - - GridStack.prototype.willItFit = function(x, y, width, height, autoPosition) { - var node = {x: x, y: y, width: width, height: height, autoPosition: autoPosition}; - return this.grid.canBePlacedWithRespectToHeight(node); - }; - - GridStack.prototype.removeWidget = function(el, detachNode) { - detachNode = typeof detachNode === 'undefined' ? true : detachNode; - el = $(el); - var node = el.data('_gridstack_node'); - - // For Meteor support: https://github.com/troolee/gridstack.js/pull/272 - if (!node) { - node = this.grid.getNodeDataByDOMEl(el); - } - - this.grid.removeNode(node, detachNode); - el.removeData('_gridstack_node'); - this._updateContainerHeight(); - if (detachNode) { - el.remove(); - } - this._triggerChangeEvent(true); - this._triggerRemoveEvent(); - }; - - GridStack.prototype.removeAll = function(detachNode) { - _.each(this.grid.nodes, _.bind(function(node) { - this.removeWidget(node.el, detachNode); - }, this)); - this.grid.nodes = []; - this._updateContainerHeight(); - }; - - GridStack.prototype.destroy = function(detachGrid) { - $(window).off('resize', this.onResizeHandler); - this.disable(); - if (typeof detachGrid != 'undefined' && !detachGrid) { - this.removeAll(false); - this.container.removeData('gridstack'); - } else { - this.container.remove(); - } - Utils.removeStylesheet(this._stylesId); - if (this.grid) { - this.grid = null; - } - }; - - GridStack.prototype.resizable = function(el, val) { - var self = this; - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node == 'undefined' || node === null) { - return; - } - - node.noResize = !(val || false); - if (node.noResize || (self._isOneColumnMode() && !self.opts.disableOneColumnMode)) { - self.dd.resizable(el, 'disable'); - } else { - self.dd.resizable(el, 'enable'); - } - }); - return this; - }; - - GridStack.prototype.movable = function(el, val) { - var self = this; - el = $(el); - el.each(function(index, el) { - el = $(el); - var node = el.data('_gridstack_node'); - if (typeof node == 'undefined' || node === null) { - return; - } - - node.noMove = !(val || false); - if (node.noMove || (self._isOneColumnMode() && !self.opts.disableOneColumnMode)) { - self.dd.draggable(el, 'disable'); - el.removeClass('ui-draggable-handle'); - } else { - self.dd.draggable(el, 'enable'); - el.addClass('ui-draggable-handle'); - } - }); - return this; - }; - - GridStack.prototype.enableMove = function(doEnable, includeNewWidgets) { - this.movable(this.container.children('.' + this.opts.itemClass), doEnable); - if (includeNewWidgets) { - this.opts.disableDrag = !doEnable; - } - }; - - GridStack.prototype.enableResize = function(doEnable, includeNewWidgets) { - this.resizable(this.container.children('.' + this.opts.itemClass), doEnable); - if (includeNewWidgets) { - this.opts.disableResize = !doEnable; - } - }; - - GridStack.prototype.disable = function() { - this.movable(this.container.children('.' + this.opts.itemClass), false); - this.resizable(this.container.children('.' + this.opts.itemClass), false); - this.container.trigger('disable'); - }; - - GridStack.prototype.enable = function() { - this.movable(this.container.children('.' + this.opts.itemClass), true); - this.resizable(this.container.children('.' + this.opts.itemClass), true); - this.container.trigger('enable'); - }; - - GridStack.prototype.locked = 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; - } - - node.locked = (val || false); - el.attr('data-gs-locked', node.locked ? 'yes' : null); - }); - 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; - }; - - 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._updateElement = function(el, callback) { - el = $(el).first(); - var node = el.data('_gridstack_node'); - if (typeof node == 'undefined' || node === null) { - return; - } - - var self = this; - - self.grid.cleanNodes(); - self.grid.beginUpdate(node); - - callback.call(this, el, node); - - self._updateContainerHeight(); - self._triggerChangeEvent(); - - self.grid.endUpdate(); - }; - - GridStack.prototype.resize = function(el, width, height) { - this._updateElement(el, function(el, node) { - width = (width !== null && typeof width != 'undefined') ? width : node.width; - height = (height !== null && typeof height != 'undefined') ? height : node.height; - - this.grid.moveNode(node, node.x, node.y, width, height); - }); - }; - - GridStack.prototype.move = function(el, x, y) { - this._updateElement(el, function(el, node) { - x = (x !== null && typeof x != 'undefined') ? x : node.x; - y = (y !== null && typeof y != 'undefined') ? y : node.y; - - this.grid.moveNode(node, x, y, node.width, node.height); - }); - }; - - GridStack.prototype.update = function(el, x, y, width, height) { - this._updateElement(el, function(el, node) { - x = (x !== null && typeof x != 'undefined') ? x : node.x; - y = (y !== null && typeof y != 'undefined') ? y : node.y; - width = (width !== null && typeof width != 'undefined') ? width : node.width; - height = (height !== null && typeof height != 'undefined') ? height : node.height; - - this.grid.moveNode(node, x, y, width, height); - }); - }; - - GridStack.prototype.verticalMargin = function(val, noUpdate) { - if (typeof val == 'undefined') { - return this.opts.verticalMargin; - } - - var heightData = Utils.parseHeight(val); - - if (this.opts.verticalMarginUnit === heightData.unit && this.opts.height === heightData.height) { - return ; - } - this.opts.verticalMarginUnit = heightData.unit; - this.opts.verticalMargin = heightData.height; - - if (!noUpdate) { - this._updateStyles(); - } - }; - - GridStack.prototype.cellHeight = function(val, noUpdate) { - if (typeof val == 'undefined') { - if (this.opts.cellHeight) { - return this.opts.cellHeight; - } - var o = this.container.children('.' + this.opts.itemClass).first(); - return Math.ceil(o.outerHeight() / o.attr('data-gs-height')); - } - var heightData = Utils.parseHeight(val); - - if (this.opts.cellHeightUnit === heightData.unit && this.opts.cellHeight === heightData.height) { - return ; - } - this.opts.cellHeightUnit = heightData.unit; - this.opts.cellHeight = heightData.height; - - if (!noUpdate) { - this._updateStyles(); - } - - }; - - GridStack.prototype.cellWidth = function() { - return Math.round(this.container.outerWidth() / this.opts.width); - }; - - GridStack.prototype.getCellFromPixel = function(position, useOffset) { - var containerPos = (typeof useOffset != 'undefined' && useOffset) ? - this.container.offset() : this.container.position(); - var relativeLeft = position.left - containerPos.left; - var relativeTop = position.top - containerPos.top; - - var columnWidth = Math.floor(this.container.width() / this.opts.width); - var rowHeight = Math.floor(this.container.height() / parseInt(this.container.attr('data-gs-current-height'))); - - return {x: Math.floor(relativeLeft / columnWidth), y: Math.floor(relativeTop / rowHeight)}; - }; - - GridStack.prototype.batchUpdate = function() { - this.grid.batchUpdate(); - }; - - GridStack.prototype.commit = function() { - this.grid.commit(); - this._updateContainerHeight(); - }; - - GridStack.prototype.isAreaEmpty = function(x, y, width, height) { - return this.grid.isAreaEmpty(x, y, width, height); - }; - - GridStack.prototype.setStatic = function(staticValue) { - this.opts.staticGrid = (staticValue === true); - this.enableMove(!staticValue); - this.enableResize(!staticValue); - this._setStaticClass(); - }; - - GridStack.prototype._setStaticClass = function() { - var staticClassName = 'grid-stack-static'; - - if (this.opts.staticGrid === true) { - this.container.addClass(staticClassName); - } else { - this.container.removeClass(staticClassName); - } - }; - - GridStack.prototype._updateNodeWidths = function(oldWidth, newWidth) { - this.grid._sortNodes(); - this.grid.batchUpdate(); - var node = {}; - for (var i = 0; i < this.grid.nodes.length; i++) { - node = this.grid.nodes[i]; - this.update(node.el, Math.round(node.x * newWidth / oldWidth), undefined, - Math.round(node.width * newWidth / oldWidth), undefined); - } - this.grid.commit(); - }; - - GridStack.prototype.setGridWidth = function(gridWidth,doNotPropagate) { - this.container.removeClass('grid-stack-' + this.opts.width); - if (doNotPropagate !== true) { - this._updateNodeWidths(this.opts.width, gridWidth); - } - this.opts.width = gridWidth; - this.grid.width = gridWidth; - this.container.addClass('grid-stack-' + gridWidth); - }; - - // jscs:disable requireCamelCaseOrUpperCaseIdentifiers - GridStackEngine.prototype.batch_update = obsolete(GridStackEngine.prototype.batchUpdate); - GridStackEngine.prototype._fix_collisions = obsolete(GridStackEngine.prototype._fixCollisions, - '_fix_collisions', '_fixCollisions'); - GridStackEngine.prototype.is_area_empty = obsolete(GridStackEngine.prototype.isAreaEmpty, - 'is_area_empty', 'isAreaEmpty'); - GridStackEngine.prototype._sort_nodes = obsolete(GridStackEngine.prototype._sortNodes, - '_sort_nodes', '_sortNodes'); - GridStackEngine.prototype._pack_nodes = obsolete(GridStackEngine.prototype._packNodes, - '_pack_nodes', '_packNodes'); - GridStackEngine.prototype._prepare_node = obsolete(GridStackEngine.prototype._prepareNode, - '_prepare_node', '_prepareNode'); - GridStackEngine.prototype.clean_nodes = obsolete(GridStackEngine.prototype.cleanNodes, - 'clean_nodes', 'cleanNodes'); - GridStackEngine.prototype.get_dirty_nodes = obsolete(GridStackEngine.prototype.getDirtyNodes, - 'get_dirty_nodes', 'getDirtyNodes'); - GridStackEngine.prototype.add_node = obsolete(GridStackEngine.prototype.addNode, - 'add_node', 'addNode, '); - GridStackEngine.prototype.remove_node = obsolete(GridStackEngine.prototype.removeNode, - 'remove_node', 'removeNode'); - GridStackEngine.prototype.can_move_node = obsolete(GridStackEngine.prototype.canMoveNode, - 'can_move_node', 'canMoveNode'); - GridStackEngine.prototype.move_node = obsolete(GridStackEngine.prototype.moveNode, - 'move_node', 'moveNode'); - GridStackEngine.prototype.get_grid_height = obsolete(GridStackEngine.prototype.getGridHeight, - 'get_grid_height', 'getGridHeight'); - GridStackEngine.prototype.begin_update = obsolete(GridStackEngine.prototype.beginUpdate, - 'begin_update', 'beginUpdate'); - GridStackEngine.prototype.end_update = obsolete(GridStackEngine.prototype.endUpdate, - 'end_update', 'endUpdate'); - GridStackEngine.prototype.can_be_placed_with_respect_to_height = - obsolete(GridStackEngine.prototype.canBePlacedWithRespectToHeight, - 'can_be_placed_with_respect_to_height', 'canBePlacedWithRespectToHeight'); - GridStack.prototype._trigger_change_event = obsolete(GridStack.prototype._triggerChangeEvent, - '_trigger_change_event', '_triggerChangeEvent'); - GridStack.prototype._init_styles = obsolete(GridStack.prototype._initStyles, - '_init_styles', '_initStyles'); - GridStack.prototype._update_styles = obsolete(GridStack.prototype._updateStyles, - '_update_styles', '_updateStyles'); - GridStack.prototype._update_container_height = obsolete(GridStack.prototype._updateContainerHeight, - '_update_container_height', '_updateContainerHeight'); - GridStack.prototype._is_one_column_mode = obsolete(GridStack.prototype._isOneColumnMode, - '_is_one_column_mode','_isOneColumnMode'); - GridStack.prototype._prepare_element = obsolete(GridStack.prototype._prepareElement, - '_prepare_element', '_prepareElement'); - GridStack.prototype.set_animation = obsolete(GridStack.prototype.setAnimation, - 'set_animation', 'setAnimation'); - GridStack.prototype.add_widget = obsolete(GridStack.prototype.addWidget, - 'add_widget', 'addWidget'); - GridStack.prototype.make_widget = obsolete(GridStack.prototype.makeWidget, - 'make_widget', 'makeWidget'); - GridStack.prototype.will_it_fit = obsolete(GridStack.prototype.willItFit, - 'will_it_fit', 'willItFit'); - GridStack.prototype.remove_widget = obsolete(GridStack.prototype.removeWidget, - 'remove_widget', 'removeWidget'); - GridStack.prototype.remove_all = obsolete(GridStack.prototype.removeAll, - 'remove_all', 'removeAll'); - GridStack.prototype.min_height = obsolete(GridStack.prototype.minHeight, - 'min_height', 'minHeight'); - GridStack.prototype.min_width = obsolete(GridStack.prototype.minWidth, - 'min_width', 'minWidth'); - GridStack.prototype._update_element = obsolete(GridStack.prototype._updateElement, - '_update_element', '_updateElement'); - GridStack.prototype.cell_height = obsolete(GridStack.prototype.cellHeight, - 'cell_height', 'cellHeight'); - GridStack.prototype.cell_width = obsolete(GridStack.prototype.cellWidth, - 'cell_width', 'cellWidth'); - GridStack.prototype.get_cell_from_pixel = obsolete(GridStack.prototype.getCellFromPixel, - 'get_cell_from_pixel', 'getCellFromPixel'); - GridStack.prototype.batch_update = obsolete(GridStack.prototype.batchUpdate, - 'batch_update', 'batchUpdate'); - GridStack.prototype.is_area_empty = obsolete(GridStack.prototype.isAreaEmpty, - 'is_area_empty', 'isAreaEmpty'); - GridStack.prototype.set_static = obsolete(GridStack.prototype.setStatic, - 'set_static', 'setStatic'); - GridStack.prototype._set_static_class = obsolete(GridStack.prototype._setStaticClass, - '_set_static_class', '_setStaticClass'); - // jscs:enable requireCamelCaseOrUpperCaseIdentifiers - - scope.GridStackUI = GridStack; - - scope.GridStackUI.Utils = Utils; - scope.GridStackUI.Engine = GridStackEngine; - scope.GridStackUI.GridStackDragDropPlugin = GridStackDragDropPlugin; - - $.fn.gridstack = function(opts) { - return this.each(function() { - var o = $(this); - if (!o.data('gridstack')) { - o - .data('gridstack', new GridStack(this, opts)); - } - }); - }; - - return scope.GridStackUI; -}); diff --git a/dist/gridstack.min.js b/dist/gridstack.min.js deleted file mode 100644 index d969554b0..000000000 --- a/dist/gridstack.min.js +++ /dev/null @@ -1,2 +0,0 @@ -!function(t){if("function"==typeof define&&define.amd)define(["jquery","lodash","exports"],t);else if("undefined"!=typeof exports){try{jQuery=require("jquery")}catch(t){}try{_=require("lodash")}catch(t){}t(jQuery,_,exports)}else t(jQuery,_,window)}(function(_,g,t){var e=function(t,e,i){var o=function(){return console.warn("gridstack.js: Function `"+e+"` is deprecated as of v0.2.5 and has been replaced with `"+i+"`. It will be **completely** removed in v1.0."),t.apply(this,arguments)};return o.prototype=t.prototype,o},d=function(t,e){console.warn("gridstack.js: Option `"+t+"` is deprecated as of v0.2.5 and has been replaced with `"+e+"`. It will be **completely** removed in v1.0.")},u={isIntercepted:function(t,e){return!(t.x+t.width<=e.x||e.x+e.width<=t.x||t.y+t.height<=e.y||e.y+e.height<=t.y)},sort:function(t,e,i){return i=i||g.chain(t).map(function(t){return t.x+t.width}).max().value(),e=-1!=e?1:-1,g.sortBy(t,[function(t){return e*(t.x+t.y*i)}])},createStylesheet:function(t){var e=document.createElement("style");return e.setAttribute("type","text/css"),e.setAttribute("data-gs-style-id",t),e.styleSheet?e.styleSheet.cssText="":e.appendChild(document.createTextNode("")),document.getElementsByTagName("head")[0].appendChild(e),e.sheet},removeStylesheet:function(t){_("STYLE[data-gs-style-id="+t+"]").remove()},insertCSSRule:function(t,e,i,o){"function"==typeof t.insertRule?t.insertRule(e+"{"+i+"}",o):"function"==typeof t.addRule&&t.addRule(e,i,o)},toBool:function(t){return"boolean"==typeof t?t:"string"==typeof t?!(""===(t=t.toLowerCase())||"no"==t||"false"==t||"0"==t):Boolean(t)},_collisionNodeCheck:function(t){return t!=this.node&&u.isIntercepted(t,this.nn)},_didCollide:function(t){return u.isIntercepted({x:this.n.x,y:this.newY,width:this.n.width,height:this.n.height},t)},_isAddNodeIntercepted:function(t){return u.isIntercepted({x:this.x,y:this.y,width:this.node.width,height:this.node.height},t)},parseHeight:function(t){var e=t,i="px";if(e&&g.isString(e)){var o=e.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/);if(!o)throw new Error("Invalid height");i=o[2]||"px",e=parseFloat(o[1])}return{height:e,unit:i}},removePositioningStyles:function(t){var e=t[0].style;e.position&&e.removeProperty("position"),e.left&&e.removeProperty("left"),e.top&&e.removeProperty("top"),e.width&&e.removeProperty("width"),e.height&&e.removeProperty("height")},getScrollParent:function(t){return null==t?null:t.scrollHeight>t.clientHeight?t:u.getScrollParent(t.parentNode)},updateScrollPosition:function(t,e,i){var o=t.getBoundingClientRect(),a=window.innerHeight||document.documentElement.clientHeight;if(o.top<0||o.bottom>a){var s=o.bottom-a,r=o.top,n=u.getScrollParent(t);if(null!=n){var d=n.scrollTop;o.top<0&&i<0?t.offsetHeight>a?n.scrollTop+=i:n.scrollTop+=Math.abs(r)>Math.abs(i)?i:r:0a?n.scrollTop+=i:n.scrollTop+=i=t._origY;){g.chain(this.nodes).take(e).find(g.bind(u._didCollide,{n:t,newY:i})).value()||(t._dirty=!0,t.y=i),--i}},this)):g.each(this.nodes,g.bind(function(t,e){if(!t.locked)for(;0this.width?t.width=this.width:t.width<1&&(t.width=1),t.height<1&&(t.height=1),t.x<0&&(t.x=0),t.x+t.width>this.width&&(e?t.width=this.width-t.x:t.x=this.width-t.width),t.y<0&&(t.y=0),t},m.prototype._notify=function(){var t=Array.prototype.slice.call(arguments,0);if(t[0]=void 0===t[0]?[]:[t[0]],t[1]=void 0===t[1]||t[1],!this._updateCounter){var e=t[0].concat(this.getDirtyNodes());this.onchange(e,t[1])}},m.prototype.cleanNodes=function(){this._updateCounter||g.each(this.nodes,function(t){t._dirty=!1})},m.prototype.getDirtyNodes=function(){return g.filter(this.nodes,function(t){return t._dirty})},m.prototype.addNode=function(t,e){if(void 0!==(t=this._prepareNode(t)).maxWidth&&(t.width=Math.min(t.width,t.maxWidth)),void 0!==t.maxHeight&&(t.height=Math.min(t.height,t.maxHeight)),void 0!==t.minWidth&&(t.width=Math.max(t.width,t.minWidth)),void 0!==t.minHeight&&(t.height=Math.max(t.height,t.minHeight)),t._id=++s,t._dirty=!0,t.autoPosition){this._sortNodes();for(var i=0;;++i){var o=i%this.width,a=Math.floor(i/this.width);if(!(o+t.width>this.width)&&!g.find(this.nodes,g.bind(u._isAddNodeIntercepted,{x:o,y:a,node:t}))){t.x=o,t.y=a;break}}}return this.nodes.push(t),void 0!==e&&e&&this._addedNodes.push(g.clone(t)),this._fixCollisions(t),this._packNodes(),this._notify(),t},m.prototype.removeNode=function(t,e){e=void 0===e||e,this._removedNodes.push(g.clone(t)),t._id=null,this.nodes=g.without(this.nodes,t),this._packNodes(),this._notify(t,e)},m.prototype.canMoveNode=function(e,t,i,o,a){if(!this.isNodeChangedPosition(e,t,i,o,a))return!1;var s,r=Boolean(g.find(this.nodes,function(t){return t.locked}));if(!this.height&&!r)return!0;var n=new m(this.width,null,this.float,0,g.map(this.nodes,function(t){return t==e?s=_.extend({},t):_.extend({},t)}));if(void 0===s)return!0;n.moveNode(s,t,i,o,a);var d=!0;return r&&(d&=!Boolean(g.find(n.nodes,function(t){return t!=s&&Boolean(t.locked)&&Boolean(t._dirty)}))),this.height&&(d&=n.getGridHeight()<=this.height),d},m.prototype.canBePlacedWithRespectToHeight=function(t){if(!this.height)return!0;var e=new m(this.width,null,this.float,0,g.map(this.nodes,function(t){return _.extend({},t)}));return e.addNode(t),e.getGridHeight()<=this.height},m.prototype.isNodeChangedPosition=function(t,e,i,o,a){return"number"!=typeof e&&(e=t.x),"number"!=typeof i&&(i=t.y),"number"!=typeof o&&(o=t.width),"number"!=typeof a&&(a=t.height),void 0!==t.maxWidth&&(o=Math.min(o,t.maxWidth)),void 0!==t.maxHeight&&(a=Math.min(a,t.maxHeight)),void 0!==t.minWidth&&(o=Math.max(o,t.minWidth)),void 0!==t.minHeight&&(a=Math.max(a,t.minHeight)),t.x!=e||t.y!=i||t.width!=o||t.height!=a},m.prototype.moveNode=function(t,e,i,o,a,s){if(!this.isNodeChangedPosition(t,e,i,o,a))return t;if("number"!=typeof e&&(e=t.x),"number"!=typeof i&&(i=t.y),"number"!=typeof o&&(o=t.width),"number"!=typeof a&&(a=t.height),void 0!==t.maxWidth&&(o=Math.min(o,t.maxWidth)),void 0!==t.maxHeight&&(a=Math.min(a,t.maxHeight)),void 0!==t.minWidth&&(o=Math.max(o,t.minWidth)),void 0!==t.minHeight&&(a=Math.max(a,t.minHeight)),t.x==e&&t.y==i&&t.width==o&&t.height==a)return t;var r=t.width!=o;return t._dirty=!0,t.x=e,t.y=i,t.width=o,t.height=a,t.lastTriedX=e,t.lastTriedY=i,t.lastTriedWidth=o,t.lastTriedHeight=a,t=this._prepareNode(t,r),this._fixCollisions(t),s||(this._packNodes(),this._notify()),t},m.prototype.getGridHeight=function(){return g.reduce(this.nodes,function(t,e){return Math.max(t,e.y+e.height)},0)},m.prototype.beginUpdate=function(t){g.each(this.nodes,function(t){t._origY=t.y}),t._updating=!0},m.prototype.endUpdate=function(){g.each(this.nodes,function(t){t._origY=t.y});var t=g.find(this.nodes,function(t){return t._updating});t&&(t._updating=!1)};var i=function(t,e){var i,o,h=this;e=e||{},this.container=_(t),void 0!==e.handle_class&&(e.handleClass=e.handle_class,d("handle_class","handleClass")),void 0!==e.item_class&&(e.itemClass=e.item_class,d("item_class","itemClass")),void 0!==e.placeholder_class&&(e.placeholderClass=e.placeholder_class,d("placeholder_class","placeholderClass")),void 0!==e.placeholder_text&&(e.placeholderText=e.placeholder_text,d("placeholder_text","placeholderText")),void 0!==e.cell_height&&(e.cellHeight=e.cell_height,d("cell_height","cellHeight")),void 0!==e.vertical_margin&&(e.verticalMargin=e.vertical_margin,d("vertical_margin","verticalMargin")),void 0!==e.min_width&&(e.minWidth=e.min_width,d("min_width","minWidth")),void 0!==e.static_grid&&(e.staticGrid=e.static_grid,d("static_grid","staticGrid")),void 0!==e.is_nested&&(e.isNested=e.is_nested,d("is_nested","isNested")),void 0!==e.always_show_resize_handle&&(e.alwaysShowResizeHandle=e.always_show_resize_handle,d("always_show_resize_handle","alwaysShowResizeHandle")),e.itemClass=e.itemClass||"grid-stack-item";var a=0
'+this.opts.placeholderText+"
").hide(),this._updateContainerHeight(),this._updateHeightsOnResize=g.throttle(function(){h.cellHeight(h.cellWidth(),!1)},100),this.onResizeHandler=function(){if(o&&h._updateHeightsOnResize(),h._isOneColumnMode()&&!h.opts.disableOneColumnMode){if(i)return;h.container.addClass(h.opts.oneColumnModeClass),i=!0,h.grid._sortNodes(),g.each(h.grid.nodes,function(t){h.container.append(t.el),h.opts.staticGrid||(h.dd.draggable(t.el,"disable"),h.dd.resizable(t.el,"disable"),t.el.trigger("resize"))})}else{if(!i)return;if(h.container.removeClass(h.opts.oneColumnModeClass),i=!1,h.opts.staticGrid)return;g.each(h.grid.nodes,function(t){t.noMove||h.opts.disableDrag||h.dd.draggable(t.el,"enable"),t.noResize||h.opts.disableResize||h.dd.resizable(t.el,"enable"),t.el.trigger("resize")})}},_(window).resize(this.onResizeHandler),this.onResizeHandler(),!h.opts.staticGrid&&"string"==typeof h.opts.removable){var n=_(h.opts.removable);this.dd.isDroppable(n)||this.dd.droppable(n,h.opts.removableOptions),this.dd.on(n,"dropover",function(t,e){var i=_(e.draggable);i.data("_gridstack_node")._grid===h&&(i.data("inTrashZone",!0),h._setupRemovingTimeout(i))}).on(n,"dropout",function(t,e){var i=_(e.draggable);i.data("_gridstack_node")._grid===h&&(i.data("inTrashZone",!1),h._clearRemovingTimeout(i))})}if(!h.opts.staticGrid&&h.opts.acceptWidgets){var l=null,p=function(t,e){var i=l,o=i.data("_gridstack_node"),a=h.getCellFromPixel({left:t.pageX,top:t.pageY},!0),s=Math.max(0,a.x),r=Math.max(0,a.y);o._added||(o._added=!0,o.el=i,o.autoPosition=!0,o.x=s,o.y=r,h.grid.cleanNodes(),h.grid.beginUpdate(o),h.grid.addNode(o),h.container.append(h.placeholder),h.placeholder.attr("data-gs-x",o.x).attr("data-gs-y",o.y).attr("data-gs-width",o.width).attr("data-gs-height",o.height).show(),o.el=h.placeholder,o._beforeDragX=o.x,o._beforeDragY=o.y,h._updateContainerHeight()),h.grid.canMoveNode(o,s,r)&&(h.grid.moveNode(o,s,r),h._updateContainerHeight())};this.dd.droppable(h.container,{accept:function(t){var e=(t=_(t)).data("_gridstack_node");return(!e||e._grid!==h)&&t.is(!0===h.opts.acceptWidgets?".grid-stack-item":h.opts.acceptWidgets)}}).on(h.container,"dropover",function(t,e){h.container.offset();var i=_(e.draggable),o=h.cellWidth(),a=h.cellHeight(),s=i.data("_gridstack_node"),r=s?s.width:Math.ceil(i.outerWidth()/o),n=s?s.height:Math.ceil(i.outerHeight()/a);l=i;var d=h.grid._prepareNode({width:r,height:n,_added:!1,_temporary:!0});i.data("_gridstack_node",d),i.data("_gridstack_node_orig",s),i.on("drag",p)}).on(h.container,"dropout",function(t,e){var i=_(e.draggable);if(i.data("_gridstack_node")){i.unbind("drag",p);var o=i.data("_gridstack_node");o.el=null,h.grid.removeNode(o),h.placeholder.detach(),h._updateContainerHeight(),i.data("_gridstack_node",i.data("_gridstack_node_orig"))}}).on(h.container,"drop",function(t,e){h.placeholder.detach();var i=_(e.draggable).data("_gridstack_node");i._grid=h;var o=_(e.draggable).clone(!1);o.data("_gridstack_node",i);var a=_(e.draggable).data("_gridstack_node_orig");void 0!==a&&void 0!==a._grid&&a._grid._triggerRemoveEvent(),_(e.helper).remove(),i.el=o,h.placeholder.hide(),u.removePositioningStyles(o),o.find("div.ui-resizable-handle").remove(),o.attr("data-gs-x",i.x).attr("data-gs-y",i.y).attr("data-gs-width",i.width).attr("data-gs-height",i.height).addClass(h.opts.itemClass).enableSelection().removeData("draggable").removeClass("ui-draggable ui-draggable-dragging ui-draggable-disabled").unbind("drag",p),h.container.append(o),h._prepareElementsByNode(o,i),h._updateContainerHeight(),h.grid._addedNodes.push(i),h._triggerAddEvent(),h._triggerChangeEvent(),h.grid.endUpdate(),_(e.draggable).unbind("drag",p),_(e.draggable).removeData("_gridstack_node"),_(e.draggable).removeData("_gridstack_node_orig"),h.container.trigger("dropped",[a,i])})}};return i.prototype._triggerChangeEvent=function(t){var e=this.grid.getDirtyNodes(),i=!1,o=[];e&&e.length&&(o.push(e),i=!0),(i||!0===t)&&this.container.trigger("change",o)},i.prototype._triggerAddEvent=function(){this.grid._addedNodes&&0this._styles._max)){for(var a=this._styles._max;a=c.grid.width||s<0||!c.grid.float&&s>c.grid.getGridHeight()){if(l._temporaryRemoved)return;!0===c.opts.removable&&c._setupRemovingTimeout(h),a=l._beforeDragX,s=l._beforeDragY,c.placeholder.detach(),c.placeholder.hide(),c.grid.removeNode(l),c._updateContainerHeight(),l._temporaryRemoved=!0}else c._clearRemovingTimeout(h),l._temporaryRemoved&&(c.grid.addNode(l),c.placeholder.attr("data-gs-x",a).attr("data-gs-y",s).attr("data-gs-width",i).attr("data-gs-height",o).show(),c.container.append(c.placeholder),l.el=c.placeholder,l._temporaryRemoved=!1)}else if("resize"==t.type&&a<0)return;var n=void 0!==i?i:l.lastTriedWidth,d=void 0!==o?o:l.lastTriedHeight;!c.grid.canMoveNode(l,a,s,i,o)||l.lastTriedX===a&&l.lastTriedY===s&&l.lastTriedWidth===n&&l.lastTriedHeight===d||(l.lastTriedX=a,l.lastTriedY=s,l.lastTriedWidth=i,l.lastTriedHeight=o,c.grid.moveNode(l,a,s,i,o),c._updateContainerHeight(),"resize"==t.type&&_(t.target).trigger("gsresize",l))},e=function(t,e){c.container.append(c.placeholder);var i=_(this);c.grid.cleanNodes(),c.grid.beginUpdate(l),p=c.cellWidth();var o=Math.ceil(i.outerHeight()/i.attr("data-gs-height"));g=c.container.height()/parseInt(c.container.attr("data-gs-current-height")),c.placeholder.attr("data-gs-x",i.attr("data-gs-x")).attr("data-gs-y",i.attr("data-gs-y")).attr("data-gs-width",i.attr("data-gs-width")).attr("data-gs-height",i.attr("data-gs-height")).show(),l.el=c.placeholder,l._beforeDragX=l.x,l._beforeDragY=l.y,l._prevYPix=e.position.top,c.dd.resizable(h,"option","minWidth",p*(l.minWidth||1)),c.dd.resizable(h,"option","minHeight",o*(l.minHeight||1)),"resizestart"==t.type&&i.find(".grid-stack-item").trigger("resizestart")},i=function(t,e){var i=_(this);if(i.data("_gridstack_node")){var o=!1;if(c.placeholder.detach(),l.el=i,c.placeholder.hide(),l._isAboutToRemove)o=!0,h.data("_gridstack_node")._grid._triggerRemoveEvent(),h.removeData("_gridstack_node"),h.remove();else c._clearRemovingTimeout(h),l._temporaryRemoved?(u.removePositioningStyles(i),i.attr("data-gs-x",l._beforeDragX).attr("data-gs-y",l._beforeDragY).attr("data-gs-width",l.width).attr("data-gs-height",l.height),l.x=l._beforeDragX,l.y=l._beforeDragY,l._temporaryRemoved=!1,c.grid.addNode(l)):(u.removePositioningStyles(i),i.attr("data-gs-x",l.x).attr("data-gs-y",l.y).attr("data-gs-width",l.width).attr("data-gs-height",l.height));c._updateContainerHeight(),c._triggerChangeEvent(o),c.grid.endUpdate();var a=i.find(".grid-stack");a.length&&"resizestop"==t.type&&(a.each(function(t,e){_(e).data("gridstack").onResizeHandler()}),i.find(".grid-stack-item").trigger("resizestop"),i.find(".grid-stack-item").trigger("gsresizestop")),"resizestop"==t.type&&c.container.trigger("gsresizestop",i)}};this.dd.draggable(h,{start:e,stop:i,drag:t}).resizable(h,{start:e,stop:i,resize:t}),(l.noMove||this._isOneColumnMode()&&!c.opts.disableOneColumnMode||this.opts.disableDrag||this.opts.staticGrid)&&this.dd.draggable(h,"disable"),(l.noResize||this._isOneColumnMode()&&!c.opts.disableOneColumnMode||this.opts.disableResize||this.opts.staticGrid)&&this.dd.resizable(h,"disable"),h.attr("data-gs-locked",l.locked?"yes":null)},i.prototype._prepareElement=function(t,e){e=void 0!==e&&e;(t=_(t)).addClass(this.opts.itemClass);var i=this.grid.addNode({x:parseInt(t.attr("data-gs-x"),10),y:parseInt(t.attr("data-gs-y"),10),width:t.attr("data-gs-width"),height:t.attr("data-gs-height"),maxWidth:t.attr("data-gs-max-width"),minWidth:t.attr("data-gs-min-width"),maxHeight:t.attr("data-gs-max-height"),minHeight:t.attr("data-gs-min-height"),autoPosition:u.toBool(t.attr("data-gs-auto-position")),noResize:u.toBool(t.attr("data-gs-no-resize")),noMove:u.toBool(t.attr("data-gs-no-move")),locked:u.toBool(t.attr("data-gs-locked")),resizeHandles:t.attr("data-gs-resize-handles"),el:t,id:t.attr("data-gs-id"),_grid:this},e);t.data("_gridstack_node",i),this._prepareElementsByNode(t,i)},i.prototype.setAnimation=function(t){t?this.container.addClass("grid-stack-animate"):this.container.removeClass("grid-stack-animate")},i.prototype.addWidget=function(t,e,i,o,a,s,r,n,d,h,l){return t=_(t),void 0!==e&&t.attr("data-gs-x",e),void 0!==i&&t.attr("data-gs-y",i),void 0!==o&&t.attr("data-gs-width",o),void 0!==a&&t.attr("data-gs-height",a),void 0!==s&&t.attr("data-gs-auto-position",s?"yes":null),void 0!==r&&t.attr("data-gs-min-width",r),void 0!==n&&t.attr("data-gs-max-width",n),void 0!==d&&t.attr("data-gs-min-height",d),void 0!==h&&t.attr("data-gs-max-height",h),void 0!==l&&t.attr("data-gs-id",l),this.container.append(t),this._prepareElement(t,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),t},i.prototype.makeWidget=function(t){return t=_(t),this._prepareElement(t,!0),this._triggerAddEvent(),this._updateContainerHeight(),this._triggerChangeEvent(!0),t},i.prototype.willItFit=function(t,e,i,o,a){var s={x:t,y:e,width:i,height:o,autoPosition:a};return this.grid.canBePlacedWithRespectToHeight(s)},i.prototype.removeWidget=function(t,e){e=void 0===e||e;var i=(t=_(t)).data("_gridstack_node");i||(i=this.grid.getNodeDataByDOMEl(t)),this.grid.removeNode(i,e),t.removeData("_gridstack_node"),this._updateContainerHeight(),e&&t.remove(),this._triggerChangeEvent(!0),this._triggerRemoveEvent()},i.prototype.removeAll=function(e){g.each(this.grid.nodes,g.bind(function(t){this.removeWidget(t.el,e)},this)),this.grid.nodes=[],this._updateContainerHeight()},i.prototype.destroy=function(t){_(window).off("resize",this.onResizeHandler),this.disable(),void 0===t||t?this.container.remove():(this.removeAll(!1),this.container.removeData("gridstack")),u.removeStylesheet(this._stylesId),this.grid&&(this.grid=null)},i.prototype.resizable=function(t,o){var a=this;return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(i.noResize=!o,i.noResize||a._isOneColumnMode()&&!a.opts.disableOneColumnMode?a.dd.resizable(e,"disable"):a.dd.resizable(e,"enable"))}),this},i.prototype.movable=function(t,o){var a=this;return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(i.noMove=!o,i.noMove||a._isOneColumnMode()&&!a.opts.disableOneColumnMode?(a.dd.draggable(e,"disable"),e.removeClass("ui-draggable-handle")):(a.dd.draggable(e,"enable"),e.addClass("ui-draggable-handle")))}),this},i.prototype.enableMove=function(t,e){this.movable(this.container.children("."+this.opts.itemClass),t),e&&(this.opts.disableDrag=!t)},i.prototype.enableResize=function(t,e){this.resizable(this.container.children("."+this.opts.itemClass),t),e&&(this.opts.disableResize=!t)},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(t,o){return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(i.locked=o||!1,e.attr("data-gs-locked",i.locked?"yes":null))}),this},i.prototype.maxHeight=function(t,o){return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.maxHeight=o||!1,e.attr("data-gs-max-height",o)))}),this},i.prototype.minHeight=function(t,o){return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.minHeight=o||!1,e.attr("data-gs-min-height",o)))}),this},i.prototype.maxWidth=function(t,o){return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.maxWidth=o||!1,e.attr("data-gs-max-width",o)))}),this},i.prototype.minWidth=function(t,o){return(t=_(t)).each(function(t,e){var i=(e=_(e)).data("_gridstack_node");null!=i&&(isNaN(o)||(i.minWidth=o||!1,e.attr("data-gs-min-width",o)))}),this},i.prototype._updateElement=function(t,e){var i=(t=_(t).first()).data("_gridstack_node");if(null!=i){var o=this;o.grid.cleanNodes(),o.grid.beginUpdate(i),e.call(this,t,i),o._updateContainerHeight(),o._triggerChangeEvent(),o.grid.endUpdate()}},i.prototype.resize=function(t,i,o){this._updateElement(t,function(t,e){i=null!=i?i:e.width,o=null!=o?o:e.height,this.grid.moveNode(e,e.x,e.y,i,o)})},i.prototype.move=function(t,i,o){this._updateElement(t,function(t,e){i=null!=i?i:e.x,o=null!=o?o:e.y,this.grid.moveNode(e,i,o,e.width,e.height)})},i.prototype.update=function(t,i,o,a,s){this._updateElement(t,function(t,e){i=null!=i?i:e.x,o=null!=o?o:e.y,a=null!=a?a:e.width,s=null!=s?s:e.height,this.grid.moveNode(e,i,o,a,s)})},i.prototype.verticalMargin=function(t,e){if(void 0===t)return this.opts.verticalMargin;var i=u.parseHeight(t);this.opts.verticalMarginUnit===i.unit&&this.opts.height===i.height||(this.opts.verticalMarginUnit=i.unit,this.opts.verticalMargin=i.height,e||this._updateStyles())},i.prototype.cellHeight=function(t,e){if(void 0===t){if(this.opts.cellHeight)return this.opts.cellHeight;var i=this.container.children("."+this.opts.itemClass).first();return Math.ceil(i.outerHeight()/i.attr("data-gs-height"))}var o=u.parseHeight(t);this.opts.cellHeightUnit===o.unit&&this.opts.cellHeight===o.height||(this.opts.cellHeightUnit=o.unit,this.opts.cellHeight=o.height,e||this._updateStyles())},i.prototype.cellWidth=function(){return Math.round(this.container.outerWidth()/this.opts.width)},i.prototype.getCellFromPixel=function(t,e){var i=void 0!==e&&e?this.container.offset():this.container.position(),o=t.left-i.left,a=t.top-i.top,s=Math.floor(this.container.width()/this.opts.width),r=Math.floor(this.container.height()/parseInt(this.container.attr("data-gs-current-height")));return{x:Math.floor(o/s),y:Math.floor(a/r)}},i.prototype.batchUpdate=function(){this.grid.batchUpdate()},i.prototype.commit=function(){this.grid.commit(),this._updateContainerHeight()},i.prototype.isAreaEmpty=function(t,e,i,o){return this.grid.isAreaEmpty(t,e,i,o)},i.prototype.setStatic=function(t){this.opts.staticGrid=!0===t,this.enableMove(!t),this.enableResize(!t),this._setStaticClass()},i.prototype._setStaticClass=function(){var t="grid-stack-static";!0===this.opts.staticGrid?this.container.addClass(t):this.container.removeClass(t)},i.prototype._updateNodeWidths=function(t,e){this.grid._sortNodes(),this.grid.batchUpdate();for(var i={},o=0;o - + @@ -60,8 +60,8 @@

gridstack.js tests

this.grid.removeAll(); items = GridStackUI.Utils.sort(items); var id = 0; - _.each(items, function(node) { - var w = $('
'); + items.forEach(function(node) { + var w = $('
'); w.attr('id', 'item-' + (++id)); this.grid.addWidget(w, node.x, node.y, node.width, node.height); diff --git a/spec/gridstack-engine-spec.js b/spec/gridstack-engine-spec.js index ef18255a0..67e4785a4 100644 --- a/spec/gridstack-engine-spec.js +++ b/spec/gridstack-engine-spec.js @@ -209,7 +209,7 @@ describe('gridstack engine', function() { var engine; var findNode = function(engine, id) { - return _.find(engine.nodes, function(i) { return i._id === id; }); + return engine.nodes.find(function(i) { return i._id === id; }); }; beforeEach(function() { diff --git a/src/gridstack.jQueryUI.js b/src/gridstack.jQueryUI.js index 71c390e70..1cc01ccbc 100644 --- a/src/gridstack.jQueryUI.js +++ b/src/gridstack.jQueryUI.js @@ -7,7 +7,7 @@ */ (function(factory) { if (typeof define === 'function' && define.amd) { - define(['jquery', 'lodash', 'gridstack', 'exports', 'jquery-ui/data', 'jquery-ui/disable-selection', + define(['jquery', 'gridstack', 'exports', 'jquery-ui/data', 'jquery-ui/disable-selection', 'jquery-ui/focusable', 'jquery-ui/form', 'jquery-ui/ie', 'jquery-ui/keycode', 'jquery-ui/labels', 'jquery-ui/jquery-1-7', 'jquery-ui/plugin', 'jquery-ui/safe-active-element', 'jquery-ui/safe-blur', 'jquery-ui/scroll-parent', 'jquery-ui/tabbable', 'jquery-ui/unique-id', 'jquery-ui/version', @@ -15,13 +15,12 @@ 'jquery-ui/widgets/droppable', 'jquery-ui/widgets/resizable'], factory); } else if (typeof exports !== 'undefined') { try { jQuery = require('jquery'); } catch (e) {} - try { _ = require('lodash'); } catch (e) {} try { gridstack = require('gridstack'); } catch (e) {} - factory(jQuery, _, gridstack.GridStackUI, exports); + factory(jQuery, gridstack.GridStackUI, exports); } else { - factory(jQuery, _, GridStackUI, window); + factory(jQuery, GridStackUI, window); } -})(function($, _, GridStackUI, scope) { +})(function($, GridStackUI, scope) { /** * @class JQueryUIGridStackDragDropPlugin * jQuery UI implementation of drag'n'drop gridstack plugin. @@ -46,7 +45,7 @@ } else { var handles = el.data('gs-resize-handles') ? el.data('gs-resize-handles') : this.grid.opts.resizable.handles; - el.resizable(_.extend({}, this.grid.opts.resizable, { + el.resizable($.extend({}, this.grid.opts.resizable, { handles: handles }, { start: opts.start || function() {}, @@ -62,7 +61,7 @@ if (opts === 'disable' || opts === 'enable') { el.draggable(opts); } else { - el.draggable(_.extend({}, this.grid.opts.draggable, { + el.draggable($.extend({}, this.grid.opts.draggable, { containment: this.grid.opts.isNested ? this.grid.container.parent() : null, start: opts.start || function() {}, stop: opts.stop || function() {}, diff --git a/src/gridstack.js b/src/gridstack.js index a600d2aff..195c4a19f 100644 --- a/src/gridstack.js +++ b/src/gridstack.js @@ -7,15 +7,14 @@ */ (function(factory) { if (typeof define === 'function' && define.amd) { - define(['jquery', 'lodash', 'exports'], factory); + define(['jquery', 'exports'], factory); } else if (typeof exports !== 'undefined') { try { jQuery = require('jquery'); } catch (e) {} - try { _ = require('lodash'); } catch (e) {} - factory(jQuery, _, exports); + factory(jQuery, exports); } else { - factory(jQuery, _, window); + factory(jQuery, window); } -})(function($, _, scope) { +})(function($, scope) { var obsolete = function(f, oldName, newName) { var wrapper = function() { console.warn('gridstack.js: Function `' + oldName + '` is deprecated as of v0.2.5 and has been replaced ' + @@ -33,14 +32,19 @@ }; var Utils = { + isIntercepted: function(a, b) { return !(a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y || b.y + b.height <= a.y); }, sort: function(nodes, dir, width) { - width = width || _.chain(nodes).map(function(node) { return node.x + node.width; }).max().value(); + if (!width) { + var widths = nodes.map(function(node) { return node.x + node.width; }); + width = Math.max.apply(Math, widths); + } + dir = dir != -1 ? 1 : -1; - return _.sortBy(nodes, [function(n) { return dir * (n.x + n.y * width); }]); + return Utils.sortBy(nodes, function(n) { return dir * (n.x + n.y * width); }); }, createStylesheet: function(id) { @@ -94,7 +98,7 @@ parseHeight: function(val) { var height = val; var heightUnit = 'px'; - if (height && _.isString(height)) { + if (height && typeof height === 'string') { var match = height.match(/^(-[0-9]+\.[0-9]+|[0-9]*\.[0-9]+|-[0-9]+|[0-9]+)(px|em|rem|vh|vw)?$/); if (!match) { throw new Error('Invalid height'); @@ -105,6 +109,60 @@ return {height: height, unit: heightUnit}; }, + without: function(array, item) { + var index = array.indexOf(item); + + if (index !== -1) { + array = array.slice(0); + array.splice(index, 1); + } + + return array; + }, + + sortBy: function(array, getter) { + return array.slice(0).sort(function(left, right) { + var valueLeft = getter(left); + var valueRight = getter(right); + + if (valueRight === valueLeft) { + return 0; + } + + return valueLeft > valueRight ? 1 : -1; + }); + }, + + defaults: function(target) { + var sources = Array.prototype.slice.call(arguments, 1); + + sources.forEach(function(source) { + for (var prop in source) { + if (source.hasOwnProperty(prop) && !target.hasOwnProperty(prop)) { + target[prop] = source[prop]; + } + } + }); + + return target; + }, + + clone: function(target) { + return $.extend({}, target); + }, + + throttle: function(callback, delay) { + var isWaiting = false; + + return function() { + if (!isWaiting) { + callback.apply(this, arguments); + isWaiting = true; + setTimeout(function() { isWaiting = false; }, delay); + } + }; + }, + removePositioningStyles: function(el) { var style = el[0].style; if (style.position) { @@ -249,7 +307,7 @@ // For Meteor support: https://github.com/troolee/gridstack.js/pull/272 GridStackEngine.prototype.getNodeDataByDOMEl = function(el) { - return _.find(this.nodes, function(n) { return el.get(0) === n.el.get(0); }); + return this.nodes.find(function(n) { return el.get(0) === n.el.get(0); }); }; GridStackEngine.prototype._fixCollisions = function(node) { @@ -257,12 +315,12 @@ this._sortNodes(-1); var nn = node; - var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; })); + var hasLocked = Boolean(this.nodes.find(function(n) { return n.locked; })); if (!this.float && !hasLocked) { nn = {x: 0, y: node.y, width: this.width, height: node.height}; } while (true) { - var collisionNode = _.find(this.nodes, _.bind(Utils._collisionNodeCheck, {node: node, nn: nn})); + var collisionNode = this.nodes.find(Utils._collisionNodeCheck, {node: node, nn: nn}); if (typeof collisionNode == 'undefined') { return; } @@ -273,9 +331,9 @@ GridStackEngine.prototype.isAreaEmpty = function(x, y, width, height) { var nn = {x: x || 0, y: y || 0, width: width || 1, height: height || 1}; - var collisionNode = _.find(this.nodes, _.bind(function(n) { + var collisionNode = this.nodes.find(function(n) { return Utils.isIntercepted(n, nn); - }, this)); + }); return collisionNode === null || typeof collisionNode === 'undefined'; }; @@ -287,17 +345,16 @@ this._sortNodes(); if (this.float) { - _.each(this.nodes, _.bind(function(n, i) { + this.nodes.forEach(function(n, i) { if (n._updating || typeof n._origY == 'undefined' || n.y == n._origY) { return; } var newY = n.y; while (newY >= n._origY) { - var collisionNode = _.chain(this.nodes) - .take(i) - .find(_.bind(Utils._didCollide, {n: n, newY: newY})) - .value(); + var collisionNode = this.nodes + .slice(0, i) + .find(Utils._didCollide, {n: n, newY: newY}); if (!collisionNode) { n._dirty = true; @@ -305,9 +362,9 @@ } --newY; } - }, this)); + }, this); } else { - _.each(this.nodes, _.bind(function(n, i) { + this.nodes.forEach(function(n, i) { if (n.locked) { return; } @@ -316,10 +373,9 @@ var canBeMoved = i === 0; if (i > 0) { - var collisionNode = _.chain(this.nodes) - .take(i) - .find(_.bind(Utils._didCollide, {n: n, newY: newY})) - .value(); + var collisionNode = this.nodes + .slice(0, i) + .find(Utils._didCollide, {n: n, newY: newY}); canBeMoved = typeof collisionNode == 'undefined'; } @@ -329,12 +385,12 @@ n._dirty = n.y != newY; n.y = newY; } - }, this)); + }, this); } }; GridStackEngine.prototype._prepareNode = function(node, resizing) { - node = _.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0}); + node = Utils.defaults(node || {}, {width: 1, height: 1, x: 0, y: 0}); node.x = parseInt('' + node.x); node.y = parseInt('' + node.y); @@ -388,11 +444,11 @@ if (this._updateCounter) { return; } - _.each(this.nodes, function(n) {n._dirty = false; }); + this.nodes.forEach(function(n) { n._dirty = false; }); }; GridStackEngine.prototype.getDirtyNodes = function() { - return _.filter(this.nodes, function(n) { return n._dirty; }); + return this.nodes.filter(function(n) { return n._dirty; }); }; GridStackEngine.prototype.addNode = function(node, triggerAddEvent) { @@ -415,7 +471,7 @@ if (x + node.width > this.width) { continue; } - if (!_.find(this.nodes, _.bind(Utils._isAddNodeIntercepted, {x: x, y: y, node: node}))) { + if (!this.nodes.find(Utils._isAddNodeIntercepted, {x: x, y: y, node: node})) { node.x = x; node.y = y; break; @@ -425,7 +481,7 @@ this.nodes.push(node); if (typeof triggerAddEvent != 'undefined' && triggerAddEvent) { - this._addedNodes.push(_.clone(node)); + this._addedNodes.push(Utils.clone(node)); } this._fixCollisions(node); @@ -436,9 +492,9 @@ GridStackEngine.prototype.removeNode = function(node, detachNode) { detachNode = typeof detachNode === 'undefined' ? true : detachNode; - this._removedNodes.push(_.clone(node)); + this._removedNodes.push(Utils.clone(node)); node._id = null; - this.nodes = _.without(this.nodes, node); + this.nodes = Utils.without(this.nodes, node); this._packNodes(); this._notify(node, detachNode); }; @@ -447,7 +503,7 @@ if (!this.isNodeChangedPosition(node, x, y, width, height)) { return false; } - var hasLocked = Boolean(_.find(this.nodes, function(n) { return n.locked; })); + var hasLocked = Boolean(this.nodes.find(function(n) { return n.locked; })); if (!this.height && !hasLocked) { return true; @@ -459,7 +515,7 @@ null, this.float, 0, - _.map(this.nodes, function(n) { + this.nodes.map(function(n) { if (n == node) { clonedNode = $.extend({}, n); return clonedNode; @@ -476,7 +532,7 @@ var res = true; if (hasLocked) { - res &= !Boolean(_.find(clone.nodes, function(n) { + res &= !Boolean(clone.nodes.find(function(n) { return n != clonedNode && Boolean(n.locked) && Boolean(n._dirty); })); } @@ -497,7 +553,7 @@ null, this.float, 0, - _.map(this.nodes, function(n) { return $.extend({}, n); })); + this.nodes.map(function(n) { return $.extend({}, n); })); clone.addNode(node); return clone.getGridHeight() <= this.height; }; @@ -561,21 +617,21 @@ }; GridStackEngine.prototype.getGridHeight = function() { - return _.reduce(this.nodes, function(memo, n) { return Math.max(memo, n.y + n.height); }, 0); + return this.nodes.reduce(function(memo, n) { return Math.max(memo, n.y + n.height); }, 0); }; GridStackEngine.prototype.beginUpdate = function(node) { - _.each(this.nodes, function(n) { + this.nodes.forEach(function(n) { n._origY = n.y; }); node._updating = true; }; GridStackEngine.prototype.endUpdate = function() { - _.each(this.nodes, function(n) { + this.nodes.forEach(function(n) { n._origY = n.y; }); - var n = _.find(this.nodes, function(n) { return n._updating; }); + var n = this.nodes.find(function(n) { return n._updating; }); if (n) { n._updating = false; } @@ -635,7 +691,7 @@ opts.itemClass = opts.itemClass || 'grid-stack-item'; var isNested = this.container.closest('.' + opts.itemClass).length > 0; - this.opts = _.defaults(opts || {}, { + this.opts = Utils.defaults(opts || {}, { width: parseInt(this.container.attr('data-gs-width')) || 12, height: parseInt(this.container.attr('data-gs-height')) || 0, itemClass: 'grid-stack-item', @@ -652,11 +708,11 @@ _class: 'grid-stack-instance-' + (Math.random() * 10000).toFixed(0), animate: Boolean(this.container.attr('data-gs-animate')) || false, alwaysShowResizeHandle: opts.alwaysShowResizeHandle || false, - resizable: _.defaults(opts.resizable || {}, { + resizable: Utils.defaults(opts.resizable || {}, { autoHide: !(opts.alwaysShowResizeHandle || false), handles: 'se' }), - draggable: _.defaults(opts.draggable || {}, { + draggable: Utils.defaults(opts.draggable || {}, { handle: (opts.handleClass ? '.' + opts.handleClass : (opts.handle ? opts.handle : '')) || '.grid-stack-item-content', scroll: false, @@ -666,7 +722,7 @@ disableResize: opts.disableResize || false, rtl: 'auto', removable: false, - removableOptions: _.defaults(opts.removableOptions || {}, { + removableOptions: Utils.defaults(opts.removableOptions || {}, { accept: '.' + opts.itemClass }), removeTimeout: 2000, @@ -680,7 +736,7 @@ if (this.opts.ddPlugin === false) { this.opts.ddPlugin = GridStackDragDropPlugin; } else if (this.opts.ddPlugin === null) { - this.opts.ddPlugin = _.first(GridStackDragDropPlugin.registeredPlugins) || GridStackDragDropPlugin; + this.opts.ddPlugin = GridStackDragDropPlugin.registeredPlugins[0] || GridStackDragDropPlugin; } this.dd = new this.opts.ddPlugin(this); @@ -716,10 +772,10 @@ this.grid = new GridStackEngine(this.opts.width, function(nodes, detachNode) { detachNode = typeof detachNode === 'undefined' ? true : detachNode; var maxHeight = 0; - _.each(this.nodes, function(n) { + this.nodes.forEach(function(n) { maxHeight = Math.max(maxHeight, n.y + n.height); }); - _.each(nodes, function(n) { + nodes.forEach(function(n) { if (detachNode && n._id === null) { if (n.el) { n.el.remove(); @@ -746,9 +802,9 @@ i: parseInt(el.attr('data-gs-x')) + parseInt(el.attr('data-gs-y')) * _this.opts.width }); }); - _.chain(elements).sortBy(function(x) { return x.i; }).each(function(i) { - self._prepareElement(i.el); - }).value(); + Utils.sortBy(elements, function(x) { return x.i; }).forEach(function(i) { + this._prepareElement(i.el); + }, this); } this.setAnimation(this.opts.animate); @@ -759,7 +815,7 @@ this._updateContainerHeight(); - this._updateHeightsOnResize = _.throttle(function() { + this._updateHeightsOnResize = Utils.throttle(function() { self.cellHeight(self.cellWidth(), false); }, 100); @@ -776,7 +832,7 @@ oneColumnMode = true; self.grid._sortNodes(); - _.each(self.grid.nodes, function(node) { + self.grid.nodes.forEach(function(node) { self.container.append(node.el); if (self.opts.staticGrid) { @@ -799,7 +855,7 @@ return; } - _.each(self.grid.nodes, function(node) { + self.grid.nodes.forEach(function(node) { if (!node.noMove && !self.opts.disableDrag) { self.dd.draggable(node.el, 'enable'); } @@ -983,14 +1039,14 @@ GridStack.prototype._triggerAddEvent = function() { if (this.grid._addedNodes && this.grid._addedNodes.length > 0) { - this.container.trigger('added', [_.map(this.grid._addedNodes, _.clone)]); + this.container.trigger('added', [this.grid._addedNodes.map(Utils.clone)]); this.grid._addedNodes = []; } }; GridStack.prototype._triggerRemoveEvent = function() { if (this.grid._removedNodes && this.grid._removedNodes.length > 0) { - this.container.trigger('removed', [_.map(this.grid._removedNodes, _.clone)]); + this.container.trigger('removed', [this.grid._removedNodes.map(Utils.clone)]); this.grid._removedNodes = []; } }; @@ -1417,9 +1473,9 @@ }; GridStack.prototype.removeAll = function(detachNode) { - _.each(this.grid.nodes, _.bind(function(node) { + this.grid.nodes.forEach(function(node) { this.removeWidget(node.el, detachNode); - }, this)); + }, this); this.grid.nodes = []; this._updateContainerHeight(); };