forked from oitozero/ngSweetAlert
-
Notifications
You must be signed in to change notification settings - Fork 12
/
SweetAlert.js
131 lines (117 loc) · 2.99 KB
/
SweetAlert.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
/**
@fileOverview
@toc
*/
'use strict';
angular.module('19degrees.ngSweetAlert2', [])
.factory('sweetAlert', [ '$timeout', '$window' ,'$q', function ( $timeout, $window, $q ) {
var swal = $window.swal;
var self = function ( arg1, arg2, arg3 ) {
$timeout(function() {
if( typeof(arg2) === 'function' ) {
swal(arg1, function(isConfirm) {
$timeout( function() {
arg2(isConfirm);
});
}, arg3 );
} else {
swal( arg1, arg2, arg3 );
}
});
};
//public methods
var props = {
swal: swal,
adv: function( object ) {
$timeout(function() {
swal( object );
});
},
timed: function( title, message, type, time ) {
$timeout(function() {
swal( {
title: title,
text: message,
type: type,
timer: time
} );
});
},
success: function(title, message) {
$timeout(function(){
swal( title, message, 'success' );
});
},
error: function(title, message) {
$timeout(function(){
swal( title, message, 'error' );
});
},
warning: function(title, message) {
$timeout(function(){
swal( title, message, 'warning' );
});
},
info: function(title, message) {
$timeout(function(){
swal( title, message, 'info' );
});
},
/**
* Display a SweetAlert with the ajax loader and wait until both "promise" and
* timeout (with "time" to wait) are done before resolving/rejecting the promise even if
* the promise fails.
* Like that you could display a waiting SweetAlert until your ajax is done and the alert
* will stay at least "time" ms opened even if the ajax already responded
*
* @param title
* @param message
* @param promise
* @param time
* @returns promise
*/
ajax: function (title, message, promise, time) {
return $timeout(function () {
swal({
title: title,
text: message,
type: 'info'
});
}).then(function () {
swal.disableButtons();
var deferred = $q.defer();
var callBackValue = null;
var nbCallback = 0;
var success = true;
$timeout(function () {
nbCallback++;
if (nbCallback == 2) {
if (success) {
deferred.resolve(callBackValue);
}
else {
deferred.reject(callBackValue);
}
}
}, time ? time : 500);
promise.then(function (response) {
callBackValue = response;
nbCallback++;
if (nbCallback == 2) {
deferred.resolve(callBackValue);
}
}, function (response) {
success = false;
callBackValue = response;
nbCallback++;
if (nbCallback == 2) {
deferred.reject(callBackValue);
}
});
return deferred.promise;
});
}
};
angular.extend(self, props);
return self;
}]);