Skip to content

Commit

Permalink
Added toastr position class
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanbozkus committed Mar 10, 2020
1 parent e19581d commit 6f1905f
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist/formhelper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions sample/FormHelper.Samples/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
// Add FormHelper to the project.
services.AddFormHelper(new FormHelperConfiguration
{
RedirectDelay = 30
});
services.AddFormHelper();

// Add FormHelper to the project with configurations.
//services.AddFormHelper(new FormHelperConfiguration
//{
// CheckTheFormFieldsMessage = "Form alanlarını kontrol ediniz."
// RedirectDelay = 30,
// ToastrDefaultPosition = ToastrPosition.BottomFullWidth
//});

// You can add these validators in a separate class.
Expand Down
2 changes: 2 additions & 0 deletions sample/FormHelper.Samples/Views/Home/HtmlHelper.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
BeforeSubmit = "ProductFormBeforeSubmit", // optional
Callback = "ProductFormCallback", // optional,
// EnableButtonAfterSuccess = true, // default => false
// ResetFormAfterAuccess = false // default => true
// DataType = FormDataType.Json // default => FormData
// ToastrPosition = ToastrPosition.BottomLeft // default => TopRight
};
}

Expand Down
1 change: 1 addition & 0 deletions sample/FormHelper.Samples/Views/Home/TagHelper.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
asp-dataType="FormData"
asp-callback="ProductFormCallback"
asp-beforeSubmit="ProductFormBeforeSubmit"
asp-enableButtonAfterSuccess="true"
asp-resetFormAfterSuccess="false"
enctype="multipart/form-data">

Expand Down
18 changes: 18 additions & 0 deletions src/FormHelper/Enums/ToastrPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FormHelper
{
public enum ToastrPosition
{
TopRight,
BottomRight,
BottomLeft,
TopLeft,
TopFullWidth,
BottomFullWidth,
TopCenter,
BottomCenter
}
}
33 changes: 29 additions & 4 deletions src/FormHelper/Extensions/FormHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public static bool IsAjaxRequest(this HttpRequest request)
return false;
}

public static bool IsMobileDevice(this HttpRequest request)
{
return request.Headers["User-Agent"].ToString().ToLower().Contains("mobi");
}
//private static bool IsMobileDevice(this HttpRequest request)
//{
// return request.Headers["User-Agent"].ToString().ToLower().Contains("mobi");
//}

public static string GenerateCoupon(int length)
{
Expand All @@ -33,5 +33,30 @@ public static string GenerateCoupon(int length)
}
return result.ToString();
}

public static string ToClassName(this ToastrPosition position)
{
switch (position)
{
case ToastrPosition.TopRight:
return "toast-top-right";
case ToastrPosition.BottomRight:
return "toast-bottom-right";
case ToastrPosition.BottomLeft:
return "toast-bottom-left";
case ToastrPosition.TopLeft:
return "toast-top-left";
case ToastrPosition.TopFullWidth:
return "toast-top-full-width";
case ToastrPosition.BottomFullWidth:
return "toast-bottom-full-width";
case ToastrPosition.TopCenter:
return "toast-bottom-full-width";
case ToastrPosition.BottomCenter:
return "toast-bottom-center";
default:
return "toast-top-right";
}
}
}
}
2 changes: 1 addition & 1 deletion src/FormHelper/FormHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>FormHelper</PackageId>
<Version>3.0.5</Version>
<Version>3.1.0</Version>
<Authors>Sinan BOZKUS</Authors>
<projectUrl>https://github.com/sinanbozkus/FormHelper</projectUrl>
<RepositoryUrl>https://github.com/sinanbozkus/FormHelper</RepositoryUrl>
Expand Down
3 changes: 2 additions & 1 deletion src/FormHelper/Helpers/FormHelperHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static HtmlString RenderFormScript(this IHtmlHelper html, FormConfig conf
callback: '{config.Callback}',
enableButtonAfterSuccess: {(config.EnableButtonAfterSuccess ? "true" : "false")},
resetFormAfterSuccess: {(config.ResetFormAfterSuccess ? "true" : "false")},
checkTheFormFieldsMessage: '{configuration.CheckTheFormFieldsMessage}'
checkTheFormFieldsMessage: '{configuration.CheckTheFormFieldsMessage}',
toastrPositionClass: '{(config.ToastrPosition == null ? configuration.ToastrDefaultPosition.ToClassName() : config.ToastrPosition.Value.ToClassName())}'
}});
}});
</script>
Expand Down
41 changes: 23 additions & 18 deletions src/FormHelper/Scripts/formhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

options = $.extend({}, $.formhelper.defaultOptions, options);

if (window.mobileAndTabletcheck() && toastr) {
toastr.options.positionClass = "toast-top-full-width";
}

$form.unbind('submit');

$form.on('submit', function (e) {

e.preventDefault();

var toastrOptions = {
positionClass: window.mobileAndTabletcheck() ? "toast-top-full-width" : options.toastrPositionClass
};

$form.removeData("validator");
$form.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse($form);
Expand All @@ -39,7 +39,7 @@

if (!validationResult) {
if (toastr) {
toastr.error(options.checkTheFormFieldsMessage);
toastr.error(options.checkTheFormFieldsMessage, null, toastrOptions);
}
return false;
}
Expand All @@ -49,6 +49,7 @@
var headers = {};
var formData = {};
var contentType = {};

if (options.dataType === "FormData") {
formData = new FormData($form[0]);
contentType = false;
Expand Down Expand Up @@ -84,13 +85,11 @@
}
},
success: function (result, status) {

if (result.isSucceed === false) {
$form.find("button[type='submit']").removeAttr('disabled');
}

var toastrOptions = {};

if (result.redirectUri) {
toastrOptions = {
timeOut: 0,
Expand All @@ -113,7 +112,7 @@
toastr.error(result.message, null, toastrOptions);
}
} else if (result.isSucceed === false) {
toastr.error(options.checkTheFormFieldsMessage);
toastr.error(options.checkTheFormFieldsMessage, null, toastrOptions);
}

if (result.validationErrors && result.validationErrors.length > 0) {
Expand All @@ -140,18 +139,22 @@
}, hasMessage ? delay : 1);
}

if (options.enableButtonAfterSuccess) {
$form.find("button[type='submit']").removeAttr('disabled');
}


if (result.status === 1) {

if (options.resetFormAfterSuccess) {

$form[0].reset();
if (options.enableButtonAfterSuccess) {
$form.find("button[type='submit']").removeAttr('disabled');
}

if (options.resetFormAfterSuccess) {
$form[0].reset();
}
}
},
error: function (request, status, error) {
console.error(request.responseText);
toastr.error(request.responseText);
toastr.error(request.responseText, null, toastrOptions);
}
});

Expand All @@ -172,7 +175,8 @@
beforeSubmit: null,
callback: null,
enableButtonAfterSuccess: false,
resetFormAfterSuccess: false
resetFormAfterSuccess: false,
toastrPositionClass: null
};


Expand All @@ -191,7 +195,8 @@
beforeSubmit: $(this).attr("beforeSubmit"),
callback: $(this).attr("callback"),
enableButtonAfterSuccess: $(this).attr("enableButtonAfterSuccess") === "True",
resetFormAfterSuccess: $(this).attr("ResetFormAfterSuccess") === "True"
resetFormAfterSuccess: $(this).attr("ResetFormAfterSuccess") === "True",
toastrPositionClass: $(this).attr("toastrPositionClass")
};

return new $.formhelper(options, this);
Expand Down
Loading

0 comments on commit 6f1905f

Please sign in to comment.