Skip to content

Commit 0425782

Browse files
committed
feat(tpl): use form submission for deleting
Reference: #5
1 parent 81c6096 commit 0425782

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/serverHandler/page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func updateSubItemsHtml(data *responseData) {
4343
_, isRenamedInfo := info.(*renamedFileInfo)
4444
_, isFakeInfo := info.(*fakeFileInfo)
4545
if !isRenamedInfo && !isFakeInfo {
46-
deleteUrl = data.SubItemPrefix + "?delete&name=" + urlEscapedName
46+
deleteUrl = name
4747
}
4848
}
4949

src/tpl/frontend/index.css

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ html {
88
font-family: "roboto_condensedbold", "Helvetica Neue", Helvetica, Arial, sans-serif;
99
}
1010

11-
body, input, textarea {
11+
body, input, textarea, button {
1212
font-family: Consolas, "Lucida Console", "San Francisco Mono", Menlo, Monaco, "Andale Mono", "DejaVu Sans Mono", monospace;
1313
}
1414

@@ -353,14 +353,22 @@ em {
353353
top: 0;
354354
right: 0;
355355
bottom: 0;
356+
display: flex;
357+
align-items: stretch;
358+
}
359+
360+
.item-list .delete button {
361+
border: 0;
356362
color: #800000;
363+
background: none;
357364
font-weight: bold;
358365
font-size: 1.6em;
359366
line-height: 1em;
360367
padding: 0.1875em 0.3125em 0.3125em;
368+
cursor: pointer;
361369
}
362370

363-
.item-list .delete:hover {
371+
.item-list .delete button:hover {
364372
background: #fee;
365373
}
366374

src/tpl/frontend/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<body class="{{if .IsRoot}}root-dir{{else}}sub-dir{{end}}">
1414
{{$contextQueryString := .Context.QueryString}}
1515
{{$isDownload := .IsDownload}}
16+
{{$SubItemPrefix := .SubItemPrefix}}
1617
{{if not $isDownload}}
1718
<ol class="path-list">
1819
{{range .Paths}}
@@ -64,9 +65,8 @@
6465

6566
{{if .CanDelete}}
6667
<script type="text/javascript">
67-
function confirmDelete(el) {
68-
var href = el.href;
69-
var name = decodeURIComponent(href.substr(href.lastIndexOf('=') + 1));
68+
function confirmDelete(form) {
69+
var name = form.name.value;
7070
return confirm('{{.Trans.DeleteConfirm}}\n' + name);
7171
}
7272
</script>
@@ -98,7 +98,7 @@
9898
<span class="field size">{{.DisplaySize}}</span>
9999
<span class="field time">{{.DisplayTime}}</span>
100100
</a>
101-
{{if and (not $isDownload) .DeleteUrl}}<a href="{{.DeleteUrl}}" class="delete" onclick="return confirmDelete(this)">x</a>{{end}}
101+
{{if and (not $isDownload) .DeleteUrl}}<form class="delete" action="{{$SubItemPrefix}}" onsubmit="return confirmDelete(this)"><input type="hidden" name="delete"/><input type="hidden" name="name" value="{{.DeleteUrl}}"/><button type="submit">x</button></form>{{end}}
102102
</li>
103103
{{end}}
104104
</ul>

src/tpl/frontend/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,8 @@
946946
return;
947947
}
948948

949-
itemList.addEventListener('click', function (e) {
950-
if (e.defaultPrevented || !e.target || !e.target.href || e.target.className.indexOf('delete') < 0) {
949+
itemList.addEventListener('submit', function (e) {
950+
if (e.defaultPrevented) {
951951
return;
952952
}
953953

@@ -963,8 +963,22 @@
963963
elItemParent && elItemParent.removeChild(elItem);
964964
}
965965

966+
var params = '';
967+
var els = [];
968+
Array.prototype.push.apply(els, e.target.elements)
969+
for (var i = 0, len = els.length; i < len; i++) {
970+
if (!els[i].name) {
971+
continue
972+
}
973+
if (params.length > 0) {
974+
params += '&'
975+
}
976+
params += els[i].name + '=' + encodeURIComponent(els[i].value)
977+
}
978+
var url = e.target.action + '?' + params
979+
966980
var xhr = new XMLHttpRequest();
967-
xhr.open('POST', e.target.href); // will retrieve deleted result into bfcache
981+
xhr.open('POST', url); // will retrieve deleted result into bfcache
968982
xhr.addEventListener('load', onLoad);
969983
xhr.send();
970984
e.preventDefault();

0 commit comments

Comments
 (0)