Skip to content

Commit

Permalink
feat(transition): Add transition service
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Dec 17, 2012
1 parent 7241dbd commit 1455507
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/transition/test/transition.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
describe('$transition', function() {
var $transition;

beforeEach(module('ui.bootstrap.transition'));

beforeEach(inject(function(_$transition_) {
$transition = _$transition_;
}));

it('returns a promise', function() {
var element = angular.element('<div></div>');
expect($transition(element, '').then).toBeDefined();
});

it('changes the css if passed a string', function() {
var element = angular.element('<div></div>');
spyOn(element, 'addClass');
$transition(element, 'triggerClass');
expect(element.addClass).toHaveBeenCalledWith('triggerClass');
});

it('changes the style if passed an object', function() {
var element = angular.element('<div></div>');
var triggerStyle = { height: '11px' };
spyOn(element, 'css');
$transition(element, triggerStyle);
expect(element.css).toHaveBeenCalledWith(triggerStyle);
});

it('calls the function if passed', function() {
var element = angular.element('<div></div>');
var triggerFunction = jasmine.createSpy('triggerFunction');
$transition(element, triggerFunction);
expect(triggerFunction).toHaveBeenCalledWith(element);
});

it('binds a transitionEnd handler to the element', function() {
var element = angular.element('<div></div>');
spyOn(element, 'bind');
$transition(element, '');
expect(element.bind).toHaveBeenCalledWith($transition.transitionEndEventName, jasmine.any(Function));
});


describe('transitionEndEventName', function() {
it('should be a string if it is defined', function() {
if ( $transition.transitionEndEventName ) {
expect($transition.transitionEndEventName).toMatch(/transitionend$/i);
}
});
});

});

40 changes: 40 additions & 0 deletions src/transition/transition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
angular.module('ui.bootstrap.transition', [])

.factory('$transition', ['$q', function($q) {

var $transition = function(element, trigger) {
if ( !$transition.transitionEndEventName ) {
return;
}
var deferred = $q.defer();
var transitionEndHandler = function(event) {
element.unbind($transition.transitionEndEventName, transitionEndHandler);
deferred.resolve(element);
};
element.bind($transition.transitionEndEventName, transitionEndHandler);
if ( angular.isString(trigger) ) {
element.addClass(trigger);
} else if ( angular.isFunction(trigger) ) {
trigger(element);
} else if ( angular.isObject(trigger) ) {
element.css(trigger);
}
return deferred.promise;
};

// Work out the name of the transitionEnd event
var transElement = document.createElement('trans');
var transitionEndEventNames = {
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'transitionend',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd',
'transition': 'transitionend'
};
for (var name in transitionEndEventNames){
if (transElement.style[name] !== undefined) {
$transition.transitionEndEventName = transitionEndEventNames[name];
}
}
return $transition;
}]);

0 comments on commit 1455507

Please sign in to comment.