mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-30 15:05:15 -04:00
Clean code.
This commit is contained in:
parent
aebfdeb1cd
commit
3e14d7bed4
4 changed files with 54 additions and 48 deletions
|
@ -9,11 +9,11 @@ const defaultThemeHref = themesHref + 'default/';
|
||||||
const defaultIcons = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid', 'x'];
|
const defaultIcons = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid', 'x'];
|
||||||
|
|
||||||
|
|
||||||
function image(id) {
|
const image = id => {
|
||||||
return uiHref + id + '.svg';
|
return uiHref + id + '.svg';
|
||||||
}
|
};
|
||||||
|
|
||||||
function icon(id) {
|
const icon = id => {
|
||||||
const baseId = (id || '').split('-')[0];
|
const baseId = (id || '').split('-')[0];
|
||||||
const href = config.theme[id] || config.theme[baseId];
|
const href = config.theme[id] || config.theme[baseId];
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ function icon(id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultThemeHref + 'file.svg';
|
return defaultThemeHref + 'file.svg';
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -10,23 +10,23 @@ const reSplitPath = /^(.*\/)([^\/]+\/?)$/;
|
||||||
const cache = {};
|
const cache = {};
|
||||||
|
|
||||||
|
|
||||||
function startsWith(sequence, part) {
|
const startsWith = (sequence, part) => {
|
||||||
if (!sequence || !sequence.indexOf) {
|
if (!sequence || !sequence.indexOf) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sequence.indexOf(part) === 0;
|
return sequence.indexOf(part) === 0;
|
||||||
}
|
};
|
||||||
|
|
||||||
function createLabel(sequence) {
|
const createLabel = sequence => {
|
||||||
sequence = sequence.replace(reEndsWithSlash, '');
|
sequence = sequence.replace(reEndsWithSlash, '');
|
||||||
try {
|
try {
|
||||||
sequence = decodeURIComponent(sequence);
|
sequence = decodeURIComponent(sequence);
|
||||||
} catch (e) {/* skip */}
|
} catch (e) {/* skip */}
|
||||||
return sequence;
|
return sequence;
|
||||||
}
|
};
|
||||||
|
|
||||||
function splitPath(sequence) { // eslint-disable-line consistent-return
|
const splitPath = sequence => { // eslint-disable-line consistent-return
|
||||||
if (sequence === '/') {
|
if (sequence === '/') {
|
||||||
return {
|
return {
|
||||||
parent: null,
|
parent: null,
|
||||||
|
@ -46,9 +46,9 @@ function splitPath(sequence) { // eslint-disable-line consistent-return
|
||||||
}
|
}
|
||||||
return split;
|
return split;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function getItem(options) {
|
const getItem = options => {
|
||||||
if (isStr(options)) {
|
if (isStr(options)) {
|
||||||
options = {href: options};
|
options = {href: options};
|
||||||
} else if (!options || !isStr(options.href)) {
|
} else if (!options || !isStr(options.href)) {
|
||||||
|
@ -61,7 +61,7 @@ function getItem(options) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const item = cache[href] || new Item(href); // eslint-disable-line no-use-before-define
|
const item = cache[href] || Item(href); // eslint-disable-line no-use-before-define
|
||||||
|
|
||||||
if (isNum(options.time)) {
|
if (isNum(options.time)) {
|
||||||
item.time = options.time;
|
item.time = options.time;
|
||||||
|
@ -77,9 +77,9 @@ function getItem(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
};
|
||||||
|
|
||||||
function removeItem(absHref) {
|
const removeItem = absHref => {
|
||||||
absHref = location.forceEncoding(absHref);
|
absHref = location.forceEncoding(absHref);
|
||||||
|
|
||||||
const item = cache[absHref];
|
const item = cache[absHref];
|
||||||
|
@ -93,9 +93,9 @@ function removeItem(absHref) {
|
||||||
removeItem(child.absHref);
|
removeItem(child.absHref);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function fetchContent(absHref, callback) {
|
const fetchContent = (absHref, callback) => {
|
||||||
const item = getItem(absHref);
|
const item = getItem(absHref);
|
||||||
|
|
||||||
if (!isFn(callback)) {
|
if (!isFn(callback)) {
|
||||||
|
@ -115,33 +115,39 @@ function fetchContent(absHref, callback) {
|
||||||
callback(item);
|
callback(item);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
function Item(absHref) {
|
const Item = absHref => {
|
||||||
const split = splitPath(absHref);
|
const split = splitPath(absHref);
|
||||||
|
|
||||||
cache[absHref] = this;
|
const inst = Object.assign(Object.create(Item.prototype), {
|
||||||
|
absHref,
|
||||||
|
type: types.getType(absHref),
|
||||||
|
label: createLabel(absHref === '/' ? location.getDomain() : split.name),
|
||||||
|
time: null,
|
||||||
|
size: null,
|
||||||
|
parent: null,
|
||||||
|
isManaged: null,
|
||||||
|
content: {}
|
||||||
|
});
|
||||||
|
|
||||||
this.absHref = absHref;
|
cache[absHref] = inst;
|
||||||
this.type = types.getType(absHref);
|
|
||||||
this.label = createLabel(absHref === '/' ? location.getDomain() : split.name);
|
|
||||||
this.time = null;
|
|
||||||
this.size = null;
|
|
||||||
this.parent = null;
|
|
||||||
this.isManaged = null;
|
|
||||||
this.content = {};
|
|
||||||
|
|
||||||
if (split.parent) {
|
if (split.parent) {
|
||||||
this.parent = getItem(split.parent);
|
inst.parent = getItem(split.parent);
|
||||||
this.parent.content[this.absHref] = this;
|
inst.parent.content[inst.absHref] = inst;
|
||||||
if (keys(this.parent.content).length > 1) {
|
if (keys(inst.parent.content).length > 1) {
|
||||||
this.parent.isContentFetched = true;
|
inst.parent.isContentFetched = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(Item.prototype, {
|
return inst;
|
||||||
|
};
|
||||||
|
|
||||||
|
Item.prototype = {
|
||||||
|
constructor: Item,
|
||||||
|
|
||||||
isFolder() {
|
isFolder() {
|
||||||
return reEndsWithSlash.test(this.absHref);
|
return reEndsWithSlash.test(this.absHref);
|
||||||
},
|
},
|
||||||
|
@ -151,12 +157,12 @@ Object.assign(Item.prototype, {
|
||||||
},
|
},
|
||||||
|
|
||||||
isInCurrentFolder() {
|
isInCurrentFolder() {
|
||||||
return Boolean(this.parent) && this.parent.isCurrentFolder();
|
return !!this.parent && this.parent.isCurrentFolder();
|
||||||
},
|
},
|
||||||
|
|
||||||
isCurrentParentFolder() {
|
isCurrentParentFolder() {
|
||||||
const item = getItem(location.getAbsHref());
|
const item = getItem(location.getAbsHref());
|
||||||
return Boolean(item) && this === item.parent;
|
return !!item && this === item.parent;
|
||||||
},
|
},
|
||||||
|
|
||||||
isDomain() {
|
isDomain() {
|
||||||
|
@ -221,7 +227,7 @@ Object.assign(Item.prototype, {
|
||||||
depth
|
depth
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -3,13 +3,13 @@ const base = require('./base');
|
||||||
|
|
||||||
const $el = jq('<div id="notification"/>').hide().appendTo(base.$root);
|
const $el = jq('<div id="notification"/>').hide().appendTo(base.$root);
|
||||||
|
|
||||||
function set(content) {
|
const set = content => {
|
||||||
if (content) {
|
if (content) {
|
||||||
$el.stop(true, true).html(content).fadeIn(400);
|
$el.stop(true, true).html(content).fadeIn(400);
|
||||||
} else {
|
} else {
|
||||||
$el.stop(true, true).fadeOut(400);
|
$el.stop(true, true).fadeOut(400);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
$el,
|
$el,
|
||||||
|
|
|
@ -27,7 +27,7 @@ let modes;
|
||||||
let sizes;
|
let sizes;
|
||||||
|
|
||||||
|
|
||||||
function onChanged(mode, size) {
|
const onChanged = (mode, size) => {
|
||||||
jq('#viewmode-settings .mode').removeClass('active');
|
jq('#viewmode-settings .mode').removeClass('active');
|
||||||
jq('#viewmode-' + mode).addClass('active');
|
jq('#viewmode-' + mode).addClass('active');
|
||||||
jq('#viewmode-size').val(sizes.indexOf(size));
|
jq('#viewmode-size').val(sizes.indexOf(size));
|
||||||
|
@ -36,9 +36,9 @@ function onChanged(mode, size) {
|
||||||
mode = modes[(modes.indexOf(mode) + 1) % modes.length];
|
mode = modes[(modes.indexOf(mode) + 1) % modes.length];
|
||||||
}
|
}
|
||||||
jq('#viewmode-toggle img').attr('src', resource.image('view-' + mode));
|
jq('#viewmode-toggle img').attr('src', resource.image('view-' + mode));
|
||||||
}
|
};
|
||||||
|
|
||||||
function addSettings() {
|
const addSettings = () => {
|
||||||
if (modes.length < 2 && sizes.length < 2) {
|
if (modes.length < 2 && sizes.length < 2) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -66,25 +66,25 @@ function addSettings() {
|
||||||
}
|
}
|
||||||
|
|
||||||
$viewBlock.appendTo(sidebar.$el);
|
$viewBlock.appendTo(sidebar.$el);
|
||||||
}
|
};
|
||||||
|
|
||||||
function onToggle() {
|
const onToggle = () => {
|
||||||
const mode = view.getMode();
|
const mode = view.getMode();
|
||||||
const nextIdx = (modes.indexOf(mode) + 1) % modes.length;
|
const nextIdx = (modes.indexOf(mode) + 1) % modes.length;
|
||||||
const nextMode = modes[nextIdx];
|
const nextMode = modes[nextIdx];
|
||||||
|
|
||||||
view.setMode(nextMode);
|
view.setMode(nextMode);
|
||||||
}
|
};
|
||||||
|
|
||||||
function addToggle() {
|
const addToggle = () => {
|
||||||
if (settings.modeToggle && modes.length > 1) {
|
if (settings.modeToggle && modes.length > 1) {
|
||||||
jq(tplToggle)
|
jq(tplToggle)
|
||||||
.on('click', onToggle)
|
.on('click', onToggle)
|
||||||
.appendTo(base.$toolbar);
|
.appendTo(base.$toolbar);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function init() {
|
const init = () => {
|
||||||
modes = view.getModes();
|
modes = view.getModes();
|
||||||
sizes = view.getSizes();
|
sizes = view.getSizes();
|
||||||
|
|
||||||
|
@ -93,7 +93,7 @@ function init() {
|
||||||
onChanged(view.getMode(), view.getSize());
|
onChanged(view.getMode(), view.getSize());
|
||||||
|
|
||||||
event.sub('view.mode.changed', onChanged);
|
event.sub('view.mode.changed', onChanged);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue