Skip to content

Commit

Permalink
last two files, except the one in #324
Browse files Browse the repository at this point in the history
Co-authored-by: Ward Cunningham <[email protected]>
  • Loading branch information
paul90 and WardCunningham committed Oct 31, 2024
1 parent 76eb521 commit 2bd464c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 66 deletions.
65 changes: 29 additions & 36 deletions lib/pageHandler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
// The pageHandler bundles fetching and storing json pages
// from origin, remote and browser local storage. It handles
// incremental updates and implicit forks when pages are edited.
Expand Down Expand Up @@ -41,13 +34,13 @@ var recursiveGet = function({pageInformation, whenGotten, whenNotGotten, localCo

const localBeforeOrigin = {
get(slug, done) {
return wiki.local.get(slug, function(err, page) {
wiki.local.get(slug, function(err, page) {
// console.log [err, page]
if (err) {
return wiki.origin.get(slug, done);
wiki.origin.get(slug, done);
} else {
site = 'local';
return done(null, page);
done(null, page);
}
});
}
Expand All @@ -62,25 +55,25 @@ var recursiveGet = function({pageInformation, whenGotten, whenNotGotten, localCo
if (site === window.location.host) { site = 'origin'; }
if (site === null) { site = 'view'; }

const adapter = (() => { switch (site) {
case 'local': return wiki.local;
case 'origin': return wiki.origin;
case 'recycler': return wiki.recycler;
case 'view': return localBeforeOrigin;
default: return wiki.site(site);
} })();
// suggest by Claude Sonnet as an alternative to using switch.
const adapter = {
'local': wiki.local,
'origin': wiki.origin,
'recycler': wiki.recycler,
'view': localBeforeOrigin
}[site] ?? wiki.site(site);

return adapter.get(`${slug}.json`, function(err, page) {
adapter.get(`${slug}.json`, function(err, page) {
if (!err) {
// console.log 'got', site, page
if (rev) { page = revision.create(rev, page); }
return whenGotten(newPage(page, site));
whenGotten(newPage(page, site));
} else {
if (([403, 404].includes(err.xhr.status) ) || (err.xhr.status === 0)) {
if (localContext.length > 0) {
return recursiveGet( {pageInformation, whenGotten, whenNotGotten, localContext} );
recursiveGet( {pageInformation, whenGotten, whenNotGotten, localContext} );
} else {
return whenNotGotten();
whenNotGotten();
}
} else {
const url = adapter.getDirectURL(pageInformation.slug);
Expand All @@ -100,7 +93,7 @@ More information might be accessible by fetching the page outside of wiki.
`;
const trouble = newPage({title: "Trouble: Can't Get Page"}, null);
trouble.addItem({type:'html', text});
return whenGotten(trouble);
whenGotten(trouble);
}
}
});
Expand All @@ -113,13 +106,13 @@ pageHandler.get = function({whenGotten,whenNotGotten,pageInformation} ) {
let localPage;
if (localPage = pageFromLocalStorage(pageInformation.slug)) {
if (pageInformation.rev) { localPage = revision.create(pageInformation.rev, localPage); }
return whenGotten(newPage( localPage, 'local' ));
whenGotten(newPage( localPage, 'local' ));
}
}

if (!pageHandler.context.length) { pageHandler.context = ['view']; }

return recursiveGet({
recursiveGet({
pageInformation,
whenGotten,
whenNotGotten,
Expand All @@ -145,9 +138,9 @@ const pushToLocal = function($page, pagePutInfo, action) {
}
}
revision.apply(page, action);
return wiki.local.put(pagePutInfo.slug, page, function() {
wiki.local.put(pagePutInfo.slug, page, function() {
addToJournal($page.find('.journal'), action);
return $page.addClass("local");
$page.addClass("local");
});
};

Expand All @@ -160,10 +153,10 @@ const pushToServer = function($page, pagePutInfo, action) {
bundle.forkPage = deepCopy(pageObject.getRawPage());
}

return wiki.origin.put(pagePutInfo.slug, bundle, function(err) {
wiki.origin.put(pagePutInfo.slug, bundle, function(err) {
if (err) {
action.error = { type: err.type, msg: err.msg, response: err.xhr.responseText};
return pushToLocal($page, pagePutInfo, action);
pushToLocal($page, pagePutInfo, action);
} else {
if (pageObject?.apply) { pageObject.apply(action); }
neighborhood.updateSitemap(pageObject);
Expand All @@ -176,7 +169,7 @@ const pushToServer = function($page, pagePutInfo, action) {
// implicit fork, probably only affects image plugin
if (action.item.type === 'image') {
const index = $page.find('.item').index($page.find('#' + action.item.id).context);
return wiki.renderFrom(index);
wiki.renderFrom(index);
}
}
}
Expand Down Expand Up @@ -248,30 +241,30 @@ pageHandler.put = function($page, action) {

// store as appropriate
if (pageHandler.useLocalStorage() || (pagePutInfo.site === 'local')) {
return pushToLocal($page, pagePutInfo, action);
pushToLocal($page, pagePutInfo, action);
} else {
return pushToServer($page, pagePutInfo, action);
pushToServer($page, pagePutInfo, action);
}
};

pageHandler.delete = function(pageObject, $page, done) {
// console.log 'delete server-side'
// console.log 'pageObject:', pageObject
if (pageObject.isRecycler()) {
return wiki.recycler.delete(`${pageObject.getSlug()}.json`, function(err) {
wiki.recycler.delete(`${pageObject.getSlug()}.json`, function(err) {
const more = () => done(err);
return setTimeout(more, 300);
setTimeout(more, 300);
});
} else {
return wiki.origin.delete(`${pageObject.getSlug()}.json`, function(err) {
wiki.origin.delete(`${pageObject.getSlug()}.json`, function(err) {
const more = function() {
if (!err) {
neighborhood.deleteFromSitemap(pageObject);
neighborhood.deleteFromIndex(pageObject);
}
return done(err);
done(err);
};
return setTimeout(more, 300);
setTimeout(more, 300);
}); // simulate server turnaround
}
};
50 changes: 20 additions & 30 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
// The plugin module manages the dynamic retrieval of plugin
// javascript including additional scripts that may be requested.

Expand All @@ -25,15 +19,14 @@ const escape = s => (''+s)

const loadScript = function(url) {
console.log("loading url:", url);
return import(url)
import(url)
};

const scripts = [];
const loadingScripts = {};
const getScript = (plugin.getScript = async function(url, callback) {
if (callback == null) { callback = function() {}; }
const getScript = (plugin.getScript = async function (url, callback = () => {}) {
if (scripts.includes(url)) {
return callback();
callback();
} else {

try {
Expand All @@ -43,13 +36,13 @@ const getScript = (plugin.getScript = async function(url, callback) {
}

scripts.push(url);
return callback();
callback();
}
});

plugin.renderFrom = async function(notifIndex) {
const $items = $(".item").slice(notifIndex);

// console.log "notifIndex", notifIndex, "about to render", $items.toArray()

for (const itemElem of $items.toArray()) {
Expand Down Expand Up @@ -112,11 +105,11 @@ const bind = function(name, pluginBind) {
console.log('warn: no items in lineup that produces', consuming);
}
// console.log("there are #{producers.length} instances of #{consuming}")
return producers.each(function(_i, el) {
producers.each(function(_i, el) {
const page_key = $(el).parents('.page').data('key');
const item_id = $(el).attr('data-id');
$item[0].consuming.push(`${page_key}/${item_id}`);
return deps.push(el.promise);
deps.push(el.promise);
});
});
await Promise.all(deps);
Expand Down Expand Up @@ -151,17 +144,16 @@ function wrap (name, p) {
return p;
};

plugin.get = (plugin.getPlugin = async function(name, callback) {
if (callback == null) { callback = function() {}; }
plugin.get = (plugin.getPlugin = async function(name, callback = () => {}) {
if (window.pluginSuccessor[name]) {
wiki.log('plugin successor', name, window.pluginSuccessor[name]);
name = window.pluginSuccessor[name];
name = window.pluginSuccessor[name];
}
if (window.plugins[name]) {
callback(window.plugins[name])
return window.plugins[name]
}
if (!loadingScripts[name]) {
if (!loadingScripts[name]) {
loadingScripts[name] = (async function() {
if (window.plugins[name]) { return window.plugins[name]; }
await getScript(`/plugins/${name}/${name}.js`)
Expand All @@ -185,15 +177,13 @@ plugin.get = (plugin.getPlugin = async function(name, callback) {
});


plugin.do = (plugin.doPlugin = async function($item, item, done) {
if (done == null) { done = function() {}; }
plugin.do = (plugin.doPlugin = async function ($item, item, done = () => {}) {
$item.data('item', item);
await plugin.renderFrom($('.item').index($item));
return done()
});

plugin.emit = async function(div, item, done) {
if (done == null) { done = function() {}; }
plugin.emit = async function(div, item, done = () => {}) {
const error = function(ex, script) {
div.append(`\
<div class="error">
Expand All @@ -202,10 +192,10 @@ plugin.emit = async function(div, item, done) {
</div>\
`
);
if (item.text != null) {
if (item.text) {
div.find('.error').on('dblclick', e => wiki.textEditor(div, item));
}
return div.find('button').on('click', function() {
div.find('button').on('click', function() {
// only append dialog if not already done.
if (!div[0].querySelector('dialog')) {
div.append(`\
Expand All @@ -229,25 +219,25 @@ plugin.emit = async function(div, item, done) {
<img src="/images/external-link-ltr-icon.png">
</a>
</p>
</dialog>\
`
);
}
const dialog = div[0].querySelector('dialog');
dialog.addEventListener('click', function(evt) {
if (evt.target === dialog) {
return dialog.close();
dialog.close();
}
});
dialog.showModal();
$('.close').on('click', () => dialog.close());
return $('.retry').on('click', function() {
$('.retry').on('click', function() {
if (script.emit.length > 2) {
return script.emit(div, item, () => done());
script.emit(div, item, () => done());
} else {
script.emit(div, item);
return done();
done();
}
});
});
Expand All @@ -272,7 +262,7 @@ plugin.emit = async function(div, item, done) {
console.log('plugin error', err);
error(err, script);
}
return done();
done();
};

plugin.registerPlugin = (pluginName, pluginFn) => window.plugins[pluginName] = pluginFn($);

0 comments on commit 2bd464c

Please sign in to comment.