update ModalPrompt module

This commit is contained in:
joshuaboud 2021-07-22 16:53:25 -03:00
parent d01a9ef665
commit 074f52c0be
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E

View File

@ -1,20 +1,19 @@
/* /*
Cockpit Navigator - A File System Browser for Cockpit. ModalPrompt - A Custom Prompt Module for Cockpit Plugins.
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com> Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
Copyright (C) 2021 Sam Silver <ssilver@45drives.com>
Copyright (C) 2021 Dawson Della Valle <ddellavalle@45drives.com>
This file is part of Cockpit Navigator. This program is free software: you can redistribute it and/or modify
Cockpit Navigator is free software: you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Cockpit Navigator is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. Lesser GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>. You should have received a copy of the Lesser GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/** /**
@ -168,8 +167,20 @@ export class ModalPrompt {
this.footer.innerHTML = ""; this.footer.innerHTML = "";
this.footer.append(this.cancel, this.ok); this.footer.append(this.cancel, this.ok);
let inputs = []; let inputs = [];
let simple_prompt = false;
if (typeof requests === "string") {
let label = requests;
simple_prompt = true;
requests = {
"key": {
"label": label,
"type": "text"
}
}
}
if (typeof requests === "object") {
let req_holder = document.createElement("div"); let req_holder = document.createElement("div");
req_holder.style.display = "flex"; req_holder.style.display = "flex";
req_holder.style.flexFlow = "column nowrap"; req_holder.style.flexFlow = "column nowrap";
@ -208,7 +219,6 @@ export class ModalPrompt {
break; break;
} }
} }
}
this.show(); this.show();
inputs[0].focus(); inputs[0].focus();
@ -222,7 +232,11 @@ export class ModalPrompt {
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.ok.onclick = () => { this.ok.onclick = () => {
let response = {}; let response
if (simple_prompt) {
response = inputs[0].value;
} else {
response = {};
for (let input of inputs) { for (let input of inputs) {
switch (input.type) { switch (input.type) {
case "checkbox": case "checkbox":
@ -234,6 +248,7 @@ export class ModalPrompt {
break; break;
} }
} }
}
resolve(response); resolve(response);
this.hide(); this.hide();
} }