Skip to content

Commit 5705c3f

Browse files
chrisswainejellekralt
authored andcommitted
Toggle via attribute (#5)
* Toggle functionality based on attribute value * Updated Readme
1 parent 11343d9 commit 5705c3f

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ Lightweight drag to scroll directive for AngularJS
1919
angular.module('MyApp', ['ng-drag-scroll']);
2020
```
2121

22-
* Add an attribute to an element with a scrollbar to make the content scrollable
22+
* Add an attribute set to true to an element with a scrollbar to make the content scrollable, setting the attribute to false will prevent drag to scroll behaviour.
23+
The attribute value can be set to a scope varibale to easily toggle between enabled and disabled.
2324

2425
```html
25-
<div drag-scroll>
26+
<div drag-scroll="true">
2627
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.</p>
2728
</div>
2829
```

src/ng-drag-scroll.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
//Usage:
1313
//<div drag-scroll>Lorem ipsum dolor sit amet</div>
1414
var directive = {
15-
scope: false,
15+
scope:{
16+
dragScroll:'='
17+
},
1618
restrict: 'A',
1719
link: function($scope, $element, $attributes, vm) {
1820
var allowedClickOffset = 5;
@@ -53,70 +55,78 @@
5355
* @param {object} e MouseDown event
5456
*/
5557
function handleMouseDown (e) {
56-
for (var i= 0; i<excludedClasses.length; i++) {
57-
if (angular.element(e.target).hasClass(excludedClasses[i])) {
58-
return false;
58+
if($scope.dragScroll){
59+
for (var i= 0; i<excludedClasses.length; i++) {
60+
if (angular.element(e.target).hasClass(excludedClasses[i])) {
61+
return false;
62+
}
5963
}
60-
}
6164

62-
$scope.$apply(function() {
63-
onDragStart($scope);
64-
});
65+
$scope.$apply(function() {
66+
onDragStart($scope);
67+
});
6568

66-
// Set mouse drag listeners
67-
setDragListeners();
69+
// Set mouse drag listeners
70+
setDragListeners();
6871

69-
// Set 'pushed' state
70-
pushed = true;
71-
lastClientX = startClientX = e.clientX;
72-
lastClientY = startClientY = e.clientY;
72+
// Set 'pushed' state
73+
pushed = true;
74+
lastClientX = startClientX = e.clientX;
75+
lastClientY = startClientY = e.clientY;
7376

74-
clearSelection();
77+
clearSelection();
7578

76-
e.preventDefault();
77-
e.stopPropagation();
79+
e.preventDefault();
80+
e.stopPropagation();
81+
}
82+
7883
}
7984

8085
/**
8186
* Handles mouseup event
8287
* @param {object} e MouseUp event
8388
*/
8489
function handleMouseUp (e) {
85-
var selectable = ('drag-scroll-text' in e.target.attributes);
86-
var withinXConstraints = (e.clientX >= (startClientX - allowedClickOffset) && e.clientX <= (startClientX + allowedClickOffset));
87-
var withinYConstraints = (e.clientY >= (startClientY - allowedClickOffset) && e.clientY <= (startClientY + allowedClickOffset));
90+
if($scope.dragScroll){
91+
var selectable = ('drag-scroll-text' in e.target.attributes);
92+
var withinXConstraints = (e.clientX >= (startClientX - allowedClickOffset) && e.clientX <= (startClientX + allowedClickOffset));
93+
var withinYConstraints = (e.clientY >= (startClientY - allowedClickOffset) && e.clientY <= (startClientY + allowedClickOffset));
94+
95+
// Set 'pushed' state
96+
pushed = false;
97+
98+
// Check if cursor falls within X and Y axis constraints
99+
if(selectable && withinXConstraints && withinYConstraints) {
100+
// If so, select the text inside the target element
101+
selectText(e.target);
102+
}
88103

89-
// Set 'pushed' state
90-
pushed = false;
104+
$scope.$apply(function() {
105+
onDragEnd($scope);
106+
});
91107

92-
// Check if cursor falls within X and Y axis constraints
93-
if(selectable && withinXConstraints && withinYConstraints) {
94-
// If so, select the text inside the target element
95-
selectText(e.target);
108+
removeDragListeners();
96109
}
97-
98-
$scope.$apply(function() {
99-
onDragEnd($scope);
100-
});
101-
102-
removeDragListeners();
110+
103111
}
104112

105113
/**
106114
* Handles mousemove event
107115
* @param {object} e MouseMove event
108116
*/
109117
function handleMouseMove (e) {
110-
if (pushed) {
111-
if(!axis || axis === 'x') {
112-
$element[0].scrollLeft -= (-lastClientX + (lastClientX = e.clientX));
113-
}
114-
if(!axis || axis === 'y') {
115-
$element[0].scrollTop -= (-lastClientY + (lastClientY = e.clientY));
118+
if($scope.dragScroll){
119+
if (pushed) {
120+
if(!axis || axis === 'x') {
121+
$element[0].scrollLeft -= (-lastClientX + (lastClientX = e.clientX));
122+
}
123+
if(!axis || axis === 'y') {
124+
$element[0].scrollTop -= (-lastClientY + (lastClientY = e.clientY));
125+
}
116126
}
117-
}
118127

119-
e.preventDefault();
128+
e.preventDefault();
129+
}
120130
}
121131

122132
/**

0 commit comments

Comments
 (0)