mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-25 12:34:47 -04:00
Refactored and lots of modification. See README.md.
This commit is contained in:
parent
859a680e19
commit
71ed41fa69
85 changed files with 3191 additions and 2969 deletions
282
src/_h5ai/js/inc/model/entry.js
Normal file
282
src/_h5ai/js/inc/model/entry.js
Normal file
|
@ -0,0 +1,282 @@
|
|||
|
||||
module.define('model/entry', [jQuery, 'core/types'], function ($, types) {
|
||||
|
||||
var domain = document.domain,
|
||||
location = document.location.pathname,
|
||||
|
||||
|
||||
// utils
|
||||
|
||||
reEndsWithSlash = /\/$/,
|
||||
|
||||
pathEndsWithSlash = function (sequence) {
|
||||
|
||||
return reEndsWithSlash.test(sequence);
|
||||
},
|
||||
|
||||
createLabel = function (sequence) {
|
||||
|
||||
if (sequence.length > 1 && reEndsWithSlash.test(sequence)) {
|
||||
sequence = sequence.slice(0, -1);
|
||||
}
|
||||
try {
|
||||
sequence = decodeURI(sequence);
|
||||
} catch (err) {}
|
||||
return sequence;
|
||||
},
|
||||
|
||||
|
||||
reSplitPath = /^\/([^\/]+\/?)$/,
|
||||
reSplitPath2 = /^(\/(?:.*\/)*?([^\/]+)\/)([^\/]+\/?)$/,
|
||||
|
||||
splitPath = function (sequence) {
|
||||
|
||||
var match;
|
||||
|
||||
sequence = sequence.replace(/\/+/g, '/');
|
||||
if (sequence === '/') {
|
||||
return {
|
||||
parent: null,
|
||||
parentname: null,
|
||||
name: '/'
|
||||
};
|
||||
}
|
||||
match = reSplitPath2.exec(sequence);
|
||||
if (match) {
|
||||
return {
|
||||
parent: match[1],
|
||||
parentname: match[2],
|
||||
name: match[3]
|
||||
};
|
||||
}
|
||||
match = reSplitPath.exec(sequence);
|
||||
if (match) {
|
||||
return {
|
||||
parent: '/',
|
||||
parentname: '/',
|
||||
name: match[1]
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
extToFileType = (function (types) {
|
||||
var map = {};
|
||||
$.each(types, function (type, exts) {
|
||||
$.each(exts, function (idx, ext) {
|
||||
map[ext] = type;
|
||||
});
|
||||
});
|
||||
return map;
|
||||
}(types)),
|
||||
|
||||
getFileType = function (sequence) {
|
||||
|
||||
if (pathEndsWithSlash(sequence)) {
|
||||
return 'folder';
|
||||
}
|
||||
|
||||
var dotidx = sequence.lastIndexOf('.'),
|
||||
ext = dotidx >= 0 ? sequence.substr(dotidx) : sequence;
|
||||
|
||||
return extToFileType[ext.toLowerCase()] || 'unknown';
|
||||
},
|
||||
|
||||
|
||||
reContentType = /^text\/html;h5ai=/,
|
||||
|
||||
ajaxRequest = function (self, parser, callback) {
|
||||
|
||||
$.ajax({
|
||||
url: self.absHref,
|
||||
type: parser ? 'GET' : 'HEAD',
|
||||
complete: function (xhr) {
|
||||
|
||||
if (xhr.status === 200 && reContentType.test(xhr.getResponseHeader('Content-Type'))) {
|
||||
self.status = 'h5ai';
|
||||
if (parser) {
|
||||
parser.parse(self.absHref, xhr.responseText);
|
||||
}
|
||||
} else {
|
||||
self.status = xhr.status;
|
||||
}
|
||||
|
||||
callback(self);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
// Entry
|
||||
|
||||
cache = {},
|
||||
|
||||
Entry = function (absHref) {
|
||||
|
||||
var split = splitPath(absHref);
|
||||
|
||||
cache[absHref] = this;
|
||||
|
||||
this.absHref = absHref;
|
||||
this.type = getFileType(absHref);
|
||||
this.label = createLabel(absHref === '/' ? domain : split.name);
|
||||
this.time = null;
|
||||
this.size = null;
|
||||
this.parent = null;
|
||||
this.status = null;
|
||||
this.content = {};
|
||||
|
||||
if (split.parent) {
|
||||
this.parent = cache[split.parent] || new Entry(split.parent);
|
||||
this.parent.content[this.absHref] = this;
|
||||
}
|
||||
},
|
||||
|
||||
get = function (absHref, time, size, status) {
|
||||
|
||||
absHref = absHref || location;
|
||||
|
||||
var self = cache[absHref] || new Entry(absHref);
|
||||
|
||||
if (_.isNumber(time)) {
|
||||
self.time = time;
|
||||
}
|
||||
if (_.isNumber(size)) {
|
||||
self.size = size;
|
||||
}
|
||||
if (status) {
|
||||
self.status = status;
|
||||
}
|
||||
|
||||
return self;
|
||||
},
|
||||
|
||||
folderstatus = module.isDefined('ext/folderstatus') ? module.require('ext/folderstatus') : {},
|
||||
|
||||
fetchStatus = function (absHref, callback) {
|
||||
|
||||
var self = cache[absHref] || new Entry(absHref);
|
||||
|
||||
if (self.status || !self.isFolder()) {
|
||||
callback(self);
|
||||
} else if (folderstatus[absHref]) {
|
||||
self.status = folderstatus[absHref];
|
||||
callback(self);
|
||||
} else {
|
||||
ajaxRequest(self, null, callback);
|
||||
}
|
||||
},
|
||||
|
||||
fetchContent = function (absHref, parser, callback) {
|
||||
|
||||
var self = cache[absHref] || new Entry(absHref);
|
||||
|
||||
if (self.isContentFetched || _.keys(self.content).length > 1) {
|
||||
self.isContentFetched = true;
|
||||
callback(self);
|
||||
} else {
|
||||
fetchStatus(absHref, function (self) {
|
||||
|
||||
self.isContentFetched = true;
|
||||
if (self.status === 'h5ai') {
|
||||
ajaxRequest(self, parser, callback);
|
||||
} else {
|
||||
callback(self);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
_.extend(Entry.prototype, {
|
||||
|
||||
isFolder: function () {
|
||||
|
||||
return pathEndsWithSlash(this.absHref);
|
||||
},
|
||||
|
||||
isCurrentFolder: function () {
|
||||
|
||||
return this.absHref === location;
|
||||
},
|
||||
|
||||
isDomain: function () {
|
||||
|
||||
return this.absHref === '/';
|
||||
},
|
||||
|
||||
isEmpty: function () {
|
||||
|
||||
return _.keys(this.content).length === 0;
|
||||
},
|
||||
|
||||
fetchStatus: function (callback) {
|
||||
|
||||
return fetchStatus(this.absHref, callback);
|
||||
},
|
||||
|
||||
fetchContent: function (parser, callback) {
|
||||
|
||||
return fetchContent(this.absHref, parser, callback);
|
||||
},
|
||||
|
||||
getCrumb: function () {
|
||||
|
||||
var entry = this,
|
||||
crumb = [entry];
|
||||
|
||||
while (entry.parent) {
|
||||
entry = entry.parent;
|
||||
crumb.unshift(entry);
|
||||
}
|
||||
|
||||
return crumb;
|
||||
},
|
||||
|
||||
getSubfolders: function () {
|
||||
|
||||
return _.sortBy(_.filter(this.content, function (entry) {
|
||||
|
||||
return entry.isFolder();
|
||||
}), function (entry) {
|
||||
|
||||
return entry.absHref;
|
||||
});
|
||||
},
|
||||
|
||||
getStats: function () {
|
||||
|
||||
var folders = 0,
|
||||
files = 0;
|
||||
|
||||
_.each(this.content, function (entry) {
|
||||
|
||||
if (entry.isFolder()) {
|
||||
folders += 1;
|
||||
} else {
|
||||
files += 1;
|
||||
}
|
||||
});
|
||||
|
||||
var depth = 0,
|
||||
entry = this;
|
||||
|
||||
while (entry.parent) {
|
||||
depth += 1;
|
||||
entry = entry.parent;
|
||||
}
|
||||
|
||||
return {
|
||||
folders: folders,
|
||||
files: files,
|
||||
depth: depth
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
get: get
|
||||
};
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue