Browse Source

Use const when possible

master
KaKi87 5 months ago
parent
commit
06316f09e3
2 changed files with 36 additions and 33 deletions
  1. 34
    31
      public/assets/js/index.js
  2. 2
    2
      public/assets/js/main.js

+ 34
- 31
public/assets/js/index.js View File

@@ -3,7 +3,7 @@ Search
*/
// Android versions
let androidVersions = [
const androidVersions = [
'1.0', '1.1', '1.5', '1.6',
'2.0', '2.0.1', '2.1', '2.2', '2.2.1', '2.2.2', '2.2.3', '2.3', '2.3.1', '2.3.2', '2.3.3', '2.3.4', '2.3.5', '2.3.6', '2.3.7',
'3.0', '3.1', '3.2', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5', '3.2.6',
@@ -23,14 +23,14 @@ androidVersions.forEach(v => document.querySelector('#minAndroidVersion').innerH
* @param callback
*/
let lastQuery = '';
let search = (query, callback) => {
const search = (query, callback) => {
if(query.name === lastQuery)
query.number = 120;
socket.emit('search', query);
socket.on('res', res => {
let r = [];
const r = [];
for(let i = 0; i < res.length; i++){
let _c = [];
const _c = [];
for(let j = 0; j < query.conditions.length; j++){
_c.push(query.conditions[j](res[i]));
}
@@ -63,7 +63,7 @@ let search = (query, callback) => {
/*
Conditions checking functions
*/
let _c = {
const _c = {
maxPrice: (app, n) => app.price <= n,
adFree: app => !app.adSupported,
inAppFree: app => !app.offersIAP,
@@ -77,10 +77,10 @@ let _c = {
// Global search settings
let Query_Conditions, Query_Price, Query_Number;
// Default search text display (for reset)
let Query_Text = [...document.querySelectorAll('.search__settings span')].map(el => el.textContent);
let Query_Set = [...document.querySelectorAll('.search__setting')].map(el => el.classList.contains('search__setting--set'));
const Query_Text = [...document.querySelectorAll('.search__settings span')].map(el => el.textContent);
const Query_Set = [...document.querySelectorAll('.search__setting')].map(el => el.classList.contains('search__setting--set'));
let resetSettings = () => {
const resetSettings = () => {
// Reset search settings
Query_Conditions = [_c.adFree, _c.inAppFree];
Query_Price = 'free';
@@ -104,19 +104,19 @@ resetSettings();
document.querySelector('.resetSettings').onclick = resetSettings;
// Reset arrow sort symbols in results table
let resetSorting = () => document.querySelectorAll('.output__header th').forEach(el => el.classList.remove('output__header--sortUp', 'output__header--sortDown'));
const resetSorting = () => document.querySelectorAll('.output__header th').forEach(el => el.classList.remove('output__header--sortUp', 'output__header--sortDown'));
let searchInput = document.querySelector('.search__input');
let searchButton = document.querySelector('.search__submit');
const searchInput = document.querySelector('.search__input');
const searchButton = document.querySelector('.search__submit');
searchButton.onclick = () => {
// Empty query won't work
if(!searchInput.value) return;
// Time search
let startTime = Date.now();
let outputMessage = document.querySelector('.output__message');
const startTime = Date.now();
const outputMessage = document.querySelector('.output__message');
// Query settings
let query = { name: searchInput.value, number: Query_Number, price: Query_Price, conditions: Query_Conditions };
const query = { name: searchInput.value, number: Query_Number, price: Query_Price, conditions: Query_Conditions };
// Let user know that I'm searching
outputMessage.innerHTML = '<div class="spinner" data-uk-spinner="ratio: 0.75"></div></div>Searching<span class="loading-dots"></span>';
// Reset and hide previous search elements
@@ -131,18 +131,18 @@ searchButton.onclick = () => {
return outputMessage.textContent = 'No result.';
}
// Calculate elapsed time
let elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2);
/*
Show results
*/
// Message
outputMessage.textContent = `Results : ${res.length} items (${(res.length / query.number * 100).toFixed(2)}% of ${query.number} apps), elapsed time : ${elapsedTime}s`;
let displayedPermissions = [];
const displayedPermissions = [];
res.forEach(app => {
// Show all permissions
app.permissions.forEach(permission => {
let p = permission.permission;
let d = permission.description;
const p = permission.permission;
const d = permission.description;
if(displayedPermissions.indexOf(p) === -1){
displayedPermissions.push(p);
document.querySelector('.output2__data').innerHTML += `<tr class="output2__permission"><th class="output2__permission__name" data-uk-tooltip="title: ${d}; pos: right">${p}</th></tr>`;
@@ -183,16 +183,16 @@ searchInput.onkeydown = e => {
searchInput.focus();
let inputs = document.querySelectorAll('.search__settings input, .search__settings select');
const inputs = document.querySelectorAll('.search__settings input, .search__settings select');
inputs.forEach(el => el.oninput = () => {
/*
Set query conditions from user input
*/
let filter = el.id;
const filter = el.id;
Query_Conditions = [];
inputs.forEach(el => {
if([el.min, 'on', ''].indexOf(el.value) === -1 || el.checked){
let filter = el.id;
const filter = el.id;
switch(filter){
case 'minAndroidVersion':
Query_Conditions.push(app => _c[filter](app, el.value));
@@ -208,7 +208,7 @@ inputs.forEach(el => el.oninput = () => {
Query_Conditions.push(_c[filter]);
break;
case 'minInstalls':
let range = [0, 1, 1000, 10000, 100000, 1000000, 10000000];
const range = [0, 1, 1000, 10000, 100000, 1000000, 10000000];
Query_Conditions.push(app => _c[filter](app, range[parseInt(el.value)]));
break;
}
@@ -223,10 +223,11 @@ inputs.forEach(el => el.oninput = () => {
*/
Query_Number = Math.floor(Query_Conditions.length * 12.5) || 25;
if(el.type === 'range'){
let text = document.querySelector(`.${filter}-text`);
let suffix = '';
let minText = 'min';
let maxText = 'max';
const text = document.querySelector(`.${filter}-text`);
let
suffix = '',
minText = 'min',
maxText = 'max';
if(filter === 'maxSize') suffix = 'M';
if(filter === 'maxPrice'){
minText = 'free';
@@ -246,7 +247,7 @@ inputs.forEach(el => el.oninput = () => {
text.textContent = maxText;
else {
if(filter === 'minInstalls'){
let range = ['0', '1', '1k', '10k', '100k', '1M', '10M'];
const range = ['0', '1', '1k', '10k', '100k', '1M', '10M'];
return text.textContent = range[parseInt(el.value)];
}
text.textContent = `${el.value}${suffix}`;
@@ -263,9 +264,11 @@ document.querySelectorAll('.output__header th').forEach(el => el.onclick = () =>
if(['output__header__description', 'output__header__version'].indexOf(el.className) !== -1) return;
// Reset and re-sort
resetSorting();
let items = [...document.querySelectorAll('.output__item')];
let sortedItems = items.slice(0);
let getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).textContent;
const
items = [...document.querySelectorAll('.output__item')];
let
sortedItems = items.slice(0),
getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).textContent;
if(el.className === 'output__header__last-update'){
// Use UNIX timestamp instead of interpreted date string
getItem = item => item.querySelector(`.output__data__${el.className.split('__').slice(-1)[0]}`).getAttribute('data-date');
@@ -311,7 +314,7 @@ document.querySelectorAll('.output__header th').forEach(el => el.onclick = () =>
// Hide all items
items.forEach(item => item.parentNode.removeChild(item));
// Unhide all items in proper sort order
for(let item of sortedItems){
for(const item of sortedItems){
document.querySelector('.output__data').appendChild(item);
}
});

+ 2
- 2
public/assets/js/main.js View File

@@ -8,7 +8,7 @@ setInterval(() => document.querySelectorAll('.loading-dots').forEach(el => el.te
/*
Dark mode
*/
let toggleDark = () => {
const toggleDark = () => {
// Toggle UIkit properties
['uk-light', 'uk-background-secondary'].forEach(Class => [document.body, document.querySelector('.uk-modal-body')].forEach(el => el.classList.toggle(Class)));
// Toggle dark button text
@@ -29,4 +29,4 @@ document.querySelector('.dark').onclick = () => {
};
// Restore saved dark style
if(Cookies.get('dark') === 'true') toggleDark();
if(Cookies.get('dark') === 'true') toggleDark();

Loading…
Cancel
Save