Showing
1 changed file
with
55 additions
and
39 deletions
... | ... | @@ -234,54 +234,68 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ |
234 | 234 | removeResizeListener(gridsterParent[0], onGirdsterParentResize); // eslint-disable-line no-undef |
235 | 235 | }); |
236 | 236 | |
237 | + watchWidgets(); | |
238 | + | |
237 | 239 | function onGirdsterParentResize() { |
238 | 240 | if (gridsterParent.height() && autofillHeight()) { |
239 | 241 | updateMobileOpts(); |
240 | 242 | } |
241 | 243 | } |
242 | 244 | |
243 | - $scope.$watchCollection('vm.widgets', function () { | |
244 | - sortWidgets(); | |
245 | - var ids = []; | |
246 | - for (var i=0;i<vm.widgets.length;i++) { | |
247 | - var widget = vm.widgets[i]; | |
248 | - if (!widget.id) { | |
249 | - widget.id = utils.guid(); | |
250 | - } | |
251 | - ids.push(widget.id); | |
252 | - var layoutInfoObject = vm.widgetLayoutInfo[widget.id]; | |
253 | - if (!layoutInfoObject) { | |
254 | - layoutInfoObject = { | |
255 | - widget: widget | |
256 | - }; | |
257 | - Object.defineProperty(layoutInfoObject, 'sizeX', { | |
258 | - get: function() { return widgetSizeX(this.widget) }, | |
259 | - set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)} | |
260 | - }); | |
261 | - Object.defineProperty(layoutInfoObject, 'sizeY', { | |
262 | - get: function() { return widgetSizeY(this.widget) }, | |
263 | - set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)} | |
264 | - }); | |
265 | - Object.defineProperty(layoutInfoObject, 'row', { | |
266 | - get: function() { return widgetRow(this.widget) }, | |
267 | - set: function(newRow) { setWidgetRow(this.widget, newRow)} | |
268 | - }); | |
269 | - Object.defineProperty(layoutInfoObject, 'col', { | |
270 | - get: function() { return widgetCol(this.widget) }, | |
271 | - set: function(newCol) { setWidgetCol(this.widget, newCol)} | |
272 | - }); | |
273 | - vm.widgetLayoutInfo[widget.id] = layoutInfoObject; | |
245 | + function watchWidgets() { | |
246 | + $scope.widgetsCollectionWatch = $scope.$watchCollection('vm.widgets', function () { | |
247 | + var ids = []; | |
248 | + for (var i=0;i<vm.widgets.length;i++) { | |
249 | + var widget = vm.widgets[i]; | |
250 | + if (!widget.id) { | |
251 | + widget.id = utils.guid(); | |
252 | + } | |
253 | + ids.push(widget.id); | |
254 | + var layoutInfoObject = vm.widgetLayoutInfo[widget.id]; | |
255 | + if (!layoutInfoObject) { | |
256 | + layoutInfoObject = { | |
257 | + widget: widget | |
258 | + }; | |
259 | + Object.defineProperty(layoutInfoObject, 'sizeX', { | |
260 | + get: function() { return widgetSizeX(this.widget) }, | |
261 | + set: function(newSizeX) { setWidgetSizeX(this.widget, newSizeX)} | |
262 | + }); | |
263 | + Object.defineProperty(layoutInfoObject, 'sizeY', { | |
264 | + get: function() { return widgetSizeY(this.widget) }, | |
265 | + set: function(newSizeY) { setWidgetSizeY(this.widget, newSizeY)} | |
266 | + }); | |
267 | + Object.defineProperty(layoutInfoObject, 'row', { | |
268 | + get: function() { return widgetRow(this.widget) }, | |
269 | + set: function(newRow) { setWidgetRow(this.widget, newRow)} | |
270 | + }); | |
271 | + Object.defineProperty(layoutInfoObject, 'col', { | |
272 | + get: function() { return widgetCol(this.widget) }, | |
273 | + set: function(newCol) { setWidgetCol(this.widget, newCol)} | |
274 | + }); | |
275 | + vm.widgetLayoutInfo[widget.id] = layoutInfoObject; | |
276 | + } | |
274 | 277 | } |
275 | - } | |
276 | - for (var widgetId in vm.widgetLayoutInfo) { | |
277 | - if (ids.indexOf(widgetId) === -1) { | |
278 | - delete vm.widgetLayoutInfo[widgetId]; | |
278 | + for (var widgetId in vm.widgetLayoutInfo) { | |
279 | + if (ids.indexOf(widgetId) === -1) { | |
280 | + delete vm.widgetLayoutInfo[widgetId]; | |
281 | + } | |
279 | 282 | } |
283 | + $mdUtil.nextTick(function () { | |
284 | + sortWidgets(); | |
285 | + if (autofillHeight()) { | |
286 | + updateMobileOpts(); | |
287 | + } | |
288 | + }); | |
289 | + }); | |
290 | + } | |
291 | + | |
292 | + function stopWatchWidgets() { | |
293 | + if ($scope.widgetsCollectionWatch) { | |
294 | + $scope.widgetsCollectionWatch(); | |
295 | + $scope.widgetsCollectionWatch = null; | |
280 | 296 | } |
281 | - if (autofillHeight()) { | |
282 | - updateMobileOpts(); | |
283 | - } | |
284 | - }); | |
297 | + } | |
298 | + | |
285 | 299 | |
286 | 300 | //TODO: widgets visibility |
287 | 301 | /*gridsterParent.scroll(function () { |
... | ... | @@ -500,6 +514,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ |
500 | 514 | loadDashboard(); |
501 | 515 | |
502 | 516 | function sortWidgets() { |
517 | + stopWatchWidgets(); | |
503 | 518 | vm.widgets.sort(function (widget1, widget2) { |
504 | 519 | var row1 = widgetOrder(widget1); |
505 | 520 | var row2 = widgetOrder(widget2); |
... | ... | @@ -509,6 +524,7 @@ function DashboardController($scope, $rootScope, $element, $timeout, $mdMedia, $ |
509 | 524 | } |
510 | 525 | return res; |
511 | 526 | }); |
527 | + watchWidgets(); | |
512 | 528 | } |
513 | 529 | |
514 | 530 | function reload() { | ... | ... |