From b0658eedf4bc1590cd5003d17d9c71dd47b3c70c Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 5 Jun 2014 11:22:44 -0400 Subject: [PATCH] Use AJAX to manage the SSH keys panel --- .../com/gitblit/wicket/pages/UserPage.java | 2 +- .../gitblit/wicket/panels/SshKeysPanel.java | 46 +++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index 505f55d9..00a36272 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -283,7 +283,7 @@ public class UserPage extends RootPage { private void addSshKeys(final UserModel user) { Fragment keysTab = new Fragment("sshKeysTab", "sshKeysTabFragment", this); - keysTab.add(new SshKeysPanel("sshKeysPanel", user, getClass(), getPageParameters())); + keysTab.add(new SshKeysPanel("sshKeysPanel", user)); // add the SSH keys tab add(new Fragment("sshKeysLink", "sshKeysLinkFragment", this).setRenderBodyOnly(true)); diff --git a/src/main/java/com/gitblit/wicket/panels/SshKeysPanel.java b/src/main/java/com/gitblit/wicket/panels/SshKeysPanel.java index 03cb93ca..15ebd67b 100644 --- a/src/main/java/com/gitblit/wicket/panels/SshKeysPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/SshKeysPanel.java @@ -15,16 +15,15 @@ */ package com.gitblit.wicket.panels; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import org.apache.wicket.PageParameters; import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.AjaxLink; import org.apache.wicket.ajax.markup.html.form.AjaxButton; -import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.repeater.Item; import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.markup.repeater.data.ListDataProvider; @@ -39,7 +38,7 @@ import com.gitblit.wicket.GitBlitWebSession; /** - * A panel that enumerates and manages SSH public keys. + * A panel that enumerates and manages SSH public keys using AJAX. * * @author James Moger * @@ -50,25 +49,21 @@ public class SshKeysPanel extends BasePanel { private final UserModel user; - private final Class pageClass; - - private final PageParameters params; - - public SshKeysPanel(String wicketId, UserModel user, Class pageClass, PageParameters params) { + public SshKeysPanel(String wicketId, UserModel user) { super(wicketId); this.user = user; - this.pageClass = pageClass; - this.params = params; } @Override protected void onInitialize() { super.onInitialize(); - List keys = app().keys().getKeys(user.username); + setOutputMarkupId(true); + + final List keys = new ArrayList(app().keys().getKeys(user.username)); final ListDataProvider dp = new ListDataProvider(keys); - DataView keysView = new DataView("keys", dp) { + final DataView keysView = new DataView("keys", dp) { private static final long serialVersionUID = 1L; @Override @@ -79,15 +74,19 @@ public class SshKeysPanel extends BasePanel { item.add(new Label("permission", key.getPermission().toString())); item.add(new Label("algorithm", key.getAlgorithm())); - Link delete = new Link("delete") { + AjaxLink delete = new AjaxLink("delete") { private static final long serialVersionUID = 1L; @Override - public void onClick() { + public void onClick(AjaxRequestTarget target) { if (app().keys().removeKey(user.username, key)) { - setRedirect(true); - setResponsePage(pageClass, params); + // reset the keys list + keys.clear(); + keys.addAll(app().keys().getKeys(user.username)); + + // update the panel + target.addComponent(SshKeysPanel.this); } } }; @@ -150,8 +149,17 @@ public class SshKeysPanel extends BasePanel { } if (app().keys().addKey(user.username, key)) { - setRedirect(true); - setResponsePage(pageClass, params); + // reset add key fields + keyData.setObject(""); + keyPermission.setObject(AccessPermission.PUSH); + keyComment.setObject(""); + + // reset the keys list + keys.clear(); + keys.addAll(app().keys().getKeys(user.username)); + + // update the panel + target.addComponent(SshKeysPanel.this); } } }); -- 2.39.5