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 140 vm.widgetLayoutInfo = {
141 141 };
142 142
  143 + vm.widgetIds = [];
  144 +
143 145 vm.widgetItemMap = {
144 146 sizeX: 'vm.widgetLayoutInfo[widget.id].sizeX',
145 147 sizeY: 'vm.widgetLayoutInfo[widget.id].sizeY',
... ... @@ -233,73 +235,67 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
233 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 299 //TODO: widgets visibility
304 300 /*gridsterParent.scroll(function () {
305 301 updateVisibleRect();
... ... @@ -346,10 +342,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
346 342
347 343 $scope.$watch(function() { return $mdMedia('gt-sm'); }, function() {
348 344 updateMobileOpts();
  345 + sortWidgets();
349 346 });
350 347
351 348 $scope.$watch('vm.isMobile', function () {
352 349 updateMobileOpts();
  350 + sortWidgets();
353 351 });
354 352
355 353 $scope.$watch('vm.autofillHeight', function () {
... ... @@ -366,6 +364,12 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
366 364
367 365 $scope.$watch('vm.isMobileDisabled', function () {
368 366 updateMobileOpts();
  367 + sortWidgets();
  368 + });
  369 +
  370 + $scope.$watch('vm.widgetLayouts', function () {
  371 + updateMobileOpts();
  372 + sortWidgets();
369 373 });
370 374
371 375 $scope.$watch('vm.columns', function () {
... ... @@ -410,6 +414,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
410 414 $scope.$on('gridster-resized', function (event, sizes, theGridster) {
411 415 if (checkIsLocalGridsterElement(theGridster)) {
412 416 vm.gridster = theGridster;
  417 + setupGridster(vm.gridster);
413 418 vm.isResizing = false;
414 419 //TODO: widgets visibility
415 420 //updateVisibleRect(false, true);
... ... @@ -419,6 +424,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
419 424 $scope.$on('gridster-mobile-changed', function (event, theGridster) {
420 425 if (checkIsLocalGridsterElement(theGridster)) {
421 426 vm.gridster = theGridster;
  427 + setupGridster(vm.gridster);
422 428 detectRowSize(vm.gridster.isMobile).then(
423 429 function(rowHeight) {
424 430 if (vm.gridsterOpts.rowHeight != rowHeight) {
... ... @@ -517,18 +523,15 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
517 523 loadDashboard();
518 524
519 525 function sortWidgets() {
520   - stopWatchWidgets();
521 526 vm.widgets.sort(function (widget1, widget2) {
522 527 var row1 = widgetOrder(widget1);
523 528 var row2 = widgetOrder(widget2);
524 529 var res = row1 - row2;
525 530 if (res === 0) {
526   - res = widget1.col - widget2.col;
  531 + res = widgetCol(widget1) - widgetCol(widget2);
527 532 }
528 533 return res;
529 534 });
530   - vm.skipInitialWidgetsWatch = true;
531   - watchWidgets();
532 535 }
533 536
534 537 function reload() {
... ... @@ -1037,6 +1040,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $
1037 1040 $scope.gridsterScopeWatcher = null;
1038 1041 var gridsterScope = gridsterElement.scope();
1039 1042 vm.gridster = gridsterScope.gridster;
  1043 + setupGridster(vm.gridster);
1040 1044 if (vm.onInit) {
1041 1045 vm.onInit({dashboard: vm});
1042 1046 }
... ... @@ -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 1062 function loading() {
1050 1063 return !vm.ignoreLoading && $rootScope.loading;
1051 1064 }
... ...