Simon Pichugin <spichugi(a)redhat.com> writes:
The scenario I want to execute:
1. User clicks a button 'Run Full Report';
2. It creates a promise and checks through '.stream()' if the execution
requires user input;
3. If it requires - Show the input modal and wait for the user press
Confirm Button;
4. When the user enters the data (which I store in state) and confirms -
continue the execution in the existing '.stream()';
5. Repeat as many times as it requires (and it is dynamic because it
depends on the actual network state)
This should be possible, see the example below.
You don't need to block or wait in the .stream() callback. Just look
for the prompt that indicates that user input is needed and pop up the
dialog. Your stream callback wont be called anymore since the program
itself is waiting at the prompt and doesn't produce any output any more.
Once the dialog is complete, call .input() with the answer to the
prompt. The program should continue.
-- index.html:
<html>
<script src="../base1/cockpit.js"></script>
<script src="index.js"></script>
<body>
<button id="run">Run</button>
<br/>
<div id="dialog" hidden>
<span id="prompt">Question!</span>
<input id="text"/>
<button id="reply">Reply</button>
</div>
<br/>
<pre id="output">
</pre>
</body>
</html>
--
-- index.js
function id(name) {
return document.getElementById(name);
}
function run() {
var buffer = "";
var proc = cockpit.spawn(["/usr/libexec/dialog-demo"])
.stream(function (data) {
buffer += data;
var lines = buffer.split("\n");
var last_line = lines[lines.length-1];
if (last_line.startsWith("PROMPT: ")) {
id("dialog").removeAttribute("hidden");
id("prompt").innerText = last_line.slice(8);
id("reply").addEventListener("click", function () {
proc.input(id("text").value + "\n");
buffer += id("text").value + "\n"; // fake echo
})
}
})
.done(function () {
id("output").innerText = buffer;
});
}
function init() {
id("run").addEventListener("click", run);
}
document.addEventListener("DOMContentLoaded", init);
--
-- /usr/libexec/dialog-demo
#! /bin/sh
echo -n "PROMPT: What is your name? "
read name
echo "Hello, $name"
--