Created
August 6, 2010 15:31
-
-
Save balupton/511491 to your computer and use it in GitHub Desktop.
Dan Wiersema Ajaxy Upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:var%20e=document.createElement('script');e.setAttribute('language','javascript');e.setAttribute('src','http://gist.github.com/raw/511491/b4f033402d200486aceab558c819a91f615856b3/danwiersema-ajaxy.js');document.body.appendChild(e);void(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Dan Wiersema Ajaxy Upgrade | |
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com} | |
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com} | |
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html} | |
*/ | |
(function($){ | |
$(function(){ | |
/** | |
* Append a Script to the DOM | |
* @version 1.1.0 | |
* @date July 23, 2010 | |
* @since 1.0.0, June 30, 2010 | |
* @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle} | |
* @author Benjamin "balupton" Lupton {@link http://www.balupton.com} | |
* @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com} | |
* @license GNU Affero General Public License version 3 {@link http://www.gnu.org/licenses/agpl-3.0.html} | |
*/ | |
$.appendScript = $.appendScript || function(url, overwrite){ | |
// Check | |
if ( !(document.body||false) ) { | |
setTimeout(function(){ | |
$.appendScript.apply($,[url,overwrite]); | |
},500); | |
// Chain | |
return $; | |
} | |
// Prepare | |
var id = 'script-'+url.replace(/[^a-zA-Z0-9]/g, '');; | |
var $old = $('#'+id); | |
if ( typeof overwrite === 'undefined' ) { | |
overwrite = false; | |
} | |
// Check | |
if ( $old.length === 1 ) { | |
if ( overwrite ) { | |
$old.remove(); | |
} | |
else { | |
// Chain | |
return $; | |
} | |
} | |
// Create | |
var bodyEl = document.getElementsByTagName($.browser.safari ? 'head' : 'body')[0]; | |
var scriptEl = document.createElement('script'); | |
scriptEl.type = 'text/javascript'; | |
scriptEl.src = url; | |
scriptEl.id = id; | |
bodyEl.appendChild(scriptEl); | |
// Chain | |
return $; | |
}; | |
// Our applications code | |
$.appendScript('http://github.com/balupton/jquery-ajaxy/raw/master/scripts/jquery.ajaxy.js'); | |
// Set our Ready Event | |
var ajaxyReady = function(){ | |
// Fetch Elements | |
var $body = $(document.body), | |
$menu = $('#mainnav > ul'), | |
$content = $('#content'); | |
// Assign Ajaxy Controller | |
$menu.find('a').addClass('ajaxy ajaxy-page').filter(':first').attr('href','/?home'); | |
// Configure Ajaxy | |
$.Ajaxy.configure({ | |
'options': { | |
'root_url': 'http://danwiersema.com', | |
'base_url': '', | |
'redirect': true, | |
'relative_as_base': false, | |
'track_all_internal_links': false | |
}, | |
'Controllers': { | |
'_generic': { | |
request: function(){ | |
// Loading | |
$body.addClass('loading'); | |
// Done | |
return true; | |
}, | |
response: function(){ | |
// Prepare | |
var Ajaxy = $.Ajaxy; var data = this.State.Response.data; var state = this.state||'unknown'; | |
// Title | |
var title = data.title||false; // if we have a title in the response JSON | |
if ( !title && this.state||false ) title = 'Dan Wiersema: '+this.state; // if not use the state as the title | |
if ( title ) document.title = title; // if we have a new title use it | |
// Loaded | |
$body.removeClass('loading'); | |
// Return true | |
return true; | |
}, | |
error: function(){ | |
// Prepare | |
var Ajaxy = $.Ajaxy; var data = this.State.Error.data||this.State.Response.data; var state = this.state||'unknown'; | |
// Error | |
var error = data.error||data.responseText||false; | |
var error_message = data.content||error; | |
// Log what is happening | |
window.console.error('$.Ajaxy.configure.Controllers._generic.error', [this, arguments], error_message); | |
// Loaded | |
$body.removeClass('loading'); | |
// Display State | |
$('#current').text('Our current state is: ['+state+']'); | |
// Done | |
return true; | |
} | |
}, | |
'page': { | |
selector: '.ajaxy-page', | |
matches: /./, // match all | |
request: function(){ | |
// Adjust Menu | |
$menu.children('.active').removeClass('active'); | |
// Hide Content | |
$content.stop(true,true).fadeOut(400); | |
// Return true | |
return true; | |
}, | |
response: function(){ | |
// Prepare | |
var Ajaxy = $.Ajaxy; var data = this.State.Response.data; var state = this.state; | |
// Adjust Menu | |
$menu.children(':has(a[href*="'+state+'"])').addClass('active').siblings('.active').removeClass('active'); | |
// Fetch new content | |
$responseBody = $(data.body); | |
$responseContent = $(data.content).find('#content'); | |
// Adjust body css classes | |
$body.attr('class',$responseBody.attr('class')); | |
// Show Content | |
var Action = this; | |
$content.empty().append($responseContent).fadeIn(400,function(){ | |
$content.find('a[href^=/]:not([rel=shadowbox])').addClass('ajaxy ajaxy-page'); // ajaxify links | |
Action.documentReady($content); | |
}); | |
// Return true | |
return true; | |
} | |
} | |
} | |
}); | |
}; | |
// Set our Check Event | |
var ajaxyCheck = function(){ | |
if ( typeof $.Ajaxy === 'undefined' ) { | |
setTimeout(ajaxyCheck,1000); | |
} | |
else { | |
ajaxyReady(); | |
} | |
} | |
// Fire our Check Event | |
ajaxyCheck(); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment