forked from jameskleeh/angular-confirm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-confirm.js
94 lines (81 loc) · 2.53 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
/*
* angular-confirm
* https://github.com/Schlogen/angular-confirm
* @version v1.1.2 - 2015-09-26
* @license Apache
*/
angular.module('angular-confirm', ['ui.bootstrap.modal'])
.controller('ConfirmModalController', 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()">{{data.cancel}}</button>' +
'</div>',
controller: 'ConfirmModalController',
defaultLabels: {
title: 'Confirm',
ok: 'OK',
cancel: 'Cancel'
}
})
.factory('$confirm', function ($modal, $confirmModalDefaults) {
return function (data, settings) {
var defaults = angular.copy($confirmModalDefaults);
settings = angular.extend(defaults, (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', function ($confirm) {
return {
priority: 1,
restrict: 'A',
scope: {
confirmIf: "=",
ngClick: '&',
confirm: '@',
confirmSettings: "=",
confirmTitle: '@',
confirmOk: '@',
confirmCancel: '@'
},
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;
}
$confirm(data, scope.confirmSettings || {}).then(scope.ngClick);
} else {
scope.$apply(scope.ngClick);
}
});
}
}
});