Swap queue awesomeness

This commit is contained in:
Dustin Moore
2012-11-27 14:10:11 -08:00
parent 136923ec50
commit 24cd3be70d
7 changed files with 539 additions and 164 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-26
/*! gridster.js - v0.1.0 - 2012-11-27
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
+178 -53
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-26
/*! gridster.js - v0.1.0 - 2012-11-27
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
@@ -786,6 +786,7 @@
this.$widgets = this.$el.children(this.options.widget_selector).addClass('gs_w');
this.widgets = [];
this.$changed = $([]);
this.w_queue = {};
this.wrapper_width = this.$wrapper.width();
this.min_widget_width = (this.options.widget_margins[0] * 2) +
this.options.widget_base_dimensions[0];
@@ -1074,9 +1075,12 @@
size_y: size_y
});
/*
$nexts.not(exclude).each($.proxy(function(i, widget) {
console.log("from_remove")
this.move_widget_up( $(widget), size_y );
}, this));
*/
this.set_dom_grid_height();
@@ -1159,6 +1163,7 @@
if (!silent) {
$nexts.each($.proxy(function(i, widget) {
console.log("from_remove_widget");
this.move_widget_up( $(widget), wgd.size_y );
}, this));
}
@@ -1312,12 +1317,13 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
/*if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
console.log("from_add_to_gridmap");
this.move_widget_up( $(widget));
}, this));
}
} */
};
@@ -1509,6 +1515,7 @@
this.player_grid_data = {};
this.cells_occupied_by_placeholder = {};
this.cells_occupied_by_player = {};
this.w_queue = {};
this.set_dom_grid_height();
};
@@ -1607,10 +1614,8 @@
this.empty_cells_player_occupies();
}
var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
console.log("cell: ");
console.log(cell);
var to_col = cell.col;
var to_row = cell.row;
var to_row = cell.row || row;
this.player_grid_data = {
col: to_col,
@@ -1625,7 +1630,6 @@
//Added placeholder for more advanced movement.
this.cells_occupied_by_placeholder = this.get_cells_occupied(
this.placeholder_grid_data);
console.log(this.player_grid_data);
var $overlapped_widgets = this.get_widgets_overlapped(
this.player_grid_data);
@@ -1633,75 +1637,85 @@
var player_size_y = this.player_grid_data.size_y;
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
var $gr = this;
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
console.log($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 ($w.hasClass($gr.options.static_class)){
//next iteration
return true;
}
console.log($w);
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
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;
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) && !$gr.is_in_queue(placeholder_cells.cols[0], wgd.row, $w)){
console.log("1st if");
swap = $gr.queue_widget(placeholder_cells.cols[0], wgd.row, $w);
}
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(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row) && !$gr.is_in_queue(outside_col, wgd.row, $w)){
console.log("2nd if");
swap = $gr.queue_widget(outside_col, wgd.row, $w);
}
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, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0]) && !$gr.is_in_queue(wgd.col, placeholder_cells.rows[0], $w)){
console.log("3rd if");
swap = $gr.queue_widget(wgd.col, placeholder_cells.rows[0], $w);
}
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(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row) && !$gr.is_in_queue(wgd.col, outside_row, $w)){
console.log("4th if");
swap = $gr.queue_widget(wgd.col, outside_row, $w);
}
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;
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]) && !$gr.is_in_queue(placeholder_cells.cols[0],placeholder_cells.rows[0], $w)){
console.log("5th if");
swap = $gr.queue_widget(placeholder_cells.cols[0], placeholder_cells.rows[0], $w);
} else {
console.log("Contingency else");
//in one last attempt we check for any other empty spaces
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = placeholder_cells.cols[0]+c;
var rowc = placeholder_cells.rows[0]+r;
if (!$gr.is_swap_occupied(colc,rowc, wgd.size_x, wgd.size_y) && !$gr.is_player_in(colc,rowc) && !$gr.is_in_queue(colc, rowc, $w)){
swap = $gr.queue_widget(colc, rowc, $w);
c = player_size_x;
break;
}
}
}
}
swap = true;
}
}
}));
console.log("queued items:");
for(var key in this.w_queue){
console.log("key " +key);
console.log(this.w_queue[key]);
}
//Move queued widgets
if(swap){
for(var key in w_queue){
if(swap && this.can_placeholder_be_set(to_col, to_row, player_size_x, player_size_y)){
for(var key in this.w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
if (this.w_queue[key] != "full"){
this.new_move_widget_to(this.w_queue[key], col, row);
}
}
this.set_placeholder(to_col, to_row);
}
/*If set to false smaller widgets will not displace larger widgets.
///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);
this.manage_movements(constraints.can_go_up, to_col, to_row);
this.manage_movements(constraints.can_not_go_up, to_col, to_row);
}
*/
/* if there is not widgets overlapping in the new player position,
@@ -1714,6 +1728,8 @@
this.set_placeholder(to_col, to_row);
}
this.w_queue = {};
return {
col: to_col,
row: to_row
@@ -1727,14 +1743,117 @@
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if(this.is_occupied(colc,rowc)){
occupied = true;
} else if(key in this.w_queue){
if(this.w_queue[key] == "full"){
occupied = true;
continue;
}
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
//remove queued items if no longer under player.
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key];
}
}
}
}
return occupied;
}
fn.can_placeholder_be_set = function(col, row, player_size_x, player_size_y){
var can_set = true;
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
var $tw = this.is_widget(colc, rowc);
//if this space is occupied and not queued for move.
console.log("place check");
if(this.is_occupied(colc,rowc) && !this.is_widget_queued($tw)){
can_set = false;
}
}
}
console.log("can_set: " + can_set);
return can_set;
}
fn.queue_widget = function(col, row, $widget){
var $w = $widget
var wgd = $w.coords().grid;
var primary_key = col+"_"+row;
if (primary_key in this.w_queue){
return false;
}
this.w_queue[primary_key] = $w;
for (var c = 0; c < wgd.size_x; c++){
for (var r = 0; r < wgd.size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if (key == primary_key){
continue;
}
console.log("queue loop")
console.log(key);
this.w_queue[key] = "full";
}
}
return true;
}
fn.is_widget_queued = function($widget){
var queued = false;
if ($widget === false){
return false;
}
for(var key in this.w_queue){
if(this.w_queue[key] == "full"){
continue;
}
if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")){
queued = true;
}
}
return queued
}
fn.is_in_queue = function(col,row, $widget){
var queued = false;
var key = col+"_"+row;
if ((key in this.w_queue)){
console.log("key is in queue");
if (this.w_queue[key] == "full"){
queued = true;
} else {
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key]
queued = false;
} else if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")) {
delete this.w_queue[key]
queued = false;
} else {
queued = true;
}
}
}
console.log("queued: " + queued);
return queued;
}
/**
* See which of the widgets in the $widgets param collection can go to
@@ -2130,10 +2249,11 @@
});
if (moved_down || changed_column) {
$nexts.each($.proxy(function(i, widget) {
/* $nexts.each($.proxy(function(i, widget) {
console.log("set_placeholder");
this.move_widget_up(
$(widget), this.placeholder_grid_data.col - col + phgd.size_y);
}, this));
}, this)); */
}
@@ -2141,6 +2261,7 @@
if ($widgets_under_ph.length) {
$widgets_under_ph.each($.proxy(function(i, widget) {
var $w = $(widget);
console.log("moving down from set_placeholder");
this.move_widget_down(
$w, row + phgd.size_y - $w.data('coords').grid.row);
}, this));
@@ -2396,10 +2517,11 @@
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],
/*this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
console.log("from_on_stop_overlapping_column");
self.move_widget_up(this, self.player_grid_data.size_y);
});
});*/
};
@@ -2414,11 +2536,12 @@
//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++) {
/*for (var c = 0, cl = cols.length; c < cl; c++) {
this.for_each_widget_below(cols[c], row, function(tcol, trow) {
console.log("from_on_stop_overlapping_row");
self.move_widget_up(this, self.player_grid_data.size_y);
});
}
}*/
};
//Not yet part of api - DM.
@@ -2492,6 +2615,7 @@
* @return {Class} Returns the instance of the Gridster Class.
*/
fn.move_widget_up = function($widget, y_units) {
console.log("move_widget_up");
var el_grid_data = $widget.coords().grid;
var actual_row = el_grid_data.row;
var moved = [];
@@ -2522,9 +2646,10 @@
moved.push($widget);
$next_widgets.each($.proxy(function(i, widget) {
/* $next_widgets.each($.proxy(function(i, widget) {
console.log("from_within_move_widget_up");
this.move_widget_up($(widget), y_units);
}, this));
}, this)); */
}
});
+1 -1
View File
@@ -1,3 +1,3 @@
/*! gridster.js - v0.1.0 - 2012-11-26
/*! gridster.js - v0.1.0 - 2012-11-27
* 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
+178 -53
View File
@@ -1,4 +1,4 @@
/*! gridster.js - v0.1.0 - 2012-11-26
/*! gridster.js - v0.1.0 - 2012-11-27
* http://gridster.net/
* Copyright (c) 2012 ducksboard; Licensed MIT */
@@ -786,6 +786,7 @@
this.$widgets = this.$el.children(this.options.widget_selector).addClass('gs_w');
this.widgets = [];
this.$changed = $([]);
this.w_queue = {};
this.wrapper_width = this.$wrapper.width();
this.min_widget_width = (this.options.widget_margins[0] * 2) +
this.options.widget_base_dimensions[0];
@@ -1074,9 +1075,12 @@
size_y: size_y
});
/*
$nexts.not(exclude).each($.proxy(function(i, widget) {
console.log("from_remove")
this.move_widget_up( $(widget), size_y );
}, this));
*/
this.set_dom_grid_height();
@@ -1159,6 +1163,7 @@
if (!silent) {
$nexts.each($.proxy(function(i, widget) {
console.log("from_remove_widget");
this.move_widget_up( $(widget), wgd.size_y );
}, this));
}
@@ -1312,12 +1317,13 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
/*if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
console.log("from_add_to_gridmap");
this.move_widget_up( $(widget));
}, this));
}
} */
};
@@ -1509,6 +1515,7 @@
this.player_grid_data = {};
this.cells_occupied_by_placeholder = {};
this.cells_occupied_by_player = {};
this.w_queue = {};
this.set_dom_grid_height();
};
@@ -1607,10 +1614,8 @@
this.empty_cells_player_occupies();
}
var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
console.log("cell: ");
console.log(cell);
var to_col = cell.col;
var to_row = cell.row;
var to_row = cell.row || row;
this.player_grid_data = {
col: to_col,
@@ -1625,7 +1630,6 @@
//Added placeholder for more advanced movement.
this.cells_occupied_by_placeholder = this.get_cells_occupied(
this.placeholder_grid_data);
console.log(this.player_grid_data);
var $overlapped_widgets = this.get_widgets_overlapped(
this.player_grid_data);
@@ -1633,75 +1637,85 @@
var player_size_y = this.player_grid_data.size_y;
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
var $gr = this;
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
console.log($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 ($w.hasClass($gr.options.static_class)){
//next iteration
return true;
}
console.log($w);
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
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;
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) && !$gr.is_in_queue(placeholder_cells.cols[0], wgd.row, $w)){
console.log("1st if");
swap = $gr.queue_widget(placeholder_cells.cols[0], wgd.row, $w);
}
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(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row) && !$gr.is_in_queue(outside_col, wgd.row, $w)){
console.log("2nd if");
swap = $gr.queue_widget(outside_col, wgd.row, $w);
}
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, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0]) && !$gr.is_in_queue(wgd.col, placeholder_cells.rows[0], $w)){
console.log("3rd if");
swap = $gr.queue_widget(wgd.col, placeholder_cells.rows[0], $w);
}
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(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row) && !$gr.is_in_queue(wgd.col, outside_row, $w)){
console.log("4th if");
swap = $gr.queue_widget(wgd.col, outside_row, $w);
}
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;
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]) && !$gr.is_in_queue(placeholder_cells.cols[0],placeholder_cells.rows[0], $w)){
console.log("5th if");
swap = $gr.queue_widget(placeholder_cells.cols[0], placeholder_cells.rows[0], $w);
} else {
console.log("Contingency else");
//in one last attempt we check for any other empty spaces
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = placeholder_cells.cols[0]+c;
var rowc = placeholder_cells.rows[0]+r;
if (!$gr.is_swap_occupied(colc,rowc, wgd.size_x, wgd.size_y) && !$gr.is_player_in(colc,rowc) && !$gr.is_in_queue(colc, rowc, $w)){
swap = $gr.queue_widget(colc, rowc, $w);
c = player_size_x;
break;
}
}
}
}
swap = true;
}
}
}));
console.log("queued items:");
for(var key in this.w_queue){
console.log("key " +key);
console.log(this.w_queue[key]);
}
//Move queued widgets
if(swap){
for(var key in w_queue){
if(swap && this.can_placeholder_be_set(to_col, to_row, player_size_x, player_size_y)){
for(var key in this.w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
if (this.w_queue[key] != "full"){
this.new_move_widget_to(this.w_queue[key], col, row);
}
}
this.set_placeholder(to_col, to_row);
}
/*If set to false smaller widgets will not displace larger widgets.
///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);
this.manage_movements(constraints.can_go_up, to_col, to_row);
this.manage_movements(constraints.can_not_go_up, to_col, to_row);
}
*/
/* if there is not widgets overlapping in the new player position,
@@ -1714,6 +1728,8 @@
this.set_placeholder(to_col, to_row);
}
this.w_queue = {};
return {
col: to_col,
row: to_row
@@ -1727,14 +1743,117 @@
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if(this.is_occupied(colc,rowc)){
occupied = true;
} else if(key in this.w_queue){
if(this.w_queue[key] == "full"){
occupied = true;
continue;
}
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
//remove queued items if no longer under player.
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key];
}
}
}
}
return occupied;
}
fn.can_placeholder_be_set = function(col, row, player_size_x, player_size_y){
var can_set = true;
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
var $tw = this.is_widget(colc, rowc);
//if this space is occupied and not queued for move.
console.log("place check");
if(this.is_occupied(colc,rowc) && !this.is_widget_queued($tw)){
can_set = false;
}
}
}
console.log("can_set: " + can_set);
return can_set;
}
fn.queue_widget = function(col, row, $widget){
var $w = $widget
var wgd = $w.coords().grid;
var primary_key = col+"_"+row;
if (primary_key in this.w_queue){
return false;
}
this.w_queue[primary_key] = $w;
for (var c = 0; c < wgd.size_x; c++){
for (var r = 0; r < wgd.size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if (key == primary_key){
continue;
}
console.log("queue loop")
console.log(key);
this.w_queue[key] = "full";
}
}
return true;
}
fn.is_widget_queued = function($widget){
var queued = false;
if ($widget === false){
return false;
}
for(var key in this.w_queue){
if(this.w_queue[key] == "full"){
continue;
}
if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")){
queued = true;
}
}
return queued
}
fn.is_in_queue = function(col,row, $widget){
var queued = false;
var key = col+"_"+row;
if ((key in this.w_queue)){
console.log("key is in queue");
if (this.w_queue[key] == "full"){
queued = true;
} else {
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key]
queued = false;
} else if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")) {
delete this.w_queue[key]
queued = false;
} else {
queued = true;
}
}
}
console.log("queued: " + queued);
return queued;
}
/**
* See which of the widgets in the $widgets param collection can go to
@@ -2130,10 +2249,11 @@
});
if (moved_down || changed_column) {
$nexts.each($.proxy(function(i, widget) {
/* $nexts.each($.proxy(function(i, widget) {
console.log("set_placeholder");
this.move_widget_up(
$(widget), this.placeholder_grid_data.col - col + phgd.size_y);
}, this));
}, this)); */
}
@@ -2141,6 +2261,7 @@
if ($widgets_under_ph.length) {
$widgets_under_ph.each($.proxy(function(i, widget) {
var $w = $(widget);
console.log("moving down from set_placeholder");
this.move_widget_down(
$w, row + phgd.size_y - $w.data('coords').grid.row);
}, this));
@@ -2396,10 +2517,11 @@
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],
/*this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
console.log("from_on_stop_overlapping_column");
self.move_widget_up(this, self.player_grid_data.size_y);
});
});*/
};
@@ -2414,11 +2536,12 @@
//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++) {
/*for (var c = 0, cl = cols.length; c < cl; c++) {
this.for_each_widget_below(cols[c], row, function(tcol, trow) {
console.log("from_on_stop_overlapping_row");
self.move_widget_up(this, self.player_grid_data.size_y);
});
}
}*/
};
//Not yet part of api - DM.
@@ -2492,6 +2615,7 @@
* @return {Class} Returns the instance of the Gridster Class.
*/
fn.move_widget_up = function($widget, y_units) {
console.log("move_widget_up");
var el_grid_data = $widget.coords().grid;
var actual_row = el_grid_data.row;
var moved = [];
@@ -2522,9 +2646,10 @@
moved.push($widget);
$next_widgets.each($.proxy(function(i, widget) {
/* $next_widgets.each($.proxy(function(i, widget) {
console.log("from_within_move_widget_up");
this.move_widget_up($(widget), y_units);
}, this));
}, this)); */
}
});
File diff suppressed because one or more lines are too long
+177 -52
View File
@@ -90,6 +90,7 @@
this.$widgets = this.$el.children(this.options.widget_selector).addClass('gs_w');
this.widgets = [];
this.$changed = $([]);
this.w_queue = {};
this.wrapper_width = this.$wrapper.width();
this.min_widget_width = (this.options.widget_margins[0] * 2) +
this.options.widget_base_dimensions[0];
@@ -378,9 +379,12 @@
size_y: size_y
});
/*
$nexts.not(exclude).each($.proxy(function(i, widget) {
console.log("from_remove")
this.move_widget_up( $(widget), size_y );
}, this));
*/
this.set_dom_grid_height();
@@ -463,6 +467,7 @@
if (!silent) {
$nexts.each($.proxy(function(i, widget) {
console.log("from_remove_widget");
this.move_widget_up( $(widget), wgd.size_y );
}, this));
}
@@ -616,12 +621,13 @@
*/
fn.add_to_gridmap = function(grid_data, value) {
this.update_widget_position(grid_data, value || grid_data.el);
if (grid_data.el) {
/*if (grid_data.el) {
var $widgets = this.widgets_below(grid_data.el);
$widgets.each($.proxy(function(i, widget) {
console.log("from_add_to_gridmap");
this.move_widget_up( $(widget));
}, this));
}
} */
};
@@ -813,6 +819,7 @@
this.player_grid_data = {};
this.cells_occupied_by_placeholder = {};
this.cells_occupied_by_player = {};
this.w_queue = {};
this.set_dom_grid_height();
};
@@ -911,10 +918,8 @@
this.empty_cells_player_occupies();
}
var cell = !no_player ? self.colliders_data[0].el.data : {col: col};
console.log("cell: ");
console.log(cell);
var to_col = cell.col;
var to_row = cell.row;
var to_row = cell.row || row;
this.player_grid_data = {
col: to_col,
@@ -929,7 +934,6 @@
//Added placeholder for more advanced movement.
this.cells_occupied_by_placeholder = this.get_cells_occupied(
this.placeholder_grid_data);
console.log(this.player_grid_data);
var $overlapped_widgets = this.get_widgets_overlapped(
this.player_grid_data);
@@ -937,75 +941,85 @@
var player_size_y = this.player_grid_data.size_y;
var player_size_x = this.player_grid_data.size_x;
var placeholder_cells = this.cells_occupied_by_placeholder;
var $gr = this;
var w_queue = {};
var $gr = this;
//Queue Swaps
$overlapped_widgets.each($.proxy(function(i, w){
var $w = $(w);
console.log($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 ($w.hasClass($gr.options.static_class)){
//next iteration
return true;
}
console.log($w);
if(wgd.size_x <= player_size_x && wgd.size_y <= player_size_y){
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;
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) && !$gr.is_in_queue(placeholder_cells.cols[0], wgd.row, $w)){
console.log("1st if");
swap = $gr.queue_widget(placeholder_cells.cols[0], wgd.row, $w);
}
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(outside_col, wgd.row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(outside_col, wgd.row) && !$gr.is_in_queue(outside_col, wgd.row, $w)){
console.log("2nd if");
swap = $gr.queue_widget(outside_col, wgd.row, $w);
}
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, placeholder_cells.rows[0], wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, placeholder_cells.rows[0]) && !$gr.is_in_queue(wgd.col, placeholder_cells.rows[0], $w)){
console.log("3rd if");
swap = $gr.queue_widget(wgd.col, placeholder_cells.rows[0], $w);
}
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(wgd.col, outside_row, wgd.size_x, wgd.size_y) && !$gr.is_player_in(wgd.col, outside_row) && !$gr.is_in_queue(wgd.col, outside_row, $w)){
console.log("4th if");
swap = $gr.queue_widget(wgd.col, outside_row, $w);
}
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;
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]) && !$gr.is_in_queue(placeholder_cells.cols[0],placeholder_cells.rows[0], $w)){
console.log("5th if");
swap = $gr.queue_widget(placeholder_cells.cols[0], placeholder_cells.rows[0], $w);
} else {
console.log("Contingency else");
//in one last attempt we check for any other empty spaces
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = placeholder_cells.cols[0]+c;
var rowc = placeholder_cells.rows[0]+r;
if (!$gr.is_swap_occupied(colc,rowc, wgd.size_x, wgd.size_y) && !$gr.is_player_in(colc,rowc) && !$gr.is_in_queue(colc, rowc, $w)){
swap = $gr.queue_widget(colc, rowc, $w);
c = player_size_x;
break;
}
}
}
}
swap = true;
}
}
}));
console.log("queued items:");
for(var key in this.w_queue){
console.log("key " +key);
console.log(this.w_queue[key]);
}
//Move queued widgets
if(swap){
for(var key in w_queue){
if(swap && this.can_placeholder_be_set(to_col, to_row, player_size_x, player_size_y)){
for(var key in this.w_queue){
var col = parseInt(key.split("_")[0]);
var row = parseInt(key.split("_")[1]);
this.new_move_widget_to(w_queue[key], col, row);
if (this.w_queue[key] != "full"){
this.new_move_widget_to(this.w_queue[key], col, row);
}
}
this.set_placeholder(to_col, to_row);
}
/*If set to false smaller widgets will not displace larger widgets.
///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);
this.manage_movements(constraints.can_go_up, to_col, to_row);
this.manage_movements(constraints.can_not_go_up, to_col, to_row);
}
*/
/* if there is not widgets overlapping in the new player position,
@@ -1018,6 +1032,8 @@
this.set_placeholder(to_col, to_row);
}
this.w_queue = {};
return {
col: to_col,
row: to_row
@@ -1031,14 +1047,117 @@
for (var r = 0; r < w_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if(this.is_occupied(colc,rowc)){
occupied = true;
} else if(key in this.w_queue){
if(this.w_queue[key] == "full"){
occupied = true;
continue;
}
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
//remove queued items if no longer under player.
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key];
}
}
}
}
return occupied;
}
fn.can_placeholder_be_set = function(col, row, player_size_x, player_size_y){
var can_set = true;
for (var c = 0; c < player_size_x; c++){
for (var r = 0; r < player_size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
var $tw = this.is_widget(colc, rowc);
//if this space is occupied and not queued for move.
console.log("place check");
if(this.is_occupied(colc,rowc) && !this.is_widget_queued($tw)){
can_set = false;
}
}
}
console.log("can_set: " + can_set);
return can_set;
}
fn.queue_widget = function(col, row, $widget){
var $w = $widget
var wgd = $w.coords().grid;
var primary_key = col+"_"+row;
if (primary_key in this.w_queue){
return false;
}
this.w_queue[primary_key] = $w;
for (var c = 0; c < wgd.size_x; c++){
for (var r = 0; r < wgd.size_y; r++){
var colc = col + c;
var rowc = row + r;
var key = colc+"_"+rowc;
if (key == primary_key){
continue;
}
console.log("queue loop")
console.log(key);
this.w_queue[key] = "full";
}
}
return true;
}
fn.is_widget_queued = function($widget){
var queued = false;
if ($widget === false){
return false;
}
for(var key in this.w_queue){
if(this.w_queue[key] == "full"){
continue;
}
if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")){
queued = true;
}
}
return queued
}
fn.is_in_queue = function(col,row, $widget){
var queued = false;
var key = col+"_"+row;
if ((key in this.w_queue)){
console.log("key is in queue");
if (this.w_queue[key] == "full"){
queued = true;
} else {
$tw = this.w_queue[key];
tgd = $tw.coords().grid;
if(!this.is_widget_under_player(tgd.col,tgd.row)){
delete this.w_queue[key]
queued = false;
} else if(this.w_queue[key].attr("data-col") == $widget.attr("data-col") && this.w_queue[key].attr("data-row") == $widget.attr("data-row")) {
delete this.w_queue[key]
queued = false;
} else {
queued = true;
}
}
}
console.log("queued: " + queued);
return queued;
}
/**
* See which of the widgets in the $widgets param collection can go to
@@ -1434,10 +1553,11 @@
});
if (moved_down || changed_column) {
$nexts.each($.proxy(function(i, widget) {
/* $nexts.each($.proxy(function(i, widget) {
console.log("set_placeholder");
this.move_widget_up(
$(widget), this.placeholder_grid_data.col - col + phgd.size_y);
}, this));
}, this)); */
}
@@ -1445,6 +1565,7 @@
if ($widgets_under_ph.length) {
$widgets_under_ph.each($.proxy(function(i, widget) {
var $w = $(widget);
console.log("moving down from set_placeholder");
this.move_widget_down(
$w, row + phgd.size_y - $w.data('coords').grid.row);
}, this));
@@ -1700,10 +1821,11 @@
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],
/*this.for_each_widget_below(col, this.cells_occupied_by_player.rows[0],
function(tcol, trow) {
console.log("from_on_stop_overlapping_column");
self.move_widget_up(this, self.player_grid_data.size_y);
});
});*/
};
@@ -1718,11 +1840,12 @@
//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++) {
/*for (var c = 0, cl = cols.length; c < cl; c++) {
this.for_each_widget_below(cols[c], row, function(tcol, trow) {
console.log("from_on_stop_overlapping_row");
self.move_widget_up(this, self.player_grid_data.size_y);
});
}
}*/
};
//Not yet part of api - DM.
@@ -1796,6 +1919,7 @@
* @return {Class} Returns the instance of the Gridster Class.
*/
fn.move_widget_up = function($widget, y_units) {
console.log("move_widget_up");
var el_grid_data = $widget.coords().grid;
var actual_row = el_grid_data.row;
var moved = [];
@@ -1826,9 +1950,10 @@
moved.push($widget);
$next_widgets.each($.proxy(function(i, widget) {
/* $next_widgets.each($.proxy(function(i, widget) {
console.log("from_within_move_widget_up");
this.move_widget_up($(widget), y_units);
}, this));
}, this)); */
}
});