Browse Source

UI: Improve dashboard layout calculations.

pull/481/head
Igor Kulikov 9 years ago
parent
commit
1b525cfed7
  1. 137
      ui/src/app/components/dashboard.directive.js

137
ui/src/app/components/dashboard.directive.js

@ -140,6 +140,8 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
vm.widgetLayoutInfo = { vm.widgetLayoutInfo = {
}; };
vm.widgetIds = [];
vm.widgetItemMap = { vm.widgetItemMap = {
sizeX: 'vm.widgetLayoutInfo[widget.id].sizeX', sizeX: 'vm.widgetLayoutInfo[widget.id].sizeX',
sizeY: 'vm.widgetLayoutInfo[widget.id].sizeY', sizeY: 'vm.widgetLayoutInfo[widget.id].sizeY',
@ -233,73 +235,67 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
removeResizeListener(gridsterParent[0], onGridsterParentResize); // eslint-disable-line no-undef removeResizeListener(gridsterParent[0], onGridsterParentResize); // eslint-disable-line no-undef
}); });
watchWidgets(); $scope.$watchCollection('vm.widgets', function () {
var ids = [];
function onGridsterParentResize() { for (var i=0;i<vm.widgets.length;i++) {
if (gridsterParent.height() && autofillHeight()) { var widget = vm.widgets[i];
updateMobileOpts(); if (!widget.id) {
widget.id = utils.guid();
}
ids.push(widget.id);
} }
} ids.sort(function (id1, id2) {
return id1.localeCompare(id2);
function watchWidgets() { });
$scope.widgetsCollectionWatch = $scope.$watchCollection('vm.widgets', function () { if (angular.equals(ids, vm.widgetIds)) {
if (vm.skipInitialWidgetsWatch) { return;
$timeout(function() { vm.skipInitialWidgetsWatch = false; }); }
return; vm.widgetIds = ids;
for (i=0;i<vm.widgets.length;i++) {
widget = vm.widgets[i];
var layoutInfoObject = vm.widgetLayoutInfo[widget.id];
if (!layoutInfoObject) {
layoutInfoObject = {
widget: widget
};
Object.defineProperty(layoutInfoObject, 'sizeX', {
get: function() { return widgetSizeX(this.widget) },
set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)}
});
Object.defineProperty(layoutInfoObject, 'sizeY', {
get: function() { return widgetSizeY(this.widget) },
set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)}
});
Object.defineProperty(layoutInfoObject, 'row', {
get: function() { return widgetRow(this.widget) },
set: function(newRow) { setWidgetRow(this.widget, newRow)}
});
Object.defineProperty(layoutInfoObject, 'col', {
get: function() { return widgetCol(this.widget) },
set: function(newCol) { setWidgetCol(this.widget, newCol)}
});
vm.widgetLayoutInfo[widget.id] = layoutInfoObject;
} }
var ids = []; }
for (var i=0;i<vm.widgets.length;i++) { for (var widgetId in vm.widgetLayoutInfo) {
var widget = vm.widgets[i]; if (ids.indexOf(widgetId) === -1) {
if (!widget.id) { delete vm.widgetLayoutInfo[widgetId];
widget.id = utils.guid();
}
ids.push(widget.id);
var layoutInfoObject = vm.widgetLayoutInfo[widget.id];
if (!layoutInfoObject) {
layoutInfoObject = {
widget: widget
};
Object.defineProperty(layoutInfoObject, 'sizeX', {
get: function() { return widgetSizeX(this.widget) },
set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)}
});
Object.defineProperty(layoutInfoObject, 'sizeY', {
get: function() { return widgetSizeY(this.widget) },
set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)}
});
Object.defineProperty(layoutInfoObject, 'row', {
get: function() { return widgetRow(this.widget) },
set: function(newRow) { setWidgetRow(this.widget, newRow)}
});
Object.defineProperty(layoutInfoObject, 'col', {
get: function() { return widgetCol(this.widget) },
set: function(newCol) { setWidgetCol(this.widget, newCol)}
});
vm.widgetLayoutInfo[widget.id] = layoutInfoObject;
}
} }
for (var widgetId in vm.widgetLayoutInfo) { }
if (ids.indexOf(widgetId) === -1) { sortWidgets();
delete vm.widgetLayoutInfo[widgetId]; $mdUtil.nextTick(function () {
} if (autofillHeight()) {
updateMobileOpts();
} }
$mdUtil.nextTick(function () {
sortWidgets();
if (autofillHeight()) {
updateMobileOpts();
}
});
}); });
} });
function stopWatchWidgets() { function onGridsterParentResize() {
if ($scope.widgetsCollectionWatch) { if (gridsterParent.height() && autofillHeight()) {
$scope.widgetsCollectionWatch(); updateMobileOpts();
$scope.widgetsCollectionWatch = null;
} }
} }
//TODO: widgets visibility //TODO: widgets visibility
/*gridsterParent.scroll(function () { /*gridsterParent.scroll(function () {
updateVisibleRect(); updateVisibleRect();
@ -346,10 +342,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
$scope.$watch(function() { return $mdMedia('gt-sm'); }, function() { $scope.$watch(function() { return $mdMedia('gt-sm'); }, function() {
updateMobileOpts(); updateMobileOpts();
sortWidgets();
}); });
$scope.$watch('vm.isMobile', function () { $scope.$watch('vm.isMobile', function () {
updateMobileOpts(); updateMobileOpts();
sortWidgets();
}); });
$scope.$watch('vm.autofillHeight', function () { $scope.$watch('vm.autofillHeight', function () {
@ -366,6 +364,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
$scope.$watch('vm.isMobileDisabled', function () { $scope.$watch('vm.isMobileDisabled', function () {
updateMobileOpts(); updateMobileOpts();
sortWidgets();
});
$scope.$watch('vm.widgetLayouts', function () {
updateMobileOpts();
sortWidgets();
}); });
$scope.$watch('vm.columns', function () { $scope.$watch('vm.columns', function () {
@ -410,6 +414,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
$scope.$on('gridster-resized', function (event, sizes, theGridster) { $scope.$on('gridster-resized', function (event, sizes, theGridster) {
if (checkIsLocalGridsterElement(theGridster)) { if (checkIsLocalGridsterElement(theGridster)) {
vm.gridster = theGridster; vm.gridster = theGridster;
setupGridster(vm.gridster);
vm.isResizing = false; vm.isResizing = false;
//TODO: widgets visibility //TODO: widgets visibility
//updateVisibleRect(false, true); //updateVisibleRect(false, true);
@ -419,6 +424,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
$scope.$on('gridster-mobile-changed', function (event, theGridster) { $scope.$on('gridster-mobile-changed', function (event, theGridster) {
if (checkIsLocalGridsterElement(theGridster)) { if (checkIsLocalGridsterElement(theGridster)) {
vm.gridster = theGridster; vm.gridster = theGridster;
setupGridster(vm.gridster);
detectRowSize(vm.gridster.isMobile).then( detectRowSize(vm.gridster.isMobile).then(
function(rowHeight) { function(rowHeight) {
if (vm.gridsterOpts.rowHeight != rowHeight) { if (vm.gridsterOpts.rowHeight != rowHeight) {
@ -517,18 +523,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
loadDashboard(); loadDashboard();
function sortWidgets() { function sortWidgets() {
stopWatchWidgets();
vm.widgets.sort(function (widget1, widget2) { vm.widgets.sort(function (widget1, widget2) {
var row1 = widgetOrder(widget1); var row1 = widgetOrder(widget1);
var row2 = widgetOrder(widget2); var row2 = widgetOrder(widget2);
var res = row1 - row2; var res = row1 - row2;
if (res === 0) { if (res === 0) {
res = widget1.col - widget2.col; res = widgetCol(widget1) - widgetCol(widget2);
} }
return res; return res;
}); });
vm.skipInitialWidgetsWatch = true;
watchWidgets();
} }
function reload() { function reload() {
@ -1037,6 +1040,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
$scope.gridsterScopeWatcher = null; $scope.gridsterScopeWatcher = null;
var gridsterScope = gridsterElement.scope(); var gridsterScope = gridsterElement.scope();
vm.gridster = gridsterScope.gridster; vm.gridster = gridsterScope.gridster;
setupGridster(vm.gridster);
if (vm.onInit) { if (vm.onInit) {
vm.onInit({dashboard: vm}); vm.onInit({dashboard: vm});
} }
@ -1046,6 +1050,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
}); });
} }
function setupGridster(gridster) {
if (gridster) {
if (!gridster.origMoveOverlappingItems) {
gridster.origMoveOverlappingItems = gridster.moveOverlappingItems;
gridster.moveOverlappingItems = () => {};
}
}
}
function loading() { function loading() {
return !vm.ignoreLoading && $rootScope.loading; return !vm.ignoreLoading && $rootScope.loading;
} }

Loading…
Cancel
Save