UNPKG

foundation-apps

Version:

A responsive, Angular-powered framework for web apps from ZURB.

188 lines (149 loc) 4.7 kB
(function() { 'use strict'; angular.module('foundation.core', [ 'foundation.core.animation' ]) .service('FoundationApi', FoundationApi) .service('FoundationAdapter', FoundationAdapter) .factory('Utils', Utils) .run(Setup); ; 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; service.animateAndAdvise = animateAndAdvise; 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) { return FoundationAnimation.animate(element, futureState, animationIn, animationOut); } function animateAndAdvise(element, futureState, animationIn, animationOut) { var promise = FoundationAnimation.animate(element, futureState, animationIn, animationOut); promise.then(function() { publish(element[0].id, futureState ? 'active-true' : 'active-false'); }, function() { publish(element[0].id, 'active-aborted'); }); return promise; } } 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); } }; } } function Setup() { // Attach FastClick if (typeof(FastClick) !== 'undefined') { FastClick.attach(document.body); } // Attach viewport units buggyfill if (typeof(viewportUnitsBuggyfill) !== 'undefined') { viewportUnitsBuggyfill.init(); } } })();