Sovun

with observers

Sep 11th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. /**
  2. @module ember-flexberry-gis
  3. */
  4.  
  5. import Ember from 'ember';
  6. import layout from '../templates/components/flexberry-boundingbox';
  7. import FlexberryMapActionsHandlerMixin from '../mixins/flexberry-map-actions-handler';
  8.  
  9.  
  10.  
  11. export default Ember.Component.extend(
  12. FlexberryMapActionsHandlerMixin, {
  13. /**
  14. Reference to component's template.
  15. */
  16. layout,
  17. /**
  18. Leaflet map.
  19. @property leafletMap
  20. @type <a href="http://leafletjs.com/reference-1.0.0.html#map">L.Map</a>
  21. @default null
  22. */
  23. leafletMap: null,
  24.  
  25. /**
  26. Observes changes in reference to leaflet map.
  27. Initializes area select plugin.
  28. @method _leafletMapDidChange
  29. @private
  30. */
  31. _leafletMapDidChange: Ember.on('init', Ember.observer('leafletMap', function() {
  32. let leafletMap = this.get('leafletMap');
  33. if (!Ember.isNone(leafletMap)) {
  34. let areaSelect = L.areaSelect({width:100, height:100});
  35. areaSelect.addTo(leafletMap);
  36. this.set('areaSelect', areaSelect);
  37. areaSelect.on("change", () => {
  38. let bounds = areaSelect.getBounds();
  39. this.set('minLgt', bounds.getWest());
  40. this.set('maxLgt', bounds.getEast());
  41. this.set('minLat', bounds.getSouth());
  42. this.set('maxLat', bounds.getNorth());
  43. console.log("сработал ареаселект чендж");
  44. return bounds;
  45. });
  46. }
  47. })),
  48.  
  49. _latDidChange: Ember.observer('minLat', 'maxLat', function () {
  50. //Ember.run.once(this, function(){
  51. let minLat = Number(this.get('minLat'));
  52. let maxLat = Number(this.get('maxLat'));
  53. if(minLat>maxLat){
  54. [minLat, maxLat] = [maxLat, minLat];
  55. }
  56. if(minLat<-90){
  57. minLat = -90;
  58. }
  59. if(maxLat>90){
  60. maxLat = 90;
  61. }
  62. this.set('minLat', minLat);
  63. this.set('maxLat', maxLat);
  64.  
  65. let leafletMap = this.get('leafletMap');
  66. let areaSelect = this.get('areaSelect');
  67. let maxLgt = Number(this.get('maxLgt')),
  68. minLgt = Number(this.get('minLgt'));
  69. let coords = L.latLng((maxLat+minLat)/2, (maxLgt+minLgt)/2);
  70. leafletMap.panTo(coords);
  71. leafletMap.fitBounds(L.latLngBounds(L.latLng(minLat, minLgt),L.latLng(maxLat, maxLgt)));
  72. let newWidth = Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, maxLgt)).x) - Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, minLgt)).x);
  73. let newHeight = Math.abs(leafletMap.latLngToLayerPoint(L.latLng(maxLat, minLgt)).y) - Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, minLgt)).y);
  74. newWidth = Math.abs(newWidth);
  75. newHeight = Math.abs(newHeight);
  76. console.log("w: ", newWidth,"h: ", newHeight);
  77. areaSelect.setDimensions({width:newWidth, height:newHeight});
  78. //});
  79. }),
  80.  
  81. _lgtDidChange: Ember.observer('minLgt', 'maxLgt', function () {
  82. //Ember.run.once(this, function(){
  83.  
  84. let minLgt = Number(this.get('minLgt'));
  85. let maxLgt = Number(this.get('maxLgt'));
  86. if(minLgt>maxLgt){
  87. [minLgt, maxLgt] = [maxLgt, minLgt];
  88. }
  89. if(minLgt<-90){
  90. minLgt = -90;
  91. }
  92. if(maxLgt>90){
  93. maxLgt = 90;
  94. }
  95. this.set('minLgt', minLgt);
  96. this.set('maxLgt', maxLgt);
  97.  
  98. let leafletMap = this.get('leafletMap');
  99. let areaSelect = this.get('areaSelect');
  100. let maxLat = Number(this.get('maxLat')),
  101. minLat = Number(this.get('minLat'));
  102. let coords = L.latLng((maxLat+minLat)/2, (maxLgt+minLgt)/2);
  103. leafletMap.panTo(coords);
  104. leafletMap.fitBounds(L.latLngBounds(L.latLng(minLat, minLgt),L.latLng(maxLat, maxLgt)));
  105. let newWidth = Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, maxLgt)).x) - Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, minLgt)).x);
  106. let newHeight = Math.abs(leafletMap.latLngToLayerPoint(L.latLng(maxLat, minLgt)).y) - Math.abs(leafletMap.latLngToLayerPoint(L.latLng(minLat, minLgt)).y);
  107. newWidth = Math.abs(newWidth);
  108. newHeight = Math.abs(newHeight);
  109. console.log("w: ", newWidth,"h: ", newHeight);
  110. areaSelect.setDimensions({width:newWidth, height:newHeight});
  111. // });
  112. }),
  113.  
  114.  
  115.  
  116. actions: {
  117. },
  118. });
Advertisement
Add Comment
Please, Sign In to add comment