Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / foundation-apps / js / angular / services / foundation.dynamicRouting.animations.js
1 (function() {
2   'use strict';
3
4   angular.module('foundation.dynamicRouting.animations', ['foundation.dynamicRouting'])
5     .directive('uiView', uiView)
6   ;
7
8   uiView.$inject = ['$rootScope', '$state'];
9
10   function uiView($rootScope, $state) {
11     var directive = {
12       restrict : 'ECA',
13       priority : -400,
14       link     : link
15     };
16
17     return directive;
18
19     function link(scope, element) {
20       var animation = {};
21       var animationEnded = false;
22       var presetHeight;
23
24       var cleanup = [
25         $rootScope.$on('$stateChangeStart', onStateChangeStart),
26         $rootScope.$on('$stateChangeError', onStateChangeError),
27         scope.$on('$stateChangeSuccess', onStateChangeSuccess),
28         scope.$on('$viewContentAnimationEnded', onViewContentAnimationEnded)
29       ];
30
31       var destroyed = scope.$on('$destroy', function onDestroy() {
32         angular.forEach(cleanup, function (cb) {
33           if (angular.isFunction(cb)) {
34             cb();
35           }
36         });
37
38         destroyed();
39       });
40
41       function onStateChangeStart(event, toState, toParams, fromState, fromParams) {
42
43         if (fromState.animation) {
44           if (!fromState.animation.leave && !toState.animation.leave) {
45             return;
46           }
47           else {
48              animationRouter(event, toState, fromState);
49           }
50         }
51       }
52
53       function animationRouter(event, toState, fromState) {
54         if (!animationEnded) {
55           resetParent();
56           prepareParent();
57
58           element.removeClass(fromState.animation.leave);
59         }
60         else {
61           prepareParent();
62
63           element.addClass(fromState.animation.leave);
64         }
65
66       }
67
68       function onStateChangeError() {
69         if(animation.leave) {
70           element.removeClass(animation.leave);
71         }
72
73         resetParent(); //reset parent if state change fails
74       }
75
76       function onStateChangeSuccess() {
77         resetParent();
78         if ($state.includes(getState()) && animation.enter) {
79           element.addClass(animation.enter);
80         }
81       }
82
83       function onViewContentAnimationEnded(event) {
84         if (event.targetScope === scope && animation.enter) {
85           element.removeClass(animation.enter);
86         }
87         
88         animationEnded = true;
89
90       }
91
92       function getState() {
93         var view  = element.data('$uiView');
94         var state = view && view.state && view.state.self;
95
96         if (state) {
97           angular.extend(animation, state.animation);
98         }
99
100         return state;
101       }
102
103       function resetParent() {
104         element.parent().removeClass('position-absolute');
105         if(presetHeight !== true) {
106           element.parent()[0].style.height = null;
107         }
108       }
109
110       function prepareParent() {
111         var parentHeight = parseInt(element.parent()[0].style.height);
112         var elHeight = parseInt(window.getComputedStyle(element[0], null).getPropertyValue('height'));
113         var tempHeight = parentHeight > 0 ? parentHeight : elHeight > 0 ? elHeight : '';
114
115         if(parentHeight > 0) {
116           presetHeight = true;
117         }
118
119         element.parent()[0].style.height = tempHeight + 'px';
120         element.parent().addClass('position-absolute');
121       }
122     }
123   }
124
125 })();