Refactored js to be an object.

This commit is contained in:
Lars Jung 2011-06-24 15:14:45 +02:00
parent b07d702896
commit e07f05864c
2 changed files with 285 additions and 255 deletions

View file

@ -1,300 +1,324 @@
( function( $ ) {
/*******************************
* init after dom load
*******************************/
$( function() {
initH5ai();
applyViewmode();
initBreadcrumb();
initViews();
customize();
$.h5ai = new H5ai();
} );
/*******************************
* config
* h5ai
*******************************/
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"
},
customHeader: "h5ai.header.html",
customFooter: "h5ai.footer.html"
};
H5ai = function ( options ) {
/*******************************
* config
*******************************/
/*******************************
* init h5ai extension
*******************************/
function initH5ai() {
H5ai = function () {
var folderClickFns = [];
var fileClickFns = [];
this.folderClick = function ( fn ) {
if ( typeof fn === "function" ) {
folderClickFns.push( fn );
};
};
this.fileClick = function ( fn ) {
if ( typeof fn === "function" ) {
fileClickFns.push( fn );
};
};
this.applyFolderClick = function ( label ) {
for ( idx in folderClickFns ) {
folderClickFns[idx].call( window, label );
};
};
this.applyFileClick = function ( label ) {
for ( idx in fileClickFns ) {
fileClickFns[idx].call( window, label );
};
};
};
$.h5ai = new H5ai();
};
/*******************************
* local stored viewmode
*******************************/
function getViewmode() {
var viewmode = localStorage.getItem( config.store.viewmode );
if ( $.inArray( viewmode, config.viewmodes ) ) {
return viewmode;
};
return config.viewmodes[0];
};
function applyViewmode( viewmode ) {
if ( viewmode !== undefined ) {
localStorage.setItem( config.store.viewmode, viewmode );
}
if ( getViewmode() === "icons" ) {
$( "#details" ).hide();
$( "#icons" ).show();
} else {
$( "#details" ).show();
$( "#icons" ).hide();
}
};
/*******************************
* breadcrumb
*******************************/
function initBreadcrumb() {
$( "#domain span" ).text( document.domain );
var pathname = decodeURI( document.location.pathname );
var parts = pathname.split( "/" );
var path = "/";
var $ul = $( "nav ul" );
for ( idx in parts ) {
var part = parts[idx];
if ( part !== "" ) {
path += part + "/";
$ul.append( $( "<li class='crumb'><a href='" + path + "'><img src='" + config.icons.crumb + "' alt='>' />" + part + "</a></li>" ) );
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"
},
customHeader: "h5ai.header.html",
customFooter: "h5ai.footer.html",
callbacks: {
folderClick: [],
fileClick: []
}
}
$( "nav li a" ).closest( "li" )
.click( function () {
document.location.href = $( this ).find( "a" ).attr( "href" );
} );
document.title = document.domain + pathname;
};
};
/*******************************
* public api
*******************************/
this.folderClick = function ( fn ) {
/*******************************
* details view
*******************************/
function makeTableHtml5Conform() {
$( "#details td" ).removeAttr( "align" ).removeAttr( "valign" );
};
function getColumnClass( idx ) {
if ( idx >= 0 && idx < config.columnClasses.length ) {
return config.columnClasses[idx];
}
return "unknown";
};
function initTableColumns() {
$( "#details tr" ).each( function () {
var colIdx = 0;
$( this ).find( "th,td" ).each( function () {
$( this ).addClass( getColumnClass( colIdx ) );
colIdx++;
} );
} );
};
function initTableRows() {
$( "#details th a" ).closest( "th" )
.addClass( "header" )
.click( function () {
document.location.href = $( this ).find( "a" ).attr( "href" );
} );
$( "#details td.name a" ).closest( "tr" )
.addClass( "entry" )
.click( function () {
document.location.href = $( this ).find( "td.name a" ).attr( "href" );
} );
$( "#details tr.entry" ).each( function () {
var $row = $( this );
$row.find( "td.name a" ).addClass( "label" );
if ( $row.find( "td.icon img" ).attr( "alt" ) === "[DIR]" ) {
$row.addClass( "folder" );
} else {
$row.addClass( "file" );
if ( typeof fn === "function" ) {
config.callbacks.folderClick.push( fn );
};
} );
$entries = $( "#details tr.entry" );
if ( $entries.size() === 0 || $entries.size() === 1 && $entries.find( "td.name a" ).text() === "Parent Directory" ) {
$( "#details" ).append( $( "<div class='empty'>empty</div>" ) );
}
};
return this;
};
function addSortOrderIcon() {
this.fileClick = function ( fn ) {
var order = document.location.search;
if ( order === "" ) {
order = config.defaultSortOrder;
}
var $icon;
if ( order.indexOf( "O=A" ) >= 0 ) {
$icon = $( "<img src='" + config.icons.ascending + "' class='sort' alt='ascending' />" );
} else {
$icon = $( "<img src='" + config.icons.descending + "' class='sort' alt='descending' />" );
}
if ( order.indexOf( "C=N" ) >= 0 ) {
$( "#details th.name a" ).append( $icon );
} else if ( order.indexOf( "C=M" ) >= 0 ) {
$( "#details th.date a" ).prepend( $icon );
} else if ( order.indexOf( "C=S" ) >= 0 ) {
$( "#details th.size a" ).prepend( $icon );
}
};
function initDetailsView() {
makeTableHtml5Conform();
initTableColumns();
initTableRows();
addSortOrderIcon();
};
if ( typeof fn === "function" ) {
config.callbacks.fileClick.push( fn );
};
return this;
};
/*******************************
* init (will be called at the bottom)
*******************************/
/*******************************
* icons view
*******************************/
var init = function () {
function initIconsView() {
applyViewmode();
initBreadcrumb();
initViews();
customize();
};
var $div = $( "<div></div>" );
$( "#details td.name a" ).closest( "tr" ).each( function () {
var $tr = $( this );
var icon = $tr.find( "td.icon img" ).attr( "src" ).replace( "icons", "images" );
var $link = $tr.find( "td.name a" );
var $entry = $( "<div class='entry'><img src='" + icon + "' /><div class='label'>" + $link.text() + "</div></div>" )
/*******************************
* callback triggers
*******************************/
var triggerFolderClick = function ( label ) {
for ( idx in config.callbacks.folderClick ) {
config.callbacks.folderClick[idx].call( window, label );
};
};
var triggerFileClick = function ( label ) {
for ( idx in config.callbacks.fileClick ) {
config.callbacks.fileClick[idx].call( window, label );
};
};
/*******************************
* local stored viewmode
*******************************/
var getViewmode = function () {
var viewmode = localStorage.getItem( config.store.viewmode );
if ( $.inArray( viewmode, config.viewmodes ) ) {
return viewmode;
};
return config.viewmodes[0];
};
var applyViewmode = function ( viewmode ) {
if ( viewmode !== undefined ) {
localStorage.setItem( config.store.viewmode, viewmode );
}
if ( getViewmode() === "icons" ) {
$( "#details" ).hide();
$( "#icons" ).show();
} else {
$( "#details" ).show();
$( "#icons" ).hide();
}
};
/*******************************
* breadcrumb
*******************************/
var initBreadcrumb = function () {
$( "#domain span" ).text( document.domain );
var pathname = decodeURI( document.location.pathname );
var parts = pathname.split( "/" );
var path = "/";
var $ul = $( "nav ul" );
for ( idx in parts ) {
var part = parts[idx];
if ( part !== "" ) {
path += part + "/";
$ul.append( $( "<li class='crumb'><a href='" + path + "'><img src='" + config.icons.crumb + "' alt='>' />" + part + "</a></li>" ) );
}
}
$( "nav li a" ).closest( "li" )
.click( function () {
document.location.href = $link.attr( "href" );
} ).
appendTo( $div );
if ( $tr.hasClass( "folder" ) ) {
$entry.addClass( "folder" );
} else {
$entry.addClass( "file" );
document.location.href = $( this ).find( "a" ).attr( "href" );
} );
document.title = document.domain + pathname;
};
/*******************************
* details view
*******************************/
var makeTableHtml5Conform = function () {
$( "#details td" ).removeAttr( "align" ).removeAttr( "valign" );
};
var getColumnClass = function ( idx ) {
if ( idx >= 0 && idx < config.columnClasses.length ) {
return config.columnClasses[idx];
}
} );
$div.append( $( "<div class='clearfix'></div>" ) );
$( "#icons" ).append( $div );
};
return "unknown";
};
var initTableColumns = function () {
/*******************************
* init views
*******************************/
function initViews() {
initDetailsView();
initIconsView();
$( "#content .entry.folder" ).click( function() {
$.h5ai.applyFolderClick( $( this ).find( ".label" ).text() );
} );
$( "#content .entry.file" ).click( function() {
$.h5ai.applyFileClick( $( this ).find( ".label" ).text() );
} );
$( "#viewdetails" ).closest( "li" )
.click( function () {
applyViewmode( "details" );
$( "#details tr" ).each( function () {
var colIdx = 0;
$( this ).find( "th,td" ).each( function () {
$( this ).addClass( getColumnClass( colIdx ) );
colIdx++;
} );
} );
$( "#viewicons" ).closest( "li" )
.click( function () {
applyViewmode( "icons" );
};
var initTableRows = function () {
$( "#details th a" ).closest( "th" )
.addClass( "header" )
.click( function () {
document.location.href = $( this ).find( "a" ).attr( "href" );
} );
$( "#details td.name a" ).closest( "tr" )
.addClass( "entry" )
.click( function () {
document.location.href = $( this ).find( "td.name a" ).attr( "href" );
} );
$( "#details tr.entry" ).each( function () {
var $row = $( this );
$row.find( "td.name a" ).addClass( "label" );
if ( $row.find( "td.icon img" ).attr( "alt" ) === "[DIR]" ) {
$row.addClass( "folder" );
} else {
$row.addClass( "file" );
};
} );
};
$entries = $( "#details tr.entry" );
if ( $entries.size() === 0 || $entries.size() === 1 && $entries.find( "td.name a" ).text() === "Parent Directory" ) {
$( "#details" ).append( $( "<div class='empty'>empty</div>" ) );
}
};
var addSortOrderIcon = function () {
var order = document.location.search;
if ( order === "" ) {
order = config.defaultSortOrder;
}
var $icon;
if ( order.indexOf( "O=A" ) >= 0 ) {
$icon = $( "<img src='" + config.icons.ascending + "' class='sort' alt='ascending' />" );
} else {
$icon = $( "<img src='" + config.icons.descending + "' class='sort' alt='descending' />" );
}
if ( order.indexOf( "C=N" ) >= 0 ) {
$( "#details th.name a" ).append( $icon );
} else if ( order.indexOf( "C=M" ) >= 0 ) {
$( "#details th.date a" ).prepend( $icon );
} else if ( order.indexOf( "C=S" ) >= 0 ) {
$( "#details th.size a" ).prepend( $icon );
}
};
var initDetailsView = function () {
makeTableHtml5Conform();
initTableColumns();
initTableRows();
addSortOrderIcon();
};
/*******************************
* icons view
*******************************/
/*******************************
* customize
*******************************/
var initIconsView = function () {
var $div = $( "<div></div>" );
$( "#details td.name a" ).closest( "tr" ).each( function () {
var $tr = $( this );
var icon = $tr.find( "td.icon img" ).attr( "src" ).replace( "icons", "images" );
var $link = $tr.find( "td.name a" );
var $entry = $( "<div class='entry'><img src='" + icon + "' /><div class='label'>" + $link.text() + "</div></div>" )
.click( function () {
document.location.href = $link.attr( "href" );
} ).
appendTo( $div );
if ( $tr.hasClass( "folder" ) ) {
$entry.addClass( "folder" );
} else {
$entry.addClass( "file" );
}
} );
$div.append( $( "<div class='clearfix'></div>" ) );
$( "#icons" ).append( $div );
};
/*******************************
* init views
*******************************/
var initViews = function () {
initDetailsView();
initIconsView();
$( "#content .entry.folder" )
.click( function() {
triggerFolderClick( $( this ).find( ".label" ).text() );
} );
$( "#content .entry.file" )
.click( function() {
triggerFileClick( $( this ).find( ".label" ).text() );
} );
$( "#viewdetails" ).closest( "li" )
.click( function () {
applyViewmode( "details" );
} );
$( "#viewicons" ).closest( "li" )
.click( function () {
applyViewmode( "icons" );
} );
};
/*******************************
* customize
*******************************/
var customize = function () {
function customize() {
try {
$.ajax( {
url: config.customHeader,
dataType: "html",
@ -302,8 +326,7 @@
$( "#content > header" ).append( $( data ) ).show();
}
} );
} catch( err ) {};
try {
$.ajax( {
url: config.customFooter,
dataType: "html",
@ -311,8 +334,15 @@
$( "#content > footer" ).prepend( $( data ) ).show();
}
} );
} catch( err ) {};
};
/*******************************
* finally run init
*******************************/
init();
};
} )( jQuery );

View file

@ -1 +1 @@
(function(h){h(function(){i();a();f();n();k()});var d={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"},customHeader:"h5ai.header.html",customFooter:"h5ai.footer.html"};function i(){H5ai=function(){var p=[];var q=[];this.folderClick=function(r){if(typeof r==="function"){p.push(r)}};this.fileClick=function(r){if(typeof r==="function"){q.push(r)}};this.applyFolderClick=function(r){for(idx in p){p[idx].call(window,r)}};this.applyFileClick=function(r){for(idx in q){q[idx].call(window,r)}}};h.h5ai=new H5ai()}function g(){var p=localStorage.getItem(d.store.viewmode);if(h.inArray(p,d.viewmodes)){return p}return d.viewmodes[0]}function a(p){if(p!==undefined){localStorage.setItem(d.store.viewmode,p)}if(g()==="icons"){h("#details").hide();h("#icons").show()}else{h("#details").show();h("#icons").hide()}}function f(){h("#domain span").text(document.domain);var t=decodeURI(document.location.pathname);var s=t.split("/");var r="/";var q=h("nav ul");for(idx in s){var p=s[idx];if(p!==""){r+=p+"/";q.append(h("<li class='crumb'><a href='"+r+"'><img src='"+d.icons.crumb+"' alt='>' />"+p+"</a></li>"))}}h("nav li a").closest("li").click(function(){document.location.href=h(this).find("a").attr("href")});document.title=document.domain+t}function c(){h("#details td").removeAttr("align").removeAttr("valign")}function j(p){if(p>=0&&p<d.columnClasses.length){return d.columnClasses[p]}return"unknown"}function b(){h("#details tr").each(function(){var p=0;h(this).find("th,td").each(function(){h(this).addClass(j(p));p++})})}function m(){h("#details th a").closest("th").addClass("header").click(function(){document.location.href=h(this).find("a").attr("href")});h("#details td.name a").closest("tr").addClass("entry").click(function(){document.location.href=h(this).find("td.name a").attr("href")});h("#details tr.entry").each(function(){var p=h(this);p.find("td.name a").addClass("label");if(p.find("td.icon img").attr("alt")==="[DIR]"){p.addClass("folder")}else{p.addClass("file")}});$entries=h("#details tr.entry");if($entries.size()===0||$entries.size()===1&&$entries.find("td.name a").text()==="Parent Directory"){h("#details").append(h("<div class='empty'>empty</div>"))}}function e(){var p=document.location.search;if(p===""){p=d.defaultSortOrder}var q;if(p.indexOf("O=A")>=0){q=h("<img src='"+d.icons.ascending+"' class='sort' alt='ascending' />")}else{q=h("<img src='"+d.icons.descending+"' class='sort' alt='descending' />")}if(p.indexOf("C=N")>=0){h("#details th.name a").append(q)}else{if(p.indexOf("C=M")>=0){h("#details th.date a").prepend(q)}else{if(p.indexOf("C=S")>=0){h("#details th.size a").prepend(q)}}}}function l(){c();b();m();e()}function o(){var p=h("<div></div>");h("#details td.name a").closest("tr").each(function(){var t=h(this);var s=t.find("td.icon img").attr("src").replace("icons","images");var q=t.find("td.name a");var r=h("<div class='entry'><img src='"+s+"' /><div class='label'>"+q.text()+"</div></div>").click(function(){document.location.href=q.attr("href")}).appendTo(p);if(t.hasClass("folder")){r.addClass("folder")}else{r.addClass("file")}});p.append(h("<div class='clearfix'></div>"));h("#icons").append(p)}function n(){l();o();h("#content .entry.folder").click(function(){h.h5ai.applyFolderClick(h(this).find(".label").text())});h("#content .entry.file").click(function(){h.h5ai.applyFileClick(h(this).find(".label").text())});h("#viewdetails").closest("li").click(function(){a("details")});h("#viewicons").closest("li").click(function(){a("icons")})}function k(){try{h.ajax({url:d.customHeader,dataType:"html",success:function(q){h("#content > header").append(h(q)).show()}})}catch(p){}try{h.ajax({url:d.customFooter,dataType:"html",success:function(q){h("#content > footer").prepend(h(q)).show()}})}catch(p){}}})(jQuery);
(function(a){a(function(){a.h5ai=new H5ai()});H5ai=function(r){var e={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"},customHeader:"h5ai.header.html",customFooter:"h5ai.footer.html",callbacks:{folderClick:[],fileClick:[]}};this.folderClick=function(s){if(typeof s==="function"){e.callbacks.folderClick.push(s)}return this};this.fileClick=function(s){if(typeof s==="function"){e.callbacks.fileClick.push(s)}return this};var p=function(){b();h();n();k()};var o=function(s){for(idx in e.callbacks.folderClick){e.callbacks.folderClick[idx].call(window,s)}};var g=function(s){for(idx in e.callbacks.fileClick){e.callbacks.fileClick[idx].call(window,s)}};var i=function(){var s=localStorage.getItem(e.store.viewmode);if(a.inArray(s,e.viewmodes)){return s}return e.viewmodes[0]};var b=function(s){if(s!==undefined){localStorage.setItem(e.store.viewmode,s)}if(i()==="icons"){a("#details").hide();a("#icons").show()}else{a("#details").show();a("#icons").hide()}};var h=function(){a("#domain span").text(document.domain);var w=decodeURI(document.location.pathname);var v=w.split("/");var u="/";var t=a("nav ul");for(idx in v){var s=v[idx];if(s!==""){u+=s+"/";t.append(a("<li class='crumb'><a href='"+u+"'><img src='"+e.icons.crumb+"' alt='>' />"+s+"</a></li>"))}}a("nav li a").closest("li").click(function(){document.location.href=a(this).find("a").attr("href")});document.title=document.domain+w};var d=function(){a("#details td").removeAttr("align").removeAttr("valign")};var j=function(s){if(s>=0&&s<e.columnClasses.length){return e.columnClasses[s]}return"unknown"};var c=function(){a("#details tr").each(function(){var s=0;a(this).find("th,td").each(function(){a(this).addClass(j(s));s++})})};var m=function(){a("#details th a").closest("th").addClass("header").click(function(){document.location.href=a(this).find("a").attr("href")});a("#details td.name a").closest("tr").addClass("entry").click(function(){document.location.href=a(this).find("td.name a").attr("href")});a("#details tr.entry").each(function(){var s=a(this);s.find("td.name a").addClass("label");if(s.find("td.icon img").attr("alt")==="[DIR]"){s.addClass("folder")}else{s.addClass("file")}});$entries=a("#details tr.entry");if($entries.size()===0||$entries.size()===1&&$entries.find("td.name a").text()==="Parent Directory"){a("#details").append(a("<div class='empty'>empty</div>"))}};var f=function(){var s=document.location.search;if(s===""){s=e.defaultSortOrder}var t;if(s.indexOf("O=A")>=0){t=a("<img src='"+e.icons.ascending+"' class='sort' alt='ascending' />")}else{t=a("<img src='"+e.icons.descending+"' class='sort' alt='descending' />")}if(s.indexOf("C=N")>=0){a("#details th.name a").append(t)}else{if(s.indexOf("C=M")>=0){a("#details th.date a").prepend(t)}else{if(s.indexOf("C=S")>=0){a("#details th.size a").prepend(t)}}}};var l=function(){d();c();m();f()};var q=function(){var s=a("<div></div>");a("#details td.name a").closest("tr").each(function(){var w=a(this);var v=w.find("td.icon img").attr("src").replace("icons","images");var t=w.find("td.name a");var u=a("<div class='entry'><img src='"+v+"' /><div class='label'>"+t.text()+"</div></div>").click(function(){document.location.href=t.attr("href")}).appendTo(s);if(w.hasClass("folder")){u.addClass("folder")}else{u.addClass("file")}});s.append(a("<div class='clearfix'></div>"));a("#icons").append(s)};var n=function(){l();q();a("#content .entry.folder").click(function(){o(a(this).find(".label").text())});a("#content .entry.file").click(function(){g(a(this).find(".label").text())});a("#viewdetails").closest("li").click(function(){b("details")});a("#viewicons").closest("li").click(function(){b("icons")})};var k=function(){a.ajax({url:e.customHeader,dataType:"html",success:function(s){a("#content > header").append(a(s)).show()}});a.ajax({url:e.customFooter,dataType:"html",success:function(s){a("#content > footer").prepend(a(s)).show()}})};p()}})(jQuery);