mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	displaying and saving number and boolean promoted attributes
This commit is contained in:
		@@ -13,10 +13,13 @@ class Attribute extends Entity {
 | 
				
			|||||||
    constructor(row) {
 | 
					    constructor(row) {
 | 
				
			||||||
        super(row);
 | 
					        super(row);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        if (this.isDefinition()) {
 | 
				
			||||||
            this.value = JSON.parse(this.value);
 | 
					            try {
 | 
				
			||||||
 | 
					                this.value = JSON.parse(this.value);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            catch (e) {
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch(e) {}
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async getNote() {
 | 
					    async getNote() {
 | 
				
			||||||
@@ -24,7 +27,7 @@ class Attribute extends Entity {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    isDefinition() {
 | 
					    isDefinition() {
 | 
				
			||||||
        return this.type === 'label' || this.type === 'relation';
 | 
					        return this.type === 'label-definition' || this.type === 'relation-definition';
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async beforeSaving() {
 | 
					    async beforeSaving() {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -238,44 +238,70 @@ async function loadAttributes() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (promoted.length > 0) {
 | 
					    if (promoted.length > 0) {
 | 
				
			||||||
        for (const definitionAttr of promoted) {
 | 
					        for (const definitionAttr of promoted) {
 | 
				
			||||||
            const valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === definitionAttr.type.substr(0, definitionAttr.type.length - 11));
 | 
					            const definitionType = definitionAttr.type;
 | 
				
			||||||
 | 
					            const definition = definitionAttr.value;
 | 
				
			||||||
 | 
					            const valueType = definitionType.substr(0, definitionType.length - 11);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === valueType);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (valueAttrs.length === 0) {
 | 
					            if (valueAttrs.length === 0) {
 | 
				
			||||||
                valueAttrs.push({
 | 
					                valueAttrs.push({
 | 
				
			||||||
                    attributeId: "",
 | 
					                    attributeId: "",
 | 
				
			||||||
                    type: definitionAttr.type.substr(0, definitionAttr.type.length - 11),
 | 
					                    type: valueType,
 | 
				
			||||||
                    name: definitionAttr.name,
 | 
					                    name: definitionAttr.name,
 | 
				
			||||||
                    value: ""
 | 
					                    value: ""
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for (const valueAttr of valueAttrs) {
 | 
					            for (const valueAttr of valueAttrs) {
 | 
				
			||||||
 | 
					                const inputId = "promoted-input-" + idx;
 | 
				
			||||||
 | 
					                const $tr = $("<tr>");
 | 
				
			||||||
 | 
					                const $labelCell = $("<th>").append(valueAttr.name);
 | 
				
			||||||
 | 
					                const $input = $("<input>")
 | 
				
			||||||
 | 
					                    .prop("id", inputId)
 | 
				
			||||||
 | 
					                    .prop("attribute-id", valueAttr.attributeId)
 | 
				
			||||||
 | 
					                    .prop("attribute-type", valueAttr.type)
 | 
				
			||||||
 | 
					                    .prop("attribute-name", valueAttr.name)
 | 
				
			||||||
 | 
					                    .prop("value", valueAttr.value)
 | 
				
			||||||
 | 
					                    .addClass("form-control")
 | 
				
			||||||
 | 
					                    .addClass("promoted-attribute-input");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const $inputCell = $("<td>").append($input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (valueAttr.type === 'label') {
 | 
					                if (valueAttr.type === 'label') {
 | 
				
			||||||
                    const inputId = "promoted-input-" + idx;
 | 
					                    if (definition.labelType === 'text') {
 | 
				
			||||||
                    const $tr = $("<tr>");
 | 
					                        $input.prop("type", "text");
 | 
				
			||||||
                    const $labelCell = $("<th>").append(valueAttr.name);
 | 
					                    }
 | 
				
			||||||
                    const $input = $("<input>")
 | 
					                    else if (definition.labelType === 'number') {
 | 
				
			||||||
                        .prop("id", inputId)
 | 
					                        $input.prop("type", "number");
 | 
				
			||||||
                        .prop("attribute-id", valueAttr.attributeId)
 | 
					                    }
 | 
				
			||||||
                        .prop("attribute-type", valueAttr.type)
 | 
					                    else if (definition.labelType === 'boolean') {
 | 
				
			||||||
                        .prop("attribute-name", valueAttr.name)
 | 
					                        $input.prop("type", "checkbox");
 | 
				
			||||||
                        .prop("value", valueAttr.value)
 | 
					 | 
				
			||||||
                        .addClass("form-control")
 | 
					 | 
				
			||||||
                        .addClass("promoted-attribute-input");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    const $inputCell = $("<td>").append($input);
 | 
					                        if (valueAttr.value === "true") {
 | 
				
			||||||
 | 
					                            $input.prop("checked", "checked");
 | 
				
			||||||
                    $tr.append($labelCell).append($inputCell);
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    $promotedAttributesContainer.append($tr);
 | 
					                    else if (definitionAttr.labelType === 'date') {
 | 
				
			||||||
 | 
					                        $input.prop("type", "text");
 | 
				
			||||||
 | 
					                        $input.addClass("date");
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    else if (definitionAttr.labelType === 'datetime') {
 | 
				
			||||||
 | 
					                        $input.prop("type", "text");
 | 
				
			||||||
 | 
					                        $input.addClass("datetime");
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $tr.append($labelCell).append($inputCell);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $promotedAttributesContainer.append($tr);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else {
 | 
					    else {
 | 
				
			||||||
        $attributeListInner.html('');
 | 
					        $attributeListInner.html('');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (attributes.length > 0) {console.log(attributes);
 | 
					        if (attributes.length > 0) {
 | 
				
			||||||
            for (const attribute of attributes) {
 | 
					            for (const attribute of attributes) {
 | 
				
			||||||
                if (attribute.type === 'label') {
 | 
					                if (attribute.type === 'label') {
 | 
				
			||||||
                    $attributeListInner.append(utils.formatLabel(attribute) + " ");
 | 
					                    $attributeListInner.append(utils.formatLabel(attribute) + " ");
 | 
				
			||||||
@@ -368,11 +394,20 @@ messagingService.subscribeToSyncMessages(syncData => {
 | 
				
			|||||||
$promotedAttributesContainer.on('change', '.promoted-attribute-input', async event => {
 | 
					$promotedAttributesContainer.on('change', '.promoted-attribute-input', async event => {
 | 
				
			||||||
    const $attr = $(event.target);
 | 
					    const $attr = $(event.target);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if ($attr.prop("type") === "checkbox") {
 | 
				
			||||||
 | 
					        value = $attr.is(':checked') ? "true" : "false";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					        value = $attr.val();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await server.put("notes/" + getCurrentNoteId() + "/attribute", {
 | 
					    await server.put("notes/" + getCurrentNoteId() + "/attribute", {
 | 
				
			||||||
        attributeId: $attr.prop("attribute-id"),
 | 
					        attributeId: $attr.prop("attribute-id"),
 | 
				
			||||||
        type: $attr.prop("attribute-type"),
 | 
					        type: $attr.prop("attribute-type"),
 | 
				
			||||||
        name: $attr.prop("attribute-name"),
 | 
					        name: $attr.prop("attribute-name"),
 | 
				
			||||||
        value: $attr.val()
 | 
					        value: value
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    infoService.showMessage("Attribute has been saved.");
 | 
					    infoService.showMessage("Attribute has been saved.");
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -59,7 +59,6 @@ async function updateNoteAttribute(req) {
 | 
				
			|||||||
    const body = req.body;
 | 
					    const body = req.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let attribute;
 | 
					    let attribute;
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (body.attributeId) {
 | 
					    if (body.attributeId) {
 | 
				
			||||||
        attribute = await repository.getAttribute(body.attributeId);
 | 
					        attribute = await repository.getAttribute(body.attributeId);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user