WizardNavigation.cs

75 lines | 2.422 kB Blame History Raw Download
namespace Web.Controls.WizardNavigation
{
    using DotVVM.Framework.Binding;
    using DotVVM.Framework.Binding.Expressions;
    using DotVVM.Framework.Controls;
    using DotVVM.Framework.Hosting;
    using System.Threading.Tasks;

    public class WizardNavigation : DotvvmMarkupControl
    {
        public Command NextButtonCommand
        {
            get { return (Command)GetValue(NextButtonCommandProperty); }
            set
            {
                SetValue(NextButtonCommandProperty, value);
            }
        }
        public static readonly DotvvmProperty NextButtonCommandProperty
            = DotvvmProperty.Register<Command, WizardNavigation>(c => c.NextButtonCommand, null);

        public Command PreviousButtonCommand
        {
            get { return (Command)GetValue(PreviousButtonCommandProperty); }
            set
            {
                SetValue(PreviousButtonCommandProperty, value);
            }
        }
        public static readonly DotvvmProperty PreviousButtonCommandProperty
            = DotvvmProperty.Register<Command, WizardNavigation>(c => c.PreviousButtonCommand, null);

        protected override void OnInit(IDotvvmRequestContext context)
        {
            var dataContext = DataContext as WizardNavigationViewModel;
            if (dataContext == null)
                throw new DotvvmControlException("Data Context is null.");

            if(dataContext.Total <= 0)
                throw new DotvvmControlException("Initialization was not called.");

            base.OnInit(context);
        }

        public async Task NextCommand()
        {
            var binding = GetCommandBinding(NextButtonCommandProperty);

            if (binding == null)
                return;

            var dataContext = DataContext as WizardNavigationViewModel;
            if (dataContext == null)
                throw new DotvvmControlException("Data Context is null.");

            await dataContext.OnNextClickAsync(NextButtonCommand);
        }

        public async Task PreviousCommand()
        {
            var binding = GetCommandBinding(PreviousButtonCommandProperty);

            if (binding == null)
                return;

            var dataContext = DataContext as WizardNavigationViewModel;
            if (dataContext == null)
                throw new DotvvmControlException("Data Context is null.");

            await dataContext.OnPreviousClickAsync(PreviousButtonCommand);
        }
    }
}