diff --git a/public/javascripts/dialogs/attributes.js b/public/javascripts/dialogs/attributes.js index 65645ee88..1603799bb 100644 --- a/public/javascripts/dialogs/attributes.js +++ b/public/javascripts/dialogs/attributes.js @@ -2,19 +2,38 @@ const attributesDialog = (function() { const dialogEl = $("#attributes-dialog"); + const attributesModel = new AttributesModel(); - function AttributesModel(attributes) { - const model = this; + function AttributesModel() { + const self = this; - this.attributes = ko.observableArray(attributes); + this.attributes = ko.observableArray(); + + this.loadAttributes = async function() { + const noteId = noteEditor.getCurrentNoteId(); + + const attributes = await server.get('notes/' + noteId + '/attributes'); + + this.attributes(attributes); + }; this.addNewRow = function() { - model.attributes.push({ + self.attributes.push({ attribute_id: '', name: '', value: '' }); - } + }; + + this.save = async function() { + const noteId = noteEditor.getCurrentNoteId(); + + const attributes = await server.put('notes/' + noteId + '/attributes', this.attributes()); + + self.attributes(attributes); + + showMessage("Attributes have been saved."); + }; } async function showDialog() { @@ -26,11 +45,7 @@ const attributesDialog = (function() { height: 700 }); - const noteId = noteEditor.getCurrentNoteId(); - - const attributes = await server.get('notes/' + noteId + '/attributes'); - - ko.applyBindings(new AttributesModel(attributes)); + attributesModel.loadAttributes(); } $(document).bind('keydown', 'alt+a', e => { @@ -39,6 +54,8 @@ const attributesDialog = (function() { e.preventDefault(); }); + ko.applyBindings(attributesModel); + return { showDialog }; diff --git a/routes/api/attributes.js b/routes/api/attributes.js index 1b67fff32..ef42d662a 100644 --- a/routes/api/attributes.js +++ b/routes/api/attributes.js @@ -4,6 +4,8 @@ const express = require('express'); const router = express.Router(); const sql = require('../../services/sql'); const auth = require('../../services/auth'); +const sync_table = require('../../services/sync_table'); +const utils = require('../../services/utils'); const wrap = require('express-promise-wrap').wrap; router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { @@ -12,4 +14,35 @@ router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) res.send(await sql.getAll("SELECT * FROM attributes WHERE note_id = ? ORDER BY date_created", [noteId])); })); +router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { + const noteId = req.params.noteId; + const attributes = req.body; + const now = utils.nowDate(); + + await sql.doInTransaction(async () => { + for (const attr of attributes) { + if (attr.attribute_id) { + await sql.execute("UPDATE attributes SET name = ?, value = ?, date_modified = ? WHERE attribute_id = ?", + [attr.name, attr.value, now, attr.attribute_id]); + } + else { + attr.attribute_id = utils.newAttributeId(); + + await sql.insert("attributes", { + attribute_id: attr.attribute_id, + note_id: noteId, + name: attr.name, + value: attr.value, + date_created: now, + date_modified: now + }); + } + + await sync_table.addAttributeSync(attr.attribute_id); + } + }); + + res.send(await sql.getAll("SELECT * FROM attributes WHERE note_id = ? ORDER BY date_created", [noteId])); +})); + module.exports = router; \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index d35a16229..2f5ba0e2e 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -341,28 +341,34 @@