-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·241 lines (219 loc) · 7.6 KB
/
app.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
API_URL = "http://127.0.0.1:5000"
API_PRESET = "/preset"
API_PRESET_LIST = "/presetlist"
API_PRESET_IMG = "/presetimg"
PIXEL_GRID_HEIGHT = 16
PIXEL_GRID_LENGTH = 16
// Initialize the preset list for search filtering
presets = [];
// Grab the search filtering input
input = document.getElementById('filter-sidebar-input');
// Run when page loads
$(document).ready(function () {
// Generates grid for assigning colors to pixels
$("#pixel-grid-container").html(generatePixelGrid());
// Gets and displays list of presets from
getPresetList();
// Assign the search input to filter the preset list
// when something is typed inside.
input.addEventListener('keyup', filterUsers)
});
// Filters the preset list in the sidebar based on the search bar input in real-time
function filterUsers(event) {
keyword = input.value.toLowerCase();
presetsShown = presets.filter(function (preset) {
preset = preset.toLowerCase();
return preset.indexOf(keyword) > -1;
});
presetsHidden = _.reject(presets, function (preset) {
preset = preset.toLowerCase();
return preset.indexOf(keyword) > -1;
});
console.log(presetsHidden)
renderFilteredLists(presetsShown, presetsHidden);
}
// Show or hide presets given in list parameters
function renderFilteredLists(presetsShown, presetsHidden) {
presetsShown.forEach(function (i) {
$(`#preset-${i}`).css('visibility', 'visible');
$(`#preset-${i}`).css('content-visibility', 'visible');
});
presetsHidden.forEach(function (i) {
$(`#preset-${i}`).css('visibility', 'hidden');
$(`#preset-${i}`).css('content-visibility', 'hidden');
});
}
// Generates grid of pixels with dimensions: PIXEL_GRID_HEIGHT x PIXEL_GRID_LENGTH
function generatePixelGrid() {
htmlString = ``;
// Loops over and creates each row of pixels
for (let i = 0; i < PIXEL_GRID_HEIGHT; i++) {
htmlString += `<div class="row pixel-row" id="matrix-${i}-row">`;
if (i % 2 == 0) {
// Loops over and creates each pixel in each row
for (let j = PIXEL_GRID_LENGTH - 1; j >= 0; j--) {
htmlString +=
`
<div class="ratio ratio-1x1 col pixel-box pixel">
<input class="pixel-input" id="pix[${PIXEL_GRID_LENGTH * i + j}]" name="pix[${PIXEL_GRID_LENGTH * i + j}]" type="color" value="#000000">
</div>
`;
};
}
else {
// Loops over and creates each pixel in each row
for (let j = 0; j < PIXEL_GRID_LENGTH; j++) {
htmlString +=
`
<div class="ratio ratio-1x1 col pixel-box pixel">
<input class="pixel-input" id="pix[${PIXEL_GRID_LENGTH * i + j}]" name="pix[${PIXEL_GRID_LENGTH * i + j}]" type="color" value="#000000">
</div>
`;
};
}
htmlString +=
`
</div>
`;
};
return htmlString;
}
// Get's list of presets from API
function getPresetList() {
$.ajax({
type: "GET",
url: API_URL + API_PRESET_LIST,
data: JSON.stringify("None"),
dataType: "json",
contentType: 'application/json;charset=UTF-8',
// On a successful request, do the following.
success: function (response) {
loadPresetList(response);
},
// On a failed request, do the following.
error: function (xhr, resp, text) {
console.log(text)
}
});
}
// Insert presets from API into preset list sidebar
// Retrieves image from API
function loadPresetList(response) {
let dateTime = new Date();
let currDate = dateTime.getDate() + dateTime.getUTCMilliseconds() + dateTime.getTime();
htmlString = ``;
$.each(response, function (index, keyValPair) {
presets.push(keyValPair.presetName)
htmlString +=
`
<a class="preset-container list-group-item-action py-2 ripple" aria-current="true" id="preset-${keyValPair.presetName}" onmousedown="getPreset(this)">
<img src="${API_URL}${API_PRESET_IMG}?presetName=${keyValPair.presetName}&cacheNoMore=${currDate}" />
<p>${keyValPair.presetName}</p>
</a>
`;
});
$('#preset-list-container').html(htmlString);
}
// Get function called when a preset is selected and calls
// loadPreset() so the values of that preset can be attached
// to the pixel inputs.
// Sticks the name of the preset into the input field under the create/edit preset form.
function getPreset(ele) {
presetName = $(ele).children('p').text();
$('#preset-name-input').val(presetName)
$.ajax({
type: "GET",
url: `${API_URL}${API_PRESET}?presetName=${presetName}`,
// On a successful request, do the following.
success: function (response) {
loadPreset(response)
},
// On a failed request, do the following.
error: function (xhr, resp, text) {
console.log(text)
}
});
}
// Given a set of values, it'll change the pixel inputs to the
// specified values.
// The paramenter "data" is essentially a JSON string detailing
// the name of the input element intended to be changed and the
// value that it is intended to be changed to.
function loadPreset(response) {
$.each(response, function (index, keyValPair) {
$(`input[name="${keyValPair.name}"]`).val(keyValPair.value);
});
}
function createOrEditPreset() {
if ($('#preset-name-input').val() == "") {
alert("Please fill in a preset name.")
return;
}
data =
{
presetName: `${$('#preset-name-input').val()}`,
pixels: []
};
data.pixels = $('#pixel-form').serializeArray();
$.ajax({
type: "POST",
url: API_URL + API_PRESET,
data: JSON.stringify(data),
dataType: "json",
contentType: 'application/json;charset=UTF-8',
// On a successful request, do the following.
success: function (response) {
},
// On a failed request, do the following.
error: function (xhr, resp, text) {
console.log(text)
}
});
location.reload();
}
// Submits the HTTP Request of the pixels' colors to API
function submitPixelValues() {
// Sends the request and gets a response without refreshing the web page.
$.ajax({
type: "POST",
url: API_URL,
data: JSON.stringify($('#pixel-form').serializeArray()),
dataType: "json",
contentType: 'application/json;charset=UTF-8',
// On a successful request, do the following.
success: function (response) {
},
// On a failed request, do the following.
error: function (xhr, resp, text) {
console.log(text)
}
});
}
function uploadImageFunc(ele) {
if ($('#upload-name-input').val() == "" || $('#preset-image-upload-input').val() == '') {
alert("Please fill in a preset name & select a 16x16 PNG file.")
return;
}
let formData = new FormData($('#upload-image-form')[0])
$.ajax({
type: 'POST',
url: API_URL + API_PRESET_IMG,
data: formData,
contentType: false,
cache: false,
processData: false,
// On a successful request, do the following.
success: function (data) {
},
// On a failed request, do the following.
error: function (xhr, resp, text) {
console.log(text)
}
});
location.reload();
}
function clearPixelGrid() {
$("#pixel-form input").each(function (index) {
$(this).val("#000000");
});
}