Commit 8f2975c6 by Torkel Ödegaard

moving panels betwen rows are starting to work

parent 9f945578
...@@ -20,6 +20,8 @@ const template = ` ...@@ -20,6 +20,8 @@ const template = `
</div> </div>
`; `;
var rowIndex = 0;
export class GridCtrl { export class GridCtrl {
options: any; options: any;
row: any; row: any;
...@@ -27,9 +29,14 @@ export class GridCtrl { ...@@ -27,9 +29,14 @@ export class GridCtrl {
panels: any; panels: any;
gridstack: any; gridstack: any;
gridElem: any; gridElem: any;
isInitialized: boolean;
isDestroyed: boolean;
index: number;
/** @ngInject */ /** @ngInject */
constructor(private $rootScope, private $element, private $timeout) { constructor(private $scope, private $element, private $timeout) {
this.index = rowIndex;
rowIndex += 1;
} }
init() { init() {
...@@ -43,6 +50,8 @@ export class GridCtrl { ...@@ -43,6 +50,8 @@ export class GridCtrl {
handle: '.panel-header' handle: '.panel-header'
}).data('gridstack'); }).data('gridstack');
this.isInitialized = true;
this.gridElem.on('added', (e, items) => { this.gridElem.on('added', (e, items) => {
for (let item of items) { for (let item of items) {
this.onGridStackItemAdded(item); this.onGridStackItemAdded(item);
...@@ -61,52 +70,79 @@ export class GridCtrl { ...@@ -61,52 +70,79 @@ export class GridCtrl {
} }
onGridStackItemAdded(item) { onGridStackItemAdded(item) {
console.log('item added', item); console.log('row: ' + this.index + ' item added', item);
if (this.dashboard.tempPanel) { // if item has id dont need to do anything
//this.gridstack.removeWidget(item.el, false); if (item.id) {
return;
this.$timeout(() => {
this.row.panels.push(this.dashboard.tempPanel);
});
} }
// if this comes from another row we need to remove it
this.$timeout(() => this.gridstack.removeWidget(item.el, true));
} }
onGridStackItemRemoved(item) { onGridStackItemRemoved(item) {
console.log('item removed', item.id); console.log('row: ' + this.index + ' item removed', item.id, item);
let panel = this.dashboard.getPanelById(parseInt(item.id)); // ignore items that have no panel id
this.dashboard.tempPanel = panel; // if (!item.id) {
this.$timeout(() => { // return;
this.row.removePanel(panel, false); // }
}); //
// let panel = this.dashboard.getPanelById(parseInt(item.id));
//
// if (panel) {
// panelChangingRow = panel
// this.row.removePanel(panel, false);
// }
} }
onGridStackItemsChanged(items) { onGridStackItemsChanged(items) {
console.log('row: ' +this.index + ' changes', items);
for (let item of items) { for (let item of items) {
var panel = this.dashboard.getPanelById(parseInt(item.id)); let isFromOtherRow = false;
if (!item.id) {
item.id = parseInt(item.el.attr('data-gs-id'));
isFromOtherRow = true;
}
// find panel
var panelInfo = this.dashboard.getPanelInfoById(parseInt(item.id));
if (!panelInfo) {
console.log('row: ' + this.index + ' item change but no panel found for item', item);
continue;
}
// update panel model position
let panel = panelInfo.panel;
panel.x = item.x; panel.x = item.x;
panel.y = item.y; panel.y = item.y;
panel.width = item.width; panel.width = item.width;
panel.height = item.height; panel.height = item.height;
}
this.$rootScope.$broadcast('render');
}
bindItem(element) { // wait a bit before adding
if (this.gridstack) { if (isFromOtherRow) {
this.gridstack.makeWidget(element); let movePanelFn = (panel, row) => {
return this.$timeout(() => {
console.log('moving panel movel to another row', panel);
// remove from source row
row.removePanel(panel, false);
// add this this row
this.row.panels.push(panel);
});
};
movePanelFn(panelInfo.panel, panelInfo.row);
} }
} }
itemScopeDestroyed(element) { this.$scope.$broadcast('render');
console.log('itemScopeDestroyed');
if (this.gridstack) {
this.gridstack.removeWidget(element, false);
}
} }
destroy() { destroy() {
this.gridstack.destroy(); this.gridstack.destroy();
this.gridstack = null; this.gridstack = null;
this.isDestroyed = true;
} }
} }
...@@ -169,9 +205,22 @@ export function dashGridItem($timeout, $rootScope) { ...@@ -169,9 +205,22 @@ export function dashGridItem($timeout, $rootScope) {
}, scope); }, scope);
scope.$on('$destroy', () => { scope.$on('$destroy', () => {
gridCtrl.itemScopeDestroyed(element); console.log('grid-item scope $destroy');
if (gridCtrl.isDestroyed) {
return;
}
let node = element.data('_gridstack_node');
if (node) {
console.log('grid-item scope $destroy removeWidget');
node._grid.removeWidget(element);
}
}); });
if (gridCtrl.isInitialized) {
gridCtrl.gridstack.makeWidget(element);
}
// scope.onItemRemoved({item: item}); // scope.onItemRemoved({item: item});
// ctrl.removeItem(element); // ctrl.removeItem(element);
......
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
</a> </a>
</div> </div>
<dash-grid row="ctrl.row" dashboard="ctrl.dashboard"></dash-grid> <dash-grid ng-if="!ctrl.row.collapse" row="ctrl.row" dashboard="ctrl.dashboard"></dash-grid>
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
.grid-stack { .grid-stack {
position: relative; position: relative;
min-height: 150px;
} }
.grid-stack.grid-stack-rtl { .grid-stack.grid-stack-rtl {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment