crmConfirm - Allow confirmation dialogs using Angular partials
authorTim Otten <totten@civicrm.org>
Fri, 3 Apr 2015 09:00:56 +0000 (02:00 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 6 Apr 2015 23:25:09 +0000 (16:25 -0700)
js/angular-crm-ui.js

index 3e821bb7e56fdc13f3cf22642ed993cecc7ca7f5..7dd588aeda58ea5947460a544c91971faeb12bf0 100644 (file)
 
     // Example: <button crm-confirm="{message: ts('Are you sure you want to continue?')}" on-yes="frobnicate(123)">Frobincate</button>
     // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button>
-    .directive('crmConfirm', function () {
+    // Example: <button crm-confirm="{templateUrl: '~/path/to/view.html', export: {foo: bar}}" on-yes="frobnicate(123)">Frobincate</button>
+    .directive('crmConfirm', function ($compile, $rootScope, $templateRequest, $q) {
       // Helpers to calculate default options for CRM.confirm()
       var defaultFuncs = {
         'disable': function (options) {
           };
         }
       };
+      var confirmCount = 0;
       return {
         link: function (scope, element, attrs) {
           $(element).click(function () {
               options.title = attrs.title;
             }
             var defaults = (options.type) ? defaultFuncs[options.type](options) : {};
+
+            var tpl = null, stubId = null;
+            if (!options.message) {
+              if (options.templateUrl) {
+                tpl = $templateRequest(options.templateUrl);
+              }
+              else if (options.template) {
+                tpl = options.template;
+              }
+              if (tpl) {
+                stubId = 'crmUiConfirm_' + (++confirmCount);
+                options.message = '<div id="' + stubId + '"></div>';
+              }
+            }
+
             CRM.confirm(_.extend(defaults, options))
-              .on('crmConfirm:yes', function () { scope.$apply(attrs.onYes); })
-              .on('crmConfirm:no', function () { scope.$apply(attrs.onNo); });
+              .on('crmConfirm:yes', function() { scope.$apply(attrs.onYes); })
+              .on('crmConfirm:no', function() { scope.$apply(attrs.onNo); });
+
+            if (tpl && stubId) {
+              $q.when(tpl, function(html) {
+                var scope = options.scope || $rootScope.$new();
+                if (options.export) {
+                  angular.extend(scope, options.export);
+                }
+                var linker = $compile(html);
+                $('#' + stubId).append($(linker(scope)));
+              });
+            }
           });
         }
       };