added publish (git add, git push)

master
David Zálešák 4 years ago
parent dcfaef47a6
commit 35138ec30b

@ -113,6 +113,19 @@ sub {
top: 0; top: 0;
} }
.publishLoading {
display: none;
}
#errorPublish {
display: none;
text-align: center;
background-color: red;
padding: 0.5em 0;
font-weight: bold;
color: white;
}
.settingsBtn { .settingsBtn {
float: right; float: right;
} }
@ -146,7 +159,7 @@ form {
text-align: end; text-align: end;
} }
#settingsClose { .settingsClose {
background-color: rgb(88, 88, 88); background-color: rgb(88, 88, 88);
color: rgb(255, 255, 255); color: rgb(255, 255, 255);
cursor: pointer; cursor: pointer;

@ -51,7 +51,7 @@
</footer> </footer>
<div class="settingsbg"> <div class="settingsbg">
<div class="settings"> <div class="settings">
<div id="settingsClose" class="close">X</div> <div class="close settingsClose">X</div>
<form action="javascript:void(0);"> <form action="javascript:void(0);">
<label for="inputRepository">Git repository:</label> <label for="inputRepository">Git repository:</label>
<input type="text" id="inputRepository" required> <input type="text" id="inputRepository" required>
@ -61,20 +61,26 @@
<input type="text" id="inputName" required> <input type="text" id="inputName" required>
<label for="inputEmail">Email:</label> <label for="inputEmail">Email:</label>
<input type="email" id="inputEmail" required> <input type="email" id="inputEmail" required>
<button class="close">Cancel</button>
<button id="settingsSubmit">Submit</button> <button id="settingsSubmit">Submit</button>
<button id="settingsCancel" class="close">Cancel</button>
</form> </form>
</div> </div>
</div> </div>
<div class="commitWrapper"> <div class="commitWrapper">
<div class="commit"> <div class="commit">
<div id="settingsClose" class="close">X</div> <div class="close settingsClose">X</div>
<div id="errorPublish">Test</div>
<form action="javascript:void(0);"> <form action="javascript:void(0);">
<label for="inputCommitMsg">Poznámka k úpravám:</label> <label for="inputCommitMsg">Poznámka k úpravám:</label>
<input type="text" id="inputCommitMsg"> <input type="text" id="inputCommitMsg" required>
<button id="settingsSubmit">Submit</button> <label for="inputUser">Git uživatelské jméno:</label>
<button id="settingsCancel" class="close">Cancel</button> <input type="text" id="inputUser" required>
<label for="inputPasswd">Git heslo:</label>
<input type="password" id="inputPasswd" required>
<button class="close">Cancel</button>
<button id="commitSubmit">Publikovat</button>
</form> </form>
<img src="./img/loading.gif" alt="loading" class="publishLoading">
</div> </div>
</div> </div>
</div> </div>

@ -3,9 +3,6 @@ class Controller {
this.model = model; this.model = model;
this.view = view; this.view = view;
this.redrawDirTree(view);
//this.pullRep();
this.addListeners(); this.addListeners();
this.init(); this.init();
} }
@ -40,15 +37,22 @@ class Controller {
} }
); );
} }
if (localStorage.getItem("pushError")) {
this.view.openPublish();
this.view.errorPublish("Máte nepublikované změny: publikujte je.");
}
} }
addListeners() { addListeners() {
var cloneBtn = document.getElementsByClassName("clone")[0]; var cloneBtn = document.getElementsByClassName("clone")[0];
var purgeBtn = document.getElementsByClassName("wipe")[0]; var purgeBtn = document.getElementsByClassName("wipe")[0];
var settingsBtn = document.getElementsByClassName("settingsBtn")[0]; var settingsBtn = document.getElementsByClassName("settingsBtn")[0];
var settingsClose = document.getElementsByClassName("close"); var closeDialog = document.getElementsByClassName("close");
var settingsSubmit = document.getElementById("settingsSubmit"); var settingsSubmit = document.getElementById("settingsSubmit");
var saveBtn = document.getElementById("saveBtn"); var saveBtn = document.getElementById("saveBtn");
var publishBtn = document.getElementsByClassName("publishBtn")[0];
var commitSubmit = document.getElementById("commitSubmit");
cloneBtn.addEventListener("click", this.cloneRep.bind(this)); cloneBtn.addEventListener("click", this.cloneRep.bind(this));
purgeBtn.addEventListener("click", this.wipeFS.bind(this)); purgeBtn.addEventListener("click", this.wipeFS.bind(this));
@ -56,15 +60,17 @@ class Controller {
"click", "click",
this.view.openSettings.bind(this.view) this.view.openSettings.bind(this.view)
); );
for (let i = 0; i < settingsClose.length; i++) { for (let i = 0; i < closeDialog.length; i++) {
settingsClose[i].addEventListener( closeDialog[i].addEventListener(
"click", "click",
this.view.closeSettings.bind(this) this.view.closeDialog.bind(this)
); );
} }
settingsSubmit.addEventListener("click", this.saveSettings.bind(this)); settingsSubmit.addEventListener("click", this.saveSettings.bind(this));
this.view.simplemde.codemirror.on("change", this.saveButton.bind(this)); this.view.simplemde.codemirror.on("change", this.saveButton.bind(this));
saveBtn.addEventListener("click", this.saveFile.bind(this)); saveBtn.addEventListener("click", this.saveFile.bind(this));
publishBtn.addEventListener("click", this.view.openPublish.bind(this.view));
commitSubmit.addEventListener("click", this.publish.bind(this));
} }
saveButton() { saveButton() {
@ -102,8 +108,10 @@ class Controller {
this.redrawDirTree(this.view); this.redrawDirTree(this.view);
}.bind(this), }.bind(this),
function (error) { function (error) {
// zatim to teda znovu naclonuj
this.cloneRep();
console.log(error); console.log(error);
} }.bind(this)
); );
} }
@ -153,10 +161,9 @@ class Controller {
// udělej clone // udělej clone
this.cloneRep(); this.cloneRep();
} else { } else {
// udelej jenom pull this.redrawDirTree(this.view);
this.pullRep();
} }
this.view.closeSettings(); this.view.closeDialog();
} }
loadFile(file) { loadFile(file) {
@ -172,6 +179,53 @@ class Controller {
); );
} }
publish() {
let msg = document.getElementById("inputCommitMsg");
let gitUser = document.getElementById("inputUser");
let gitPass = document.getElementById("inputPasswd");
if (msg.value == "" || gitUser.value == "" || gitPass.gitPass == "") {
return;
}
let loading = document.getElementsByClassName("publishLoading")[0];
loading.style.display = "block";
this.model.gitAddAll().then(
function (value) {
this.model.gitCommit(msg.value).then(
function (value) {
this.updateFileStats();
this.model.gitPush(gitUser.value, gitPass.value).then(
function (value) {
localStorage.setItem("pushError", "false");
loading.style.removeProperty("display");
this.view.closeDialog();
this.view.errorPublish();
}.bind(this),
function (error) {
console.log(error);
if (error == "HttpError: HTTP Error: 401 Unauthorized") {
this.view.errorPublish(
"Změny nebyly publikovány: Chyba přihlášení"
);
}
loading.style.removeProperty("display");
localStorage.setItem("pushError", "true");
}.bind(this)
);
}.bind(this),
function (error) {
console.log(error);
}
);
}.bind(this),
function (error) {
console.log(error);
}
);
}
saveFile() { saveFile() {
var textArea = this.view.simplemde; var textArea = this.view.simplemde;
this.model.saveFile(this.openedFile, textArea.value()); this.model.saveFile(this.openedFile, textArea.value());

@ -57,8 +57,6 @@ class Model {
} }
async dirGitStatus(list) { async dirGitStatus(list) {
let dirlist = [list[0]];
for (let i = 1; i < list.length; i++) { for (let i = 1; i < list.length; i++) {
if (Array.isArray(list[i])) { if (Array.isArray(list[i])) {
await this.dirGitStatus(list[i]); await this.dirGitStatus(list[i]);
@ -103,6 +101,52 @@ class Model {
}); });
} }
async gitAddAll(dirTree) {
if (!dirTree) {
dirTree = await this.getDirTree();
}
for (let i = 1; i < dirTree.length; i++) {
if (Array.isArray(dirTree[i])) {
await this.gitAddAll(dirTree[i]);
} else {
await git.add({
fs,
dir,
filepath: dirTree[0].substring(1) + dirTree[i],
});
console.log(dirTree[0].substring(1) + dirTree[i]);
}
}
}
async gitCommit(msg) {
await git.commit({
fs,
dir,
message: msg,
author: {
name: localStorage.getItem("name"),
email: localStorage.getItem("email"),
},
});
}
async gitPush(gitUser, gitPass) {
let pushResult = await git.push({
fs,
http,
dir,
onAuth: (url) => {
const auth = {
username: gitUser,
password: gitPass,
};
return auth;
},
});
console.log(pushResult);
}
setRepo(repoURL) { setRepo(repoURL) {
localStorage.setItem("repo", repoURL); localStorage.setItem("repo", repoURL);
} }

@ -117,13 +117,34 @@ class View {
email.value = localStorage.getItem("email"); email.value = localStorage.getItem("email");
} }
closeSettings() { closeDialog() {
var settings = document.getElementsByClassName("settingsbg")[0]; var settings = document.getElementsByClassName("settingsbg")[0];
var publish = document.getElementsByClassName("commitWrapper")[0];
settings.style.removeProperty("display"); settings.style.removeProperty("display");
publish.style.removeProperty("display");
}
openPublish() {
this.closeEditor();
var publish = document.getElementsByClassName("commitWrapper")[0];
publish.style.display = "grid";
} }
showSaveButton(show) { showSaveButton(show) {
var saveButton = document.getElementById("saveBtn"); var saveButton = document.getElementById("saveBtn");
saveButton.disabled = show; saveButton.disabled = show;
} }
errorPublish(error) {
let errorDiv = document.getElementById("errorPublish");
if (!error) {
errorDiv.style.removeProperty("display");
return;
}
errorDiv.style.display = "block";
errorDiv.innerHTML = error;
}
} }