Lines
98.55 %
Functions
50 %
Branches
100 %
//! Opening the transaction-, account- and commodity-edit forms from a `load-form` reply.
use cli_core::eval::escape_str;
use cli_core::forms::parse_loaded_reply;
use forms::{
ACCOUNT_EDIT, COMMODITY_EDIT, FormValues, SCRIPT_EDIT, TAG_EDIT, TEMPLATE_EDIT,
TRANSACTION_EDIT,
};
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::route::{RouteCtx, ScriptRoute, TemplateRoute};
use crate::view::ViewId;
use crate::window::WindowId;
impl App {
/// Dispatch `(load-form "transaction-edit" "<id>")` with `RouteCtx::TransactionEdit`.
///
/// The async reply opens the edit form, or shows why the transaction
/// cannot be edited.
pub fn open_transaction_edit_async(&mut self, id: &str) {
self.load_for_edit(
ViewId::Transactions,
RouteCtx::TransactionEdit,
TRANSACTION_EDIT.name,
(id, "transaction"),
);
}
/// Dispatch `(load-form "account-edit" "<id>")` with `RouteCtx::AccountEdit`.
pub fn open_account_edit_async(&mut self, id: &str) {
ViewId::Accounts,
RouteCtx::AccountEdit,
ACCOUNT_EDIT.name,
(id, "account"),
/// Dispatch `(load-form "commodity-edit" "<id>")` with `RouteCtx::CommodityEdit`.
pub fn open_commodity_edit_async(&mut self, id: &str) {
ViewId::Commodities,
RouteCtx::CommodityEdit,
COMMODITY_EDIT.name,
(id, "commodity"),
/// Dispatch `(load-form "tag-edit" "<id>")` with `RouteCtx::TagEdit`.
pub fn open_tag_edit_async(&mut self, id: &str) {
self.load_for_edit(ViewId::Tags, RouteCtx::TagEdit, TAG_EDIT.name, (id, "tag"));
/// Dispatch `(load-form "template-edit" "<id>")` with `TemplateRoute::Edit`.
pub fn open_template_edit_async(&mut self, id: &str) {
let ctx = RouteCtx::Template(TemplateRoute::Edit);
self.load_for_edit(ViewId::Templates, ctx, TEMPLATE_EDIT.name, (id, "template"));
/// Dispatch `(load-form "script-edit" "<id>")` with `ScriptRoute::Edit`.
pub fn open_script_edit_async(&mut self, id: &str) {
let ctx = RouteCtx::Script(ScriptRoute::Edit);
self.load_for_edit(ViewId::Scripts, ctx, SCRIPT_EDIT.name, (id, "script"));
/// As `open_transaction_edit_async`, from `window` whatever it shows: a detail's edit.
pub fn open_transaction_edit_from(&mut self, window: WindowId, id: &str) {
self.load_edit_in(
window,
pub(super) fn load_for_edit(
&mut self,
view: ViewId,
ctx: RouteCtx,
name: &str,
what: (&str, &str),
) {
if let Some(window) = self.front_or_say(view) {
self.load_edit_in(window, ctx, name, what);
/// Asks the form of `id` for the window `window` names; the reply opens it as its dialog.
fn load_edit_in(
window: WindowId,
(id, what): (&str, &str),
if self.console_eval.is_none() {
self.status = "console not connected".to_string();
return;
if self
.pending_routes
.values()
.any(|route| route.ctx.is_same_request(&ctx))
{
self.status = format!("the {what} is already loading");
let form = format!("(load-form {} {})", escape_str(name), escape_str(id));
if self.dispatch_eval(window, ctx, form).is_none() {
self.status = crate::tabs::nms_eval::UNAVAILABLE.to_string();
/// Called from `deliver_reply` when a `TransactionEdit` route reply arrives.
pub(super) fn deliver_transaction_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
self.open_loaded(
target,
&TRANSACTION_EDIT,
wire,
"transaction",
|app, loaded| {
let form = Form::transaction_edit(loaded);
app.overlays.push(Modal::Form(form));
app.fetch_transaction_form_options();
},
/// Called from `deliver_reply` when an `AccountEdit` route reply arrives.
pub(super) fn deliver_account_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
self.open_loaded(target, &ACCOUNT_EDIT, wire, "account", |app, loaded| {
let form = Form::account_edit(loaded);
app.fetch_account_form_options();
});
/// Called from `deliver_reply` when a `CommodityEdit` route reply arrives.
pub(super) fn deliver_commodity_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
self.open_loaded(target, &COMMODITY_EDIT, wire, "commodity", |app, loaded| {
let form = Form::commodity_edit(loaded);
/// Called from `deliver_reply` when a `TagEdit` route reply arrives.
pub(super) fn deliver_tag_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
self.open_loaded(target, &TAG_EDIT, wire, "tag", |app, loaded| {
let form = Form::tag_edit(loaded);
/// Called when a `TemplateRoute::Edit` reply arrives.
pub(super) fn deliver_template_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
self.open_loaded(target, &TEMPLATE_EDIT, wire, "template", |app, loaded| {
let form = Form::template_edit(loaded);
/// Called when a `ScriptRoute::Edit` reply arrives; the server's notice rides on the form.
pub(super) fn deliver_script_edit_reply(&mut self, target: Option<WindowId>, wire: &str) {
let notices = rpc::parse_notices(wire);
self.open_loaded(target, &SCRIPT_EDIT, wire, "script", |app, loaded| {
let mut form = Form::script_edit(loaded);
form.notices = notices;
fn open_loaded(
target: Option<WindowId>,
descriptor: &forms::Form,
wire: &str,
what: &str,
open: impl FnOnce(&mut Self, &FormValues),
match parse_loaded_reply(descriptor, wire) {
Ok(loaded) if self.dialog_free("edit") => {
if self.begin_dialog_of(target) {
open(self, &loaded);
Ok(_) => {}
Err(reason) => self.status = format!("can't edit this {what}: {reason}"),