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() {
if (this.hoistedNoteId === 'root'
&& 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
@ -221,7 +221,7 @@ class NoteContext extends Component {
return false;
}
if (this.note.hasLabel('readOnly')) {
if (this.note.isLabelTruthy('readOnly')) {
return true;
}
@ -236,7 +236,7 @@ class NoteContext extends Component {
: options.getInt('autoReadonlySizeCode');
return blob.contentLength > sizeLimit
&& !this.note.hasLabel('autoReadOnlyDisabled');
&& !this.note.isLabelTruthy('autoReadOnlyDisabled');
}
async entitiesReloadedEvent({loadResults}) {
@ -261,7 +261,7 @@ class NoteContext extends Component {
&& this.note.hasChildren()
&& ['book', 'text', 'code'].includes(this.note.type)
&& this.note.mime !== 'text/x-sqlite;schema=trilium'
&& !this.note.hasLabel('hideChildrenOverview');
&& !this.note.isLabelTruthy('hideChildrenOverview');
}
async getTextEditor(callback) {

View file

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

View file

@ -230,7 +230,7 @@ class NoteListRenderer {
const pageNotes = await froca.getNotes(pageNoteIds);
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);
}

View file

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

View file

@ -10,7 +10,7 @@ export default class ScriptLauncher extends AbstractLauncher {
}
async launch() {
if (this.launcherNote.hasLabel('scriptInLauncherContent')) {
if (this.launcherNote.isLabelTruthy('scriptInLauncherContent')) {
await this.launcherNote.executeScript();
} else {
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`);
}
if (!utils.isDesktop() && note.hasLabel('desktopOnly')) {
if (!utils.isDesktop() && note.isLabelTruthy('desktopOnly')) {
return false;
}

View file

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

View file

@ -44,7 +44,7 @@ export default class NoteWrapperWidget extends FlexContainer {
this.$widget.toggleClass("full-content-width",
['image', 'mermaid', 'book', 'render', 'canvas', 'webView'].includes(note.type)
|| !!note?.hasLabel('fullContentWidth')
|| !!note?.isLabelTruthy('fullContentWidth')
);
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.on('click', async () => {
if (!this.note.hasLabel('expanded')) {
if (!this.note.isLabelTruthy('expanded')) {
await attributeService.addLabel(this.noteId, 'expanded');
}

View file

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

View file

@ -661,7 +661,7 @@ function saveLinks(note, content) {
/** @param {BNote} note */
function saveRevisionIfNeeded(note) {
// 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;
}

View file

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

View file

@ -83,7 +83,7 @@ class SNote extends AbstractShacaEntity {
return this.getChildBranches()
.filter(branch => !branch.isHidden)
.map(branch => branch.getNote())
.filter(childNote => !childNote.hasLabel('shareHiddenFromTree'));
.filter(childNote => !childNote.isLabelTruthy('shareHiddenFromTree'));
}
/** @returns {boolean} */
@ -238,6 +238,20 @@ class SNote extends AbstractShacaEntity {
*/
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
* @returns {boolean} true if label exists (excluding inherited)

View file

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