Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.

feat(item): add watchers to move and resize widgets when their x,y or… #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 additions & 32 deletions dist/gridstack-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ angular.module('gridstack-angular', []);
var app = angular.module('gridstack-angular');

app.controller('GridstackController', ['$scope', function($scope) {

var gridstack = null;
var self = this;

this.init = function(element, options) {
gridstack = element.gridstack(options).data('gridstack');
return gridstack;
self.gridstackHandler = element.gridstack(options).data('gridstack');
return self.gridstackHandler;
};

this.removeItem = function(element) {
if(gridstack) {
return gridstack.removeWidget(element, false);
if(self.gridstackHandler) {
return self.gridstackHandler.removeWidget(element, false);
}
return null;
};

this.addItem = function(element) {
if(gridstack) {
gridstack.makeWidget(element);
if(self.gridstackHandler) {
self.gridstackHandler.makeWidget(element);
return element;
}
return null;
};

}]);
})();

(function() {
'use strict';

Expand All @@ -48,7 +48,8 @@ app.directive('gridstack', ['$timeout', function($timeout) {
return {
restrict: 'A',
controller: 'GridstackController',
scope: {
controllerAs: '$gridstack',
bindToController: {
onChange: '&',
onDragStart: '&',
onDragStop: '&',
Expand All @@ -58,36 +59,34 @@ app.directive('gridstack', ['$timeout', function($timeout) {
options: '='
},
link: function(scope, element, attrs, controller, ngModel) {

var gridstack = controller.init(element, scope.options);
scope.gridstackHandler = gridstack;
controller.init(element, controller.options);

element.on('change', function(e, items) {
$timeout(function() {
scope.$apply();
scope.onChange({event: e, items: items});
controller.onChange({event: e, items: items});
});
});

element.on('dragstart', function(e, ui) {
scope.onDragStart({event: e, ui: ui});
controller.onDragStart({event: e, ui: ui});
});

element.on('dragstop', function(e, ui) {
$timeout(function() {
scope.$apply();
scope.onDragStop({event: e, ui: ui});
controller.onDragStop({event: e, ui: ui});
});
});

element.on('resizestart', function(e, ui) {
scope.onResizeStart({event: e, ui: ui});
controller.onResizeStart({event: e, ui: ui});
});

element.on('resizestop', function(e, ui) {
$timeout(function() {
scope.$apply();
scope.onResizeStop({event: e, ui: ui});
controller.onResizeStop({event: e, ui: ui});
});
});

Expand All @@ -106,7 +105,6 @@ app.directive('gridstackItem', ['$timeout', function($timeout) {

return {
restrict: 'A',
controller: 'GridstackController',
require: '^gridstack',
scope: {
gridstackItem: '=',
Expand All @@ -119,45 +117,60 @@ app.directive('gridstackItem', ['$timeout', function($timeout) {
gsItemHeight: '=',
gsItemAutopos: '='
},
link: function(scope, element, attrs, controller) {
link: function(scope, element, attrs, gridstackController) {
if (scope.gsItemId) {
$(element).attr('data-gs-id', scope.gsItemId);
element.attr('data-gs-id', scope.gsItemId);
}
$(element).attr('data-gs-x', scope.gsItemX);
$(element).attr('data-gs-y', scope.gsItemY);
$(element).attr('data-gs-width', scope.gsItemWidth);
$(element).attr('data-gs-height', scope.gsItemHeight);
$(element).attr('data-gs-auto-position', scope.gsItemAutopos);
var widget = controller.addItem(element);
element.attr('data-gs-x', scope.gsItemX);
element.attr('data-gs-y', scope.gsItemY);
element.attr('data-gs-width', scope.gsItemWidth);
element.attr('data-gs-height', scope.gsItemHeight);
element.attr('data-gs-auto-position', scope.gsItemAutopos);
var widget = gridstackController.addItem(element);
var item = element.data('_gridstack_node');
$timeout(function() {
scope.onItemAdded({item: item});
});

scope.$watch(function() { return $(element).attr('data-gs-id'); }, function(val) {
// Update gridstack element after scope changes
// NOTE we must only make a gridstack update call for these watchers if something changed.
// Otherwise it will cause issues with the 'change' event not firing because you ran an
// update op partway through it.
scope.$watchGroup(['gsItemX', 'gsItemY', 'gsItemWidth', 'gsItemHeight'], function() {
if (Number(element.attr('data-gs-x')) !== scope.gsItemX ||
Number(element.attr('data-gs-y')) !== scope.gsItemY ||
Number(element.attr('data-gs-width')) !== scope.gsItemWidth ||
Number(element.attr('data-gs-height')) !== scope.gsItemHeight) {
gridstackController.gridstackHandler.update(element, scope.gsItemX, scope.gsItemY,
scope.gsItemWidth, scope.gsItemHeight);
}
});

// Update scope after gridstack attributes change
scope.$watch(function() { return element.attr('data-gs-id'); }, function(val) {
scope.gsItemId = val;
});

scope.$watch(function() { return $(element).attr('data-gs-x'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-x'); }, function(val) {
scope.gsItemX = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-y'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-y'); }, function(val) {
scope.gsItemY = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-width'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-width'); }, function(val) {
scope.gsItemWidth = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-height'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-height'); }, function(val) {
scope.gsItemHeight = Number(val);
});

element.bind('$destroy', function() {
var item = element.data('_gridstack_node');
scope.onItemRemoved({item: item});
controller.removeItem(element);
gridstackController.removeItem(element);
});

}
Expand Down
6 changes: 3 additions & 3 deletions dist/gridstack-angular.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions src/gridstack.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ angular.module('gridstack-angular', []);
var app = angular.module('gridstack-angular');

app.controller('GridstackController', ['$scope', function($scope) {

var gridstack = null;
var self = this;

this.init = function(element, options) {
gridstack = element.gridstack(options).data('gridstack');
return gridstack;
self.gridstackHandler = element.gridstack(options).data('gridstack');
return self.gridstackHandler;
};

this.removeItem = function(element) {
if(gridstack) {
return gridstack.removeWidget(element, false);
if(self.gridstackHandler) {
return self.gridstackHandler.removeWidget(element, false);
}
return null;
};

this.addItem = function(element) {
if(gridstack) {
gridstack.makeWidget(element);
if(self.gridstackHandler) {
self.gridstackHandler.makeWidget(element);
return element;
}
return null;
};

}]);
})();
})();
17 changes: 8 additions & 9 deletions src/gridstack.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ app.directive('gridstack', ['$timeout', function($timeout) {
return {
restrict: 'A',
controller: 'GridstackController',
scope: {
controllerAs: '$gridstack',
bindToController: {
onChange: '&',
onDragStart: '&',
onDragStop: '&',
Expand All @@ -18,36 +19,34 @@ app.directive('gridstack', ['$timeout', function($timeout) {
options: '='
},
link: function(scope, element, attrs, controller, ngModel) {

var gridstack = controller.init(element, scope.options);
scope.gridstackHandler = gridstack;
controller.init(element, controller.options);

element.on('change', function(e, items) {
$timeout(function() {
scope.$apply();
scope.onChange({event: e, items: items});
controller.onChange({event: e, items: items});
});
});

element.on('dragstart', function(e, ui) {
scope.onDragStart({event: e, ui: ui});
controller.onDragStart({event: e, ui: ui});
});

element.on('dragstop', function(e, ui) {
$timeout(function() {
scope.$apply();
scope.onDragStop({event: e, ui: ui});
controller.onDragStop({event: e, ui: ui});
});
});

element.on('resizestart', function(e, ui) {
scope.onResizeStart({event: e, ui: ui});
controller.onResizeStart({event: e, ui: ui});
});

element.on('resizestop', function(e, ui) {
$timeout(function() {
scope.$apply();
scope.onResizeStop({event: e, ui: ui});
controller.onResizeStop({event: e, ui: ui});
});
});

Expand Down
44 changes: 29 additions & 15 deletions src/gridstackitem.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ app.directive('gridstackItem', ['$timeout', function($timeout) {

return {
restrict: 'A',
controller: 'GridstackController',
require: '^gridstack',
scope: {
gridstackItem: '=',
Expand All @@ -20,45 +19,60 @@ app.directive('gridstackItem', ['$timeout', function($timeout) {
gsItemHeight: '=',
gsItemAutopos: '='
},
link: function(scope, element, attrs, controller) {
link: function(scope, element, attrs, gridstackController) {
if (scope.gsItemId) {
$(element).attr('data-gs-id', scope.gsItemId);
element.attr('data-gs-id', scope.gsItemId);
}
$(element).attr('data-gs-x', scope.gsItemX);
$(element).attr('data-gs-y', scope.gsItemY);
$(element).attr('data-gs-width', scope.gsItemWidth);
$(element).attr('data-gs-height', scope.gsItemHeight);
$(element).attr('data-gs-auto-position', scope.gsItemAutopos);
var widget = controller.addItem(element);
element.attr('data-gs-x', scope.gsItemX);
element.attr('data-gs-y', scope.gsItemY);
element.attr('data-gs-width', scope.gsItemWidth);
element.attr('data-gs-height', scope.gsItemHeight);
element.attr('data-gs-auto-position', scope.gsItemAutopos);
var widget = gridstackController.addItem(element);
var item = element.data('_gridstack_node');
$timeout(function() {
scope.onItemAdded({item: item});
});

scope.$watch(function() { return $(element).attr('data-gs-id'); }, function(val) {
// Update gridstack element after scope changes
// NOTE we must only make a gridstack update call for these watchers if something changed.
// Otherwise it will cause issues with the 'change' event not firing because you ran an
// update op partway through it.
scope.$watchGroup(['gsItemX', 'gsItemY', 'gsItemWidth', 'gsItemHeight'], function() {
if (Number(element.attr('data-gs-x')) !== scope.gsItemX ||
Number(element.attr('data-gs-y')) !== scope.gsItemY ||
Number(element.attr('data-gs-width')) !== scope.gsItemWidth ||
Number(element.attr('data-gs-height')) !== scope.gsItemHeight) {
gridstackController.gridstackHandler.update(element, scope.gsItemX, scope.gsItemY,
scope.gsItemWidth, scope.gsItemHeight);
}
});

// Update scope after gridstack attributes change
scope.$watch(function() { return element.attr('data-gs-id'); }, function(val) {
scope.gsItemId = val;
});

scope.$watch(function() { return $(element).attr('data-gs-x'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-x'); }, function(val) {
scope.gsItemX = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-y'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-y'); }, function(val) {
scope.gsItemY = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-width'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-width'); }, function(val) {
scope.gsItemWidth = Number(val);
});

scope.$watch(function() { return $(element).attr('data-gs-height'); }, function(val) {
scope.$watch(function() { return element.attr('data-gs-height'); }, function(val) {
scope.gsItemHeight = Number(val);
});

element.bind('$destroy', function() {
var item = element.data('_gridstack_node');
scope.onItemRemoved({item: item});
controller.removeItem(element);
gridstackController.removeItem(element);
});

}
Expand Down