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