Merge afb-client
[src/app-framework-demo.git] / afb-client / bower_components / foundation-apps / js / angular / services / foundation.core.js
diff --git a/afb-client/bower_components/foundation-apps/js/angular/services/foundation.core.js b/afb-client/bower_components/foundation-apps/js/angular/services/foundation.core.js
new file mode 100644 (file)
index 0000000..6aeb9da
--- /dev/null
@@ -0,0 +1,163 @@
+(function() {
+  'use strict';
+
+  angular.module('foundation.core', [
+      'foundation.core.animation'
+    ])
+    .service('FoundationApi', FoundationApi)
+    .service('FoundationAdapter', FoundationAdapter)
+    .factory('Utils', Utils)
+  ;
+
+  FoundationApi.$inject = ['FoundationAnimation'];
+
+  function FoundationApi(FoundationAnimation) {
+    var listeners  = {};
+    var settings   = {};
+    var uniqueIds  = [];
+    var service    = {};
+
+    service.subscribe           = subscribe;
+    service.unsubscribe         = unsubscribe;
+    service.publish             = publish;
+    service.getSettings         = getSettings;
+    service.modifySettings      = modifySettings;
+    service.generateUuid        = generateUuid;
+    service.toggleAnimate       = toggleAnimate;
+    service.closeActiveElements = closeActiveElements;
+    service.animate             = animate;
+
+    return service;
+
+    function subscribe(name, callback) {
+      if (!listeners[name]) {
+        listeners[name] = [];
+      }
+
+      listeners[name].push(callback);
+      return true;
+    }
+
+    function unsubscribe(name, callback) {
+      if (listeners[name] !== undefined) {
+        delete listeners[name];
+      }
+      if (typeof callback == 'function') {
+          callback.call(this);
+      }
+    }
+
+    function publish(name, msg) {
+      if (!listeners[name]) {
+        listeners[name] = [];
+      }
+
+      listeners[name].forEach(function(cb) {
+        cb(msg);
+      });
+
+      return;
+    }
+
+    function getSettings() {
+      return settings;
+    }
+
+    function modifySettings(tree) {
+      settings = angular.extend(settings, tree);
+      return settings;
+    }
+
+    function generateUuid() {
+      var uuid = '';
+
+      //little trick to produce semi-random IDs
+      do {
+        uuid += 'zf-uuid-';
+        for (var i=0; i<15; i++) {
+          uuid += Math.floor(Math.random()*16).toString(16);
+        }
+      } while(!uniqueIds.indexOf(uuid));
+
+      uniqueIds.push(uuid);
+      return uuid;
+    }
+
+    function toggleAnimate(element, futureState) {
+      FoundationAnimation.toggleAnimate(element, futureState);
+    }
+
+    function closeActiveElements(options) {
+      var self = this;
+      options = options || {};
+      var activeElements = document.querySelectorAll('.is-active[zf-closable]');
+      // action sheets are nested zf-closable elements, so we have to target the parent
+      var nestedActiveElements = document.querySelectorAll('[zf-closable] > .is-active')
+      
+      if (activeElements.length) {
+        angular.forEach(activeElements, function(el) {
+          if (options.exclude !== el.id) {
+            self.publish(el.id, 'close');
+          }
+        });
+      }
+      if (nestedActiveElements.length) {
+        angular.forEach(nestedActiveElements, function(el) {
+          var parentId = el.parentNode.id;
+          if (options.exclude !== parentId) {
+            self.publish(parentId, 'close');
+          }
+        })
+      }
+    }
+
+    function animate(element, futureState, animationIn, animationOut) {
+      FoundationAnimation.animate(element, futureState, animationIn, animationOut);
+    }
+  }
+
+  FoundationAdapter.$inject = ['FoundationApi'];
+
+  function FoundationAdapter(foundationApi) {
+
+    var service    = {};
+
+    service.activate = activate;
+    service.deactivate = deactivate;
+
+    return service;
+
+    function activate(target) {
+      foundationApi.publish(target, 'show');
+    }
+
+    function deactivate(target) {
+      foundationApi.publish(target, 'hide');
+    }
+  }
+
+
+  function Utils() {
+    var utils = {};
+
+    utils.throttle = throttleUtil;
+
+    return utils;
+
+    function throttleUtil(func, delay) {
+      var timer = null;
+
+      return function () {
+        var context = this, args = arguments;
+
+        if (timer === null) {
+          timer = setTimeout(function () {
+            func.apply(context, args);
+            timer = null;
+          }, delay);
+        }
+      };
+    }
+  }
+
+})();