app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. let currentPage = 2;
  3. const tabs = document.getElementsByClassName('page');
  4. const validField = (formId, fieldId, customFieldId) => {
  5. const form = document.getElementById(formId);
  6. const element = form.querySelector(`#${fieldId}`);
  7. if (element.value === '') {
  8. form.classList.add('was-validated');
  9. console.log("invalid", fieldId)
  10. return false;
  11. } else {
  12. if (customFieldId && element.value === 'other') {
  13. const customField = form.querySelector(`#${customFieldId}`);
  14. if (customField.value === '') {
  15. customField.classList.add('is-invalid');
  16. console.log("invalidcustom", customFieldId)
  17. return false
  18. } else {
  19. console.log("valid custom", customFieldId)
  20. return true
  21. }
  22. }
  23. console.log("custom with response", fieldId, customFieldId)
  24. return true;
  25. }
  26. }
  27. const conditions = {
  28. agreement: [
  29. () => {
  30. const agreementCheckbox = document.getElementById('agreement');
  31. if (agreementCheckbox.checked) {
  32. return true;
  33. } else {
  34. const form = document.getElementById('terms-and-conditions-form');
  35. form.classList.add('was-validated');
  36. return false;
  37. }
  38. }
  39. ],
  40. personalInformation: [
  41. () => validField('personal-information-form', 'age-range'),
  42. () => validField('personal-information-form', 'gender', 'gender-custom'),
  43. () => validField('personal-information-form', 'ethnicity', 'ethnicity-custom'),
  44. () => validField('personal-information-form', 'town-birth'),
  45. () => validField('personal-information-form', 'postal-code'),
  46. ]
  47. }
  48. const showCustomInput = (customElementId, element) => {
  49. if (element.value === 'other') {
  50. const customInput = document.getElementById(customElementId);
  51. customInput.style.display = 'block'
  52. }
  53. }
  54. const showPage = (page) => {
  55. tabs[page].style.display = 'block';
  56. }
  57. const isValid = condition => condition();
  58. const changePage = (page, condition) => {
  59. if (condition && !conditions[condition].map(isValid).every(result => result)) {
  60. console.log("not all conditions passed");
  61. return;
  62. }
  63. tabs[currentPage].style.display = 'none';
  64. showPage(page);
  65. currentPage = page;
  66. }
  67. showPage(2);
  68. mapboxgl.accessToken = 'pk.eyJ1IjoidGluYW1hIiwiYSI6ImNpbWV2dDJudjAxM2Z0c2traGthOTJyZjYifQ.s_lP9rp2Iro7Urb4IZAzTQ';
  69. const bounds = [
  70. [-2.460270, 53.370427],
  71. [-2.062598, 53.552900]
  72. ]
  73. const map = new mapboxgl.Map({
  74. container: 'map',
  75. style: 'mapbox://styles/tinama/ck7avipta06nj1jmomolvvrbr',
  76. center: [-2.884808, 53.495743],
  77. zoom: 9.5,
  78. });
  79. const Draw = new MapboxDraw({
  80. displayControlsDefault: false,
  81. controls: {
  82. polygon: true,
  83. trash: true
  84. }
  85. });
  86. console.log("turf", turf);
  87. const AddPopup = (map) => (area) => {
  88. let polygon = turf.polygon(area);
  89. let center = turf.centroid(polygon);
  90. console.log(center, polygon)
  91. return new mapboxgl.Popup({closeOnClick: false})
  92. .setLngLat(center.geometry.coordinates)
  93. .setHTML(`
  94. <h3>form</h3>
  95. <label for="data">some data</label>
  96. <input type="text" class="form-control" id="data" placeholder="" required>
  97. <button type="button">save</button>
  98. `)
  99. .addTo(map)
  100. }
  101. map.addControl(Draw, 'top-right');
  102. map.on('load', function() {
  103. let drawnData = {};
  104. map.on('draw.create', createArea);
  105. map.on('draw.delete', deleteArea);
  106. map.on('draw.update', updateArea);
  107. function createArea(polygon) {
  108. let area = polygon.features[0].geometry;
  109. drawnData = {
  110. ...drawnData,
  111. [polygon.features[0].id]: area
  112. };
  113. console.log("create", area);
  114. const popup = AddPopup(map)(area.coordinates);
  115. }
  116. function deleteArea(polygon) {
  117. console.log('delete', polygon);
  118. }
  119. function updateArea(polygon) {
  120. console.log('update', polygon)
  121. }
  122. });