-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- discussed in #51 - FEATURE: version and swarm peer info read from API - UX: hide irrelevant elements when redirect is disabled
- Loading branch information
Showing
8 changed files
with
184 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,65 @@ | ||
/* global self */ | ||
let getById = (id) => { | ||
return document.getElementById(id) | ||
} | ||
|
||
let renderGatewayAddress = (prefs) => { | ||
if (prefs.useCustomGateway) { | ||
return prefs.customGatewayHost + ':' + prefs.customGatewayPort | ||
} else { | ||
return 'OFF' | ||
} | ||
} | ||
|
||
function showIf(id, condition) { | ||
getById(id).style.display = condition ? 'block' : 'none' | ||
} | ||
|
||
// incoming | ||
self.port.on('show', function (context) { | ||
// console.log('show event received by panel.js (ipfsResource='+ipfsResource+')') | ||
let isPinnable = context['isPinnable'] | ||
let prefs = context.preferences | ||
|
||
// render diagnostic info | ||
getById('gateway-address-val').innerHTML = renderGatewayAddress(prefs) | ||
|
||
// show/hide ipfs-only items | ||
document.getElementById('ipfs-only') | ||
.style.display = isPinnable ? 'block' : 'none' | ||
// if current page is pinnable | ||
showIf('ipfs-resource-actions', context.isPinnable) | ||
|
||
// match panel size | ||
// if custom gateway is used | ||
showIf('open-webui', prefs.useCustomGateway) | ||
showIf('gateway-version', prefs.useCustomGateway) | ||
showIf('swarm-peers', prefs.useCustomGateway) | ||
showIf('pin-current-ipfs-address', prefs.useCustomGateway) | ||
|
||
// resize panel to match size of rendered items | ||
self.port.emit('resize', { 'width': document.body.scrollWidth, 'height': document.body.scrollHeight }) | ||
}) | ||
|
||
self.port.on('version', function (update) { | ||
getById('gateway-version-val').innerHTML = update | ||
? update.Version + '-' + update.Commit | ||
: 'n/a' | ||
}) | ||
|
||
self.port.on('swarm-peers', function (update) { | ||
getById('swarm-peers-val').innerHTML = (update && update.Strings) | ||
? update.Strings.length | ||
: 'n/a' | ||
}) | ||
|
||
// outgoing | ||
function forwardClickEvent (eventName) { | ||
document.getElementById(eventName) | ||
getById(eventName) | ||
.addEventListener('click', function (event) { // eslint-disable-line no-unused-vars | ||
self.port.emit(eventName) | ||
}) | ||
} | ||
|
||
// always visible | ||
forwardClickEvent('toggle-gateway-redirect') | ||
forwardClickEvent('open-webui') | ||
forwardClickEvent('open-preferences') | ||
|
||
// ipfs-only | ||
forwardClickEvent('pin-current-ipfs-address') | ||
forwardClickEvent('copy-current-ipfs-address') | ||
forwardClickEvent('copy-current-public-gw-url') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const prefs = require('sdk/simple-prefs').prefs | ||
|
||
const notifications = require('sdk/notifications') | ||
const Request = require('sdk/request').Request | ||
const URL = require('sdk/url').URL | ||
|
||
function apiUrl (path) { | ||
return URL('http://' + prefs.customGatewayHost + ':' + prefs.customApiPort + '/api/v0/' + path).toString() | ||
} | ||
|
||
function pin (address) { | ||
address = address.split('#')[0] // ignore URL hash | ||
address = address.split('?')[0] // ignore GET params | ||
|
||
new Request({ | ||
url: apiUrl('pin/add?arg=' + address), | ||
onComplete: function (response) { | ||
let pinned = response.status === 200 && response.json.Pinned ? response.json.Pinned[0] : false | ||
notifications.notify({ | ||
title: pinned ? 'Pinned' : 'Failed to pin', | ||
text: pinned || address.slice(6) | ||
}) | ||
} | ||
}).get() | ||
} | ||
|
||
function query (path, callback) { | ||
new Request({ | ||
url: apiUrl(path), | ||
onComplete: function (response) { | ||
let data = null | ||
if (response.status === 200) { | ||
data = response.json | ||
} | ||
callback(data) | ||
} | ||
}).get() | ||
} | ||
|
||
function getVersion (callback) { | ||
query('version', callback) | ||
} | ||
|
||
function getSwarmPeers (callback) { | ||
query('swarm/peers?stream-channels=true', callback) | ||
} | ||
|
||
exports.query = query | ||
exports.pin = pin | ||
exports.getVersion = getVersion | ||
exports.getSwarmPeers = getSwarmPeers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters