|
| 1 | +(function (L, setTimeout) { |
| 2 | + |
| 3 | + var _controlContainer, |
| 4 | + _map, |
| 5 | + _zoomThreshold, |
| 6 | + _hideClass = 'leaflet-control-edit-hidden', |
| 7 | + _anchorClass = 'leaflet-control-edit-in-osm-toggle', |
| 8 | + |
| 9 | + _Widgets = { |
| 10 | + MultiButton: function (config) { |
| 11 | + var className = 'leaflet-control-edit-in-osm', |
| 12 | + helpText = "Open this map extent in a map editor to provide more accurate data to OpenStreetMap", |
| 13 | + addEditors = function (container, editors) { |
| 14 | + for (var i in editors) { |
| 15 | + addEditorToWidget(container, editors[i]); |
| 16 | + } |
| 17 | + }; |
| 18 | + |
| 19 | + return { |
| 20 | + className: (config && config.className) || className, |
| 21 | + helpText: (config && config.helpText) || helpText, |
| 22 | + addEditors: (config && config.addEditors) || addEditors |
| 23 | + }; |
| 24 | + }, |
| 25 | + AttributionBox: function (config) { |
| 26 | + var className = 'leaflet-control-attribution', |
| 27 | + helpText = "Edit in OSM", |
| 28 | + addEditors = function (container, editors) { |
| 29 | + addEditorToWidget(container, editors[0], helpText); |
| 30 | + }; |
| 31 | + |
| 32 | + return { |
| 33 | + className: (config && config.className) || className, |
| 34 | + helpText: (config && config.helpText) || helpText, |
| 35 | + addEditors: (config && config.addEditors) || addEditors |
| 36 | + }; |
| 37 | + } |
| 38 | + }, |
| 39 | + |
| 40 | + _Editors = { |
| 41 | + Id: function (config) { |
| 42 | + var url = 'http://openstreetmap.us/iD/release/#map=', |
| 43 | + displayName = "iD", |
| 44 | + buildUrl = function (map) { |
| 45 | + return this.url + [ |
| 46 | + map.getZoom(), |
| 47 | + map.getCenter().wrap().lng, |
| 48 | + map.getCenter().wrap().lat |
| 49 | + ].join('/'); |
| 50 | + }; |
| 51 | + return { |
| 52 | + url: (config && config.url) || url, |
| 53 | + displayName: (config && config.displayName) || displayName, |
| 54 | + buildUrl: (config && config.buildUrl) || buildUrl |
| 55 | + }; |
| 56 | + }, |
| 57 | + Josm: function (config) { |
| 58 | + var url = 'http://127.0.0.1:8111/load_and_zoom', |
| 59 | + timeout = 1000, |
| 60 | + displayName = "JOSM", |
| 61 | + buildUrl = function (map) { |
| 62 | + var bounds = map.getBounds(); |
| 63 | + return this.url + L.Util.getParamString({ |
| 64 | + left: bounds.getNorthWest().lng, |
| 65 | + right: bounds.getSouthEast().lng, |
| 66 | + top: bounds.getNorthWest().lat, |
| 67 | + bottom: bounds.getSouthEast().lat |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + return { |
| 72 | + url: (config && config.url) || url, |
| 73 | + timeout: (config && config.timeout) || timeout, |
| 74 | + displayName: (config && config.displayName) || displayName, |
| 75 | + buildUrl: (config && config.buildUrl) || buildUrl |
| 76 | + }; |
| 77 | + }, |
| 78 | + Potlatch: function (config) { |
| 79 | + var url = 'http://open.mapquestapi.com/dataedit/index_flash.html', |
| 80 | + displayName = "P2", |
| 81 | + buildUrl = function (map) { |
| 82 | + return this.url + L.Util.getParamString({ |
| 83 | + lon: map.getCenter().wrap().lng, |
| 84 | + lat: map.getCenter().wrap().lat, |
| 85 | + zoom: map.getZoom() |
| 86 | + }); |
| 87 | + }; |
| 88 | + return { |
| 89 | + url: (config && config.url) || url, |
| 90 | + displayName: (config && config.displayName) || displayName, |
| 91 | + buildUrl: (config && config.buildUrl) || buildUrl |
| 92 | + }; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + |
| 97 | + // Takes an editor, calls their buildUrl method |
| 98 | + // and opens the url in the browser |
| 99 | + function openRemote (editor) { |
| 100 | + var url = editor.buildUrl(_map), |
| 101 | + w = window.open(url); |
| 102 | + if (editor.timeout) { |
| 103 | + setTimeout(function() {w.close();}, editor.timeout); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + function addEditorToWidget(widgetContainer, editor, text) { |
| 108 | + var editorWidget = L.DomUtil.create('a', "osm-editor", widgetContainer); |
| 109 | + editorWidget.href = "#"; |
| 110 | + editorWidget.innerHTML = text || editor.displayName; |
| 111 | + L.DomEvent.on(editorWidget, "click", function (e) { |
| 112 | + openRemote(editor); |
| 113 | + L.DomEvent.stop(e); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + // Make the EditInOSM widget visible or invisible after each |
| 118 | + // zoom event. |
| 119 | + // |
| 120 | + // configurable by setting the *zoomThreshold* option. |
| 121 | + function showOrHideUI() { |
| 122 | + var zoom = _map.getZoom(); |
| 123 | + if (zoom < _zoomThreshold) { |
| 124 | + L.DomUtil.addClass(_controlContainer, _hideClass); |
| 125 | + } else { |
| 126 | + L.DomUtil.removeClass(_controlContainer, _hideClass); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + L.Control.EditInOSM = L.Control.extend({ |
| 131 | + |
| 132 | + options: { |
| 133 | + position: "topright", |
| 134 | + zoomThreshold: 0, |
| 135 | + widget: "multiButton", |
| 136 | + editors: ["id", "josm"] |
| 137 | + }, |
| 138 | + |
| 139 | + initialize: function (options) { |
| 140 | + var newEditors = [], |
| 141 | + widget, |
| 142 | + widgetSmallName, |
| 143 | + editor, |
| 144 | + editorSmallName; |
| 145 | + |
| 146 | + L.setOptions(this, options); |
| 147 | + |
| 148 | + _zoomThreshold = this.options.zoomThreshold; |
| 149 | + |
| 150 | + widget = this.options.widget; |
| 151 | + widgetSmallName = typeof(widget) === 'string' ? widget.toLowerCase() : ''; |
| 152 | + |
| 153 | + // setup widget from string or object |
| 154 | + if (widgetSmallName === "multibutton") { |
| 155 | + this.options.widget = new _Widgets.MultiButton(); |
| 156 | + } else if (widgetSmallName === "attributionbox") { |
| 157 | + this.options.widget = new _Widgets.AttributionBox(); |
| 158 | + } |
| 159 | + |
| 160 | + // setup editors from strings or objects |
| 161 | + for (var i in this.options.editors) { |
| 162 | + editor = this.options.editors[i], |
| 163 | + editorSmallName = typeof(editor) === "string" ? editor.toLowerCase() : null; |
| 164 | + |
| 165 | + if (editorSmallName === "id") { |
| 166 | + newEditors.push(new _Editors.Id()); |
| 167 | + } else if (editorSmallName === "josm") { |
| 168 | + newEditors.push(new _Editors.Josm()); |
| 169 | + } else if (editorSmallName === "potlatch") { |
| 170 | + newEditors.push(new _Editors.Potlatch()); |
| 171 | + } else { |
| 172 | + newEditors.push(editor); |
| 173 | + } |
| 174 | + } |
| 175 | + this.options.editors = newEditors; |
| 176 | + |
| 177 | + }, |
| 178 | + |
| 179 | + onAdd: function (map) { |
| 180 | + _map = map; |
| 181 | + map.on('zoomend', showOrHideUI); |
| 182 | + |
| 183 | + _controlContainer = L.DomUtil.create('div', this.options.widget.className); |
| 184 | + |
| 185 | + _controlContainer.title = this.options.widget.helpText || ''; |
| 186 | + |
| 187 | + L.DomUtil.create('a', _anchorClass, _controlContainer); |
| 188 | + |
| 189 | + this.options.widget.addEditors(_controlContainer, this.options.editors); |
| 190 | + return _controlContainer; |
| 191 | + }, |
| 192 | + |
| 193 | + onRemove: function (map) { |
| 194 | + map.off('zoomend', this._onZoomEnd); |
| 195 | + } |
| 196 | + |
| 197 | + }); |
| 198 | + |
| 199 | + L.Control.EditInOSM.Widgets = _Widgets; |
| 200 | + L.Control.EditInOSM.Editors = _Editors; |
| 201 | + |
| 202 | + L.Map.addInitHook(function () { |
| 203 | + if (this.options.editInOSMControlOptions) { |
| 204 | + var options = this.options.editInOSMControlOptions || {}; |
| 205 | + this.editInOSMControl = (new L.Control.EditInOSM(options)).addTo(this); |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | +})(L, setTimeout); |
0 commit comments