This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(ngTouch): override mouse event directives #9724
Closed
douglasduteil
wants to merge
1
commit into
angular:master
from
douglasduteil:feat-ngtouch-override-mouse-event-directives
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
/* global ngTouch: false, POINTER_EVENTS: false, getPointerEventNames: false */ | ||
|
||
/* | ||
* A collection of directives that allows creation of custom event handlers that are defined as | ||
* angular expressions and are compiled and executed within the current scope. | ||
*/ | ||
var ngTouchEventDirectives = {}; | ||
|
||
|
||
// Duplicate from the ng module... | ||
var forceAsyncEvents = { | ||
'blur': true, | ||
'focus': true | ||
}; | ||
|
||
// Duplicated from jqLite. | ||
var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g; | ||
var MOZ_HACK_REGEXP = /^moz([A-Z])/; | ||
/** | ||
* Converts snake_case to camelCase. | ||
* Also there is special case for Moz prefix starting with upper case letter. | ||
* @param name Name to normalize | ||
*/ | ||
function camelCase(name) { | ||
return name. | ||
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) { | ||
return offset ? letter.toUpperCase() : letter; | ||
}). | ||
replace(MOZ_HACK_REGEXP, 'Moz$1'); | ||
} | ||
|
||
angular.forEach(Object.keys(POINTER_EVENTS.mouse), | ||
function (eventType) { | ||
|
||
var eventName = POINTER_EVENTS.mouse[eventType]; | ||
var directiveName = camelCase('ng-' + eventName); | ||
|
||
ngTouch.config(['$provide', function ($provide) { | ||
$provide.decorator(directiveName + 'Directive', ['$delegate', function ($delegate) { | ||
// drop the default mouse directives | ||
$delegate.shift(); | ||
return $delegate; | ||
}]); | ||
}]); | ||
|
||
ngTouchEventDirectives[directiveName] = ['$parse', '$rootScope', function ($parse, $rootScope) { | ||
return { | ||
restrict: 'A', | ||
compile: function ($element, attr) { | ||
var fn = $parse(attr[directiveName]); | ||
return function ngEventHandler(scope, element) { | ||
// | ||
element.on(getPointerEventNames(eventType), function (event) { | ||
var callback = function () { | ||
fn(scope, {$event: event}); | ||
}; | ||
if (forceAsyncEvents[eventName] && $rootScope.$$phase) { | ||
scope.$evalAsync(callback); | ||
} else { | ||
scope.$apply(callback); | ||
} | ||
}); | ||
|
||
}; | ||
} | ||
}; | ||
}]; | ||
} | ||
); | ||
|
||
ngTouch.directive(ngTouchEventDirectives); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
describe('ngMousedown (touch)', function() { | ||
var element; | ||
|
||
beforeEach(function() { | ||
module('ngTouch'); | ||
}); | ||
|
||
afterEach(function() { | ||
dealoc(element); | ||
}); | ||
|
||
it('should pass event object on mousedown', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mousedown="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'mousedown'); | ||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
|
||
it('should pass event object on touchstart too', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mousedown="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'touchstart'); | ||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
}); | ||
|
||
|
||
describe('ngMousemove (touch)', function() { | ||
var element; | ||
|
||
beforeEach(function() { | ||
module('ngTouch'); | ||
}); | ||
|
||
afterEach(function() { | ||
dealoc(element); | ||
}); | ||
|
||
it('should pass event object on mousemove', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mousemove="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'mousemove'); | ||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
|
||
it('should pass event object on touchstart too', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mousemove="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'mousemove'); | ||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
}); | ||
|
||
describe('ngMouseup (touch)', function() { | ||
var element; | ||
|
||
beforeEach(function() { | ||
module('ngTouch'); | ||
}); | ||
|
||
afterEach(function() { | ||
dealoc(element); | ||
}); | ||
|
||
it('should pass event object on mouseup', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mouseup="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'mouseup'); | ||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
|
||
it('should pass event object on touchstart too', inject(function($rootScope, $compile) { | ||
element = $compile('<div ng-mouseup="event = $event"></div>')($rootScope); | ||
$rootScope.$digest(); | ||
|
||
browserTrigger(element, 'mouseup'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this trigger a |
||
expect($rootScope.event).toBeDefined(); | ||
})); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be a
touchstart
event ?