diff --git a/src/_h5ai/client/js/inc/core/entry.js b/src/_h5ai/client/js/inc/core/entry.js index ace3e99f..03e374a4 100644 --- a/src/_h5ai/client/js/inc/core/entry.js +++ b/src/_h5ai/client/js/inc/core/entry.js @@ -1,13 +1,5 @@ -modulejs.define('core/entry', ['_', 'config', 'model/entry'], function (_, config, Entry) { +modulejs.define('core/entry', ['core/location'], function (location) { - _.each(config.entries || [], function (entry) { - - Entry.get(entry.absHref, entry.time, entry.size, entry.status, entry.content); - }); - - var entry = Entry.get(); - entry.status = '=h5ai='; - - return entry; + return location.getItem(); }); diff --git a/src/_h5ai/client/js/inc/core/location.js b/src/_h5ai/client/js/inc/core/location.js index bea9577b..8a870525 100644 --- a/src/_h5ai/client/js/inc/core/location.js +++ b/src/_h5ai/client/js/inc/core/location.js @@ -1,7 +1,13 @@ -modulejs.define('core/location', [], function () { +modulejs.define('core/location', ['_', 'modernizr', 'core/settings', 'core/event', 'core/notify'], function (_, modernizr, allsettings, event, notify) { - var doc = document, + var settings = _.extend({ + smartBrowsing: false + }, allsettings.view), + + doc = document, + + history = settings.smartBrowsing && modernizr.history ? window.history : null, forceEncoding = function (href) { @@ -17,36 +23,109 @@ modulejs.define('core/location', [], function () { .replace(/\=/g, '%3D'); }, - absHref = (function () { + reUriToPathname = /^.*:\/\/[^\/]*|[^\/]*$/g, + uriToPathname = function (uri) { - var rePrePathname = /.*:\/\/[^\/]*/, - rePostPathname = /[^\/]*$/, + return uri.replace(reUriToPathname, ''); + }, - uriToPathname = function (uri) { + hrefsAreDecoded = (function () { - return uri.replace(rePrePathname, '').replace(rePostPathname, ''); - }, - - testpathname = '/a b', - a = doc.createElement('a'), - isDecoded, location; + var testpathname = '/a b', + a = doc.createElement('a'); a.href = testpathname; - isDecoded = uriToPathname(a.href) === testpathname; + return uriToPathname(a.href) === testpathname; + }()), - a.href = doc.location.href; + encodedHref = function (href) { + + var a = doc.createElement('a'), + location; + + a.href = href; location = uriToPathname(a.href); - if (isDecoded) { + if (hrefsAreDecoded) { location = encodeURIComponent(location).replace(/%2F/ig, '/'); } return forceEncoding(location); - }()); + }; + + + var absHref = null, + + getDomain = function () { + + return doc.domain; + }, + + getAbsHref = function () { + + return absHref; + }, + + getItem = function () { + + return modulejs.require('model/entry').get(absHref); + }, + + setLocation = function (newAbsHref, keepBrowserUrl) { + + newAbsHref = encodedHref(newAbsHref); + if (absHref !== newAbsHref) { + absHref = newAbsHref; + event.pub('location.changed', absHref); + + notify.set('loading...'); + modulejs.require('core/refresh')(function () { notify.set(); }); + + if (history) { + if (keepBrowserUrl) { + history.replaceState({absHref: absHref}, '', absHref); + } else { + history.pushState({absHref: absHref}, '', absHref); + } + } + } + }, + + setLink = function ($el, item) { + + $el.attr('href', item.absHref); + + if (history && item.isFolder() && item.status === '=h5ai=') { + $el.on('click', function () { + + setLocation(item.absHref); + return false; + }); + } + + if (item.status !== '=h5ai=') { + $el.attr('target', '_blank'); + } + }; + + + if (history) { + window.onpopstate = function (e) { + + if (e.state && e.state.absHref) { + setLocation(e.state.absHref, true); + } + }; + } + return { - domain: doc.domain, - absHref: absHref, - forceEncoding: forceEncoding + forceEncoding: forceEncoding, + encodedHref: encodedHref, + getDomain: getDomain, + getAbsHref: getAbsHref, + getItem: getItem, + setLocation: setLocation, + setLink: setLink }; }); diff --git a/src/_h5ai/client/js/inc/core/refresh.js b/src/_h5ai/client/js/inc/core/refresh.js index 23d60b84..0ac66bcf 100644 --- a/src/_h5ai/client/js/inc/core/refresh.js +++ b/src/_h5ai/client/js/inc/core/refresh.js @@ -1,5 +1,5 @@ -modulejs.define('core/refresh', ['_', 'core/server', 'model/entry'], function (_, server, Entry) { +modulejs.define('core/refresh', ['_', 'config', 'core/server', 'model/entry', 'core/location'], function (_, config, server, Entry, location) { var parseJson = function (entry, json) { @@ -20,7 +20,7 @@ modulejs.define('core/refresh', ['_', 'core/server', 'model/entry'], function (_ refresh = function (callback) { - var entry = Entry.get(); + var entry = Entry.get(location.getAbsHref()); server.request({action: 'get', entries: true, entriesHref: entry.absHref, entriesWhat: 1}, function (json) { diff --git a/src/_h5ai/client/js/inc/core/server.js b/src/_h5ai/client/js/inc/core/server.js index 759b81bb..3ca818f6 100644 --- a/src/_h5ai/client/js/inc/core/server.js +++ b/src/_h5ai/client/js/inc/core/server.js @@ -1,5 +1,5 @@ -modulejs.define('core/server', ['$', '_', 'config'], function ($, _, config) { +modulejs.define('core/server', ['$', '_', 'config', 'core/location'], function ($, _, config, location) { var server = _.extend({}, config.server, { @@ -7,7 +7,7 @@ modulejs.define('core/server', ['$', '_', 'config'], function ($, _, config) { if (server.api) { $.ajax({ - url: '.', + url: location.getAbsHref(), data: data, type: 'POST', dataType: 'json', diff --git a/src/_h5ai/client/js/inc/ext/crumb.js b/src/_h5ai/client/js/inc/ext/crumb.js index da128980..72ebfbd3 100644 --- a/src/_h5ai/client/js/inc/ext/crumb.js +++ b/src/_h5ai/client/js/inc/ext/crumb.js @@ -1,5 +1,5 @@ -modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/event', 'core/entry'], function (_, $, allsettings, resource, event, entry) { +modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/event', 'core/location'], function (_, $, allsettings, resource, event, location) { var settings = _.extend({ enabled: false @@ -26,11 +26,11 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/ $html .addClass(entry.isFolder() ? 'folder' : 'file') + .data('item', entry) .data('status', entry.status); - $a - .attr('href', entry.absHref) - .find('span').text(entry.label).end(); + location.setLink($a, entry); + $a.find('span').text(entry.label).end(); if (entry.isDomain()) { $html.addClass('domain'); @@ -69,25 +69,49 @@ modulejs.define('ext/crumb', ['_', '$', 'core/settings', 'core/resource', 'core/ } }, - // creates the complete crumb from entry down to the root - init = function (entry) { + onLocationChanged = function (item) { + + var crumb = item.getCrumb(), + $ul = $('#navbar'), + found = false; + + $ul.find('.crumb').each(function () { + + var $html = $(this); + if ($html.data('item') === item) { + found = true; + $html.addClass('current'); + } else { + $html.removeClass('current'); + } + }); + + if (!found) { + $ul.find('.crumb').remove(); + _.each(crumb, function (e) { + + $ul.append(update(e)); + }); + } + }, + + init = function () { if (!settings.enabled) { return; } - var crumb = entry.getCrumb(), - $ul = $('#navbar'); - - _.each(crumb, function (e) { - - $ul.append(update(e)); - }); - // event.sub('entry.created', onContentChanged); // event.sub('entry.removed', onContentChanged); event.sub('entry.changed', onContentChanged); + + event.sub('location.changed', function () { + + onLocationChanged(location.getItem()); + }); + + onLocationChanged(location.getItem()); }; - init(entry); + init(); }); diff --git a/src/_h5ai/client/js/inc/ext/custom.js b/src/_h5ai/client/js/inc/ext/custom.js index 5da2495b..649fcc70 100644 --- a/src/_h5ai/client/js/inc/ext/custom.js +++ b/src/_h5ai/client/js/inc/ext/custom.js @@ -1,5 +1,5 @@ -modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server'], function (_, $, allsettings, server) { +modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/event'], function (_, $, allsettings, server, event) { var settings = _.extend({ enabled: false, @@ -7,23 +7,35 @@ modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server'], functi footer: '_h5ai.footer.html' }, allsettings.custom), + onLocationChanged = function () { + + $('#content-header, #content-footer').stop(true, true).slideUp(200); + + server.request({action: 'get', custom: true}, function (response) { + + if (response) { + if (response.custom.header) { + $('#content-header').html(response.custom.header).stop(true, true).slideDown(400); + } + if (response.custom.footer) { + $('#content-footer').html(response.custom.footer).stop(true, true).slideDown(400); + } + } + }); + }, + init = function () { if (!settings.enabled) { return; } - server.request({action: 'get', custom: true}, function (response) { + $('
').hide().prependTo('#content'); + $('