pootzko
3/18/2015 - 11:56 AM

Modal service for handling modal states

Modal service for handling modal states

'use strict';

app.service("ModalService", function ($timeout) {
    var self = this;

    this.modals = {
        modalFirst: {
            name: "modalFirst",
            id: "#first-modal",
            isOpen: false
        },
        modalSecond: {
            name: "modalSecond",
            id: "#second-info-modal",
            isOpen: false
        }
    };

    // "modalName" is 
    this.openModal = function (modalName) {
        var modal = self.modals[modalName];
        modal.isOpen = true;
        $timeout(function () {
            $(modal.id).foundation('reveal', 'open');
        });
    };

    this.closeModal = function (modalName) {
        var modal = self.modals[modalName];
        $(modal.id).foundation('reveal', 'close');
    };

    this.excludeAllModals = function () {
        $timeout(function () {
            _.each(self.modals, function (modal) {
                modal.isOpen = false;
            });
        });
    };
});
// This goes inside .run()

$rootScope.$on('$viewContentLoaded', function () {
    $(document)
        .foundation()
        .on('closed.fndtn.reveal', '[data-reveal]', function () {
            ModalService.excludeAllModals(false);
        });
});