Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / foundation-apps / js / angular / components / common / common.js
1 (function() {
2   'use strict';
3
4   angular.module('foundation.common', ['foundation.core'])
5     .directive('zfClose', zfClose)
6     .directive('zfOpen', zfOpen)
7     .directive('zfToggle', zfToggle)
8     .directive('zfEscClose', zfEscClose)
9     .directive('zfSwipeClose', zfSwipeClose)
10     .directive('zfHardToggle', zfHardToggle)
11   ;
12
13   zfClose.$inject = ['FoundationApi'];
14
15   function zfClose(foundationApi) {
16     var directive = {
17       restrict: 'A',
18       link: link
19     };
20
21     return directive;
22
23     function link(scope, element, attrs) {
24       var targetId = '';
25       if (attrs.zfClose) {
26         targetId = attrs.zfClose;
27       } else {
28         var parentElement= false;
29         var tempElement = element.parent();
30         //find parent modal
31         while(parentElement === false) {
32           if(tempElement[0].nodeName == 'BODY') {
33             parentElement = '';
34           }
35
36           if(typeof tempElement.attr('zf-closable') !== 'undefined' && tempElement.attr('zf-closable') !== false) {
37             parentElement = tempElement;
38           }
39
40           tempElement = tempElement.parent();
41         }
42         targetId = parentElement.attr('id');
43       }
44
45       element.on('click', function(e) {
46         foundationApi.publish(targetId, 'close');
47         e.preventDefault();
48       });
49     }
50   }
51
52   zfOpen.$inject = ['FoundationApi'];
53
54   function zfOpen(foundationApi) {
55     var directive = {
56       restrict: 'A',
57       link: link
58     };
59
60     return directive;
61
62     function link(scope, element, attrs) {
63       element.on('click', function(e) {
64         foundationApi.publish(attrs.zfOpen, 'open');
65         e.preventDefault();
66       });
67     }
68   }
69
70   zfToggle.$inject = ['FoundationApi'];
71
72   function zfToggle(foundationApi) {
73     var directive = {
74       restrict: 'A',
75       link: link
76     }
77
78     return directive;
79
80     function link(scope, element, attrs) {
81       element.on('click', function(e) {
82         foundationApi.publish(attrs.zfToggle, 'toggle');
83         e.preventDefault();
84       });
85     }
86   }
87
88   zfEscClose.$inject = ['FoundationApi'];
89
90   function zfEscClose(foundationApi) {
91     var directive = {
92       restrict: 'A',
93       link: link
94     };
95
96     return directive;
97
98     function link(scope, element, attrs) {
99       element.on('keyup', function(e) {
100         if (e.keyCode === 27) {
101           foundationApi.closeActiveElements();
102         }
103         e.preventDefault();
104       });
105     }
106   }
107
108   zfSwipeClose.$inject = ['FoundationApi'];
109
110   function zfSwipeClose(foundationApi) {
111     var directive = {
112       restrict: 'A',
113       link: link
114     };
115     return directive;
116
117     function link($scope, element, attrs) {
118       var swipeDirection;
119       var hammerElem;
120       if (Hammer) {
121         hammerElem = new Hammer(element[0]);
122         // set the options for swipe (to make them a bit more forgiving in detection)
123         hammerElem.get('swipe').set({
124           direction: Hammer.DIRECTION_ALL,
125           threshold: 5, // this is how far the swipe has to travel
126           velocity: 0.5 // and this is how fast the swipe must travel
127         });
128       }
129       // detect what direction the directive is pointing
130       switch (attrs.zfSwipeClose) {
131         case 'right':
132           swipeDirection = 'swiperight';
133           break;
134         case 'left':
135           swipeDirection = 'swipeleft';
136           break;
137         case 'up':
138           swipeDirection = 'swipeup';
139           break;
140         case 'down':
141           swipeDirection = 'swipedown';
142           break;
143         default:
144           swipeDirection = 'swipe';
145       }
146       hammerElem.on(swipeDirection, function() {
147         foundationApi.publish(attrs.id, 'close');
148       });
149     }
150   }
151
152   zfHardToggle.$inject = ['FoundationApi'];
153
154   function zfHardToggle(foundationApi) {
155     var directive = {
156       restrict: 'A',
157       link: link
158     };
159
160     return directive;
161
162     function link(scope, element, attrs) {
163       element.on('click', function(e) {
164         foundationApi.closeActiveElements({exclude: attrs.zfHardToggle});
165         foundationApi.publish(attrs.zfHardToggle, 'toggle');
166         e.preventDefault();
167       });
168     }
169   }
170
171 })();