Revision d5a0c586
| b/webodf/filelister.js | ||
|---|---|---|
| 1 |
|
|
| 1 |
/*global XMLHttpRequest*/ |
|
| 2 | 2 |
/** asynchroneous function that lists all files **/ |
| 3 | 3 |
function listFiles(startdir, filepattern, fileCallback, doneCallback) {
|
| 4 | 4 |
|
| 5 |
var todoList = []; |
|
| 6 |
|
|
| 7 |
var doneList = []; |
|
| 8 |
|
|
| 9 |
var dirpattern = /\/$/; |
|
| 5 |
var todoList = [], |
|
| 6 |
doneList = [], |
|
| 7 |
dirpattern = /\/$/; |
|
| 10 | 8 |
|
| 11 |
function getNextFileListWithWebDav() {
|
|
| 12 |
var url = todoList.shift(); |
|
| 13 |
if (!url) {
|
|
| 14 |
if (doneCallback) {
|
|
| 15 |
doneCallback(); |
|
| 16 |
} |
|
| 17 |
return; |
|
| 9 |
function processWebDavResponse(xml) {
|
|
| 10 |
if (!xml) {
|
|
| 11 |
throw new Error('No proper XML response.');
|
|
| 12 |
|
|
| 13 |
var refs = xml.getElementsByTagNameNS('DAV:', 'href'),
|
|
| 14 |
directories = [], |
|
| 15 |
files = [], |
|
| 16 |
i, d, name; |
|
| 17 |
for (i in refs) {
|
|
| 18 |
if (refs[i].firstChild) {
|
|
| 19 |
name = refs[i].firstChild.nodeValue; |
|
| 20 |
if (dirpattern.test(name)) {
|
|
| 21 |
directories.push(name); |
|
| 22 |
} else if (filepattern.test(name)) {
|
|
| 23 |
files.push(name); |
|
| 24 |
} |
|
| 25 |
} |
|
| 26 |
} |
|
| 27 |
for (d in directories) {
|
|
| 28 |
if (d) {
|
|
| 29 |
d = directories[d]; |
|
| 30 |
if (doneList.indexOf(d) === -1 && todoList.indexOf(d) === -1) {
|
|
| 31 |
todoList.push(d); |
|
| 32 |
} |
|
| 33 |
} |
|
| 34 |
} |
|
| 35 |
fileCallback(directories, files); |
|
| 18 | 36 |
} |
| 19 | 37 |
|
| 20 |
var req = new XMLHttpRequest(); |
|
| 21 |
req.open('PROPFIND', url, true);
|
|
| 22 |
req.onreadystatechange = function(evt) {
|
|
| 23 |
if (req.readyState != 4) return; |
|
| 24 |
if (req.status >= 200 && req.status < 300) {
|
|
| 25 |
processWebDavResponse(req.responseXML); |
|
| 26 |
} |
|
| 27 |
getNextFileListWithWebDav(); |
|
| 28 |
} |
|
| 29 |
req.setRequestHeader('Depth', '1');
|
|
| 30 |
req.send(null); |
|
| 38 |
function getNextFileListWithWebDav() {
|
|
| 39 |
var url = todoList.shift(), |
|
| 40 |
req; |
|
| 41 |
if (!url) {
|
|
| 42 |
if (doneCallback) {
|
|
| 43 |
doneCallback(); |
|
| 44 |
} |
|
| 45 |
return; |
|
| 46 |
} |
|
| 31 | 47 |
|
| 32 |
doneList.push(url); |
|
| 33 |
} |
|
| 48 |
req = new XMLHttpRequest(); |
|
| 49 |
req.open('PROPFIND', url, true);
|
|
| 50 |
req.onreadystatechange = function (evt) {
|
|
| 51 |
if (req.readyState !== 4) {
|
|
| 52 |
return; |
|
| 53 |
} |
|
| 54 |
if (req.status >= 200 && req.status < 300) {
|
|
| 55 |
processWebDavResponse(req.responseXML); |
|
| 56 |
} |
|
| 57 |
getNextFileListWithWebDav(); |
|
| 58 |
}; |
|
| 59 |
req.setRequestHeader('Depth', '1');
|
|
| 60 |
req.send(null); |
|
| 34 | 61 |
|
| 35 |
function getNextFileListWithIndexHtml() {
|
|
| 36 |
var url = todoList.shift(); |
|
| 37 |
while (url && typeof url != 'string') {
|
|
| 38 |
url = todoList.shift(); |
|
| 39 |
} |
|
| 40 |
if (!url) {
|
|
| 41 |
if (doneCallback) {
|
|
| 42 |
doneCallback(); |
|
| 43 |
} |
|
| 44 |
return; |
|
| 62 |
doneList.push(url); |
|
| 45 | 63 |
} |
| 46 | 64 |
|
| 47 |
var req = new XMLHttpRequest(); |
|
| 48 |
req.open('GET', url, true);
|
|
| 49 |
req.onreadystatechange = function(evt) {
|
|
| 50 |
if (req.readyState != 4) return; |
|
| 51 |
if (req.status >= 200 && req.status < 300) {
|
|
| 52 |
processIndexHtmlResponse(url, req.responseText); |
|
| 53 |
} |
|
| 54 |
getNextFileListWithIndexHtml(); |
|
| 65 |
function processIndexHtmlResponse(base, text) {
|
|
| 66 |
// use regex because index.html is usually not valid xml |
|
| 67 |
var re = /href="([^\/\?"][^"]*)"/ig, |
|
| 68 |
matches, |
|
| 69 |
files = [], |
|
| 70 |
directories = [], |
|
| 71 |
name, d; |
|
| 72 |
while ((matches = re.exec(text)) !== null) {
|
|
| 73 |
name = matches[1]; |
|
| 74 |
if (dirpattern.test(name)) {
|
|
| 75 |
directories.push(base + name); |
|
| 76 |
} else if (filepattern.test(name)) {
|
|
| 77 |
files.push(base + name); |
|
| 78 |
} |
|
| 79 |
} |
|
| 80 |
for (d in directories) {
|
|
| 81 |
if (d) {
|
|
| 82 |
d = directories[d]; |
|
| 83 |
if (doneList.indexOf(d) === -1 && todoList.indexOf(d) === -1) {
|
|
| 84 |
todoList.push(d); |
|
| 85 |
} |
|
| 86 |
} |
|
| 87 |
} |
|
| 88 |
fileCallback(directories, files); |
|
| 55 | 89 |
} |
| 56 |
req.send(null); |
|
| 57 |
|
|
| 58 |
doneList.push(url); |
|
| 59 |
} |
|
| 60 | 90 |
|
| 61 |
function processWebDavResponse(xml) {
|
|
| 62 |
if (!xml) {
|
|
| 63 |
throw new Error('No proper XML response.');
|
|
| 64 |
} |
|
| 65 |
var refs = xml.getElementsByTagNameNS('DAV:', 'href');
|
|
| 66 |
var directories = []; |
|
| 67 |
var files = []; |
|
| 68 |
for (var i in refs) {
|
|
| 69 |
if (refs[i].firstChild) {
|
|
| 70 |
var name = refs[i].firstChild.nodeValue; |
|
| 71 |
if (dirpattern.test(name)) {
|
|
| 72 |
directories.push(name); |
|
| 73 |
} else if (filepattern.test(name)) {
|
|
| 74 |
files.push(name); |
|
| 91 |
function getNextFileListWithIndexHtml() {
|
|
| 92 |
var url = todoList.shift(), |
|
| 93 |
req; |
|
| 94 |
while (url && typeof url !== 'string') {
|
|
| 95 |
url = todoList.shift(); |
|
| 96 |
} |
|
| 97 |
if (!url) {
|
|
| 98 |
if (doneCallback) {
|
|
| 99 |
doneCallback(); |
|
| 100 |
} |
|
| 101 |
return; |
|
| 75 | 102 |
} |
| 76 |
} |
|
| 77 |
} |
|
| 78 |
for (var d in directories) {
|
|
| 79 |
d = directories[d]; |
|
| 80 |
if (doneList.indexOf(d) == -1 && todoList.indexOf(d) == -1) {
|
|
| 81 |
todoList.push(d); |
|
| 82 |
} |
|
| 83 |
} |
|
| 84 |
fileCallback(directories, files); |
|
| 85 |
} |
|
| 86 | 103 |
|
| 87 |
function processIndexHtmlResponse(base, text) {
|
|
| 88 |
// use regex because index.html is usually not valid xml |
|
| 89 |
var re = /href="([^\/\?"][^"]*)"/ig; |
|
| 90 |
var matches; |
|
| 91 |
var files = []; |
|
| 92 |
var directories = []; |
|
| 93 |
while ((matches = re.exec(text)) != null) {
|
|
| 94 |
var name = matches[1]; |
|
| 95 |
if (dirpattern.test(name)) {
|
|
| 96 |
directories.push(base + name); |
|
| 97 |
} else if (filepattern.test(name)) {
|
|
| 98 |
files.push(base + name); |
|
| 99 |
} |
|
| 100 |
} |
|
| 101 |
for (var d in directories) {
|
|
| 102 |
d = directories[d]; |
|
| 103 |
if (doneList.indexOf(d) == -1 && todoList.indexOf(d) == -1) {
|
|
| 104 |
todoList.push(d); |
|
| 105 |
} |
|
| 104 |
req = new XMLHttpRequest(); |
|
| 105 |
req.open('GET', url, true);
|
|
| 106 |
req.onreadystatechange = function (evt) {
|
|
| 107 |
if (req.readyState !== 4) {
|
|
| 108 |
return; |
|
| 109 |
} |
|
| 110 |
if (req.status >= 200 && req.status < 300) {
|
|
| 111 |
processIndexHtmlResponse(url, req.responseText); |
|
| 112 |
} |
|
| 113 |
getNextFileListWithIndexHtml(); |
|
| 114 |
}; |
|
| 115 |
req.send(null); |
|
| 116 |
|
|
| 117 |
doneList.push(url); |
|
| 106 | 118 |
} |
| 107 |
fileCallback(directories, files); |
|
| 108 |
} |
|
| 109 | 119 |
|
| 110 |
todoList.push(startdir); |
|
| 111 |
// getNextFileListWithWebDav(); |
|
| 112 |
getNextFileListWithIndexHtml(); |
|
| 120 |
todoList.push(startdir);
|
|
| 121 |
// getNextFileListWithWebDav();
|
|
| 122 |
getNextFileListWithIndexHtml();
|
|
| 113 | 123 |
} |
| 114 | 124 |
|
Also available in: Unified diff