app.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. let currentPage = 0;
  3. const tabs = document.getElementsByClassName('page');
  4. const conditions = {
  5. agreement: [
  6. () => {
  7. const agreementCheckbox = document.getElementById('agreement');
  8. if (agreementCheckbox.checked) {
  9. return true;
  10. } else {
  11. const form = document.getElementById('terms-and-conditions');
  12. form.classList.add('was-validated');
  13. return false;
  14. }
  15. }
  16. ]
  17. }
  18. const showPage = (page) => {
  19. tabs[page].style.display = 'block';
  20. }
  21. const isValid = condition => condition();
  22. const changePage = (page, condition) => {
  23. if (condition && !conditions[condition].every(isValid)) {
  24. console.log("not all conditions passed");
  25. return;
  26. }
  27. tabs[currentPage].style.display = 'none';
  28. showPage(page);
  29. currentPage = page;
  30. }
  31. showPage(0);
  32. mapboxgl.accessToken = 'pk.eyJ1IjoidGluYW1hIiwiYSI6ImNpbWV2dDJudjAxM2Z0c2traGthOTJyZjYifQ.s_lP9rp2Iro7Urb4IZAzTQ';
  33. const bounds = [
  34. [-2.460270, 53.370427],
  35. [-2.062598, 53.552900]
  36. ]
  37. const map = new mapboxgl.Map({
  38. container: 'map',
  39. style: 'mapbox://styles/tinama/ck7avipta06nj1jmomolvvrbr',
  40. center: [-2.884808, 53.495743],
  41. zoom: 9.5,
  42. });
  43. const Draw = new MapboxDraw({
  44. displayControlsDefault: false,
  45. controls: {
  46. polygon: true,
  47. trash: true
  48. }
  49. });
  50. console.log("turf", turf);
  51. const AddPopup = (map) => (area) => {
  52. let polygon = turf.polygon(area);
  53. let center = turf.centroid(polygon);
  54. console.log(center, polygon)
  55. return new mapboxgl.Popup({closeOnClick: false})
  56. .setLngLat(center.geometry.coordinates)
  57. .setHTML(`
  58. <h3>form</h3>
  59. <label for="data">some data</label>
  60. <input type="text" class="form-control" id="data" placeholder="" required>
  61. <button type="button">save</button>
  62. `)
  63. .addTo(map)
  64. }
  65. map.addControl(Draw, 'top-right');
  66. map.on('load', function() {
  67. let drawnData = {};
  68. map.on('draw.create', createArea);
  69. map.on('draw.delete', deleteArea);
  70. map.on('draw.update', updateArea);
  71. function createArea(polygon) {
  72. let area = polygon.features[0].geometry;
  73. drawnData = {
  74. ...drawnData,
  75. [polygon.features[0].id]: area
  76. };
  77. console.log("create", area);
  78. const popup = AddPopup(map)(area.coordinates);
  79. }
  80. function deleteArea(polygon) {
  81. console.log('delete', polygon);
  82. }
  83. function updateArea(polygon) {
  84. console.log('update', polygon)
  85. }
  86. });