-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSfunctions.js
executable file
·169 lines (140 loc) · 4.23 KB
/
JSfunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ---------------- page elements: --------------
var addNewItemButton = document.getElementById("add_item");
var items = document.getElementsByClassName("item");
var editItemButtons = document.getElementsByClassName("edit_item");
var deleteItemButtons = document.getElementsByClassName("delete_item");
var closePopup = document.getElementById("close");
var saveButton = document.getElementById("save");
var deleteButton = document.getElementById("delete");
var editButton = document.getElementById("edit");
var mainpage = document.getElementById("content");
// ------------------ on click events: ------------
window.onclick = e => {
var el = e.target;
var className = el.className;
var id = el.id;
if (id == "popup-content-wrapper"
|| (id == "popup" && className == "popup")
|| (id == "" && className == "")) {
if (popupIsOpen()) {
popupClose();
}
}
if (className.search("item_action") != -1) {
var item_id = el.parentNode.parentNode.getElementsByClassName("item_id")[0].getAttribute("value");
if (className == "item_action_edit") {
clickedEditExistingItem(item_id)
}
if (className == "item_action_delete") {
clickedDeleteExistingItem(item_id);
}
}
}
addNewItemButton.onclick = clickedAddNewItem;
editButton.onclick = enableInputs;
saveButton.onclick = clickedSaveItem;
deleteButton.onclick = clickedDeleteItem;
function clickedAddNewItem() { //adding a new item
loadPopup(null);
popupOpen();
}
function clickedEditExistingItem(item_id) {
loadPopup(item_id);
popupOpen();
}
function clickedDeleteExistingItem(item_id) {
deleteItem(item_id);
loadTable();
popupClose();
}
function clickedDeleteItem() {
var item_id = this.parentNode.parentNode.parentNode.getElementsByClassName("item_id")[0].getAttribute("value");
console.log(item_id);
if (item_id != 'null') {
deleteItem(item_id);
popupClose();
loadTable();
} else {
alert("Can't delete a non-existing item");
}
}
function clickedSaveItem() {
var item_id = this.parentNode.parentNode.parentNode.getElementsByClassName("item_id")[0].getAttribute("value");
var things = this.parentNode.parentNode.parentNode.getElementsByClassName("changeable");
var thing1 = things[0].value;
var thing2 = things[1].value;
var thing3 = things[2].value;
if (thing1 == "" || thing2 == "" || thing3 == "") {
alert("You can't save the item with empty fields!");
} else {
var inputs = [thing1, thing2, thing3];
saveItem(item_id, inputs);
loadTable();
popupClose();
}
}
//------------------- functions: -----------------
function popupIsOpen() {
var attr = document.getElementById("popup").style.display;
if (attr == "block") {
return true;
} else {
return false;
}
}
function enableInputs() {
var inputs = document.getElementsByClassName("changeable");
for (i=0; i < inputs.length; i++) {
inputs[i].removeAttribute("disabled");
}
}
function popupOpen() {
var popup = document.getElementById("popup");
$("#container").attr("style", "opacity: 0.5");
popup.style.display = "block";
}
function popupClose() {
$("#container").attr("style", "opacity: 1");
popup.style.display = "none";
}
function saveItem(item_id, inputArray) {
sendGETXhttpRequest(
"saveitem.php?item_id=" + item_id +
"&thing1=" + inputArray[0] +
"&thing2=" + inputArray[1] +
"&thing3=" + inputArray[2]
, null);
}
function deleteItem(item_id) {
sendGETXhttpRequest("deleteitem.php?item_id=" + item_id, null);
}
function loadPopup(item_id) {
sendGETXhttpRequest("createpopup.php?item_id=" + item_id, "#values");
}
function loadTable() { //vaadata äkki saab ajaxi peale ümber teha
sendGETXhttpRequest("createtable.php", "#added_items");
}
function sendGETXhttpRequest(url, IdOfelementToBeChanged) {
//ajax xml was working but i wanted to try ajax jquery as well
/*var xhttp = new XMLHttpRequest();
$('document').ready(function(){
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (IdOfelementToBeChanged != null) {
document.getElementById(IdOfelementToBeChanged).innerHTML = this.responseText;
}
}
};
});
xhttp.open("GET", url, true);
xhttp.send();*/
$.ajax({
type: "GET",
url: url,
success: function(result) {
if (IdOfelementToBeChanged != null) {
$(IdOfelementToBeChanged).html(result);
}
}
});
}