Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
/*! gridster.js - v0.1.0 - 2012-10-20
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
(function(a,b,c,d){function e(b){return b[0]&&a.isPlainObject(b[0])?this.data=b[0]:this.el=b,this.isCoords=!0,this.coords={},this.init(),this}var f=e.prototype;f.init=function(){this.set(),this.original_coords=this.get()},f.set=function(a,b){var c=this.el;c&&!a&&(this.data=c.offset(),this.data.width=c.width(),this.data.height=c.height());if(c&&a&&!b){var d=c.offset();this.data.top=d.top,this.data.left=d.left}var e=this.data;return this.coords.x1=e.left,this.coords.y1=e.top,this.coords.x2=e.left+e.width,this.coords.y2=e.top+e.height,this.coords.cx=e.left+e.width/2,this.coords.cy=e.top+e.height/2,this.coords.width=e.width,this.coords.height=e.height,this.coords.el=c||!1,this},f.update=function(b){if(!b&&!this.el)return this;if(b){var c=a.extend({},this.data,b);return this.data=c,this.set(!0,!0)}return this.set(!0),this},f.get=function(){return this.coords},a.fn.coords=function(){if(this.data("coords"))return this.data("coords");var a=new e(this,arguments[0]);return this.data("coords",a),a}})(jQuery,window,document),function(a,b,c,d){function f(b,c,d){this.options=a.extend(e,d),this.$element=b,this.last_colliders=[],this.last_colliders_coords=[],typeof c=="string"||c instanceof jQuery?this.$colliders=a(c,this.options.colliders_context).not(this.$element):this.colliders=a(c),this.init()}var e={colliders_context:c.body},g=f.prototype;g.init=function(){this.find_collisions()},g.overlaps=function(a,b){var c=!1,d=!1;if(b.x1>=a.x1&&b.x1<=a.x2||b.x2>=a.x1&&b.x2<=a.x2||a.x1>=b.x1&&a.x2<=b.x2)c=!0;if(b.y1>=a.y1&&b.y1<=a.y2||b.y2>=a.y1&&b.y2<=a.y2||a.y1>=b.y1&&a.y2<=b.y2)d=!0;return c&&d},g.detect_overlapping_region=function(a,b){var c="",d="";return a.y1>b.cy&&a.y1<b.y2&&(c="N"),a.y2>b.y1&&a.y2<b.cy&&(c="S"),a.x1>b.cx&&a.x1<b.x2&&(d="W"),a.x2>b.x1&&a.x2<b.cx&&(d="E"),c+d||"C"},g.calculate_overlapped_area_coords=function(b,c){var d=Math.max(b.x1,c.x1),e=Math.max(b.y1,c.y1),f=Math.min(b.x2,c.x2),g=Math.min(b.y2,c.y2);return a({left:d,top:e,width:f-d,height:g-e}).coords().get()},g.calculate_overlapped_area=function(a){return a.width*a.height},g.manage_colliders_start_stop=function(b,c,d){var e=this.last_colliders_coords;for(var f=0,g=e.length;f<g;f++)a.inArray(e[f],b)===-1&&c.call(this,e[f]);for(var h=0,i=b.length;h<i;h++)a.inArray(b[h],e)===-1&&d.call(this,b[h])},g.find_collisions=function(b){var c=this,d=[],e=[],f=this.colliders||this.$colliders,g=f.length,h=c.$element.coords().update(b||!1).get();while(g--){var i=c.$colliders?a(f[g]):f[g],j=i.isCoords?i:i.coords(),k=j.get(),l=c.overlaps(h,k);if(!l)continue;var m=c.detect_overlapping_region(h,k);if(m==="C"){var n=c.calculate_overlapped_area_coords(h,k),o=c.calculate_overlapped_area(n),p={area:o,area_coords:n,region:m,coords:k,player_coords:h,el:i};c.options.on_overlap&&c.options.on_overlap.call(this,p),d.push(j),e.push(p)}}return(c.options.on_overlap_stop||c.options.on_overlap_start)&&this.manage_colliders_start_stop(d,c.options.on_overlap_stop,c.options.on_overlap_start),this.last_colliders_coords=d,e},g.get_closest_colliders=function(a){var b=this.find_collisions(a);return b.sort(function(a,b){return a.region==="C"&&b.region==="C"?a.coords.y1<b.coords.y1||a.coords.x1<b.coords.x1?-1:1:a.area<b.area?1:1}),b},a.fn.collision=function(a,b){return new f(this,a,b)}}(jQuery,window,document),function(a,b){a.debounce=function(a,b,c){var d;return function(){var e=this,f=arguments,g=function(){d=null,c||a.apply(e,f)};c&&!d&&a.apply(e,f),clearTimeout(d),d=setTimeout(g,b)}},a.throttle=function(a,b){var c,d,e,f,g,h,i=debounce(function(){g=f=!1},b);return function(){c=this,d=arguments;var j=function(){e=null,g&&a.apply(c,d),i()};return e||(e=setTimeout(j,b)),f?g=!0:h=a.apply(c,d),i(),f=!0,h}}}(window),function(a,b,c,d){function i(b,d){this.options=a.extend({},e,d),this.$body=a(c.body),this.$container=a(b),this.$dragitems=a(this.options.items,this.$container),this.is_dragging=!1,this.player_min_left=0+this.options.offset_left,this.init()}var e={items:".gs_w",distance:1,limit:!0,offset_left:0,autoscroll:!0,ignore_dragging:["INPUT","TEXTAREA","SELECT","BUTTON"],handle:null},f=a(b),g="ontouchstart"in b,h={start:g?"touchstart":"mousedown.draggable",move:g?"touchmove":"mousemove.draggable",end:g?"touchend":"mouseup.draggable"},j=i.prototype;j.init=function(){this.calculate_positions(),this.$container.css("position","relative"),this.disabled=!1,this.events(),a(b).bind("resize",throttle(a.proxy(this.calculate_positions,this),200))},j.events=function(){this.$container.on("selectstart",a.proxy(this.on_select_start,this)),this.$container.on(h.start,this.options.items,a.proxy(this.drag_handler,this)),this.$body.on(h.end,a.proxy(function(a){this.is_dragging=!1;if(this.disabled)return;this.$body.off(h.move),this.drag_start&&this.on_dragstop(a)},this))},j.get_actual_pos=function(a){var b=a.position();return b},j.get_mouse_pos=function(a){if(g){var b=a.originalEvent;a=b.touches.length?b.touches[0]:b.changedTouches[0]}return{left:a.clientX,top:a.clientY}},j.get_offset=function(a){a.preventDefault();var b=this.get_mouse_pos(a),c=Math.round(b.left-this.mouse_init_pos.left),d=Math.round(b.top-this.mouse_init_pos.top),e=Math.round(this.el_init_offset.left+c-this.baseX),f=Math.round(this.el_init_offset.top+d-this.baseY+this.scrollOffset);return this.options.limit&&(e>this.player_max_left?e=this.player_max_left:e<this.player_min_left&&(e=this.player_min_left)),{left:e,top:f,mouse_left:b.left,mouse_top:b.top}},j.manage_scroll=function(a){var b,c=f.scrollTop(),d=c,e=d+this.window_height,g=e-50,h=d+50,i=a.mouse_left,j=d+a.mouse_top,k=this.doc_height-this.window_height+this.player_height;j>=g&&(b=c+30,b<k&&(f.scrollTop(b),this.scrollOffset=this.scrollOffset+30)),j<=h&&(b=c-30,b>0&&(f.scrollTop(b),this.scrollOffset=this.scrollOffset-30))},j.calculate_positions=function(a){this.window_height=f.height()},j.drag_handler=function(b){var c=b.target.nodeName;if(this.disabled||b.which!==1&&!g)return;if(this.ignore_drag(b))return;var d=this,e=!0;return this.$player=a(b.currentTarget),this.el_init_pos=this.get_actual_pos(this.$player),this.mouse_init_pos=this.get_mouse_pos(b),this.offsetY=this.mouse_init_pos.top-this.el_init_pos.top,this.$body.on(h.move,function(a){var b=d.get_mouse_pos(a),c=Math.abs(b.left-d.mouse_init_pos.left),f=Math.abs(b.top-d.mouse_init_pos.top);return c>d.options.distance||f>d.options.distance?e?(e=!1,d.on_dragstart.call(d,a),!1):(d.is_dragging===!0&&d.on_dragmove.call(d,a),!1):!1}),!1},j.on_dragstart=function(b){b.preventDefault(),this.drag_start=!0,this.is_dragging=!0;var d=this.$container.offset();return this.baseX=Math.round(d.left),this.baseY=Math.round(d.top),this.doc_height=a(c).height(),this.options.helper==="clone"?(this.$helper=this.$player.clone().appendTo(this.$container).addClass("helper"),this.helper=!0):this.helper=!1,this.scrollOffset=0,this.el_init_offset=this.$player.offset(),this.player_width=this.$player.width(),this.player_height=this.$player.height(),this.player_max_left=this.$container.width()-this.player_width+this.options.offset_left,this.options.start&&this.options.start.call(this.$player,b,{helper:this.helper?this.$helper:this.$player}),!1},j.on_dragmove=function(a){var b=this.get_offset(a);this.options.autoscroll&&this.manage_scroll(b),(this.helper?this.$helper:this.$player).css({position:"absolute",left:b.left,top:b.top});var c={position:{left:b.left,top:b.top}};return this.options.drag&&this.options.drag.call(this.$player,a,c),!1},j.on_dragstop=function(a){var b=this.get_offset(a);this.drag_start=!1;var c={position:{left:b.left,top:b.top}};return this.options.stop&&this.options.stop.call(this.$player,a,c),this.helper&&this.$helper.remove(),!1},j.on_select_start=function(a){if(this.disabled)return;if(this.ignore_drag(a))return;return!1},j.enable=function(){this.disabled=!1},j.disable=function(){this.disabled=!0},j.destroy=function(){this.disable(),a.removeData(this.$container,"drag")},j.ignore_drag=function(b){return this.options.handle?!a(b.target).is(this.options.handle):a.inArray(b.target.nodeName,this.options.ignore_dragging)>=0},a.fn.drag=function(b){return this.each(function(){a.data(this,"drag")||a.data(this,"drag",new i(this,b))})}}(jQuery,window,document),function(a,b,c,d){function f(b,c){this.options=a.extend(!0,e,c),this.$el=a(b),this.$wrapper=this.$el.parent(),this.$widgets=this.$el.children(this.options.widget_selector).addClass("gs_w"),this.widgets=[],this.$changed=a([]),this.wrapper_width=this.$wrapper.width(),this.min_widget_width=this.options.widget_margins[0]*2+this.options.widget_base_dimensions[0],this.min_widget_height=this.options.widget_margins[1]*2+this.options.widget_base_dimensions[1],this.init()}var e={namespace:"",widget_selector:"li",widget_margins:[10,10],widget_base_dimensions:[400,225],extra_rows:0,extra_cols:0,min_cols:1,min_rows:15,max_size_x:6,autogenerate_stylesheet:!0,avoid_overlapped_widgets:!0,serialize_params:function(a,b){return{col:b.col,row:b.row,size_x:b.size_x,size_y:b.size_y}},collision:{},draggable:{distance:4}};f.generated_stylesheets=[];var g=f.prototype;g.init=function(){this.generate_grid_and_stylesheet(),this.get_widgets_from_DOM(),this.set_dom_grid_height(),this.$wrapper.addClass("ready"),this.draggable(),a(b).bind("resize",throttle(a.proxy(this.recalculate_faux_grid,this),200))},g.disable=function(){return this.$wrapper.find(".player-revert").removeClass("player-revert"),this.drag_api.disable(),this},g.enable=function(){return this.drag_api.enable(),this},g.add_widget=function(b,c,d,e,f){var g;c||(c=1),d||(d=1),!e&!f?g=this.next_position(c,d):(g={col:e,row:f},this.empty_cells(e,f,c,d));var h=a(b).attr({"data-col":g.col,"data-row":g.row,"data-sizex":c,"data-sizey":d}).addClass("gs_w").appendTo(this.$el).hide();return this.$widgets=this.$widgets.add(h),this.register_widget(h),this.add_faux_rows(g.size_y),this.set_dom_grid_height(),h.fadeIn()},g.resize_widget=function(b,c,d){var e=b.coords().grid;c||(c=e.size_x),d||(d=e.size_y),c>this.cols&&(c=this.cols);var f=this.get_cells_occupied(e),g=e.size_x,h=e.size_y,i=e.col,j=i,k=c>g,l=d>h;if(i+c-1>this.cols){var m=i+(c-1)-this.cols,n=i-m;j=Math.max(1,n)}var o={col:j,row:e.row,size_x:c,size_y:d},p=this.get_cells_occupied(o),q=[];a.each(f.cols,function(b,c){a.inArray(c,p.cols)===-1&&q.push(c)});var r=[];a.each(p.cols,function(b,c){a.inArray(c,f.cols)===-1&&r.push(c)});var s=[];a.each(f.rows,function(b,c){a.inArray(c,p.rows)===-1&&s.push(c)});var t=[];a.each(p.rows,function(b,c){a.inArray(c,f.rows)===-1&&t.push(c)}),this.remove_from_gridmap(e);if(r.length){var u=[j,e.row,c,Math.min(h,d),b];this.empty_cells.apply(this,u)}if(t.length){var v=[j,e.row,c,d,b];this.empty_cells.apply(this,v)}e.col=j,e.size_x=c,e.size_y=d,this.add_to_gridmap(o,b),b.data("coords").update({width:c*this.options.widget_base_dimensions[0]+(c-1)*this.options.widget_margins[0]*2,height:d*this.options.widget_base_dimensions[1]+(d-1)*this.options.widget_margins[1]*2}),d>h&&this.add_faux_rows(d-h),c>g&&this.add_faux_cols(c-g),b.attr({"data-col":j,"data-sizex":c,"data-sizey":d});if(q.length){var w=[q[0],e.row,q.length,Math.min(h,d),b];this.remove_empty_cells.apply(this,w)}if(s.length){var x=[j,e.row,c,d,b];this.remove_empty_cells.apply(this,x)}return b},g.empty_cells=function(b,c,d,e,f){var g=this.widgets_below({col:b,row:c-e,size_x:d,size_y:e});return g.not(f).each(a.proxy(function(b,d){var f=a(d).coords().grid;if(!(f.row<=c+e-1))return;var g=c+e-f.row;this.move_widget_down(a(d),g)},this)),this.set_dom_grid_height(),this},g.remove_empty_cells=function(b,c,d,e,f){var g=this.widgets_below({col:b,row:c,size_x:d,size_y:e});return g.not(f).each(a.proxy(function(b,c){this.move_widget_up(a(c),e)},this)),this.set_dom_grid_height(),this},g.next_position=function(a,b){a||(a=1),b||(b=1);var c=this.gridmap,d=c.length,e=[],f;for(var g=1;g<d;g++){f=c[g].length;for(var h=1;h<=f;h++){var i=this.can_move_to({size_x:a,size_y:b},g,h);i&&e.push({col:g,row:h,size_y:b,size_x:a})}}return e.length?this.sort_by_row_and_col_asc(e)[0]:!1},g.remove_widget=function(b,c,d){var e=b instanceof jQuery?b:a(b),f=e.coords().grid;a.isFunction(c)&&(d=c,c=!1),this.cells_occupied_by_placeholder={},this.$widgets=this.$widgets.not(e);var g=this.widgets_below(e);this.remove_from_gridmap(f),e.fadeOut(a.proxy(function(){e.remove(),c||g.each(a.proxy(function(b,c){this.move_widget_up(a(c),f.size_y)},this)),this.set_dom_grid_height(),d&&d.call(this,b)},this))},g.remove_all_widgets=function(b){return this.$widgets.each(a.proxy(function(a,c){this.remove_widget(c,!0,b)},this)),this},g.serialize=function(b){b||(b=this.$widgets);var c=[];return b.each(a.proxy(function(b,d){c.push(this.options.serialize_params(a(d),a(d).coords().grid))},this)),c},g.serialize_changed=function(){return this.serialize(this.$changed)},g.register_widget=function(a){var b={col:parseInt(a.attr("data-col"),10),row:parseInt(a.attr("data-row"),10),size_x:parseInt(a.attr("data-sizex"),10),size_y:parseInt(a.attr("data-sizey"),10),el:a};return this.options.avoid_overlapped_widgets&&!this.can_move_to({size_x:b.size_x,size_y:b.size_y},b.col,b.row)&&(b=this.next_position(b.size_x,b.size_y),b.el=a,a.attr({"data-col":b.col,"data-row":b.row,"data-sizex":b.size_x,"data-sizey":b.size_y})),a.data("coords",a.coords()),a.data("coords").grid=b,this.add_to_gridmap(b,a),this},g.update_widget_position=function(a,b){return this.for_each_cell_occupied(a,function(a,c){if(!this.gridmap[a])return this;this.gridmap[a][c]=b}),this},g.remove_from_gridmap=function(a){return this.update_widget_position(a,!1)},g.add_to_gridmap=function(b,c){this.update_widget_position(b,c||b.el);if(b.el){var d=this.widgets_below(b.el);d.each(a.proxy(function(b,c){this.move_widget_up(a(c))},this))}},g.draggable=function(){var b=this,c=a.extend(!0,{},this.options.draggable,{offset_left:this.options.widget_margins[0],start:function(c,d){b.$widgets.filter(".player-revert").removeClass("player-revert"),b.$player=a(this),b.$helper=b.options.draggable.helper==="clone"?a(d.helper):b.$player,b.helper=!b.$helper.is(b.$player),b.on_start_drag.call(b,c,d),b.$el.trigger("gridster:dragstart")},stop:function(a,c){b.on_stop_drag.call(b,a,c),b.$el.trigger("gridster:dragstop")},drag:throttle(function(a,c){b.on_drag.call(b,a,c),b.$el.trigger("gridster:drag")},60)});return this.drag_api=this.$el.drag(c).data("drag"),this},g.on_start_drag=function(b,c){this.$helper.add(this.$player).add(this.$wrapper).addClass("dragging"),this.$player.addClass("player"),this.player_grid_data=this.$player.coords().grid,this.placeholder_grid_data=a.extend({},this.player_grid_data),this.$el.css("height",this.$el.height()+this.player_grid_data.size_y*this.min_widget_height);var d=this.faux_grid,e=this.$player.data("coords").coords;this.cells_occupied_by_player=this.get_cells_occupied(this.player_grid_data),this.cells_occupied_by_placeholder=this.get_cells_occupied(this.placeholder_grid_data),this.last_cols=[],this.last_rows=[],this.collision_api=this.$helper.collision(d,this.options.collision),this.$preview_holder=a("<li />",{"class":"preview-holder","data-row":this.$player.attr("data-row"),"data-col":this.$player.attr("data-col"),css:{width:e.width,height:e.height}}).appendTo(this.$el),this.options.draggable.start&&this.options.draggable.start.call(this,b,c)},g.on_drag=function(a,b){if(this.$player===null)return!1;var c={left:b.position.left+this.baseX,top:b.position.top+this.baseY};this.colliders_data=this.collision_api.get_closest_colliders(c),this.on_overlapped_column_change(this.on_start_overlapping_column,this.on_stop_overlapping_column),this.on_overlapped_row_change(this.on_start_overlapping_row,this.on_stop_overlapping_row),this.helper&&this.$player&&this.$player.css({left:b.position.left,top:b.position.top}),this.options.draggable.drag&&this.options.draggable.drag.call(this,a,b)},g.on_stop_drag=function(a,b){this.$helper.add(this.$player).add(this.$wrapper).removeClass("dragging"),b.position.left=b.position.left+this.baseX,b.position.top=b.position.top+this.baseY,this.colliders_data=this.collision_api.get_closest_colliders(b.position),this.on_overlapped_column_change(this.on_start_overlapping_column,this.on_stop_overlapping_column),this.on_overlapped_row_change(this.on_start_overlapping_row,this.on_stop_overlapping_row),this.$player.addClass("player-revert").removeClass("player").attr({"data-col":this.placeholder_grid_data.col,"data-row":this.placeholder_grid_data.row}).css({left:"",top:""}),this.$changed=this.$changed.add(this.$player),this.cells_occupied_by_player=this.get_cells_occupied(this.placeholder_grid_data),this.set_cells_player_occupies(this.placeholder_grid_data.col,this.placeholder_grid_data.row),this.$player.coords().grid.row=this.placeholder_grid_data.row,this.$player.coords().grid.col=this.placeholder_grid_data.col,this.options.draggable.stop&&this.options.draggable.stop.call(this,a,b),this.$preview_holder.remove(),this.$player=null,this.$helper=null,this.placeholder_grid_data={},this.player_grid_data={},this.cells_occupied_by_placeholder={},this.cells_occupied_by_player={},this.set_dom_grid_height()},g.on_overlapped_column_change=function(b,c){if(!this.colliders_data.length)return;var d=this.get_targeted_columns(this.colliders_data[0].el.data.col),e=this.last_cols.length,f=d.length,g;for(g=0;g<f;g++)a.inArray(d[g],this.last_cols)===-1&&(b||a.noop).call(this,d[g]);for(g=0;g<e;g++)a.inArray(this.last_cols[g],d)===-1&&(c||a.noop).call(this,this.last_cols[g]);return this.last_cols=d,this},g.on_overlapped_row_change=function(b,c){if(!this.colliders_data.length)return;var d=this.get_targeted_rows(this.colliders_data[0].el.data.row),e=this.last_rows.length,f=d.length,g;for(g=0;g<f;g++)a.inArray(d[g],this.last_rows)===-1&&(b||a.noop).call(this,d[g]);for(g=0;g<e;g++)a.inArray(this.last_rows[g],d)===-1&&(c||a.noop).call(this,this.last_rows[g]);this.last_rows=d},g.set_player=function(a,b,c){var d=this;c||this.empty_cells_player_occupies();var e=c?{col:a}:d.colliders_data[0].el.data,f=e.col,g=b||e.row;this.player_grid_data={col:f,row:g,size_y:this.player_grid_data.size_y,size_x:this.player_grid_data.size_x},this.cells_occupied_by_player=this.get_cells_occupied(this.player_grid_data);var h=this.get_widgets_overlapped(this.player_grid_data),i=this.widgets_constraints(h);this.manage_movements(i.can_go_up,f,g),this.manage_movements(i.can_not_go_up,f,g);if(!h.length){var j=this.can_go_player_up(this.player_grid_data);j!==!1&&(g=j),this.set_placeholder(f,g)}return{col:f,row:g}},g.widgets_constraints=function(b){var c=a([]),d,e=[],f=[];return b.each(a.proxy(function(b,d){var g=a(d),h=g.coords().grid;this.can_go_widget_up(h)?(c=c.add(g),e.push(h)):f.push(h)},this)),d=b.not(c),{can_go_up:this.sort_by_row_asc(e),can_not_go_up:this.sort_by_row_desc(f)}},g.sort_by_row_asc=function(b){return b=b.sort(function(b,c){return b.row||(b=a(b).coords().grid,c=a(c).coords().grid),b.row>c.row?1:-1}),b},g.sort_by_row_and_col_asc=function(a){return a=a.sort(function(a,b){return a.row>b.row||a.row===b.row&&a.col>b.col?1:-1}),a},g.sort_by_col_asc=function(a){return a=a.sort(function(a,b){return a.col>b.col?1:-1}),a},g.sort_by_row_desc=function(a){return a=a.sort(function(a,b){return a.row+a.size_y<b.row+b.size_y?1:-1}),a},g.manage_movements=function(b,c,d){return a.each(b,a.proxy(function(a,b){var e=b,f=e.el,g=this.can_go_widget_up(e);if(g)this.move_widget_to(f,g),this.set_placeholder(c,g+e.size_y);else{var h=this.can_go_player_up(this.player_grid_data);if(!h){var i=d+this.player_grid_data.size_y-e.row;this.move_widget_down(f,i),this.set_placeholder(c,d)}}},this)),this},g.is_player=function(a,b){if(b&&!this.gridmap[a])return!1;var c=b?this.gridmap[a][b]:a;return c&&(c.is(this.$player)||c.is(this.$helper))},g.is_player_in=function(b,c){var d=this.cells_occupied_by_player||{};return a.inArray(b,d.cols)>=0&&a.inArray(c,d.rows)>=0},g.is_placeholder_in=function(b,c){var d=this.cells_occupied_by_placeholder||{};return this.is_placeholder_in_col(b)&&a.inArray(c,d.rows)>=0},g.is_placeholder_in_col=function(b){var c=this.cells_occupied_by_placeholder||[];return a.inArray(b,c.cols)>=0},g.is_empty=function(a,b){return typeof this.gridmap[a]!="undefined"&&typeof this.gridmap[a][b]!="undefined"&&this.gridmap[a][b]===!1?!0:!1},g.is_occupied=function(a,b){return this.gridmap[a]?this.gridmap[a][b]?!0:!1:!1},g.is_widget=function(a,b){var c=this.gridmap[a];return c?(c=c[b],c?c:!1):!1},g.is_widget_under_player=function(a,b){return this.is_widget(a,b)?this.is_player_in(a,b):!1},g.get_widgets_under_player=function(b){b||(b=this.cells_occupied_by_player||{cols:[],rows:[]});var c=a([]);return a.each(b.cols,a.proxy(function(d,e){a.each(b.rows,a.proxy(function(a,b){this.is_widget(e,b)&&(c=c.add(this.gridmap[e][b]))},this))},this)),c},g.set_placeholder=function(b,c){var d=a.extend({},this.placeholder_grid_data),e=this.widgets_below({col:d.col,row:d.row,size_y:d.size_y,size_x:d.size_x}),f=b+d.size_x-1;f>this.cols&&(b=b-(f-b));var g=this.placeholder_grid_data.row<c,h=this.placeholder_grid_data.col!==b;this.placeholder_grid_data.col=b,this.placeholder_grid_data.row=c,this.cells_occupied_by_placeholder=this.get_cells_occupied(this.placeholder_grid_data),this.$preview_holder.attr({"data-row":c,"data-col":b}),(g||h)&&e.each(a.proxy(function(c,e){this.move_widget_up(a(e),this.placeholder_grid_data.col-b+d.size_y)},this));var i=this.get_widgets_under_player(this.cells_occupied_by_placeholder);i.length&&i.each(a.proxy(function(b,e){var f=a(e);this.move_widget_down(f,c+d.size_y-f.data("coords").grid.row)},this))},g.can_go_player_up=function(a){var b=a.row+a.size_y-1,c=!0,d=[],e=1e4,f=this.get_widgets_under_player();return this.for_each_column_occupied(a,function(a){var g=this.gridmap[a],h=b+1;d[a]=[];while(--h>0)if(this.is_empty(a,h)||this.is_player(a,h)||this.is_widget(a,h)&&g[h].is(f))d[a].push(h),e=h<e?h:e;else break;if(d[a].length===0)return c=!1,!0;d[a].sort()}),c?this.get_valid_rows(a,d,e):!1},g.can_go_widget_up=function(a){var b=a.row+a.size_y-1,c=!0,d=[],e=1e4;return this.for_each_column_occupied(a,function(f){var g=this.gridmap[f];d[f]=[];var h=b+1;while(--h>0){if(this.is_widget(f,h)&&!this.is_player_in(f,h)&&!g[h].is(a.el))break;!this.is_player(f,h)&&!this.is_placeholder_in(f,h)&&!this.is_player_in(f,h)&&d[f].push(h),h<e&&(e=h)}if(d[f].length===0)return c=!1,!0;d[f].sort()}),c?this.get_valid_rows(a,d,e):!1},g.get_valid_rows=function(b,c,d){var e=b.row,f=b.row+b.size_y-1,g=b.size_y,h=d-1,i=[];while(++h<=f){var j=!0;a.each(c,function(b,c){a.isArray(c)&&a.inArray(h,c)===-1&&(j=!1)});if(j===!0){i.push(h);if(i.length===g)break}}var k=!1;return g===1?i[0]!==e&&(k=i[0]||!1):i[0]!==e&&(k=this.get_consecutive_numbers_index(i,g)),k},g.get_consecutive_numbers_index=function(a,b){var c=a.length,d=[],e=!0,f=-1;for(var g=0;g<c;g++){if(e||a[g]===f+1){d.push(g);if(d.length===b)break;e=!1}else d=[],e=!0;f=a[g]}return d.length>=b?a[d[0]]:!1},g.get_widgets_overlapped=function(){var b,c=a([]),d=[],e=this.cells_occupied_by_player.rows.slice(0);return e.reverse(),a.each(this.cells_occupied_by_player.cols,a.proxy(function(b,f){a.each(e,a.proxy(function(b,e){if(!this.gridmap[f])return!0;var g=this.gridmap[f][e];this.is_occupied(f,e)&&!this.is_player(g)&&a.inArray(g,d)===-1&&(c=c.add(g),d.push(g))},this))},this)),c},g.on_start_overlapping_column=function(a){this.set_player(a,!1)},g.on_start_overlapping_row=function(a){this.set_player(!1,a)},g.on_stop_overlapping_column=function(a){this.set_player(a,!1);var b=this;this.for_each_widget_below(a,this.cells_occupied_by_player.rows[0],function(a,c){b.move_widget_up(this,b.player_grid_data.size_y)})},g.on_stop_overlapping_row=function(a){this.set_player(!1,a);var b=this,c=this.cells_occupied_by_player.cols;for(var d=0,e=c.length;d<e;d++)this.for_each_widget_below(c[d],a,function(a,c){b.move_widget_up(this,b.player_grid_data.size_y)})},g.move_widget_to=function(b,c){var d=this,e=b.coords().grid,f=c-e.row,g=this.widgets_below(b),h=this.can_move_to(e,e.col,c,b);return h===!1?!1:(this.remove_from_gridmap(e),e.row=c,this.add_to_gridmap(e),b.attr("data-row",c),this.$changed=this.$changed.add(b),g.each(function(b,c){var e=a(c),f=e.coords().grid,g=d.can_go_widget_up(f);g&&g!==f.row&&d.move_widget_to(e,g)}),this)},g.move_widget_up=function(b,c){var d=b.coords().grid,e=d.row,f=[],g=!0;c||(c=1);if(!this.can_go_up(b))return!1;this.for_each_column_occupied(d,function(d){if(a.inArray(b,f)===-1){var g=b.coords().grid,h=e-c;h=this.can_go_up_to_row(g,d,h);if(!h)return!0;var i=this.widgets_below(b);this.remove_from_gridmap(g),g.row=h,this.add_to_gridmap(g),b.attr("data-row",g.row),this.$changed=this.$changed.add(b),f.push(b),i.each(a.proxy(function(b,d){this.move_widget_up(a(d),c)},this))}})},g.move_widget_down=function(b,c){var d=b.coords().grid,e=d.row,f=[],g=c;if(!b)return!1;if(a.inArray(b,f)===-1){var h=b.coords().grid,i=e+c,j=this.widgets_below(b);this.remove_from_gridmap(h),j.each(a.proxy(function(b,c){var d=a(c),e=d.coords().grid,f=this.displacement_diff(e,h,g);f>0&&this.move_widget_down(d,f)},this)),h.row=i,this.update_widget_position(h,b),b.attr("data-row",h.row),this.$changed=this.$changed.add(b),f.push(b)}},g.can_go_up_to_row=function(b,c,d){var e=this.gridmap,f=!0,g=[],h=b.row,i;this.for_each_column_occupied(b,function(a){var b=e[a];g[a]=[],i=h;while(i--)if(this.is_empty(a,i)&&!this.is_placeholder_in(a,i))g[a].push(i);else break;if(!g[a].length)return f=!1,!0});if(!f)return!1;i=d;for(i=1;i<h;i++){var j=!0;for(var k=0,l=g.length;k<l;k++)g[k]&&a.inArray(i,g[k])===-1&&(j=!1);if(j===!0){f=i;break}}return f},g.displacement_diff=function(a,b,c){var d=a.row,e=[],f=b.row+b.size_y;this.for_each_column_occupied(a,function(a){var b=0;for(var c=f;c<d;c++)this.is_empty(a,c)&&(b=b+1);e.push(b)});var g=Math.max.apply(Math,e);return c=c-g,c>0?c:0},g.widgets_below=function(b){var c=a.isPlainObject(b)?b:b.coords().grid,d=this,e=this.gridmap,f=c.row+c.size_y-1,g=a([]);return this.for_each_column_occupied(c,function(b){d.for_each_widget_below(b,f,function(b,c){if(!d.is_player(this)&&a.inArray(this,g)===-1)return g=g.add(this),!0})}),this.sort_by_row_asc(g)},g.set_cells_player_occupies=function(a,b){return this.remove_from_gridmap(this.placeholder_grid_data),this.placeholder_grid_data.col=a,this.placeholder_grid_data.row=b,this.add_to_gridmap(this.placeholder_grid_data,this.$player),this},g.empty_cells_player_occupies=function(){return this.remove_from_gridmap(this.placeholder_grid_data),this},g.can_go_up=function(a){var b=a.coords().grid,c=b.row,d=c-1,e=this.gridmap,f=[],g=!0;return c===1?!1:(this.for_each_column_occupied(b,function(a){var b=this.is_widget(a,d);if(this.is_occupied(a,d)||this.is_player(a,d)||this.is_placeholder_in(a,d)||this.is_player_in(a,d))return g=!1,!0}),g)},g.can_move_to=function(a,b,c,d){var e=this.gridmap,f=a.el,g={size_y:a.size_y,size_x:a.size_x,col:b,row:c},h=!0,i=b+a.size_x-1;return i>this.cols?!1:d&&d<c+a.size_y-1?!1:(this.for_each_cell_occupied(g,function(b,c){var d=this.is_widget(b,c);d&&(!a.el||d.is(f))&&(h=!1)}),h)},g.get_targeted_columns=function(a){var b=(a||this.player_grid_data.col)+(this.player_grid_data.size_x-1),c=[];for(var d=a;d<=b;d++)c.push(d);return c},g.get_targeted_rows=function(a){var b=(a||this.player_grid_data.row)+(this.player_grid_data.size_y-1),c=[];for(var d=a;d<=b;d++)c.push(d);return c},g.get_cells_occupied=function(a){var b={cols:[],rows:[]},c;arguments[1]instanceof jQuery&&(a=arguments[1].coords().grid);for(c=0;c<a.size_x;c++){var d=a.col+c;b.cols.push(d)}for(c=0;c<a.size_y;c++){var e=a.row+c;b.rows.push(e)}return b},g.for_each_cell_occupied=function(a,b){return this.for_each_column_occupied(a,function(c){this.for_each_row_occupied(a,function(a){b.call(this,c,a)})}),this},g.for_each_column_occupied=function(a,b){for(var c=0;c<a.size_x;c++){var d=a.col+c;b.call(this,d,a)}},g.for_each_row_occupied=function(a,b){for(var c=0;c<a.size_y;c++){var d=a.row+c;b.call(this,d,a)}},g._traversing_widgets=function(b,c,d,e,f){var g=this.gridmap;if(!g[d])return;var h,i,j=b+"/"+c;if(arguments[2]instanceof jQuery){var k=arguments[2].coords().grid;d=k.col,e=k.row,f=arguments[3]}var l=[],m=e,n={"for_each/above":function(){while(m--)if(m>0&&this.is_widget(d,m)&&a.inArray(g[d][m],l)===-1){h=f.call(g[d][m],d,m),l.push(g[d][m]);if(h)break}},"for_each/below":function(){for(m=e+1,i=g[d].length;m<i;m++)if(this.is_widget(d,m)&&a.inArray(g[d][m],l)===-1){h=f.call(g[d][m],d,m),l.push(g[d][m]);if(h)break}}};n[j]&&n[j].call(this)},g.for_each_widget_above=function(a,b,c){return this._traversing_widgets("for_each","above",a,b,c),this},g.for_each_widget_below=function(a,b,c){return this._traversing_widgets("for_each","below",a,b,c),this},g.get_highest_occupied_cell=function(){var a,b=this.gridmap,c=[],d=[];for(var e=b.length-1;e>=1;e--)for(a=b[e].length-1;a>=1;a--)if(this.is_widget(e,a)){c.push(a),d[a]=e;break}var f=Math.max.apply(Math,c);return this.highest_occupied_cell={col:d[f],row:f},this.highest_occupied_cell},g.get_widgets_from=function(b,c){var d=this.gridmap,e=a();return b&&(e=e.add(this.$widgets.filter(function(){var c=a(this).attr("data-col");return c===b||c>b}))),c&&(e=e.add(this.$widgets.filter(function(){var b=a(this).attr("data-row");return b===c||b>c}))),e},g.set_dom_grid_height=function(){var a=this.get_highest_occupied_cell().row;return this.$el.css("height",a*this.min_widget_height),this},g.generate_stylesheet=function(b){var c="",d=this.options.max_size_x,e=0,g=0,h,i;b||(b={}),b.cols||(b.cols=this.cols),b.rows||(b.rows=this.rows),b.namespace||(b.namespace=this.options.namespace),b.widget_base_dimensions||(b.widget_base_dimensions=this.options.widget_base_dimensions),b.widget_margins||(b.widget_margins=this.options.widget_margins),b.min_widget_width=b.widget_margins[0]*2+b.widget_base_dimensions[0],b.min_widget_height=b.widget_margins[1]*2+b.widget_base_dimensions[1];var j=a.param(b);if(a.inArray(j,f.generated_stylesheets)>=0)return!1;f.generated_stylesheets.push(j);for(h=b.cols;h>=0;h--)c+=b.namespace+' [data-col="'+(h+1)+'"] { left:'+(h*b.widget_base_dimensions[0]+h*b.widget_margins[0]+(h+1)*b.widget_margins[0])+"px;} ";for(h=b.rows;h>=0;h--)c+=b.namespace+' [data-row="'+(h+1)+'"] { top:'+(h*b.widget_base_dimensions[1]+h*b.widget_margins[1]+(h+1)*b.widget_margins[1])+"px;} ";for(var k=1;k<=b.rows;k++)c+=b.namespace+' [data-sizey="'+k+'"] { height:'+(k*b.widget_base_dimensions[1]+(k-1)*b.widget_margins[1]*2)+"px;}";for(var l=1;l<=d;l++)c+=b.namespace+' [data-sizex="'+l+'"] { width:'+(l*b.widget_base_dimensions[0]+(l-1)*b.widget_margins[0]*2)+"px;}";return this.add_style_tag(c)},g.add_style_tag=function(a){var b=c,d=b.createElement("style");return b.getElementsByTagName("head")[0].appendChild(d),d.setAttribute("type","text/css"),d.styleSheet?d.styleSheet.cssText=a:d.appendChild(c.createTextNode(a)),this},g.generate_faux_grid=function(a,b){this.faux_grid=[],this.gridmap=[];var c,d;for(c=b;c>0;c--){this.gridmap[c]=[];for(d=a;d>0;d--)this.add_faux_cell(d,c)}return this},g.add_faux_cell=function(b,c){var d=a({left:this.baseX+(c-1)*this.min_widget_width,top:this.baseY+(b-1)*this.min_widget_height,width:this.min_widget_width,height:this.min_widget_height,col:c,row:b,original_col:c,original_row:b}).coords();return a.isArray(this.gridmap[c])||(this.gridmap[c]=[]),this.gridmap[c][b]=!1,this.faux_grid.push(d),this},g.add_faux_rows=function(a){var b=this.rows,c=b+(a||1);for(var d=c;d>b;d--)for(var e=this.cols;e>=1;e--)this.add_faux_cell(d,e);return this.rows=c,this.options.autogenerate_stylesheet&&this.generate_stylesheet(),this},g.add_faux_cols=function(a){var b=this.cols,c=b+(a||1);for(var d=b;d<c;d++)for(var e=this.rows;e>=1;e--)this.add_faux_cell(e,d);return this.cols=c,this.options.autogenerate_stylesheet&&this.generate_stylesheet(),this},g.recalculate_faux_grid=function(){var c=this.$wrapper.width();return this.baseX=(a(b).width()-c)/2,this.baseY=this.$wrapper.offset().top,a.each(this.faux_grid,a.proxy(function(a,b){this.faux_grid[a]=b.update({left:this.baseX+(b.data.col-1)*this.min_widget_width,top:this.baseY+(b.data.row-1)*this.min_widget_height})},this)),this},g.get_widgets_from_DOM=function(){return this.$widgets.each(a.proxy(function(b,c){this.register_widget(a(c))},this)),this},g.generate_grid_and_stylesheet=function(){var c=this.$wrapper.width(),d=this.$wrapper.height(),e=Math.floor(c/this.min_widget_width)+this.options.extra_cols,f=this.$widgets.map(function(){return a(this).attr("data-col")});f=Array.prototype.slice.call(f,0),f.length||(f=[0]);var g=Math.max.apply(Math,f),h=this.options.extra_rows;return this.$widgets.each(function(b,c){h+=+a(c).attr("data-sizey")}),this.cols=Math.max(g,e,this.options.min_cols),this.rows=Math.max(h,this.options.min_rows),this.baseX=(a(b).width()-c)/2,this.baseY=this.$wrapper.offset().top,this.options.autogenerate_stylesheet&&this.generate_stylesheet(),this.generate_faux_grid(this.rows,this.cols)},a.fn.gridster=function(b){return this.each(function(){a(this).data("gridster")||a(this).data("gridster",new f(this,b))})},a.Gridster=g}(jQuery,window,document);
New to GrepCode? Check out our FAQ X