Clean code.

This commit is contained in:
Lars Jung
2016-06-20 01:37:18 +02:00
parent 3246525d78
commit f6150c58f4
3 changed files with 39 additions and 37 deletions

View File

@@ -1,10 +1,5 @@
const {request} = require('./server'); const {request} = require('./server');
const config = module.exports = { const config = module.exports = {
_update: query => { _update: query => request(query).then(resp => Object.assign(config, resp))
return request(query).then(response => {
Object.assign(config, response);
return config;
});
}
}; };

View File

@@ -3,6 +3,11 @@ const {request} = require('../server');
const allsettings = require('./settings'); const allsettings = require('./settings');
const event = require('./event'); const event = require('./event');
const each = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key));
const values = obj => Object.keys(obj).map(key => obj[key]);
const isFn = x => typeof x === 'function';
const difference = (...args) => lo.difference(...args);
const notification = require('../view/notification'); const notification = require('../view/notification');
const doc = win.document; const doc = win.document;
@@ -38,15 +43,15 @@ const reForceEncoding = [
let absHref = null; let absHref = null;
function forceEncoding(href) { const forceEncoding = href => {
return reForceEncoding.reduce((nuHref, data) => { return reForceEncoding.reduce((nuHref, data) => {
return nuHref.replace(data[0], data[1]); return nuHref.replace(data[0], data[1]);
}, href); }, href);
} };
function uriToPathname(uri) { const uriToPathname = uri => {
return uri.replace(reUriToPathname, ''); return uri.replace(reUriToPathname, '');
} };
const hrefsAreDecoded = (() => { const hrefsAreDecoded = (() => {
const testpathname = '/a b'; const testpathname = '/a b';
@@ -56,7 +61,7 @@ const hrefsAreDecoded = (() => {
return uriToPathname(a.href) === testpathname; return uriToPathname(a.href) === testpathname;
})(); })();
function encodedHref(href) { const encodedHref = href => {
const a = doc.createElement('a'); const a = doc.createElement('a');
let location; let location;
@@ -68,22 +73,22 @@ function encodedHref(href) {
} }
return forceEncoding(location); return forceEncoding(location);
} };
function getDomain() { const getDomain = () => {
return doc.domain; return doc.domain;
} };
function getAbsHref() { const getAbsHref = () => {
return absHref; return absHref;
} };
function getItem() { const getItem = () => {
const Item = require('../model/item'); const Item = require('../model/item');
return Item.get(absHref); return Item.get(absHref);
} };
function load(callback) { const load = callback => {
request({action: 'get', items: {href: absHref, what: 1}}).then(json => { request({action: 'get', items: {href: absHref, what: 1}}).then(json => {
const Item = require('../model/item'); const Item = require('../model/item');
const item = Item.get(absHref); const item = Item.get(absHref);
@@ -91,39 +96,39 @@ function load(callback) {
if (json) { if (json) {
const found = {}; const found = {};
lo.each(json.items, jsonItem => { each(json.items, jsonItem => {
const e = Item.get(jsonItem); const e = Item.get(jsonItem);
found[e.absHref] = true; found[e.absHref] = true;
}); });
lo.each(item.content, e => { each(item.content, e => {
if (!found[e.absHref]) { if (!found[e.absHref]) {
Item.remove(e.absHref); Item.remove(e.absHref);
} }
}); });
} }
if (lo.isFunction(callback)) { if (isFn(callback)) {
callback(item); callback(item);
} }
}); });
} };
function refresh() { const refresh = () => {
const item = getItem(); const item = getItem();
const oldItems = lo.values(item.content); const oldItems = values(item.content);
event.pub('location.beforeRefresh'); event.pub('location.beforeRefresh');
load(() => { load(() => {
const newItems = lo.values(item.content); const newItems = values(item.content);
const added = lo.difference(newItems, oldItems); const added = difference(newItems, oldItems);
const removed = lo.difference(oldItems, newItems); const removed = difference(oldItems, newItems);
event.pub('location.refreshed', item, added, removed); event.pub('location.refreshed', item, added, removed);
}); });
} };
function setLocation(newAbsHref, keepBrowserUrl) { const setLocation = (newAbsHref, keepBrowserUrl) => {
event.pub('location.beforeChange'); event.pub('location.beforeChange');
newAbsHref = encodedHref(newAbsHref); newAbsHref = encodedHref(newAbsHref);
@@ -152,9 +157,9 @@ function setLocation(newAbsHref, keepBrowserUrl) {
event.pub('location.changed', item); event.pub('location.changed', item);
}); });
} }
} };
function setLink($el, item) { const setLink = ($el, item) => {
$el.attr('href', item.absHref); $el.attr('href', item.absHref);
if (history && item.isFolder() && item.isManaged) { if (history && item.isFolder() && item.isManaged) {
@@ -167,13 +172,13 @@ function setLink($el, item) {
if (settings.unmanagedInNewWindow && !item.isManaged) { if (settings.unmanagedInNewWindow && !item.isManaged) {
$el.attr('target', '_blank'); $el.attr('target', '_blank');
} }
} };
function onPopState(ev) { const onPopState = ev => {
if (ev.state && ev.state.absHref) { if (ev.state && ev.state.absHref) {
setLocation(ev.state.absHref, true); setLocation(ev.state.absHref, true);
} }
} };
win.onpopstate = history ? onPopState : null; win.onpopstate = history ? onPopState : null;

View File

@@ -1,4 +1,6 @@
const {jq, lo} = require('./globals'); const {jq} = require('./globals');
const each = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key));
const request = data => { const request = data => {
return new Promise(resolve => { return new Promise(resolve => {
@@ -16,7 +18,7 @@ const request = data => {
const formRequest = data => { const formRequest = data => {
const $form = jq('<form method="post" action="?" style="display:none;"/>'); const $form = jq('<form method="post" action="?" style="display:none;"/>');
lo.each(data, (val, key) => { each(data, (val, key) => {
jq('<input type="hidden"/>') jq('<input type="hidden"/>')
.attr('name', key) .attr('name', key)
.attr('value', val) .attr('value', val)