/** * Ajaxy - An Ajax-integration solution * (c) Copyright James Padolsey * For sale on ThemeForest.net */ // Delete this line in production code: if ( !window.jQuery ) { alert("jQuery is required for Ajaxy to run."); throw new Error("Ajaxy requires jQuery!"); } jQuery.ajaxy = (function(){ var settings = { /*** SETTINGS ***/ /* See documentation file, it will tell you how to configure Ajaxy */ domainBase: 'http://www.wvpe.com', base: '', homepage: 'index.php', anchorFilter: function() { // Add mailto to keep from redirecting mailto links. Jeremy Barngrover (3/23/2010) return !/.(pdf|jpe?g|gif|png|bmp|tif|txt|mp3|wmv|wma)$|(mailto)/.test(this.href); }, update: ['#container','title', '#session_data'], updateHandler: { '#container': function(newContent) { // '#content': function(newContent) { jQuery(this).animate({ height: "hide", opacity: 0 }, function(){ jQuery(this).html(newContent).animate({ height: "show", opacity: 1 }); log(jQuery('#container .script')+jQuery('#container .script').size()); jQuery.each(jQuery('#container .script'), function() { eval(this.innerHTML); } ); }); } }, initialStyles: "h1{visibility:hidden;} #content{height:0;opacity:0;overflow:hidden;}", loader: { css: { position: 'relative', background: '#FFF url(media/images/loader2.gif) no-repeat center', MozBorderRadius: 4, WebkitBorderRadius: 4, padding: '5px', fontSize: '.8em', opacity: 0, top: jQuery('#header').offset().top, left: -12, height: 16, width: 16 }, html: "", attr: {}, init: function(loader){ loader.appendTo('#header'); jQuery(document).ready(function(){ loader.css({ top: jQuery('#header').position().top + 88 }); }); }, show: function(loader, href){ loader.stop(true).animate({ opacity: 1 }); }, hide: function(loader){ loader.animate({ opacity: 0 }); } }, errorHandler: function( XHR ) { if (XHR.status === 404) { //$('#content').html('

404 Page Not Found

'); jQuery.ajaxy.load("404.php"); settings.loader.hide( ajaxy.loader ); } }, autoInit: true, cache: false /*** END SETTINGS ***/ }, /********************************** *** DON'T TOUCH ANYTHING BELOW *** **********************************/ run = false, HEAD = jQuery('head')[0], BODY = document.body, util = { isExternal: function(url) { return !RegExp('^' + location.href.match(/.+?\/(?=[^\/])/)[0] + location.hostname).test(url); }, hash: { bind: function(newHashFn) { //jQuery.historyInit( newHashFn ); jQuery.history.init(newHashFn); }, load: function(h){ return jQuery.history.load(h); } } }; function log(a) { arguments[0] = '[AJAXY] ' + a; return window.console && console.log && console.log.apply( console, arguments ); } /** Ajaxy **/ function load(href) { log("Load called: ", href); if (ajaxy.requestInProgress) { return; } /* Prepare loader */ if (!ajaxy.loader) { ajaxy.loader = jQuery('
'); ajaxy.loader .css(settings.loader.css) .html(settings.loader.html) .attr(settings.loader.attr) .appendTo(BODY); settings.loader.init(ajaxy.loader); } settings.loader.show( ajaxy.loader, href ); ajaxy.requestInProgress = true; var requestPath = /\//.test(href) ? href.replace(/\/[^\/]+$/, '/') : ''; // Deal with absolute URLs href = href.replace( window.location.href.replace(/(:\/\/.+?\/).+/, '$1') + settings.base, '' ); // Deal with based-relative URLs href = href.replace( settings.base, '' ); jQuery.ajax({ url: href, cache: settings.cache, type: 'GET', complete: function(response, status) { log("XHR: " + status); if ( status !== "success" && status !== "notmodified" ) { return; } ajaxy.requestInProgress = false; var responseText = response.responseText .replace(//,'') .replace(/<(\/?)(body|head|html|title)(?=.*?>)/g, '<$1ajaxy$2') .replace(//g, "") .replace(/((?:href|src|action)\s*=\s*)('|")((?:\S|\\\2)+?)(\2|\s)/g, // Correct relative URLs, by prepending the current path. // mailto added to allow contact page mailto links. Jeremy Barngrover (3/23/2010) function($0,$1,$2,URL,$4){ if ( !/^https?:\/\/|^\/|(mailto:)/.test(URL) ) { return $1 + $2 + requestPath + URL + $4; } return $0; } ), specialHTML = (function(){ var elMatches = responseText.match(//ig), out = {}; if (elMatches) { jQuery.each(elMatches, function(i, match){ out[match.match(/<(.+?)(>|\s)/)[1].toLowerCase()] = match.replace(/^').append(responseText); domResponse.children().each(function(){ if ( /^(meta|style|link)$/i.test(this.nodeName) ) { jQuery(this).appendTo(HEAD); } if ( /^(script)$/i.test(this.nodeName) ) { jQuery(this).remove(); } }); settings.update = jQuery( settings.update[0].nodeName || (settings.update instanceof jQuery) || jQuery.isArray(settings.update) ? settings.update : [settings.update] ); settings.update.each(function(i, item){ log("Updating item: ", item); var $item = jQuery(item), clone = $item.clone(true), selector = $item.selector.replace(/(^|\s)(body|html|head)$/, '$1ajaxy$2'); if ( selector === 'title' ) { // De-entitify title, before setting document.title = jQuery('
').append((responseText.match(/(.+?)<\/ajaxytitle>/i)||['',''])[1]).text(); return true; } $item.each(function(i){ var replacement = domResponse.find(selector).eq(i); if (!replacement[0]) {return true;} if ( /^ajaxy/i.test(replacement[0].nodeName) ) { replacement = jQuery(specialHTML[replacement[0].nodeName.toLowerCase()]).append(replacement[0].childNodes); } if (settings.updateHandler && settings.updateHandler[selector]) { settings.updateHandler[selector].call( this, replacement.html(), replacement ); } else { jQuery(this).replaceWith( replacement ); } }); return true; }); settings.loader.hide( ajaxy.loader, href ); // Hide initialHidden elems if (ajaxy.headStyle) { ajaxy.headStyle.remove(); } }, error: function() { ajaxy.requestInProgress = false; if (ajaxy.headStyle) { ajaxy.headStyle.remove(); } return settings.errorHandler.apply(this, arguments); } }); } function redirectToHomePage() { var loc = window.location; /* Users landing on a.html will be redirected to * homepage.html#a.html ... */ var fullPath = settings.base + settings.homepage; // added code to look for #tag so redirects stop. Allows static bookmarking. Jeremy Barngrover (3/23/2010) if (loc.pathname !== fullPath && loc.pathname.indexOf(settings.base) === 0 && !(loc.hash)) { var curPage = loc.pathname.replace(settings.base, ''); if (curPage) { window.location = settings.base + settings.homepage + '#' + curPage; } } } function ajaxy(ops) { if (!run){ redirectToHomePage(); } if (settings.initialStyles && window.location.hash) { ajaxy.headStyle = ajaxy.headStyle || jQuery('').appendTo('head'); } /* Wait for body to be ready */ if (!document.body) { var interval = setInterval(function(){ if (document.body) { BODY = document.body; clearInterval(interval); ajaxy(ops); } }, 20); return; } if (run) { return; } else { run = true; } util.hash.bind(load, settings.base); jQuery.extend( settings, ops ); jQuery('a').live('click', function(e){ var href = this.href, matches = jQuery.isFunction(settings.anchorFilter) ? settings.anchorFilter.call(this) : jQuery(this).is(settings.anchorFilter); var url = this.href.replace(jQuery.ajaxy.settings.domainBase,""); if ( !util.isExternal(href) && matches && e.button === 0 ) { log("Matching anchor clicked: ", url); jQuery('html,body').animate({scrollTop:0},'fast'); //jQuery('#content').css({backgroundColor:'#213e66'}).animate({opacity:0},'fast'); // log("Matching anchor clicked: ", this.pathname); e.preventDefault(); util.hash.load( url /* this.pathname .replace(/^[^\/]/, '/$&') .replace(settings.base, '') */ ); return false; } }); } if (settings.autoInit) { jQuery(document).ready(function() { ajaxy(); }); } ajaxy.load = load; ajaxy.settings = settings; return ajaxy; })();