예제 #1
0
        /// <summary>
        ///     Normalizes the arguments you can give to Swal.fire() in an object of type SweetAlertOptions.
        /// </summary>
        /// <param name="parameters">The array of arguments to normalize.</param>
        /// <exception cref="ArgumentException">Thrown if parameters is not 1, 2, or 3 elements long.</exception>
        public SweetAlertOptions ArgsToParams(IEnumerable <string> parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var parametersList = parameters.ToList();
            var paramLength    = parametersList.Count;

            if (paramLength > 3 || paramLength < 1)
            {
                throw new ArgumentException("Parameters can only be 1, 2, or 3 elements long.");
            }
            var paramEnum = parametersList.GetEnumerator();

            paramEnum.MoveNext();
            var optionsToReturn = new SweetAlertOptions
            {
                Title = paramEnum.Current
            };

            if (paramEnum.MoveNext())
            {
                optionsToReturn.Html = paramEnum.Current;
            }
            if (paramEnum.MoveNext())
            {
                optionsToReturn.Icon = paramEnum.Current;
            }

            paramEnum.Dispose();

            return(optionsToReturn);
        }
예제 #2
0
        /// <summary>
        ///     Function to display a SweetAlert2 modal, with an object of options, all being optional.
        /// </summary>
        /// <example>
        ///     <code>
        /// Swal.FireAsync(new SweetAlertOptions {
        ///     Title = "Auto close alert!",
        ///     Text = "I will close in 2 seconds.",
        ///     Timer = 2000
        /// });
        /// </code>
        /// </example>
        /// <param name="settings"></param>
        public async Task <SweetAlertResult> FireAsync(SweetAlertOptions settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (_defaultOptions != null)
            {
                settings = SweetAlertOptionsMixingService.Mix(_defaultOptions, settings);
            }

            var tcs       = new TaskCompletionSource <SweetAlertResult>();
            var requestId = Guid.NewGuid();

            PendingFireRequests.Add(requestId, tcs);

            AddCallbackToDictionaries(settings, requestId);

            await _jSRuntime.InvokeAsync <SweetAlertResult>(
                "CurrieTechnologies.Razor.SweetAlert2.FireSettings",
                requestId,
                settings.ToPOCO()).ConfigureAwait(false);

            return(await tcs.Task.ConfigureAwait(false));
        }
예제 #3
0
 /// <summary>
 ///     Reuse configuration by creating a Swal instance.
 /// </summary>
 /// <param name="settings">The default options to set for this instance.</param>
 /// <returns></returns>
 public SweetAlertMixin Mixin(SweetAlertOptions settings)
 {
     if (_defaultOptions != null)
     {
         settings = SweetAlertOptionsMixingService.Mix(_defaultOptions, settings);
     }
     return(new SweetAlertMixin(settings, this));
 }
예제 #4
0
        /// <summary>
        /// Function to display a simple SweetAlert2 modal.
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public Task <SweetAlertResult> FireAsync(string title = null, string message = null, SweetAlertIcon icon = null)
        {
            SweetAlertOptions newSettings = this.Mix(this.storedOptions);

            newSettings.Title = title;
            newSettings.Html  = message ?? newSettings.Html;
            newSettings.Icon  = icon ?? newSettings.Icon;
            return(this.swal.FireAsync(newSettings));
        }
예제 #5
0
        /// <summary>
        ///     Function to display a SweetAlert2 modal, with an object of options, all being optional.
        /// </summary>
        /// <param name="settings"></param>
        public Task <SweetAlertResult> FireAsync(SweetAlertOptions settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            return(_swal.FireAsync(Mix(settings)));
        }
예제 #6
0
        /// <summary>
        ///     Reuse configuration by creating a Swal instance.
        /// </summary>
        /// <param name="settings">The default options to set for this instance.</param>
        /// <returns></returns>
        public SweetAlertMixin Mixin(SweetAlertOptions settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            return(new SweetAlertMixin(Mix(settings), _swal));
        }
예제 #7
0
        /// <summary>
        /// Inserts a modal in the queue.
        /// </summary>
        /// <param name="step">The step configuration (same object as in the Swal.fire() call).</param>
        /// <param name="index">The index to insert the step at. By default a modal will be added to the end of a queue.</param>
        public Task <double> InsertQueueStepAsync(SweetAlertOptions step, double?index = null)
        {
            if (step == null)
            {
                throw new ArgumentNullException(nameof(step));
            }

            return(this.swal.InsertQueueStepAsync(this.Mix(step), index));
        }
예제 #8
0
        /// <summary>
        ///     Updates popup options.
        /// </summary>
        /// <param name="newSettings"></param>
        public Task UpdateAsync(SweetAlertOptions newSettings)
        {
            if (newSettings == null)
            {
                throw new ArgumentNullException(nameof(newSettings));
            }

            return(_swal.UpdateAsync(Mix(newSettings)));
        }
예제 #9
0
        /// <summary>
        /// Inserts a modal in the queue.
        /// </summary>
        /// <param name="step">The step configuration (same object as in the Swal.fire() call).</param>
        /// <param name="index">The index to insert the step at. By default a modal will be added to the end of a queue.</param>
        public Task<double> InsertQueueStepAsync(SweetAlertOptions step, double? index = null)
        {
            if (step == null)
            {
                throw new ArgumentNullException(nameof(step));
            }

            var requestId = Guid.NewGuid();
            AddCallbackToDictionaries(step, requestId);
            return jSRuntime.InvokeAsync<double>("CurrieTechnologies.Razor.SweetAlert2.InsertQueueStep", requestId, step.ToPOCO(), index).AsTask();
        }
예제 #10
0
        /// <summary>
        /// Updates popup options.
        /// </summary>
        /// <param name="newSettings"></param>
        public async Task UpdateAsync(SweetAlertOptions newSettings)
        {
            if (newSettings == null)
            {
                throw new ArgumentNullException(nameof(newSettings));
            }

            Guid requestId = Guid.NewGuid();
            AddCallbackToDictionaries(newSettings, requestId);
            await jSRuntime.InvokeAsync<SweetAlertResult>(
                "CurrieTechnologies.Razor.SweetAlert2.Update",
                requestId,
                newSettings.ToPOCO()).ConfigureAwait(false);
        }
예제 #11
0
        private static void AddCallbackToDictionaries(SweetAlertOptions settings, Guid requestId)
        {
            if (settings.PreConfirm != null)
            {
                PreConfirmCallbacks.Add(requestId, settings.PreConfirm);
            }

            if (settings.PreDeny != null)
            {
                PreDenyCallbacks.Add(requestId, settings.PreDeny);
            }

            if (settings.InputValidator != null)
            {
                InputValidatorCallbacks.Add(requestId, settings.InputValidator);
            }

            if (settings.DidOpen != null)
            {
                DidOpenCallbacks.Add(requestId, settings.DidOpen);
            }

            if (settings.WillClose != null)
            {
                WillCloseCallbacks.Add(requestId, settings.WillClose);
            }

            if (settings.DidRender != null)
            {
                DidRenderCallbacks.Add(requestId, settings.DidRender);
            }

            if (settings.WillOpen != null)
            {
                WillOpenCallbacks.Add(requestId, settings.WillOpen);
            }

            if (settings.DidClose != null)
            {
                DidCloseCallbacks.Add(requestId, settings.DidClose);
            }

            if (settings.DidDestroy != null)
            {
                DidDestroyCallbacks.Add(requestId, settings.DidDestroy);
            }
        }
예제 #12
0
        public SweetAlertService(IJSRuntime jSRuntime, SweetAlertServiceOptions options)
        {
            _jSRuntime = jSRuntime;
            if (options == null)
            {
                return;
            }
            _theme             = options.Theme;
            _colorSchemeThemes = options.ColorSchemeThemes
                                 .Select(kvp => new[] { (int)kvp.Key, (int)kvp.Value })
                                 .ToArray();

            if (options.DefaultOptions != null)
            {
                _defaultOptions = options.DefaultOptions;
            }

            _ = SendThemesToJs();
        }
예제 #13
0
        /// <summary>
        /// Function to display a SweetAlert2 modal, with an object of options, all being optional.
        /// </summary>
        /// <example>
        /// <code>
        /// Swal.FireAsync(new SweetAlertOptions {
        ///     Title = "Auto close alert!",
        ///     Text = "I will close in 2 seconds.",
        ///     Timer = 2000
        /// });
        /// </code>
        /// </example>
        /// <param name="settings"></param>
        public async Task<SweetAlertResult> FireAsync(SweetAlertOptions settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var tcs = new TaskCompletionSource<SweetAlertResult>();
            Guid requestId = Guid.NewGuid();
            PendingFireRequests.Add(requestId, tcs);

            AddCallbackToDictionaries(settings, requestId);

            await jSRuntime.InvokeAsync<SweetAlertResult>(
                "CurrieTechnologies.Razor.SweetAlert2.FireSettings",
                requestId,
                settings.ToPOCO(),
                (int)theme,
                colorSchemeThemes).ConfigureAwait(false);
            return await tcs.Task.ConfigureAwait(false);
        }
예제 #14
0
        private static void AddCallbackToDictionaries(SweetAlertOptions settings, Guid requestId)
        {
            if (settings.PreConfirm != null)
            {
                PreConfirmCallbacks.Add(requestId, settings.PreConfirm);
            }

            if (settings.InputValidator != null)
            {
                InputValidatorCallbacks.Add(requestId, settings.InputValidator);
            }

            if (settings.OnOpen != null)
            {
                OnOpenCallbacks.Add(requestId, settings.OnOpen);
            }

            if (settings.OnClose != null)
            {
                OnCloseCallbacks.Add(requestId, settings.OnClose);
            }

            if (settings.OnRender != null)
            {
                OnRenderCallbacks.Add(requestId, settings.OnRender);
            }

            if (settings.OnBeforeOpen != null)
            {
                OnBeforeOpenCallbacks.Add(requestId, settings.OnBeforeOpen);
            }

            if (settings.OnAfterClose != null)
            {
                OnAfterCloseCallbacks.Add(requestId, settings.OnAfterClose);
            }
        }
예제 #15
0
 /// <summary>
 /// Reuse configuration by creating a Swal instance.
 /// </summary>
 /// <param name="settings">The default options to set for this instance.</param>
 /// <returns></returns>
 public SweetAlertMixin Mixin(SweetAlertOptions settings)
 {
     return(new SweetAlertMixin(settings, this));
 }
예제 #16
0
 private SweetAlertOptions Mix(SweetAlertOptions newSettings)
 {
     return(SweetAlertOptionsMixingService.Mix(_storedOptions, newSettings));
 }
예제 #17
0
 internal SweetAlertMixin(SweetAlertOptions settings, SweetAlertService service)
 {
     _storedOptions = settings;
     _swal          = service;
 }
예제 #18
0
 private SweetAlertOptions Mix(SweetAlertOptions newSettings)
 {
     return(new SweetAlertOptions
     {
         Title = newSettings.Title ?? this.storedOptions.Title,
         TitleText = newSettings.TitleText ?? this.storedOptions.TitleText,
         Text = newSettings.Text ?? this.storedOptions.Text,
         Html = newSettings.Html ?? this.storedOptions.Html,
         Footer = newSettings.Footer ?? this.storedOptions.Footer,
         Icon = newSettings.Icon ?? this.storedOptions.Icon,
         IconHtml = newSettings.IconHtml ?? this.storedOptions.IconHtml,
         Backdrop = newSettings.Backdrop ?? this.storedOptions.Backdrop,
         Toast = newSettings.Toast ?? this.storedOptions.Toast,
         Target = newSettings.Target ?? this.storedOptions.Target,
         Input = newSettings.Input ?? this.storedOptions.Input,
         Width = newSettings.Width ?? this.storedOptions.Width,
         Padding = newSettings.Padding ?? this.storedOptions.Padding,
         Background = newSettings.Background ?? this.storedOptions.Background,
         Position = newSettings.Position ?? this.storedOptions.Position,
         Grow = newSettings.Grow ?? this.storedOptions.Grow,
         ShowClass = newSettings.ShowClass ?? this.storedOptions.ShowClass,
         HideClass = newSettings.HideClass ?? this.storedOptions.HideClass,
         CustomClass = newSettings.CustomClass ?? this.storedOptions.CustomClass,
         Timer = newSettings.Timer ?? this.storedOptions.Timer,
         TimerProgressBar = newSettings.TimerProgressBar ?? this.storedOptions.TimerProgressBar,
         HeightAuto = newSettings.HeightAuto ?? this.storedOptions.HeightAuto,
         AllowOutsideClick = newSettings.AllowOutsideClick ?? this.storedOptions.AllowOutsideClick,
         AllowEscapeKey = newSettings.AllowEscapeKey ?? this.storedOptions.AllowEscapeKey,
         AllowEnterKey = newSettings.AllowEnterKey ?? this.storedOptions.AllowEnterKey,
         StopKeydownPropagation = newSettings.StopKeydownPropagation ?? this.storedOptions.StopKeydownPropagation,
         KeydownListenerCapture = newSettings.KeydownListenerCapture ?? this.storedOptions.KeydownListenerCapture,
         ShowConfirmButton = newSettings.ShowConfirmButton ?? this.storedOptions.ShowConfirmButton,
         ShowDenyButton = newSettings.ShowDenyButton ?? this.storedOptions.ShowDenyButton,
         ShowCancelButton = newSettings.ShowCancelButton ?? this.storedOptions.ShowCancelButton,
         ConfirmButtonText = newSettings.ConfirmButtonText ?? this.storedOptions.ConfirmButtonText,
         DenyButtonText = newSettings.DenyButtonText ?? this.storedOptions.DenyButtonText,
         CancelButtonText = newSettings.CancelButtonText ?? this.storedOptions.CancelButtonText,
         ConfirmButtonColor = newSettings.ConfirmButtonColor ?? this.storedOptions.ConfirmButtonColor,
         DenyButtonColor = newSettings.DenyButtonColor ?? this.storedOptions.DenyButtonColor,
         CancelButtonColor = newSettings.CancelButtonColor ?? this.storedOptions.CancelButtonColor,
         ConfirmButtonAriaLabel = newSettings.ConfirmButtonAriaLabel ?? this.storedOptions.ConfirmButtonAriaLabel,
         DenyButtonAriaLabel = newSettings.DenyButtonAriaLabel ?? this.storedOptions.DenyButtonAriaLabel,
         CancelButtonAriaLabel = newSettings.CancelButtonAriaLabel ?? this.storedOptions.CancelButtonAriaLabel,
         ButtonsStyling = newSettings.ButtonsStyling ?? this.storedOptions.ButtonsStyling,
         ReverseButtons = newSettings.ReverseButtons ?? this.storedOptions.ReverseButtons,
         FocusConfirm = newSettings.FocusConfirm ?? this.storedOptions.FocusConfirm,
         FocusDeny = newSettings.FocusDeny ?? this.storedOptions.FocusDeny,
         FocusCancel = newSettings.FocusCancel ?? this.storedOptions.FocusCancel,
         ShowCloseButton = newSettings.ShowCloseButton ?? this.storedOptions.ShowCloseButton,
         CloseButtonHtml = newSettings.CloseButtonHtml ?? this.storedOptions.CloseButtonHtml,
         CloseButtonAriaLabel = newSettings.CloseButtonAriaLabel ?? this.storedOptions.CloseButtonAriaLabel,
         LoaderHtml = newSettings.LoaderHtml ?? this.storedOptions.LoaderHtml,
         ShowLoaderOnConfirm = newSettings.ShowLoaderOnConfirm ?? this.storedOptions.ShowLoaderOnConfirm,
         PreConfirm = newSettings.PreConfirm ?? this.storedOptions.PreConfirm,
         PreDeny = newSettings.PreDeny ?? this.storedOptions.PreDeny,
         ImageUrl = newSettings.ImageUrl ?? this.storedOptions.ImageUrl,
         ImageWidth = newSettings.ImageWidth ?? this.storedOptions.ImageWidth,
         ImageHeight = newSettings.ImageHeight ?? this.storedOptions.ImageHeight,
         ImageAlt = newSettings.ImageAlt ?? this.storedOptions.ImageAlt,
         InputLabel = newSettings.InputLabel ?? this.storedOptions.InputLabel,
         InputPlaceholder = newSettings.InputPlaceholder ?? this.storedOptions.InputPlaceholder,
         InputValue = newSettings.InputValue ?? this.storedOptions.InputValue,
         InputOptions = newSettings.InputOptions ?? this.storedOptions.InputOptions,
         InputAutoTrim = newSettings.InputAutoTrim ?? this.storedOptions.InputAutoTrim,
         InputAttributes = newSettings.InputAttributes ?? this.storedOptions.InputAttributes,
         InputValidator = newSettings.InputValidator ?? this.storedOptions.InputValidator,
         ReturnInputValueOnDeny = newSettings.ReturnInputValueOnDeny ?? this.storedOptions.ReturnInputValueOnDeny,
         ValidationMessage = newSettings.ValidationMessage ?? this.storedOptions.ValidationMessage,
         ProgressSteps = newSettings.ProgressSteps ?? this.storedOptions.ProgressSteps,
         CurrentProgressStep = newSettings.CurrentProgressStep ?? this.storedOptions.CurrentProgressStep,
         ProgressStepsDistance = newSettings.ProgressStepsDistance ?? this.storedOptions.ProgressStepsDistance,
         WillOpen = newSettings.WillOpen ?? this.storedOptions.WillOpen,
         DidClose = newSettings.DidClose ?? this.storedOptions.DidClose,
         DidDestroy = newSettings.DidDestroy ?? this.storedOptions.DidDestroy,
         DidOpen = newSettings.DidOpen ?? this.storedOptions.DidOpen,
         WillClose = newSettings.WillClose ?? this.storedOptions.WillClose,
         DidRender = newSettings.DidRender ?? this.storedOptions.DidRender,
         ScrollbarPadding = newSettings.ScrollbarPadding ?? this.storedOptions.ScrollbarPadding,
     });
 }