Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# project-front

<a href="https://github.com/LeeTovetz/JRU-JavaProfessional" >**Mini-project 03 on the topic: HTML, CSS, JS, jQuery by JavaRush University**</a><br>

Task: We need to make a UI for CRUD backend, using HTML, CSS, JS, jQuery.<br>
We will make an admin panel to manage online game accounts.<br>

To do this:<br>
- Make a fork from the repository;
- Download your version of the project to your computer;
- Download and install Tomcat;
- Add HTML, CSS, JS, jQuery;
- Pour the changes into your GitHub repository and send the link to it to your teacher. <br>
16 changes: 16 additions & 0 deletions src/main/webapp/css/my.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
body {
font-family: Arial, Verdana, sans-serif;
font-size: 14px;
background-color: #F5FFFA;
color: #708090;
}
h1 {
color: #CD5C5C;
font-size: 24px;
font-family: Georgia, Times, serif;
font-weight: normal;
}
th, td {
border: 1px solid black;
padding: 2px;
}
312 changes: 311 additions & 1 deletion src/main/webapp/html/my.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,318 @@
<script src=https://code.jquery.com/jquery-3.6.0.min.js></script>
<link href="/css/my.css" rel="stylesheet">
</head>
<body>
<body onload="printList(0)">
<h1>RPG admin panel</h1>

<label for="count_1">Count per page:</label>
<select id = "count_1" onchange="printList(0)">
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>

<table id="main_table" >
<tr>
<th>#</th>
<th>Name</th>
<th>Title</th>
<th>Race</th>
<th>Profession</th>
<th>Level</th>
<th>Birthday</th>
<th>Banned</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</table>

<div id = "paging">Pages:</div>

<hr>
<h2>Create new account:</h2>
<label for="input_name_new">Name:</label>
<input type="text" id = "input_name_new" name="name" required size = "12" maxlength="12">
<br><br>

<label for="input_title_new">Title:</label>
<input type="text" id = "input_title_new" name="title" required size = "30" maxlength="30">
<br><br>

<label for="input_race_new">Race:</label>
<select id = "input_race_new" name='race'>
<option value='HUMAN'>HUMAN</option>
<option value='DWARF'>DWARF</option>
<option value='ELF'>ELF</option>
<option value='GIANT'>GIANT</option>
<option value='ORC'>ORC</option>
<option value='TROLL'>TROLL</option>
<option value='HOBBIT'>HOBBIT</option>
</select>
<br><br>

<label for="input_profession_new">Profession:</label>
<select id = "input_profession_new" name='profession'>
<option value='WARRIOR'>WARRIOR</option>
<option value='ROGUE'>ROGUE</option>
<option value='SORCERER'>SORCERER</option>
<option value='CLERIC'>CLERIC</option>
<option value='PALADIN'>PALADIN</option>
<option value='NAZGUL'>NAZGUL</option>
<option value='DRUID'>DRUID</option>
</select>
<br><br>

<label for="input_level_new">Level:</label>
<input type="number" id = "input_level_new" name="level" min = "0" max="100">
<br><br>

<label for="input_birthday_new">Birthday:</label>
<input type="date" id = "input_birthday_new" name="birthday" min = "2000-01-01" max="3000-12-31">
<br><br>

<label for="input_banned_new">Banned:</label>
<select id = "input_banned_new" name='banned'>
<option value='false'>false</option>
<option value='true'>true</option>
</select>
<br><br>

<span>
<button type="submit" onclick="createAcc()">Save</button>
</span>

<script>
function printList(page_number) {
$("tr:has(td)").remove();

let url = "/rest/players?";

let countPerPage = $("#count_1").val();

if (countPerPage == null) {
countPerPage = 3;
}
url = url.concat("pageSize=").concat(countPerPage);

if (page_number == null) {
page_number = 0;
}
url = url.concat("&pageNumber=").concat(page_number);

$.get(url, function (response) {
$.each(response, function (i, item) {
$('<tr>').html("<td>"
+ item.id + "</td><td>"
+ item.name + "</td><td>"
+ item.title + "</td><td>"
+ item.race + "</td><td>"
+ item.profession + "</td><td>"
+ item.level + "</td><td>"
+ new Date(item.birthday).toLocaleDateString() + "</td><td>"
+ item.banned + "</td><td>"
+ "<button id='button_edit_" + item.id + "' onclick = 'editAcc(" + item.id + ")'>"
+ "<img src='/img/edit.png'>"
+ "</td>" + "</td><td>"
+ "<button id='button_delete_" + item.id +"' onclick = 'deleteAcc(" + item.id + ")'>"
+ "<img src='/img/delete.png'>"
+ "</button>" + "</td>"
).appendTo("#main_table");
});
});
let totalCount = getCount();

let pagesCount = Math.ceil(totalCount / countPerPage);

$("button.pgn-btn-styled").remove();

for (let i = 0; i < pagesCount; i++) {
let button_tag = "<button>" + (i + 1) + "</button>"
let btn = $(button_tag)
.attr('id', "paging_button_" + i)
.attr('onclick', "printList(" + i +")")
.addClass('pgn-btn-styled');
$('#paging').append(btn);
}
let identifier = "#paging_button_" + page_number;
$(identifier).css('color', "red")
}

function getCount() {
let url = "/rest/players/count";
let count = 0;
$.ajax({
url: url,
async: false,
success: function (result) {
count = parseInt(result);
}
})
return count;
}

function deleteAcc(id) {
let url = "/rest/players/" + id;
$.ajax({
url: url,
type: "DELETE",
success: function () {
printList(getCurrentPage());
}
})
}

function editAcc(id) {
let identifier_edit = "#button_edit_" + id;
let identifier_delete = "#button_delete_" + id;
$(identifier_delete).remove();

let save_image_tag = "<img src='/img/save.png'>";
$(identifier_edit).html(save_image_tag);

let current_tr_element = $(identifier_edit).parent().parent();
let children = current_tr_element.children();

let td_name = children[1];
td_name.innerHTML = "<input id = 'input_name_" + id + "' type = 'text' value = '" + td_name.innerHTML + "'>";

let td_title = children[2];
td_title.innerHTML = "<input id = 'input_title_" + id + "' type = 'text' value = '" + td_title.innerHTML + "'>";

let td_race = children[3];
let race_id = "#select_race_" + id;
let race_current_value = td_race.innerHTML;
td_race.innerHTML = getDropdownRaceHtml(id);
$(race_id).val(race_current_value).change();

let td_profession = children[4];
let profession_id = "#select_profession_" + id;
let profession_current_value = td_profession.innerHTML;
td_profession.innerHTML = getDropdownProfessionHtml(id);
$(profession_id).val(profession_current_value).change();

let td_banned = children[7];
let banned_id = "#select_banned_" + id;
let banned_current_value = td_banned.innerHTML;
td_banned.innerHTML = getDropdownBannedHtml(id);
$(banned_id).val(banned_current_value).change();

let property_save_tag = "saveAcc(" + id + ")";
$(identifier_edit).attr('onclick', property_save_tag);
}

function saveAcc(id) {
let value_name = $("#input_name_" + id).val();
let value_title = $("#input_title_" + id).val();
let value_race = $("#select_race_" + id).val();
let value_profession = $("#select_profession_" + id).val();
let value_banned = $("#select_banned_" + id).val();

let url = "/rest/players/" + id;
$.ajax({
url:url,
type: 'POST',
dataType: 'json',
contentType : 'application/json;charset=UTF-8',
async: false,
data: JSON.stringify({
"name": value_name,
"title": value_title,
"race": value_race,
"profession": value_profession,
"banned": value_banned
}),
success: function () {
printList(getCurrentPage());
}
});
}

function getDropdownRaceHtml(id) {
let race_id = "select_race_" + id;
return "<label for = 'race'></label>"
+ "<select id =" + race_id + " name='race'>"
+ "<option value='HUMAN'>HUMAN</option>"
+ "<option value='DWARF'>DWARF</option>"
+ "<option value='ELF'>ELF</option>"
+ "<option value='GIANT'>GIANT</option>"
+ "<option value='ORC'>ORC</option>"
+ "<option value='TROLL'>TROLL</option>"
+ "<option value='HOBBIT'>HOBBIT</option>"
+ "</select>";
}

function getDropdownProfessionHtml(id) {
let profession_id = "select_profession_" + id;
return "<label for = 'profession'></label>"
+ "<select id =" + profession_id + " name='race'>"
+ "<option value='WARRIOR'>WARRIOR</option>"
+ "<option value='ROGUE'>ROGUE</option>"
+ "<option value='SORCERER'>SORCERER</option>"
+ "<option value='CLERIC'>CLERIC</option>"
+ "<option value='PALADIN'>PALADIN</option>"
+ "<option value='NAZGUL'>NAZGUL</option>"
+ "<option value='DRUID'>DRUID</option>"
+ "</select>";
}

function getDropdownBannedHtml(id) {
let banned_id = "select_banned_" + id;
return "<label for = 'banned'></label>"
+ "<select id =" + banned_id + " name='race'>"
+ "<option value='true'>true</option>"
+ "<option value='false'>false</option>"
+ "</select>";
}

function getCurrentPage() {
let current_page = 0;
$('button:parent(div)').each(function () {
if ($(this).css('color') === 'rgb(255, 0, 9)') {
current_page = $(this).text();
}
});
return parseInt(current_page);
}

function createAcc() {
let value_name = $("#input_name_new").val();
let value_title = $("#input_title_new").val();
let value_race = $("#input_race_new").val();
let value_profession = $("#input_profession_new").val();
let value_level = $("#input_level_new").val();
let value_birthday = $("#input_birthday_new").val();
let value_banned = $("#input_banned_new").val();

let url = "/rest/players"
$.ajax({
url:url,
type: 'POST',
dataType: 'json',
contentType : 'application/json;charset=UTF-8',
async: false,
data: JSON.stringify({
"name": value_name,
"title": value_title,
"race": value_race,
"profession": value_profession,
"level": value_level,
"birthday": new Date(value_birthday).getTime(),
"banned": value_banned
}),
success: function () {
$("#input_name_new").val("");
$("#input_title_new").val("");
$("#select_race_new").val("");
$("#select_profession_new").val("");
$("#select_level_new").val("");
$("#select_birthday_new").val("");
$("#select_banned_new").val("");
printList(getCurrentPage());
}
})
}
</script>

</body>
</html>