diff --git a/demo/AutoCrudAdmin.Demo.SqlServer/Controllers/TasksController.cs b/demo/AutoCrudAdmin.Demo.SqlServer/Controllers/TasksController.cs index b4bde86..8847c28 100644 --- a/demo/AutoCrudAdmin.Demo.SqlServer/Controllers/TasksController.cs +++ b/demo/AutoCrudAdmin.Demo.SqlServer/Controllers/TasksController.cs @@ -5,7 +5,9 @@ namespace AutoCrudAdmin.Demo.SqlServer.Controllers; using System.Linq; using AutoCrudAdmin.Controllers; using AutoCrudAdmin.Demo.Models.Models; +using AutoCrudAdmin.Attributes; using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Mvc; public class TasksController : AutoCrudAdminController @@ -37,6 +39,12 @@ protected override IEnumerable DateTimeFormats "MM/dd/yyyy hh:mm:ss tt", }; + [InjectScript("/js/tasks.js")] + public override IActionResult Index() + { + return base.Index(); + } + protected override IQueryable ApplyIncludes(IQueryable set) => set.Include(x => x.Project); } \ No newline at end of file diff --git a/demo/AutoCrudAdmin.Demo.SqlServer/wwwroot/js/tasks.js b/demo/AutoCrudAdmin.Demo.SqlServer/wwwroot/js/tasks.js new file mode 100644 index 0000000..9738c56 --- /dev/null +++ b/demo/AutoCrudAdmin.Demo.SqlServer/wwwroot/js/tasks.js @@ -0,0 +1,4 @@ +(function(){ + [...document.getElementsByTagName("td")] + .forEach((td,index)=> td.style.color = index % 2 === 0 ? "red" : "green") +})() \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index ab7fd44..5c2381e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -205,6 +205,21 @@ protected override IEnumerable HiddenFormControlNames {..} See [API Reference](https://github.com/minkov/autocrudadmin/blob/master/src/AutoCrudAdmin/Controllers/AutoCrudAdminController.cs) for full customization options. +### Inject Scripts in endpoints + +The InjectScript attribute allows modular JavaScript injection into views by specifying the script path in the controller action. + +Usage example: +```csharp +[InjectScript("/js/tasks.js")] +public override IActionResult Index() +{ + return base.Index(); +} +``` + +See [Injecting Scripts](inject-script-attribute.md) for more details and options. + ## Troubleshooting Some common issues: diff --git a/docs/inject-script-attribute.md b/docs/inject-script-attribute.md new file mode 100644 index 0000000..146020f --- /dev/null +++ b/docs/inject-script-attribute.md @@ -0,0 +1,71 @@ +# The InjectScript attribute + +The InjectScript attribute is an integral part of the AutoCrudAdmin framework, designed to enhance the EntityForm view by injecting custom JavaScript modules. This attribute allows developers to add client-side logic to the form without modifying the core files. It leverages the power of ASP.NET Core's MVC filters to inject the script automatically. + +# Problem Statement + +Customizing the behavior of an entity form usually requires modifications to the base HTML or JavaScript files, making it hard to manage and update. This traditional method is cumbersome, prone to errors, and not modular. + +# Traditional Methods and Drawbacks + +- Inline Scripting: Embedding JavaScript directly into the HTML. This is not modular and makes the HTML file bulky. + +- Global Script Files: Adding custom logic in a global JavaScript file, making it hard to manage and leading to potential conflicts. + +- Manual Injection: Manually adding ` \ No newline at end of file + + +@Html.Raw(additionalScriptPath != null + ? $"" + : null) \ No newline at end of file diff --git a/src/AutoCrudAdmin/Views/AutoCrudAdmin/Index.cshtml b/src/AutoCrudAdmin/Views/AutoCrudAdmin/Index.cshtml index a96d835..bc45aff 100644 --- a/src/AutoCrudAdmin/Views/AutoCrudAdmin/Index.cshtml +++ b/src/AutoCrudAdmin/Views/AutoCrudAdmin/Index.cshtml @@ -3,6 +3,7 @@ @{ Layout = ViewBag.LayoutName; + var additionalScriptPath = this.ViewData["AdditionalScriptPath"] as string; } @@ -54,5 +55,9 @@ + +@Html.Raw(additionalScriptPath != null + ? $"" + : null) \ No newline at end of file