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

searchfunctions.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_tecnicas(i,input)){
  53. x[i].style.display = "list-item";
  54. continue;
  55. }
  56. else if (find_artesania(i,input)){
  57. x[i].style.display = "list-item";
  58. continue;
  59. }
  60. else if (find_municipio(i,input)){
  61. x[i].style.display = "list-item";
  62. continue;
  63. }
  64. else if (find_telefono(i,input)){
  65. x[i].style.display = "list-item";
  66. continue;
  67. }
  68. else if (find_email(i,input)){
  69. x[i].style.display = "list-item";
  70. continue;
  71. }
  72. }
  73. return;
  74. }
  75. //Te trata de buscar si cualquiera de ellos tienen el input que se dio aplica a ese artista especifico (i)
  76. function find_any(i,input){
  77. //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
  78. if (find_tecnicas(i,input) || find_name(i,input) || find_telefono(i,input) || find_email(i,input) || find_artesania(i,input) || find_municipio(i,input)){
  79. return 1;
  80. }
  81. else{
  82. return 0;
  83. }
  84. }
  85. //Trata de ver si el input es parte del nombre del artista i
  86. //Mayuscula y acento proof
  87. function find_name(i,input){
  88. var name = all_artesanos[i].Nombre;
  89. name = name.trim().toLowerCase();
  90. name = acento_replace(name);
  91. if(name.search(input) != -1){
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. //trata de ver si el input es parte de alguna artesania que trabaja el artista i
  97. //Mayuscula y acento proof
  98. function find_artesania(i,input){
  99. var artesanias = all_artesanos[i].Especificacion;
  100. artesanias = artesanias.trim().toLowerCase();
  101. artesanias = acento_replace(artesanias);
  102. if(artesanias.search(input) != -1){
  103. return 1;
  104. }
  105. //artesanias = artesanias.split(',');
  106. //for (var j = 0; j < artesanias.length; j++){
  107. //var artesania = artesanias[j];
  108. //artesania = artesania.trim().toLowerCase();
  109. //artesania = acento_replace(artesania);
  110. //if (artesania.search(input) != -1){
  111. //return 1;
  112. //}
  113. //}
  114. return 0;
  115. }
  116. function find_tecnicas(i,input){
  117. var tecnicas = all_artesanos[i]["Tecnica 1"];
  118. tecnicas = tecnicas.trim().toLowerCase();
  119. tecnicas = acento_replace(tecnicas);
  120. if(tecnicas.search(input) != -1){
  121. return 1;
  122. }
  123. //artesanias = artesanias.split(',');
  124. //for (var j = 0; j < artesanias.length; j++){
  125. //var artesania = artesanias[j];
  126. //artesania = artesania.trim().toLowerCase();
  127. //artesania = acento_replace(artesania);
  128. //if (artesania.search(input) != -1){
  129. //return 1;
  130. //}
  131. //}
  132. return 0;
  133. }
  134. //trata de ver si el input es parte del municipio del artista i
  135. //Mayuscula y acento proof
  136. function find_municipio(i,input){
  137. var mun = all_artesanos[i].Municipio;
  138. mun = mun.trim().toLowerCase();
  139. mun = acento_replace(mun);
  140. if(mun.search(input) != -1){
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. //trata de ver si el input es parte del telefono del artista i
  146. //'(',')','-' and ' ' proof
  147. function find_telefono(i,input){
  148. var tel = all_artesanos[i]["Telefono 1"];
  149. tel = tel.trim();
  150. //To make sure they're both the same im taking out spaces,'(',')' and '-' to make sure they're both the same format
  151. tel = tel.replace(/\s/g,'').replace(/\u0028/g,'').replace(/\u0029/g,'').replace(/\u002D/g,'');
  152. input = input.replace(/\s/g,'').replace(/\u0028/g,'').replace(/\u0029/g,'').replace(/\u002D/g,'');
  153. if(tel.search(input) != -1){
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. //trata de ver si el input es parte del email del artista i
  159. //Mayuscula and acento proof
  160. function find_email(i,input){
  161. var email = all_artesanos[i]["E-mail"];
  162. email = email.trim().toLowerCase();
  163. email = acento_replace(email);
  164. if(email.search(input) != -1){
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. //Esta funcion trata de coger el numero de caso que se esta buscando, esto es para saber que
  170. //caso del switch para considerar
  171. //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
  172. function getcasenum(input,inputarte,inputmuni){
  173. //this will get the case number
  174. //think of this as 3 binary numbers where 001 would mean that only its input muni
  175. //010 is only inputarte and 100 is only input
  176. //max number is 7
  177. var casenum = 0;
  178. if (input.length != 0){
  179. casenum += 4;
  180. }
  181. if (inputarte.length != 0){
  182. casenum += 2;
  183. }
  184. if (inputmuni.length != 0){
  185. casenum += 1;
  186. }
  187. return casenum;
  188. }
  189. //case 3: artesania y municipio
  190. //Busca todos los artistas que trabajan con esa artesania y en ese municipio
  191. function search_art_mun(inputarte,inputmuni){
  192. var x = document.getElementsByClassName('artists');
  193. for (var i = 0; i < all_artesanos.length; i++){
  194. if (find_artesania(i,inputarte) && find_municipio(i,inputmuni)){
  195. x[i].style.display = "list-item";
  196. }
  197. }
  198. }
  199. //Busca todos los artistas que salen con el primer input y trabajan en ese municipio
  200. function search_any_mun(input,inputmuni){
  201. var x = document.getElementsByClassName('artists');
  202. for (var i = 0; i < all_artesanos.length; i++){
  203. if(find_any(i,input) && find_municipio(i,inputmuni)){
  204. x[i].style.display = "list-item";
  205. }
  206. }
  207. }
  208. //Busca todos los artistas que salen con el primer input y trabajan con esa artesania
  209. function search_any_art(input,inputarte){
  210. var x = document.getElementsByClassName('artists');
  211. for (var i = 0; i < all_artesanos.length; i++){
  212. if(find_any(i,input) && find_artesania(i,inputarte)){
  213. x[i].style.display = "list-item";
  214. }
  215. }
  216. }
  217. //Busca todos los artistas que salen con el primer input, trabajan con esa artesania y trabajan en ese municipio
  218. function search_any_art_mun(input,inputarte,inputmuni){
  219. var x = document.getElementsByClassName('artists');
  220. for (var i = 0; i < all_artesanos.length; i++){
  221. if(find_any(i,input) && find_artesania(i,inputarte) && find_municipio(i,inputmuni)){
  222. x[i].style.display = "list-item";
  223. }
  224. }
  225. }
  226. //Replacing special characters with non special characters in case input doesnt include it
  227. function acento_replace(string){
  228. var precstring = "";
  229. var poststring = "";
  230. for( i = 0; i < string.length ; i++){
  231. 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) == 'ö'){
  232. precstring = string.slice(0,i);
  233. postcstring = "";
  234. if(i != string.length-1){
  235. postcstring = string.slice(i+1,string.length);
  236. }
  237. switch(string.charAt(i)){
  238. case 'á':
  239. string = precstring.concat('a',postcstring);
  240. break;
  241. case 'é':
  242. string = precstring.concat('e',postcstring);
  243. break;
  244. case 'í':
  245. string = precstring.concat('i',postcstring);
  246. break;
  247. case 'ó':
  248. string = precstring.concat('o',postcstring);
  249. break;
  250. case 'ú':
  251. string = precstring.concat('u',postcstring);
  252. break;
  253. case 'ü':
  254. string = precstring.concat('u',postcstring);
  255. break;
  256. case 'ñ':
  257. string = precstring.concat('n',postcstring);
  258. break;
  259. case 'ö':
  260. string = precstring.concat('o',postcstring);
  261. break;
  262. }
  263. }
  264. }
  265. return string;
  266. }