Skip to content

Commit

Permalink
Add a check to validate the recognizers configuration for the directi…
Browse files Browse the repository at this point in the history
…ves.
  • Loading branch information
josketres committed Apr 24, 2015
1 parent a26c690 commit 3391e06
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
7 changes: 6 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "angular-gestures",
"description": "AngularJS directive that adds support for multi touch gestures to your app. Based on hammer.js.",
"version": "0.3.0",
"main": ["dist/gestures.min.js"],
"main": [
"dist/gestures.min.js"
],
"homepage": "http://github.com/wzr1337/angular-gestures",
"repository": {
"type": "git",
Expand All @@ -14,5 +16,8 @@
"dependencies": {
"angular": ">=1.2.0 <=1.4.0",
"hammerjs": "~2.0.0"
},
"devDependencies": {
"angular-mocks": ">=1.2.0 <=1.4.0"
}
}
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'components/**/*.min.js', // dependecies
'components/angular-mocks/angular-mocks.js', // dependecies
'src/**/*.js',
'test/**/*.Spec.js'
],
Expand Down
49 changes: 49 additions & 0 deletions src/gestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ var HGESTURES = {
hmTransformend: 'transformend'
};

var HRECOGNIZERS = {
hmDoubleTap: [Hammer.Tap, 'Hammer.Tap'],
hmDragstart: [Hammer.Pan, 'Hammer.Pan'],
hmDrag: [Hammer.Pan, 'Hammer.Pan'],
hmDragUp: [Hammer.Pan, 'Hammer.Pan'],
hmDragDown: [Hammer.Pan, 'Hammer.Pan'],
hmDragLeft: [Hammer.Pan, 'Hammer.Pan'],
hmDragRight: [Hammer.Pan, 'Hammer.Pan'],
hmDragend: [Hammer.Pan, 'Hammer.Pan'],
hmPanstart: [Hammer.Pan, 'Hammer.Pan'],
hmPan: [Hammer.Pan, 'Hammer.Pan'],
hmPanUp: [Hammer.Pan, 'Hammer.Pan'],
hmPanDown: [Hammer.Pan, 'Hammer.Pan'],
hmPanLeft: [Hammer.Pan, 'Hammer.Pan'],
hmPanRight: [Hammer.Pan, 'Hammer.Pan'],
hmPanend: [Hammer.Pan, 'Hammer.Pan'],
hmHold: [Hammer.Press, 'Hammer.Press'],
hmPinch: [Hammer.Pinch, 'Hammer.Pinch'],
hmPinchIn: [Hammer.Pinch, 'Hammer.Pinch'],
hmPinchOut: [Hammer.Pinch, 'Hammer.Pinch'],
hmPress: [Hammer.Press, 'Hammer.Press'],
hmRotate: [Hammer.Rotate, 'Hammer.Rotate'],
hmSwipe: [Hammer.Swipe, 'Hammer.Swipe'],
hmSwipeUp: [Hammer.Swipe, 'Hammer.Swipe'],
hmSwipeDown: [Hammer.Swipe, 'Hammer.Swipe'],
hmSwipeLeft: [Hammer.Swipe, 'Hammer.Swipe'],
hmSwipeRight: [Hammer.Swipe, 'Hammer.Swipe'],
hmTap: [Hammer.Tap, 'Hammer.Tap']
};

var VERBOSE = false;

angular.forEach(HGESTURES, function(eventName, directiveName) {
Expand All @@ -64,6 +94,25 @@ angular.forEach(HGESTURES, function(eventName, directiveName) {
angular.extend(defaultOpts, opts);

if (angular.isUndefined(element.hammertime)) {

// validate that needed recognizer is enabled
var recognizers = angular.isDefined(defaultOpts.recognizers) ? defaultOpts.recognizers : [];
var recognizer = HRECOGNIZERS[directiveName];
if(angular.isDefined(recognizer)) {
var enabled = false;
angular.forEach(recognizers, function(r) {
if (recognizer[0] == r[0]) {
if (angular.isUndefined(r[1].enable) || r[1].enable == true) {
enabled = true;
}
}
});
if (!enabled) {
throw new Error("Directive " + directiveName + " requires gesture recognizer ["
+ recognizer[1] + "] to be enabled");
}
}

element.hammer = new Hammer.Manager(element[0], defaultOpts);
scope.$on('$destroy', function() {
element.hammer.off(eventName);
Expand Down
60 changes: 57 additions & 3 deletions test/gestures.Spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
describe("A suite", function() {
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
it("contains spec with an expectation", function() {
expect(true).toBe(true);
});
});

describe("Gesture recognizers validation", function() {

var $compile,
$rootScope;

beforeEach(module('angular-gestures', function(hammerDefaultOptsProvider) {
hammerDefaultOptsProvider.set({
recognizers: [
[Hammer.Tap, {}],
[Hammer.Pinch, {
enable: false
}],
[Hammer.Rotate, {
enable: true
}],
]
});
}));

beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it("should throw if no swipe recognizer is not configured and hmSwipe directive is used", function() {
expect(function() {
var element = $compile("<div hm-swipe='foo()'></div>")($rootScope);
$rootScope.$digest();
}).toThrow("Directive hmSwipe requires gesture recognizer [Hammer.Swipe] to be enabled");
});

it("should not throw if tap recognizer is configured and hmTap directive is used", function() {
var element = $compile("<div hm-tap='foo()'></div>")($rootScope);
$rootScope.$digest();
});

it("should throw if pinch recognizer is configured but disabled and hmPinch directive is used", function() {
expect(function() {
var element = $compile("<div hm-pinch='foo()'></div>")($rootScope);
$rootScope.$digest();
}).toThrow("Directive hmPinch requires gesture recognizer [Hammer.Pinch] to be enabled");
});

it("should not throw if rotate recognizer is configured and explicitly enabled and hmRotate directive is used", function() {
var element = $compile("<div hm-rotate='foo()'></div>")($rootScope);
$rootScope.$digest();
});

it("should not throw if hmTouch directive is used (no recognizer needed)", function() {
var element = $compile("<div hm-touch='foo()'></div>")($rootScope);
$rootScope.$digest();
});
});

0 comments on commit 3391e06

Please sign in to comment.