Skip to content

Commit 13ba814

Browse files
committed
Extracted findProfileImage from findImage in setImage
findImage used to be a private function in UI.widgets.buttons.setImage, but the logic can be reused other places, so I've tried to extract the logic so that it becomes a method exposed as UI.authn.findProfileImage. Partially fixes SolidOS/solid-panes#169
1 parent c7f3d9e commit 13ba814

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/signin.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = {
3737
filterAvailablePanes, // Async
3838
findAppInstances,
3939
findOriginOwner,
40+
findProfileImage, // Sync
4041
getUserRoles, // Async
4142
loadTypeIndexes,
4243
logIn,
@@ -1303,6 +1304,19 @@ async function getUserRoles () {
13031304
return UI.store.each(profile, ns.rdf('type'), null, preferencesFile.doc())
13041305
}
13051306

1307+
function findProfileImage (profile) {
1308+
const iconDir = UI.icons.iconBase
1309+
if (profile.sameTerm(ns.foaf('Agent')) || profile.sameTerm(ns.rdf('Resource'))) {
1310+
return iconDir + 'noun_98053.svg' // Globe
1311+
}
1312+
const image = kb.any(profile, ns.sioc('avatar')) ||
1313+
kb.any(profile, ns.foaf('img')) ||
1314+
kb.any(profile, ns.vcard('logo')) ||
1315+
kb.any(profile, ns.vcard('hasPhoto')) ||
1316+
kb.any(profile, ns.vcard('photo')) ||
1317+
kb.any(profile, ns.foaf('depiction'))
1318+
return image ? image.uri : null
1319+
}
13061320
async function filterAvailablePanes (panes) {
13071321
const userRoles = await getUserRoles()
13081322
return Object.values(panes).filter(pane => isMatchingAudience(pane, userRoles))

src/widgets/buttons.js

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
*/
33
/* global alert */
44

5+
const { findProfileImage } = require('../signin')
6+
57
module.exports = {}
68

79
var buttons = {}
@@ -221,28 +223,13 @@ buttons.findImageByClass = function findImageByClass (x) {
221223

222224
// @@ Also add icons for *properties* like home, work, email, range, domain, comment,
223225

224-
buttons.setImage = function (element, x) {
226+
buttons.setImage = function (element, profile) {
225227
const kb = UI.store
226-
const ns = UI.ns
227-
const iconDir = UI.icons.iconBase
228-
var findImage = function (x) {
229-
if (x.sameTerm(ns.foaf('Agent')) || x.sameTerm(ns.rdf('Resource'))) {
230-
return iconDir + 'noun_98053.svg' // Globe
231-
}
232-
var image = kb.any(x, ns.sioc('avatar')) ||
233-
kb.any(x, ns.foaf('img')) ||
234-
kb.any(x, ns.vcard('logo')) ||
235-
kb.any(x, ns.vcard('hasPhoto')) ||
236-
kb.any(x, ns.vcard('photo')) ||
237-
kb.any(x, ns.foaf('depiction'))
238-
return image ? image.uri : null
239-
}
240-
241-
var uri = findImage(x)
242-
element.setAttribute('src', uri || buttons.findImageByClass(x))
243-
if (!uri && x.uri) {
244-
kb.fetcher.nowOrWhenFetched(x.doc(), undefined, function (ok) {
245-
element.setAttribute('src', findImage(x) || buttons.findImageByClass(x))
228+
const uri = findProfileImage(profile)
229+
element.setAttribute('src', uri || buttons.findImageByClass(profile))
230+
if (!uri && profile.uri) {
231+
kb.fetcher.nowOrWhenFetched(profile.doc(), undefined, () => {
232+
element.setAttribute('src', findProfileImage(profile) || buttons.findImageByClass(profile))
246233
})
247234
}
248235
}

0 commit comments

Comments
 (0)