Skip to content

The Unified and Autonomous Development Platform

Add utility class ParentPathAndName

robin shine8503b0b59fa4
diff --git a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
index 665c3dde7a..6af6b6b658 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/FileEdit.java
@@ -20,6 +20,7 @@ import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
+import com.google.common.base.Charsets;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
@@ -38,9 +39,9 @@ public class FileEdit implements Serializable {
private final String newPath;
- private final byte[] content;
+ private final String content;
- public FileEdit(@Nullable String oldPath, @Nullable String newPath, byte[] content) {
+ public FileEdit(@Nullable String oldPath, @Nullable String newPath, String content) {
this.oldPath = GitUtils.normalizePath(oldPath);
this.newPath= GitUtils.normalizePath(newPath);
this.content = content;
@@ -57,7 +58,7 @@ public class FileEdit implements Serializable {
return newPath;
}
- public byte[] getContent() {
+ public String getContent() {
return content;
}
@@ -76,7 +77,7 @@ public class FileEdit implements Serializable {
oldPathFound = true;
if (name.equals(currentNewPath)) {
newPathFound = true;
- ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content);
+ ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, content.getBytes(Charsets.UTF_8));
formatter.append(name, FileMode.REGULAR_FILE, blobId);
appended = true;
}
@@ -134,7 +135,7 @@ public class FileEdit implements Serializable {
for (int i=splitted.size()-1; i>=0; i--) {
if (childId == null) {
childName = splitted.get(i);
- childId = inserter.insert(Constants.OBJ_BLOB, content);
+ childId = inserter.insert(Constants.OBJ_BLOB, content.getBytes(Charsets.UTF_8));
childMode = FileMode.REGULAR_FILE;
} else {
TreeFormatter childFormatter = new TreeFormatter();
diff --git a/commons.git/src/main/java/com/pmease/commons/git/ParentPathAndName.java b/commons.git/src/main/java/com/pmease/commons/git/ParentPathAndName.java
new file mode 100644
index 0000000000..0422ea07bf
--- /dev/null
+++ b/commons.git/src/main/java/com/pmease/commons/git/ParentPathAndName.java
@@ -0,0 +1,57 @@
+package com.pmease.commons.git;
+
+import java.io.Serializable;
+
+import javax.annotation.Nullable;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class ParentPathAndName implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String parentPath;
+
+ private final String name;
+
+ public ParentPathAndName(String path) {
+ if (path.contains("/")) {
+ parentPath = StringUtils.substringBeforeLast(path, "/");
+ name = StringUtils.substringAfterLast(path, "/");
+ } else {
+ parentPath = null;
+ name = path;
+ }
+ }
+
+ public ParentPathAndName(@Nullable String parentPath, @Nullable String name) {
+ this.parentPath = parentPath;
+ this.name = name;
+ }
+
+ @Nullable
+ public String getParentPath() {
+ return parentPath;
+ }
+
+ @Nullable
+ public String getName() {
+ return name;
+ }
+
+ public String getPath() {
+ if (name == null)
+ return null;
+ else if (parentPath != null)
+ return GitUtils.normalizePath(parentPath + "/" + name);
+ else
+ return GitUtils.normalizePath(name);
+ }
+
+ public String getPath(String name) {
+ if (parentPath != null)
+ return GitUtils.normalizePath(parentPath + "/" + name);
+ else
+ return GitUtils.normalizePath(name);
+ }
+}
diff --git a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
index 7b0de6a487..fca681d543 100644
--- a/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
+++ b/commons.git/src/test/java/com/pmease/commons/git/FileEditTest.java
@@ -95,7 +95,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
- "client/c.java", "a".getBytes());
+ "client/c.java", "a");
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename");
try (RevWalk revWalk = new RevWalk(repo)) {
RevTree revTree = revWalk.parseCommit(newCommitId).getTree();
@@ -125,7 +125,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a",
- "client/c.java", "a".getBytes());
+ "client/c.java", "a");
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotFileException should be thrown", false);
@@ -151,7 +151,7 @@ public class FileEditTest extends AbstractGitTest {
PersonIdent person = new PersonIdent(repo);
FileEdit edit = new FileEdit("server/src/com/example/a/a.java",
- "client/a.java/a.java", "a".getBytes());
+ "client/a.java/a.java", "a");
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("A NotTreeException should be thrown", false);
@@ -176,7 +176,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
+ FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c");
ObjectId newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -189,7 +189,7 @@ public class FileEditTest extends AbstractGitTest {
}
oldCommitId = newCommitId;
- edit = new FileEdit(null, "/common/common.java", "common".getBytes());
+ edit = new FileEdit(null, "/common/common.java", "common");
newCommitId = edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
try (RevWalk revWalk = new RevWalk(repo)) {
@@ -220,7 +220,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId oldCommitId = repo.resolve(refName);
PersonIdent person = new PersonIdent(repo);
- FileEdit edit = new FileEdit(null, "client/a.java", "a".getBytes());
+ FileEdit edit = new FileEdit(null, "client/a.java", "a");
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test rename tree");
assertTrue("An ObjectAlreadyExistException should be thrown", false);
@@ -242,7 +242,7 @@ public class FileEditTest extends AbstractGitTest {
ObjectId newCommitId = repo.resolve(refName);
- FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c".getBytes());
+ FileEdit edit = new FileEdit(null, "/server/src/com/example/c/c.java", "c");
try {
edit.commit(repo, refName, oldCommitId, oldCommitId, person, "test add");
assertTrue("An ObsoleteCommitException should be thrown", false);
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/codemirror.css b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/codemirror.css
index d53102fed8..6482606cd4 100644
--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/codemirror.css
+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/codemirror.css
@@ -1,6 +1,12 @@
.cm-identifierhighlight {
background: #ACF0A6;
}
+.CodeMirror {
+ font-size: 13px;
+}
+.CodeMirror-gutters {
+ background: white;
+}
.CodeMirror-selected {
background-color: #3399FF !important;
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
index 87c131c71b..b71578703b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/BlobViewPanel.java
@@ -30,10 +30,12 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.BlobIdent;
import com.pmease.commons.git.Git;
+import com.pmease.commons.git.ParentPathAndName;
import com.pmease.commons.wicket.assets.closestdescendant.ClosestDescendantResourceReference;
import com.pmease.gitplex.core.model.Repository;
import com.pmease.gitplex.web.component.editsave.CancelListener;
import com.pmease.gitplex.web.component.editsave.EditSavePanel;
+import com.pmease.gitplex.web.component.fileedit.FileEditPanel;
import com.pmease.gitplex.web.page.repository.file.HistoryState;
import com.pmease.gitplex.web.resource.BlobResource;
import com.pmease.gitplex.web.resource.BlobResourceReference;
@@ -164,6 +166,13 @@ public abstract class BlobViewPanel extends Panel {
changeActions.add(new AjaxLink<Void>("edit") {
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+
+ setVisible(context.getBlob().getText() != null);
+ }
+
@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
@@ -171,9 +180,51 @@ public abstract class BlobViewPanel extends Panel {
if (!context.getRepository().getRefs(Git.REFS_HEADS).containsKey(context.getState().file.revision))
tag.put("disabled", "disabled");
}
-
+
@Override
public void onClick(AjaxRequestTarget target) {
+ BlobViewPanel blobViewPanel = BlobViewPanel.this;
+ String panelId = blobViewPanel.getId();
+ IModel<Repository> repoModel = new AbstractReadOnlyModel<Repository>() {
+
+ @Override
+ public Repository getObject() {
+ return context.getRepository();
+ }
+
+ };
+
+ ObjectId commitId = context.getRepository().getObjectId(
+ context.getState().file.revision, true);
+
+ final BlobIdent file = context.getState().file;
+ String refName = Git.REFS_HEADS + file.revision;
+ final ParentPathAndName parentPathAndName = new ParentPathAndName(file.path);
+
+ FileEditPanel fileEditPanel = new FileEditPanel(panelId, repoModel, refName,
+ parentPathAndName, context.getBlob().getText().getContent(), commitId) {
+
+ @Override
+ protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId, String newName) {
+ Repository repo = context.getRepository();
+ repo.cacheObjectId(file.revision, newCommitId);
+
+ if (newName.equals(parentPathAndName.getName())) {
+ viewBlob(target);
+ } else {
+ String newPath = parentPathAndName.getPath(newName);
+ context.onSelect(target, new BlobIdent(file.revision, newPath, file.mode), null);
+ }
+ }
+
+ @Override
+ protected void onCancel(AjaxRequestTarget target) {
+ viewBlob(target);
+ }
+
+ };
+ blobViewPanel.replaceWith(fileEditPanel);
+ target.add(fileEditPanel);
}
});
@@ -206,29 +257,19 @@ public abstract class BlobViewPanel extends Panel {
final BlobIdent file = context.getState().file;
String refName = Git.REFS_HEADS + file.revision;
- String parentPath;
- String oldName;
- if (file.path.contains("/")) {
- parentPath = StringUtils.substringBeforeLast(file.path, "/");
- oldName = StringUtils.substringAfterLast(file.path, "/");
- } else {
- parentPath = null;
- oldName = file.path;
- }
+
+ final ParentPathAndName parentPathAndName = new ParentPathAndName(file.path);
CancelListener cancelListener = new CancelListener() {
@Override
public void onCancel(AjaxRequestTarget target) {
- BlobViewPanel blobViewPanel = context.render(getId());
- replaceWith(blobViewPanel);
- target.add(blobViewPanel);
- target.appendJavaScript("$(window).resize();");
+ viewBlob(target);
}
};
EditSavePanel saveChangePanel = new EditSavePanel(panelId, repoModel, refName,
- parentPath, oldName, null, commitId, cancelListener) {
+ parentPathAndName, null, commitId, cancelListener) {
@Override
protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
@@ -254,7 +295,7 @@ public abstract class BlobViewPanel extends Panel {
}
};
- BlobViewPanel.this.replaceWith(saveChangePanel);
+ blobViewPanel.replaceWith(saveChangePanel);
target.add(saveChangePanel);
}
@@ -262,6 +303,13 @@ public abstract class BlobViewPanel extends Panel {
setOutputMarkupId(true);
}
+
+ private void viewBlob(AjaxRequestTarget target) {
+ BlobViewPanel blobViewPanel = context.render(getId());
+ replaceWith(blobViewPanel);
+ target.add(blobViewPanel);
+ target.appendJavaScript("$(window).resize();");
+ }
protected WebMarkupContainer newCustomActions(String id) {
return new WebMarkupContainer(id);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
index e6430e6022..a0ed4d3b44 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/blobview/source/source-view.css
@@ -23,12 +23,6 @@
line-height: 1.6;
}
-.CodeMirror {
- font-size: 13px;
-}
-.CodeMirror-gutters {
- background: white;
-}
.CodeMirror-annotations {
width: 240px;
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
index 348eabe612..8bf13185b0 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.html
@@ -9,11 +9,19 @@
<a wicket:id="changes">See what changed.</a>
</div>
<form wicket:id="form">
+ <wicket:enclosure child="fileName">
<div class="form-group">
+ <label>File Name</label>
+ <input wicket:id="fileName" class="form-control"></input>
+ </div>
+ </wicket:enclosure>
+ <div class="form-group">
+ <label>Summary Commit Message</label>
<input wicket:id="summaryCommitMessage" class="form-control"></input>
</div>
<div class="form-group">
- <textarea wicket:id="detailCommitMessage" class="form-control" rows="10" placeholder="Optional extended description"></textarea>
+ <label>Detail Commit Message</label>
+ <textarea wicket:id="detailCommitMessage" class="form-control" rows="10"></textarea>
</div>
<div class="actions">
<input wicket:id="save" type="submit" class="btn btn-primary" value="Commit"></input>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
index 95defd3817..f77a63a0d8 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/editsave/EditSavePanel.java
@@ -29,6 +29,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.FileEdit;
+import com.pmease.commons.git.ParentPathAndName;
import com.pmease.commons.git.exception.NotTreeException;
import com.pmease.commons.git.exception.ObjectAlreadyExistException;
import com.pmease.commons.git.exception.ObsoleteCommitException;
@@ -45,11 +46,9 @@ public abstract class EditSavePanel extends Panel {
private final String refName;
- private final String parentPath;
+ private final ParentPathAndName parentPathAndName;
- private final String oldName;
-
- private final IModel<byte[]> contentModel;
+ private final IModel<String> contentModel;
private final CancelListener cancelListener;
@@ -64,36 +63,35 @@ public abstract class EditSavePanel extends Panel {
private String detailCommitMessage;
public EditSavePanel(String id, IModel<Repository> repoModel, String refName,
- @Nullable String parentPath, @Nullable String oldName,
- @Nullable IModel<byte[]> contentModel, ObjectId prevCommitId,
- @Nullable CancelListener cancelListener) {
+ ParentPathAndName parentPathAndName, @Nullable IModel<String> contentModel,
+ ObjectId prevCommitId, @Nullable CancelListener cancelListener) {
super(id);
this.repoModel = repoModel;
this.refName = refName;
- this.parentPath = parentPath!=null?parentPath:"";
- this.oldName = oldName;
+ this.parentPathAndName = parentPathAndName;
this.contentModel = contentModel;
this.cancelListener = cancelListener;
this.prevCommitId = prevCommitId;
- newName = oldName;
+ newName = parentPathAndName.getName();
}
private String getDefaultCommitMessage() {
+ String name = parentPathAndName.getName();
if (contentModel != null) {
- if (oldName != null) {
- if (oldName.equals(newName))
- return "Edit " + oldName;
+ if (name != null) {
+ if (name.equals(newName))
+ return "Edit " + name;
else
- return "Rename " + oldName;
+ return "Rename " + name;
} else if (newName != null) {
return "Add " + newName;
} else {
return "Add new file";
}
} else {
- return "Delete " + oldName;
+ return "Delete " + name;
}
}
@@ -102,7 +100,9 @@ public abstract class EditSavePanel extends Panel {
super.onInitialize();
final FeedbackPanel feedback = new FeedbackPanel("feedback", this);
-
+ feedback.setOutputMarkupPlaceholderTag(true);
+ add(feedback);
+
final WebMarkupContainer hasChangesContainer = new WebMarkupContainer("hasChanges");
hasChangesContainer.setVisibilityAllowed(false);
hasChangesContainer.setOutputMarkupPlaceholderTag(true);
@@ -117,10 +117,34 @@ public abstract class EditSavePanel extends Panel {
Form<?> form = new Form<Void>("form");
add(form);
-
- feedback.setOutputMarkupPlaceholderTag(true);
- add(feedback);
+
+ form.add(new TextField<String>("fileName", new IModel<String>() {
+
+ @Override
+ public void detach() {
+ }
+
+ @Override
+ public String getObject() {
+ return newName;
+ }
+
+ @Override
+ public void setObject(String object) {
+ newName = object;
+ }
+
+ }) {
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(contentModel != null);
+ }
+
+ });
+
form.add(new TextField<String>("summaryCommitMessage", new IModel<String>() {
@Override
@@ -170,6 +194,12 @@ public abstract class EditSavePanel extends Panel {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
super.onSubmit(target, form);
+
+ if (contentModel != null && StringUtils.isBlank(newName)) {
+ EditSavePanel.this.error("Please specify file name.");
+ target.add(feedback);
+ return;
+ }
try (FileRepository jgitRepo = repoModel.getObject().openAsJGitRepo()) {
String commitMessage = summaryCommitMessage;
@@ -179,23 +209,19 @@ public abstract class EditSavePanel extends Panel {
commitMessage += "\n\n" + detailCommitMessage;
User user = Preconditions.checkNotNull(GitPlex.getInstance(UserManager.class).getCurrent());
- String oldPath;
- if (oldName != null)
- oldPath = parentPath + "/" + oldName;
- else
- oldPath = null;
+ String path = parentPathAndName.getPath();
String newPath;
- byte[] content;
+ String content;
if (contentModel != null) {
- newPath = parentPath + "/" + newName;
+ newPath = parentPathAndName.getPath(newName);
content = contentModel.getObject();
} else {
newPath = null;
content = null;
}
- FileEdit edit = new FileEdit(oldPath, newPath, content);
+ FileEdit edit = new FileEdit(path, newPath, content);
ObjectId newCommitId = null;
while(newCommitId == null) {
try {
@@ -253,9 +279,11 @@ public abstract class EditSavePanel extends Panel {
} catch (ObjectAlreadyExistException e) {
EditSavePanel.this.error("A file with same name already exists. "
+ "Please choose a different name and try again.");
+ target.add(feedback);
} catch (NotTreeException e) {
EditSavePanel.this.error("A file exists where you’re trying to create a subdirectory. "
+ "Choose a new path and try again..");
+ target.add(feedback);
}
}
if (newCommitId != null)
@@ -283,6 +311,10 @@ public abstract class EditSavePanel extends Panel {
setOutputMarkupId(true);
}
+ public String getNewName() {
+ return newName;
+ }
+
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html
index abf8f8b4c5..40ea21f8d7 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.html
@@ -1,13 +1,14 @@
<wicket:panel>
<div class="file-edit">
<div class="head">
- <a wicket:id="editLink"></a>
- <a wicket:id="previewLink"></a>
- <a wicket:id="saveLink"></a>
+ <a class="edit">Edit</a>
+ <a wicket:id="previewLink" class="preview">Preview</a>
+ <a wicket:id="saveLink" class="save">Save</a>
+ <a wicket:id="cancelLink" class="cancel">Cancel</a>
</div>
<div class="body">
- <div wicket:id="edit" class="edit"></div>
- <div wicket:id="preview" class="preview"></div>
+ <div class="edit"></div>
+ <div wicket:id="preview" class="preview">TBD</div>
<div wicket:id="save" class="save"></div>
</div>
</div>
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java
index c6c8e2496e..55d4d8ed13 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/FileEditPanel.java
@@ -1,27 +1,128 @@
package com.pmease.gitplex.web.component.fileedit;
-import javax.annotation.Nullable;
-
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.CallbackParameter;
+import org.apache.wicket.ajax.markup.html.AjaxLink;
+import org.apache.wicket.markup.head.CssHeaderItem;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
+import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
+import org.apache.wicket.request.IRequestParameters;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.resource.CssResourceReference;
+import org.apache.wicket.request.resource.JavaScriptResourceReference;
import org.eclipse.jgit.lib.ObjectId;
+import com.pmease.commons.git.ParentPathAndName;
import com.pmease.gitplex.core.model.Repository;
+import com.pmease.gitplex.web.component.editsave.EditSavePanel;
@SuppressWarnings("serial")
-public class FileEditPanel extends Panel {
+public abstract class FileEditPanel extends Panel {
private final IModel<Repository> repoModel;
+ private final String refName;
+
+ private final ParentPathAndName parentPathAndName;
+
+ private final ObjectId prevCommitId;
+
+ private String content;
+
+ private AbstractDefaultAjaxBehavior previewBehavior;
+
+ private AbstractDefaultAjaxBehavior saveBehavior;
+
public FileEditPanel(String id, IModel<Repository> repoModel, String refName,
- @Nullable String oldPath, ObjectId prevCommitId) {
+ ParentPathAndName parentPathAndName, String content, ObjectId prevCommitId) {
super(id);
this.repoModel = repoModel;
+ this.refName = refName;
+ this.parentPathAndName = parentPathAndName;
+ this.content = content;
+ this.prevCommitId = prevCommitId;
}
@Override
protected void onInitialize() {
super.onInitialize();
+
+ previewBehavior = new AbstractDefaultAjaxBehavior() {
+
+ @Override
+ protected void respond(AjaxRequestTarget target) {
+ IRequestParameters params = RequestCycle.get().getRequest().getPostParameters();
+ content = params.getParameterValue("content").toString();
+ target.appendJavaScript(String.format("gitplex.fileEdit.preview('%s');", getMarkupId()));
+ }
+
+ };
+ add(new WebMarkupContainer("previewLink").add(previewBehavior));
+
+ saveBehavior = new AbstractDefaultAjaxBehavior() {
+
+ @Override
+ protected void respond(AjaxRequestTarget target) {
+ IRequestParameters params = RequestCycle.get().getRequest().getPostParameters();
+ content = params.getParameterValue("content").toString();
+ target.appendJavaScript(String.format("gitplex.fileEdit.save('%s');", getMarkupId()));
+ }
+
+ };
+ add(new WebMarkupContainer("saveLink").add(saveBehavior));
+
+ add(new AjaxLink<Void>("cancelLink") {
+
+ @Override
+ public void onClick(AjaxRequestTarget target) {
+ FileEditPanel.this.onCancel(target);
+ }
+
+ });
+
+ add(new WebMarkupContainer("preview"));
+
+ IModel<String> contentModel = new AbstractReadOnlyModel<String>() {
+
+ @Override
+ public String getObject() {
+ return content;
+ }
+
+ };
+ add(new EditSavePanel("save", repoModel, refName, parentPathAndName, contentModel, prevCommitId, null) {
+
+ @Override
+ protected void onCommitted(AjaxRequestTarget target, ObjectId newCommitId) {
+ FileEditPanel.this.onCommitted(target, newCommitId, getNewName());
+ }
+
+ });
+
+ setOutputMarkupId(true);
+ }
+
+ @Override
+ public void renderHead(IHeaderResponse response) {
+ super.renderHead(response);
+
+ response.render(JavaScriptHeaderItem.forReference(
+ new JavaScriptResourceReference(FileEditPanel.class, "file-edit.js")));
+ response.render(CssHeaderItem.forReference(
+ new CssResourceReference(FileEditPanel.class, "file-edit.css")));
+
+ String script = String.format("gitplex.fileEdit.init('%s', '%s', %s, %s);",
+ getMarkupId(), StringEscapeUtils.escapeEcmaScript(content),
+ previewBehavior.getCallbackFunction(CallbackParameter.explicit("content")),
+ saveBehavior.getCallbackFunction(CallbackParameter.explicit("content")));
+ response.render(OnDomReadyHeaderItem.forScript(script));
}
@Override
@@ -31,4 +132,7 @@ public class FileEditPanel extends Panel {
super.onDetach();
}
+ protected abstract void onCommitted(AjaxRequestTarget target, ObjectId newCommitId, String newName);
+
+ protected abstract void onCancel(AjaxRequestTarget target);
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.css b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.css
new file mode 100644
index 0000000000..c8e079ad19
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.css
@@ -0,0 +1,3 @@
+.file-edit>.body>.preview, .file-edit>.body>.save {
+ display: none;
+}
\ No newline at end of file
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.js b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.js
new file mode 100644
index 0000000000..aa7e24e73d
--- /dev/null
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/fileedit/file-edit.js
@@ -0,0 +1,52 @@
+gitplex.fileEdit = {
+ init: function(containerId, fileContent, previewCallback, saveCallback) {
+ var $container = $("#" + containerId);
+ var $code = $container.find(">file-edit>.body>.edit");
+ var cm = CodeMirror($code[0], {
+ value: fileContent,
+ theme: "eclipse",
+ lineNumbers: true,
+ lineWrapping: true,
+ styleActiveLine: true,
+ styleSelectedText: true,
+ foldGutter: true,
+ matchBrackets: true,
+ scrollbarStyle: "simple",
+ highlightIdentifiers: {delay: 500}
+ });
+
+ var modeInfo = CodeMirror.findModeByFileName(filePath);
+ if (modeInfo) {
+ // specify mode via mime does not work for gfm (github flavored markdown)
+ if (modeInfo.mode === "gfm")
+ cm.setOption("mode", "gfm");
+ else
+ cm.setOption("mode", modeInfo.mime);
+ CodeMirror.autoLoadMode(cm, modeInfo.mode);
+ }
+
+ $container.find(">file-edit>.head>.edit").click(function() {
+ $container.find(">.file-edit>.body>.preview").hide();
+ $container.find(">.file-edit>.body>.save").hide();
+ $container.find(">.file-edit>.body>.edit").show();
+ });
+ $container.find(">.file-edit>.head>.preview").click(function() {
+ previewCallback($cm.getValue());
+ });
+ $container.find(">.file-edit>.head>.save").click(function() {
+ saveCallback($cm.getValue());
+ });
+ },
+ save: function(containerId) {
+ var $container = $("#" + containerId);
+ $container.find(">.file-edit>.body>.edit").hide();
+ $container.find(">.file-edit>.body>.preview").hide();
+ $container.find(">.file-edit>.body>.save").show();
+ },
+ preview: function(containerId) {
+ var $container = $("#" + containerId);
+ $container.find(">.file-edit>.body>.edit").hide();
+ $container.find(">.file-edit>.body>.save").hide();
+ $container.find(">.file-edit>.body>.preview").show();
+ }
+}