Skip to content

Commit 353ff3e

Browse files
committed
Refractored javascript code to pure js and removed jquery library
1 parent e25146f commit 353ff3e

File tree

3 files changed

+58
-84
lines changed

3 files changed

+58
-84
lines changed

code_share/app_code_share/templates/app_code_share/homepage.html

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
<meta http-equiv="X-UA-Compatible" content="IE=edge">
1010
<meta name="viewport" content="width=device-width, initial-scale=1">
1111
<title>Shitty Code Share</title>
12-
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
12+
<link rel="stylelesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
1313
<link rel="stylesheet" href="{% static 'css/normalize.css' %}">
1414
<link rel="stylesheet" href="{% static 'css/milligram.css' %}">
15+
<script type="text/javascript" crossorigin="anonymous" src="{% static 'js/submit_button_handler.js' %}"></script>
16+
<!-- <script src="https://code.jquery.com/jquery-3.2.1.min.js"
17+
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
18+
crossorigin="anonymous"></script> -->
1519

1620
</head>
1721
<body>
1822
<a href="{% url 'code_share:app_home'%}" style="color:inherit;">
19-
<h3 style="padding-top:10px; padding-bottom:20px; padding-left:10px">Shitty Code Share</h3>
23+
<h3 style="padding-top:10px; padding-bottom:20px; padding-left:10px;">Shitty Code Share</h3>
2024
</a>
2125

2226
<div class="container">
@@ -41,7 +45,7 @@ <h3 style="padding-top:10px; padding-bottom:20px; padding-left:10px">Shitty Code
4145

4246
{%endif%}
4347

44-
<textarea id="code_snippet" name="code_snippet">{{code_share.code}}</textarea><br>
48+
<textarea id="code_snippet" name="code_snippet" onkeyup="control_submit_button()">{{code_share.code}}</textarea><br>
4549
<input id="submit_edit" type="submit" value="edit">
4650
<br>
4751
<a href="{% url 'code_share:app_home'%}" >
@@ -51,8 +55,8 @@ <h3 style="padding-top:10px; padding-bottom:20px; padding-left:10px">Shitty Code
5155
{%else%}
5256
<input type="text" id="file_field" name="file_name" placeholder="File Name" value="{{code_share.file_name}}">
5357

54-
<textarea id="code_snippet" name="code_snippet" placeholder="Code Snippet">{{code_share.code}}</textarea><br>
55-
<input id="submit_new" type="submit" value="submit" onmouseover="check_content(this)"><br>
58+
<textarea id="code_snippet" name="code_snippet" placeholder="Code Snippet" onkeyup="control_submit_button()">{{code_share.code}}</textarea><br>
59+
<input id="submit_new" type="submit" value="submit" ><br>
5660

5761
{%endif%}
5862

@@ -68,61 +72,4 @@ <h3 style="padding-top:10px; padding-bottom:20px; padding-left:10px">Shitty Code
6872

6973
</body>
7074

71-
<script
72-
src="https://code.jquery.com/jquery-3.2.1.min.js"
73-
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
74-
crossorigin="anonymous"></script>
75-
76-
<script>
77-
78-
79-
// disables the submit button upon document ready
80-
window.onload = function() {
81-
82-
if (document.getElementById("submit_new")){
83-
document.getElementById("submit_new").disabled = true;
84-
}
85-
86-
if (document.getElementById("submit_edit")){
87-
document.getElementById("submit_edit").disabled = true;
88-
}
89-
90-
};
91-
92-
93-
/*enables the submit button on checking if there is
94-
content in the code_snippet textarea.
95-
96-
EventListener on Keyup on the textarea(code_snippet) calls this method.
97-
Content in the textarea is checked and according to that
98-
submit buttons are disabled/enabled.
99-
*/
100-
code_snippet.addEventListener("keyup", (event) => {
101-
102-
if (document.getElementById("code_snippet").value != ""){
103-
104-
if (document.getElementById("submit_new")){
105-
document.getElementById("submit_new").disabled = false;
106-
}
107-
if (document.getElementById("submit_edit")){
108-
document.getElementById("submit_edit").disabled = false;
109-
}
110-
111-
}
112-
113-
else {
114-
115-
if (document.getElementById("submit_new")){
116-
document.getElementById("submit_new").disabled = true;
117-
}
118-
119-
if (document.getElementById("submit_edit")){
120-
document.getElementById("submit_edit").disabled = true;
121-
}
122-
}
123-
124-
});
125-
126-
</script>
127-
128-
</html>
75+
</html>

code_share/app_code_share/views.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def home(request):
3838
if CodeShare.objects.filter(file_name=file_name).exists() == True and file_name != '':
3939

4040
messages.error(
41-
request, 'Awww!! An error. Probably we might have a file with same name. Damn those folks.')
41+
request, 'An error occured')
4242
return render(request, 'app_code_share/homepage.html', {})
4343

4444
CodeShare.objects.create(code=code_share,
@@ -77,23 +77,4 @@ def view_by_hash(request, hash_id):
7777
code_obj.code = code_share
7878
code_obj.save()
7979

80-
return redirect('code_share:view_by_hash', hash_id=hash_id)
81-
82-
83-
# Removing this as it's not needed
84-
#
85-
# def view_by_file(request, file_name):
86-
# """
87-
# """
88-
89-
# if request.method == 'GET':
90-
# code_share = CodeShare.objects.get(file_name=file_name)
91-
# return render(request, 'app_code_share/homepage.html', {'code_share':
92-
# code_share, "filename": "yes"})
93-
94-
# if request.method == 'POST':
95-
# code_share = request.POST.get('code_snippet')
96-
# code_obj = goo404(CodeShare, file_name=file_name)
97-
# code_obj.code = code_share
98-
# code_obj.save()
99-
# return redirect('code_share:view_by_file', file_name=file_name)
80+
return redirect('code_share:view_by_hash', hash_id=hash_id)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
// disables the submit button upon document ready
3+
window.onload = function() {
4+
5+
if (document.getElementById("submit_new")){
6+
document.getElementById("submit_new").disabled = true;
7+
}
8+
9+
if (document.getElementById("submit_edit")){
10+
document.getElementById("submit_edit").disabled = true;
11+
}
12+
13+
};
14+
15+
16+
/*
17+
EventListener on Keyup on the textarea(code_snippet) calls this method.
18+
Content in the textarea is checked and according to that
19+
submit buttons are disabled/enabled.
20+
*/
21+
22+
function control_submit_button(){
23+
24+
if (document.getElementById("code_snippet").value != ""){
25+
26+
if (document.getElementById("submit_new")){
27+
document.getElementById("submit_new").disabled = false;
28+
}
29+
if (document.getElementById("submit_edit")){
30+
document.getElementById("submit_edit").disabled = false;
31+
}
32+
33+
}
34+
35+
else {
36+
37+
if (document.getElementById("submit_new")){
38+
document.getElementById("submit_new").disabled = true;
39+
}
40+
41+
if (document.getElementById("submit_edit")){
42+
document.getElementById("submit_edit").disabled = true;
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)