1/** 2 * @file 3 * Polygons support plugin. 4 */ 5 6(function($) { 7 Drupal.behaviors.yamapsPolygon = { 8 attach: function (context, settings) { 9 ymaps.ready(function() { 10 // Class for one polygon. 11 $.yaMaps.YamapsPolygon = function(geometry, properties, options) { 12 this._init(new ymaps.Polygon(geometry, properties, options)); 13 }; 14 $.yaMaps.YamapsPolygon.prototype = $.yaMaps.BaseYamapsObject; 15 16 // Class for polygons collection. 17 $.yaMaps.YamapsPolygonCollection = function(options) { 18 this._init(options); 19 // Selector "storagePrefix + MAP_ID" will be used 20 // for export collection data. 21 this.storagePrefix = '.field-yamaps-polygons-'; 22 23 // Create polygon and add to collection. 24 this.createPolygon = function(geometry, properties, options) { 25 return this.add(new $.yaMaps.YamapsPolygon(geometry, properties, options)); 26 }; 27 }; 28 $.yaMaps.YamapsPolygonCollection.prototype = $.yaMaps.BaseYamapsObjectCollection; 29
30 // Edit polygon balloon template. 31 $.yaMaps.addLayout('yamaps#PolygonBalloonEditLayout', 32 ymaps.templateLayoutFactory.createClass( 33 [ 34 '<div class="yamaps-balloon yamaps-polygon-edit">', 35 '<div class="form-element line-colors">', 36 '<label>' + Drupal.t('Line color') + '</label>', 37 '$[[yamaps#ColorPicker]]', 38 '</div>', 39 '<div class="form-element poly-colors">', 40 '<label>' + Drupal.t('Polygon color') + '</label>', 41 '$[[yamaps#ColorPicker]]', 42 '</div>', 43 '<div class="form-element line-width">', 44 '$[[yamaps#StrokeWidthLayout]]', 45 '</div>', 46 '<div class="form-element poly-opacity">', 47 '$[[yamaps#OpacityLayout]]', 48 '</div>', 49 '<div class="form-element">', 50 '<label for="balloonContent">' + Drupal.t('Balloon text') + '</label>', 51 '<input type="text" id="balloonContent" value="$[properties.balloonContent]"/>', 52 '</div>', 53 '$[[yamaps#ActionsButtons]]', 54 '</div>' 55 ].join(""), 56 { 57 build: function () { 58 this.constructor.superclass.build.call(this); 59 this.properties = this.getData().properties.getAll(); 60 // Balloon HTML element. 61 var $element = $(this.getParentElement()); 62 var _this = this; 63 64 // Polygon background colorpicker. 65 this.$polyColors = $element.find('.poly-colors .yamaps-color'); 66 this.$polyColors.each(function() { 67 var $this = $(this); 68 var $div = $this.children('div'); 69 if (_this.properties.fillColor == $div.attr('data-content')) { 70 $this.addClass('yamaps-color-active'); 71 } 72 }); 73 this.$polyColors.bind('click', this, this.fillColorClick); 74 75 // Polygon line colorpicker. 76 this.$lineColors = $element.find('.line-colors .yamaps-color'); 77 this.$lineColors.each(function() { 78 var $this = $(this); 79 var $div = $this.children('div'); 80 if (_this.properties.strokeColor == $div.attr('data-content')) { 81 $this.addClass('yamaps-color-active'); 82 } 83 }); 84 this.$lineColors.bind('click', this, this.strokeColorClick); 85 86 // Opacity. 87 this.$opacity = $element.find('.poly-opacity select'); 88 this.$opacity.val(_this.properties.opacity); 89 90 // Stroke width. 91 this.$width = $element.find('.line-width select'); 92 this.$width.val(_this.properties.strokeWidth); 93 94 // Balloon content. 95 this.$balloonContent = $element.find('#balloonContent'); 96 97 // Actions. 98 $('#deleteButton').bind('click', this, this.onDeleteClick); 99 $('#saveButton').bind('click', this, this.onSaveClick); 100 }, 101 clear: function () { 102 this.constructor.superclass.build.call(this); 103 this.$polyColors.unbind('click', this, this.fillColorClick); 104 this.$lineColors.unbind('click', this, this.strokeColorClick); 105 $('#deleteButton').unbind('click', this, this.onDeleteClick); 106 $('#saveButton').unbind('click', this, this.onSaveClick); 107 }, 108 fillColorClick: function(e) { 109 // Fill colorpicker click. 110 e.data.properties.fillColor = $(this).children('div').attr('data-content'); 111 }, 112 strokeColorClick: function(e) { 113 // Stroke colorpicker click. 114 e.data.properties.strokeColor = $(this).children('div').attr('data-content'); 115 }, 116 onDeleteClick: function (e) { 117 // Delete click. 118 e.data.properties.element.remove(); 119 e.preventDefault(); 120 }, 121 onSaveClick: function(e) { 122 // Save click. 123 var polygon = e.data.properties.element; 124 // Set opacity. 125 e.data.properties.opacity = e.data.$opacity.val(); 126 polygon.setOpacity(e.data.properties.opacity); 127 // Set stroke width. 128 e.data.properties.strokeWidth = e.data.$width.val(); 129 polygon.setWidth(e.data.properties.strokeWidth); 130 // Set colors. 131 polygon.setColor(e.data.properties.strokeColor, e.data.properties.fillColor); 132 // Set balloon content. 133 polygon.setContent(e.data.$balloonContent.val()); 134 polygon.closeBalloon(); 135 } 136 } 137 ) 138 ); 139 140 // Add polygons support to map. 141 $.yaMaps.addMapTools(function(Map) { 142 // Default options. 143 var options = { 144 balloonMaxWidth: 300, 145 balloonCloseButton: true, 146 strokeWidth: 3, 147 elements: {} 148 }; 149 if (Map.options.edit) { 150 // If map in edit mode set edit mode to polygons options. 151 options.balloonContentBodyLayout = 'yamaps#PolygonBalloonEditLayout'; 152 options.draggable = true; 153 } 154 155 // Create polygons collection. 156 var polygonsCollection = new $.yaMaps.YamapsPolygonCollection(options); 157 158 // Add empty collection to the map. 159 Map.map.geoObjects.add(polygonsCollection.elements); 160 161 // Add already created polygons to map. 162 for (var i in Map.options.polygons) { 163 var Polygon = polygonsCollection.createPolygon(Map.options.polygons[i].coords, Map.options.polygons[i].params); 164 if (Map.options.edit) { 165 Polygon.startEditing(); 166 } 167 } 168 169 // If map in view mode exit. 170 if (!Map.options.edit) { 171 return; 172 } 173 174 // If map in edit mode set map click listener to adding new polygon. 175 var mapClick = function(event) { 176 var Polygon = polygonsCollection.createPolygon([[event.get('coordPosition')]], {balloonContent: '', fillColor: 'lightblue', strokeColor: 'blue', opacity: 0.6, strokeWidth: 3}); 177 Polygon.startEditing(true); 178 }; 179 180 // Add new button. 181 var polygonButton = new ymaps.control.Button({ 182 data: { 183 content: '<ymaps class="ymaps-b-form-button__text"><ymaps class="ymaps-b-ico ymaps-b-ico_type_poly"></ymaps></ymaps>', 184 title: Drupal.t('Drawing polygons') 185 } 186 }); 187 188 // Button actions. 189 polygonButton.events 190 .add('select', function(event) { 191 Map.cursor = Map.map.cursors.push('pointer'); 192 Map.mapListeners.add('click', mapClick); 193 }) 194 .add('deselect', function(event) { 195 Map.cursor.remove(); 196 Map.mapListeners.remove('click', mapClick); 197 }); 198 199 return polygonButton; 200 }); 201 }); 202 } 203 } 204})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.