diff --git a/src/_h5ai/client/js/inc/core/entry.js b/src/_h5ai/client/js/inc/core/entry.js index 128e736b..53431840 100644 --- a/src/_h5ai/client/js/inc/core/entry.js +++ b/src/_h5ai/client/js/inc/core/entry.js @@ -1,9 +1,47 @@ -modulejs.define('core/entry', ['$', 'core/parser', 'model/entry'], function ($, parser, Entry) { +modulejs.define('core/entry', ['_', '$', 'core/format', 'model/entry'], function (_, $, format, Entry) { - var entry = Entry.get(); + var parseGenericJson = function (absHref, $container) { - parser.parse(entry.absHref, $('body')); + return JSON.parse($.trim($container.text()) || '{}').entries; + }, + + parseApacheTable = function (absHref, $table) { + + return _.compact(_.map($table.find('td').closest('tr'), function (tr) { + + var $tds = $(tr).find('td'), + $a = $tds.eq(1).find('a'); + + return $a.text() === 'Parent Directory' ? null : { + absHref: absHref + $a.attr('href'), + time: format.parseDate($tds.eq(2).text(), ['YYYY-MM-DD HH:mm', 'DD-MMM-YYYY HH:mm']), + size: format.parseSize($tds.eq(3).text()) + }; + })); + }, + + parse = function (absHref, $html) { + + var $generic = $html.find('#data-generic-json'), + $apache = $html.find('#data-apache-autoindex table'), + json = []; + + if ($generic.length) { + json = parseGenericJson(absHref, $generic); + } else if ($apache.length) { + json = parseApacheTable(absHref, $apache); + } + + return _.map(json, function (entry) { + + return Entry.get(entry.absHref, entry.time, entry.size, entry.status, entry.content); + }); + }, + + entry = Entry.get(); + + parse(entry.absHref, $('body')); entry.status = '=h5ai='; return entry; diff --git a/src/_h5ai/client/js/inc/core/parser.js b/src/_h5ai/client/js/inc/core/parser.js deleted file mode 100644 index 6b06915e..00000000 --- a/src/_h5ai/client/js/inc/core/parser.js +++ /dev/null @@ -1,15 +0,0 @@ - -modulejs.define('core/parser', ['$'], function ($) { - - if ($('#data-apache-autoindex').length) { - return modulejs.require('parser/apache-autoindex'); - } - if ($('#data-generic-json').length) { - return modulejs.require('parser/generic-json'); - } - - return { - dataType: 'N/A', - parse: function () { return []; } - }; -}); diff --git a/src/_h5ai/client/js/inc/core/server.js b/src/_h5ai/client/js/inc/core/server.js index 8b49ff13..259e3dcd 100644 --- a/src/_h5ai/client/js/inc/core/server.js +++ b/src/_h5ai/client/js/inc/core/server.js @@ -63,15 +63,11 @@ modulejs.define('core/server-request-mock-aai', ['$', '_', 'core/settings', 'cor parse = function (absHref, html) { - var id = '#data-apache-autoindex', - $html = $(html), - $id = $html.filter(id); + html = '