Refactors entry model. Hopefully not breaking anything.

This commit is contained in:
Lars Jung 2012-07-17 19:42:23 +02:00
parent a4c81135d5
commit 050d91daa8

View file

@ -3,9 +3,11 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
var doc = document, var doc = document,
domain = doc.domain, domain = doc.domain,
forceEncode = function (href) {
forceEncoding = function (href) {
return href return href
.replace(/\/+/g, '/')
.replace(/'/g, '%27') .replace(/'/g, '%27')
.replace(/\[/g, '%5B') .replace(/\[/g, '%5B')
.replace(/\]/g, '%5D') .replace(/\]/g, '%5D')
@ -14,6 +16,17 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
.replace(/\+/g, '%2B') .replace(/\+/g, '%2B')
.replace(/\=/g, '%3D'); .replace(/\=/g, '%3D');
}, },
rePrePathname = /.*:\/\/[^\/]*/,
rePostPathname = /[^\/]*$/,
uriToPathname = function (uri) {
return uri.replace(rePrePathname, '').replace(rePostPathname, '');
},
location = (function () { location = (function () {
var testpathname = '/a b', var testpathname = '/a b',
@ -21,22 +34,25 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
isDecoded, location; isDecoded, location;
a.href = testpathname; a.href = testpathname;
isDecoded = a.href.replace(/.*:\/\/[^\/]*/, '') === testpathname; isDecoded = uriToPathname(a.href) === testpathname;
a.href = '.'; a.href = doc.location.href;
location = a.href.replace(/.*:\/\/[^\/]*/, '').replace(/[^\/]*$/, ''); location = uriToPathname(a.href);
if (isDecoded) { if (isDecoded) {
location = encodeURIComponent(location).replace(/%2F/ig, '/'); location = encodeURIComponent(location).replace(/%2F/ig, '/');
} }
if (!location) { return forceEncoding(location);
location = doc.location.href.replace(/.*:\/\/[^\/]*/, '').replace(/[^\/]*$/, '');
}
return forceEncode(location);
}()), }()),
folderstatus = (function () {
try { return modulejs.require('ext/folderstatus'); } catch (e) {}
return {};
}()),
// utils // utils
@ -48,9 +64,7 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
if (sequence.length > 1 && reEndsWithSlash.test(sequence)) { if (sequence.length > 1 && reEndsWithSlash.test(sequence)) {
sequence = sequence.slice(0, -1); sequence = sequence.slice(0, -1);
} }
try { try { sequence = decodeURIComponent(sequence); } catch (e) {}
sequence = decodeURIComponent(sequence);
} catch (err) {}
return sequence; return sequence;
}, },
@ -62,7 +76,6 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
var match; var match;
sequence = sequence.replace(/\/+/g, '/');
if (sequence === '/') { if (sequence === '/') {
return { parent: null, name: '/' }; return { parent: null, name: '/' };
} }
@ -91,40 +104,13 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
// Cache
// Entry
cache = {}, cache = {},
Entry = function (absHref) { getEntry = function (absHref, time, size, status, isContentFetched) {
absHref = forceEncode(absHref); absHref = forceEncoding(absHref || location);
var split = splitPath(absHref);
cache[absHref] = this;
this.absHref = absHref;
this.type = types.getType(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;
if (_.keys(this.parent.content).length > 1) {
this.parent.isContentFetched = true;
}
}
},
get = function (absHref, time, size, status, isContentFetched) {
absHref = forceEncode(absHref || location);
var self = cache[absHref] || new Entry(absHref); var self = cache[absHref] || new Entry(absHref);
@ -144,18 +130,9 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
return self; return self;
}, },
folderstatus = (function () {
try {
return modulejs.require('ext/folderstatus');
} catch (e) {}
return {};
}()),
fetchStatus = function (absHref, callback) { fetchStatus = function (absHref, callback) {
var self = cache[absHref] || new Entry(absHref); var self = getEntry(absHref);
if (self.status || !self.isFolder()) { if (self.status || !self.isFolder()) {
callback(self); callback(self);
@ -169,7 +146,7 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
fetchContent = function (absHref, parser, callback) { fetchContent = function (absHref, parser, callback) {
var self = cache[absHref] || new Entry(absHref); var self = getEntry(absHref);
if (self.isContentFetched) { if (self.isContentFetched) {
callback(self); callback(self);
@ -187,6 +164,33 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
}; };
// Entry
var Entry = function (absHref) {
var split = splitPath(absHref);
cache[absHref] = this;
this.absHref = absHref;
this.type = types.getType(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 = getEntry(split.parent);
this.parent.content[this.absHref] = this;
if (_.keys(this.parent.content).length > 1) {
this.parent.isContentFetched = true;
}
}
};
_.extend(Entry.prototype, { _.extend(Entry.prototype, {
isFolder: function () { isFolder: function () {
@ -273,7 +277,9 @@ modulejs.define('model/entry', ['_', 'core/types', 'core/ajax'], function (_, ty
} }
}); });
return { return {
get: get get: getEntry
}; };
}); });