Skip to content

Commit 4ec5870

Browse files
committed
Show error message
1 parent 4897e2a commit 4ec5870

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

css/style.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,18 @@ select:not([multiple]) {
554554
margin: auto;
555555
}
556556

557+
#invalid-message {
558+
display: none;
559+
position: inherit;
560+
color: red;
561+
z-index: 4000;
562+
font-size: 14px;
563+
padding: 10px;
564+
margin-top: 10px;
565+
line-height: 20px;
566+
background-color: white;
567+
}
568+
557569
@media only screen and (max-width: 1048px) and (min-width: 992px) {
558570
.warning {
559571
justify-content: start;

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
<label>
175175
<div class="et-form">
176176
<input autofocus id="github-url" class="et-btn-input" placeholder="Enter your github repository url..." type="text">
177+
<div id="invalid-message"></div>
177178
<a id="generate" class="et-btn et-btn-submit cp" onclick="generateFromGithub()">Submit</a>
178179
</div>
179180
</label>

js/script.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var imageStyles = {
1111
lineHeight: 22,
1212
watermarkEnabled: true
1313
};
14-
14+
var repoRegexp = new RegExp('((http(s){0,1})|(git@[\\w\\.]{1,}))(:(//){0,1})([\\w\\.@:/\\-~]{1,})(\\.git)(/){0,1}');
15+
var errorBlock = document.getElementById('invalid-message');
1516
var watermarkText = 'Generated by `foldertotree.cc`';
1617

1718
var body = document.getElementsByTagName('body')[0];
@@ -24,6 +25,15 @@ function openSelectFile() {
2425
document.getElementById('my-awesome-dropzone').click();
2526
}
2627

28+
function showErrorMessage(label) {
29+
errorBlock.style.display = 'block';
30+
errorBlock.innerHTML = label;
31+
}
32+
33+
function hideErrorMessage() {
34+
errorBlock.style.display = 'none';
35+
}
36+
2737
function initDownload() {
2838
loading.style.display = 'block';
2939
var canvas = document.getElementById('tree-canvas');
@@ -43,21 +53,27 @@ function retry() {
4353
}
4454

4555
function generateFromGithub() {
56+
var githubUrl = document.getElementById('github-url').value;
57+
if (!repoRegexp.test(githubUrl)) {
58+
this.showErrorMessage('Invalid url, please provide url in this format: `https://github.com/styopdev/folder-to-tree.git`');
59+
return;
60+
}
61+
4662
loading.style.display = 'block';
47-
var githubUrl = 'https://cors-anywhere.herokuapp.com/';
48-
githubUrl += document.getElementById('github-url').value;
49-
githubUrl += '/archive/master.zip';
63+
var repoUrl = 'https://cors-anywhere.herokuapp.com/';
64+
repoUrl += githubUrl.substr(0, githubUrl.length - 4);
65+
repoUrl += '/archive/master.zip';
5066
// TODO add githubUrl validation;
5167

52-
JSZipUtils.getBinaryContent(githubUrl, function(err, data) {
68+
JSZipUtils.getBinaryContent(repoUrl, function(err, data) {
5369
if (err) {
5470
throw err; // or handle err
5571
}
5672

5773
unzip
5874
.loadAsync(data)
59-
.then(function(data2) {
60-
processFile(data2);
75+
.then(function(zipData) {
76+
processFile(zipData);
6177
});
6278
});
6379
}
@@ -85,6 +101,7 @@ function toTree(fileNameList, files) {
85101
}
86102

87103
function processFile(zip) {
104+
hideErrorMessage();
88105
fileNames = Object.keys(zip.files);
89106
fileNamesTree = toTree(fileNames, zip.files);
90107

0 commit comments

Comments
 (0)