9 lines
No EOL
20 KiB
JavaScript
9 lines
No EOL
20 KiB
JavaScript
/**
|
|
* gridstack.js 0.2.5-dev
|
|
* http://troolee.github.io/gridstack.js/
|
|
* (c) 2014-2016 Pavel Reznikov
|
|
* gridstack.js may be freely distributed under the MIT license.
|
|
* @preserve
|
|
*/
|
|
!function(a){if("function"==typeof define&&define.amd)define(["jquery","lodash","jquery-ui/core","jquery-ui/widget","jquery-ui/mouse","jquery-ui/draggable","jquery-ui/resizable"],a);else if("undefined"!=typeof exports){try{jQuery=require("jquery")}catch(b){}try{_=require("lodash")}catch(b){}a(jQuery,_)}else a(jQuery,_)}(function(a,b){function c(a){var c=a,d="px";if(c&&b.isString(c)){var e=c.match(/^([0-9]+)(px|em|rem)?$/);if(!e)throw new Error("Invalid height");d=e[2],c=parseInt(e[1])}return{height:c,unit:d}}var d=window,e={is_intercepted: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(a,c,d){return d=d||b.chain(a).map(function(a){return a.x+a.width}).max().value(),c=-1!=c?1:-1,b.sortBy(a,function(a){return c*(a.x+a.y*d)})},create_stylesheet:function(a){var b=document.createElement("style");return b.setAttribute("type","text/css"),b.setAttribute("data-gs-id",a),b.styleSheet?b.styleSheet.cssText="":b.appendChild(document.createTextNode("")),document.getElementsByTagName("head")[0].appendChild(b),b.sheet},remove_stylesheet:function(b){a("STYLE[data-gs-id="+b+"]").remove()},insert_css_rule:function(a,b,c,d){"function"==typeof a.insertRule?a.insertRule(b+"{"+c+"}",d):"function"==typeof a.addRule&&a.addRule(b,c,d)},toBool:function(a){return"boolean"==typeof a?a:"string"==typeof a?(a=a.toLowerCase(),!(""==a||"no"==a||"false"==a||"0"==a)):Boolean(a)}},f=0,g=function(a,b,c,d,e){this.width=a,this["float"]=c||!1,this.height=d||0,this.nodes=e||[],this.onchange=b||function(){},this._update_counter=0,this._float=this["float"]};g.prototype.batch_update=function(){this._update_counter=1,this["float"]=!0},g.prototype.commit=function(){this._update_counter=0,0==this._update_counter&&(this["float"]=this._float,this._pack_nodes(),this._notify())},g.prototype._fix_collisions=function(a){this._sort_nodes(-1);var c=a,d=Boolean(b.find(this.nodes,function(a){return a.locked}));for(this["float"]||d||(c={x:0,y:a.y,width:this.width,height:a.height});;){var f=b.find(this.nodes,b.bind(function(b){return b!=a&&e.is_intercepted(b,c)},this));if("undefined"==typeof f)return;this.move_node(f,f.x,a.y+a.height,f.width,f.height,!0)}},g.prototype.is_area_empty=function(a,c,d,f){var g={x:a||0,y:c||0,width:d||1,height:f||1},h=b.find(this.nodes,b.bind(function(a){return e.is_intercepted(a,g)},this));return null==h},g.prototype._sort_nodes=function(a){this.nodes=e.sort(this.nodes,a,this.width)},g.prototype._pack_nodes=function(){this._sort_nodes(),this["float"]?b.each(this.nodes,b.bind(function(a,c){if(!a._updating&&"undefined"!=typeof a._orig_y&&a.y!=a._orig_y)for(var d=a.y;d>=a._orig_y;){var f=b.chain(this.nodes).find(function(b){return a!=b&&e.is_intercepted({x:a.x,y:d,width:a.width,height:a.height},b)}).value();f||(a._dirty=!0,a.y=d),--d}},this)):b.each(this.nodes,b.bind(function(a,c){if(!a.locked)for(;a.y>0;){var d=a.y-1,f=0==c;if(c>0){var g=b.chain(this.nodes).take(c).find(function(b){return e.is_intercepted({x:a.x,y:d,width:a.width,height:a.height},b)}).value();f="undefined"==typeof g}if(!f)break;a._dirty=a.y!=d,a.y=d}},this))},g.prototype._prepare_node=function(a,c){return a=b.defaults(a||{},{width:1,height:1,x:0,y:0}),a.x=parseInt(""+a.x),a.y=parseInt(""+a.y),a.width=parseInt(""+a.width),a.height=parseInt(""+a.height),a.auto_position=a.auto_position||!1,a.no_resize=a.no_resize||!1,a.no_move=a.no_move||!1,a.width>this.width?a.width=this.width:a.width<1&&(a.width=1),a.height<1&&(a.height=1),a.x<0&&(a.x=0),a.x+a.width>this.width&&(c?a.width=this.width-a.x:a.x=this.width-a.width),a.y<0&&(a.y=0),a},g.prototype._notify=function(){if(!this._update_counter){var a=Array.prototype.slice.call(arguments,1).concat(this.get_dirty_nodes());a=a.concat(this.get_dirty_nodes()),this.onchange(a)}},g.prototype.clean_nodes=function(){b.each(this.nodes,function(a){a._dirty=!1})},g.prototype.get_dirty_nodes=function(){return b.filter(this.nodes,function(a){return a._dirty})},g.prototype.add_node=function(a){if(a=this._prepare_node(a),"undefined"!=typeof a.max_width&&(a.width=Math.min(a.width,a.max_width)),"undefined"!=typeof a.max_height&&(a.height=Math.min(a.height,a.max_height)),"undefined"!=typeof a.min_width&&(a.width=Math.max(a.width,a.min_width)),"undefined"!=typeof a.min_height&&(a.height=Math.max(a.height,a.min_height)),a._id=++f,a._dirty=!0,a.auto_position){this._sort_nodes();for(var c=0;;++c){var d=c%this.width,g=Math.floor(c/this.width);if(!(d+a.width>this.width||b.find(this.nodes,function(b){return e.is_intercepted({x:d,y:g,width:a.width,height:a.height},b)}))){a.x=d,a.y=g;break}}}return this.nodes.push(a),this._fix_collisions(a),this._pack_nodes(),this._notify(),a},g.prototype.remove_node=function(a){a._id=null,this.nodes=b.without(this.nodes,a),this._pack_nodes(),this._notify(a)},g.prototype.can_move_node=function(c,d,e,f,h){var i=Boolean(b.find(this.nodes,function(a){return a.locked}));if(!this.height&&!i)return!0;var j,k=new g(this.width,null,this["float"],0,b.map(this.nodes,function(b){return b==c?j=a.extend({},b):a.extend({},b)}));k.move_node(j,d,e,f,h);var l=!0;return i&&(l&=!Boolean(b.find(k.nodes,function(a){return a!=j&&Boolean(a.locked)&&Boolean(a._dirty)}))),this.height&&(l&=k.get_grid_height()<=this.height),l},g.prototype.can_be_placed_with_respect_to_height=function(c){if(!this.height)return!0;var d=new g(this.width,null,this["float"],0,b.map(this.nodes,function(b){return a.extend({},b)}));return d.add_node(c),d.get_grid_height()<=this.height},g.prototype.move_node=function(a,b,c,d,e,f){if("number"!=typeof b&&(b=a.x),"number"!=typeof c&&(c=a.y),"number"!=typeof d&&(d=a.width),"number"!=typeof e&&(e=a.height),"undefined"!=typeof a.max_width&&(d=Math.min(d,a.max_width)),"undefined"!=typeof a.max_height&&(e=Math.min(e,a.max_height)),"undefined"!=typeof a.min_width&&(d=Math.max(d,a.min_width)),"undefined"!=typeof a.min_height&&(e=Math.max(e,a.min_height)),a.x==b&&a.y==c&&a.width==d&&a.height==e)return a;var g=a.width!=d;return a._dirty=!0,a.x=b,a.y=c,a.width=d,a.height=e,a=this._prepare_node(a,g),this._fix_collisions(a),f||(this._pack_nodes(),this._notify()),a},g.prototype.get_grid_height=function(){return b.reduce(this.nodes,function(a,b){return Math.max(a,b.y+b.height)},0)},g.prototype.begin_update=function(a){b.each(this.nodes,function(a){a._orig_y=a.y}),a._updating=!0},g.prototype.end_update=function(){b.each(this.nodes,function(a){a._orig_y=a.y});var a=b.find(this.nodes,function(a){return a._updating});a&&(a._updating=!1)};var h=function(c,d){var e,f=this;d=d||{},this.container=a(c),d.item_class=d.item_class||"grid-stack-item";var h=this.container.closest("."+d.item_class).size()>0;if(this.opts=b.defaults(d||{},{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",placeholder_text:"",handle:".grid-stack-item-content",handle_class:null,cell_height:60,vertical_margin:20,auto:!0,min_width:768,"float":!1,static_grid:!1,_class:"grid-stack-instance-"+(1e4*Math.random()).toFixed(0),animate:Boolean(this.container.attr("data-gs-animate"))||!1,always_show_resize_handle:d.always_show_resize_handle||!1,resizable:b.defaults(d.resizable||{},{autoHide:!d.always_show_resize_handle,handles:"se"}),draggable:b.defaults(d.draggable||{},{handle:(d.handle_class?"."+d.handle_class:d.handle?d.handle:"")||".grid-stack-item-content",scroll:!1,appendTo:"body"})}),this.opts.is_nested=h,this.cell_height(this.opts.cell_height,!0),this.vertical_margin(this.opts.vertical_margin,!0),this.container.addClass(this.opts._class),this._set_static_class(),h&&this.container.addClass("grid-stack-nested"),this._init_styles(),this.grid=new g(this.opts.width,function(a){var c=0;b.each(a,function(a){null==a._id?a.el.remove():(a.el.attr("data-gs-x",a.x).attr("data-gs-y",a.y).attr("data-gs-width",a.width).attr("data-gs-height",a.height),c=Math.max(c,a.y+a.height))}),f._update_styles(c+10)},this.opts["float"],this.opts.height),this.opts.auto){var i=[],j=this;this.container.children("."+this.opts.item_class+":not(."+this.opts.placeholder_class+")").each(function(b,c){c=a(c),i.push({el:c,i:parseInt(c.attr("data-gs-x"))+parseInt(c.attr("data-gs-y"))*j.opts.width})}),b.chain(i).sortBy(function(a){return a.i}).each(function(a){f._prepare_element(a.el)}).value()}this.set_animation(this.opts.animate),this.placeholder=a('<div class="'+this.opts.placeholder_class+" "+this.opts.item_class+'"><div class="placeholder-content">'+this.opts.placeholder_text+"</div></div>").hide(),this._update_container_height(),this.on_resize_handler=function(){if(f._is_one_column_mode()){if(e)return;e=!0,f.grid._sort_nodes(),b.each(f.grid.nodes,function(a){f.container.append(a.el),f.opts.static_grid||(a.no_move||a.el.draggable("disable"),a.no_resize||a.el.resizable("disable"))})}else{if(!e)return;if(e=!1,f.opts.static_grid)return;b.each(f.grid.nodes,function(a){a.no_move||a.el.draggable("enable"),a.no_resize||a.el.resizable("enable")})}},a(window).resize(this.on_resize_handler),this.on_resize_handler()};return h.prototype._trigger_change_event=function(a){var b=this.grid.get_dirty_nodes(),c=!1,d=[];b&&b.length&&(d.push(b),c=!0),(c||a===!0)&&this.container.trigger("change",d)},h.prototype._init_styles=function(){this.opts.cell_height&&(this._styles_id&&a('[data-gs-id="'+this._styles_id+'"]').remove(),this._styles_id="gridstack-style-"+(1e5*Math.random()).toFixed(),this._styles=e.create_stylesheet(this._styles_id),null!=this._styles&&(this._styles._max=0))},h.prototype._update_styles=function(a){if(null!=this._styles){var b,c="."+this.opts._class+" ."+this.opts.item_class,d=this;if("undefined"==typeof a&&(a=this._styles._max,this._init_styles(),this._update_container_height()),this.opts.cell_height&&!(0!==this._styles._max&&a<=this._styles._max)&&(b=this.opts.vertical_margin&&this.opts.cell_height_unit!==this.opts.vertical_margin_unit?function(a,b){return a&&b?"calc("+(d.opts.cell_height*a+d.opts.cell_height_unit)+" + "+(d.opts.vertical_margin*b+d.opts.vertical_margin_unit)+")":d.opts.cell_height*a+d.opts.vertical_margin*b+d.opts.cell_height_unit}:function(a,b){return d.opts.cell_height*a+d.opts.vertical_margin*b+d.opts.cell_height_unit},0==this._styles._max&&e.insert_css_rule(this._styles,c,"min-height: "+b(1,0)+";",0),a>this._styles._max)){for(var f=this._styles._max;a>f;++f)e.insert_css_rule(this._styles,c+'[data-gs-height="'+(f+1)+'"]',"height: "+b(f+1,f)+";",f),e.insert_css_rule(this._styles,c+'[data-gs-min-height="'+(f+1)+'"]',"min-height: "+b(f+1,f)+";",f),e.insert_css_rule(this._styles,c+'[data-gs-max-height="'+(f+1)+'"]',"max-height: "+b(f+1,f)+";",f),e.insert_css_rule(this._styles,c+'[data-gs-y="'+f+'"]',"top: "+b(f,f)+";",f);this._styles._max=a}}},h.prototype._update_container_height=function(){if(!this.grid._update_counter){var a=this.grid.get_grid_height();this.container.attr("data-gs-current-height",a),this.opts.cell_height&&(this.opts.vertical_margin?this.opts.cell_height_unit===this.opts.vertical_margin_unit?this.container.css("height",a*(this.opts.cell_height+this.opts.vertical_margin)-this.opts.vertical_margin+this.opts.cell_height_unit):this.container.css("height","calc("+(a*this.opts.cell_height+this.opts.cell_height_unit)+" + "+(a*(this.opts.vertical_margin-1)+this.opts.vertical_margin_unit)+")"):this.container.css("height",a*this.opts.cell_height+this.opts.cell_height_unit))}},h.prototype._is_one_column_mode=function(){return(window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth)<=this.opts.min_width},h.prototype._prepare_element=function(c){var d=this;c=a(c),c.addClass(this.opts.item_class);var f=d.grid.add_node({x:c.attr("data-gs-x"),y:c.attr("data-gs-y"),width:c.attr("data-gs-width"),height:c.attr("data-gs-height"),max_width:c.attr("data-gs-max-width"),min_width:c.attr("data-gs-min-width"),max_height:c.attr("data-gs-max-height"),min_height:c.attr("data-gs-min-height"),auto_position:e.toBool(c.attr("data-gs-auto-position")),no_resize:e.toBool(c.attr("data-gs-no-resize")),no_move:e.toBool(c.attr("data-gs-no-move")),locked:e.toBool(c.attr("data-gs-locked")),el:c});if(c.data("_gridstack_node",f),!d.opts.static_grid){var g,h,i=function(a,b){var c,e,i=Math.round(b.position.left/g),j=Math.floor((b.position.top+h/2)/h);"drag"!=a.type&&(c=Math.round(b.size.width/g),e=Math.round(b.size.height/h)),d.grid.can_move_node(f,i,j,c,e)&&(d.grid.move_node(f,i,j,c,e),d._update_container_height())},j=function(b,e){d.container.append(d.placeholder);var i=a(this);d.grid.clean_nodes(),d.grid.begin_update(f),g=Math.ceil(i.outerWidth()/i.attr("data-gs-width"));var j=Math.ceil(i.outerHeight()/i.attr("data-gs-height"));h=d.container.height()/parseInt(d.container.attr("data-gs-current-height")),d.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(),f.el=d.placeholder,c.resizable("option","minWidth",g*(f.min_width||1)),c.resizable("option","minHeight",j*(f.min_height||1)),"resizestart"==b.type&&i.find(".grid-stack-item").trigger("resizestart")},k=function(b,c){d.placeholder.detach();var e=a(this);f.el=e,d.placeholder.hide(),e.attr("data-gs-x",f.x).attr("data-gs-y",f.y).attr("data-gs-width",f.width).attr("data-gs-height",f.height).removeAttr("style"),d._update_container_height(),d._trigger_change_event(),d.grid.end_update();var g=e.find(".grid-stack");g.length&&"resizestop"==b.type&&(g.each(function(b,c){a(c).data("gridstack").on_resize_handler()}),e.find(".grid-stack-item").trigger("resizestop"))};c.draggable(b.extend(this.opts.draggable,{containment:this.opts.is_nested?this.container.parent():null,start:j,stop:k,drag:i})).resizable(b.extend(this.opts.resizable,{start:j,stop:k,resize:i})),(f.no_move||this._is_one_column_mode())&&c.draggable("disable"),(f.no_resize||this._is_one_column_mode())&&c.resizable("disable"),c.attr("data-gs-locked",f.locked?"yes":null)}},h.prototype.set_animation=function(a){a?this.container.addClass("grid-stack-animate"):this.container.removeClass("grid-stack-animate")},h.prototype.add_widget=function(b,c,d,e,f,g){return b=a(b),"undefined"!=typeof c&&b.attr("data-gs-x",c),"undefined"!=typeof d&&b.attr("data-gs-y",d),"undefined"!=typeof e&&b.attr("data-gs-width",e),"undefined"!=typeof f&&b.attr("data-gs-height",f),"undefined"!=typeof g&&b.attr("data-gs-auto-position",g?"yes":null),this.container.append(b),this._prepare_element(b),this._update_container_height(),this._trigger_change_event(!0),b},h.prototype.make_widget=function(b){return b=a(b),this._prepare_element(b),this._update_container_height(),this._trigger_change_event(!0),b},h.prototype.will_it_fit=function(a,b,c,d,e){var f={x:a,y:b,width:c,height:d,auto_position:e};return this.grid.can_be_placed_with_respect_to_height(f)},h.prototype.remove_widget=function(b,c){c="undefined"==typeof c?!0:c,b=a(b);var d=b.data("_gridstack_node");this.grid.remove_node(d),b.removeData("_gridstack_node"),this._update_container_height(),c&&b.remove(),this._trigger_change_event(!0)},h.prototype.remove_all=function(a){b.each(this.grid.nodes,b.bind(function(b){this.remove_widget(b.el,a)},this)),this.grid.nodes=[],this._update_container_height()},h.prototype.destroy=function(){a(window).off("resize",this.on_resize_handler),this.disable(),this.container.remove(),e.remove_stylesheet(this._styles_id),this.grid&&(this.grid=null)},h.prototype.resizable=function(b,c){var d=this;return b=a(b),b.each(function(b,e){e=a(e);var f=e.data("_gridstack_node");"undefined"!=typeof f&&null!=f&&(f.no_resize=!c,f.no_resize||d._is_one_column_mode()?e.resizable("disable"):e.resizable("enable"))}),this},h.prototype.movable=function(b,c){var d=this;return b=a(b),b.each(function(b,e){e=a(e);var f=e.data("_gridstack_node");"undefined"!=typeof f&&null!=f&&(f.no_move=!c,f.no_move||d._is_one_column_mode()?(e.draggable("disable"),e.removeClass("ui-draggable-handle")):(e.draggable("enable"),e.addClass("ui-draggable-handle")))}),this},h.prototype.disable=function(){this.movable(this.container.children("."+this.opts.item_class),!1),this.resizable(this.container.children("."+this.opts.item_class),!1),this.container.trigger("disable")},h.prototype.enable=function(){this.movable(this.container.children("."+this.opts.item_class),!0),this.resizable(this.container.children("."+this.opts.item_class),!0),this.container.trigger("enable")},h.prototype.locked=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!=e&&(e.locked=c||!1,d.attr("data-gs-locked",e.locked?"yes":null))}),this},h.prototype.min_height=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!=e&&(isNaN(c)||(e.min_height=c||!1,d.attr("data-gs-min-height",c)))}),this},h.prototype.min_width=function(b,c){return b=a(b),b.each(function(b,d){d=a(d);var e=d.data("_gridstack_node");"undefined"!=typeof e&&null!=e&&(isNaN(c)||(e.min_width=c||!1,d.attr("data-gs-min-width",c)))}),this},h.prototype._update_element=function(b,c){b=a(b).first();var d=b.data("_gridstack_node");if("undefined"!=typeof d&&null!=d){var e=this;e.grid.clean_nodes(),e.grid.begin_update(d),c.call(this,b,d),e._update_container_height(),e._trigger_change_event(),e.grid.end_update()}},h.prototype.resize=function(a,b,c){this._update_element(a,function(a,d){b=null!=b&&"undefined"!=typeof b?b:d.width,c=null!=c&&"undefined"!=typeof c?c:d.height,this.grid.move_node(d,d.x,d.y,b,c)})},h.prototype.move=function(a,b,c){this._update_element(a,function(a,d){b=null!=b&&"undefined"!=typeof b?b:d.x,c=null!=c&&"undefined"!=typeof c?c:d.y,this.grid.move_node(d,b,c,d.width,d.height)})},h.prototype.update=function(a,b,c,d,e){this._update_element(a,function(a,f){b=null!=b&&"undefined"!=typeof b?b:f.x,c=null!=c&&"undefined"!=typeof c?c:f.y,d=null!=d&&"undefined"!=typeof d?d:f.width,e=null!=e&&"undefined"!=typeof e?e:f.height,this.grid.move_node(f,b,c,d,e)})},h.prototype.vertical_margin=function(a,b){if("undefined"==typeof a)return this.opts.vertical_margin;var d=c(a);(this.opts.vertical_margin_unit!==d.unit||this.opts.height!==d.height)&&(this.opts.vertical_margin_unit=d.unit,this.opts.vertical_margin=d.height,b||this._update_styles())},h.prototype.cell_height=function(a,b){if("undefined"==typeof a){if(this.opts.cell_height)return this.opts.cell_height;var d=this.container.children("."+this.opts.item_class).first();return Math.ceil(d.outerHeight()/d.attr("data-gs-height"))}var e=c(a);(this.opts.cell_height_unit!==e.height_unit||this.opts.height!==e.height)&&(this.opts.cell_height_unit=e.unit,this.opts.cell_height=e.height,b||this._update_styles())},h.prototype.cell_width=function(){var a=this.container.children("."+this.opts.item_class).first();return Math.ceil(a.outerWidth()/a.attr("data-gs-width"))},h.prototype.get_cell_from_pixel=function(a){var b=this.container.position(),c=a.left-b.left,d=a.top-b.top,e=Math.floor(this.container.width()/this.opts.width),f=Math.floor(this.container.height()/parseInt(this.container.attr("data-gs-current-height")));return{x:Math.floor(c/e),y:Math.floor(d/f)}},h.prototype.batch_update=function(){this.grid.batch_update()},h.prototype.commit=function(){this.grid.commit(),this._update_container_height()},h.prototype.is_area_empty=function(a,b,c,d){return this.grid.is_area_empty(a,b,c,d)},h.prototype.set_static=function(a){this.opts.static_grid=a===!0,this._set_static_class()},h.prototype._set_static_class=function(){var a="grid-stack-static";this.opts.static_grid===!0?this.container.addClass(a):this.container.removeClass(a)},d.GridStackUI=h,d.GridStackUI.Utils=e,a.fn.gridstack=function(b){return this.each(function(){var c=a(this);c.data("gridstack")||c.data("gridstack",new h(this,b))})},d.GridStackUI});
|
|
//# sourceMappingURL=gridstack.min.map
|