egroupware_official/phpgwapi/js/jquery/gridster/jquery.gridster.min.js
2013-05-29 19:23:49 +00:00

2 lines
37 KiB
JavaScript

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