Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 00f61ba

Browse files
committed
Add support for cascading left & right. Closes #2. Closes #3
1 parent d563751 commit 00f61ba

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

NgGrid.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ export class NgGrid {
146146
this.colWidth = Math.max(this._setWidth, this._minWidth);
147147
break;
148148
case 'cascade':
149-
this.cascade = val;
149+
if (this.cascade != val) {
150+
this.cascade = val;
151+
this._cascadeGrid();
152+
}
150153
break;
151154
case 'fix_to_grid':
152155
this._fixToGrid = val ? true : false;
@@ -467,7 +470,7 @@ export class NgGrid {
467470

468471
this._draggingItem.setGridPosition(itemPos.col, itemPos.row);
469472
this._addToGrid(this._draggingItem);
470-
473+
console.log("STOP", itemPos);
471474
this._cascadeGrid();
472475

473476
this._draggingItem.stopMoving();
@@ -641,6 +644,56 @@ export class NgGrid {
641644
}
642645
}
643646
break;
647+
case "left":
648+
case "right":
649+
var lowCol: Array<number> = [0];
650+
651+
for (var i:number = 1; i <= this._getMaxRow(); i++)
652+
lowCol[i] = 1;
653+
654+
for (var r:number = 1; r <= this._getMaxRow(); r++) {
655+
if (this._itemGrid[r] == undefined) continue;
656+
657+
for (var c:number = 1; c <= this._getMaxCol(); c++) {
658+
if (this._itemGrid[r] == undefined) break;
659+
if (c < lowCol[r]) continue;
660+
console.log(r, c, lowCol);
661+
662+
if (this._itemGrid[r][c] != null) {
663+
var item = this._itemGrid[r][c];
664+
var itemDims = item.getSize();
665+
var itemPos = item.getGridPosition();
666+
667+
if (itemPos.col != c || itemPos.row != r) continue; // If this is not the element's start
668+
669+
var lowest = lowCol[r];
670+
671+
for (var i: number = 1; i < itemDims.y; i++) {
672+
lowest = Math.max(lowCol[(r + i)], lowest);
673+
}
674+
675+
if (pos && (r + itemDims.y) > pos.row && r < (pos.row + dims.y)) { // If our element is in one of the item's rows
676+
if ((c >= pos.col && c < (pos.col + dims.x)) || // If this col is occupied by our element
677+
((itemDims.x > (pos.col - lowest)) && // Or the item can't fit above our element
678+
(c >= (pos.col + dims.x) && lowest < (pos.col + dims.x)))) { // And this col is below our element, but we haven't caught it
679+
lowest = Math.max(lowest, pos.col + dims.x); // Set the lowest col to be below it
680+
}
681+
}
682+
683+
if (lowest != itemPos.col) { // If the item is not already on this col move it up
684+
this._removeFromGrid(item);
685+
item.setGridPosition(lowest, r);
686+
this._addToGrid(item);
687+
}
688+
689+
for (var i: number = 0; i < itemDims.y; i++) {
690+
lowCol[r+i] = lowest + itemDims.x; // Update the lowest col to be below the item
691+
}
692+
693+
}
694+
}
695+
}
696+
break;
644697
}
645698
}
646699

@@ -704,7 +757,7 @@ export class NgGrid {
704757
var maxRow = Math.max(this._getMaxRow(), row);
705758
var maxCol = Math.max(this._getMaxCol(), col);
706759

707-
this._renderer.setElementStyle(this._ngEl, 'width', (maxCol * (this.colWidth + this.marginLeft + this.marginRight))+"px");
760+
this._renderer.setElementStyle(this._ngEl, 'width', "100%");//(maxCol * (this.colWidth + this.marginLeft + this.marginRight))+"px");
708761
this._renderer.setElementStyle(this._ngEl, 'height', (maxRow * (this.rowHeight + this.marginTop + this.marginBottom)) + "px");
709762
}
710763

@@ -763,7 +816,7 @@ export class NgGrid {
763816
var top = e.clientY - refPos.top;
764817

765818
if (this.cascade == "down") top = refPos.top + refPos.height - e.clientY;
766-
if (this.cascade == "right") top = refPos.left + refPos.width - e.clientX;
819+
if (this.cascade == "right") left = refPos.left + refPos.width - e.clientX;
767820

768821
return {
769822
left: left,

app.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ <h1>Angular 2 Grid Demo</h1>
3636
<select name="cascade" ng-control="cascade" [(ng-model)]="gridConfig.cascade">
3737
<option value="up">Up</option>
3838
<option value="down">Down</option>
39-
<!-- <option value="left">Left</option>
40-
<option value="right">Right</option> -->
39+
<option value="left">Left</option>
40+
<option value="right">Right</option>
4141
</select>
4242
</div>
4343
</div>

0 commit comments

Comments
 (0)