-
Notifications
You must be signed in to change notification settings - Fork 1
/
angular-popup-boxes.js
155 lines (127 loc) · 3.16 KB
/
angular-popup-boxes.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
angular.module("angularPopupBoxes", ["ui.bootstrap"])
.provider("angularPopupBoxes", function()
{
var that = this;
this.okText = 'Ok';
this.cancelText = 'Cancel';
var setTexts = this.setTexts = function (okText, cancelText)
{
that.okText = okText;
that.cancelText = cancelText;
}
var extendOptions = function(options, defaults)
{
options = options || {};
if(typeof options.okText === 'undefined')
options.okText = defaults.okText;
if(typeof options.cancelText === 'undefined')
options.cancelText = defaults.cancelText;
if(typeof options.width === 'undefined')
options.width = 370;
return options;
}
this.$get = ["$modal", "$q", function($modal, $q)
{
function buildModal(content, options)
{
var modalId = 'modal-' + Math.floor(Math.random() * 9999);
var modalEl;
var html = '<div class="modal-body" id="' + modalId + '">';
html += content;
html += '</div>';
html += '<div class="modal-footer">';
if(typeof options.cancelText === 'string')
html += '<button class="btn btn-sm btn-danger angular-notification-btn-cancel">' + options.cancelText + '</button>';
if(typeof options.okText === 'string')
html += '<button class="btn btn-sm btn-primary angular-notification-btn-ok">' + options.okText + '</button>';
html += '</div>';
var modal = $modal.open(
{
template: html,
keyboard: false,
backdrop: 'static'
});
setTimeout(function()
{
modalEl = $("#" + modalId).parent().parent();
modalEl.width(options.width);
modalEl.find(".angular-notification-btn-ok").click(function()
{
modal.close();
});
modalEl.find(".angular-notification-btn-cancel").click(function()
{
modal.dismiss();
});
modal.el = modalEl;
}, 1);
return modal;
}
function confirm(content, options)
{
options = extendOptions(options,
{
okText: that.okText,
cancelText: that.cancelText
});
return buildModal(content, options);
}
function alert(content, options)
{
options = extendOptions(options,
{
okText: that.okText,
cancelText: false
});
return buildModal(content, options);
}
function input(content, options)
{
content += '<br/><br/><form class="form"><input name="input" class="form-control angular-notification-input" /></form>';
options = extendOptions(options,
{
okText: that.okText,
cancelText: that.cancelText
});
var modal = buildModal(content, options);
var deferred = $q.defer();
var input;
setTimeout(function()
{
input = modal.el.find(".angular-notification-input");
$(window).one('keydown', function()
{
input.focus();
});
input.on('keyup', function(e)
{
if(e.keyCode == 13)
{
modal.close();
}
});
}, 3);
modal.result.then(function()
{
var val = modal.el.find(".angular-notification-input").val();
input.off();
if(val === "")
{
deferred.reject();
}
else
{
deferred.resolve(val);
}
}, deferred.reject);
modal.result = deferred.promise;
return modal;
}
return {
setTexts: setTexts,
confirm: confirm,
alert: alert,
input: input
};
}];
});