From 9e521a188dd53f5282494b8fbd6f52bd7017c781 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Fri, 17 Jun 2011 02:46:32 +0200 Subject: [PATCH] More refactoring. Added global includes. --- README.md | 7 +++ h5ai/footer.html | 2 +- h5ai/main.js | 114 ++++++++++++++++++++++++++++------------------- 3 files changed, 77 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 61be2556..bf61a549 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,13 @@ Changelog --------- +### v0.2.3 +*2011-06-17* + +* more refactoring in main.js +* added custom js support, and global includes + + ### v0.2.2 *2011-06-16* diff --git a/h5ai/footer.html b/h5ai/footer.html index 55d40f9b..38cef0ac 100644 --- a/h5ai/footer.html +++ b/h5ai/footer.html @@ -10,7 +10,7 @@ html5-storage html5-css3 - h5ai + h5ai using HTML5 Boilerplate and diff --git a/h5ai/main.js b/h5ai/main.js index 04bee449..9d6dfc4f 100644 --- a/h5ai/main.js +++ b/h5ai/main.js @@ -7,10 +7,10 @@ $( function() { - checkViewmode(); - addBreadcrumb(); - addTopAndBottom(); + applyViewmode(); + initBreadcrumb(); initViews(); + loadIncludes(); } ); @@ -20,10 +20,26 @@ * config *******************************/ - var columnClasses = [ "icon", "name", "date", "size" ]; - var defaultSortOrder = "C=N;O=A" - var h5aiPath = "/h5ai" - var viewmodes = [ "details", "icons" ]; + var config = { + columnClasses: [ "icon", "name", "date", "size" ], + defaultSortOrder: "C=N;O=A", + viewmodes: [ "details", "icons" ], + store: { + viewmode: "h5ai.viewmode" + }, + icons: { + crumb: "/h5ai/icons/crumb.png", + ascending: "/h5ai/icons/ascending.png", + descending: "/h5ai/icons/descending.png" + }, + globalPath: "/h5ai/global/", + localPrefix: "h5ai.", + includes: { + top: "top.html", + bottom: "bottom.html", + include: "include.js" + } + }; @@ -34,23 +50,19 @@ function getViewmode() { - var viewmode = localStorage.getItem( "h5ai.viewmode" ); - if ( $.inArray( viewmode, viewmodes ) ) { + var viewmode = localStorage.getItem( config.store.viewmode ); + if ( $.inArray( viewmode, config.viewmodes ) ) { return viewmode; }; - return viewmodes[0]; + return config.viewmodes[0]; }; - function setViewmode( viewmode ) { - - localStorage.setItem( "h5ai.viewmode", viewmode ); - checkViewmode(); - }; + function applyViewmode( viewmode ) { - - function checkViewmode() { - + if ( viewmode !== undefined ) { + localStorage.setItem( config.store.viewmode, viewmode ); + } if ( getViewmode() === "icons" ) { $( "#details" ).hide(); $( "#icons" ).show(); @@ -67,7 +79,7 @@ * breadcrumb *******************************/ - function addBreadcrumb() { + function initBreadcrumb() { $( "#domain span" ).text( document.domain ); var pathname = decodeURI( document.location.pathname ); @@ -78,7 +90,7 @@ var part = parts[idx]; if ( part !== "" ) { path += part + "/"; - $ul.append( $( "
  • >" + part + "
  • " ) ); + $ul.append( $( "
  • >" + part + "
  • " ) ); } } @@ -97,7 +109,7 @@ * details view *******************************/ - function convertToHtml5() { + function makeTableHtml5Conform() { $( "#details td" ).removeAttr( "align" ).removeAttr( "valign" ); }; @@ -105,14 +117,14 @@ function getColumnClass( idx ) { - if ( idx >= 0 && idx < columnClasses.length ) { - return columnClasses[idx]; + if ( idx >= 0 && idx < config.columnClasses.length ) { + return config.columnClasses[idx]; } return "unknown"; }; - function addColumnClasses() { + function initTableColumns() { $( "#details tr" ).each( function () { var colIdx = 0; @@ -143,17 +155,17 @@ }; - function addSortOrderIcons() { + function addSortOrderIcon() { var order = document.location.search; if ( order === "" ) { - order = defaultSortOrder; + order = config.defaultSortOrder; } var $icon; if ( order.indexOf( "O=A" ) >= 0 ) { - $icon = $( "ascending" ); + $icon = $( "ascending" ); } else { - $icon = $( "descending" ); + $icon = $( "descending" ); } if ( order.indexOf( "C=N" ) >= 0 ) { $( "#details th.name a" ).append( $icon ); @@ -167,10 +179,10 @@ function initDetailsView() { - convertToHtml5(); - addColumnClasses(); + makeTableHtml5Conform(); + initTableColumns(); initTableRows(); - addSortOrderIcons(); + addSortOrderIcon(); }; @@ -185,13 +197,11 @@ var $div = $( "
    " ); $( "#details td.name a" ).closest( "tr" ).each( function () { var $tr = $( this ); - var icon = $tr.find( "td.icon img" ).attr( "src" ).replace( "icon", "image" ); - var name = $tr.find( "td.name a" ).text(); - $( "
    " ) - .append( $( "" ) ) - .append( $( "
    " + name + "
    " ) ) + var icon = $tr.find( "td.icon img" ).attr( "src" ).replace( "icons", "images" ); + var $link = $tr.find( "td.name a" ); + $( "
    " + $link.text() + "
    " ) .click( function () { - document.location.href = $tr.find( "td.name a" ).attr( "href" ); + document.location.href = $link.attr( "href" ); } ). appendTo( $div ); } ); @@ -213,11 +223,11 @@ $( "#viewdetails" ).closest( "li" ) .click( function () { - setViewmode( "details" ); + applyViewmode( "details" ); } ); $( "#viewicons" ).closest( "li" ) .click( function () { - setViewmode( "icons" ); + applyViewmode( "icons" ); } ); }; @@ -225,21 +235,35 @@ /******************************* - * top and bottom messages + * includes *******************************/ - function addTopAndBottom() { + function loadIncludes() { - $( "#top" ).load( "h5ai.top.html", function( response, status, xhr ) { - if (status != "error") { + $( "#top" ).load( config.localPrefix + config.includes.top, function( response, status ) { + if (status !== "error") { $( "#top" ).show(); + } else { + $( "#top" ).load( config.globalPath + config.includes.top, function( response, status ) { + if (status !== "error") { + $( "#top" ).show(); + } + } ); } } ); - $( "#bottom" ).load( "h5ai.bottom.html", function( response, status, xhr ) { - if (status != "error") { + $( "#bottom" ).load( config.localPrefix + config.includes.bottom, function( response, status ) { + if (status !== "error") { $( "#bottom" ).show(); + } else { + $( "#bottom" ).load( config.globalPath + config.includes.bottom, function( response, status ) { + if (status !== "error") { + $( "#bottom" ).show(); + } + } ); } } ); + $.getScript( config.globalPath + config.includes.include ); + $.getScript( config.localPrefix + config.includes.include ); }; } )( jQuery );