From 5220de5c66dc7a00b97761d7507b2c5330bdfb57 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 28 Oct 2012 12:08:37 -0700 Subject: [PATCH] Lazy load exisitng series New: Existing series will be lazy loaded on Add Series page --- NzbDrone.Web/Helpers/RenderActionHelper.cs | 26 +++++++++++++++++++ NzbDrone.Web/NzbDrone.Web.csproj | 1 + .../Scripts/jquery.unobtrusive-ajax.js | 11 ++++++++ .../Scripts/jquery.unobtrusive-ajax.min.js | 6 +---- NzbDrone.Web/Views/AddSeries/Index.cshtml | 9 ++++++- 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 NzbDrone.Web/Helpers/RenderActionHelper.cs diff --git a/NzbDrone.Web/Helpers/RenderActionHelper.cs b/NzbDrone.Web/Helpers/RenderActionHelper.cs new file mode 100644 index 000000000..8c226d244 --- /dev/null +++ b/NzbDrone.Web/Helpers/RenderActionHelper.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Mvc.Ajax; + +namespace NzbDrone.Web.Helpers +{ + public static class RenderActionHelper + { + public static MvcHtmlString RenderAction(this AjaxHelper helper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) + { + var urlHelper = new UrlHelper(helper.ViewContext.RequestContext); + + var url = urlHelper.Action(actionName, controllerName, routeValues); + + var tagBuilder = new TagBuilder("div"); + if (ajaxOptions != null) tagBuilder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes()); + tagBuilder.MergeAttribute("data-ajax-action-link", "true"); + tagBuilder.MergeAttribute("data-href", url); + + return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index 77b8e0086..5b4724237 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -154,6 +154,7 @@ + diff --git a/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.js b/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.js index eecd7c91c..039a15e28 100644 --- a/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.js +++ b/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.js @@ -160,4 +160,15 @@ data: clickInfo.concat($(this).serializeArray()) }); }); + + //Custom Extensions + $(document).ready(function () { + $("div[data-ajax-action-link=true]").each(function () { + asyncRequest(this, { + url: $(this).attr('data-href'), + type: "GET", + data: [] + }); + }); + }); }(jQuery)); \ No newline at end of file diff --git a/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.min.js b/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.min.js index 3542991c1..87b00b3b9 100644 --- a/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.min.js +++ b/NzbDrone.Web/Scripts/jquery.unobtrusive-ajax.min.js @@ -1,5 +1 @@ -/* -** Unobtrusive Ajax support library for jQuery -** Copyright (C) Microsoft Corporation. All rights reserved. -*/ -(function(a){var b="unobtrusiveAjaxClick",g="unobtrusiveValidation";function c(d,b){var a=window,c=(d||"").split(".");while(a&&c.length)a=a[c.shift()];if(typeof a==="function")return a;b.push(d);return Function.constructor.apply(null,b)}function d(a){return a==="GET"||a==="POST"}function f(b,a){!d(a)&&b.setRequestHeader("X-HTTP-Method-Override",a)}function h(c,b,e){var d;if(e.indexOf("application/x-javascript")!==-1)return;d=(c.getAttribute("data-ajax-mode")||"").toUpperCase();a(c.getAttribute("data-ajax-update")).each(function(f,c){var e;switch(d){case"BEFORE":e=c.firstChild;a("
").html(b).contents().each(function(){c.insertBefore(this,e)});break;case"AFTER":a("
").html(b).contents().each(function(){c.appendChild(this)});break;default:a(c).html(b)}})}function e(b,e){var j,k,g,i;j=b.getAttribute("data-ajax-confirm");if(j&&!window.confirm(j))return;k=a(b.getAttribute("data-ajax-loading"));i=b.getAttribute("data-ajax-loading-duration")||0;a.extend(e,{type:b.getAttribute("data-ajax-method")||undefined,url:b.getAttribute("data-ajax-url")||undefined,beforeSend:function(d){var a;f(d,g);a=c(b.getAttribute("data-ajax-begin"),["xhr"]).apply(this,arguments);a!==false&&k.show(i);return a},complete:function(){k.hide(i);c(b.getAttribute("data-ajax-complete"),["xhr","status"]).apply(this,arguments)},success:function(a,e,d){h(b,a,d.getResponseHeader("Content-Type")||"text/html");c(b.getAttribute("data-ajax-success"),["data","status","xhr"]).apply(this,arguments)},error:c(b.getAttribute("data-ajax-failure"),["xhr","status","error"])});e.data.push({name:"X-Requested-With",value:"XMLHttpRequest"});g=e.type.toUpperCase();if(!d(g)){e.type="POST";e.data.push({name:"X-HTTP-Method-Override",value:g})}a.ajax(e)}function i(c){var b=a(c).data(g);return!b||!b.validate||b.validate()}a("a[data-ajax=true]").live("click",function(a){a.preventDefault();e(this,{url:this.href,type:"GET",data:[]})});a("form[data-ajax=true] input[type=image]").live("click",function(c){var g=c.target.name,d=a(c.target),f=d.parents("form")[0],e=d.offset();a(f).data(b,[{name:g+".x",value:Math.round(c.pageX-e.left)},{name:g+".y",value:Math.round(c.pageY-e.top)}]);setTimeout(function(){a(f).removeData(b)},0)});a("form[data-ajax=true] :submit").live("click",function(c){var e=c.target.name,d=a(c.target).parents("form")[0];a(d).data(b,e?[{name:e,value:c.target.value}]:[]);setTimeout(function(){a(d).removeData(b)},0)});a("form[data-ajax=true]").live("submit",function(d){var c=a(this).data(b)||[];d.preventDefault();if(!i(this))return;e(this,{url:this.action,type:this.method||"GET",data:c.concat(a(this).serializeArray())})})})(jQuery); \ No newline at end of file +(function(n){function i(n,t){for(var i=window,r=(n||"").split(".");i&&r.length;)i=i[r.shift()];return typeof i=="function"?i:(t.push(n),Function.constructor.apply(null,t))}function u(n){return n==="GET"||n==="POST"}function e(n,t){u(t)||n.setRequestHeader("X-HTTP-Method-Override",t)}function o(t,i,r){var u;r.indexOf("application/x-javascript")===-1&&(u=(t.getAttribute("data-ajax-mode")||"").toUpperCase(),n(t.getAttribute("data-ajax-update")).each(function(t,r){var f;switch(u){case"BEFORE":f=r.firstChild,n("
").html(i).contents().each(function(){r.insertBefore(this,f)});break;case"AFTER":n("
").html(i).contents().each(function(){r.appendChild(this)});break;default:n(r).html(i)}}))}function r(t,r){var s,h,f,c;(s=t.getAttribute("data-ajax-confirm"),!s||window.confirm(s))&&(h=n(t.getAttribute("data-ajax-loading")),c=t.getAttribute("data-ajax-loading-duration")||0,n.extend(r,{type:t.getAttribute("data-ajax-method")||undefined,url:t.getAttribute("data-ajax-url")||undefined,beforeSend:function(n){var r;return e(n,f),r=i(t.getAttribute("data-ajax-begin"),["xhr"]).apply(this,arguments),r!==!1&&h.show(c),r},complete:function(){h.hide(c),i(t.getAttribute("data-ajax-complete"),["xhr","status"]).apply(this,arguments)},success:function(n,r,u){o(t,n,u.getResponseHeader("Content-Type")||"text/html"),i(t.getAttribute("data-ajax-success"),["data","status","xhr"]).apply(this,arguments)},error:i(t.getAttribute("data-ajax-failure"),["xhr","status","error"])}),r.data.push({name:"X-Requested-With",value:"XMLHttpRequest"}),f=r.type.toUpperCase(),u(f)||(r.type="POST",r.data.push({name:"X-HTTP-Method-Override",value:f})),n.ajax(r))}function s(t){var i=n(t).data(f);return!i||!i.validate||i.validate()}var t="unobtrusiveAjaxClick",f="unobtrusiveValidation";n("a[data-ajax=true]").live("click",function(n){n.preventDefault(),r(this,{url:this.href,type:"GET",data:[]})}),n("form[data-ajax=true] input[type=image]").live("click",function(i){var r=i.target.name,u=n(i.target),f=u.parents("form")[0],e=u.offset();n(f).data(t,[{name:r+".x",value:Math.round(i.pageX-e.left)},{name:r+".y",value:Math.round(i.pageY-e.top)}]),setTimeout(function(){n(f).removeData(t)},0)}),n("form[data-ajax=true] :submit").live("click",function(i){var r=i.target.name,u=n(i.target).parents("form")[0];n(u).data(t,r?[{name:r,value:i.target.value}]:[]),setTimeout(function(){n(u).removeData(t)},0)}),n("form[data-ajax=true]").live("submit",function(i){var u=n(this).data(t)||[];(i.preventDefault(),s(this))&&r(this,{url:this.action,type:this.method||"GET",data:u.concat(n(this).serializeArray())})}),n(document).ready(function(){n("div[data-ajax-action-link=true]").each(function(){r(this,{url:n(this).attr("data-href"),type:"GET",data:[]})})})})(jQuery) \ No newline at end of file diff --git a/NzbDrone.Web/Views/AddSeries/Index.cshtml b/NzbDrone.Web/Views/AddSeries/Index.cshtml index 9f5506601..a9e15a06c 100644 --- a/NzbDrone.Web/Views/AddSeries/Index.cshtml +++ b/NzbDrone.Web/Views/AddSeries/Index.cshtml @@ -67,7 +67,14 @@

Add Series Already on Disk

- @{ Html.RenderAction("ExistingSeries", "AddSeries"); } + + + @Ajax.RenderAction( + "ExistingSeries", + "AddSeries", + null, + new AjaxOptions { UpdateTargetId = "existingSeries", InsertionMode = InsertionMode.Replace } + )