mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
fix multiple UI bugs, closes #2616
This commit is contained in:
parent
1c4358086d
commit
0ca9bff61b
@ -42,7 +42,9 @@ class EtapiToken extends AbstractEntity {
|
|||||||
/** @type {boolean} */
|
/** @type {boolean} */
|
||||||
this.isDeleted = !!row.isDeleted;
|
this.isDeleted = !!row.isDeleted;
|
||||||
|
|
||||||
this.becca.etapiTokens[this.etapiTokenId] = this;
|
if (this.etapiTokenId) {
|
||||||
|
this.becca.etapiTokens[this.etapiTokenId] = this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -48,46 +48,46 @@ const TPL = `
|
|||||||
export default class EtapiOptions {
|
export default class EtapiOptions {
|
||||||
constructor() {
|
constructor() {
|
||||||
$("#options-etapi").html(TPL);
|
$("#options-etapi").html(TPL);
|
||||||
|
|
||||||
$("#create-etapi-token").on("click", async () => {
|
$("#create-etapi-token").on("click", async () => {
|
||||||
const promptDialog = await import('../../dialogs/prompt.js');
|
const promptDialog = await import('../../dialogs/prompt.js');
|
||||||
const tokenName = await promptDialog.ask({
|
const tokenName = await promptDialog.ask({
|
||||||
title: "New ETAPI token",
|
title: "New ETAPI token",
|
||||||
message: "Please enter new token's name",
|
message: "Please enter new token's name",
|
||||||
defaultValue: "new token"
|
defaultValue: "new token"
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!tokenName.trim()) {
|
if (!tokenName.trim()) {
|
||||||
alert("Token name can't be empty");
|
alert("Token name can't be empty");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const {token} = await server.post('etapi-tokens', {tokenName});
|
|
||||||
|
|
||||||
await promptDialog.ask({
|
const {authToken} = await server.post('etapi-tokens', {tokenName});
|
||||||
|
|
||||||
|
await promptDialog.ask({
|
||||||
title: "ETAPI token created",
|
title: "ETAPI token created",
|
||||||
message: 'Copy the created token into clipboard. Trilium stores the token hashed and this is the last time you see it.',
|
message: 'Copy the created token into clipboard. Trilium stores the token hashed and this is the last time you see it.',
|
||||||
defaultValue: token
|
defaultValue: authToken
|
||||||
});
|
});
|
||||||
|
|
||||||
this.refreshTokens();
|
this.refreshTokens();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.refreshTokens();
|
this.refreshTokens();
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshTokens() {
|
async refreshTokens() {
|
||||||
const $noTokensYet = $("#no-tokens-yet");
|
const $noTokensYet = $("#no-tokens-yet");
|
||||||
const $tokensTable = $("#tokens-table");
|
const $tokensTable = $("#tokens-table");
|
||||||
|
|
||||||
const tokens = await server.get('etapi-tokens');
|
const tokens = await server.get('etapi-tokens');
|
||||||
|
|
||||||
$noTokensYet.toggle(tokens.length === 0);
|
$noTokensYet.toggle(tokens.length === 0);
|
||||||
$tokensTable.toggle(tokens.length > 0);
|
$tokensTable.toggle(tokens.length > 0);
|
||||||
|
|
||||||
const $tokensTableBody = $tokensTable.find("tbody");
|
const $tokensTableBody = $tokensTable.find("tbody");
|
||||||
$tokensTableBody.empty();
|
$tokensTableBody.empty();
|
||||||
|
|
||||||
for (const token of tokens) {
|
for (const token of tokens) {
|
||||||
$tokensTableBody.append(
|
$tokensTableBody.append(
|
||||||
$("<tr>")
|
$("<tr>")
|
||||||
@ -112,7 +112,7 @@ export default class EtapiOptions {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await server.patch(`etapi-tokens/${etapiTokenId}`, {name: tokenName});
|
await server.patch(`etapi-tokens/${etapiTokenId}`, {name: tokenName});
|
||||||
|
|
||||||
this.refreshTokens();
|
this.refreshTokens();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,9 +13,9 @@ let shownCb;
|
|||||||
|
|
||||||
export function ask({ title, message, defaultValue, shown }) {
|
export function ask({ title, message, defaultValue, shown }) {
|
||||||
shownCb = shown;
|
shownCb = shown;
|
||||||
|
|
||||||
$("#prompt-title").text(title || "Prompt");
|
$("#prompt-title").text(title || "Prompt");
|
||||||
|
|
||||||
$question = $("<label>")
|
$question = $("<label>")
|
||||||
.prop("for", "prompt-dialog-answer")
|
.prop("for", "prompt-dialog-answer")
|
||||||
.text(message);
|
.text(message);
|
||||||
@ -51,7 +51,8 @@ $dialog.on("hidden.bs.modal", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$form.on('submit', () => {
|
$form.on('submit', e => {
|
||||||
|
e.preventDefault();
|
||||||
resolve($answer.val());
|
resolve($answer.val());
|
||||||
|
|
||||||
$dialog.modal('hide');
|
$dialog.modal('hide');
|
||||||
|
@ -2,16 +2,14 @@ const etapiTokenService = require("../../services/etapi_tokens");
|
|||||||
|
|
||||||
function getTokens() {
|
function getTokens() {
|
||||||
const tokens = etapiTokenService.getTokens();
|
const tokens = etapiTokenService.getTokens();
|
||||||
|
|
||||||
tokens.sort((a, b) => a.utcDateCreated < b.utcDateCreated ? -1 : 1);
|
tokens.sort((a, b) => a.utcDateCreated < b.utcDateCreated ? -1 : 1);
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createToken(req) {
|
function createToken(req) {
|
||||||
return {
|
return etapiTokenService.createToken(req.body.tokenName);
|
||||||
authToken: etapiTokenService.createToken(req.body.tokenName)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function patchToken(req) {
|
function patchToken(req) {
|
||||||
@ -27,4 +25,4 @@ module.exports = {
|
|||||||
createToken,
|
createToken,
|
||||||
patchToken,
|
patchToken,
|
||||||
deleteToken
|
deleteToken
|
||||||
};
|
};
|
||||||
|
@ -19,7 +19,7 @@ function createToken(tokenName) {
|
|||||||
name: tokenName,
|
name: tokenName,
|
||||||
tokenHash
|
tokenHash
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
authToken: `${etapiToken.etapiTokenId}_${token}`
|
authToken: `${etapiToken.etapiTokenId}_${token}`
|
||||||
};
|
};
|
||||||
@ -29,14 +29,14 @@ function parseAuthToken(auth) {
|
|||||||
if (!auth) {
|
if (!auth) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunks = auth.split("_");
|
const chunks = auth.split("_");
|
||||||
|
|
||||||
if (chunks.length === 1) {
|
if (chunks.length === 1) {
|
||||||
return { token: auth }; // legacy format without etapiTokenId
|
return { token: auth }; // legacy format without etapiTokenId
|
||||||
}
|
}
|
||||||
else if (chunks.length === 2) {
|
else if (chunks.length === 2) {
|
||||||
return {
|
return {
|
||||||
etapiTokenId: chunks[0],
|
etapiTokenId: chunks[0],
|
||||||
token: chunks[1]
|
token: chunks[1]
|
||||||
}
|
}
|
||||||
@ -48,20 +48,20 @@ function parseAuthToken(auth) {
|
|||||||
|
|
||||||
function isValidAuthHeader(auth) {
|
function isValidAuthHeader(auth) {
|
||||||
const parsed = parseAuthToken(auth);
|
const parsed = parseAuthToken(auth);
|
||||||
|
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const authTokenHash = getTokenHash(parsed.token);
|
const authTokenHash = getTokenHash(parsed.token);
|
||||||
|
|
||||||
if (parsed.etapiTokenId) {
|
if (parsed.etapiTokenId) {
|
||||||
const etapiToken = becca.getEtapiToken(parsed.etapiTokenId);
|
const etapiToken = becca.getEtapiToken(parsed.etapiTokenId);
|
||||||
|
|
||||||
if (!etapiToken) {
|
if (!etapiToken) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return etapiToken.tokenHash === authTokenHash;
|
return etapiToken.tokenHash === authTokenHash;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -70,31 +70,30 @@ function isValidAuthHeader(auth) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renameToken(etapiTokenId, newName) {
|
function renameToken(etapiTokenId, newName) {
|
||||||
const etapiToken = becca.getEtapiToken(etapiTokenId);
|
const etapiToken = becca.getEtapiToken(etapiTokenId);
|
||||||
|
|
||||||
if (!etapiToken) {
|
if (!etapiToken) {
|
||||||
throw new Error(`Token ${etapiTokenId} does not exist`);
|
throw new Error(`Token ${etapiTokenId} does not exist`);
|
||||||
}
|
}
|
||||||
|
|
||||||
etapiToken.name = newName;
|
etapiToken.name = newName;
|
||||||
etapiToken.save();
|
etapiToken.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteToken(etapiTokenId) {
|
function deleteToken(etapiTokenId) {
|
||||||
const etapiToken = becca.getEtapiToken(etapiTokenId);
|
const etapiToken = becca.getEtapiToken(etapiTokenId);
|
||||||
|
|
||||||
if (!etapiToken) {
|
if (!etapiToken) {
|
||||||
return; // ok, already deleted
|
return; // ok, already deleted
|
||||||
}
|
}
|
||||||
|
|
||||||
etapiToken.isDeleted = true;
|
etapiToken.markAsDeletedSimple();
|
||||||
etapiToken.save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -104,4 +103,4 @@ module.exports = {
|
|||||||
deleteToken,
|
deleteToken,
|
||||||
parseAuthToken,
|
parseAuthToken,
|
||||||
isValidAuthHeader
|
isValidAuthHeader
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<div id="prompt-dialog" class="modal mx-auto" tabindex="-1" role="dialog">
|
<div id="prompt-dialog" class="modal mx-auto" tabindex="-1" role="dialog">
|
||||||
<div class="modal-dialog" role="document">
|
<div class="modal-dialog modal-lg" role="document">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<form id="prompt-dialog-form">
|
<form id="prompt-dialog-form">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user