site.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. $(function () {
  3. $('.fieldlist').each(function() {
  4. var $this=$(this);
  5. var lis=$this.children('li');
  6. lis.first().children(':first').after(' <button class="btn btn-mini" type="button"><i class="icon-plus"></i></button>');
  7. lis.first().children('button').click(function() {
  8. clone_fieldlist($this.children('li:last'));
  9. });
  10. lis=lis.slice(1);
  11. lis.each(function() {
  12. append_remove_button($(this));
  13. });
  14. });
  15. $('.selectpicker').selectpicker();
  16. $("[rel=tooltip]").tooltip();
  17. init_map();
  18. });
  19. window.isps_covered_areas={};
  20. function get_covered_areas(isp_id, cb) {
  21. if(isp_id in window.isps_covered_areas) {
  22. cb(window.isps_covered_areas[isp_id]);
  23. return;
  24. } else {
  25. window.isps_covered_areas[isp_id]=[];
  26. }
  27. $.getJSON('/isp/'+isp_id+'/covered_areas.json', function(data) {
  28. $.each(data, function(k, covered_area) {
  29. if(!('area' in covered_area))
  30. return;
  31. window.isps_covered_areas[isp_id].push(
  32. L.geoJson(covered_area['area'], {
  33. style: {
  34. "color": "#ff7800",
  35. "weight": 5,
  36. "opacity": 0.65
  37. }
  38. })
  39. );
  40. });
  41. cb(window.isps_covered_areas[isp_id]);
  42. });
  43. }
  44. function init_map() {
  45. var mapquest=L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg', {
  46. attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, '+
  47. 'Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>',
  48. subdomains: '1234'
  49. });
  50. var mapquestsat=L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg', {
  51. attribution: '&copy; Tiles courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>, '+
  52. 'Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency',
  53. subdomains: '1234',
  54. maxZoom: 11
  55. });
  56. var osm=L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  57. attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
  58. subdomains: 'ab'
  59. });
  60. var hyb=L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/hyb/{z}/{x}/{y}.jpg', {
  61. subdomains: '1234',
  62. maxZoom: 11
  63. });
  64. if(!$('#map').length)
  65. return;
  66. var map = L.map('map', {
  67. center: new L.LatLng(46.603354, 10),
  68. zoom: 4,
  69. layers: [mapquest]
  70. });
  71. map.attributionControl.setPrefix('');
  72. L.control.layers({'MapQuest': mapquest, 'OSM Mapnik': osm, 'MapQuest Aerial': mapquestsat}).addTo(map);
  73. map.on('baselayerchange', function(a) {
  74. if(a.name == 'MapQuest Aerial') {
  75. map.addLayer(hyb);
  76. hyb.bringToFront();
  77. } else {
  78. map.removeLayer(hyb);
  79. }
  80. });
  81. var icon = L.icon({
  82. iconUrl: 'static/img/marker.png',
  83. iconSize: [14, 20], // size of the icon
  84. shadowSize: [14, 20], // size of the shadow
  85. iconAnchor: [7, 20], // point of the icon which will correspond to marker's location
  86. popupAnchor: [0, -20] // point from which the popup should open relative to the iconAnchor
  87. });
  88. var icon_ffdn = $.extend(true, {}, icon);
  89. icon_ffdn['options']['iconUrl'] = 'static/img/marker_ffdn.png';
  90. $.getJSON('/isp/map_data.json', function(data) {
  91. $.each(data, function(k, isp) {
  92. if(!('coordinates' in isp))
  93. return; // cannot display an ISP without coordinates
  94. var marker = L.marker([isp['coordinates']['latitude'], isp['coordinates']['longitude']],
  95. {'icon': isp.ffdn_member ? icon_ffdn : icon});
  96. marker.on('click', function() {
  97. get_covered_areas(isp.id, function(items) {
  98. $.each(items, function(k, ca) {
  99. ca.addTo(map);
  100. });
  101. });
  102. }).bindPopup(isp.popup);
  103. marker._popup.on('close', function() {
  104. $.each(window.isps_covered_areas[isp.id], function(k, ca) {
  105. map.removeLayer(ca);
  106. });
  107. });
  108. marker.addTo(map);
  109. });
  110. });
  111. }
  112. function change_input_num(li, new_num, reset=false) {
  113. li.find('input,select').each(function() {
  114. var id = $(this).attr('id').replace(/^(.*)-\d{1,4}/, '$1-'+new_num);
  115. $(this).attr({'name': id, 'id': id});
  116. if(reset)
  117. $(this).val('').removeAttr('checked');
  118. });
  119. }
  120. function append_remove_button(li) {
  121. li.children(':first').after(' <button class="btn btn-mini" type="button"><i class="icon-minus"></i></button>');
  122. li.children('button').click(function() {
  123. var ul=li.parent();
  124. li.remove();
  125. var i=0;
  126. ul.children('li').each(function() {
  127. change_input_num($(this), i);
  128. i++;
  129. });
  130. });
  131. };
  132. function clone_fieldlist(el) {
  133. var new_element = el.clone(true);
  134. var elem_id = new_element.find(':input')[0].id;
  135. var elem_num = parseInt(elem_id.replace(/^.*-(\d{1,4})/, '$1')) + 1;
  136. change_input_num(new_element, elem_num, true);
  137. new_element.children('button').remove();
  138. new_element.children('.help-inline.error-list').remove();
  139. new_element.find('.bootstrap-select').remove();
  140. append_remove_button(new_element);
  141. new_element.find('.selectpicker').data('selectpicker', null).selectpicker();
  142. el.after(new_element);
  143. }