forked from ThrivingKings/Apprise-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprise-v2.js
319 lines (282 loc) · 8.55 KB
/
apprise-v2.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
/***
*
* Apprise v2.5.9 - 2024-05-10 - Sgarbossa Domenico
*
* Custom version of original Apprise v2 from Daniel Raftery
*
* New features
* - added support for textarea / input box
* - added support for radio buttons e group of radio buttons
* - added support for attributes on buttons
* - added support for locking Enter key (useful when editing textarea)
* - added support for scrollable contents
* - added support for buttons inline mode
* - added restore focused element after closing
* - added runOnClose optional function (for default and specific 'close' action)
*/
// Global Apprise variables
var $Apprise = null,
$overlay = null,
$body = null,
$window = null,
$cA = null,
lastFocus = null,
AppriseQueue = [];
// Add overlay and set opacity for cross-browser compatibility
jQuery(function () {
$Apprise = jQuery('<div class="apprise">');
$overlay = jQuery('<div class="apprise-overlay">');
$body = jQuery("body");
$window = jQuery(window);
$body.append($overlay.css("opacity", ".94")).append($Apprise);
});
function Apprise(text, options) {
// Restrict blank modals
if (text === undefined || !text) {
return false;
}
// Necessary variables
let $me = this,
$_inner = jQuery('<div class="apprise-inner">'),
$_radios_container = jQuery('<div class="apprise-radios" style="display: none;">'),
$_buttons = jQuery('<div class="apprise-buttons">'),
$_input = jQuery('<input type="text">');
// Default settings (edit these to your liking)
let settings = {
animation: 450, // Animation speed
width: "auto", // custom width in %
buttons: {
confirm: {
action: function () {
$me.dissapear();
}, // Callback function
className: null, // Custom class name(s)
attrName: null,
attrVal: null,
id: "confirm", // Element ID
text: "Ok" // Button text
}
},
input: false, // input dialog
tipo_input: 'textarea',
textarea: false,
override: true, // Override browser navigation while Apprise is visible
enable_Enter: true,
radio_groups: false,
scrollable_content: false,
buttons_inline: false,
buttons_inline_min_height: 30,
runOnClose: null,
};
// Merge settings with options
jQuery.extend(settings, options);
// Close current Apprise, exit
if (text == "close") {
$cA.dissapear(options);
return;
}
// If an Apprise is already open, push it to the queue
if ($Apprise.is(":visible")) {
AppriseQueue.push({ text: text, options: settings });
return;
}
// Width adjusting function
this.adjustWidth = function () {
var window_width = $window.width(),
w = "20%",
l = "40%";
if (window_width <= 800) {
(w = "90%"), (l = "5%");
} else if (window_width <= 1400 && window_width > 800) {
(w = "70%"), (l = "15%");
} else if (window_width <= 1800 && window_width > 1400) {
(w = "50%"), (l = "25%");
} else if (window_width <= 2200 && window_width > 1800) {
(w = "30%"), (l = "35%");
}
// custom width
if (settings.width != "auto" && parseInt(settings.width)) {
w = parseInt(settings.width) + "%";
l = Math.floor((100 - parseInt(settings.width)) / 2) + "%";
} else if (settings.width.indexOf('custpx') >= 0) {
var custom_width_apprise = settings.width.replace('custpx_', '');
settings.width = custom_width_apprise;
w = parseInt(settings.width) + "px";
calculate_left = 50;
if (custom_width_apprise < jQuery(window).width()) {
var calculate_left = (jQuery(window).width() - custom_width_apprise) / 2;
}
l = Math.floor(calculate_left) + "px";
}
$Apprise.css("width", w).css("left", l);
};
// Close function
this.dissapear = function (runOnClose) {
if (typeof runOnClose === 'function') {
settings.runOnClose = runOnClose;
}
$Apprise.animate({ top: "-100%" }, settings.animation, function () {
$overlay.fadeOut(300, () => {
if (typeof settings.runOnClose === 'function') {
settings.runOnClose();
}
});
$Apprise.hide();
// Unbind window listeners
$window.unbind("beforeunload");
$window.unbind("keydown");
// If in queue, run it
if (AppriseQueue[0]) {
Apprise(AppriseQueue[0].text, AppriseQueue[0].options);
AppriseQueue.splice(0, 1);
}
if (lastFocus) {
lastFocus.focus();
}
});
return;
};
// Keypress function
this.keyPress = function () {
$window.bind("keydown", function (e) {
// Close if the ESC key is pressed
if (e.keyCode === 27) {
if (settings.buttons.cancel) {
jQuery(
"#apprise-btn-" + settings.buttons.cancel.id
).trigger("click");
} else {
$me.dissapear();
}
} else if (e.keyCode === 13) {
if (settings.buttons.confirm && settings.enable_Enter) {
jQuery(
"#apprise-btn-" + settings.buttons.confirm.id
).trigger("click");
} else {
if (settings.enable_Enter) $me.dissapear();
}
}
});
};
// add radios groups
jQuery.each(settings.radio_groups, function (i, radio_group) {
if (radio_group) {
var sub_container = "";
var group_title = radio_group.title ? radio_group.title : "";
if (Object.keys(radio_group.radios).length) {
sub_container = '<div class="apprise-radios-sub-container">';
// add section title
sub_container += '<div style="float:left; text-align: left;"><span>' + group_title + "</span></div>";
// add group's radios
sub_container += '<div style="float:rigth;">';
jQuery.each(radio_group.radios, function (b, radio) {
sub_container += '<input type="radio" id="apprise-radio-' + radio.id + '" name="apprise-radio-' + i + '" ' + (radio.checked ? 'checked' : '')
+ ' > <label for="apprise-radio-' + radio.id + '" ><span></span>' + radio.text + "</label>";
});
sub_container += "</div></div>";
}
// Add to radios group
$_radios_container.css("display", "block");
$_radios_container.append(sub_container);
}
});
// Add buttons
jQuery.each(settings.buttons, function (i, button) {
if (button) {
// Create button
var $_button = jQuery(
'<button id="apprise-btn-' + button.id + '">'
).append(button.text);
// Add custom class names
if (button.className) {
$_button.addClass(button.className);
}
// add custom attr
if (button.attrName && button.attrVal) {
$_button.attr(button.attrName, button.attrVal);
}
// Add to buttons
$_buttons.append($_button);
// Callback (or close) function
$_button.on("click", function () {
// Build response object
var response = {
clicked: button, // Pass back the object of the button that was clicked
input: $_input.val() ? $_input.val() : null // User inputted text
};
button.action(response);
//$me.dissapear();
});
}
});
// Disabled browser actions while open
if (settings.override) {
$window.bind("beforeunload", function (e) {
return "An alert requires attention";
});
}
// Adjust dimensions based on window
$me.adjustWidth();
$window.resize(function () {
$me.adjustWidth();
});
// Save current element focused
lastFocus = document.activeElement;
// Append elements, show Apprise
$Apprise.html("").append($_inner.append('<div class="apprise-content">' + text + "</div>")).append($_radios_container).append($_buttons);
$cA = this;
if (settings.input) {
if (settings.tipo_input == 'input') {
$_input = jQuery(
'<input type="text" ' +
(typeof settings.textarea == "number"
? ' maxlength="' + settings.textarea + '" '
: '') +
'" ' +
(settings.textarea_id
? 'id="' + settings.textarea_id + '"'
: "") +
' style="width: 95%;">'
);
} else {
if (settings.textarea) {
$_input = jQuery(
'<textarea rows="' +
(typeof settings.textarea == "number"
? settings.textarea
: 1) +
'" ' +
(settings.textarea_id
? 'id="' + settings.textarea_id + '"'
: "") +
' style="width: 95%;">'
);
}
}
$_inner.find(".apprise-content").append(jQuery('<div class="apprise-input">').append($_input));
}
$overlay.fadeIn(300);
$Apprise.show().animate({ top: "10%" }, settings.animation, function () { $me.keyPress(); });
parent.window.appriseActive = true;
// attivo contenuto scrollabile
if (settings.scrollable_content) {
jQuery("div.apprise .apprise-inner").css("overflow-y", "scroll");
}
// attivo modalità tasti in-line
if (settings.buttons_inline) {
jQuery("div.apprise .apprise-buttons").css({
"display": "flex",
"align-items": "center",
});
jQuery("div.apprise .apprise-buttons button").css({
"display": "block",
"min-height": settings.buttons_inline_min_height + "px",
});
}
// Focus on input
if (settings.input) {
if (typeof settings.input == "string") $_input.val(settings.input);
$_input.focus();
}
} // end Apprise();