diff --git a/src/core/js/caosdb.js b/src/core/js/caosdb.js index 90d6bfd23b989bbff133a63d65e1e68d99f4b3bb..c43a68de358a441c1bbfafe732bf576702e8585b 100644 --- a/src/core/js/caosdb.js +++ b/src/core/js/caosdb.js @@ -1074,13 +1074,17 @@ function createFileXML(name, id, parents, * @param {(Document|XMLDocumentFragment)} xmls The xml documents. * @return {Document} A new xml document. */ -function wrapXML(root, xmls) { +function wrapXML(root, xmls, start_with_root=undefined) { xmls = caosdb_utils.assert_array(xmls, "param `xmls`", true); caosdb_utils.assert_string(root, "param `root`"); var doc = _createDocument(root); for (var i = 0; i < xmls.length; i++) { - doc.firstElementChild.appendChild(xmls[i].firstElementChild); + if (start_with_root === undefined || start_with_root != true){ + doc.firstElementChild.appendChild(xmls[i].firstElementChild); + }else { + doc.firstElementChild.appendChild(xmls[i]); + } } return doc; @@ -1164,6 +1168,28 @@ async function update(xml) { return await transaction.updateEntitiesXml(wrapped); } + +/** + * Restore an old version of an entity using an xml representation. + * First, the old version is retrieved and the current version is set to the + * old one. + * @param versionid The version id (e.g. 123@abbabbaeff23322) of the version of + * the entity which shall be restored. + */ +async function restore_old_version(versionid){ + // retrieve entity + var ent = await transaction.retrieveEntityById(versionid); + // remove unwanted tags (Version and Permissions) + ent.getElementsByTagName("Version")[0].remove(); + var permissions = ent.getElementsByTagName("Permissions"); + for (let i = permissions.length-1; i >=0 ; i--) { + permissions[i].remove(); + } + // use XML to update entity/restore old version + reps = await transaction.updateEntitiesXml( + wrapXML("Request", [ent], start_with_root=true)) +} + /** * Insert an entity in xml representation. * diff --git a/src/core/js/webcaosdb.js b/src/core/js/webcaosdb.js index efa28c9b39921df3e02a25ec95d4601f752fa288..1e2017d19574e8d9013cff735bec4c6ec5bb79c0 100644 --- a/src/core/js/webcaosdb.js +++ b/src/core/js/webcaosdb.js @@ -1035,6 +1035,7 @@ var version_history = new function () { .retrieve_history(entity_id_version); sparse.replaceWith(history_table); version_history.init_export_history_buttons(entity); + version_history.init_restore_version_buttons(entity); }); } } @@ -1072,7 +1073,7 @@ var version_history = new function () { for (let version_info of $(entity) .find(".caosdb-f-entity-version-info")) { $(version_info).find(".caosdb-f-entity-version-export-history-btn") - .click(async () => { + .click(() => { const html_table = $(version_info).find("table")[0]; const history_tsv = this.get_history_tsv(html_table); version_history._download_tsv(history_tsv); @@ -1080,6 +1081,29 @@ var version_history = new function () { } } + /** + * Initialize the export 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 trigger the download of a tsv file with the version history. + * + * @param {HTMLElement} [entity] - if undefined, the export buttons of all + * page entities are initialized. + */ + this.init_restore_version_buttons = function (entity) { + entity = entity || $(".caosdb-entity-panel"); + for (let version_info of $(entity) + .find(".caosdb-f-entity-version-info")) { + $(version_info).find(".caosdb-f-entity-version-restore-btn") + .click(async (eve) => { + const versionid = eve.target.getAttribute("data-version-id") + restore_old_version(versionid) + }); + } + } + this._download_tsv = function (tsv_link) { window.location.href = tsv_link; } @@ -1088,6 +1112,7 @@ var version_history = new function () { this.init = function () { this.init_load_history_buttons(); this.init_export_history_buttons(); + this.init_restore_version_buttons(); } } @@ -1927,4 +1952,4 @@ class _CaosDBModules { var caosdb_modules = new _CaosDBModules() -$(document).ready(initOnDocumentReady); \ No newline at end of file +$(document).ready(initOnDocumentReady); diff --git a/src/core/xsl/entity.xsl b/src/core/xsl/entity.xsl index 92f08ea70645a8282f97f364c9fc4143f37afd6a..6344e7daee011c59fe96cb2a71707798701245a0 100644 --- a/src/core/xsl/entity.xsl +++ b/src/core/xsl/entity.xsl @@ -644,11 +644,14 @@ <div class="modal-body"> <table class="table table-hover"> <thead> - <tr><th><div class="export-data">Entity ID</div></th> + <tr> + <th></th> + <th class="invisible"><div class="export-data">Entity ID</div></th> <th class="export-data">Version ID</th> <th class="export-data">Date</th> <th class="export-data">User</th> <th class="invisible"><div class="export-data">URI</div></th> + <th></th> </tr></thead> <tbody> <xsl:apply-templates mode="entity-version-modal-successor" select="Successor"> @@ -664,6 +667,8 @@ <xsl:value-of select="@username"/>@<xsl:value-of select="@realm"/> </td> <td class="invisible"><div class="export-data"><xsl:value-of select="concat($entitypath, $entityId, '@', @id)"/></div></td> + <td> + </td> </tr> <xsl:apply-templates mode="entity-version-modal-predecessor" select="Predecessor"> <xsl:with-param name="entityId" select="$entityId"/> @@ -745,6 +750,11 @@ <xsl:value-of select="@username"/>@<xsl:value-of select="@realm"/> </td> <td class="invisible"><div class="export-data"><xsl:value-of select="concat($entitypath, $entityId, '@', @id)"/></div></td> + <td> + <button type="button" class="caosdb-f-entity-version-restore-btn btn btn-primary" > + <xsl:attribute name="data-version-id"><xsl:value-of select="$entityId"/>@<xsl:value-of select="@id"/></xsl:attribute> + <i class="bi-arrow-counterclockwise"></i></button> + </td> </tr> </xsl:template>