use isLabelTruthy() for most binary labels

This commit is contained in:
zadam 2023-06-29 00:14:12 +02:00
parent 430f2975f8
commit a46c5a2243
14 changed files with 34 additions and 20 deletions

View File

@ -85,7 +85,7 @@ class NoteContext extends Component {
async setHoistedNoteIfNeeded() { async setHoistedNoteIfNeeded() {
if (this.hoistedNoteId === 'root' if (this.hoistedNoteId === 'root'
&& this.notePath.startsWith("root/_hidden") && this.notePath.startsWith("root/_hidden")
&& !this.note.hasLabel("keepCurrentHoisting") && !this.note.isLabelTruthy("keepCurrentHoisting")
) { ) {
// hidden subtree displays only when hoisted, so it doesn't make sense to keep root as hoisted note // hidden subtree displays only when hoisted, so it doesn't make sense to keep root as hoisted note
@ -221,7 +221,7 @@ class NoteContext extends Component {
return false; return false;
} }
if (this.note.hasLabel('readOnly')) { if (this.note.isLabelTruthy('readOnly')) {
return true; return true;
} }
@ -236,7 +236,7 @@ class NoteContext extends Component {
: options.getInt('autoReadonlySizeCode'); : options.getInt('autoReadonlySizeCode');
return blob.contentLength > sizeLimit return blob.contentLength > sizeLimit
&& !this.note.hasLabel('autoReadOnlyDisabled'); && !this.note.isLabelTruthy('autoReadOnlyDisabled');
} }
async entitiesReloadedEvent({loadResults}) { async entitiesReloadedEvent({loadResults}) {
@ -261,7 +261,7 @@ class NoteContext extends Component {
&& this.note.hasChildren() && this.note.hasChildren()
&& ['book', 'text', 'code'].includes(this.note.type) && ['book', 'text', 'code'].includes(this.note.type)
&& this.note.mime !== 'text/x-sqlite;schema=trilium' && this.note.mime !== 'text/x-sqlite;schema=trilium'
&& !this.note.hasLabel('hideChildrenOverview'); && !this.note.isLabelTruthy('hideChildrenOverview');
} }
async getTextEditor(callback) { async getTextEditor(callback) {

View File

@ -761,7 +761,7 @@ class FNote {
} }
getPromotedDefinitionAttributes() { getPromotedDefinitionAttributes() {
if (this.hasLabel('hidePromotedAttributes')) { if (this.isLabelTruthy('hidePromotedAttributes')) {
return []; return [];
} }

View File

@ -230,7 +230,7 @@ class NoteListRenderer {
const pageNotes = await froca.getNotes(pageNoteIds); const pageNotes = await froca.getNotes(pageNoteIds);
for (const note of pageNotes) { for (const note of pageNotes) {
const $card = await this.renderNote(note, this.parentNote.hasLabel('expanded')); const $card = await this.renderNote(note, this.parentNote.isLabelTruthy('expanded'));
$container.append($card); $container.append($card);
} }

View File

@ -20,7 +20,7 @@ export default class BookmarkButtons extends FlexContainer {
for (const note of await bookmarkParentNote.getChildNotes()) { for (const note of await bookmarkParentNote.getChildNotes()) {
this.noteIds.push(note.noteId); this.noteIds.push(note.noteId);
const buttonWidget = note.hasLabel("bookmarkFolder") const buttonWidget = note.isLabelTruthy("bookmarkFolder")
? new BookmarkFolderWidget(note) ? new BookmarkFolderWidget(note)
: new OpenNoteButtonWidget(note) : new OpenNoteButtonWidget(note)
.class("launcher-button"); .class("launcher-button");

View File

@ -10,7 +10,7 @@ export default class ScriptLauncher extends AbstractLauncher {
} }
async launch() { async launch() {
if (this.launcherNote.hasLabel('scriptInLauncherContent')) { if (this.launcherNote.isLabelTruthy('scriptInLauncherContent')) {
await this.launcherNote.executeScript(); await this.launcherNote.executeScript();
} else { } else {
const script = await this.launcherNote.getRelationTarget('script'); const script = await this.launcherNote.getRelationTarget('script');

View File

@ -31,7 +31,7 @@ export default class LauncherWidget extends BasicWidget {
throw new Error(`Note '${note.noteId}' '${note.title}' is not a launcher even though it's in the launcher subtree`); throw new Error(`Note '${note.noteId}' '${note.title}' is not a launcher even though it's in the launcher subtree`);
} }
if (!utils.isDesktop() && note.hasLabel('desktopOnly')) { if (!utils.isDesktop() && note.isLabelTruthy('desktopOnly')) {
return false; return false;
} }

View File

@ -65,10 +65,10 @@ export default class EditabilitySelectWidget extends NoteContextAwareWidget {
async refreshWithNote(note) { async refreshWithNote(note) {
let editability = 'auto' let editability = 'auto'
if (this.note.hasLabel('readOnly')) { if (this.note.isLabelTruthy('readOnly')) {
editability = 'readOnly'; editability = 'readOnly';
} }
else if (this.note.hasLabel('autoReadOnlyDisabled')) { else if (this.note.isLabelTruthy('autoReadOnlyDisabled')) {
editability = 'autoReadOnlyDisabled'; editability = 'autoReadOnlyDisabled';
} }

View File

@ -44,7 +44,7 @@ export default class NoteWrapperWidget extends FlexContainer {
this.$widget.toggleClass("full-content-width", this.$widget.toggleClass("full-content-width",
['image', 'mermaid', 'book', 'render', 'canvas', 'webView'].includes(note.type) ['image', 'mermaid', 'book', 'render', 'canvas', 'webView'].includes(note.type)
|| !!note?.hasLabel('fullContentWidth') || !!note?.isLabelTruthy('fullContentWidth')
); );
this.$widget.addClass(note.getCssClass()); this.$widget.addClass(note.getCssClass());

View File

@ -72,7 +72,7 @@ export default class BookPropertiesWidget extends NoteContextAwareWidget {
this.$expandChildrenButton = this.$widget.find('.expand-children-button'); this.$expandChildrenButton = this.$widget.find('.expand-children-button');
this.$expandChildrenButton.on('click', async () => { this.$expandChildrenButton.on('click', async () => {
if (!this.note.hasLabel('expanded')) { if (!this.note.isLabelTruthy('expanded')) {
await attributeService.addLabel(this.noteId, 'expanded'); await attributeService.addLabel(this.noteId, 'expanded');
} }

View File

@ -42,7 +42,7 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
isEnabled() { isEnabled() {
return super.isEnabled() return super.isEnabled()
&& this.note.type !== 'search' && this.note.type !== 'search'
&& !this.note.hasLabel('similarNotesWidgetDisabled'); && !this.note.isLabelTruthy('similarNotesWidgetDisabled');
} }
getTitle() { getTitle() {

View File

@ -661,7 +661,7 @@ function saveLinks(note, content) {
/** @param {BNote} note */ /** @param {BNote} note */
function saveRevisionIfNeeded(note) { function saveRevisionIfNeeded(note) {
// files and images are versioned separately // files and images are versioned separately
if (note.type === 'file' || note.type === 'image' || note.hasLabel('disableVersioning')) { if (note.type === 'file' || note.type === 'image' || note.isLabelTruthy('disableVersioning')) {
return; return;
} }

View File

@ -27,7 +27,7 @@ function getSharedSubTreeRoot(note) {
} }
function addNoIndexHeader(note, res) { function addNoIndexHeader(note, res) {
if (note.hasLabel('shareDisallowRobotIndexing')) { if (note.isLabelTruthy('shareDisallowRobotIndexing')) {
res.setHeader('X-Robots-Tag', 'noindex'); res.setHeader('X-Robots-Tag', 'noindex');
} }
} }
@ -113,7 +113,7 @@ function register(router) {
addNoIndexHeader(note, res); addNoIndexHeader(note, res);
if (note.hasLabel('shareRaw')) { if (note.isLabelTruthy('shareRaw')) {
res.setHeader('Content-Type', note.mime) res.setHeader('Content-Type', note.mime)
.send(note.getContent()); .send(note.getContent());

View File

@ -83,7 +83,7 @@ class SNote extends AbstractShacaEntity {
return this.getChildBranches() return this.getChildBranches()
.filter(branch => !branch.isHidden) .filter(branch => !branch.isHidden)
.map(branch => branch.getNote()) .map(branch => branch.getNote())
.filter(childNote => !childNote.hasLabel('shareHiddenFromTree')); .filter(childNote => !childNote.isLabelTruthy('shareHiddenFromTree'));
} }
/** @returns {boolean} */ /** @returns {boolean} */
@ -238,6 +238,20 @@ class SNote extends AbstractShacaEntity {
*/ */
hasLabel(name) { return this.hasAttribute(LABEL, name); } hasLabel(name) { return this.hasAttribute(LABEL, name); }
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (including inherited) and does not have "false" value.
*/
isLabelTruthy(name) {
const label = this.getLabel(name);
if (!label) {
return false;
}
return label && label.value !== 'false';
}
/** /**
* @param {string} name - label name * @param {string} name - label name
* @returns {boolean} true if label exists (excluding inherited) * @returns {boolean} true if label exists (excluding inherited)

View File

@ -13,7 +13,7 @@
<link rel="shortcut icon" href="../favicon.ico"> <link rel="shortcut icon" href="../favicon.ico">
<% } %> <% } %>
<script src="../<%= appPath %>/share.js"></script> <script src="../<%= appPath %>/share.js"></script>
<% if (!note.hasLabel("shareOmitDefaultCss")) { %> <% if (!note.isLabelTruthy("shareOmitDefaultCss")) { %>
<link href="../<%= assetPath %>/libraries/normalize.min.css" rel="stylesheet"> <link href="../<%= assetPath %>/libraries/normalize.min.css" rel="stylesheet">
<link href="../<%= assetPath %>/stylesheets/share.css" rel="stylesheet"> <link href="../<%= assetPath %>/stylesheets/share.css" rel="stylesheet">
<% } %> <% } %>
@ -26,7 +26,7 @@
<% for (const jsRelation of note.getRelations("shareJs")) { %> <% for (const jsRelation of note.getRelations("shareJs")) { %>
<script type="module" src="api/notes/<%= jsRelation.value %>/download"></script> <script type="module" src="api/notes/<%= jsRelation.value %>/download"></script>
<% } %> <% } %>
<% if (note.hasLabel('shareDisallowRobotIndexing')) { %> <% if (note.isLabelTruthy('shareDisallowRobotIndexing')) { %>
<meta name="robots" content="noindex,follow" /> <meta name="robots" content="noindex,follow" />
<% } %> <% } %>
<%- header %> <%- header %>