4 angular.module('foundation.modal', ['foundation.core'])
5 .directive('zfModal', modalDirective)
6 .factory('ModalFactory', ModalFactory)
9 FoundationModal.$inject = ['FoundationApi', 'ModalFactory'];
11 function FoundationModal(foundationApi, ModalFactory) {
14 service.activate = activate;
15 service.deactivate = deactivate;
16 service.newModal = newModal;
20 //target should be element ID
21 function activate(target) {
22 foundationApi.publish(target, 'show');
25 //target should be element ID
26 function deactivate(target) {
27 foundationApi.publish(target, 'hide');
30 //new modal has to be controlled via the new instance
31 function newModal(config) {
32 return new ModalFactory(config);
36 modalDirective.$inject = ['FoundationApi'];
38 function modalDirective(foundationApi) {
42 templateUrl: 'components/modal/modal.html',
51 function compile(tElement, tAttrs, transclude) {
59 function preLink(scope, iElement, iAttrs, controller) {
60 iAttrs.$set('zf-closable', type);
63 function postLink(scope, element, attrs) {
64 var dialog = angular.element(element.children()[0]);
66 scope.active = scope.active || false;
67 scope.overlay = attrs.overlay === 'false' ? false : true;
68 scope.overlayClose = attrs.overlayClose === 'false' ? false : true;
70 var animationIn = attrs.animationIn || 'fadeIn';
71 var animationOut = attrs.animationOut || 'fadeOut';
73 var overlayIn = 'fadeIn';
74 var overlayOut = 'fadeOut';
76 scope.hideOverlay = function() {
77 if(scope.overlayClose) {
82 scope.hide = function() {
88 scope.show = function() {
96 scope.toggle = function() {
97 scope.active = !scope.active;
105 foundationApi.subscribe(attrs.id, function(msg) {
106 if(msg === 'show' || msg === 'open') {
108 } else if (msg === 'close' || msg === 'hide') {
110 } else if (msg === 'toggle') {
114 if (scope.$root && !scope.$root.$$phase) {
122 //animate both overlay and dialog
124 element.css('background', 'transparent');
127 foundationApi.animate(element, scope.active, overlayIn, overlayOut);
128 foundationApi.animate(dialog, scope.active, animationIn, animationOut);
140 ModalFactory.$inject = ['$http', '$templateCache', '$rootScope', '$compile', '$timeout', '$q', 'FoundationApi'];
142 function ModalFactory($http, $templateCache, $rootScope, $compile, $timeout, $q, foundationApi) {
145 function modalFactory(config) {
146 var self = this, //for prototype functions
147 container = angular.element(config.container || document.body),
148 id = config.id || foundationApi.generateUuid(),
165 if(config.templateUrl) {
167 fetched = $http.get(config.templateUrl, {
168 cache: $templateCache
169 }).then(function (response) {
170 html = response.data;
174 } else if(config.template) {
175 //use provided template
177 html = config.template;
181 self.activate = activate;
182 self.deactivate = deactivate;
183 self.toggle = toggle;
184 self.destroy = destroy;
189 deactivate: deactivate,
194 function checkStatus() {
196 throw "Error: Modal was destroyed. Delete the object and create a new ModalFactory instance."
200 function activate() {
202 $timeout(function() {
204 foundationApi.publish(id, 'show');
208 function deactivate() {
210 $timeout(function() {
212 foundationApi.publish(id, 'hide');
218 $timeout(function() {
220 foundationApi.publish(id, 'toggle');
224 function init(state) {
225 $q.when(fetched).then(function() {
226 if(!attached && html.length > 0) {
227 var modalEl = container.append(element);
229 scope.active = state;
230 $compile(element)(scope);
237 function assembleDirective() {
238 // check for duplicate elements to prevent factory from cloning modals
239 if (document.getElementById(id)) {
243 html = '<zf-modal id="' + id + '">' + html + '</zf-modal>';
245 element = angular.element(html);
247 scope = $rootScope.$new();
249 // account for directive attributes
250 for(var i = 0; i < props.length; i++) {
256 element.attr('animation-in', config[prop]);
259 element.attr('animation-out', config[prop]);
262 element.attr(prop, config[prop]);
266 // access view scope variables
267 if (config.contentScope) {
268 contentScope = config.contentScope;
269 for (var prop in config.contentScope) {
270 if (config.contentScope.hasOwnProperty(prop)) {
271 scope[prop] = config.contentScope[prop];
279 setTimeout(function() {
284 foundationApi.unsubscribe(id);