joemfb / ml-common-ng

An angular module of common components for the MarkLogic REST API
https://joemfb.github.io/ml-common-ng/
2 stars 2 forks source link

Add modal service #5

Closed grtjn closed 9 years ago

grtjn commented 9 years ago

Taken from demo-cat, just stripped user ref:

(function () {
  'use strict';

  angular.module('sample.common')
  .service('ModalService', ['$modal', '$sce', ModalService]);

  function ModalService($modal, $sce) {

    var service = {
      show: showModal
    };

    return service;

    function showModal(template, title, model, validate, modalOptions) {
      return $modal.open(
        angular.extend({
          templateUrl: template+'',
          controller: function ($scope, $modalInstance, title, model, validate) {
            $scope.title = title;
            $scope.model = model;
            $scope.alerts = [];
            $scope.ok = function () {
              if (validate) {
                $scope.alerts = validate($scope.model);
              }
              if ($scope.alerts.length === 0) {
                $modalInstance.close($scope.model);
              }
            };
            $scope.cancel = function () {
              $modalInstance.dismiss('cancel');
            };
            $scope.encodeURIComponent = encodeURIComponent;
            $scope.trustUrl = $sce.trustAsResourceUrl;
          },
          size: 'lg',
          resolve: {
            title: function () {
              return title;
            },
            model: function () {
              return model;
            },
            validate: function () {
              return validate;
            }
          }
        }, modalOptions)
      ).result;
    }
  }
}());
grtjn commented 9 years ago

I see potential use for this in ml-search-ng..

joemfb commented 9 years ago

See my comments on #8