From 35138ec30b04497df42820a01ba83653d1b2b22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Z=C3=A1le=C5=A1=C3=A1k?= Date: Thu, 7 Jan 2021 22:54:41 +0100 Subject: [PATCH] added publish (git add, git push) --- css/style.css | 15 +++++++++- index.html | 18 ++++++++---- js/controller.js | 76 +++++++++++++++++++++++++++++++++++++++++------- js/model.js | 48 ++++++++++++++++++++++++++++-- js/view.js | 23 ++++++++++++++- 5 files changed, 159 insertions(+), 21 deletions(-) diff --git a/css/style.css b/css/style.css index 84338fd..ab89188 100644 --- a/css/style.css +++ b/css/style.css @@ -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; diff --git a/index.html b/index.html index 0c81374..e767475 100644 --- a/index.html +++ b/index.html @@ -51,7 +51,7 @@
-
X
+
X
@@ -61,20 +61,26 @@ + -
-
X
+
X
+
Test
- - - + + + + + + +
+ loading
diff --git a/js/controller.js b/js/controller.js index 7dbde69..6d99c65 100644 --- a/js/controller.js +++ b/js/controller.js @@ -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()); diff --git a/js/model.js b/js/model.js index 674396e..72ab65f 100644 --- a/js/model.js +++ b/js/model.js @@ -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); } diff --git a/js/view.js b/js/view.js index ef86425..d9acc20 100644 --- a/js/view.js +++ b/js/view.js @@ -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; + } }