From fff8fc8adb6092dd16c4e30c8d93a94105bd91b3 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Fri, 8 May 2020 20:00:38 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0FAjax=E6=94=AF=E6=8C=81?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TagHelpers/FAjaxFunTagHelper.cs | 21 ++
FAuth/Extensions/TagHelpers/FAjaxTagHelper.cs | 80 +++++++
FAuth/FAuth.csproj | 10 +-
FAuth/Views/Shared/_Layout.cshtml | 1 +
FAuth/Views/_ViewImports.cshtml | 1 +
.../jquery.unobtrusive-ajax.js | 205 ++++++++++++++++++
.../jquery.unobtrusive-ajax.min.js | 16 ++
7 files changed, 332 insertions(+), 2 deletions(-)
create mode 100644 FAuth/Extensions/TagHelpers/FAjaxFunTagHelper.cs
create mode 100644 FAuth/Extensions/TagHelpers/FAjaxTagHelper.cs
create mode 100644 FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.js
create mode 100644 FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.min.js
diff --git a/FAuth/Extensions/TagHelpers/FAjaxFunTagHelper.cs b/FAuth/Extensions/TagHelpers/FAjaxFunTagHelper.cs
new file mode 100644
index 0000000..e33ae90
--- /dev/null
+++ b/FAuth/Extensions/TagHelpers/FAjaxFunTagHelper.cs
@@ -0,0 +1,21 @@
+using Microsoft.AspNetCore.Razor.TagHelpers;
+
+namespace FAuth.Extensions.TagHelpers
+{
+ [HtmlTargetElement("a",Attributes = "fajaxfun")]
+ [HtmlTargetElement("form",Attributes = "fajaxfun")]
+ public class FAjaxFunTagHelper:TagHelper
+ {
+ [HtmlAttributeName("fajaxfun")]
+ public string Fun { get; set; }
+
+ public override void Process(TagHelperContext context,TagHelperOutput output) {
+ //data-ajax="true" data-ajax-update="main" data-ajax-mode="replace"
+ output.Attributes.SetAttribute("data-ajax","true");
+ output.Attributes.SetAttribute("data-ajax-update",this.Fun);
+ output.Attributes.SetAttribute("data-ajax-mode","replace");
+
+ //base.Process(context,output);
+ }
+ }
+}
diff --git a/FAuth/Extensions/TagHelpers/FAjaxTagHelper.cs b/FAuth/Extensions/TagHelpers/FAjaxTagHelper.cs
new file mode 100644
index 0000000..042147e
--- /dev/null
+++ b/FAuth/Extensions/TagHelpers/FAjaxTagHelper.cs
@@ -0,0 +1,80 @@
+using Falcon.Extend;
+using Microsoft.AspNetCore.Razor.TagHelpers;
+
+namespace FAuth.Extensions.TagHelpers
+{
+ ///
+ /// 实现ajax调用的属性
+ ///
+ [HtmlTargetElement("a")]
+ [HtmlTargetElement("form")]
+ public class FAjaxTagHelper:TagHelper
+ {
+ ///
+ /// 是否允许FAjax,默认True,一般不需要设置
+ ///
+ [HtmlAttributeName("fajax-enable")]
+ public bool Enable { get; set; } = true;
+ ///
+ /// 更新元素选择器
+ ///
+ [HtmlAttributeName("fajax-updata")]
+ public string Updata { get; set; } = "";
+ ///
+ /// 更新模式。默认为空,可选BEFORE、AFTER或REPLACE-WITH
+ ///
+ [HtmlAttributeName("fajax-model")]
+ public string Model { get; set; } = "";
+ ///
+ /// 开始ajax调用之前执行
+ ///
+ [HtmlAttributeName("fajax-begin")]
+ public string OnBegin { get; set; } = "";
+ ///
+ /// 调用ajax之后执行
+ ///
+ [HtmlAttributeName("fajax-complete")]
+ public string OnComplete { get; set; } = "";
+ ///
+ /// 调用成功后执行
+ ///
+ [HtmlAttributeName("fajax-success")]
+ public string OnSuccess { get; set; } = "";
+ ///
+ /// 调用失败执行
+ ///
+ [HtmlAttributeName("fajax-failure")]
+ public string OnFailure { get; set; } = "";
+
+ public override void Process(TagHelperContext context,TagHelperOutput output) {
+ if(this.Updata.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-update",this.Updata);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(this.Model.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-mode",this.Model);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(this.OnBegin.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-begin",this.OnBegin);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(this.OnComplete.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-complete",this.OnComplete);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(this.OnSuccess.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-success",this.OnSuccess);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(this.OnFailure.IsNotNullOrEmpty()) {
+ output.Attributes.SetAttribute("data-ajax-failure",this.OnFailure);
+ output.Attributes.SetAttribute("data-ajax","true");
+ }
+ if(!this.Enable) {
+ output.Attributes.RemoveAll("data-ajax");
+ }
+ //base.Process(context,output);
+ }
+ }
+}
diff --git a/FAuth/FAuth.csproj b/FAuth/FAuth.csproj
index 9086620..63fcf95 100644
--- a/FAuth/FAuth.csproj
+++ b/FAuth/FAuth.csproj
@@ -1,4 +1,4 @@
-
+
netcoreapp3.1
@@ -11,6 +11,8 @@
+
+
@@ -49,6 +51,10 @@
-
+
+
+
+
+
diff --git a/FAuth/Views/Shared/_Layout.cshtml b/FAuth/Views/Shared/_Layout.cshtml
index fadf70b..297dd94 100644
--- a/FAuth/Views/Shared/_Layout.cshtml
+++ b/FAuth/Views/Shared/_Layout.cshtml
@@ -41,6 +41,7 @@
+
@RenderSection("Scripts", required: false)
diff --git a/FAuth/Views/_ViewImports.cshtml b/FAuth/Views/_ViewImports.cshtml
index 48daad5..c2e2b46 100644
--- a/FAuth/Views/_ViewImports.cshtml
+++ b/FAuth/Views/_ViewImports.cshtml
@@ -1,3 +1,4 @@
@using FAuth
@using FAuth.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
+@addTagHelper FAuth.Extensions.TagHelpers.*, FAuth
\ No newline at end of file
diff --git a/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.js b/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.js
new file mode 100644
index 0000000..85a9b1d
--- /dev/null
+++ b/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.js
@@ -0,0 +1,205 @@
+// Unobtrusive Ajax support library for jQuery
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+// @version v3.2.6
+//
+// Microsoft grants you the right to use these script files for the sole
+// purpose of either: (i) interacting through your browser with the Microsoft
+// website or online service, subject to the applicable licensing or use
+// terms; or (ii) using the files as included with a Microsoft product subject
+// to that product's license terms. Microsoft reserves all other rights to the
+// files not expressly granted by Microsoft, whether by implication, estoppel
+// or otherwise. Insofar as a script file is dual licensed under GPL,
+// Microsoft neither took the code under GPL nor distributes it thereunder but
+// under the terms set out in this paragraph. All notices and licenses
+// below are for informational purposes only.
+
+/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: false */
+/*global window: false, jQuery: false */
+
+(function ($) {
+ var data_click = "unobtrusiveAjaxClick",
+ data_target = "unobtrusiveAjaxClickTarget",
+ data_validation = "unobtrusiveValidation";
+
+ function getFunction(code, argNames) {
+ var fn = window, parts = (code || "").split(".");
+ while (fn && parts.length) {
+ fn = fn[parts.shift()];
+ }
+ if (typeof (fn) === "function") {
+ return fn;
+ }
+ argNames.push(code);
+ return Function.constructor.apply(null, argNames);
+ }
+
+ function isMethodProxySafe(method) {
+ return method === "GET" || method === "POST";
+ }
+
+ function asyncOnBeforeSend(xhr, method) {
+ if (!isMethodProxySafe(method)) {
+ xhr.setRequestHeader("X-HTTP-Method-Override", method);
+ }
+ }
+
+ function asyncOnSuccess(element, data, contentType) {
+ var mode;
+
+ if (contentType.indexOf("application/x-javascript") !== -1) { // jQuery already executes JavaScript for us
+ return;
+ }
+
+ mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
+ $(element.getAttribute("data-ajax-update")).each(function (i, update) {
+ var top;
+
+ switch (mode) {
+ case "BEFORE":
+ $(update).prepend(data);
+ break;
+ case "AFTER":
+ $(update).append(data);
+ break;
+ case "REPLACE-WITH":
+ $(update).replaceWith(data);
+ break;
+ default:
+ $(update).html(data);
+ break;
+ }
+ });
+ }
+
+ function asyncRequest(element, options) {
+ var confirm, loading, method, duration;
+
+ confirm = element.getAttribute("data-ajax-confirm");
+ if (confirm && !window.confirm(confirm)) {
+ return;
+ }
+
+ loading = $(element.getAttribute("data-ajax-loading"));
+ duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0;
+
+ $.extend(options, {
+ type: element.getAttribute("data-ajax-method") || undefined,
+ url: element.getAttribute("data-ajax-url") || undefined,
+ cache: (element.getAttribute("data-ajax-cache") || "").toLowerCase() === "true",
+ beforeSend: function (xhr) {
+ var result;
+ asyncOnBeforeSend(xhr, method);
+ result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments);
+ if (result !== false) {
+ loading.show(duration);
+ }
+ return result;
+ },
+ complete: function () {
+ loading.hide(duration);
+ getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments);
+ },
+ success: function (data, status, xhr) {
+ asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
+ getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments);
+ },
+ error: function () {
+ getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments);
+ }
+ });
+
+ options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
+
+ method = options.type.toUpperCase();
+ if (!isMethodProxySafe(method)) {
+ options.type = "POST";
+ options.data.push({ name: "X-HTTP-Method-Override", value: method });
+ }
+
+ // change here:
+ // Check for a Form POST with enctype=multipart/form-data
+ // add the input file that were not previously included in the serializeArray()
+ // set processData and contentType to false
+ var $element = $(element);
+ if ($element.is("form") && $element.attr("enctype") == "multipart/form-data") {
+ var formdata = new FormData();
+ $.each(options.data, function (i, v) {
+ formdata.append(v.name, v.value);
+ });
+ $("input[type=file]", $element).each(function () {
+ var file = this;
+ $.each(file.files, function (n, v) {
+ formdata.append(file.name, v);
+ });
+ });
+ $.extend(options, {
+ processData: false,
+ contentType: false,
+ data: formdata
+ });
+ }
+ // end change
+
+ $.ajax(options);
+ }
+
+ function validate(form) {
+ var validationInfo = $(form).data(data_validation);
+ return !validationInfo || !validationInfo.validate || validationInfo.validate();
+ }
+
+ $(document).on("click", "a[data-ajax=true]", function (evt) {
+ evt.preventDefault();
+ asyncRequest(this, {
+ url: this.href,
+ type: "GET",
+ data: []
+ });
+ });
+
+ $(document).on("click", "form[data-ajax=true] input[type=image]", function (evt) {
+ var name = evt.target.name,
+ target = $(evt.target),
+ form = $(target.parents("form")[0]),
+ offset = target.offset();
+
+ form.data(data_click, [
+ { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
+ { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
+ ]);
+
+ setTimeout(function () {
+ form.removeData(data_click);
+ }, 0);
+ });
+
+ $(document).on("click", "form[data-ajax=true] :submit", function (evt) {
+ var name = evt.currentTarget.name,
+ target = $(evt.target),
+ form = $(target.parents("form")[0]);
+
+ form.data(data_click, name ? [{ name: name, value: evt.currentTarget.value }] : []);
+ form.data(data_target, target);
+
+ setTimeout(function () {
+ form.removeData(data_click);
+ form.removeData(data_target);
+ }, 0);
+ });
+
+ $(document).on("submit", "form[data-ajax=true]", function (evt) {
+ var clickInfo = $(this).data(data_click) || [],
+ clickTarget = $(this).data(data_target),
+ isCancel = clickTarget && (clickTarget.hasClass("cancel") || clickTarget.attr('formnovalidate') !== undefined);
+ evt.preventDefault();
+ if (!isCancel && !validate(this)) {
+ return;
+ }
+ asyncRequest(this, {
+ url: this.action,
+ type: this.method || "GET",
+ data: clickInfo.concat($(this).serializeArray())
+ });
+ });
+}(jQuery));
diff --git a/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.min.js b/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.min.js
new file mode 100644
index 0000000..bccd1f7
--- /dev/null
+++ b/FAuth/wwwroot/lib/jquery.unobtrusive-ajax/jquery.unobtrusive-ajax.min.js
@@ -0,0 +1,16 @@
+// Unobtrusive Ajax support library for jQuery
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+// @version v3.2.6
+//
+// Microsoft grants you the right to use these script files for the sole
+// purpose of either: (i) interacting through your browser with the Microsoft
+// website or online service, subject to the applicable licensing or use
+// terms; or (ii) using the files as included with a Microsoft product subject
+// to that product's license terms. Microsoft reserves all other rights to the
+// files not expressly granted by Microsoft, whether by implication, estoppel
+// or otherwise. Insofar as a script file is dual licensed under GPL,
+// Microsoft neither took the code under GPL nor distributes it thereunder but
+// under the terms set out in this paragraph. All notices and licenses
+// below are for informational purposes only.
+!function(t){function a(t,a){for(var e=window,r=(t||"").split(".");e&&r.length;)e=e[r.shift()];return"function"==typeof e?e:(a.push(t),Function.constructor.apply(null,a))}function e(t){return"GET"===t||"POST"===t}function r(t,a){e(a)||t.setRequestHeader("X-HTTP-Method-Override",a)}function n(a,e,r){var n;r.indexOf("application/x-javascript")===-1&&(n=(a.getAttribute("data-ajax-mode")||"").toUpperCase(),t(a.getAttribute("data-ajax-update")).each(function(a,r){switch(n){case"BEFORE":t(r).prepend(e);break;case"AFTER":t(r).append(e);break;case"REPLACE-WITH":t(r).replaceWith(e);break;default:t(r).html(e)}}))}function i(i,u){var o,c,d,s;if(o=i.getAttribute("data-ajax-confirm"),!o||window.confirm(o)){c=t(i.getAttribute("data-ajax-loading")),s=parseInt(i.getAttribute("data-ajax-loading-duration"),10)||0,t.extend(u,{type:i.getAttribute("data-ajax-method")||void 0,url:i.getAttribute("data-ajax-url")||void 0,cache:"true"===(i.getAttribute("data-ajax-cache")||"").toLowerCase(),beforeSend:function(t){var e;return r(t,d),e=a(i.getAttribute("data-ajax-begin"),["xhr"]).apply(i,arguments),e!==!1&&c.show(s),e},complete:function(){c.hide(s),a(i.getAttribute("data-ajax-complete"),["xhr","status"]).apply(i,arguments)},success:function(t,e,r){n(i,t,r.getResponseHeader("Content-Type")||"text/html"),a(i.getAttribute("data-ajax-success"),["data","status","xhr"]).apply(i,arguments)},error:function(){a(i.getAttribute("data-ajax-failure"),["xhr","status","error"]).apply(i,arguments)}}),u.data.push({name:"X-Requested-With",value:"XMLHttpRequest"}),d=u.type.toUpperCase(),e(d)||(u.type="POST",u.data.push({name:"X-HTTP-Method-Override",value:d}));var p=t(i);if(p.is("form")&&"multipart/form-data"==p.attr("enctype")){var f=new FormData;t.each(u.data,function(t,a){f.append(a.name,a.value)}),t("input[type=file]",p).each(function(){var a=this;t.each(a.files,function(t,e){f.append(a.name,e)})}),t.extend(u,{processData:!1,contentType:!1,data:f})}t.ajax(u)}}function u(a){var e=t(a).data(d);return!e||!e.validate||e.validate()}var o="unobtrusiveAjaxClick",c="unobtrusiveAjaxClickTarget",d="unobtrusiveValidation";t(document).on("click","a[data-ajax=true]",function(t){t.preventDefault(),i(this,{url:this.href,type:"GET",data:[]})}),t(document).on("click","form[data-ajax=true] input[type=image]",function(a){var e=a.target.name,r=t(a.target),n=t(r.parents("form")[0]),i=r.offset();n.data(o,[{name:e+".x",value:Math.round(a.pageX-i.left)},{name:e+".y",value:Math.round(a.pageY-i.top)}]),setTimeout(function(){n.removeData(o)},0)}),t(document).on("click","form[data-ajax=true] :submit",function(a){var e=a.currentTarget.name,r=t(a.target),n=t(r.parents("form")[0]);n.data(o,e?[{name:e,value:a.currentTarget.value}]:[]),n.data(c,r),setTimeout(function(){n.removeData(o),n.removeData(c)},0)}),t(document).on("submit","form[data-ajax=true]",function(a){var e=t(this).data(o)||[],r=t(this).data(c),n=r&&(r.hasClass("cancel")||void 0!==r.attr("formnovalidate"));a.preventDefault(),(n||u(this))&&i(this,{url:this.action,type:this.method||"GET",data:e.concat(t(this).serializeArray())})})}(jQuery);
\ No newline at end of file