Skip to content
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
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = space
charset = utf-8

[*.js]
indent_size = 4

[*.css]
indent_size = 2

9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
Expand All @@ -19,13 +19,13 @@
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
Expand All @@ -36,4 +36,5 @@
ehthumbs.db
Thumbs.db

node_modules
node_modules
.idea
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.

2 changes: 2 additions & 0 deletions src/angular-resizable.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.resizable {
position: relative;
}

.resizable.no-transition {
transition: none !important;
}
Expand All @@ -18,6 +19,7 @@
user-select: none;
background: transparent;
}

.rg-right span, .rg-left span, .rg-top span, .rg-bottom span {
position: absolute;
box-sizing: border-box;
Expand Down
79 changes: 51 additions & 28 deletions src/angular-resizable.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
angular.module('angularResizable', [])
.directive('resizable', function() {
(function () {
'use strict';

angular.module('angularResizable', [])
.directive('resizable', resizable);

function resizable () {
var toCall;
function throttle(fun) {

function throttle (fun) {
if (toCall === undefined) {
toCall = fun;
setTimeout(function() {
setTimeout(function () {
toCall();
toCall = undefined;
}, 100);
} else {
toCall = fun;
}
}

return {
restrict: 'AE',
scope: {
Expand All @@ -23,18 +30,22 @@ angular.module('angularResizable', [])
rFlex: '=',
rGrabber: '@',
rDisabled: '@',
rNoThrottle: '='
rNoThrottle: '=',
rMinWidth: '=',
rMinHeight: '=',
rMaxWidth: '=',
rMaxHeight: '='
},
link: function(scope, element, attr) {
link: function (scope, element, attr) {
var flexBasis = 'flexBasis' in document.documentElement.style ? 'flexBasis' :
'webkitFlexBasis' in document.documentElement.style ? 'webkitFlexBasis' :
'msFlexPreferredSize' in document.documentElement.style ? 'msFlexPreferredSize' : 'flexBasis';

// register watchers on width and height attributes if they are set
scope.$watch('rWidth', function(value){
scope.$watch('rWidth', function (value) {
element[0].style[scope.rFlex ? flexBasis : 'width'] = scope.rWidth + 'px';
});
scope.$watch('rHeight', function(value){
scope.$watch('rHeight', function (value) {
element[0].style[scope.rFlex ? flexBasis : 'height'] = scope.rHeight + 'px';
});

Expand All @@ -52,55 +63,63 @@ angular.module('angularResizable', [])
axis,
info = {};

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

var getClientX = function(e) {
var getClientX = function (e) {
return e.touches ? e.touches[0].clientX : e.clientX;
};

var getClientY = function(e) {
var getClientY = function (e) {
return e.touches ? e.touches[0].clientY : e.clientY;
};

var dragging = function(e) {
var prop, offset = axis === 'x' ? start - getClientX(e) : start - getClientY(e);
switch(dragDir) {
var dragging = function (e) {
var prop, newHeight, newWidth, offset = axis === 'x' ? start - getClientX(e) : start - getClientY(e);
switch (dragDir) {
case 'top':
newHeight = h + (offset * vy);
prop = scope.rFlex ? flexBasis : 'height';
element[0].style[prop] = h + (offset * vy) + 'px';
element[0].style[prop] = (scope.rMaxHeight && scope.rMaxHeight < newHeight) ? scope.rMaxHeight : newHeight + 'px';
break;
case 'bottom':
newHeight = h - (offset * vy);
prop = scope.rFlex ? flexBasis : 'height';
element[0].style[prop] = h - (offset * vy) + 'px';
element[0].style[prop] = (scope.rMinHeight && scope.rMinHeight > newHeight) ? scope.rMinHeight : newHeight + 'px';
break;
case 'right':
newWidth = w - (offset * vx);
prop = scope.rFlex ? flexBasis : 'width';
element[0].style[prop] = w - (offset * vx) + 'px';
element[0].style[prop] = (scope.rMaxWidth && scope.rMaxWidth < newHeight) ? scope.rMaxWidth : newWidth + 'px';
break;
case 'left':
newWidth = w + (offset * vx);
prop = scope.rFlex ? flexBasis : 'width';
element[0].style[prop] = w + (offset * vx) + 'px';
element[0].style[prop] = (scope.rMinWidth && scope.rMinWidth > newHeight) ? scope.rMinWidth : newWidth + 'px';
break;
}
updateInfo(e);
function resizingEmit(){

function resizingEmit () {
scope.$emit('angular-resizable.resizing', info);
}

if (scope.rNoThrottle) {
resizingEmit();
} else {
throttle(resizingEmit);
}
};
var dragEnd = function(e) {

var dragEnd = function (e) {
updateInfo();
scope.$emit('angular-resizable.resizeEnd', info);
scope.$apply();
Expand All @@ -110,7 +129,8 @@ angular.module('angularResizable', [])
document.removeEventListener('touchmove', dragging, false);
element.removeClass('no-transition');
};
var dragStart = function(e, direction) {

var dragStart = function (e, direction) {
dragDir = direction;
axis = dragDir === 'left' || dragDir === 'right' ? 'x' : 'y';
start = axis === 'x' ? getClientX(e) : getClientY(e);
Expand All @@ -126,8 +146,8 @@ angular.module('angularResizable', [])
document.addEventListener('touchmove', dragging, false);

// Disable highlighting while dragging
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
if (e.preventDefault) e.preventDefault();
e.cancelBubble = true;
e.returnValue = false;

Expand All @@ -143,9 +163,11 @@ angular.module('angularResizable', [])
grabber.setAttribute('class', 'rg-' + direction);
grabber.innerHTML = inner;
element[0].appendChild(grabber);
grabber.ondragstart = function() { return false; };
grabber.ondragstart = function () {
return false;
};

var down = function(e) {
var down = function (e) {
var disabled = (scope.rDisabled === 'true');
if (!disabled && (e.which === 1 || e.touches)) {
// left mouse click or touch screen
Expand All @@ -157,4 +179,5 @@ angular.module('angularResizable', [])
});
}
};
});
}
})();