Refactors js.

This commit is contained in:
Lars Jung 2012-10-12 01:01:24 +02:00
parent 2cabbad0d6
commit 8658bac71a
5 changed files with 45 additions and 93 deletions

View file

@ -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;

View file

@ -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 []; }
};
});

View file

@ -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 = '<div id="body-mock">' + html.replace(/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/g, '') + '</div>';
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');

View file

@ -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
};
});

View file

@ -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
};
});