Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

searchfunctions.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //Basic shit to understand
  2. //all_artesanos is the json that has the file that contains all the artesanos, this includes the studd they do,municipio,telefono,email and name
  3. //x is going to be what is displayed in the app, it basically has the same infromation as all_artesanos
  4. //THe basic mechanic is to display each artist in the search tab whenever something of them is found
  5. //You need at least 3 characters in one of the searchbars for it to display something
  6. //If nothing is written then it will display every artists
  7. //Some of these functions also try to make sure that the inputs are the same format as the thing they're looking up so that they dont have to follow a specific syntax to look for stuff
  8. //Tries to filter by the type of work that the artists do, if what they wrote in inputarte is included in one of them then the artists is displayed in the search bar
  9. function filter_artesanias(inputarte){
  10. var x = document.getElementsByClassName('artists');
  11. for (var i = 0; i < all_artesanos.length; i++){
  12. var especificaciones = all_artesanos[i].Especificacion;
  13. especificaciones = especificaciones.trim().toLowerCase();
  14. especificaciones = acento_replace(especificaciones);
  15. if (especificaciones.search(inputarte) != -1){
  16. x[i].style.display = "list-item";
  17. }
  18. //before it did this down here but im keeping it just in case...
  19. //especificaciones = especificaciones.split(',');
  20. //for (var j = 0; j < especificaciones.length; j++){
  21. //var especificacion = especificaciones[j];
  22. //especificacion = especificacion.trim().toLowerCase();
  23. //especificacion = acento_replace(especificacion);
  24. //if (especificacion.search(inputarte) != -1){
  25. //x[i].style.display = "list-item";
  26. //break;
  27. //}
  28. }
  29. return;
  30. }
  31. //Tries to filter by the municipio that they reside in or work in (not sure which one of the two it is)
  32. function filter_municipios(inputmuni){
  33. var x = document.getElementsByClassName('artists');
  34. for (var i = 0; i < all_artesanos.length; i++){
  35. var municipio = all_artesanos[i].Municipio;
  36. municipio = municipio.trim().toLowerCase();
  37. municipio = acento_replace(municipio);
  38. if (municipio.search(inputmuni)!= -1){
  39. x[i].style.display = "list-item";
  40. }
  41. }
  42. return;
  43. }
  44. //Tries to filter by any kind of information provided (name,artesania,municipio,telefono,email)
  45. function filter_any(input){
  46. var x = document.getElementsByClassName('artists');
  47. for (var i = 0; i < all_artesanos.length; i++){
  48. if (find_name(i,input)){
  49. x[i].style.display = "list-item";
  50. continue;
  51. }
  52. else if (find_artesania(i,input)){
  53. x[i].style.display = "list-item";
  54. continue;
  55. }
  56. else if (find_municipio(i,input)){
  57. x[i].style.display = "list-item";
  58. continue;
  59. }
  60. else if (find_telefono(i,input)){
  61. x[i].style.display = "list-item";
  62. continue;
  63. }
  64. else if (find_email(i,input)){
  65. x[i].style.display = "list-item";
  66. continue;
  67. }
  68. }
  69. return;
  70. }
  71. //Te trata de buscar si cualquiera de ellos tienen el input que se dio aplica a ese artista especifico (i)
  72. function find_any(i,input){
  73. //if you find any of these in that space then return 1 if you didnt find that anything matched with the input then return 0
  74. if (find_name(i,input) || find_telefono(i,input) || find_email(i,input) || find_artesania(i,input) || find_municipio(i,input)){
  75. return 1;
  76. }
  77. else{
  78. return 0;
  79. }
  80. }
  81. //Trata de ver si el input es parte del nombre del artista i
  82. //Mayuscula y acento proof
  83. function find_name(i,input){
  84. var name = all_artesanos[i].Nombre;
  85. name = name.trim().toLowerCase();
  86. name = acento_replace(name);
  87. if(name.search(input) != -1){
  88. return 1;
  89. }
  90. return 0;
  91. }
  92. //trata de ver si el input es parte de alguna artesania que trabaja el artista i
  93. //Mayuscula y acento proof
  94. function find_artesania(i,input){
  95. var artesanias = all_artesanos[i].Especificacion;
  96. artesanias = artesanias.trim().toLowerCase();
  97. artesanias = acento_replace(artesanias);
  98. if(artesanias.search(input) != -1){
  99. return 1;
  100. }
  101. //artesanias = artesanias.split(',');
  102. //for (var j = 0; j < artesanias.length; j++){
  103. //var artesania = artesanias[j];
  104. //artesania = artesania.trim().toLowerCase();
  105. //artesania = acento_replace(artesania);
  106. //if (artesania.search(input) != -1){
  107. //return 1;
  108. //}
  109. //}
  110. return 0;
  111. }
  112. //trata de ver si el input es parte del municipio del artista i
  113. //Mayuscula y acento proof
  114. function find_municipio(i,input){
  115. var mun = all_artesanos[i].Municipio;
  116. mun = mun.trim().toLowerCase();
  117. mun = acento_replace(mun);
  118. if(mun.search(input) != -1){
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. //trata de ver si el input es parte del telefono del artista i
  124. //'(',')','-' and ' ' proof
  125. function find_telefono(i,input){
  126. var tel = all_artesanos[i]["Telefono 1"];
  127. tel = tel.trim();
  128. //To make sure they're both the same im taking out spaces,'(',')' and '-' to make sure they're both the same format
  129. tel = tel.replace(/\s/g,'').replace(/\u0028/g,'').replace(/\u0029/g,'').replace(/\u002D/g,'');
  130. input = input.replace(/\s/g,'').replace(/\u0028/g,'').replace(/\u0029/g,'').replace(/\u002D/g,'');
  131. if(tel.search(input) != -1){
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. //trata de ver si el input es parte del email del artista i
  137. //Mayuscula and acento proof
  138. function find_email(i,input){
  139. var email = all_artesanos[i]["E-mail"];
  140. email = email.trim().toLowerCase();
  141. email = acento_replace(email);
  142. if(email.search(input) != -1){
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. //Esta funcion trata de coger el numero de caso que se esta buscando, esto es para saber que
  148. //caso del switch para considerar
  149. //LOs casos dependen de lo que esta escrito en cada search bar, cada vez que se escribe en uno nuevo o cuando se borra completamente uno entonces es un caso nuevo
  150. function getcasenum(input,inputarte,inputmuni){
  151. //this will get the case number
  152. //think of this as 3 binary numbers where 001 would mean that only its input muni
  153. //010 is only inputarte and 100 is only input
  154. //max number is 7
  155. var casenum = 0;
  156. if (input.length != 0){
  157. casenum += 4;
  158. }
  159. if (inputarte.length != 0){
  160. casenum += 2;
  161. }
  162. if (inputmuni.length != 0){
  163. casenum += 1;
  164. }
  165. return casenum;
  166. }
  167. //case 3: artesania y municipio
  168. //Busca todos los artistas que trabajan con esa artesania y en ese municipio
  169. function search_art_mun(inputarte,inputmuni){
  170. var x = document.getElementsByClassName('artists');
  171. for (var i = 0; i < all_artesanos.length; i++){
  172. if (find_artesania(i,inputarte) && find_municipio(i,inputmuni)){
  173. x[i].style.display = "list-item";
  174. }
  175. }
  176. }
  177. //Busca todos los artistas que salen con el primer input y trabajan en ese municipio
  178. function search_any_mun(input,inputmuni){
  179. var x = document.getElementsByClassName('artists');
  180. for (var i = 0; i < all_artesanos.length; i++){
  181. if(find_any(i,input) && find_municipio(i,inputmuni)){
  182. x[i].style.display = "list-item";
  183. }
  184. }
  185. }
  186. //Busca todos los artistas que salen con el primer input y trabajan con esa artesania
  187. function search_any_art(input,inputarte){
  188. var x = document.getElementsByClassName('artists');
  189. for (var i = 0; i < all_artesanos.length; i++){
  190. if(find_any(i,input) && find_artesania(i,inputarte)){
  191. x[i].style.display = "list-item";
  192. }
  193. }
  194. }
  195. //Busca todos los artistas que salen con el primer input, trabajan con esa artesania y trabajan en ese municipio
  196. function search_any_art_mun(input,inputarte,inputmuni){
  197. var x = document.getElementsByClassName('artists');
  198. for (var i = 0; i < all_artesanos.length; i++){
  199. if(find_any(i,input) && find_artesania(i,inputarte) && find_municipio(i,inputmuni)){
  200. x[i].style.display = "list-item";
  201. }
  202. }
  203. }
  204. //Replacing special characters with non special characters in case input doesnt include it
  205. function acento_replace(string){
  206. var precstring = "";
  207. var poststring = "";
  208. for( i = 0; i < string.length ; i++){
  209. if (string.charAt(i) == 'á' || string.charAt(i) == 'é' || string.charAt(i) == 'í'|| string.charAt(i) == 'ó' || string.charAt(i) == 'ú' || string.charAt(i) == 'ü' || string.charAt(i) == 'ñ' || string.charAt(i) == 'ö'){
  210. precstring = string.slice(0,i);
  211. postcstring = "";
  212. if(i != string.length-1){
  213. postcstring = string.slice(i+1,string.length);
  214. }
  215. switch(string.charAt(i)){
  216. case 'á':
  217. string = precstring.concat('a',postcstring);
  218. break;
  219. case 'é':
  220. string = precstring.concat('e',postcstring);
  221. break;
  222. case 'í':
  223. string = precstring.concat('i',postcstring);
  224. break;
  225. case 'ó':
  226. string = precstring.concat('o',postcstring);
  227. break;
  228. case 'ú':
  229. string = precstring.concat('u',postcstring);
  230. break;
  231. case 'ü':
  232. string = precstring.concat('u',postcstring);
  233. break;
  234. case 'ñ':
  235. string = precstring.concat('n',postcstring);
  236. break;
  237. case 'ö':
  238. string = precstring.concat('o',postcstring);
  239. break;
  240. }
  241. }
  242. }
  243. return string;
  244. }