gridstack.js/dist/gridstack.min.js
2015-01-29 11:32:31 -08:00

2 lines
No EOL
13 KiB
JavaScript

!function(t){"function"==typeof define&&define.amd?define(["jquery","underscore"],t):t(jQuery,_)}(function(t,e){var i=window,n={is_intercepted: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,i,n){return n=n||e.chain(t).map(function(t){return t.x+t.width}).max().value(),i=-1!=i?1:-1,e.sortBy(t,function(t){return i*(t.x+t.y*n)})},create_stylesheet:function(){var t=document.createElement("style");return t.appendChild(document.createTextNode("")),document.head.appendChild(t),t.sheet},toBool:function(t){return"boolean"==typeof t?t:"string"==typeof t?(t=t.toLowerCase(),!(""==t||"no"==t||"false"==t||"0"==t)):Boolean(t)}},o=0,a=function(t,e,i,n,o){this.width=t,this["float"]=i||!1,this.height=n||0,this.nodes=o||[],this.onchange=e||function(){}};a.prototype._fix_collisions=function(t){this._sort_nodes(-1);var i=t,o=Boolean(e.find(this.nodes,function(t){return t.locked}));for(this["float"]||o||(i={x:0,y:t.y,width:this.width,height:t.height});;){var a=e.find(this.nodes,function(e){return e!=t&&n.is_intercepted(e,i)},this);if("undefined"==typeof a)return;this.move_node(a,a.x,t.y+t.height,a.width,a.height,!0)}},a.prototype._sort_nodes=function(t){this.nodes=n.sort(this.nodes,t,this.width)},a.prototype._pack_nodes=function(){this._sort_nodes(),this["float"]?e.each(this.nodes,function(t){if(!t._updating&&"undefined"!=typeof t._orig_y&&t.y!=t._orig_y)for(var i=t.y;i>=t._orig_y;){var o=e.chain(this.nodes).find(function(e){return t!=e&&n.is_intercepted({x:t.x,y:i,width:t.width,height:t.height},e)}).value();o||(t._dirty=!0,t.y=i),--i}},this):e.each(this.nodes,function(t,i){if(!t.locked)for(;t.y>0;){var o=t.y-1,a=0==i;if(i>0){var h=e.chain(this.nodes).first(i).find(function(e){return n.is_intercepted({x:t.x,y:o,width:t.width,height:t.height},e)}).value();a="undefined"==typeof h}if(!a)break;t._dirty=t.y!=o,t.y=o}},this)},a.prototype._prepare_node=function(t,i){return t=e.defaults(t||{},{width:1,height:1,x:0,y:0}),t.x=parseInt(""+t.x),t.y=parseInt(""+t.y),t.width=parseInt(""+t.width),t.height=parseInt(""+t.height),t.auto_position=t.auto_position||!1,t.no_resize=t.no_resize||!1,t.no_move=t.no_move||!1,t.width>this.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&&(i?t.width=this.width-t.x:t.x=this.width-t.width),t.y<0&&(t.y=0),t},a.prototype._notify=function(){var t=Array.prototype.slice.call(arguments,1).concat(this.get_dirty_nodes());t=t.concat(this.get_dirty_nodes()),this.onchange(t)},a.prototype.clean_nodes=function(){e.each(this.nodes,function(t){t._dirty=!1})},a.prototype.get_dirty_nodes=function(){return e.filter(this.nodes,function(t){return t._dirty})},a.prototype.add_node=function(t){if(t=this._prepare_node(t),"undefined"!=typeof t.max_width&&(t.width=Math.min(t.width,t.max_width)),"undefined"!=typeof t.max_height&&(t.height=Math.min(t.height,t.max_height)),"undefined"!=typeof t.min_width&&(t.width=Math.max(t.width,t.min_width)),"undefined"!=typeof t.min_height&&(t.height=Math.max(t.height,t.min_height)),t._id=++o,t._dirty=!0,t.auto_position){this._sort_nodes();for(var i=0;;++i){var a=i%this.width,h=Math.floor(i/this.width);if(!(a+t.width>this.width||e.find(this.nodes,function(e){return n.is_intercepted({x:a,y:h,width:t.width,height:t.height},e)}))){t.x=a,t.y=h;break}}}return this.nodes.push(t),this._fix_collisions(t),this._pack_nodes(),this._notify(),t},a.prototype.remove_node=function(t){t._id=null,this.nodes=e.without(this.nodes,t),this._pack_nodes(),this._notify(t)},a.prototype.can_move_node=function(i,n,o,h,s){var d=Boolean(e.find(this.nodes,function(t){return t.locked}));if(!this.height&&!d)return!0;var r,_=new a(this.width,null,this["float"],0,e.map(this.nodes,function(e){return e==i?r=t.extend({},e):t.extend({},e)}));_.move_node(r,n,o,h,s);var c=!0;return d&&(c&=!Boolean(e.find(_.nodes,function(t){return t!=r&&Boolean(t.locked)&&Boolean(t._dirty)}))),this.height&&(c&=_.get_grid_height()<=this.height),c},a.prototype.can_be_placed_with_respect_to_height=function(i){if(!this.height)return!0;var n=new a(this.width,null,this["float"],0,e.map(this.nodes,function(e){return t.extend({},e)}));return n.add_node(i),n.get_grid_height()<=this.height},a.prototype.move_node=function(t,e,i,n,o,a){if("number"!=typeof e&&(e=t.x),"number"!=typeof i&&(i=t.y),"number"!=typeof n&&(n=t.width),"number"!=typeof o&&(o=t.height),"undefined"!=typeof t.max_width&&(n=Math.min(n,t.max_width)),"undefined"!=typeof t.max_height&&(o=Math.min(o,t.max_height)),"undefined"!=typeof t.min_width&&(n=Math.max(n,t.min_width)),"undefined"!=typeof t.min_height&&(o=Math.max(o,t.min_height)),t.x==e&&t.y==i&&t.width==n&&t.height==o)return t;var h=t.width!=n;return t._dirty=!0,t.x=e,t.y=i,t.width=n,t.height=o,t=this._prepare_node(t,h),this._fix_collisions(t),a||(this._pack_nodes(),this._notify()),t},a.prototype.get_grid_height=function(){return e.reduce(this.nodes,function(t,e){return Math.max(t,e.y+e.height)},0)},a.prototype.begin_update=function(t){e.each(this.nodes,function(t){t._orig_y=t.y}),t._updating=!0},a.prototype.end_update=function(){var t=e.find(this.nodes,function(t){return t._updating});t&&(t._updating=!1)};var h=function(i,o){var h,s=this;if(this.container=t(i),this.opts=e.defaults(o||{},{width:parseInt(this.container.attr("data-gs-width"))||12,height:parseInt(this.container.attr("data-gs-height"))||0,item_class:"grid-stack-item",placeholder_class:"grid-stack-placeholder",handle:".grid-stack-item-content",cell_height:60,vertical_margin:20,auto:!0,min_width:768,"float":!1,_class:"grid-stack-"+(1e4*Math.random()).toFixed(0),animate:Boolean(this.container.attr("data-gs-animate"))||!1,always_show_resize_handle:o.always_show_resize_handle||!1}),this.container.addClass(this.opts._class),this._styles=n.create_stylesheet(),this._styles._max=0,this.grid=new a(this.opts.width,function(t){var i=0;e.each(t,function(t){null==t._id?t.el.remove():(t.el.attr("data-gs-x",t.x).attr("data-gs-y",t.y).attr("data-gs-width",t.width).attr("data-gs-height",t.height),i=Math.max(i,t.y+t.height))}),s._update_styles(i+10)},this.opts["float"],this.opts.height),this.opts.auto){var d=[];this.container.find("."+this.opts.item_class).each(function(e,i){i=t(i),d.push({el:i,i:parseInt(i.attr("data-gs-x"))+parseInt(i.attr("data-gs-y"))*parseInt(i.attr("data-gs-width"))})}),e.chain(d).sortBy(function(t){return t.i}).each(function(t){s._prepare_element(t.el)})}this.set_animation(this.opts.animate),this.placeholder=t('<div class="'+this.opts.placeholder_class+" "+this.opts.item_class+'"><div class="placeholder-content" /></div>').hide(),this.container.append(this.placeholder),this.container.height(this.grid.get_grid_height()*(this.opts.cell_height+this.opts.vertical_margin)-this.opts.vertical_margin);var r=function(){if(s._is_one_column_mode()){if(h)return;h=!0,s.grid._sort_nodes(),e.each(s.grid.nodes,function(t){s.container.append(t.el),t.no_move||t.el.draggable("disable"),t.no_resize||t.el.resizable("disable")})}else{if(!h)return;h=!1,e.each(s.grid.nodes,function(t){t.no_move||t.el.draggable("enable"),t.no_resize||t.el.resizable("enable")})}};t(window).resize(r),r()};return h.prototype._update_styles=function(t){if("undefined"==typeof t){for(t=this._styles._max,this._styles._max=0;this._styles.rules.length;)this._styles.removeRule(0);this._update_container_height()}if(t>this._styles._max){for(var e=this._styles._max;t>e;++e){var i;i="."+this.opts._class+" ."+this.opts.item_class+'[data-gs-height="'+(e+1)+'"] { height: '+(this.opts.cell_height*(e+1)+this.opts.vertical_margin*e)+"px; }",this._styles.insertRule(i,e),i="."+this.opts._class+" ."+this.opts.item_class+'[data-gs-y="'+e+'"] { top: '+(this.opts.cell_height*e+this.opts.vertical_margin*e)+"px; }",this._styles.insertRule(i,e)}this._styles._max=t}},h.prototype._update_container_height=function(){this.container.height(this.grid.get_grid_height()*(this.opts.cell_height+this.opts.vertical_margin)-this.opts.vertical_margin)},h.prototype._is_one_column_mode=function(){return t(window).width()<=this.opts.min_width},h.prototype._prepare_element=function(e){var i=this;e=t(e),e.addClass(this.opts.item_class);var o=i.grid.add_node({x:e.attr("data-gs-x"),y:e.attr("data-gs-y"),width:e.attr("data-gs-width"),height:e.attr("data-gs-height"),max_width:e.attr("data-gs-max-width"),min_width:e.attr("data-gs-min-width"),max_height:e.attr("data-gs-max-height")||100,min_height:e.attr("data-gs-min-height"),auto_position:n.toBool(e.attr("data-gs-auto-position")),no_resize:n.toBool(e.attr("data-gs-no-resize")),no_move:n.toBool(e.attr("data-gs-no-move")),locked:n.toBool(e.attr("data-gs-locked")),el:e});e.data("_gridstack_node",o);var a,h,s=function(){var e=t(this);i.grid.clean_nodes(),i.grid.begin_update(o),a=Math.ceil(e.outerWidth()/e.attr("data-gs-width")),h=i.opts.cell_height+i.opts.vertical_margin,i.placeholder.attr("data-gs-x",e.attr("data-gs-x")).attr("data-gs-y",e.attr("data-gs-y")).attr("data-gs-width",e.attr("data-gs-width")).attr("data-gs-height",e.attr("data-gs-height")).show(),o.el=i.placeholder},d=function(){var e=t(this);o.el=e,i.placeholder.hide(),e.attr("data-gs-x",o.x).attr("data-gs-y",o.y).attr("data-gs-width",o.width).attr("data-gs-height",o.height).removeAttr("style"),i._update_container_height(),i.container.trigger("change",[i.grid.get_dirty_nodes()]),i.grid.end_update()};e.draggable({handle:this.opts.handle,scroll:!0,appendTo:"body",start:s,stop:d,drag:function(t,e){var n=Math.round(e.position.left/a),s=Math.floor((e.position.top+h/2)/h);i.grid.can_move_node(o,n,s,o.width,o.height)&&(i.grid.move_node(o,n,s),i._update_container_height())}}).resizable({autoHide:!this.opts.always_show_resize_handle,handles:"se",minHeight:this.opts.cell_height-10,minWidth:70,start:s,stop:d,resize:function(t,e){var n=Math.round(e.size.width/a),s=Math.round(e.size.height/h);i.grid.can_move_node(o,o.x,o.y,n,s)&&(i.grid.move_node(o,o.x,o.y,n,s),i._update_container_height())}}),(o.no_move||this._is_one_column_mode())&&e.draggable("disable"),(o.no_resize||this._is_one_column_mode())&&e.resizable("disable"),e.attr("data-gs-locked",o.locked?"yes":null)},h.prototype.set_animation=function(t){t?this.container.addClass("grid-stack-animate"):this.container.removeClass("grid-stack-animate")},h.prototype.add_widget=function(e,i,n,o,a,h){return e=t(e),"undefined"!=typeof i&&e.attr("data-gs-x",i),"undefined"!=typeof n&&e.attr("data-gs-y",n),"undefined"!=typeof o&&e.attr("data-gs-width",o),"undefined"!=typeof a&&e.attr("data-gs-height",a),"undefined"!=typeof h&&e.attr("data-gs-auto-position",h?"yes":null),this.container.append(e),this._prepare_element(e),this._update_container_height(),e},h.prototype.will_it_fit=function(t,e,i,n,o){var a={x:t,y:e,width:i,height:n,auto_position:o};return this.grid.can_be_placed_with_respect_to_height(a)},h.prototype.remove_widget=function(e){e=t(e);var i=e.data("_gridstack_node");this.grid.remove_node(i),e.remove(),this._update_container_height()},h.prototype.remove_all=function(){e.each(this.grid.nodes,function(t){t.el.remove()}),this.grid.nodes=[],this._update_container_height()},h.prototype.resizable=function(e,i){return e=t(e),e.each(function(e,n){n=t(n);var o=n.data("_gridstack_node");"undefined"!=typeof o&&(o.no_resize=!i,n.resizable(o.no_resize?"disable":"enable"))}),this},h.prototype.movable=function(e,i){return e=t(e),e.each(function(e,n){n=t(n);var o=n.data("_gridstack_node");"undefined"!=typeof o&&(o.no_move=!i,n.draggable(o.no_move?"disable":"enable"))}),this},h.prototype.locked=function(e,i){return e=t(e),e.each(function(e,n){n=t(n);var o=n.data("_gridstack_node");"undefined"!=typeof o&&(o.locked=i||!1,n.attr("data-gs-locked",o.locked?"yes":null))}),this},h.prototype._update_element=function(e,i){e=t(e).first();var n=e.data("_gridstack_node");if("undefined"!=typeof n){var o=this;o.grid.clean_nodes(),o.grid.begin_update(n),i.call(this,e,n),o._update_container_height(),o.container.trigger("change",[o.grid.get_dirty_nodes()]),o.grid.end_update()}},h.prototype.resize=function(t,e,i){this._update_element(t,function(t,n){e=null!=e&&"undefined"!=typeof e?e:n.width,i=null!=i&&"undefined"!=typeof i?i:n.height,this.grid.move_node(n,n.x,n.y,e,i)})},h.prototype.move=function(t,e,i){this._update_element(t,function(t,n){e=null!=e&&"undefined"!=typeof e?e:n.x,i=null!=i&&"undefined"!=typeof i?i:n.y,this.grid.move_node(n,e,i,n.width,n.height)})},h.prototype.cell_height=function(t){return"undefined"==typeof t?this.opts.cell_height:(t=parseInt(t),void(t!=this.opts.cell_height&&(this.opts.cell_height=t||this.opts.cell_height,this._update_styles())))},h.prototype.cell_width=function(){var t=this.container.find("."+this.opts.item_class).first();return Math.ceil(t.outerWidth()/t.attr("data-gs-width"))},h.prototype.get_cell_from_pixel=function(t){var e=this.container.position(),i=t.left-e.left,n=t.top-e.top,o=Math.floor(this.container.width()/this.opts.width),a=this.opts.cell_height+this.opts.vertical_margin;return{x:Math.floor(i/o),y:Math.floor(n/a)}},i.GridStackUI=h,i.GridStackUI.Utils=n,t.fn.gridstack=function(e){return this.each(function(){t(this).data("gridstack")||t(this).data("gridstack",new h(this,e))})},i.GridStackUI});
//# sourceMappingURL=gridstack.min.map