common.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. let Countries = [
  18. {
  19. 'id': 'spain',
  20. 'name': 'Spain'
  21. }
  22. ];
  23. let ProvincesFilter = [
  24. {
  25. 'provincia_id': '35'
  26. }, {
  27. 'provincia_id': '38'
  28. }
  29. ];
  30. let Provinces;
  31. let Municipalities;
  32. let ZIPCodes;
  33. function GenerateFieldValues(Data, FieldName) {
  34. let FieldValues = [];
  35. Data.forEach((Value, Key) => {
  36. FieldValues.push(Value[FieldName]);
  37. });
  38. return FieldValues;
  39. }
  40. function DumpCanaryData(Data, DependentValues, FieldName, NoId = false, NewId = '') {
  41. let CanaryData = [];
  42. let FieldValues = GenerateFieldValues(DependentValues, FieldName);
  43. Data.forEach((Value, Key) => {
  44. if (FieldValues.includes(Value[FieldName])) {
  45. if (NoId === true && NewId !== '') {
  46. Value[NewId] = Key.toString();
  47. CanaryData.push(Value);
  48. } else {
  49. CanaryData.push(Value);
  50. }
  51. }
  52. });
  53. return CanaryData;
  54. }
  55. // Load data
  56. async function LoadData() {
  57. await $.getJSON('./data/provincias.json', function (Data) {
  58. Provinces = DumpCanaryData(Data, ProvincesFilter, 'provincia_id');
  59. });
  60. await $.getJSON('./data/municipios_islas.json', function (Data) {
  61. Municipalities = DumpCanaryData(Data, Provinces, 'provincia_id');
  62. });
  63. await $.getJSON('./data/codigos_postales_municipios.json', function (Data) {
  64. ZIPCodes = DumpCanaryData(Data, Municipalities, 'municipio_id', true, 'cp_id');
  65. });
  66. }
  67. ;
  68. function CheckListValue(Data, Content, FieldName) {
  69. let Exists = false;
  70. let Position = 0;
  71. while (Position < Data.length && !Exists) {
  72. if (Data[Position][FieldName] === Content) {
  73. Exists = true;
  74. }
  75. Position++;
  76. }
  77. return Exists;
  78. }
  79. function GeneratePossibleValues(Data, Content, Name) {
  80. let PossibleValues = [];
  81. Data.forEach((Value, Key) => {
  82. if (Value[Name] === Content) {
  83. PossibleValues.push(Value);
  84. }
  85. });
  86. return PossibleValues;
  87. }
  88. function RestartZIPCode() {
  89. let ZIPCode = document.getElementById('zipcode');
  90. ZIPCode.value = '';
  91. ZIPCode.setAttribute('disabled', '');
  92. }
  93. function ShowZIPCode() {
  94. let Municipality = document.getElementById('municipality').value;
  95. let ZIPCode = document.getElementById('zipcode');
  96. let PossibleValues = GeneratePossibleValues(ZIPCodes, Municipality, 'municipio_id');
  97. if (PossibleValues.length !== 0) {
  98. ZIPCode.removeAttribute('disabled');
  99. for (let Position = 1; Position < ZIPCode.length; Position++) {
  100. let Option = ZIPCode[Position];
  101. if (CheckListValue(PossibleValues, Option.value, 'cp_id')) {
  102. Option.removeAttribute('style');
  103. } else {
  104. Option.style.display = 'none';
  105. }
  106. }
  107. } else {
  108. for (let Position = 1; Position < ZIPCode.length; Position++) {
  109. let Option = ZIPCode[Position];
  110. Option.removeAttribute('style');
  111. }
  112. }
  113. }
  114. function RestartMunicipality() {
  115. let Municipality = document.getElementById('municipality');
  116. Municipality.value = '';
  117. Municipality.setAttribute('disabled', '');
  118. }
  119. function ShowMunicipality() {
  120. let Province = document.getElementById('province').value;
  121. let Municipality = document.getElementById('municipality');
  122. let PossibleValues = GeneratePossibleValues(Municipalities, Province, 'provincia_id');
  123. if (PossibleValues.length !== 0) {
  124. Municipality.removeAttribute('disabled');
  125. for (let Position = 1; Position < Municipality.length; Position++) {
  126. let Option = Municipality[Position];
  127. if (CheckListValue(PossibleValues, Option.value, 'municipio_id')) {
  128. Option.removeAttribute('style');
  129. } else {
  130. Option.style.display = 'none';
  131. }
  132. }
  133. } else {
  134. for (let Position = 1; Position < Municipality.length; Position++) {
  135. let Option = Municipality[Position];
  136. Option.removeAttribute('style');
  137. }
  138. }
  139. RestartZIPCode();
  140. ShowZIPCode();
  141. }
  142. function RestartProvince() {
  143. let Province = document.getElementById('province');
  144. Province.value = '';
  145. Province.setAttribute('disabled', '');
  146. }
  147. function ShowProvince() {
  148. let Country = document.getElementById('country').value;
  149. let Province = document.getElementById('province');
  150. if (Country === Countries[0]['id']) {
  151. Province.removeAttribute('disabled');
  152. for (let Position = 1; Position < Province.length; Position++) {
  153. let Option = Province[Position];
  154. if (CheckListValue(Provinces, Option.value, 'provincia_id')) {
  155. Option.removeAttribute('style');
  156. } else {
  157. Option.style.display = 'none';
  158. }
  159. }
  160. } else {
  161. for (let Position = 1; Position < Province.length; Position++) {
  162. let Option = Province[Position];
  163. Option.removeAttribute('style');
  164. }
  165. }
  166. RestartMunicipality();
  167. ShowMunicipality();
  168. }