angular-gridster2
Version:
286 lines (285 loc) • 11.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var GridsterPush = (function () {
function GridsterPush(gridsterItem) {
this.pushedItems = [];
this.pushedItemsTemp = [];
this.pushedItemsTempPath = [];
this.pushedItemsPath = [];
this.gridsterItem = gridsterItem;
this.gridster = gridsterItem.gridster;
this.tryPattern = {
fromEast: [this.tryWest, this.trySouth, this.tryNorth, this.tryEast],
fromWest: [this.tryEast, this.trySouth, this.tryNorth, this.tryWest],
fromNorth: [this.trySouth, this.tryEast, this.tryWest, this.tryNorth],
fromSouth: [this.tryNorth, this.tryEast, this.tryWest, this.trySouth]
};
this.fromSouth = 'fromSouth';
this.fromNorth = 'fromNorth';
this.fromEast = 'fromEast';
this.fromWest = 'fromWest';
}
GridsterPush.prototype.destroy = function () {
delete this.gridster;
delete this.gridsterItem;
};
GridsterPush.prototype.pushItems = function (direction, disable) {
if (this.gridster.$options.pushItems && !disable) {
this.pushedItemsOrder = [];
if (!this.push(this.gridsterItem, direction)) {
this.restoreTempItems();
}
this.pushedItemsOrder = [];
this.pushedItemsTemp = [];
this.pushedItemsTempPath = [];
}
};
GridsterPush.prototype.restoreTempItems = function () {
var i = this.pushedItemsTemp.length - 1;
for (; i > -1; i--) {
this.removeFromTempPushed(this.pushedItemsTemp[i]);
}
};
GridsterPush.prototype.restoreItems = function () {
var i = 0;
var l = this.pushedItems.length;
var pushedItem;
for (; i < l; i++) {
pushedItem = this.pushedItems[i];
pushedItem.$item.x = pushedItem.item.x || 0;
pushedItem.$item.y = pushedItem.item.y || 0;
pushedItem.setSize(true);
}
this.pushedItems = [];
this.pushedItemsPath = [];
};
GridsterPush.prototype.setPushedItems = function () {
var i = 0;
var l = this.pushedItems.length;
var pushedItem;
for (; i < l; i++) {
pushedItem = this.pushedItems[i];
pushedItem.checkItemChanges(pushedItem.$item, pushedItem.item);
}
this.pushedItems = [];
this.pushedItemsPath = [];
};
GridsterPush.prototype.checkPushBack = function () {
var i = this.pushedItems.length - 1;
var change = false;
for (; i > -1; i--) {
if (this.checkPushedItem(this.pushedItems[i], i)) {
change = true;
}
}
if (change) {
this.checkPushBack();
}
};
GridsterPush.prototype.push = function (gridsterItem, direction) {
if (this.gridster.checkGridCollision(gridsterItem.$item)) {
return false;
}
var a = this.gridster.findItemsWithItem(gridsterItem.$item);
var i = a.length - 1, itemCollision;
var makePush = true;
var b = [];
for (; i > -1; i--) {
itemCollision = a[i];
if (itemCollision === this.gridsterItem) {
makePush = false;
break;
}
if (!itemCollision.canBeDragged()) {
makePush = false;
break;
}
if (this.pushedItemsTemp.indexOf(itemCollision) > -1) {
makePush = false;
break;
}
if (this.tryPattern[direction][0].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
b.push(itemCollision);
}
else if (this.tryPattern[direction][1].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
b.push(itemCollision);
}
else if (this.tryPattern[direction][2].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
b.push(itemCollision);
}
else if (this.tryPattern[direction][3].call(this, itemCollision, gridsterItem)) {
this.pushedItemsOrder.push(itemCollision);
b.push(itemCollision);
}
else {
makePush = false;
break;
}
}
if (!makePush) {
i = this.pushedItemsOrder.lastIndexOf(b[0]);
if (i > -1) {
var j = this.pushedItemsOrder.length - 1;
for (; j >= i; j--) {
itemCollision = this.pushedItemsOrder[j];
this.pushedItemsOrder.pop();
this.removeFromTempPushed(itemCollision);
this.removeFromPushedItem(itemCollision);
}
}
}
return makePush;
};
GridsterPush.prototype.trySouth = function (gridsterItemCollide, gridsterItem) {
if (!this.gridster.$options.pushDirections.south) {
return false;
}
this.addToTempPushed(gridsterItemCollide);
gridsterItemCollide.$item.y = gridsterItem.$item.y + gridsterItem.$item.rows;
if (this.push(gridsterItemCollide, this.fromNorth)) {
gridsterItemCollide.setSize(true);
this.addToPushed(gridsterItemCollide);
return true;
}
else {
this.removeFromTempPushed(gridsterItemCollide);
}
return false;
};
GridsterPush.prototype.tryNorth = function (gridsterItemCollide, gridsterItem) {
if (!this.gridster.$options.pushDirections.north) {
return false;
}
this.addToTempPushed(gridsterItemCollide);
gridsterItemCollide.$item.y = gridsterItem.$item.y - gridsterItemCollide.$item.rows;
if (this.push(gridsterItemCollide, this.fromSouth)) {
gridsterItemCollide.setSize(true);
this.addToPushed(gridsterItemCollide);
return true;
}
else {
this.removeFromTempPushed(gridsterItemCollide);
}
return false;
};
GridsterPush.prototype.tryEast = function (gridsterItemCollide, gridsterItem) {
if (!this.gridster.$options.pushDirections.east) {
return false;
}
this.addToTempPushed(gridsterItemCollide);
gridsterItemCollide.$item.x = gridsterItem.$item.x + gridsterItem.$item.cols;
if (this.push(gridsterItemCollide, this.fromWest)) {
gridsterItemCollide.setSize(true);
this.addToPushed(gridsterItemCollide);
return true;
}
else {
this.removeFromTempPushed(gridsterItemCollide);
}
return false;
};
GridsterPush.prototype.tryWest = function (gridsterItemCollide, gridsterItem) {
if (!this.gridster.$options.pushDirections.west) {
return false;
}
this.addToTempPushed(gridsterItemCollide);
gridsterItemCollide.$item.x = gridsterItem.$item.x - gridsterItemCollide.$item.cols;
if (this.push(gridsterItemCollide, this.fromEast)) {
gridsterItemCollide.setSize(true);
this.addToPushed(gridsterItemCollide);
return true;
}
else {
this.removeFromTempPushed(gridsterItemCollide);
}
return false;
};
GridsterPush.prototype.addToTempPushed = function (gridsterItem) {
var i = this.pushedItemsTemp.indexOf(gridsterItem);
if (i === -1) {
i = this.pushedItemsTemp.push(gridsterItem) - 1;
this.pushedItemsTempPath[i] = [];
}
this.pushedItemsTempPath[i].push({ x: gridsterItem.$item.x, y: gridsterItem.$item.y });
};
GridsterPush.prototype.removeFromTempPushed = function (gridsterItem) {
var i = this.pushedItemsTemp.indexOf(gridsterItem);
var tempPosition = this.pushedItemsTempPath[i].pop();
if (!tempPosition) {
return;
}
gridsterItem.$item.x = tempPosition.x;
gridsterItem.$item.y = tempPosition.y;
gridsterItem.setSize(true);
if (!this.pushedItemsTempPath[i].length) {
this.pushedItemsTemp.splice(i, 1);
this.pushedItemsTempPath.splice(i, 1);
}
};
GridsterPush.prototype.addToPushed = function (gridsterItem) {
if (this.pushedItems.indexOf(gridsterItem) < 0) {
this.pushedItems.push(gridsterItem);
this.pushedItemsPath.push([{ x: gridsterItem.item.x || 0, y: gridsterItem.item.y || 0 },
{ x: gridsterItem.$item.x, y: gridsterItem.$item.y }]);
}
else {
var i = this.pushedItems.indexOf(gridsterItem);
this.pushedItemsPath[i].push({ x: gridsterItem.$item.x, y: gridsterItem.$item.y });
}
};
GridsterPush.prototype.removeFromPushed = function (i) {
if (i > -1) {
this.pushedItems.splice(i, 1);
this.pushedItemsPath.splice(i, 1);
}
};
GridsterPush.prototype.removeFromPushedItem = function (gridsterItem) {
var i = this.pushedItems.indexOf(gridsterItem);
if (i > -1) {
this.pushedItemsPath[i].pop();
if (!this.pushedItemsPath.length) {
this.pushedItems.splice(i, 1);
this.pushedItemsPath.splice(i, 1);
}
}
};
GridsterPush.prototype.checkPushedItem = function (pushedItem, i) {
var path = this.pushedItemsPath[i];
var j = path.length - 2;
var lastPosition, x, y;
var change = false;
for (; j > -1; j--) {
lastPosition = path[j];
x = pushedItem.$item.x;
y = pushedItem.$item.y;
pushedItem.$item.x = lastPosition.x;
pushedItem.$item.y = lastPosition.y;
if (!this.gridster.findItemWithItem(pushedItem.$item)) {
pushedItem.setSize(true);
path.splice(j + 1, path.length - j - 1);
change = true;
}
else {
pushedItem.$item.x = x;
pushedItem.$item.y = y;
}
}
if (path.length < 2) {
this.removeFromPushed(i);
}
return change;
};
GridsterPush.decorators = [
{ type: core_1.Injectable },
];
/** @nocollapse */
GridsterPush.ctorParameters = function () { return [
null,
]; };
return GridsterPush;
}());
exports.GridsterPush = GridsterPush;