UNPKG

acha-framework

Version:

is a modular framework on both client (angular.js) and server (node.js) side, it provides security, orm, ioc, obfuscation and ...

130 lines 4.56 kB
(function ($, angular, underscore, window, document, undefined) { 'use strict'; angular.module('frontend.directives').factory('modalGeneratorService', [ '$q', '$compile', '$rootScope', '$templateCache', 'commonService', function ($q, $compile, $rootScope, $templateCache, commonService) { var templateUrl = '/templates/framework/directives/modal/template.html'; var modalTemplate = $templateCache.get(templateUrl); var $modal = $(modalTemplate); return function (options) { return $q(function (resolve, reject) { if (!options.template && !options.templateUrl) { reject('no template provided!'); return; } var uuid = commonService.uuid(); var modal = $modal.clone(); if (options.templateUrl) options.template = $templateCache.get(options.templateUrl); if (options.controller) { modal.find('.modal-body').attr('ng-controller', options.controller); } modal.find('.modal-body').append(options.template); var scope = $rootScope.$new(true); var dissmiss = function () { var elem = $('#' + uuid); elem.detach(); scope.$destroy(); elem.remove(); resolve(false); }; var accept = function (result) { var elem = $('#' + uuid); elem.detach(); resolve(result, scope); scope.$destroy(); elem.remove(); }; scope.cssClass = options.cssClass; scope.title = options.title; scope.titleTranslate = options.titleTranslate; scope.message = options.message; scope.messageTranslate = options.messageTranslate; scope.footer = options.footer !== undefined ? options.footer : true; scope.acceptButton = options.acceptButton !== undefined ? options.acceptButton : true; scope.dissmissButton = options.dissmissButton !== undefined ? options.dissmissButton : true; scope.uuid = uuid; scope.data = options.data; scope.vm = {}; scope.vm.dismiss = function () { if (!options.onDismiss) { dissmiss(false); return; } var result = options.onDismiss(scope); if (result === true) { dissmiss(false); } else if (result.then) { result.then(function (res) { dissmiss(res); }, function () { }); } }; scope.vm.accept = function () { if (!options.onAccept) { accept(true); return; } var result = options.onAccept(scope); if (result === true) { accept(true); } else if (result.then) { scope.vm.waiting = true; result.then(function (result) { accept(result); scope.vm.waiting = false; }, function () { scope.vm.waiting = false; }); } }; $('body').append(modal); $compile(modal)(scope); }); }; } ]).factory('modalService', [ 'modalGeneratorService', function (modalGeneratorService) { return { confirm: function (given) { var options = { title: undefined, titleTranslate: 'general.default_confirm_title', message: undefined, messageTranslate: 'general.default_confirm_message', template: undefined, templateUrl: '/templates/framework/directives/modal/confirm.html', controller: undefined, icon: undefined, data: undefined, onAccept: undefined, onDismiss: undefined }; angular.extend(options, given); return modalGeneratorService(options); }, dialog: function (given) { var options = { title: ' ', titleTranslate: undefined, template: undefined, templateUrl: undefined, controller: undefined, icon: undefined, data: undefined, onAccept: undefined, onDismiss: undefined }; angular.extend(options, given); return modalGeneratorService(options); } }; } ]); }(jQuery, angular, _, window, document));