(function() { 'use strict'; angular.module('shared') .service('LazModalService', ['ModalService', '$q', '$templateRequest', function(ModalService, $q, $templateRequest) { var service = this; service.showModal = function(options) { disableLocationChangeSuccessByDefault(options); return getTemplate(options.template, options.templateUrl) .then(function(template) { options.template = wrapTemplateInLazModal(template, options); return ModalService.showModal(options); }); }; function disableLocationChangeSuccessByDefault(options) { if (options.locationChangeSuccess == null) { options.locationChangeSuccess = false; } } function getTemplate(template, templateUrl) { var deferred = $q.defer(); if (template) { deferred.resolve(template); } else if (templateUrl) { $templateRequest(templateUrl, true) .then(function(template) { deferred.resolve(template); }, function(error) { deferred.reject(error); }); } else { deferred.reject("No template or templateUrl has been specified."); } return deferred.promise; } function wrapTemplateInLazModal(template, options) { var attributes = getLazModalAttributes(options); return '' + template + ''; } function getLazModalAttributes(options) { var onCloseHtmlAttribute = getOnCloseHtmlAttribute(options); var overrideClassAttribute = getOverrideClassHtmlAttribute(options); var overrideFocusIdHtmlAttribute = getOverrideFocusIdHtmlAttribute(options); var overrideStyleHtmlAttribute = getOverrideStyleHtmlAttribute(options); var hideCloseButtonHtmlAttribute = getHideCloseButtonHtmlAttribute(options); var overrideReturnFocusClassHtmlAttribute = getOverrideReturnFocusClassHtmlAttribute(); return onCloseHtmlAttribute + ' ' + overrideClassAttribute + ' ' + overrideFocusIdHtmlAttribute + ' ' + overrideStyleHtmlAttribute + ' ' + hideCloseButtonHtmlAttribute + ' ' + overrideReturnFocusClassHtmlAttribute; } function getOnCloseHtmlAttribute(options) { if (options.hasOwnProperty('onClose')) { return 'on-close="' + options.onClose + '"'; } else { return 'on-close="$ctrl.close"'; } } function getOverrideClassHtmlAttribute(options) { if (options.hasOwnProperty('overrideClass')) { return 'override-class="' + options.overrideClass + '"'; } else { return ''; } } function getOverrideFocusIdHtmlAttribute(options) { if (options.hasOwnProperty('overrideFocusId')) { return 'override-focus-id="' + options.overrideFocusId + '"'; } else { return ''; } } function getOverrideStyleHtmlAttribute(options) { if (options.hasOwnProperty('overrideStyle')) { return 'override-style="' + options.overrideStyle + '"'; } else { return ''; } } function getHideCloseButtonHtmlAttribute(options) { if (options.hasOwnProperty('hideCloseButton')) { return 'hide-close-button="' + options.hideCloseButton + '"'; } else { return ''; } } function getOverrideReturnFocusClassHtmlAttribute() { var activeElement = angular.element(document.activeElement); if (activeElement) { activeElement.addClass('js-elementThatOpenedModal'); return 'override-return-focus-class="js-elementThatOpenedModal"'; } else { return ''; } } }]); })();