Commit 1b525cfed73befe31e98ffff9916a4f0611c25ba

Authored by Igor Kulikov
1 parent ab344732

UI: Improve dashboard layout calculations.

@@ -140,6 +140,8 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -140,6 +140,8 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
140 vm.widgetLayoutInfo = { 140 vm.widgetLayoutInfo = {
141 }; 141 };
142 142
  143 + vm.widgetIds = [];
  144 +
143 vm.widgetItemMap = { 145 vm.widgetItemMap = {
144 sizeX: 'vm.widgetLayoutInfo[widget.id].sizeX', 146 sizeX: 'vm.widgetLayoutInfo[widget.id].sizeX',
145 sizeY: 'vm.widgetLayoutInfo[widget.id].sizeY', 147 sizeY: 'vm.widgetLayoutInfo[widget.id].sizeY',
@@ -233,73 +235,67 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -233,73 +235,67 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
233 removeResizeListener(gridsterParent[0], onGridsterParentResize); // eslint-disable-line no-undef 235 removeResizeListener(gridsterParent[0], onGridsterParentResize); // eslint-disable-line no-undef
234 }); 236 });
235 237
236 - watchWidgets();  
237 -  
238 - function onGridsterParentResize() {  
239 - if (gridsterParent.height() && autofillHeight()) {  
240 - updateMobileOpts(); 238 + $scope.$watchCollection('vm.widgets', function () {
  239 + var ids = [];
  240 + for (var i=0;i<vm.widgets.length;i++) {
  241 + var widget = vm.widgets[i];
  242 + if (!widget.id) {
  243 + widget.id = utils.guid();
  244 + }
  245 + ids.push(widget.id);
241 } 246 }
242 - }  
243 -  
244 - function watchWidgets() {  
245 - $scope.widgetsCollectionWatch = $scope.$watchCollection('vm.widgets', function () {  
246 - if (vm.skipInitialWidgetsWatch) {  
247 - $timeout(function() { vm.skipInitialWidgetsWatch = false; });  
248 - return; 247 + ids.sort(function (id1, id2) {
  248 + return id1.localeCompare(id2);
  249 + });
  250 + if (angular.equals(ids, vm.widgetIds)) {
  251 + return;
  252 + }
  253 + vm.widgetIds = ids;
  254 + for (i=0;i<vm.widgets.length;i++) {
  255 + widget = vm.widgets[i];
  256 + var layoutInfoObject = vm.widgetLayoutInfo[widget.id];
  257 + if (!layoutInfoObject) {
  258 + layoutInfoObject = {
  259 + widget: widget
  260 + };
  261 + Object.defineProperty(layoutInfoObject, 'sizeX', {
  262 + get: function() { return widgetSizeX(this.widget) },
  263 + set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)}
  264 + });
  265 + Object.defineProperty(layoutInfoObject, 'sizeY', {
  266 + get: function() { return widgetSizeY(this.widget) },
  267 + set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)}
  268 + });
  269 + Object.defineProperty(layoutInfoObject, 'row', {
  270 + get: function() { return widgetRow(this.widget) },
  271 + set: function(newRow) { setWidgetRow(this.widget, newRow)}
  272 + });
  273 + Object.defineProperty(layoutInfoObject, 'col', {
  274 + get: function() { return widgetCol(this.widget) },
  275 + set: function(newCol) { setWidgetCol(this.widget, newCol)}
  276 + });
  277 + vm.widgetLayoutInfo[widget.id] = layoutInfoObject;
249 } 278 }
250 - var ids = [];  
251 - for (var i=0;i<vm.widgets.length;i++) {  
252 - var widget = vm.widgets[i];  
253 - if (!widget.id) {  
254 - widget.id = utils.guid();  
255 - }  
256 - ids.push(widget.id);  
257 - var layoutInfoObject = vm.widgetLayoutInfo[widget.id];  
258 - if (!layoutInfoObject) {  
259 - layoutInfoObject = {  
260 - widget: widget  
261 - };  
262 - Object.defineProperty(layoutInfoObject, 'sizeX', {  
263 - get: function() { return widgetSizeX(this.widget) },  
264 - set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)}  
265 - });  
266 - Object.defineProperty(layoutInfoObject, 'sizeY', {  
267 - get: function() { return widgetSizeY(this.widget) },  
268 - set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)}  
269 - });  
270 - Object.defineProperty(layoutInfoObject, 'row', {  
271 - get: function() { return widgetRow(this.widget) },  
272 - set: function(newRow) { setWidgetRow(this.widget, newRow)}  
273 - });  
274 - Object.defineProperty(layoutInfoObject, 'col', {  
275 - get: function() { return widgetCol(this.widget) },  
276 - set: function(newCol) { setWidgetCol(this.widget, newCol)}  
277 - });  
278 - vm.widgetLayoutInfo[widget.id] = layoutInfoObject;  
279 - } 279 + }
  280 + for (var widgetId in vm.widgetLayoutInfo) {
  281 + if (ids.indexOf(widgetId) === -1) {
  282 + delete vm.widgetLayoutInfo[widgetId];
280 } 283 }
281 - for (var widgetId in vm.widgetLayoutInfo) {  
282 - if (ids.indexOf(widgetId) === -1) {  
283 - delete vm.widgetLayoutInfo[widgetId];  
284 - } 284 + }
  285 + sortWidgets();
  286 + $mdUtil.nextTick(function () {
  287 + if (autofillHeight()) {
  288 + updateMobileOpts();
285 } 289 }
286 - $mdUtil.nextTick(function () {  
287 - sortWidgets();  
288 - if (autofillHeight()) {  
289 - updateMobileOpts();  
290 - }  
291 - });  
292 }); 290 });
293 - } 291 + });
294 292
295 - function stopWatchWidgets() {  
296 - if ($scope.widgetsCollectionWatch) {  
297 - $scope.widgetsCollectionWatch();  
298 - $scope.widgetsCollectionWatch = null; 293 + function onGridsterParentResize() {
  294 + if (gridsterParent.height() && autofillHeight()) {
  295 + updateMobileOpts();
299 } 296 }
300 } 297 }
301 298
302 -  
303 //TODO: widgets visibility 299 //TODO: widgets visibility
304 /*gridsterParent.scroll(function () { 300 /*gridsterParent.scroll(function () {
305 updateVisibleRect(); 301 updateVisibleRect();
@@ -346,10 +342,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -346,10 +342,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
346 342
347 $scope.$watch(function() { return $mdMedia('gt-sm'); }, function() { 343 $scope.$watch(function() { return $mdMedia('gt-sm'); }, function() {
348 updateMobileOpts(); 344 updateMobileOpts();
  345 + sortWidgets();
349 }); 346 });
350 347
351 $scope.$watch('vm.isMobile', function () { 348 $scope.$watch('vm.isMobile', function () {
352 updateMobileOpts(); 349 updateMobileOpts();
  350 + sortWidgets();
353 }); 351 });
354 352
355 $scope.$watch('vm.autofillHeight', function () { 353 $scope.$watch('vm.autofillHeight', function () {
@@ -366,6 +364,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -366,6 +364,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
366 364
367 $scope.$watch('vm.isMobileDisabled', function () { 365 $scope.$watch('vm.isMobileDisabled', function () {
368 updateMobileOpts(); 366 updateMobileOpts();
  367 + sortWidgets();
  368 + });
  369 +
  370 + $scope.$watch('vm.widgetLayouts', function () {
  371 + updateMobileOpts();
  372 + sortWidgets();
369 }); 373 });
370 374
371 $scope.$watch('vm.columns', function () { 375 $scope.$watch('vm.columns', function () {
@@ -410,6 +414,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -410,6 +414,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
410 $scope.$on('gridster-resized', function (event, sizes, theGridster) { 414 $scope.$on('gridster-resized', function (event, sizes, theGridster) {
411 if (checkIsLocalGridsterElement(theGridster)) { 415 if (checkIsLocalGridsterElement(theGridster)) {
412 vm.gridster = theGridster; 416 vm.gridster = theGridster;
  417 + setupGridster(vm.gridster);
413 vm.isResizing = false; 418 vm.isResizing = false;
414 //TODO: widgets visibility 419 //TODO: widgets visibility
415 //updateVisibleRect(false, true); 420 //updateVisibleRect(false, true);
@@ -419,6 +424,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -419,6 +424,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
419 $scope.$on('gridster-mobile-changed', function (event, theGridster) { 424 $scope.$on('gridster-mobile-changed', function (event, theGridster) {
420 if (checkIsLocalGridsterElement(theGridster)) { 425 if (checkIsLocalGridsterElement(theGridster)) {
421 vm.gridster = theGridster; 426 vm.gridster = theGridster;
  427 + setupGridster(vm.gridster);
422 detectRowSize(vm.gridster.isMobile).then( 428 detectRowSize(vm.gridster.isMobile).then(
423 function(rowHeight) { 429 function(rowHeight) {
424 if (vm.gridsterOpts.rowHeight != rowHeight) { 430 if (vm.gridsterOpts.rowHeight != rowHeight) {
@@ -517,18 +523,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -517,18 +523,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
517 loadDashboard(); 523 loadDashboard();
518 524
519 function sortWidgets() { 525 function sortWidgets() {
520 - stopWatchWidgets();  
521 vm.widgets.sort(function (widget1, widget2) { 526 vm.widgets.sort(function (widget1, widget2) {
522 var row1 = widgetOrder(widget1); 527 var row1 = widgetOrder(widget1);
523 var row2 = widgetOrder(widget2); 528 var row2 = widgetOrder(widget2);
524 var res = row1 - row2; 529 var res = row1 - row2;
525 if (res === 0) { 530 if (res === 0) {
526 - res = widget1.col - widget2.col; 531 + res = widgetCol(widget1) - widgetCol(widget2);
527 } 532 }
528 return res; 533 return res;
529 }); 534 });
530 - vm.skipInitialWidgetsWatch = true;  
531 - watchWidgets();  
532 } 535 }
533 536
534 function reload() { 537 function reload() {
@@ -1037,6 +1040,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -1037,6 +1040,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
1037 $scope.gridsterScopeWatcher = null; 1040 $scope.gridsterScopeWatcher = null;
1038 var gridsterScope = gridsterElement.scope(); 1041 var gridsterScope = gridsterElement.scope();
1039 vm.gridster = gridsterScope.gridster; 1042 vm.gridster = gridsterScope.gridster;
  1043 + setupGridster(vm.gridster);
1040 if (vm.onInit) { 1044 if (vm.onInit) {
1041 vm.onInit({dashboard: vm}); 1045 vm.onInit({dashboard: vm});
1042 } 1046 }
@@ -1046,6 +1050,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ @@ -1046,6 +1050,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
1046 }); 1050 });
1047 } 1051 }
1048 1052
  1053 + function setupGridster(gridster) {
  1054 + if (gridster) {
  1055 + if (!gridster.origMoveOverlappingItems) {
  1056 + gridster.origMoveOverlappingItems = gridster.moveOverlappingItems;
  1057 + gridster.moveOverlappingItems = () => {};
  1058 + }
  1059 + }
  1060 + }
  1061 +
1049 function loading() { 1062 function loading() {
1050 return !vm.ignoreLoading && $rootScope.loading; 1063 return !vm.ignoreLoading && $rootScope.loading;
1051 } 1064 }