|
| 1 | +/** |
| 2 | + * @ngdoc object |
| 3 | + * @name ng.$animationProvider |
| 4 | + * @description |
| 5 | + * |
| 6 | + * The $AnimationProvider provider allows developers to register and access custom JavaScript animations directly inside |
| 7 | + * of a module. |
| 8 | + * |
| 9 | + */ |
| 10 | +$AnimationProvider.$inject = ['$provide']; |
| 11 | +function $AnimationProvider($provide) { |
| 12 | + var suffix = 'Animation'; |
| 13 | + |
| 14 | + /** |
| 15 | + * @ngdoc function |
| 16 | + * @name ng.$animation#register |
| 17 | + * @methodOf ng.$animationProvider |
| 18 | + * |
| 19 | + * @description |
| 20 | + * Registers a new injectable animation factory function. The factory function produces the animation object which |
| 21 | + * has these two properties: |
| 22 | + * |
| 23 | + * * `setup`: `function(Element):*` A function which receives the starting state of the element. The purpose |
| 24 | + * of this function is to get the element ready for animation. Optionally the function returns an memento which |
| 25 | + * is passed to the `start` function. |
| 26 | + * * `start`: `function(Element, doneFunction, *)` The element to animate, the `doneFunction` to be called on |
| 27 | + * element animation completion, and an optional memento from the `setup` function. |
| 28 | + * |
| 29 | + * @param {string} name The name of the animation. |
| 30 | + * @param {function} factory The factory function that will be executed to return the animation object. |
| 31 | + * |
| 32 | + */ |
| 33 | + this.register = function(name, factory) { |
| 34 | + $provide.factory(camelCase(name) + suffix, factory); |
| 35 | + }; |
| 36 | + |
| 37 | + this.$get = ['$injector', function($injector) { |
| 38 | + /** |
| 39 | + * @ngdoc function |
| 40 | + * @name ng.$animation |
| 41 | + * @function |
| 42 | + * |
| 43 | + * @description |
| 44 | + * The $animation service is used to retrieve any defined animation functions. When executed, the $animation service |
| 45 | + * will return a object that contains the setup and start functions that were defined for the animation. |
| 46 | + * |
| 47 | + * @param {String} name Name of the animation function to retrieve. Animation functions are registered and stored |
| 48 | + * inside of the AngularJS DI so a call to $animate('custom') is the same as injecting `customAnimation` |
| 49 | + * via dependency injection. |
| 50 | + * @return {Object} the animation object which contains the `setup` and `start` functions that perform the animation. |
| 51 | + */ |
| 52 | + return function $animation(name) { |
| 53 | + if (name) { |
| 54 | + try { |
| 55 | + return $injector.get(camelCase(name) + suffix); |
| 56 | + } catch (e) { |
| 57 | + //TODO(misko): this is a hack! we should have a better way to test if the injector has a given key. |
| 58 | + // The issue is that the animations are optional, and if not present they should be silently ignored. |
| 59 | + // The proper way to fix this is to add API onto the injector so that we can ask to see if a given |
| 60 | + // animation is supported. |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + }]; |
| 65 | +}; |
0 commit comments