forked from m-e-conroy/angular-dialog-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.js
365 lines (307 loc) · 14.3 KB
/
dialogs.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* Note:
* 1. This version requires Angular UI Bootstrap >= v0.10.0 with templates
* 2. This version requires angular-translate for i18n support
*/
//== Controllers =============================================================//
angular.module('dialogs.controllers', ['ui.bootstrap.modal'])
/**
* Error Dialog Controller
*/
.controller('errorDialogCtrl', ['$scope', '$modalInstance', 'header', 'msg', function ($scope, $modalInstance, header, msg) {
//-- Variables -----//
$scope.header = (angular.isDefined(header)) ? header : "Error";
$scope.msg = (angular.isDefined(msg)) ? msg : "An unknown error has occurred.";
//-- Methods -----//
$scope.close = function () {
$modalInstance.close();
$scope.$destroy();
}; // end close
}]) // end ErrorDialogCtrl
/**
* Wait Dialog Controller
*/
.controller('waitDialogCtrl', ['$scope', '$modalInstance', '$timeout', 'header', 'msg', 'progress', function ($scope, $modalInstance, $timeout, header, msg, progress) {
//-- Variables -----//
$scope.header = (angular.isDefined(header)) ? header : "Please Wait...";
$scope.msg = (angular.isDefined(msg)) ? msg : "Waiting on operation to complete.";
$scope.progress = (angular.isDefined(progress)) ? progress : 100;
//-- Listeners -----//
// Note: used $timeout instead of $scope.$apply() because I was getting a $$nextSibling error
// close wait dialog
$scope.$on('dialogs.wait.complete', function () {
$timeout(function () { $modalInstance.close(); $scope.$destroy(); });
}); // end on(dialogs.wait.complete)
// update the dialog's message
$scope.$on('dialogs.wait.message', function (evt, args) {
$scope.msg = (angular.isDefined(args.msg)) ? args.msg : $scope.msg;
}); // end on(dialogs.wait.message)
// update the dialog's progress (bar) and/or message
$scope.$on('dialogs.wait.progress', function (evt, args) {
$scope.msg = (angular.isDefined(args.msg)) ? args.msg : $scope.msg;
$scope.progress = (angular.isDefined(args.progress)) ? args.progress : $scope.progress;
}); // end on(dialogs.wait.progress)
//-- Methods -----//
$scope.getProgress = function () {
return { 'width': $scope.progress + '%' };
}; // end getProgress
}]) // end WaitDialogCtrl
/**
* Notify Dialog Controller
*/
.controller('notifyDialogCtrl', ['$scope', '$modalInstance', 'header', 'msg', function ($scope, $modalInstance, header, msg) {
//-- Variables -----//
$scope.header = (angular.isDefined(header)) ? header : "Notification";
$scope.msg = (angular.isDefined(msg)) ? msg : "Unknown application notification.";
//-- Methods -----//
$scope.close = function () {
$modalInstance.close();
$scope.$destroy();
}; // end close
}]) // end WaitDialogCtrl
/**
* Confirm Dialog Controller
*/
.controller('confirmDialogCtrl', ['$scope', '$modalInstance', 'header', 'msg', 'yesMsg', 'noMsg', function ($scope, $modalInstance, header, msg, yesMsg, noMsg) {
//-- Variables -----//
$scope.header = (angular.isDefined(header)) ? header : "Confirmation";
$scope.msg = (angular.isDefined(msg)) ? msg : "Confirmation required";
$scope.yesMsg = (angular.isDefined(yesMsg)) ? yesMsg : "yes";
$scope.noMsg = (angular.isDefined(noMsg)) ? noMsg : "no";
//-- Methods -----//
$scope.no = function () {
$modalInstance.dismiss('no');
}; // end close
$scope.yes = function () {
$modalInstance.close('yes');
}; // end yes
}]); // end ConfirmDialogCtrl / dialogs.controllers
//== Services ================================================================//
angular.module('dialogs.services', ['ui.bootstrap.modal', 'dialogs.controllers'])
.provider('dialogs', [function () {
var _b = true; // backdrop
var _k = true; // keyboard
var _w = 'dialogs-default'; // windowClass
var _copy = true; // controls use of angular.copy
var _wTmpl = null; // window template
var _wSize = 'lg'; // large modal window default
var _setOpts = function (opts) {
var _opts = {};
opts = angular.isDefined(opts) ? opts : {};
_opts.kb = (angular.isDefined(opts.keyboard)) ? opts.keyboard : _k; // values: true,false
_opts.bd = (angular.isDefined(opts.backdrop)) ? opts.backdrop : _b; // values: 'static',true,false
_opts.ws = (angular.isDefined(opts.size) && (angular.equals(opts.size, 'sm') || angular.equals(opts.size, 'lg'))) ? opts.size : _wSize; // values: 'sm', 'lg'
_opts.wc = (angular.isDefined(opts.windowClass)) ? opts.windowClass : _w; // additional CSS class(es) to be added to a modal window
return _opts;
} // end _setOpts
/**
* Use Backdrop
*
* Sets the use of the modal backdrop. Either to have one or not and
* whether or not it responds to mouse clicks ('static' sets the
* backdrop to true and does not respond to mouse clicks).
*
* @param val mixed (true, false, 'static')
*/
this.useBackdrop = function (val) { // possible values : true, false, 'static'
if (angular.isDefined(val))
_b = val;
}; // end useStaticBackdrop
/**
* Use ESC Close
*
* Sets the use of the ESC (escape) key to close modal windows.
*
* @param val boolean
*/
this.useEscClose = function (val) { // possible values : true, false
if (angular.isDefined(val))
_k = (!angular.equals(val, 0) && !angular.equals(val, 'false') && !angular.equals(val, 'no') && !angular.equals(val, null) && !angular.equals(val, false)) ? true : false;
}; // end useESCClose
/**
* Use Class
*
* Sets the additional CSS window class of the modal window template.
*
* @param val string
*/
this.useClass = function (val) {
if (angular.isDefined(val))
_w = val;
}; // end useClass
/**
* Use Copy
*
* Determines the use of angular.copy when sending data to the modal controller.
*
* @param val boolean
*/
this.useCopy = function (val) {
if (angular.isDefined(val))
_copy = (!angular.equals(val, 0) && !angular.equals(val, 'false') && !angular.equals(val, 'no') && !angular.equals(val, null) && !angular.equals(val, false)) ? true : false;
}; // end useCopy
/**
* Set Window Template
*
* Sets a path to a template to use overriding modal's window template.
*
* @param val string
*/
this.setWindowTmpl = function (val) {
if (angular.isDefined(val))
_wTmpl = val;
}; // end setWindowTmpl
/**
* Set Size
*
* Sets the modal size to use (sm,lg), requires Angular-ui-Bootstrap 0.11.0 and Bootstrap 3.1.0 +
*
* @param val string (sm,lg)
*/
this.setSize = function (val) {
if (angular.isDefined(val))
_wSize = (angular.equals(val, 'sm') || angular.equals(val, 'lg')) ? val : _wSize;
}; // end setSize
this.$get = ['$modal', function ($modal) {
return {
/**
* Error Dialog
*
* @param header string
* @param msg string
* @param opts object
*/
error: function (header, msg, opts) {
opts = _setOpts(opts);
return $modal.open({
templateUrl: '/dialogs/error.html',
controller: 'errorDialogCtrl',
backdrop: opts.bd,
keyboard: opts.kb,
windowClass: opts.wc,
size: opts.ws,
resolve: {
header: function () { return angular.copy(header); },
msg: function () { return angular.copy(msg); }
}
}); // end modal.open
}, // end error
/**
* Wait Dialog
*
* @param header string
* @param msg string
* @param progress int
* @param opts object
*/
wait: function (header, msg, progress, opts) {
opts = _setOpts(opts);
return $modal.open({
templateUrl: '/dialogs/wait.html',
controller: 'waitDialogCtrl',
backdrop: opts.bd,
keyboard: opts.kb,
windowClass: opts.wc,
size: opts.ws,
resolve: {
header: function () { return angular.copy(header); },
msg: function () { return angular.copy(msg); },
progress: function () { return angular.copy(progress); }
}
}); // end modal.open
}, // end wait
/**
* Notify Dialog
*
* @param header string
* @param msg string
* @param opts object
*/
notify: function (header, msg, opts) {
opts = _setOpts(opts);
return $modal.open({
templateUrl: '/dialogs/notify.html',
controller: 'notifyDialogCtrl',
backdrop: opts.bd,
keyboard: opts.kb,
windowClass: opts.wc,
size: opts.ws,
resolve: {
header: function () { return angular.copy(header); },
msg: function () { return angular.copy(msg); }
}
}); // end modal.open
}, // end notify
/**
* Confirm Dialog
*
* @param header string
* @param msg string
* @param opts object
*/
confirm: function (header, msg, opts) {
var yesMsg = 'yes';
var noMsg = 'no';
if (angular.isDefined(opts)) {
yesMsg = angular.isDefined(opts.yesMsg) ? opts.yesMsg : yesMsg;
noMsg = angular.isDefined(opts.noMsg) ? opts.noMsg : noMsg;
}
opts = _setOpts(opts);
return $modal.open({
templateUrl: '/dialogs/confirm.html',
controller: 'confirmDialogCtrl',
backdrop: opts.bd,
keyboard: opts.kb,
windowClass: opts.wc,
size: opts.ws,
resolve: {
header: function () { return angular.copy(header); },
msg: function () { return angular.copy(msg); },
yesMsg: function () { return angular.copy(yesMsg); },
noMsg: function () { return angular.copy(noMsg); }
}
}); // end modal.open
}, // end confirm
/**
* Create Custom Dialog
*
* @param url string
* @param ctrlr string
* @param data object
* @param opts object
*/
create: function (url, ctrlr, data, opts) {
var copy = (angular.isDefined(opts) && angular.isDefined(opts.copy)) ? opts.copy : _copy;
opts = _setOpts(opts);
return $modal.open({
templateUrl: url,
controller: ctrlr,
keyboard: opts.kb,
backdrop: opts.bd,
windowClass: opts.wc,
size: opts.ws,
resolve: {
data: function () {
if (copy)
return angular.copy(data);
else
return data;
}
}
}); // end modal.open
} // end create
}; // end return
}]; // end $get
}]); // end provider
//== Module ==================================================================//
angular.module('dialogs.main', ['dialogs.services', 'ngSanitize']) // requires angular-sanitize.min.js (ngSanitize) //code.angularjs.org/1.2.1/angular-sanitize.min.js
// Add default templates via $templateCache
.run(['$templateCache', '$interpolate', function ($templateCache, $interpolate) {
// get interpolation symbol (possible that someone may have changed it in their application instead of using '{{}}')
var startSym = $interpolate.startSymbol();
var endSym = $interpolate.endSymbol();
$templateCache.put('/dialogs/error.html', '<div class="modal-header dialog-header-error"><button type="button" class="close" ng-click="close()">×</button><h4 class="modal-title text-danger"><span class="glyphicon glyphicon-warning-sign"></span> <span ng-bind-html="header"></span></h4></div><div class="modal-body text-danger" ng-bind-html="msg"></div><div class="modal-footer"><button type="button" class="btn btn-default" ng-click="close()">Close</button></div>');
$templateCache.put('/dialogs/wait.html', '<div class="modal-header dialog-header-wait"><h4 class="modal-title"><span class="glyphicon glyphicon-time"></span> Please Wait</h4></div><div class="modal-body"><p ng-bind-html="msg"></p><div class="progress progress-striped active"><div class="progress-bar progress-bar-info" ng-style="getProgress()"></div><span class="sr-only">progress% Complete</span></div></div>');
$templateCache.put('/dialogs/notify.html', '<div class="modal-header dialog-header-notify"><button type="button" class="close" ng-click="close()" class="pull-right">×</button><h4 class="modal-title text-info"><span class="glyphicon glyphicon-info-sign"></span> ' + startSym + 'header' + endSym + '</h4></div><div class="modal-body text-info" ng-bind-html="msg"></div><div class="modal-footer"><button type="button" class="btn btn-primary" ng-click="close()">OK</button></div>');
$templateCache.put('/dialogs/confirm.html', '<div class="modal-header dialog-header-confirm"><button type="button" class="close" ng-click="no()">×</button><h4 class="modal-title"><span class="glyphicon glyphicon-check"></span> ' + startSym + 'header' + endSym + '</h4></div><div class="modal-body" ng-bind-html="msg"></div><div class="modal-footer"><button type="button" class="btn btn-default" ng-click="yes()">' + startSym + 'yesMsg' + endSym + '</button><button type="button" class="btn btn-primary" ng-click="no()">' + startSym + 'noMsg' + endSym + '</button></div>');
}]); // end run / dialogs