public static ValueChangedRich <bool> BindEnableAndInitializeAsObserving <T>( this IActionModel <T> model, Action <AggregatedErrorsValue <bool> > observes) { var isEnabled = new AggregatedErrorsValue <bool>(false, self => !self.Errors.Any(), observes); return(BindEnableAndInitialize(model, isEnabled)); }
public static Action BindSelectionIntoEnabled <ActionT, ItemT>( this IActionModel <ActionT> model, DataGridModel <ItemT> datagrid, SelectionNeeded mode, RowFilter <ItemT> extraRowFilterOrNull = null) { void UpdateEnabled() { bool enabled; switch (mode) { case SelectionNeeded.AtLeastOneSelected: enabled = datagrid.Selected.Length >= 1; break; case SelectionNeeded.ExactlyOneSelected: enabled = datagrid.Selected.Length == 1; break; default: throw new Exception("unsupported selectionNeeded"); } if (!enabled) { switch (mode) { case SelectionNeeded.AtLeastOneSelected: model.ChangeEnabled(false, new[] { I18n.Translate("Need at least one row selected") }, true); break; case SelectionNeeded.ExactlyOneSelected: model.ChangeEnabled(false, new[] { I18n.Translate("Need exactly one row selected") }, true); break; default: throw new Exception("unsupported selectionNeeded"); } return; } if (extraRowFilterOrNull == null) { model.ChangeEnabled(true, new string[] { }, true); return; } if (datagrid.Selected.Length != datagrid.Selected.Where(extraRowFilterOrNull.Filter).Count()) { model.ChangeEnabled(false, new[] { extraRowFilterOrNull.UserFriendlyReason }, true); return; } model.ChangeEnabled(true, new string[] { }, true); } datagrid.Selected.Changed += (_, __, ___) => UpdateEnabled(); UpdateEnabled(); return(UpdateEnabled); }
private MenuItemUserModel( IReadOnlyValue <string> label, IActionModel <Unit> actionOrNull, IEnumerable <MenuItemUserModel> subItemsOrNull, bool isExitItem, string descriptionOrNull) { Action = actionOrNull; DescriptionOrNull = descriptionOrNull; Label = label; Items = subItemsOrNull; IsExit = isExitItem; }
public static ValueChangedRich <bool> BindEnableAndInitialize <T>(this IActionModel <T> model, IReadOnlyValue <bool> validator) { void Handler(object sender, bool oldValue, bool newValue, IEnumerable <string> errors, bool isUserAction) { model.ChangeEnabled(newValue, errors, isUserAction); Logger.Debug(typeof(ActionModelExtensions), "IActionModelExtensions->BindEnableAndInitialize set enable to {0}", newValue); } validator.Changed += Handler; Handler(validator, validator.Value, validator.Value, validator.Errors, false); return(Handler); }
// REVIEW: valuetuple public static Tuple <ValueChangedRich <bool>, Action> BindAction <WidgetT, T>(this IActionView <WidgetT> view, IActionModel <T> domain) { ValueChangedRich <bool> domainHandler = (sender, oldValue, newValue, errors, isUserAction) => { Logger.Debug(typeof(ActionViewExtensions), "BindAction: changing view to {0} and {1}", newValue, errors.PrettyToString()); view.Enabled = newValue; view.DisabledReason = new HashSet <string>(errors); }; domain.EnabledChanged += domainHandler; Action viewHandler = async() => { Logger.Debug(typeof(ActionViewExtensions), "BindAction: triggering enabled?={0} action {1} from view {2}", domain.Enabled, domain, view); if (!domain.Enabled) { return; } view.State = ActionViewState.CreateOperationRunning(); view.Enabled = false; view.DisabledReason = new HashSet <string> { I18n.Translate("Please wait while operation is running...") }; try { await domain.Trigger(); view.State = ActionViewState.CreateIdleOrSuccess(); } catch (Exception ex) { Logger.Error(typeof(ActionViewExtensions), "Bound action failed to execute {0}", ex); view.State = ActionViewState.CreateOperationFailed(ex); //should be handled by domain.ActionExecuted subscribers } finally { view.Enabled = domain.Enabled; view.DisabledReason = new HashSet <string>(domain.DisabledReasons); } }; view.Triggered += viewHandler; return(new Tuple <ValueChangedRich <bool>, Action>(domainHandler, viewHandler)); }
public static Tuple <ValueChangedRich <bool>, Action> BindActionAndInitialize <WidgetT, T>(this IActionView <WidgetT> view, IActionModel <T> domain) { view.OpensNewTab = domain.ResultInNewTab; var handlers = view.BindAction(domain); handlers.Item1(domain, domain.Enabled, domain.Enabled, domain.DisabledReasons, false); return(handlers); }