feat: add option for default registries when SINGLE_REGISTRY=false

This commit is contained in:
Joxit 2021-10-31 15:09:37 +01:00
parent f4455703ca
commit 8fcae3cda4
No known key found for this signature in database
GPG key ID: F526592B8E012263
6 changed files with 42 additions and 15 deletions

View file

@ -32,7 +32,7 @@
</material-popup> </material-popup>
<script> <script>
import { import {
getRegistryServers addRegistryServers
} from '../../scripts/utils'; } from '../../scripts/utils';
import router from '../../scripts/router'; import router from '../../scripts/router';
@ -51,9 +51,7 @@
if (!input.value.startsWith('http')) { if (!input.value.startsWith('http')) {
return this.props.onNotify('The input field should start with http:// or https://.', true); return this.props.onNotify('The input field should start with http:// or https://.', true);
} }
const url = input.value.trim().replace(/\/*$/, ''); const url = addRegistryServers(input.value);
const registryServer = getRegistryServers().filter(e => e !== url);
localStorage.setItem('registryServer', JSON.stringify([url].concat(registryServer)));
router.home() router.home()
this.props.onServerChange(url); this.props.onServerChange(url);
this.props.onClose() this.props.onClose()

View file

@ -33,6 +33,7 @@
</material-popup> </material-popup>
<script> <script>
import { import {
addRegistryServers,
getRegistryServers getRegistryServers
} from '../../scripts/utils'; } from '../../scripts/utils';
import router from '../../scripts/router'; import router from '../../scripts/router';
@ -45,9 +46,7 @@
if (!select.value.startsWith('http')) { if (!select.value.startsWith('http')) {
return this.props.onNotify('The select field should start with http:// or https://.', true); return this.props.onNotify('The select field should start with http:// or https://.', true);
} }
const url = select.value.trim().replace(/\/*$/, ''); const url = addRegistryServers(select.value);
const registryServer = getRegistryServers().filter(e => e !== url);
localStorage.setItem('registryServer', JSON.stringify([url].concat(registryServer)));
router.home() router.home()
this.props.onServerChange(url); this.props.onServerChange(url);
this.props.onClose() this.props.onClose()

View file

@ -38,13 +38,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</material-popup> </material-popup>
<script> <script>
import { import {
getRegistryServers getRegistryServers,
removeRegistryServers
} from '../../scripts/utils'; } from '../../scripts/utils';
export default { export default {
remove(event) { remove(event) {
const url = event.currentTarget.attributes.url && event.currentTarget.attributes.url.value; const url = event.currentTarget.attributes.url && event.currentTarget.attributes.url.value;
const registryServer = getRegistryServers().filter(e => e !== url); removeRegistryServers(url);
localStorage.setItem('registryServer', JSON.stringify(registryServer));
setTimeout(() => this.update(), 100); setTimeout(() => this.update(), 100);
}, },
getRegistryServers getRegistryServers

View file

@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<div class="logo">Docker Registry UI</div> <div class="logo">Docker Registry UI</div>
<search-bar on-search="{ onSearch }"></search-bar> <search-bar on-search="{ onSearch }"></search-bar>
<dialogs-menu if="{props.singleRegistry !== 'true'}" on-notify="{ notifySnackbar }" <dialogs-menu if="{props.singleRegistry !== 'true'}" on-notify="{ notifySnackbar }"
on-server-change="{ onServerChange }"></dialogs-menu> on-server-change="{ onServerChange }" default-registries="{ props.defaultRegistries }"></dialogs-menu>
</material-navbar> </material-navbar>
</header> </header>
<main> <main>
@ -77,6 +77,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { import {
stripHttps, stripHttps,
getRegistryServers, getRegistryServers,
setRegistryServers,
truthy truthy
} from '../scripts/utils'; } from '../scripts/utils';
import router from '../scripts/router'; import router from '../scripts/router';
@ -96,6 +97,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
state.snackbarMessage = undefined; state.snackbarMessage = undefined;
}, },
onBeforeMount(props) { onBeforeMount(props) {
if (props.defaultRegistries && props.defaultRegistries.length > 0) {
setRegistryServers(props.defaultRegistries);
}
// props.singleRegistry === 'true' means old static version // props.singleRegistry === 'true' means old static version
const registryUrl = props.registryUrl || const registryUrl = props.registryUrl ||
(props.singleRegistry === 'true' ? undefined : (router.getUrlQueryParam() || getRegistryServers(0))) || (props.singleRegistry === 'true' ? undefined : (router.getUrlQueryParam() || getRegistryServers(0))) ||

View file

@ -37,7 +37,8 @@
<!-- build:keep production --> <!-- build:keep production -->
<docker-registry-ui registry-url="${REGISTRY_URL}" name="${REGISTRY_TITLE}" pull-url="${PULL_URL}" <docker-registry-ui registry-url="${REGISTRY_URL}" name="${REGISTRY_TITLE}" pull-url="${PULL_URL}"
show-content-digest="${SHOW_CONTENT_DIGEST}" is-image-remove-activated="${DELETE_IMAGES}" show-content-digest="${SHOW_CONTENT_DIGEST}" is-image-remove-activated="${DELETE_IMAGES}"
catalog-elements-limit="${CATALOG_ELEMENTS_LIMIT}" single-registry="${SINGLE_REGISTRY}"> catalog-elements-limit="${CATALOG_ELEMENTS_LIMIT}" single-registry="${SINGLE_REGISTRY}"
default-registries="${DEFAULT_REGISTRIES}">
</docker-registry-ui> </docker-registry-ui>
<!-- endbuild --> <!-- endbuild -->
<!-- build:keep developement --> <!-- build:keep developement -->

View file

@ -1,3 +1,5 @@
const LOCAL_STORAGE_KEY = 'registryServer';
export function bytesToSize(bytes) { export function bytesToSize(bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == undefined || isNaN(bytes)) { if (bytes == undefined || isNaN(bytes)) {
@ -152,7 +154,7 @@ export const ERROR_CAN_NOT_READ_CONTENT_DIGEST = {
export function getRegistryServers(i) { export function getRegistryServers(i) {
try { try {
const res = JSON.parse(localStorage.getItem('registryServer')); const res = JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY));
if (res instanceof Array) { if (res instanceof Array) {
return !isNaN(i) ? res[i] : res.map((url) => url.trim().replace(/\/*$/, '')); return !isNaN(i) ? res[i] : res.map((url) => url.trim().replace(/\/*$/, ''));
} }
@ -160,6 +162,28 @@ export function getRegistryServers(i) {
return !isNaN(i) ? '' : []; return !isNaN(i) ? '' : [];
} }
export function setRegistryServers(registries) {
if (typeof registries === 'string') {
registries = registries.split(',');
} else if (!Array.isArray(registries)) {
throw new Error('setRegistries must be called with string or array parameter');
}
registries = registries.map((registry) => registry.replace(/\/*$/, ''));
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(registries));
}
export function addRegistryServers(registry) {
const url = registry.trim().replace(/\/*$/, '');
const registryServer = getRegistryServers().filter((e) => e !== url);
setRegistryServers([url].concat(registryServer));
return url;
}
export function removeRegistryServers(registry) {
const registryServers = getRegistryServers().filter((e) => e !== registry);
setRegistryServers(registryServers);
}
export function encodeURI(url) { export function encodeURI(url) {
if (!url) { if (!url) {
return; return;
@ -175,5 +199,5 @@ export function decodeURI(url) {
} }
export function truthy(value) { export function truthy(value) {
return value === true || value === "true"; return value === true || value === 'true';
} }