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

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

@ -3,9 +3,6 @@ class Controller {
this.model = model;
this.view = view;
this.redrawDirTree(view);
//this.pullRep();
this.addListeners();
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() {
var cloneBtn = document.getElementsByClassName("clone")[0];
var purgeBtn = document.getElementsByClassName("wipe")[0];
var settingsBtn = document.getElementsByClassName("settingsBtn")[0];
var settingsClose = document.getElementsByClassName("close");
var closeDialog = document.getElementsByClassName("close");
var settingsSubmit = document.getElementById("settingsSubmit");
var saveBtn = document.getElementById("saveBtn");
var publishBtn = document.getElementsByClassName("publishBtn")[0];
var commitSubmit = document.getElementById("commitSubmit");
cloneBtn.addEventListener("click", this.cloneRep.bind(this));
purgeBtn.addEventListener("click", this.wipeFS.bind(this));
@ -56,15 +60,17 @@ class Controller {
"click",
this.view.openSettings.bind(this.view)
);
for (let i = 0; i < settingsClose.length; i++) {
settingsClose[i].addEventListener(
for (let i = 0; i < closeDialog.length; i++) {
closeDialog[i].addEventListener(
"click",
this.view.closeSettings.bind(this)
this.view.closeDialog.bind(this)
);
}
settingsSubmit.addEventListener("click", this.saveSettings.bind(this));
this.view.simplemde.codemirror.on("change", this.saveButton.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() {
@ -102,8 +108,10 @@ class Controller {
this.redrawDirTree(this.view);
}.bind(this),
function (error) {
// zatim to teda znovu naclonuj
this.cloneRep();
console.log(error);
}
}.bind(this)
);
}
@ -153,10 +161,9 @@ class Controller {
// udělej clone
this.cloneRep();
} else {
// udelej jenom pull
this.pullRep();
this.redrawDirTree(this.view);
}
this.view.closeSettings();
this.view.closeDialog();
}
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() {
var textArea = this.view.simplemde;
this.model.saveFile(this.openedFile, textArea.value());

@ -57,8 +57,6 @@ class Model {
}
async dirGitStatus(list) {
let dirlist = [list[0]];
for (let i = 1; i < list.length; i++) {
if (Array.isArray(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) {
localStorage.setItem("repo", repoURL);
}

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