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.core.js
1 (function() {
2   'use strict';
3
4   angular.module('foundation.core', [
5       'foundation.core.animation'
6     ])
7     .service('FoundationApi', FoundationApi)
8     .service('FoundationAdapter', FoundationAdapter)
9     .factory('Utils', Utils)
10   ;
11
12   FoundationApi.$inject = ['FoundationAnimation'];
13
14   function FoundationApi(FoundationAnimation) {
15     var listeners  = {};
16     var settings   = {};
17     var uniqueIds  = [];
18     var service    = {};
19
20     service.subscribe           = subscribe;
21     service.unsubscribe         = unsubscribe;
22     service.publish             = publish;
23     service.getSettings         = getSettings;
24     service.modifySettings      = modifySettings;
25     service.generateUuid        = generateUuid;
26     service.toggleAnimate       = toggleAnimate;
27     service.closeActiveElements = closeActiveElements;
28     service.animate             = animate;
29
30     return service;
31
32     function subscribe(name, callback) {
33       if (!listeners[name]) {
34         listeners[name] = [];
35       }
36
37       listeners[name].push(callback);
38       return true;
39     }
40
41     function unsubscribe(name, callback) {
42       if (listeners[name] !== undefined) {
43         delete listeners[name];
44       }
45       if (typeof callback == 'function') {
46           callback.call(this);
47       }
48     }
49
50     function publish(name, msg) {
51       if (!listeners[name]) {
52         listeners[name] = [];
53       }
54
55       listeners[name].forEach(function(cb) {
56         cb(msg);
57       });
58
59       return;
60     }
61
62     function getSettings() {
63       return settings;
64     }
65
66     function modifySettings(tree) {
67       settings = angular.extend(settings, tree);
68       return settings;
69     }
70
71     function generateUuid() {
72       var uuid = '';
73
74       //little trick to produce semi-random IDs
75       do {
76         uuid += 'zf-uuid-';
77         for (var i=0; i<15; i++) {
78           uuid += Math.floor(Math.random()*16).toString(16);
79         }
80       } while(!uniqueIds.indexOf(uuid));
81
82       uniqueIds.push(uuid);
83       return uuid;
84     }
85
86     function toggleAnimate(element, futureState) {
87       FoundationAnimation.toggleAnimate(element, futureState);
88     }
89
90     function closeActiveElements(options) {
91       var self = this;
92       options = options || {};
93       var activeElements = document.querySelectorAll('.is-active[zf-closable]');
94       // action sheets are nested zf-closable elements, so we have to target the parent
95       var nestedActiveElements = document.querySelectorAll('[zf-closable] > .is-active')
96       
97       if (activeElements.length) {
98         angular.forEach(activeElements, function(el) {
99           if (options.exclude !== el.id) {
100             self.publish(el.id, 'close');
101           }
102         });
103       }
104       if (nestedActiveElements.length) {
105         angular.forEach(nestedActiveElements, function(el) {
106           var parentId = el.parentNode.id;
107           if (options.exclude !== parentId) {
108             self.publish(parentId, 'close');
109           }
110         })
111       }
112     }
113
114     function animate(element, futureState, animationIn, animationOut) {
115       FoundationAnimation.animate(element, futureState, animationIn, animationOut);
116     }
117   }
118
119   FoundationAdapter.$inject = ['FoundationApi'];
120
121   function FoundationAdapter(foundationApi) {
122
123     var service    = {};
124
125     service.activate = activate;
126     service.deactivate = deactivate;
127
128     return service;
129
130     function activate(target) {
131       foundationApi.publish(target, 'show');
132     }
133
134     function deactivate(target) {
135       foundationApi.publish(target, 'hide');
136     }
137   }
138
139
140   function Utils() {
141     var utils = {};
142
143     utils.throttle = throttleUtil;
144
145     return utils;
146
147     function throttleUtil(func, delay) {
148       var timer = null;
149
150       return function () {
151         var context = this, args = arguments;
152
153         if (timer === null) {
154           timer = setTimeout(function () {
155             func.apply(context, args);
156             timer = null;
157           }, delay);
158         }
159       };
160     }
161   }
162
163 })();