forked from 92jackson/mainsail-advanced-filament-swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert-handler.js
290 lines (251 loc) · 8.34 KB
/
alert-handler.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
/*
################################################################
################################################################
################################################################
Mainsail index.html mod for ADVANCED FILAMENT SWAP by JACKSON 92
################################################################
################################################################
################################################################
########################################################
Github: https://github.com/92jackson/mainsail-advanced-filament-swap
Discord: https://discord.gg/e3eXGTJbjx
########################################################
WHAT IS THIS?:
This is an additional javscript file which must be
added to your Mainsail index.html to manage pop-up alerts.
TO INSTALL:
- Copy this file to the same directory as Mainsail's index.html (/home/pi/mainsail)
- Open index.html in a text editor and paste the following just before </head>
<!-- MOD START -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
<script src="./alert-handler.js"></script>
<!-- MOD END -->
N.B:
IF MAINSAIL PUSH AN UPDATE TO INDEX.HTML IN AN UPDATE,
YOU WILL HAVE RE-EDIT THE INDEX.HTML TO INCLUDE THE MOD.
You shouldn't have to edit any of the following source,
it should just work (provided that you have set up my
adv_filament_swap.cfg script correctly).
*/
var currAlertId = 0;
var currPushMsg = 0;
var alertIdForceCloseTo = -1;
var alert = null;
var runningMacro = false;
function isEmpty(value) {
return typeof value == 'string' && !value.trim() || typeof value == 'undefined' || value === null;
}
function fireMacro(macro) {
if (runningMacro == false) {
var wait = ( macro.indexOf('!') == -1 ) ? true : false;
macro = macro.replace('!', '');
var res = $.ajax({
type: "GET",
url: "/printer/gcode/script",
data: { script: macro },
cache: false,
timeout: 300000
});
runningMacro = true;
if (alert != null) {
if (wait == false) {
alert.close();
alert = null;
runningMacro = false;
} else {
$('.jconfirm-box-container .jconfirm-buttons .btn.'+macro).append('<div class="lds-dual-ring spin"></div>');
$('.jconfirm-box-container .jconfirm-buttons .btn').prop('disabled', true);
res.always(function( data ) {
$('.jconfirm-box-container .jconfirm-buttons .btn.'+macro+' .spin').remove();
$('.jconfirm-box-container .jconfirm-buttons .btn').prop('disabled', false);
runningMacro = false;
});
}
}
}
}
$(this).arrive('.consoleTableRow .alert-usr-close', function() {
var newAlertCloseId = $(this).attr("alertid");
if (newAlertCloseId > 0 && newAlertCloseId > alertIdForceCloseTo) alertIdForceCloseTo = newAlertCloseId;
if (currAlertId <= alertIdForceCloseTo && alert != null) {
alert.close();
alert = null;
currPushMsg = 0;
}
runningMacro = false;
});
$(this).arrive('.consoleTableRow .alert-usr', function() {
var $newAlert = $(this);
var newAlertId = parseInt($newAlert.attr("alertid"));
if (newAlertId > currAlertId && alertIdForceCloseTo < newAlertId) {
runningMacro = false;
currPushMsg = 0;
if (alert != null) {
alert.close();
alert = null;
}
currAlertId = newAlertId;
var macros = "";
var numMacros = 0;
if (!isEmpty($newAlert.attr('macro'))) {
macros = $newAlert.attr("macro").split("::");
numMacros = macros.length;
}
var color = (!isEmpty($newAlert.attr('color'))) ? $newAlert.attr('color') : 'orange';
alert = $.alert({
title: $newAlert.children("h1").text(),
content: $newAlert.html(),
type: color,
typeAnimated: false,
closeIcon: true,
theme: 'material',
columnClass: 'medium',
buttons: {
close: {
text: 'Okay',
isHidden: (numMacros > 0) ? true : false,
btnClass: 'btn-'+color
}
},
onOpenBefore: function() {
if (numMacros > 0) {
var i;
for (i = 0; i < numMacros; ++i) {
$('.jconfirm-box-container .jconfirm-buttons').append(
'<button class="btn btn-'+color+' '+macros[i].replace('!', '')+'" onclick="fireMacro(\''+macros[i]+'\')">'+
macros[i].replace('_', ' ').replace('!', '')+
'</button>'
);
}
}
},
onOpen: function() {
if ($(".consoleTableRow .soft-alert-usr[alertid='"+currAlertId+"']").length) {
pushMsg($(".consoleTableRow .soft-alert-usr[alertid='"+currAlertId+"']").first());
}
},
});
//If alarm=true attr is set, fire an audible alarm
if (!isEmpty($newAlert.attr("alarm"))) {
const audio = new Audio("https://cdn.freesound.org/previews/205/205951_981397-lq.mp3");
audio.play();
}
}
});
$(this).arrive('.consoleTableRow .soft-alert-usr', function() {
pushMsg($(this));
});
function pushMsg(newMsg) {
if (alert != null) {
var pushId = parseInt(newMsg.attr("push"));
//console.log(newMsg.attr("alertid")+"-"+currAlertId+" "+pushId+"-"+currPushMsg);
if (newMsg.attr("alertid") == currAlertId && pushId >= currPushMsg) {
$('.jconfirm-box-container .jconfirm-clear').html('<div class="push-msg"><span><b>> </b> '+newMsg.text().replace('oC', '℃')+'</span></div>');
$('.jconfirm-box-container .jconfirm-clear .push-msg').animate({height: "toggle", opacity: "toggle"}, 1000);
currPushMsg = pushId;
if (!isEmpty(newMsg.attr("eta"))) {
$('.jconfirm-box-container .jconfirm-clear').append('<div class="progress_bar"><div tyle="background-color:'+$(".jconfirm-buttons button").first().css("background-color")+ '"></div></div>');
window.setTimeout(function() {
$(".jconfirm-box-container .jconfirm-clear .progress_bar div").css({"width": "100%", "-webkit-transition": Math.ceil(newMsg.attr("eta"))+"s linear"});
}, 0);
}
}
}
}
var styleElem = document.createElement("style");
styleElem.innerHTML = `
.jconfirm-box-container {
margin: 0 auto;
font-family: Arial, Helvetica, sans-serif !important;
}
.jconfirm-box-container .jconfirm-content-pane h1 {
display: none;
}
.jconfirm-box-container .jconfirm-content-pane p, .jconfirm-box-container .jconfirm-content-pane li {
margin: 10px 10px 0 10px;
}
.jconfirm-box-container .jconfirm-content-pane a {
background: #CDCDCD;
padding: 0 6px
}
.jconfirm-box-container .jconfirm-content-pane li {
font-size: small;
}
.jconfirm-box-container .jconfirm-buttons .btn {
padding: 10px 40px !important;
}
.jconfirm-box-container .jconfirm-buttons .btn[disabled] {
background-color: #CDCDCD !important;
cursor: not-allowed;
}
.jconfirm-box-container .jconfirm-clear .push-msg {
display: none;
width: 100%;
background: #000000;
color: #FFFFFF;
font-size: small;
font-family: Roboto Mono, monospace !important;
}
.jconfirm-box-container .jconfirm-clear .push-msg span {
padding: 6px 12px;
}
@keyframes cursor-blink {
0% {
opacity: 0;
}
}
.jconfirm-box-container .jconfirm-clear .push-msg span {
display: flex;
align-items: center;
gap: 6px;
}
.jconfirm-box-container .jconfirm-clear .push-msg span::after {
content: "";
width: 5px;
height: 20px;
background: #FFFFFF;
display: inline-block;
animation: cursor-blink 1.5s steps(2) infinite;
}
.lds-dual-ring {
display: inline-block;
width: 20px;
height: 20px;
margin: -3px 0;
position: absolute
}
.lds-dual-ring:after {
content: " ";
display: block;
width: 10px;
height: 10px;
margin: 8px;
border-radius: 50%;
border: 6px solid #fff;
border-color: #fff transparent #fff transparent;
animation: lds-dual-ring 1.2s linear infinite;
}
@keyframes lds-dual-ring {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.progress_bar {
width: 100%;
height: 20px;
background: #CDCDCD;
margin-top: 10px
}
.progress_bar div {
width: 0px;
height: 20px;
background-color: #333;
}
`;
document.head.appendChild(styleElem);