Added swaping queue, better handling of large to small swaps

This commit is contained in:
Dustin Moore
2012-11-26 15:20:15 -08:00
parent 773828731c
commit 98204f1c69
7 changed files with 200 additions and 26 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-20
/*! gridster.js - v0.1.0 - 2012-11-26
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
+65 -7
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-20
/*! gridster.js - v0.1.0 - 2012-11-26
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
@@ -1310,7 +1310,6 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
@@ -1630,20 +1629,65 @@
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
var wgd = $w.coords().grid;
var outside_col = placeholder_cells.cols[0]+player_size_x-1;
var outside_row = placeholder_cells.rows[0]+player_size_y-1;
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
if(!$gr.is_widget(placeholder_cells.cols[0],placeholder_cells.rows[0])){
$gr.new_move_widget_to($w, placeholder_cells.cols[0], placeholder_cells.rows[0]);
$gr.set_placeholder(to_col, to_row);
if(!$gr.is_swap_occupied(placeholder_cells.cols[0], wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0], wgd.row)){
var key = placeholder_cells.cols[0]+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row)){
var key = outside_col+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0])){
var key = wgd.col+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row)){
var key = wgd.col+"_"+outside_row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(placeholder_cells.cols[0],placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0],placeholder_cells.rows[0])){
var key = placeholder_cells.cols[0]+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
}
}));
//Move queued widgets
if(swap){
for(var key in w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
}
this.set_placeholder(to_col, to_row);
}
//If set to false smaller widgets will not displace larger widgets.
if(this.options.shift_larger_widgets_down && !swap){
var constraints = this.widgets_constraints($overlapped_widgets);
@@ -1670,6 +1714,21 @@
};
fn.is_swap_occupied = function(col, row, w_size_x, w_size_y) {
var occupied = false;
for (var c = 0; c < w_size_x; c++){
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
if(this.is_occupied(colc,rowc)){
occupied = true;
}
}
}
return occupied;
}
/**
* See which of the widgets in the $widgets param collection can go to
* a upper row and which not.
@@ -2303,7 +2362,6 @@
*/
fn.on_stop_overlapping_column = function(col) {
//this.set_player(col, false);
var self = this;
this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
@@ -2321,7 +2379,6 @@
*/
fn.on_stop_overlapping_row = function(row) {
//this.set_player(false, row);
var self = this;
var cols = this.cells_occupied_by_player.cols;
for (var c = 0, cl = cols.length; c < cl; c++) {
@@ -2343,6 +2400,7 @@
this.add_to_gridmap(widget_grid_data);
$widget.attr('data-row', row);
$widget.attr('data-col', col);
this.update_widget_position(widget_grid_data, $widget);
this.$changed = this.$changed.add($widget);
return this;
+1 -1
View File
@@ -1,3 +1,3 @@
/*! gridster.js - v0.1.0 - 2012-11-20
/*! gridster.js - v0.1.0 - 2012-11-26
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */.gridster{position:relative}.gridster>*{margin:0 auto;-webkit-transition:height .4s;-moz-transition:height .4s;-o-transition:height .4s;-ms-transition:height .4s;transition:height .4s}.gridster .gs_w{z-index:2;position:absolute}.ready .gs_w:not(.preview-holder){-webkit-transition:opacity .3s,left .3s,top .3s;-moz-transition:opacity .3s,left .3s,top .3s;-o-transition:opacity .3s,left .3s,top .3s;transition:opacity .3s,left .3s,top .3s}.ready .gs_w:not(.preview-holder){-webkit-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-moz-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;-o-transition:opacity .3s,left .3s,top .3s,width .3s,height .3s;transition:opacity .3s,left .3s,top .3s,width .3s,height .3s}.gridster .preview-holder{z-index:1;position:absolute;background-color:#fff;border-color:#fff;opacity:.3}.gridster .player-revert{z-index:10!important;-webkit-transition:left .3s,top .3s!important;-moz-transition:left .3s,top .3s!important;-o-transition:left .3s,top .3s!important;transition:left .3s,top .3s!important}.gridster .dragging{z-index:10!important;-webkit-transition:all 0s!important;-moz-transition:all 0s!important;-o-transition:all 0s!important;transition:all 0s!important}
+2 -2
View File
File diff suppressed because one or more lines are too long
+65 -7
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-20
/*! gridster.js - v0.1.0 - 2012-11-26
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
@@ -1310,7 +1310,6 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
@@ -1630,20 +1629,65 @@
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
var wgd = $w.coords().grid;
var outside_col = placeholder_cells.cols[0]+player_size_x-1;
var outside_row = placeholder_cells.rows[0]+player_size_y-1;
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
if(!$gr.is_widget(placeholder_cells.cols[0],placeholder_cells.rows[0])){
$gr.new_move_widget_to($w, placeholder_cells.cols[0], placeholder_cells.rows[0]);
$gr.set_placeholder(to_col, to_row);
if(!$gr.is_swap_occupied(placeholder_cells.cols[0], wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0], wgd.row)){
var key = placeholder_cells.cols[0]+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row)){
var key = outside_col+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0])){
var key = wgd.col+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row)){
var key = wgd.col+"_"+outside_row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(placeholder_cells.cols[0],placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0],placeholder_cells.rows[0])){
var key = placeholder_cells.cols[0]+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
}
}));
//Move queued widgets
if(swap){
for(var key in w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
}
this.set_placeholder(to_col, to_row);
}
//If set to false smaller widgets will not displace larger widgets.
if(this.options.shift_larger_widgets_down && !swap){
var constraints = this.widgets_constraints($overlapped_widgets);
@@ -1670,6 +1714,21 @@
};
fn.is_swap_occupied = function(col, row, w_size_x, w_size_y) {
var occupied = false;
for (var c = 0; c < w_size_x; c++){
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
if(this.is_occupied(colc,rowc)){
occupied = true;
}
}
}
return occupied;
}
/**
* See which of the widgets in the $widgets param collection can go to
* a upper row and which not.
@@ -2303,7 +2362,6 @@
*/
fn.on_stop_overlapping_column = function(col) {
//this.set_player(col, false);
var self = this;
this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
@@ -2321,7 +2379,6 @@
*/
fn.on_stop_overlapping_row = function(row) {
//this.set_player(false, row);
var self = this;
var cols = this.cells_occupied_by_player.cols;
for (var c = 0, cl = cols.length; c < cl; c++) {
@@ -2343,6 +2400,7 @@
this.add_to_gridmap(widget_grid_data);
$widget.attr('data-row', row);
$widget.attr('data-col', col);
this.update_widget_position(widget_grid_data, $widget);
this.$changed = this.$changed.add($widget);
return this;
File diff suppressed because one or more lines are too long
+64 -6
View File
@@ -614,7 +614,6 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
@@ -934,20 +933,65 @@
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
var wgd = $w.coords().grid;
var outside_col = placeholder_cells.cols[0]+player_size_x-1;
var outside_row = placeholder_cells.rows[0]+player_size_y-1;
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
if(!$gr.is_widget(placeholder_cells.cols[0],placeholder_cells.rows[0])){
$gr.new_move_widget_to($w, placeholder_cells.cols[0], placeholder_cells.rows[0]);
$gr.set_placeholder(to_col, to_row);
if(!$gr.is_swap_occupied(placeholder_cells.cols[0], wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0], wgd.row)){
var key = placeholder_cells.cols[0]+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row)){
var key = outside_col+"_"+wgd.row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0])){
var key = wgd.col+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row)){
var key = wgd.col+"_"+outside_row;
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
else if(!$gr.is_swap_occupied(placeholder_cells.cols[0],placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(placeholder_cells.cols[0],placeholder_cells.rows[0])){
var key = placeholder_cells.cols[0]+"_"+placeholder_cells.rows[0];
if (!(key in w_queue)){
w_queue[key] = $w;
}
swap = true;
}
}
}));
//Move queued widgets
if(swap){
for(var key in w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
}
this.set_placeholder(to_col, to_row);
}
//If set to false smaller widgets will not displace larger widgets.
if(this.options.shift_larger_widgets_down && !swap){
var constraints = this.widgets_constraints($overlapped_widgets);
@@ -974,6 +1018,21 @@
};
fn.is_swap_occupied = function(col, row, w_size_x, w_size_y) {
var occupied = false;
for (var c = 0; c < w_size_x; c++){
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
if(this.is_occupied(colc,rowc)){
occupied = true;
}
}
}
return occupied;
}
/**
* See which of the widgets in the $widgets param collection can go to
* a upper row and which not.
@@ -1607,7 +1666,6 @@
*/
fn.on_stop_overlapping_column = function(col) {
//this.set_player(col, false);
var self = this;
this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
@@ -1625,7 +1683,6 @@
*/
fn.on_stop_overlapping_row = function(row) {
//this.set_player(false, row);
var self = this;
var cols = this.cells_occupied_by_player.cols;
for (var c = 0, cl = cols.length; c < cl; c++) {
@@ -1647,6 +1704,7 @@
this.add_to_gridmap(widget_grid_data);
$widget.attr('data-row', row);
$widget.attr('data-col', col);
this.update_widget_position(widget_grid_data, $widget);
this.$changed = this.$changed.add($widget);
return this;