Exemplo n.º 1
0
        public void Add(SnackbarType type, string message, Action <SnackbarOptions> configure)
        {
            if (message.IsEmpty())
            {
                return;
            }

            message = message.Trimmed();

            var options = new SnackbarOptions(type, Configuration);

            configure?.Invoke(options);

            var toast = new Snackbar(message, options);

            SnackBarLock.EnterWriteLock();
            try
            {
                if (Configuration.PreventDuplicates && ToastAlreadyPresent(toast))
                {
                    return;
                }
                toast.OnClose += Remove;
                SnackBarList.Add(toast);
            }
            finally
            {
                SnackBarLock.ExitWriteLock();
            }

            OnSnackbarsUpdated?.Invoke();
        }
Exemplo n.º 2
0
        public SnackbarOptions(SnackbarType type, SnackbarConfiguration configuration)
        {
            Type = type;
            SnackbarTypeClass = configuration.SnackbarTypeClass(type);

            MaximumOpacity = configuration.MaximumOpacity;

            ShowTransitionDuration = configuration.ShowTransitionDuration;

            VisibleStateDuration = configuration.VisibleStateDuration;

            HideTransitionDuration = configuration.HideTransitionDuration;

            RequireInteraction = configuration.RequireInteraction;
        }
        private static ActionResult WithSnackbar(ActionResult actionResult, SnackbarType messageType, int timeout, string format, params object[] values)
        {
            // retrieve the current instance of controller that I already stored it in HttpContext.Items while
            // controller factory class execute CreateController method.
            var controller = HttpContext.Current.Items[BaseController.CurrentControllerInstanceKey] as BaseController;

            if (controller.Request.IsAjaxRequest())
            {
                controller.Response.AddHeader(BaseController.SnackbarMessageHeaderKey, controller.Server.UrlEncode(string.Format(format, values)));
                controller.Response.AddHeader(BaseController.SnackbarMessageTypeHeaderKey, messageType.ToString());
                controller.Response.AddHeader(BaseController.SnackbarMessageTimeoutHeaderKey, timeout.ToString());
                return(actionResult);
            }

            var snackBarModel = new SnackbarViewModel(string.Format(format, values), messageType, timeout);

            // add new SnackbarViewModel instance to the LayoutModel which will be read from _Layout.cshtml while rendered.
            controller.LayoutModel.Snackbars.Add(snackBarModel);

            return(actionResult);
        }
        protected ActionResult MessageHelper(SnackbarType messageType, string message, HttpStatusCode statusCode, params object[] values)
        {
            if (!HttpContext.Request.IsAjaxRequest())
            {
                throw new InvalidOperationException("The Request must be Ajax to call these method.");
            }

            var errorMessages = from x in ModelState
                                where x.Value.Errors.Any()
                                select new
            {
                key   = x.Key,
                value = x.Value.Errors.First().ErrorMessage
            };

            Response.StatusCode        = (int)statusCode;
            Response.StatusDescription = message;

            Response.AddHeader(SnackbarMessageHeaderKey, Server.HtmlEncode(message));

            return(Json(new { message = string.Format(message, values), messageType = messageType.ToString(), modelStateErrors = errorMessages.ToArray() }, JsonRequestBehavior.AllowGet));
        }
 public void Show(string message, SnackbarType type = SnackbarType.Info)
 {
     OnShow?.Invoke(message, type);
 }
Exemplo n.º 6
0
 public SnackbarViewModel(string message, SnackbarType type, int timeout)
 {
     Message = message;
     Type    = type;
     Timeout = timeout;
 }