Clean code.

This commit is contained in:
Lars Jung 2016-06-24 21:26:59 +02:00
parent aebfdeb1cd
commit 3e14d7bed4
4 changed files with 54 additions and 48 deletions

View file

@ -9,11 +9,11 @@ const defaultThemeHref = themesHref + 'default/';
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';
}
};
function icon(id) {
const icon = id => {
const baseId = (id || '').split('-')[0];
const href = config.theme[id] || config.theme[baseId];
@ -30,7 +30,7 @@ function icon(id) {
}
return defaultThemeHref + 'file.svg';
}
};
module.exports = {

View file

@ -10,23 +10,23 @@ const reSplitPath = /^(.*\/)([^\/]+\/?)$/;
const cache = {};
function startsWith(sequence, part) {
const startsWith = (sequence, part) => {
if (!sequence || !sequence.indexOf) {
return false;
}
return sequence.indexOf(part) === 0;
}
};
function createLabel(sequence) {
const createLabel = sequence => {
sequence = sequence.replace(reEndsWithSlash, '');
try {
sequence = decodeURIComponent(sequence);
} catch (e) {/* skip */}
return sequence;
}
};
function splitPath(sequence) { // eslint-disable-line consistent-return
const splitPath = sequence => { // eslint-disable-line consistent-return
if (sequence === '/') {
return {
parent: null,
@ -46,9 +46,9 @@ function splitPath(sequence) { // eslint-disable-line consistent-return
}
return split;
}
}
};
function getItem(options) {
const getItem = options => {
if (isStr(options)) {
options = {href: options};
} else if (!options || !isStr(options.href)) {
@ -61,7 +61,7 @@ function getItem(options) {
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)) {
item.time = options.time;
@ -77,9 +77,9 @@ function getItem(options) {
}
return item;
}
};
function removeItem(absHref) {
const removeItem = absHref => {
absHref = location.forceEncoding(absHref);
const item = cache[absHref];
@ -93,9 +93,9 @@ function removeItem(absHref) {
removeItem(child.absHref);
});
}
}
};
function fetchContent(absHref, callback) {
const fetchContent = (absHref, callback) => {
const item = getItem(absHref);
if (!isFn(callback)) {
@ -115,33 +115,39 @@ function fetchContent(absHref, callback) {
callback(item);
});
}
}
};
function Item(absHref) {
const Item = 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;
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 = {};
cache[absHref] = inst;
if (split.parent) {
this.parent = getItem(split.parent);
this.parent.content[this.absHref] = this;
if (keys(this.parent.content).length > 1) {
this.parent.isContentFetched = true;
inst.parent = getItem(split.parent);
inst.parent.content[inst.absHref] = inst;
if (keys(inst.parent.content).length > 1) {
inst.parent.isContentFetched = true;
}
}
}
Object.assign(Item.prototype, {
return inst;
};
Item.prototype = {
constructor: Item,
isFolder() {
return reEndsWithSlash.test(this.absHref);
},
@ -151,12 +157,12 @@ Object.assign(Item.prototype, {
},
isInCurrentFolder() {
return Boolean(this.parent) && this.parent.isCurrentFolder();
return !!this.parent && this.parent.isCurrentFolder();
},
isCurrentParentFolder() {
const item = getItem(location.getAbsHref());
return Boolean(item) && this === item.parent;
return !!item && this === item.parent;
},
isDomain() {
@ -221,7 +227,7 @@ Object.assign(Item.prototype, {
depth
};
}
});
};
module.exports = {

View file

@ -3,13 +3,13 @@ const base = require('./base');
const $el = jq('<div id="notification"/>').hide().appendTo(base.$root);
function set(content) {
const set = content => {
if (content) {
$el.stop(true, true).html(content).fadeIn(400);
} else {
$el.stop(true, true).fadeOut(400);
}
}
};
module.exports = {
$el,

View file

@ -27,7 +27,7 @@ let modes;
let sizes;
function onChanged(mode, size) {
const onChanged = (mode, size) => {
jq('#viewmode-settings .mode').removeClass('active');
jq('#viewmode-' + mode).addClass('active');
jq('#viewmode-size').val(sizes.indexOf(size));
@ -36,9 +36,9 @@ function onChanged(mode, size) {
mode = modes[(modes.indexOf(mode) + 1) % modes.length];
}
jq('#viewmode-toggle img').attr('src', resource.image('view-' + mode));
}
};
function addSettings() {
const addSettings = () => {
if (modes.length < 2 && sizes.length < 2) {
return;
}
@ -66,25 +66,25 @@ function addSettings() {
}
$viewBlock.appendTo(sidebar.$el);
}
};
function onToggle() {
const onToggle = () => {
const mode = view.getMode();
const nextIdx = (modes.indexOf(mode) + 1) % modes.length;
const nextMode = modes[nextIdx];
view.setMode(nextMode);
}
};
function addToggle() {
const addToggle = () => {
if (settings.modeToggle && modes.length > 1) {
jq(tplToggle)
.on('click', onToggle)
.appendTo(base.$toolbar);
}
}
};
function init() {
const init = () => {
modes = view.getModes();
sizes = view.getSizes();
@ -93,7 +93,7 @@ function init() {
onChanged(view.getMode(), view.getSize());
event.sub('view.mode.changed', onChanged);
}
};
init();