src/pyams_gis/resources/js/leaflet.Control.Draw.js
changeset 0 c73bb834ccbe
equal deleted inserted replaced
-1:000000000000 0:c73bb834ccbe
       
     1 /**
       
     2  * @class L.Control.Draw
       
     3  * @aka L.Draw
       
     4  */
       
     5 L.Control.Draw = L.Control.extend({
       
     6 
       
     7 	// Options
       
     8 	options: {
       
     9 		position: 'topleft',
       
    10 		draw: {},
       
    11 		edit: false
       
    12 	},
       
    13 
       
    14 	// @method initialize(): void
       
    15 	// Initializes draw control, toolbars from the options
       
    16 	initialize: function (options) {
       
    17 		if (L.version < '0.7') {
       
    18 			throw new Error('Leaflet.draw 0.2.3+ requires Leaflet 0.7.0+. Download latest from https://github.com/Leaflet/Leaflet/');
       
    19 		}
       
    20 
       
    21 		L.Control.prototype.initialize.call(this, options);
       
    22 
       
    23 		var toolbar;
       
    24 
       
    25 		this._toolbars = {};
       
    26 
       
    27 		// Initialize toolbars
       
    28 		if (L.DrawToolbar && this.options.draw) {
       
    29 			toolbar = new L.DrawToolbar(this.options.draw);
       
    30 
       
    31 			this._toolbars[L.DrawToolbar.TYPE] = toolbar;
       
    32 
       
    33 			// Listen for when toolbar is enabled
       
    34 			this._toolbars[L.DrawToolbar.TYPE].on('enable', this._toolbarEnabled, this);
       
    35 		}
       
    36 
       
    37 		if (L.EditToolbar && this.options.edit) {
       
    38 			toolbar = new L.EditToolbar(this.options.edit);
       
    39 
       
    40 			this._toolbars[L.EditToolbar.TYPE] = toolbar;
       
    41 
       
    42 			// Listen for when toolbar is enabled
       
    43 			this._toolbars[L.EditToolbar.TYPE].on('enable', this._toolbarEnabled, this);
       
    44 		}
       
    45 		L.toolbar = this; //set global var for editing the toolbar
       
    46 	},
       
    47 
       
    48 	// @method onAdd(): container
       
    49 	// Adds the toolbar container to the map
       
    50 	onAdd: function (map) {
       
    51 		var container = L.DomUtil.create('div', 'leaflet-draw'),
       
    52 			addedTopClass = false,
       
    53 			topClassName = 'leaflet-draw-toolbar-top',
       
    54 			toolbarContainer;
       
    55 
       
    56 		for (var toolbarId in this._toolbars) {
       
    57 			if (this._toolbars.hasOwnProperty(toolbarId)) {
       
    58 				toolbarContainer = this._toolbars[toolbarId].addToolbar(map);
       
    59 
       
    60 				if (toolbarContainer) {
       
    61 					// Add class to the first toolbar to remove the margin
       
    62 					if (!addedTopClass) {
       
    63 						if (!L.DomUtil.hasClass(toolbarContainer, topClassName)) {
       
    64 							L.DomUtil.addClass(toolbarContainer.childNodes[0], topClassName);
       
    65 						}
       
    66 						addedTopClass = true;
       
    67 					}
       
    68 
       
    69 					container.appendChild(toolbarContainer);
       
    70 				}
       
    71 			}
       
    72 		}
       
    73 
       
    74 		return container;
       
    75 	},
       
    76 
       
    77 	// @method onRemove(): void
       
    78 	// Removes the toolbars from the map toolbar container
       
    79 	onRemove: function () {
       
    80 		for (var toolbarId in this._toolbars) {
       
    81 			if (this._toolbars.hasOwnProperty(toolbarId)) {
       
    82 				this._toolbars[toolbarId].removeToolbar();
       
    83 			}
       
    84 		}
       
    85 	},
       
    86 
       
    87 	// @method setDrawingOptions(options): void
       
    88 	// Sets options to all toolbar instances
       
    89 	setDrawingOptions: function (options) {
       
    90 		for (var toolbarId in this._toolbars) {
       
    91 			if (this._toolbars[toolbarId] instanceof L.DrawToolbar) {
       
    92 				this._toolbars[toolbarId].setOptions(options);
       
    93 			}
       
    94 		}
       
    95 	},
       
    96 
       
    97 	_toolbarEnabled: function (e) {
       
    98 		var enabledToolbar = e.target;
       
    99 
       
   100 		for (var toolbarId in this._toolbars) {
       
   101 			if (this._toolbars[toolbarId] !== enabledToolbar) {
       
   102 				this._toolbars[toolbarId].disable();
       
   103 			}
       
   104 		}
       
   105 	}
       
   106 });
       
   107 
       
   108 L.Map.mergeOptions({
       
   109 	drawControlTooltips: true,
       
   110 	drawControl: false
       
   111 });
       
   112 
       
   113 L.Map.addInitHook(function () {
       
   114 	if (this.options.drawControl) {
       
   115 		this.drawControl = new L.Control.Draw();
       
   116 		this.addControl(this.drawControl);
       
   117 	}
       
   118 });