diff options
Diffstat (limited to 'js/vendor/angular-ui/modules/directives/event')
-rw-r--r-- | js/vendor/angular-ui/modules/directives/event/event.js | 27 | ||||
-rw-r--r-- | js/vendor/angular-ui/modules/directives/event/test/eventSpec.js | 79 |
2 files changed, 106 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/directives/event/event.js b/js/vendor/angular-ui/modules/directives/event/event.js new file mode 100644 index 000000000..4373baf76 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/event/event.js @@ -0,0 +1,27 @@ +/** + * General-purpose Event binding. Bind any event not natively supported by Angular + * Pass an object with keynames for events to ui-event + * Allows $event object and $params object to be passed + * + * @example <input ui-event="{ focus : 'counter++', blur : 'someCallback()' }"> + * @example <input ui-event="{ myCustomEvent : 'myEventHandler($event, $params)'}"> + * + * @param ui-event {string|object literal} The event to bind to as a string or a hash of events with their callbacks + */ +angular.module('ui.directives').directive('uiEvent', ['$parse', + function ($parse) { + return function (scope, elm, attrs) { + var events = scope.$eval(attrs.uiEvent); + angular.forEach(events, function (uiEvent, eventName) { + var fn = $parse(uiEvent); + elm.bind(eventName, function (evt) { + var params = Array.prototype.slice.call(arguments); + //Take out first paramater (event object); + params = params.splice(1); + scope.$apply(function () { + fn(scope, {$event: evt, $params: params}); + }); + }); + }); + }; + }]); diff --git a/js/vendor/angular-ui/modules/directives/event/test/eventSpec.js b/js/vendor/angular-ui/modules/directives/event/test/eventSpec.js new file mode 100644 index 000000000..53865f8c5 --- /dev/null +++ b/js/vendor/angular-ui/modules/directives/event/test/eventSpec.js @@ -0,0 +1,79 @@ +describe('uiEvent', function () { + var $scope, $rootScope, $compile; + + beforeEach(module('ui.directives')); + beforeEach(inject(function (_$rootScope_, _$compile_) { + $compile = _$compile_; + $rootScope = _$rootScope_; + })); + + //helper for creating event elements + function eventElement(scope, eventObject) { + scope._uiEvent = eventObject || {}; + return $compile('<span ui-event="_uiEvent">')(scope); + } + + describe('test', function () { + it('should work with dblclick event and assignment', function () { + $scope = $rootScope.$new(); + var elm = eventElement($scope, {'dblclick': 'dbl=true'}); + expect($scope.dbl).toBeUndefined(); + elm.trigger('dblclick'); + expect($scope.dbl).toBe(true); + }); + + it('should work with two events in one key a function', function () { + $scope = $rootScope.$new(); + $scope.counter = 0; + $scope.myfn = function () { + $scope.counter++; + }; + var elm = eventElement($scope, {'keyup mouseenter': 'myfn()'}); + elm.trigger('keyup'); + elm.trigger('mouseenter'); + expect($scope.counter).toBe(2); + }); + + it('should work work with multiple entries', function () { + $scope = $rootScope.$new(); + $scope.amount = 5; + var elm = eventElement($scope, { + 'click': 'amount=amount*2', + 'mouseenter': 'amount=amount*4', + 'keyup': 'amount=amount*3' + }); + elm.trigger('click'); + expect($scope.amount).toBe(10); + elm.trigger('mouseenter'); + expect($scope.amount).toBe(40); + elm.trigger('keyup'); + expect($scope.amount).toBe(120); + }); + + it('should allow passing of $event object', function () { + $scope = $rootScope.$new(); + $scope.clicky = function (par1, $event, par2) { + expect($event.foo).toBe('bar'); + expect(par1).toBe(1); + expect(par2).toBe(2); + }; + var elm = eventElement($scope, {'click': 'clicky(1, $event, 2)'}); + $(elm).trigger({ + type: 'click', + foo: 'bar' + }); + }); + + it('should allow passing of $params object', function () { + $scope = $rootScope.$new(); + $scope.onStuff = function ($event, $params) { + expect($event.type).toBe('stuff'); + expect($params[0]).toBe('foo'); + expect($params[1]).toBe('bar'); + }; + var elm = eventElement($scope, {'stuff': 'onStuff($event, $params)'}); + $(elm).trigger('stuff', ['foo', 'bar']); + }); + }); + +});
\ No newline at end of file |