Commit 48a54edd by Torkel Ödegaard

ux(dashboard): improving drag and drop behavior!

parent 09a19d8e
...@@ -8,7 +8,12 @@ ...@@ -8,7 +8,12 @@
<div class="add-panel-panels-scroll"> <div class="add-panel-panels-scroll">
<div class="add-panel-panels"> <div class="add-panel-panels">
<div class="add-panel-item" ng-repeat="panel in ctrl.panelHits" ng-class="{active: $index === ctrl.activeIndex}" ng-click="ctrl.addPanel(panel)"> <div class="add-panel-item"
ng-repeat="panel in ctrl.panelHits"
ng-class="{active: $index === ctrl.activeIndex}"
ng-click="ctrl.addPanel(panel)"
ui-draggable="ctrl.dashboard.editMode"
drag="panel.id">
<img class="add-panel-item-img" ng-src="{{panel.info.logos.small}}"></img> <img class="add-panel-item-img" ng-src="{{panel.info.logos.small}}"></img>
<div class="add-panel-item-name">{{panel.name}}</div> <div class="add-panel-item-name">{{panel.name}}</div>
</div> </div>
......
...@@ -93,7 +93,6 @@ export class AddPanelCtrl { ...@@ -93,7 +93,6 @@ export class AddPanelCtrl {
isNew: true, isNew: true,
}; };
this.rowCtrl.dropView = 0;
this.dashboard.addPanel(panel, this.row); this.dashboard.addPanel(panel, this.row);
this.$timeout(() => { this.$timeout(() => {
this.$rootScope.$broadcast('render'); this.$rootScope.$broadcast('render');
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
</div> </div>
<div class="panels-wrapper" ng-if="!ctrl.row.collapse"> <div class="panels-wrapper" ng-if="!ctrl.row.collapse">
<div ng-repeat="panel in ctrl.row.panels track by panel.id" class="panel" ui-draggable="!ctrl.dashboard.meta.fullscreen" drag="panel.id" ui-on-drop="ctrl.onDrop($data, panel)" drag-handle-class="drag-handle" panel-width> <div ng-repeat="panel in ctrl.row.panels track by panel.id" class="panel" ui-draggable="ctrl.dashboard.editMode" drag="panel.id" ui-on-drop="ctrl.onDrop($data, panel)" drag-handle-class="drag-handle" panel-width>
<plugin-component type="panel" class="panel-margin"> <plugin-component type="panel" class="panel-margin">
</plugin-component> </plugin-component>
</div> </div>
......
...@@ -26,21 +26,61 @@ export class DashRowCtrl { ...@@ -26,21 +26,61 @@ export class DashRowCtrl {
} }
onDrop(panelId, dropTarget) { onDrop(panelId, dropTarget) {
var info = this.dashboard.getPanelInfoById(panelId); var dragObject;
// if string it's a panel type
if (_.isString(panelId)) {
// setup new panel
dragObject = {
row: this.row,
panel: {
title: config.new_panel_title,
type: panelId,
id: this.dashboard.getNextPanelId(),
},
isNew: true,
};
} else {
dragObject = this.dashboard.getPanelInfoById(panelId);
}
if (dropTarget) { if (dropTarget) {
var dropInfo = this.dashboard.getPanelInfoById(dropTarget.id); dropTarget = this.dashboard.getPanelInfoById(dropTarget.id);
dropInfo.row.panels[dropInfo.index] = info.panel; // if draging new panel onto existing panel split it
info.row.panels[info.index] = dropTarget; if (dragObject.isNew) {
var dragSpan = info.panel.span; dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
info.panel.span = dropTarget.span; // insert after
dropTarget.span = dragSpan; dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
} else if (this.row === dragObject.row) {
// just move element
this.row.movePanel(dropTarget.index, dragObject.index);
} else {
// split drop target space
dragObject.panel.span = dropTarget.panel.span = dropTarget.panel.span/2;
// insert after
dropTarget.row.panels.splice(dropTarget.index+1, 0, dragObject.panel);
// remove from source row
dragObject.row.removePanel(dragObject.panel);
}
// dropInfo.row.panels[dropInfo.index] = info.panel;
// info.row.panels[info.index] = dropTarget;
// var dragSpan = info.panel.span;
// info.panel.span = dropTarget.span;
// dropTarget.span = dragSpan;
} else { } else {
info.row.panels.splice(info.index, 1); dragObject.panel.span = 12 - this.row.span;
info.panel.span = 12 - this.dashboard.rowSpan(this.row); this.row.panels.push(dragObject.panel);
this.row.panels.push(info.panel);
// if not new remove from source row
if (!dragObject.isNew) {
dragObject.row.removePanel(dragObject.panel);
}
} }
this.$rootScope.$broadcast('render'); this.row.panelSpanChanged();
this.$timeout(() => {
this.$rootScope.$broadcast('render');
});
} }
setHeight(height) { setHeight(height) {
...@@ -216,16 +256,9 @@ coreModule.directive('panelDropZone', function($timeout) { ...@@ -216,16 +256,9 @@ coreModule.directive('panelDropZone', function($timeout) {
scope.$on("ANGULAR_DRAG_START", function() { scope.$on("ANGULAR_DRAG_START", function() {
indrag = true; indrag = true;
updateState(); updateState();
// $timeout(function() {
// var dropZoneSpan = 12 - scope.ctrl.dashboard.rowSpan(scope.ctrl.row);
// if (dropZoneSpan > 0) {
// showPanel(dropZoneSpan, 'Panel Drop Zone');
// }
// });
}); });
scope.$on("ANGULAR_DRAG_END", function() { scope.$on("ANGULAR_DRAG_END", function() {
console.log('drag end');
indrag = false; indrag = false;
updateState(); updateState();
}); });
......
...@@ -22,7 +22,6 @@ export class DashboardRow { ...@@ -22,7 +22,6 @@ export class DashboardRow {
}; };
constructor(private model) { constructor(private model) {
console.log(model.isNew);
assignModelProperties(this, model, this.defaults); assignModelProperties(this, model, this.defaults);
this.events = new Emitter(); this.events = new Emitter();
this.updateRowSpan(); this.updateRowSpan();
...@@ -63,6 +62,11 @@ export class DashboardRow { ...@@ -63,6 +62,11 @@ export class DashboardRow {
this.panels[0].span = 4; this.panels[0].span = 4;
this.panels[1].span = 4; this.panels[1].span = 4;
panel.span = 4; panel.span = 4;
} else if (panelCount === 3) {
this.panels[0].span = 3;
this.panels[1].span = 3;
this.panels[2].span = 3;
panel.span = 3;
} }
} }
...@@ -78,5 +82,9 @@ export class DashboardRow { ...@@ -78,5 +82,9 @@ export class DashboardRow {
this.events.emit('panel-removed', panel); this.events.emit('panel-removed', panel);
this.panelSpanChanged(); this.panelSpanChanged();
} }
movePanel(fromIndex, toIndex) {
this.panels.splice(toIndex, 0, this.panels.splice(fromIndex, 1)[0]);
}
} }
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