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 = '
' + html.replace(/^[\s\S]*|<\/body>[\s\S]*$/g, '') + '
'; + console.log(html, $(html)); + var $table = $(html).find('#data-apache-autoindex table'); - if (!$id.length) { - $id = $html.find(id); - } - - return _.compact(_.map($id.find('table').find('td').closest('tr'), function (tr) { + return _.compact(_.map($table.find('td').closest('tr'), function (tr) { var $tds = $(tr).find('td'), $a = $tds.eq(1).find('a'); diff --git a/src/_h5ai/client/js/inc/parser/apache-autoindex.js b/src/_h5ai/client/js/inc/parser/apache-autoindex.js deleted file mode 100644 index ceef3897..00000000 --- a/src/_h5ai/client/js/inc/parser/apache-autoindex.js +++ /dev/null @@ -1,42 +0,0 @@ - -modulejs.define('parser/apache-autoindex', ['_', '$', 'core/settings', 'core/format', 'model/entry'], function (_, $, settings, format, Entry) { - - var parseTableRow = function (absHref, tr) { - - var $tds = $(tr).find('td'), - $a = $tds.eq(1).find('a'), - label = $a.text(), - time = format.parseDate($tds.eq(2).text(), ['YYYY-MM-DD HH:mm', 'DD-MMM-YYYY HH:mm']), - size = format.parseSize($tds.eq(3).text()); - - absHref = absHref + $a.attr('href'); - - return label === 'Parent Directory' ? null : Entry.get(absHref, time, size); - }, - - parseTable = function (absHref, table) { - - return _.compact(_.map($(table).find('td').closest('tr'), function (tr) { - - return parseTableRow(absHref, tr); - })); - }, - - parse = function (absHref, html) { - - var id = '#data-apache-autoindex', - $html = $(html), - $id = $html.filter(id); - - if (!$id.length) { - $id = $html.find(id); - } - - return parseTable(absHref, $id.find('table')); - }; - - return { - dataType: 'apache-autoindex', - parse: parse - }; -}); diff --git a/src/_h5ai/client/js/inc/parser/generic-json.js b/src/_h5ai/client/js/inc/parser/generic-json.js deleted file mode 100644 index 6443593a..00000000 --- a/src/_h5ai/client/js/inc/parser/generic-json.js +++ /dev/null @@ -1,25 +0,0 @@ - -modulejs.define('parser/generic-json', ['_', '$', 'model/entry'], function (_, $, Entry) { - - var parse = function (absHref, html) { - - var id = '#data-generic-json', - $html = $(html), - $id = $html.filter(id); - - if (!$id.length) { - $id = $html.find(id); - } - - var json = JSON.parse($.trim($id.text()) || '{}'); - return _.map(json.entries, function (jsonEntry) { - - return Entry.get(jsonEntry.absHref, jsonEntry.time, jsonEntry.size, jsonEntry.status, jsonEntry.content); - }); - }; - - return { - dataType: 'generic-json', - parse: parse - }; -});