diff --git a/src/h5ai/js/main.js b/src/h5ai/js/main.js index 064b47a3..fff302b5 100644 --- a/src/h5ai/js/main.js +++ b/src/h5ai/js/main.js @@ -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( $( "
  • >" + part + "
  • " ) ); + 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( $( "
    empty
    " ) ); - } - }; + 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 = $( "ascending" ); - } else { - $icon = $( "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 = $( "
    " ); - $( "#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 = $( "
    " + $link.text() + "
    " ) + + + /******************************* + * 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( $( "
  • >" + part + "
  • " ) ); + } + } + + $( "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( $( "
    " ) ); - $( "#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( $( "
    empty
    " ) ); + } + }; + + + var addSortOrderIcon = function () { + + var order = document.location.search; + if ( order === "" ) { + order = config.defaultSortOrder; + } + var $icon; + if ( order.indexOf( "O=A" ) >= 0 ) { + $icon = $( "ascending" ); + } else { + $icon = $( "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 = $( "
    " ); + $( "#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 = $( "
    " + $link.text() + "
    " ) + .click( function () { + document.location.href = $link.attr( "href" ); + } ). + appendTo( $div ); + if ( $tr.hasClass( "folder" ) ) { + $entry.addClass( "folder" ); + } else { + $entry.addClass( "file" ); + } + } ); + $div.append( $( "
    " ) ); + $( "#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 ); diff --git a/target/h5ai/js/main.js b/target/h5ai/js/main.js index ff96a09c..b5a074ef 100644 --- a/target/h5ai/js/main.js +++ b/target/h5ai/js/main.js @@ -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("
  • >"+p+"
  • "))}}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&&pempty"))}}function e(){var p=document.location.search;if(p===""){p=d.defaultSortOrder}var q;if(p.indexOf("O=A")>=0){q=h("ascending")}else{q=h("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("
    ");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("
    "+q.text()+"
    ").click(function(){document.location.href=q.attr("href")}).appendTo(p);if(t.hasClass("folder")){r.addClass("folder")}else{r.addClass("file")}});p.append(h("
    "));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); \ No newline at end of file +(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("
  • >"+s+"
  • "))}}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&&sempty"))}};var f=function(){var s=document.location.search;if(s===""){s=e.defaultSortOrder}var t;if(s.indexOf("O=A")>=0){t=a("ascending")}else{t=a("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("
    ");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("
    "+t.text()+"
    ").click(function(){document.location.href=t.attr("href")}).appendTo(s);if(w.hasClass("folder")){u.addClass("folder")}else{u.addClass("file")}});s.append(a("
    "));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); \ No newline at end of file