class Controller { constructor(model, view) { this.model = model; this.view = view; this.redrawDirTree(view); //this.pullRep(); this.addListeners(); this.init(); } 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 { this.model.getDirTree().then( function (value) { if (value.length == 1) { //Nic nenaklonovaného, klonovat this.cloneRep(); } else { //Udělat Pull this.pullRep(); } }.bind(this), function (error) { console.log(error); } ); } } 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 settingsSubmit = document.getElementById("settingsSubmit"); var saveBtn = document.getElementById("saveBtn"); 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 < settingsClose.length; i++) { settingsClose[i].addEventListener( "click", this.view.closeSettings.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)); } saveButton() { var textArea = this.view.simplemde; this.view.showSaveButton(textArea.value() == this.openedFileValue); } wipeFS() { this.model.wipeFS(); //window.location.reload(); this.redrawDirTree(this.view); } cloneRep() { var loading = document.getElementsByClassName("loading")[0]; loading.style.display = "grid"; this.model.cloneRep().then( function (value) { loading.style.removeProperty("display"); this.redrawDirTree(this.view); }.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(this.view); }.bind(this), function (error) { console.log(error); } ); } redrawDirTree(view) { this.model.getDirTree().then( function (value) { view.removeDirTree(); view.drawDirTree(value, undefined, this); view.dirTreeToggler(); this.updateFileStats(); }.bind(this), function (error) { console.log(error); } ); } updateFileStats() { this.model.getGitStatusTree().then( function (value) { this.view.statDirTree(value, undefined); }.bind(this), function (error) { console.log(error); } ); } 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); 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(); } else { // udelej jenom pull this.pullRep(); } this.view.closeSettings(); } 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()); this.openedFileValue = textArea.value(); this.saveButton(); this.updateFileStats(); } }