File indexing completed on 2024-12-22 05:35:12

0001 import React from 'react';
0002 
0003 export function ConvertObjectToArray(object){
0004   let newArray = [];
0005   for (var i in object){
0006     newArray.push(object[i]);
0007   }
0008   return newArray;
0009 }
0010 
0011 export function GetSelectedCategory(categories,categoryId){
0012   let selectedCategory;
0013   categories.forEach(function(cat,catIndex){
0014     if (!selectedCategory){
0015       if (parseInt(cat.id) === categoryId){
0016         selectedCategory = cat;
0017         selectedCategory.categories = ConvertObjectToArray(selectedCategory.children);
0018       } else {
0019         if (cat.has_children === true){
0020           const catChildren = ConvertObjectToArray(cat.children);
0021 
0022           selectedCategory = GetSelectedCategory(catChildren,categoryId);
0023         }
0024       }
0025     }
0026   });
0027   return selectedCategory;
0028 }
0029 
0030 export function GenerateCurrentViewedCategories(categories,selectedCategory){
0031   if (selectedCategory.parent_id){
0032     const parentId = parseInt(selectedCategory.parent_id);
0033     let parentCategory = GetSelectedCategory(categories,parentId);
0034     if (parentCategory){
0035       parentCategory.categories = ConvertObjectToArray(parentCategory.children);
0036       parentCategory.categoryId = parentCategory.id;
0037       const selectedCategories = [parentCategory];
0038       return getCategoryParents(categories,selectedCategories);
0039     } else {
0040       return [];
0041     }
0042   } else {
0043     return [];
0044   }
0045 }
0046 
0047 function getCategoryParents(categories,selectedCategories){
0048   //if (selectedCategories[0].parent_id !== "34"){
0049     const parentId = parseInt(selectedCategories[0].parent_id);
0050     const parentCategory = GetSelectedCategory(categories,parentId);
0051     if (parentCategory){
0052       parentCategory.categories = ConvertObjectToArray(parentCategory.children);
0053       parentCategory.categoryId = parentCategory.id;
0054       selectedCategories = [parentCategory,...selectedCategories];
0055       return getCategoryParents(categories,selectedCategories);
0056     } else {
0057       return selectedCategories;
0058     }
0059   /*} else {
0060     return selectedCategories;
0061   }*/
0062 }
0063 
0064 export function GetCategoriesBySearchPhrase(categories,searchPhrase){
0065   let searchResults = [];
0066   categories.forEach(function(cat,index){
0067     if (cat.title.toLowerCase().indexOf(searchPhrase.toLowerCase()) > -1) searchResults.push(cat);
0068     if (cat.has_children){
0069       cat.categories = ConvertObjectToArray(cat.children);
0070       cat.categories.forEach(function(cat,index){
0071         if (cat.title.toLowerCase().indexOf(searchPhrase.toLowerCase()) > -1) searchResults.push(cat);
0072         if (cat.has_children){
0073           cat.categories = ConvertObjectToArray(cat.children);
0074           cat.categories.forEach(function(cat,index){
0075             if (cat.title.toLowerCase().indexOf(searchPhrase.toLowerCase()) > -1) searchResults.push(cat);
0076             if (cat.has_children){
0077               cat.categories = ConvertObjectToArray(cat.children);
0078               cat.categories.forEach(function(cat,index){
0079                 if (cat.title.toLowerCase().indexOf(searchPhrase.toLowerCase()) > -1) searchResults.push(cat);
0080                 if (cat.has_children){
0081                   cat.categories = ConvertObjectToArray(cat.children);
0082                   cat.categories.forEach(function(cat,index){
0083                     if (cat.title.toLowerCase().indexOf(searchPhrase.toLowerCase()) > -1) searchResults.push(cat);                   
0084                   });
0085                 }                
0086               });
0087             }            
0088           });
0089         }        
0090       });
0091     }
0092   })  
0093   return searchResults;
0094 }
0095 
0096 export function getCategoryType(selectedCategories,selectedCategoryId,categoryId){
0097   let categoryType;
0098   if (parseInt(categoryId) === selectedCategoryId){
0099     categoryType = "selected";
0100   } else {
0101     selectedCategories.forEach(function(selectedCat,index){
0102       if (selectedCat.id === categoryId){
0103         categoryType = "parent";
0104       }
0105     });
0106   }
0107   return categoryType;
0108 }
0109 
0110 export function generateCategoryLink(baseUrl,urlContext,catId,locationHref){
0111   if (window.baseUrl !== window.location.origin){
0112     baseUrl = window.location.origin;
0113   }
0114   let link = baseUrl + urlContext + "/browse/";
0115   if (catId !== "all"){
0116     link += "cat/" + catId + "/";
0117   }
0118   if (locationHref.indexOf('ord') > -1){
0119     link += "ord/" + locationHref.split('/ord/')[1];
0120   }
0121   return link;
0122 }
0123 
0124 export function sortArrayAlphabeticallyByTitle(a, b){
0125   let titleA, titleB;
0126   if (a.title){
0127     titleA = a.title.trim().toLowerCase();
0128     titleB = b.title.trim().toLowerCase();
0129   } else {
0130     titleA = a.name.trim().toLowerCase();
0131     titleB = b.name.trim().toLowerCase();
0132   }
0133   if(titleA < titleB) { return -1; }
0134   if(titleA > titleB) { return 1; }
0135   return 0;
0136 }
0137 
0138 export function getDeviceFromWidth(width){
0139   let device;
0140   if (width >= 910){
0141     device = "large";
0142   } else if (width < 910 && width >= 610){
0143     device = "mid";
0144   } else if (width < 610){
0145     device = "tablet";
0146   }
0147   return device;
0148 }
0149 
0150 export function getUrlContext(href){
0151   let urlContext = "";
0152   if (href.indexOf('/s/') > -1){
0153     urlContext = "/s/" + href.split('/s/')[1].split('/')[0];
0154   }
0155   return urlContext;
0156 }
0157 
0158 export function getAllCatItemCssClass(href,baseUrl,urlContext,categoryId){
0159   if (baseUrl !== window.location.origin){
0160     baseUrl = window.location.origin;
0161   }
0162   let allCatItemCssClass;
0163   if (categoryId && categoryId !== 0){
0164     allCatItemCssClass = "";
0165   } else {
0166     if (href === baseUrl + urlContext ||
0167         href === baseUrl + urlContext + "/browse/" || href === baseUrl + urlContext + "/browse/ord/latest/" || href === baseUrl + urlContext + "/browse/ord/top/" ||
0168         href === "https://store.kde.org" || href === "https://store.kde.org/browse/ord/latest/" ||  href === "https://store.kde.org/browse/ord/top/" ||
0169         href === "https://addons.videolan.org" || href === "https://addons.videolan.org/browse/ord/latest/" ||  href === "https://addons.videolan.org/browse/ord/top/" ||
0170         href === "https://share.krita.org/" || href === "https://share.krita.org/browse/ord/latest/" ||  href === "https://share.krita.org/browse/ord/top/"){
0171           allCatItemCssClass = "active";
0172     }
0173   }
0174   return allCatItemCssClass;
0175 }