Skip to content

Instantly share code, notes, and snippets.

@hatsumatsu
Last active August 29, 2015 13:58
Show Gist options
  • Save hatsumatsu/7e6ebd9cc3ea60906cca to your computer and use it in GitHub Desktop.
Save hatsumatsu/7e6ebd9cc3ea60906cca to your computer and use it in GitHub Desktop.
JS path module for Port F
// module path
var path = ( function() {
var settings = {};
var init = function() {
debuglog( 'site.path.init()' );
settings.element = $( 'a[href^="' + globals.blogurl + '"]' )
.not( '#wpadminbar a' );
settings.routed = false;
bindEventHandlers();
saveLinks();
rewriteLinks();
redirectDeeplink();
$( window ).trigger( 'hashchange' );
}
var reInit = function() {
debuglog( 'site.path.reInit()' );
settings.element = $( 'a[href^="' + globals.blogurl + '"]' )
.not( '#wpadminbar a' );
saveLinks();
rewriteLinks();
}
var bindEventHandlers = function() {
$( window ).on( 'hashchange', function( e ) {
debuglog( 'onHashChange' );
var hash = location.hash.replace( '#', '' );
analyzePath( hash );
doActions();
} );
}
var saveLinks = function() {
debuglog( 'site.path.saveLinks()' );
settings.element
.not( 'a[data-url]' )
.each( function() {
$( this ).attr( 'data-url', $( this ).attr( 'href' ) );
} );
}
var rewriteLinks = function() {
debuglog( 'site.path.rewriteLinks()' );
settings.element
.each( function() {
var link = $( this );
var url = link.attr( 'data-url' );
var href = link.attr( 'href' );
if( url && ( link.attr( 'rel' ) != 'keep' ) ) {
href = href.split( globals.blogurl ).join( '#' );
link.attr( 'href', href );
}
} );
}
var redirectDeeplink = function() {
debuglog( 'site.path.redirectDeeplink()' );
var path = window.location.href.replace( globals['blogurl'], '' );
debuglog( 'path: ' + path );
if( window.location.hash == '' &&
path != '' &&
path != '/' &&
path != '#' &&
path != '/#' ) {
var path = '#' + path;
window.location = globals['blogurl'] + '/' + path;
}
}
var analyzePath = function( path ) {
debuglog( 'site.path.analyzePath(' + path + ')' );
var path = path || '';
path = path.split( '/' );
settings._view = settings.view || {};
settings.view = {};
settings.view['section'] = ( path[1] && path[1] != 'page' && path[1] != 'filter' ) ? path[1] : null;
settings.view['item'] = ( path[2] && path[2] != 'page' && path[2] != 'filter' ) ? path[2] : null;
settings.view['filter'] = ( path.indexOf( 'filter' ) > -1 ) ? path[path.indexOf( 'filter' ) + 1] : null;
settings.view['page'] = ( path.indexOf( 'page' ) > -1 ) ? path[path.indexOf( 'page' ) + 1] : null;
setBodyView();
debuglog( settings.view );
}
var doActions = function() {
debuglog( 'site.path.doActions()' );
if( settings.view['section'] ) {
}
if( settings.view['item'] ) {
}
if( settings.view['filter'] ) {
}
if( settings.view['page'] ) {
}
if( !settings.routed ) {
settings.routed = true;
$( 'html' )
.removeClass( 'not-routed' )
.addClass( 'routed' );
$( document ).trigger( 'path/routed' );
}
}
var getSection = function() {
debuglog( 'site.path.getSetion()' );
return settings.view['section'];
}
var getItem = function() {
debuglog( 'site.path.getItem()' );
return settings.view['item'];
}
var getFilter = function() {
debuglog( 'site.path.getFilter()' );
return settings.view['filter'];
}
var getPage = function() {
debuglog( 'site.path.getPage()' );
return settings.view['page'];
}
var construct = function() {
debuglog( 'site.path.construct()' );
var path = '';
path += ( settings.view['section'] ) ? settings.view['section'] + '/' : '';
path += ( settings.view['item'] ) ? settings.view['item'] + '/' : '';
path += ( settings.view['filter'] ) ? 'filter/' + settings.view['filter'] + '/' : '';
path += ( settings.view['page'] ) ? 'page/' + settings.view['page'] + '/' : '';
return path;
}
var set = function( path ) {
debuglog( 'site.path.set( ' + path + ' )' );
location.hash = path;
}
var setBodyView = function() {
var body = $( 'body' );
for( property in settings.view ) {
body.attr( 'data-' + property, settings.view[property] );
}
}
var piwikTrack = function() {
debuglog( 'piwikTrack( ' + construct() + ' )' );
try {
var url = construct();
_paq.push( [ 'setCustomUrl', url ] );
_paq.push( [ 'setDocumentTitle', url ] );
_paq.push( [ 'trackPageView' ] );
} catch( error ) {
debuglog( error )
}
}
return {
init: function() { init(); },
getSection: function() { return getSection(); },
getItem: function() { return getItem(); },
getFilter: function() { return getFilter(); },
getPage: function() { return getPage(); },
construct: function() { return construct() },
set: function( path ) { set( path ); }
}
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment