|
1 | 1 | /* |
2 | 2 | * angular-confirm |
3 | 3 | * http://schlogen.github.io/angular-confirm/ |
4 | | - * Version: 1.0.4 - 2015-28-04 |
| 4 | + * Version: 1.1.0 - 2015-14-07 |
5 | 5 | * License: Apache |
6 | 6 | */ |
7 | 7 | angular.module('angular-confirm', ['ui.bootstrap']) |
8 | | -.controller('ConfirmModalController', ['$scope', '$modalInstance', 'data', function ($scope, $modalInstance, data) { |
9 | | - $scope.data = angular.copy(data); |
| 8 | + .controller('ConfirmModalController', ['$scope', '$modalInstance', 'data', function ($scope, $modalInstance, data) { |
| 9 | + $scope.data = angular.copy(data); |
10 | 10 |
|
11 | | - $scope.ok = function () { |
12 | | - $modalInstance.close(); |
13 | | - }; |
| 11 | + $scope.ok = function () { |
| 12 | + $modalInstance.close(); |
| 13 | + }; |
14 | 14 |
|
15 | | - $scope.cancel = function () { |
16 | | - $modalInstance.dismiss('cancel'); |
17 | | - }; |
| 15 | + $scope.cancel = function () { |
| 16 | + $modalInstance.dismiss('cancel'); |
| 17 | + }; |
18 | 18 |
|
19 | | -}]) |
20 | | -.value('$confirmModalDefaults', { |
21 | | - template: '<div class="modal-header"><h3 class="modal-title">{{data.title}}</h3></div>' + |
22 | | - '<div class="modal-body">{{data.text}}</div>' + |
23 | | - '<div class="modal-footer">' + |
24 | | - '<button class="btn btn-primary" ng-click="ok()">{{data.ok}}</button>' + |
25 | | - '<button class="btn btn-default" ng-click="cancel()">{{data.cancel}}</button>' + |
26 | | - '</div>', |
27 | | - controller: 'ConfirmModalController', |
28 | | - defaultLabels: { |
29 | | - title :'Confirm', |
30 | | - ok: 'OK', |
31 | | - cancel: 'Cancel' |
32 | | - } |
33 | | -}) |
34 | | -.factory('$confirm', ['$modal', '$confirmModalDefaults', function($modal, $confirmModalDefaults) { |
35 | | - return function(data, settings) { |
36 | | - settings = angular.extend($confirmModalDefaults, (settings || {})); |
37 | | - |
38 | | - data = angular.extend({}, settings.defaultLabels, data || {}); |
39 | | - |
40 | | - if ('templateUrl' in settings && 'template' in settings) { |
41 | | - delete settings.template; |
| 19 | + }]) |
| 20 | + .value('$confirmModalDefaults', { |
| 21 | + template: '<div class="modal-header"><h3 class="modal-title">{{data.title}}</h3></div>' + |
| 22 | + '<div class="modal-body">{{data.text}}</div>' + |
| 23 | + '<div class="modal-footer">' + |
| 24 | + '<button class="btn btn-primary" ng-click="ok()">{{data.ok}}</button>' + |
| 25 | + '<button class="btn btn-default" ng-click="cancel()">{{data.cancel}}</button>' + |
| 26 | + '</div>', |
| 27 | + controller: 'ConfirmModalController', |
| 28 | + defaultLabels: { |
| 29 | + title: 'Confirm', |
| 30 | + ok: 'OK', |
| 31 | + cancel: 'Cancel' |
42 | 32 | } |
| 33 | + }) |
| 34 | + .factory('$confirm', ['$modal', '$confirmModalDefaults', function ($modal, $confirmModalDefaults) { |
| 35 | + return function (data, settings) { |
| 36 | + settings = angular.extend($confirmModalDefaults, (settings || {})); |
| 37 | + |
| 38 | + data = angular.extend({}, settings.defaultLabels, data || {}); |
| 39 | + |
| 40 | + if ('templateUrl' in settings && 'template' in settings) { |
| 41 | + delete settings.template; |
| 42 | + } |
| 43 | + |
| 44 | + settings.resolve = { |
| 45 | + data: function () { |
| 46 | + return data; |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + return $modal.open(settings).result; |
| 51 | + }; |
| 52 | + }]) |
| 53 | + .directive('confirm', ['$confirm', function ($confirm) { |
| 54 | + return { |
| 55 | + priority: 1, |
| 56 | + restrict: 'A', |
| 57 | + scope: { |
| 58 | + confirmIf: "=", |
| 59 | + ngClick: '&', |
| 60 | + confirm: '@', |
| 61 | + confirmSettings: "=", |
| 62 | + confirmTitle: '@', |
| 63 | + confirmOk: '@', |
| 64 | + confirmCancel: '@' |
| 65 | + }, |
| 66 | + link: function (scope, element, attrs) { |
43 | 67 |
|
44 | | - settings.resolve = {data: function() { return data; }}; |
45 | 68 |
|
46 | | - return $modal.open(settings).result; |
47 | | - }; |
48 | | -}]) |
49 | | -.directive('confirm', ['$confirm', function($confirm) { |
50 | | - return { |
51 | | - priority: 1, |
52 | | - restrict: 'A', |
53 | | - scope: { |
54 | | - confirmIf: "=", |
55 | | - ngClick: '&', |
56 | | - confirm: '@', |
57 | | - confirmSettings: "=", |
58 | | - confirmTitle: '@', |
59 | | - confirmOk: '@', |
60 | | - confirmCancel: '@' |
61 | | - }, |
62 | | - link: function(scope, element, attrs) { |
| 69 | + element.unbind("click").bind("click", function ($event) { |
63 | 70 |
|
64 | | - function reBind(func) { |
65 | | - element.unbind("click").bind("click", function($event) { |
66 | 71 | $event.preventDefault(); |
67 | | - func(); |
68 | | - }); |
69 | | - } |
70 | 72 |
|
71 | | - function bindConfirm() { |
72 | | - var data = { text: scope.confirm }; |
73 | | - if (scope.confirmTitle) { |
74 | | - data.title = scope.confirmTitle; |
75 | | - } |
76 | | - if (scope.confirmOk) { |
77 | | - data.ok = scope.confirmOk; |
78 | | - } |
79 | | - if (scope.confirmCancel) { |
80 | | - data.cancel = scope.confirmCancel; |
81 | | - } |
82 | | - $confirm(data, scope.confirmSettings || {}).then(scope.ngClick); |
83 | | - } |
| 73 | + if (angular.isUndefined(scope.confirmIf) || scope.confirmIf) { |
84 | 74 |
|
85 | | - if (attrs.confirmIf && attrs.confirmIf != "") { |
86 | | - scope.$watch('confirmIf', function(newVal) { |
87 | | - if (newVal) { |
88 | | - reBind(bindConfirm); |
| 75 | + var data = {text: scope.confirm}; |
| 76 | + if (scope.confirmTitle) { |
| 77 | + data.title = scope.confirmTitle; |
| 78 | + } |
| 79 | + if (scope.confirmOk) { |
| 80 | + data.ok = scope.confirmOk; |
| 81 | + } |
| 82 | + if (scope.confirmCancel) { |
| 83 | + data.cancel = scope.confirmCancel; |
| 84 | + } |
| 85 | + $confirm(data, scope.confirmSettings || {}).then(scope.ngClick); |
89 | 86 | } else { |
90 | | - reBind(function() { |
91 | | - scope.$apply(scope.ngClick); |
92 | | - }); |
| 87 | + |
| 88 | + scope.$apply(scope.ngClick); |
93 | 89 | } |
94 | 90 | }); |
95 | | - } else { |
96 | | - reBind(bindConfirm); |
| 91 | + |
97 | 92 | } |
98 | 93 | } |
99 | | - } |
100 | | -}]); |
| 94 | + }]); |
0 commit comments