-
Notifications
You must be signed in to change notification settings - Fork 6
/
instructionsScriptStatus.js
executable file
·135 lines (123 loc) · 5 KB
/
instructionsScriptStatus.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
/*!
* Newmips v2.5
* Copyright 2016
* Licensed under GPLV3.0 https://www.gnu.org/licenses/gpl.html
*/
function fetchStatus() {
$.ajax({
url: '/instruction_script/status',
success: function(data) {
try {
// Skip current update status due to internal serveur error
if(data.skip){
setTimeout(fetchStatus, 50);
return;
}
$("#instructionCount").text('Instructions : ' + data.doneInstruction + ' / ' + data.totalInstruction);
var percent = (Number(data.doneInstruction) * 100 / Number(data.totalInstruction)).toFixed(0);
var str = percent + "%";
$("#progressbar").width(str);
if (typeof data.text[0] !== "undefined") {
if (data.text[0].instruction)
$("#answers").html("<i>" + data.text[0].instruction + "</i>:<br><b>" + data.text[0].message + "</b><br><br>" + $("#answers").html());
else
$("#answers").html("<b>" + data.text[0].message + "</b><br><br>" + $("#answers").html());
}
if (!data.over)
setTimeout(fetchStatus, 50);
else {
if (percent >= 100) {
window.location.href = "/application/preview?id_application="+data.id_application+"&timeout=50000";
$("#goTo").show();
$("#scriptSubmit").prop('disabled', false);
$("#goTo").prop('disabled', false);
$("#progressbarcontent").hide();
} else {
// Wait 2 sec before let user click again on button
setTimeout(function(){
$("#scriptSubmit").prop('disabled', false);
}, 2000);
$("#progressbarcontent").hide();
}
}
} catch (err) {
console.error(err);
}
},
error: function(err) {
console.error(err);
$("#scriptSubmit").prop('disabled', false);
$("#progressbarcontent").hide();
}
});
}
$(function() {
var lang = $("#lang").val();
$("#instructionsScript").submit(function() {
$("#goTo").hide();
if($("#addScript").css("display") == "none"){
var formData = new FormData($(this)[0]);
var filename = $(this).find('input').val();
$.ajax({
url: $(this).attr('action'),
method: 'post',
contentType: false,
processData: false,
data: formData,
success: function() {
$("#scriptSubmit").prop('disabled', true);
$("#progressbarcontent").show();
if (lang == 'en')
$("#filename").text('Executing instructions from file "' + filename + '".');
else if (lang == 'fr')
$("#filename").text('Executions des instructions du fichier "' + filename + '".');
setTimeout(fetchStatus, 50);
},
error: function(err) {
console.error(err);
}
});
} else{
var ajaxData = {
template_entry: $("#template_entry").val(),
text: $("#createScriptTextarea").val()
};
$.ajax({
url: $(this).attr('action')+"_alt",
method: 'post',
data: JSON.stringify(ajaxData),
contentType: "application/json",
processData: false,
success: function() {
$("#scriptSubmit").prop('disabled', true);
$("#progressbarcontent").show();
if (lang == 'en')
$("#filename").text('Executing instructions from written script.');
else if (lang == 'fr')
$("#filename").text('Executions des instructions du script écrit.');
setTimeout(fetchStatus, 50);
},
error: function(err) {
console.error(err);
}
});
}
return false;
});
$(document).on("click", "#createScript", function(){
$(this).hide();
$("#addScriptInput").hide();
$("#addScriptInput").prop("required", false);
$("#createScriptTextarea").prop("required", true);
$("#createScriptTextarea").show();
$("#addScript").show();
});
$(document).on("click", "#addScript", function(){
$(this).hide();
$("#createScriptTextarea").hide();
$("#createScriptTextarea").prop("required", false);
$("#addScriptInput").prop("required", true);
$("#addScriptInput").show();
$("#createScript").show();
});
});