Skip to content

Commit 5a5a1dd

Browse files
committed
(refs #47) Add remove file button
1 parent 79103b7 commit 5a5a1dd

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/main/scala/gitbucket/gist/controller/GistController.scala

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ trait GistControllerBase extends ControllerBase {
100100
post("/gist/_new")(usersOnly {
101101
if(context.loginAccount.isDefined){
102102
val loginAccount = context.loginAccount.get
103-
val files = getFileParameters(true)
103+
val files = getFileParameters()
104104

105105
if(files.isEmpty){
106106
redirect(s"/gist")
@@ -139,7 +139,7 @@ trait GistControllerBase extends ControllerBase {
139139
val repoName = params("repoName")
140140

141141
val loginAccount = context.loginAccount.get
142-
val files = getFileParameters(true)
142+
val files = getFileParameters()
143143
val description = params("description")
144144
val mode = Mode.from(params("mode"))
145145

@@ -501,20 +501,12 @@ trait GistControllerBase extends ControllerBase {
501501
}
502502
}
503503

504-
private def getFileParameters(flatten: Boolean): Seq[(String, String)] = {
504+
private def getFileParameters(): Seq[(String, String)] = {
505505
val count = params("count").toInt
506-
if(flatten){
507-
(0 to count - 1).flatMap { i =>
508-
(params.get(s"fileName-${i}"), params.get(s"content-${i}")) match {
509-
case (Some(fileName), Some(content)) if(content.nonEmpty) => Some((if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content))
510-
case _ => None
511-
}
512-
}
513-
} else {
514-
(0 to count - 1).map { i =>
515-
val fileName = request.getParameter(s"fileName-${i}")
516-
val content = request.getParameter(s"content-${i}")
517-
(if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content)
506+
(0 to count - 1).flatMap { i =>
507+
(params.get(s"fileName-${i}"), params.get(s"content-${i}")) match {
508+
case (Some(fileName), Some(content)) if(content.nonEmpty) => Some((if(fileName.isEmpty) s"gistfile${i + 1}.txt" else fileName, content))
509+
case _ => None
518510
}
519511
}
520512
}

src/main/twirl/gitbucket/gist/edit.scala.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,14 @@ <h1 style="margin: 0px;">New snippet</h1>
105105
}
106106
var value = null;
107107
for(var i = 0; i < count; i++){
108-
value = ace.edit('editor-' + i).getValue();
109-
if(value == ''){
110-
displayError('Files can\'t be empty.');
111-
return false;
112-
} else {
113-
$('#content-' + i).val(ace.edit('editor-' + i).getValue());
108+
if($('#editor-area-' + i).length == 1){
109+
value = ace.edit('editor-' + i).getValue();
110+
if(value == ''){
111+
displayError('Files can\'t be empty.');
112+
return false;
113+
} else {
114+
$('#content-' + i).val(ace.edit('editor-' + i).getValue());
115+
}
114116
}
115117
}
116118
return true;

src/main/twirl/gitbucket/gist/editor.scala.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
fileName: String,
33
content: gitbucket.core.util.JGitUtil.ContentInfo)(implicit context: gitbucket.core.controller.Context)
44
@import gitbucket.core.view.helpers
5-
<div class="panel panel-default">
5+
<div class="panel panel-default" id="editor-area-@i">
66
<div class="panel-heading" style="padding: 6px;">
77
<div class="pull-right">
88
<select id="wrap-@i" class="form-control" style="margin-bottom: 0px; padding: 0px;">
@@ -12,7 +12,8 @@
1212
</optgroup>
1313
</select>
1414
</div>
15-
<input type="text" id="fileName-@i" name="fileName-@i" class="form-control" value="@fileName" placeholder="Name this file..." style="width: 300px; margin-bottom: 0px;">
15+
<a href="javascript:void(0);" style="padding-left: 8px; padding-right: 6px;" id="remove-@i" data-index="@i"><span class="octicon octicon-x"></span></a>
16+
<input type="text" id="fileName-@i" name="fileName-@i" class="form-control" value="@fileName" placeholder="Name this file..." style="width: 300px; margin-bottom: 0px; display: inline;">
1617
</div>
1718
<div class="panel-body">
1819
<div id="editor-@i" class="editor" data-index="@i" style="width: 100%; height: 400px;"></div>
@@ -40,5 +41,11 @@
4041
editor.getSession().setUseWrapMode(false);
4142
}
4243
});
44+
45+
$('#remove-@i').click(function(){
46+
if(confirm('Remove this file. Are you sure?')){
47+
$('#editor-area-@i').remove();
48+
}
49+
});
4350
});
4451
</script>

0 commit comments

Comments
 (0)