From 22b2db8daf5b3eef2831bdb67bf683a7b0e751f2 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 2 Jul 2016 00:38:52 +0200 Subject: [PATCH] Clean some code. --- src/_h5ai/public/js/lib/core/location.js | 15 ++++---- src/_h5ai/public/js/lib/ext/crumb.js | 18 +++++----- src/_h5ai/public/js/lib/ext/download.js | 2 +- src/_h5ai/public/js/lib/ext/l10n.js | 21 +++++++----- src/_h5ai/public/js/lib/ext/preview-img.js | 11 +++--- src/_h5ai/public/js/lib/ext/preview-txt.js | 4 +-- src/_h5ai/public/js/lib/ext/tree.js | 13 ++++--- src/_h5ai/public/js/lib/model/item.js | 40 ++++++++++------------ src/_h5ai/public/js/lib/server.js | 4 +-- 9 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/_h5ai/public/js/lib/core/location.js b/src/_h5ai/public/js/lib/core/location.js index 092b8792..6750f189 100644 --- a/src/_h5ai/public/js/lib/core/location.js +++ b/src/_h5ai/public/js/lib/core/location.js @@ -1,4 +1,4 @@ -const {each, values, isFn, difference} = require('../util'); +const {each, values, difference} = require('../util'); const {win} = require('../globals'); const {request} = require('../server'); const allsettings = require('./settings'); @@ -75,8 +75,8 @@ const getDomain = () => doc.domain; const getAbsHref = () => absHref; const getItem = () => require('../model/item').get(absHref); -const load = callback => { - request({action: 'get', items: {href: absHref, what: 1}}).then(json => { +const load = () => { + return request({action: 'get', items: {href: absHref, what: 1}}).then(json => { const Item = require('../model/item'); const item = Item.get(absHref); @@ -94,9 +94,8 @@ const load = callback => { } }); } - if (isFn(callback)) { - callback(item); - } + + return item; }); }; @@ -106,7 +105,7 @@ const refresh = () => { event.pub('location.beforeRefresh'); - load(() => { + load().then(() => { const newItems = values(item.content); const added = difference(newItems, oldItems); const removed = difference(oldItems, newItems); @@ -138,7 +137,7 @@ const setLocation = (newAbsHref, keepBrowserUrl) => { refresh(); } else { notification.set('loading...'); - load(() => { + load().then(() => { item.isLoaded = true; notification.set(); event.pub('location.changed', item); diff --git a/src/_h5ai/public/js/lib/ext/crumb.js b/src/_h5ai/public/js/lib/ext/crumb.js index cedb8886..17e164e9 100644 --- a/src/_h5ai/public/js/lib/ext/crumb.js +++ b/src/_h5ai/public/js/lib/ext/crumb.js @@ -9,19 +9,18 @@ const base = require('../view/base'); const settings = Object.assign({ enabled: false }, allsettings.crumb); -const crumbbarTpl = '
'; -const crumbTpl = +const tplCrumbbar = '
'; +const tplCrumb = ` > `; -const pageHintTpl = +const tplPageHint = `has index page`; -let $crumbbar; const createHtml = item => { - const $html = dom(crumbTpl); + const $html = dom(tplCrumb); location.setLink($html, item); $html.find('.label').text(item.label); @@ -31,17 +30,18 @@ const createHtml = item => { } if (!item.isManaged) { - $html.app(dom(pageHintTpl)); + $html.app(dom(tplPageHint)); } - item.$crumb = $html; + item._$crumb = $html; $html[0]._item = item; return $html; }; const onLocationChanged = item => { - const $crumb = item.$crumb; + const $crumb = item._$crumb; + const $crumbbar = dom('#crumbbar'); if ($crumb && $crumb.parent()[0] === $crumbbar[0]) { $crumbbar.children().rmCls('active'); @@ -59,7 +59,7 @@ const init = () => { return; } - $crumbbar = dom(crumbbarTpl).appTo(base.$flowbar); + dom(tplCrumbbar).appTo(base.$flowbar); event.sub('location.changed', onLocationChanged); }; diff --git a/src/_h5ai/public/js/lib/ext/download.js b/src/_h5ai/public/js/lib/ext/download.js index 331c35af..8033c22d 100644 --- a/src/_h5ai/public/js/lib/ext/download.js +++ b/src/_h5ai/public/js/lib/ext/download.js @@ -49,7 +49,7 @@ const onClick = () => { }; each(selectedItems, (item, idx) => { - query['hrefs[' + idx + ']'] = item.absHref; + query[`hrefs[${idx}]`] = item.absHref; }); server.formRequest(query); diff --git a/src/_h5ai/public/js/lib/ext/l10n.js b/src/_h5ai/public/js/lib/ext/l10n.js index 89db01ee..c8954d7f 100644 --- a/src/_h5ai/public/js/lib/ext/l10n.js +++ b/src/_h5ai/public/js/lib/ext/l10n.js @@ -70,16 +70,19 @@ const update = lang => { }); }; -const loadLanguage = (isoCode, callback) => { +const loadLanguage = isoCode => { if (loaded[isoCode]) { - callback(loaded[isoCode]); - } else { - server.request({action: 'get', l10n: [isoCode]}).then(response => { - const json = response.l10n && response.l10n[isoCode] ? response.l10n[isoCode] : {}; - loaded[isoCode] = Object.assign({}, defaultTranslations, json, {isoCode}); - callback(loaded[isoCode]); - }); + return Promise.resolve(loaded[isoCode]); } + + return server.request({action: 'get', l10n: [isoCode]}).then(response => { + loaded[isoCode] = Object.assign({}, + defaultTranslations, + response.l10n && response.l10n[isoCode], + {isoCode} + ); + return loaded[isoCode]; + }); }; const localize = (languages, isoCode, useBrowserLang) => { @@ -102,7 +105,7 @@ const localize = (languages, isoCode, useBrowserLang) => { isoCode = 'en'; } - loadLanguage(isoCode, update); + loadLanguage(isoCode).then(update); }; const initLangSelector = languages => { diff --git a/src/_h5ai/public/js/lib/ext/preview-img.js b/src/_h5ai/public/js/lib/ext/preview-img.js index d2b8f748..fd3bf561 100644 --- a/src/_h5ai/public/js/lib/ext/preview-img.js +++ b/src/_h5ai/public/js/lib/ext/preview-img.js @@ -17,13 +17,12 @@ let currentIdx; let currentItem; -const requestSample = (href, callback) => { +const requestSample = href => { if (!settings.size) { - callback(href); - return; + return Promise.resolve(href); } - server.request({ + return server.request({ action: 'get', thumbs: [{ type: 'img', @@ -32,12 +31,12 @@ const requestSample = (href, callback) => { height: 0 }] }).then(json => { - callback(json && json.thumbs && json.thumbs[0] ? json.thumbs[0] : null); + return json && json.thumbs && json.thumbs[0] ? json.thumbs[0] : null; }); }; const preloadImage = (item, callback) => { - requestSample(item.absHref, src => { + return requestSample(item.absHref).then(src => { const $img = dom('') .on('load', () => { callback(item, $img); diff --git a/src/_h5ai/public/js/lib/ext/preview-txt.js b/src/_h5ai/public/js/lib/ext/preview-txt.js index b52e70aa..377de3f7 100644 --- a/src/_h5ai/public/js/lib/ext/preview-txt.js +++ b/src/_h5ai/public/js/lib/ext/preview-txt.js @@ -20,7 +20,7 @@ let currentIdx; let currentItem; -const request = href => { +const requestTextContent = href => { return new Promise((resolve, reject) => { const xhr = new XHR(); const callback = () => { @@ -40,7 +40,7 @@ const request = href => { }; const preloadText = (item, callback) => { - request(item.absHref) + requestTextContent(item.absHref) .then(content => { callback(item, content); diff --git a/src/_h5ai/public/js/lib/ext/tree.js b/src/_h5ai/public/js/lib/ext/tree.js index a9b0fbdd..a8944777 100644 --- a/src/_h5ai/public/js/lib/ext/tree.js +++ b/src/_h5ai/public/js/lib/ext/tree.js @@ -44,7 +44,7 @@ const onIndicatorClick = ev => { const item = closestItem(ev.target); if (item._treeState === 'unknown') { - item.fetchContent(() => { + item.fetchContent().then(() => { item._treeState = 'open'; update(item); // eslint-disable-line no-use-before-define }); @@ -116,14 +116,13 @@ const update = item => { return $html; }; -const fetchTree = (item, callback) => { +const fetchTree = item => { item._treeState = 'open'; - item.fetchContent(() => { + return item.fetchContent().then(() => { if (item.parent) { - fetchTree(item.parent, callback); - } else { - callback(item); + return fetchTree(item.parent); } + return item; }); }; @@ -138,7 +137,7 @@ const updateSettings = () => { }; const onLocationChanged = item => { - fetchTree(item, root => { + fetchTree(item).then(root => { dom('#tree').clr().app(update(root)); updateSettings(); }); diff --git a/src/_h5ai/public/js/lib/model/item.js b/src/_h5ai/public/js/lib/model/item.js index d6d6f088..3d725b16 100644 --- a/src/_h5ai/public/js/lib/model/item.js +++ b/src/_h5ai/public/js/lib/model/item.js @@ -1,4 +1,4 @@ -const {keys, each, filter, sortBy, isFn, isStr, isNum} = require('../util'); +const {keys, each, filter, sortBy, isStr, isNum} = require('../util'); const server = require('../server'); const location = require('../core/location'); const settings = require('../core/settings'); @@ -90,26 +90,24 @@ const removeItem = absHref => { } }; -const fetchContent = (absHref, callback) => { - const item = getItem(absHref); +const fetchContent = absHref => { + return new Promise(resolve => { + const item = getItem(absHref); - if (!isFn(callback)) { - callback = () => undefined; - } + if (item.isContentFetched) { + resolve(item); + } else { + server.request({action: 'get', items: {href: item.absHref, what: 1}}).then(response => { + if (response.items) { + each(response.items, jsonItem => { + getItem(jsonItem); + }); + } - if (item.isContentFetched) { - callback(item); - } else { - server.request({action: 'get', items: {href: item.absHref, what: 1}}).then(response => { - if (response.items) { - each(response.items, jsonItem => { - getItem(jsonItem); - }); - } - - callback(item); - }); - } + resolve(item); + }); + } + }); }; @@ -172,8 +170,8 @@ Item.prototype = { return keys(this.content).length === 0; }, - fetchContent(callback) { - return fetchContent(this.absHref, callback); + fetchContent() { + return fetchContent(this.absHref); }, getCrumb() { diff --git a/src/_h5ai/public/js/lib/server.js b/src/_h5ai/public/js/lib/server.js index e3514fde..7071ce31 100644 --- a/src/_h5ai/public/js/lib/server.js +++ b/src/_h5ai/public/js/lib/server.js @@ -5,7 +5,7 @@ const XHR = win.XMLHttpRequest; const request = data => { return new Promise(resolve => { const xhr = new XHR(); - const callback = () => { + const onReadyStateChange = () => { if (xhr.readyState === XHR.DONE) { try { resolve(JSON.parse(xhr.responseText)); @@ -16,7 +16,7 @@ const request = data => { }; xhr.open('POST', '?', true); - xhr.onreadystatechange = callback; + xhr.onreadystatechange = onReadyStateChange; xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8'); xhr.send(JSON.stringify(data)); });