Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snap to grid #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@
ehthumbs.db
Thumbs.db

node_modules
node_modules
*.bak
2 changes: 1 addition & 1 deletion angular-resizable.min.js

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"homepage": "https://github.com/Reklino/angular-resizable",
"devDependencies": {
"gulp": "^3.8.11",
"gulp-minify-css": "^1.1.1",
"gulp": "^3.9.0",
"gulp-minify-css": "^1.2.0",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^1.2.0"
}
Expand Down
33 changes: 27 additions & 6 deletions src/angular-resizable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ angular.module('angularResizable', [])
rHeight: "=",
rFlex: "=",
rGrabber: "@",
rDisabled: "@"
rDisabled: "@",
rGrid: "="
},
link: function(scope, element, attr) {

Expand All @@ -37,6 +38,8 @@ angular.module('angularResizable', [])
element.addClass('resizable');

var style = window.getComputedStyle(element[0], null),
originalW,
originalH,
w,
h,
dir = scope.rDirections,
Expand All @@ -50,16 +53,26 @@ angular.module('angularResizable', [])

var updateInfo = function(e) {
info.width = false; info.height = false;
if(axis == 'x')
if(axis == 'x')
info.width = scope.rFlex ? parseInt(element[0].style.flexBasis) : parseInt(element[0].style.width);
else
info.height = scope.rFlex ? parseInt(element[0].style.flexBasis) : parseInt(element[0].style.height);
info.id = element[0].id;
info.evt = e;
}
info.originalWidth = originalW;
info.originalHeight = originalH;
};

var dragging = function(e) {
var offset = axis == 'x' ? start - e.clientX : start - e.clientY;
var offset = (axis == 'x') ? start - e.clientX : start - e.clientY;

// bind resize to grid
var gridX = scope.rGrid[0] || 1,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If r-grid isn't set on the resizable element, this logs an error. Instead maybe use:

var gridX = scope.rGrid ? scope.rGrid[0] : 1,
    gridY = scope.rGrid ? scope.rGrid[1] : 1;

gridY = scope.rGrid[1] || 1;
offset = (axis == 'x')
? Math.round(((w+offset) - w) / gridX) * gridX
: Math.round(((h+offset) - h) / gridY) * gridY;

switch(dragDir) {
case 'top':
if(scope.rFlex) { element[0].style.flexBasis = h + (offset * vy) + 'px'; }
Expand All @@ -78,6 +91,7 @@ angular.module('angularResizable', [])
else { element[0].style.width = w + (offset * vx) + 'px'; }
break;
}

updateInfo(e);
throttle(function() { scope.$emit("angular-resizable.resizing", info);});
};
Expand All @@ -93,8 +107,15 @@ angular.module('angularResizable', [])
dragDir = direction;
axis = dragDir == 'left' || dragDir == 'right' ? 'x' : 'y';
start = axis == 'x' ? e.clientX : e.clientY;
w = parseInt(style.getPropertyValue("width"));
h = parseInt(style.getPropertyValue("height"));

// IE returns different values using "style.getPropertyValue"
//w = parseInt(style.getPropertyValue("width"));
//h = parseInt(style.getPropertyValue("height"));
var elRect = element[0].getBoundingClientRect();
w = parseInt(elRect.width);
h = parseInt(elRect.height);
originalW = w;
originalH = h;

//prevent transition while dragging
element.addClass('no-transition');
Expand Down