You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

271 lines
7.5 KiB
JavaScript

class Controller {
constructor(model, view) {
this.model = model;
this.view = view;
model.getDirTree().then(
function () {
this.addListeners();
this.init();
}.bind(this)
);
}
init() {
let repo = localStorage.getItem("repo");
let name = localStorage.getItem("name");
let email = localStorage.getItem("email");
if (
repo == null ||
name == null ||
email == null ||
repo == "" ||
name == "" ||
email == ""
) {
this.view.openSettings();
} else {
if (this.model.dirTree.length == 1) {
//Nic nenaklonovaného, klonovat
this.cloneRep();
} else {
//Udělat Pull
this.pullRep();
}
}
if (localStorage.getItem("pushError") == "true") {
this.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 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));
settingsBtn.addEventListener(
"click",
this.view.openSettings.bind(this.view)
);
for (let i = 0; i < closeDialog.length; i++) {
closeDialog[i].addEventListener(
"click",
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.openPublish.bind(this));
commitSubmit.addEventListener("click", this.publish.bind(this));
}
/* ================== Dir Tree ================== */
redrawDirTree() {
this.view.removeDirTree();
this.view.drawDirTree(this.model.dirTree, undefined, this);
this.view.dirTreeToggler();
this.updateFileStats();
}
updateFileStats() {
this.view.statDirTree(this.model.gitDirTreeStat, undefined);
}
/* ================== Text Area ================== */
saveButton() {
var textArea = this.view.simplemde;
this.view.showSaveButton(textArea.value() == this.openedFileValue);
}
loadFile(file) {
this.openedFile = file;
this.model.readFile(file).then(
function (value) {
this.openedFileValue = value;
this.view.openFile(value);
}.bind(this),
function (error) {
console.log(error);
}
);
}
saveFile() {
var textArea = this.view.simplemde;
this.model.saveFile(this.openedFile, textArea.value()).then(
function () {
this.openedFileValue = textArea.value();
this.view.closeEditor();
this.saveButton();
this.updateFileStats();
}.bind(this)
);
}
/* ================== Controls ================== */
wipeFS() {
this.view.closeEditor();
this.model.wipeFS().then(
function () {
this.redrawDirTree();
}.bind(this)
);
}
cloneRep() {
var loading = document.getElementsByClassName("loading")[0];
loading.style.display = "grid";
this.model.cloneRep().then(
function (value) {
this.redrawDirTree();
loading.style.removeProperty("display");
localStorage.setItem("pushError", "false");
this.view.errorPublish();
}.bind(this),
function (error) {
console.log(error);
}
);
}
pullRep() {
var loading = document.getElementsByClassName("loading")[0];
loading.style.display = "grid";
this.model.pullRep().then(
function (value) {
loading.style.removeProperty("display");
this.redrawDirTree();
}.bind(this),
function (error) {
// zatim to teda znovu naclonuj
this.cloneRep();
console.log(error);
}.bind(this)
);
}
gitPush(gitUser, gitPass) {
let loading = document.getElementsByClassName("publishLoading")[0];
this.model.gitPush(gitUser, gitPass).then(
function (value) {
localStorage.setItem("pushError", "false");
this.view.closeDialog();
this.view.errorPublish();
loading.style.removeProperty("display");
}.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í.");
this.openPublish();
} else if (error == "HttpError: HTTP Error: 403 Forbidden") {
this.view.errorPublish(
"Změny nebyly publikovány: Nemáte práva publikovat do tohoto repozitáře."
);
this.openPublish();
}
localStorage.setItem("pushError", "true");
loading.style.removeProperty("display");
}.bind(this)
);
}
/* ================== Windows ================== */
/* ------------------ Settings ------------------ */
saveSettings() {
let repo = document.getElementById("inputRepository");
let baseDir = document.getElementById("inputBaseDir");
let name = document.getElementById("inputName");
let email = document.getElementById("inputEmail");
let oldRepoURL = localStorage.getItem("repo");
this.model.setRepo(repo.value);
this.model.setBaseDir(baseDir.value).then(
function () {
this.redrawDirTree();
}.bind(this)
);
this.model.setName(name.value);
this.model.setEmail(email.value);
if (repo.value == "" || name.value == "" || email.value == "") {
this.view.openSettings();
return;
}
if (repo.value != oldRepoURL) {
// udělej clone
this.cloneRep();
}
this.view.closeDialog();
}
/* ------------------ Publish ------------------ */
publish() {
let msg = document.getElementById("inputCommitMsg");
let gitUser = document.getElementById("inputUser");
let gitPass = document.getElementById("inputPasswd");
let loading = document.getElementsByClassName("publishLoading")[0];
if (localStorage.getItem("pushError") == "true") {
if (gitUser.value == "" || gitPass.value == "") {
this.view.errorPublish("Vyplňte potřebné údaje.");
return;
}
loading.style.display = "block";
this.gitPush(gitUser.value, gitPass.value);
} else {
if (msg.value == "" || gitUser.value == "" || gitPass.value == "") {
this.view.errorPublish("Vyplňte potřebné údaje.");
return;
}
loading.style.display = "block";
this.model.gitAdd().then(
function (value) {
this.model.gitCommit(msg.value).then(
function (value) {
this.updateFileStats();
this.gitPush(gitUser.value, gitPass.value);
}.bind(this),
function (error) {
console.log(error);
}
);
}.bind(this),
function (error) {
console.log(error);
}
);
}
}
openPublish() {
this.model.getChangedFiles().then(
function (v) {
this.view.openPublish(v);
}.bind(this)
);
}
}