-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
170 lines (154 loc) · 5.65 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html" lang="pl">
<head>
<meta charset="UTF-8">
<script type="module" src="./songbook.js"></script>
<link rel="stylesheet" href="./song.css"/>
<link rel="stylesheet" href="./ch.css"/>
<link rel="icon" type="image/png" href="./favicon.png" />
<script>
function init() {
const urlParams = new URLSearchParams(window.location.search);
const {baseUrl,branch,file,user} = parseParams();
const loadUrl = urlParams.get('load') ? urlParams.get('load') : baseUrl ? `${baseUrl}/users/${user}/changes/${branch}/${file}` : null;
const commitOnLoad = urlParams.get('commitOnLoad');
const newFile = urlParams.get('new');
const songEditor = document.getElementsByTagName("song-editor")[0];
songEditor.setAttribute("git", branch && branch.trim() != '');
if (loadUrl) {
songEditor.setAttribute("title", "Wczytywanie ...");
fetch(loadUrl, {credentials: 'include', method: 'GET', redirect: 'follow'})
.then( (response) => {
console.log("Fetched: " + JSON.stringify(response));
if (response.status == 404 && newFile == "maybe") {
songEditor.New();
return null;
}
if (response.status != 200) {
songEditor.setAttribute("title", "Nie udało się otworzyć pliku do edycji.");
return null;
}
return response.text();
})
.then((txt) => {
if (txt == null) {
return;
}
if (!songEditor.Load(txt)) {
songEditor.setAttribute("title", "Wczytywanie się nie powiodło :(");
return;
}
if (commitOnLoad) {
// The goal is to mark the file that is being touched to prevent deletion and
// to distinguish user's changes from automatic format change.
const title = songEditor.getAttribute("title");
const msg = `Piosenka ${title}: Initial`;
const ser = songEditor.Serialize();
if (txt !== ser) {
commitInternal(songEditor, ser, msg);
}
}
})
} else {
songEditor.New();
}
songEditor.addEventListener("git:commit", async (e) => {
await commit(e.target);
} );
songEditor.addEventListener("git:commitAndPublish", (e)=>commitAndPublish(e) );
const topNav = document.getElementById("topnav");
for (const nav of [document.getElementById("topnav"), document.getElementById("bottomnav")]) {
if (baseUrl) {
nav.innerHTML = `<a href="/">[Piosenki]</a> <a href="${baseUrl}/users/me/changes">[Inne rozpoczęte edycje]</a>`;
nav.hidden = false;
} else {
nav.innerHTML = '';
nav.hidden = true;
}
}
}
function parseParams() {
const urlParams = new URLSearchParams(window.location.search);
return {
baseUrl: urlParams.get('baseUrl'),
branch: urlParams.get('branch'),
file: urlParams.get('file'),
user: urlParams.get('user')
}
}
function notice(txt, owner=null) {
const notice = document.createElement("div");
notice.classList.add("notice");
notice.innerText=txt;
setTimeout(() => {
notice.remove();
}, 2000)
if (!owner) {
document.getElementsByTagName("BODY")[0].insertBefore(notice, null);
} else {
owner.parentNode.insertBefore(notice, owner.nextSibling);
}
}
async function commitInternal(songbook, serialized, msg) {
try {
const config = {
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'text/xml',
},
body: serialized
}
const {baseUrl,branch,file,user} = parseParams();
const response = await fetch(`${baseUrl}/users/${user}/changes/${branch}/${file}?msg=${encodeURIComponent(msg)}`, config)
if (response.ok) {
songbook.MarkAsCommitted(serialized);
return true;
} else {
alert("Zapis się nie powiódł. Czy jesteś podlączony do internetu ?");
console.warn(response);
return false;
}
} catch (error) {
console.error(error);
alert(JSON.stringify(error));
return false;
}
}
async function commit(songbook) {
const {serialized, changed, title} = songbook.SerializeWithChange();//document.getElementsByTagName("song-editor")[0].Serialize();
if (!changed) {
notice("Brak nowych zmian", songbook);
return true; // To taki sukces.
}
const {branch} = parseParams();
console.log(serialized);
if (branch) {
let msg = prompt("Wprowadź, proszę, krótki opis ostatnich zmian", `...`);
if (!msg) {
return false;
}
msg = `Piosenka ${title}: ` + msg;
const ci = commitInternal(songbook, serialized, msg)
notice("Zapisano", songbook);
return ci
}
return false;
}
async function commitAndPublish(e) {
if (await commit(e.target)) {
const {baseUrl,branch,user} = parseParams();
window.location = `${baseUrl}/users/${user}/changes/${branch}` + ':publish'
}
}
</script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"/>
</head>
<body onload="init()">
<div id="topnav"></div>
<h1>Edytor piosenek</h1>
<song-editor></song-editor>
<div id="bottomnav"></div>
</body>
</html>