-
Notifications
You must be signed in to change notification settings - Fork 42
/
ng-sweet-alert.js
142 lines (119 loc) · 4.03 KB
/
ng-sweet-alert.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* * @description directive for sweet alert
* @author Tushar Borole
* @createDate 18/04/2015
* @version 1.0.3
* @lastmodifiedDate 06/18/2015
*/
(function() {
'use strict';
// Check we have sweet alert js included
if (angular.isUndefined(window.swal)) {
throw "Please inlcude sweet alert js and css from http://t4t5.github.io/sweetalert/";
}
angular
.module('ng-sweet-alert', [])
.directive('sweetalert', sweetalert)
.factory('SweetAlert', sweetalert_service);
sweetalert.$inject = ['$parse'];
/* @ngInject */
function sweetalert($parse) {
// Usage:
//
// Creates:
//
var directive = {
link: link
};
return directive;
function link(scope, element, attrs, controller) {
var sweetElement = angular.element(element);
sweetElement.click(function() {
var sweetOptions = scope.$eval(attrs.sweetOptions);
var sweetConfirmOption = scope.$eval(attrs.sweetConfirmOption);
var sweetCancelOption = scope.$eval(attrs.sweetCancelOption);
swal(sweetOptions,
function(isConfirm) {
if (isConfirm) {
if (sweetConfirmOption) swal(sweetConfirmOption);
if (attrs.sweetOnConfirm) scope.$evalAsync(attrs.sweetOnConfirm);
} else {
if (sweetCancelOption) swal(sweetCancelOption);
if (attrs.sweetOnCancel) scope.$evalAsync(attrs.sweetOnCancel);
}
});
})
}
}
// Use SweetAlert as service
//
// swal() gets two arguments;
// first argument is parameters Objects (with default values).
// second argument is Callback function when clicking on "OK"/"Cancel", which is a promise.
// register to the promise (using 'then') and handle the resolve / reject according to your business logic.
//
// Add 'SweetAlert' to your directive / controller / ect)
// Use SweetAlert.confirm(msg, options) / SweetAlert.alert(msg, options) / SweetAlert.info(msg, options) / SweetAlert.success(msg, options)
// pass arguments:
// msg; String - The message to be displayed in the alert / confirm box (mandatory).
// options; Object (optinal):
// title: String - the title of the box.
// type: String - "warning" / "info" / "error" / "success" / "" (empty string will not display a graphic icon).
// showCancelButton: Boolean - shows the "cancel" button (true will behave like confirm dialog, false will behave like alert dialog).
// Use returned promise;
//
// SweetAlert.confirm("Are you sure?", {title : "Careful now!"})
// .then(function(p) { do something on success },
// function(p) { do something on fail }
// );
//
// SweetAlert.success("You have successfully completed our poll!", {title: "Good job!"});
sweetalert_service.$inject = ['$q'];
function sweetalert_service($q) {
function swal_alert(message, options) {
return swal_confirm(message, angular.extend({
title: "Alert",
text: message,
type: "warning",
showCancelButton: false
}, options));
}
function swal_info(message, options) {
return swal_alert(message, angular.extend({
type: "info"
}, options));
}
function swal_success(message, options) {
return swal_alert(message, angular.extend({
type: "success"
}, options));
}
function swal_error(message, options) {
return swal_alert(message, angular.extend({
type: "error"
}, options));
}
function swal_confirm(message, options) {
var defered = $q.defer();
var options = angular.extend({
title: "Alert",
text: message,
type: "warning",
showCancelButton: true
}, options);
swal(options, function(r) {
defered.resolve(r);
}, function(e) {
defered.reject(e);
});
return defered.promise;
}
return {
alert: swal_alert,
confirm: swal_confirm,
info: swal_info,
success: swal_success,
error: swal_error
};
}
})();