-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.viewloader.min.js
38 lines (30 loc) · 4.19 KB
/
jquery.viewloader.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
jQuery viewloader plugin
Version 0.5 (10-June-2017)
@requires jQuery 1.5 and above (for the Deferred support)
Copyright (c) 2017 Philippe Monnet (@techarch) http://blog.monnet-usa.com
Source: http://github.com/techarch/jquery-viewloader
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
Example:
$(document).viewloader({
versionId: '1.0.0-1',
logLevel: "debug",
afterEachTemplate: function (templateId) {
// code to execute after each view has been successfully loaded
},
success: function (successfulResolution) {
// Code to execute once all views have been loaded for the page
},
error: function (failedResolution) {
// Code to execute if an error occurs while loading the views
});
*/
(function(c){"function"!=typeof c.Deferred?alert("view-loader requires jQuery v1.5 or later! You are using v"+c.fn.jquery):(c.viewloader=function(h,l){var m={versionId:"",logLevel:"none",viewExtension:".view.html",scripts:null,afterEachTemplate:null,success:null,error:null},b=this;b.id=h.id||h.toString();var n=c(h);b.settings={};b.init=function(){b.settings=c.extend({},m,l);b.templates=[];b.loadAllPartialViews()};b.loadAllPartialViews=function(){(b.settings.scripts||n.find("script[src*='"+b.settings.viewExtension+
"']")).each(function(){var a=c(this),d=a.attr("id"),e=a.attr("src");if(0==c("script[id="+d+"]").length){var f=b.settings.versionId||"";0<f.length&&(e+="?versionId="+f,a.attr("src",e));d="<script id='"+d+"' type='text/html' src='"+e+"' >\x3c/script>";c("body").append(d)}d=c.Deferred();d.templateID=a.attr("id");d.templateSrc=a.attr("src");d.execute=b.loadSourceForPartialView;b.templates.push(d)});c.when.apply(b,b.templates).done(function(a){b.settings.success?(f("viewloader.loadAllPartialViews executing 'success' function",
"debug"),b.settings.success(a)):f("viewloader.loadAllPartialViews all done since no 'success' function was provided","debug")}).fail(function(a){b.settings.error?(f("viewloader.loadAllPartialViews executing 'error' function","debug"),b.settings.error(a)):f("viewloader.loadAllPartialViews all done since no 'error' function was provided","debug")});for(var a=0;a<b.templates.length;a++){var e=b.templates[a];e.execute(e)}};b.loadSourceForPartialView=function(a){f("viewloader.loadSourceForPartialView loading source for view: id="+
a.templateID+" src="+a.templateSrc,"debug");c.ajax({url:a.templateSrc,data:a.templateID,error:function(b,c,d){f("viewloader.loadSourceForPartialView could not find the view with id: "+a.templateID+" at url: "+a.templateSrc,"error");a.reject(a.templateID+" : "+d.message)},success:function(e,h,d){e=c("#"+a.templateID);if(0==e.length)f("viewloader.loadSourceForPartialView could not find script with id: "+a.templateID,"error"),a.resolve(null);else{f("viewloader.loadSourceForPartialView saving source in "+
a.templateID,"debug");var k=d.responseText;e[0].text=k;var g=function(a){if(this.settings.afterEachTemplate){f("viewloader.loadSourceForPartialView execute afterEachTemplate callback for "+a.templateID,"debug");try{this.settings.afterEachTemplate(a.templateID)}catch(p){f("viewloader.loadSourceForPartialView afterEachTemplate callback for "+a.templateID+" failure: "+p,"error")}}a.resolve(a.templateID)};d=c(k).find("script[type$=html]");0<d.length?(d.each(function(){var a=c(this).attr("id");k=k.replace(a,
"_"+a+"_")}),e[0].text=k,e.viewloader({versionId:b.settings.versionId,logLevel:"debug",afterEachTemplate:b.settings.afterEachTemplate,scripts:d,success:function(c){g.call(b,a)},error:function(c){g.call(b,a)}})):g.call(b,a)}}})};var f=function(a,e){if("none"!=b.settings.logLevel)if(c.browser.mozilla||c.browser.chrome||c.browser.webkit)switch(e){case "debug":try{console.debug(a)}catch(g){}break;case "error":try{console.error(a)}catch(g){}break;case "info":try{console.info(a)}catch(g){}break;default:try{console.debug(a)}catch(g){}}else if(c.browser.msie)try{console.log(a)}catch(g){}};
b.init()},c.fn.viewloader=function(h){return this.each(function(){if(void 0==c(this).data("viewloader")){var l=new c.viewloader(this,h);c(this).data("viewloader",l)}})})})(jQuery);