diff --git a/CHANGELOG.md b/CHANGELOG.md index ae5c058e15f82a98df92e6822efe174fe54edece..8e0cdf996f2fb6f1757e37374ef9d22fa74643fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added (for new features, dependecies etc.) * `form_panel` module for conveniently creating a panel for web forms. +* `restore_old_version` function to base functionality (caosdb.js) +* buttons to the version history modal that allow restoring older versions ### Changed (for changes in existing functionality) diff --git a/src/core/js/caosdb.js b/src/core/js/caosdb.js index 4c74b319fe51fc3aea924885b74edd82b3c5bf03..5be29d5f50cf3d1c9a6894b6824a6170fed2a060 100644 --- a/src/core/js/caosdb.js +++ b/src/core/js/caosdb.js @@ -1178,8 +1178,6 @@ async function update(xml) { */ async function restore_old_version(versionid){ // retrieve entity - console.log("Restore ") - console.log(versionid) var ent = await transaction.retrieveEntityById(versionid); if (ent === undefined){ throw new Error(`Entity with version id ${versionid} could not be retrieved.`); @@ -1196,7 +1194,6 @@ async function restore_old_version(versionid){ if (reps.getElementsByTagName("Error").length>0) { throw new Error(`Could not restore the Entity to the version ${versionid}.`); } - } /** diff --git a/src/core/js/webcaosdb.js b/src/core/js/webcaosdb.js index a4a124f02e6965989d1e712b1c63a94222a4b4f9..d0c178d9af876e7754df264aa067b174ccc0fc5f 100644 --- a/src/core/js/webcaosdb.js +++ b/src/core/js/webcaosdb.js @@ -1082,12 +1082,13 @@ var version_history = new function () { } /** - * Initialize the export buttons of `entity`. + * Initialize the restore old version buttons of `entity`. * - * The buttons are only visible when the version history is visible and - * trigger a download of a tsv file which contains the version history. + * The buttons are only visible when the user is allowed to update the + * entity. * - * The buttons trigger the download of a tsv file with the version history. + * The causes a retrieve of the specified version of the entity and then an + * update that restores that version. * * @param {HTMLElement} [entity] - if undefined, the export buttons of all * page entities are initialized. @@ -1095,19 +1096,25 @@ var version_history = new function () { this.init_restore_version_buttons = function (entity) { entity = entity || $(".caosdb-entity-panel"); + // initialize buttons only if the user is allowed to update the entity if (hasEntityPermission(entity, "UPDATE:")){ - for (let version_info of $(entity) - .find(".caosdb-f-entity-version-info")) { - console.log(version_info) + for (let version_info of + $(entity).find(".caosdb-f-entity-version-info")) { + // find the restore button $(version_info).find(".caosdb-f-entity-version-restore-btn") + // make the button visible .toggleClass("d-none", false) .click(async (eve) => { + // the version id is stored in the restore button's + // data-version-id attribute const versionid = eve.delegateTarget.getAttribute("data-version-id") try { await restore_old_version(versionid); + // reload after sucessful update window.location.reload(); } catch (e) { console.log(e); + // print errors in an alert div $(version_info).find(".modal-body").prepend( $(`<div class="alert alert-danger alert-dismissible " role="alert"> diff --git a/test/core/js/modules/webcaosdb.js.js b/test/core/js/modules/webcaosdb.js.js index d2ef27952e41142a62eb70e144571bc9d30c52d2..15aabf3594a8fe516f07aafc69584eb2c540c0a8 100644 --- a/test/core/js/modules/webcaosdb.js.js +++ b/test/core/js/modules/webcaosdb.js.js @@ -1827,6 +1827,7 @@ QUnit.test("available", function (assert) { assert.equal(typeof version_history.init, "function"); assert.equal(typeof version_history.get_history_tsv, "function"); assert.equal(typeof version_history.init_export_history_buttons, "function"); + assert.equal(typeof version_history.init_restore_version_buttons, "function"); assert.equal(typeof version_history.init_load_history_buttons, "function"); assert.equal(typeof version_history.retrieve_history, "function"); }) @@ -1887,6 +1888,66 @@ QUnit.test("init_load_history_buttons and init_load_history_buttons", async func $(html).remove(); }); +QUnit.test("available", function (assert) { + assert.equal(typeof restore_old_version, "function"); +}) + +QUnit.test("init_restore_version_buttons", async function (assert) { + var xml_str = `<Response username="user1" realm="Realm1" srid="bc2f8f6b-71d6-49ca-890c-eebea3e38e18" timestamp="1606253365632" baseuri="https://localhost:10443" count="1"> + <UserInfo username="user1" realm="Realm1"> + <Roles> + <Role>role1</Role> + </Roles> + </UserInfo> + <Record id="8610" name="TestRecord1-6thVersion" description="This is the 6th version."> + <Permissions> + <Permission name="RETRIEVE:HISTORY" /> + <Permission name="UPDATE:*" /> + </Permissions> + <Version id="efa5ac7126c722b3f43284e150d070d6deac0ba6"> + <Predecessor id="f09114b227d88f23d4e23645ae471d688b1e82f7" /> + <Successor id="5759d2bccec3662424db5bb005acea4456a299ef" /> + </Version> + <Parent id="8609" name="TestRT" /> + </Record> +</Response> +`; + var done = assert.async(2); + var xml = str2xml(xml_str); + version_history._get = async function (entity) { + assert.equal(entity, "Entity/8610@efa5ac7126c722b3f43284e150d070d6deac0ba6?H"); + done(); + $(xml).find("Version").attr("completeHistory", "true"); + return xml; + } + var html = await transformation.transformEntities(xml); + var restore_button = $(html).find(".caosdb-f-entity-version-restore-btn"); + $("body").append(html); + + // It appears the transformation does not create sufficient html (the + // butthon is missing) + // TODO fix + console.log("restore_button"); + console.log(restore_button); + console.log(html); + + assert.ok(restore_button.hasClass("d-none"), "restore_button hidden"); + restore_button.click(); // nothing happens + + version_history.init_restore_version_buttons(); + assert.notOk(restore_button.hasClass("d-none"), "restore_button is not hidden anymore"); + + // restore_button triggers retrieval of history + restore_button.click(); + await sleep(500); + + // restore is not possible in the unit test + alertdiv = $(html).find(".alert-danger"); + assert.ok(alertdiv.has("Restore failed"), "export_button is visible"); + + $(html).remove(); +}); + /* SETUP tests for user_management */ QUnit.module("webcaosdb.js - user_management", {