| 1 | | var widgets = []; |
|---|
| 2 | | var current_pos = null; |
|---|
| 3 | | var position_marker = null; |
|---|
| 4 | | var widgets_mapstrations = []; |
|---|
| 5 | | |
|---|
| 6 | | function init_position_widget(widget_id, mapstration) |
|---|
| 7 | | { |
|---|
| 8 | | jQuery('#' + widget_id).each(function(i,w){ |
|---|
| 9 | | var widget = jQuery(w); |
|---|
| 10 | | |
|---|
| 11 | | widgets[w.id] = widget; |
|---|
| 12 | | |
|---|
| 13 | | var geodata_btn = jQuery('#' + widget_id + '_geodata_btn', widget); |
|---|
| 14 | | var indicator = jQuery('#' + widget_id + '_indicator', widget); |
|---|
| 15 | | var status_box = jQuery('#' + widget_id + '_status_box', widget); |
|---|
| 16 | | |
|---|
| 17 | | var backend_url = jQuery('input.position_widget_backend_url',widget).attr('value'); |
|---|
| 18 | | var backend_service = jQuery('input.position_widget_backend_service',widget).attr('value'); |
|---|
| 19 | | |
|---|
| 20 | | var input_data = {}; |
|---|
| | 1 | // var widgets = []; |
|---|
| | 2 | // var current_pos = []; |
|---|
| | 3 | // var position_marker = []; |
|---|
| | 4 | // var alternative_markers = []; |
|---|
| | 5 | // var alternative_markers_visible = []; |
|---|
| | 6 | // var widgets_mapstractions = []; |
|---|
| | 7 | // var widget_options = []; |
|---|
| | 8 | |
|---|
| | 9 | jQuery.fn.extend({ |
|---|
| | 10 | dm2_position_widget: function(mapstraction, options) { |
|---|
| | 11 | options = jQuery.extend(jQuery.midcom_helper_datamanager2_widget_position.defaults, options); |
|---|
| | 12 | return this.each(function() { |
|---|
| | 13 | return new jQuery.midcom_helper_datamanager2_widget_position(this, mapstraction, options); |
|---|
| | 14 | }); |
|---|
| | 15 | }, |
|---|
| | 16 | dm2_pw_new_position: function(point) { |
|---|
| | 17 | return this.trigger("new_position",[point]); |
|---|
| | 18 | }, |
|---|
| | 19 | dm2_pw_clear_alternative_markers: function() { |
|---|
| | 20 | return this.trigger("clear_alternative_markers",[]); |
|---|
| | 21 | }, |
|---|
| | 22 | dm2_pw_set_marker: function(label, info) { |
|---|
| | 23 | return this.trigger("set_marker",[label, info]); |
|---|
| | 24 | }, |
|---|
| | 25 | dm2_pw_init_current_pos: function(lat, lon) |
|---|
| | 26 | { |
|---|
| | 27 | return this.trigger("init_current_pos",[lat, lon]); |
|---|
| | 28 | }, |
|---|
| | 29 | dm2_pw_position_map_to_current: function() { |
|---|
| | 30 | return this.trigger("position_map_to_current",[]); |
|---|
| | 31 | } |
|---|
| | 32 | }); |
|---|
| | 33 | |
|---|
| | 34 | jQuery.midcom_helper_datamanager2_widget_position = function(widget_block, mapstraction, options) { |
|---|
| | 35 | |
|---|
| | 36 | var widget = jQuery(widget_block); |
|---|
| | 37 | var widget_id = widget.attr('id'); |
|---|
| | 38 | |
|---|
| | 39 | mapstraction.addMapTypeControls(); |
|---|
| | 40 | mapstraction.addEventListener('click', new_position); |
|---|
| | 41 | |
|---|
| | 42 | var geodata_btn = jQuery('#' + widget_id + '_geodata_btn', widget); |
|---|
| | 43 | var indicator = jQuery('#' + widget_id + '_indicator', widget); |
|---|
| | 44 | |
|---|
| | 45 | var revgeodata_btn = jQuery('#' + widget_id + '_revgeodata_btn', widget); |
|---|
| | 46 | var revindicator = jQuery('#' + widget_id + '_revindicator', widget); |
|---|
| | 47 | |
|---|
| | 48 | var actions_cam = jQuery('#' + widget_id + '_position_widget_action_cam', widget); |
|---|
| | 49 | |
|---|
| | 50 | var status_box = jQuery('#' + widget_id + '_status_box', widget); |
|---|
| | 51 | |
|---|
| | 52 | var current_pos_icon_url = MIDCOM_STATIC_URL + '/midcom.helper.datamanager2/position/current_position_marker.png' |
|---|
| | 53 | var backend_url = jQuery('input.position_widget_backend_url',widget).attr('value'); |
|---|
| | 54 | var backend_service = jQuery('input.position_widget_backend_service',widget).attr('value'); |
|---|
| | 55 | var input_data = {}; |
|---|
| | 56 | var current_pos = null; |
|---|
| | 57 | var position_marker = null; |
|---|
| | 58 | var alternative_markers = null; |
|---|
| | 59 | var alternative_markers_visible = true; |
|---|
| | 60 | |
|---|
| | 61 | widget.bind("new_position", function(event, point){ |
|---|
| | 62 | new_position(point); |
|---|
| | 63 | }).bind("clear_alternative_markers", function(event, point){ |
|---|
| | 64 | clear_alternative_markers(); |
|---|
| | 65 | }).bind("set_marker", function(event, label, info){ |
|---|
| | 66 | set_marker(label, info); |
|---|
| | 67 | }).bind("init_current_pos", function(event, lat, lon){ |
|---|
| | 68 | init_current_pos(lat, lon); |
|---|
| | 69 | }).bind("position_map_to_current", function(event){ |
|---|
| | 70 | position_map_to_current(); |
|---|
| | 71 | }); |
|---|
| | 72 | |
|---|
| | 73 | indicator.hide(); |
|---|
| | 74 | revindicator.hide(); |
|---|
| | 75 | |
|---|
| | 76 | geodata_btn.bind('click', function(e){ |
|---|
| | 77 | refresh_geodata(); |
|---|
| | 78 | geodata_btn.hide(); |
|---|
| | 79 | indicator.show(); |
|---|
| | 80 | }); |
|---|
| | 81 | revgeodata_btn.bind('click', function(e){ |
|---|
| | 82 | var lat = jQuery('#' + input_data['latitude']['id']).attr('value'); |
|---|
| | 83 | var lon = jQuery('#' + input_data['longitude']['id']).attr('value'); |
|---|
| | 84 | |
|---|
| | 85 | new_position(new LatLonPoint(lat,lon)); |
|---|
| | 86 | |
|---|
| | 87 | revgeodata_btn.hide(); |
|---|
| | 88 | revindicator.show(); |
|---|
| | 89 | }); |
|---|
| | 90 | |
|---|
| | 91 | actions_cam.bind('click', function(e){ |
|---|
| | 92 | clear_alternative_markers(); |
|---|
| | 93 | }); |
|---|
| | 94 | |
|---|
| | 95 | jQuery('.position_widget_input',widget).each(function(i, o){ |
|---|
| | 96 | var jqo = jQuery(o); |
|---|
| | 97 | var key = get_key_name(jqo.attr('name')); |
|---|
| | 98 | input_data[key] = { |
|---|
| | 99 | id: jqo.attr('id'), |
|---|
| | 100 | value: jqo.attr('value') |
|---|
| | 101 | }; |
|---|
| | 102 | |
|---|
| | 103 | if (input_data[key]['value'] == undefined) |
|---|
| | 104 | { |
|---|
| | 105 | input_data[key]['value'] = ''; |
|---|
| | 106 | } |
|---|
| | 107 | }); |
|---|
| | 108 | |
|---|
| | 109 | function disable_tabs() |
|---|
| | 110 | { |
|---|
| | 111 | widget.disableTab(1); |
|---|
| | 112 | widget.disableTab(2); |
|---|
| | 113 | widget.disableTab(3); |
|---|
| | 114 | } |
|---|
| | 115 | function enable_tabs() |
|---|
| | 116 | { |
|---|
| | 117 | widget.enableTab(1); |
|---|
| | 118 | widget.enableTab(2); |
|---|
| | 119 | widget.enableTab(3); |
|---|
| | 120 | } |
|---|
| | 121 | |
|---|
| | 122 | function new_position(point) |
|---|
| | 123 | { |
|---|
| | 124 | current_pos = point; |
|---|
| | 125 | |
|---|
| | 126 | jQuery('#' + input_data['latitude']['id']).attr('value', current_pos.lat); |
|---|
| | 127 | jQuery('#' + input_data['longitude']['id']).attr('value', current_pos.lon); |
|---|
| | 128 | |
|---|
| | 129 | set_marker('Current position', ''); |
|---|
| | 130 | get_reversed_geodata(); |
|---|
| | 131 | } |
|---|
| | 132 | |
|---|
| | 133 | function get_reversed_geodata() |
|---|
| | 134 | { |
|---|
| | 135 | disable_tabs(); |
|---|
| | 136 | clear_alternative_markers(); |
|---|
| | 137 | |
|---|
| | 138 | var opts_str = '?'; |
|---|
| | 139 | jQuery.each(options, function(key,value){ |
|---|
| | 140 | opts_str += 'options[' + key + ']=' + value + '&'; |
|---|
| | 141 | }); |
|---|
| | 142 | |
|---|
| | 143 | opts_str = opts_str.substr(0,opts_str.length-1); |
|---|
| | 144 | |
|---|
| | 145 | var get_params = { |
|---|
| | 146 | service: backend_service, |
|---|
| | 147 | dir: 'reverse', |
|---|
| | 148 | latitude: current_pos.lat, |
|---|
| | 149 | longitude: current_pos.lon |
|---|
| | 150 | }; |
|---|
| | 151 | |
|---|
| | 152 | jQuery.ajax({ |
|---|
| | 153 | type: "GET", |
|---|
| | 154 | url: backend_url + opts_str, |
|---|
| | 155 | data: get_params, |
|---|
| | 156 | dataType: "xml", |
|---|
| | 157 | error: function(request, type, expobj){ |
|---|
| | 158 | parse_error(request.responseText); |
|---|
| | 159 | }, |
|---|
| | 160 | success: function(data){ |
|---|
| | 161 | var parsed = parse_response(data); |
|---|
| | 162 | update_widget_inputs(parsed[0]); |
|---|
| | 163 | handle_alternatives(parsed); |
|---|
| | 164 | } |
|---|
| | 165 | }); |
|---|
| | 166 | |
|---|
| | 167 | function parse_error(error_string) |
|---|
| | 168 | { |
|---|
| | 169 | indicator.hide(); |
|---|
| | 170 | revindicator.hide(); |
|---|
| | 171 | geodata_btn.show(); |
|---|
| | 172 | revgeodata_btn.show(); |
|---|
| | 173 | enable_tabs(); |
|---|
| | 174 | |
|---|
| | 175 | status_box.html(error_string); |
|---|
| | 176 | } |
|---|
| | 177 | |
|---|
| | 178 | function parse_response(data) |
|---|
| | 179 | { |
|---|
| | 180 | status_box.html(''); |
|---|
| | 181 | |
|---|
| | 182 | var results = []; |
|---|
| | 183 | jQuery('position',data).each(function(idx) { |
|---|
| | 184 | var rel_this = jQuery(this); |
|---|
| | 185 | |
|---|
| | 186 | results[idx] = { |
|---|
| | 187 | latitude: rel_this.find("latitude").text(), |
|---|
| | 188 | longitude: rel_this.find("longitude").text(), |
|---|
| | 189 | distance: { |
|---|
| | 190 | meters: rel_this.find("distance").find("meters").text(), |
|---|
| | 191 | bearing: rel_this.find("distance").find("bearing").text() |
|---|
| | 192 | }, |
|---|
| | 193 | city: rel_this.find("city").text(), |
|---|
| | 194 | region: rel_this.find("region").text(), |
|---|
| | 195 | country: rel_this.find("country").text(), |
|---|
| | 196 | alternate_names: rel_this.find("alternate_names").text(), |
|---|
| | 197 | accuracy: rel_this.find("accuracy").text() |
|---|
| | 198 | }; |
|---|
| | 199 | }); |
|---|
| | 200 | |
|---|
| | 201 | return results; |
|---|
| | 202 | } |
|---|
| | 203 | } |
|---|
| | 204 | |
|---|
| | 205 | function refresh_geodata() |
|---|
| | 206 | { |
|---|
| | 207 | disable_tabs(); |
|---|
| | 208 | clear_alternative_markers(); |
|---|
| | 209 | |
|---|
| | 210 | var opts_str = '?'; |
|---|
| | 211 | var opts_len = options.length; |
|---|
| | 212 | var i = 1; |
|---|
| | 213 | jQuery.each(options, function(key,value){ |
|---|
| | 214 | if (i < opts_len) |
|---|
| | 215 | { |
|---|
| | 216 | opts_str += '&'; |
|---|
| | 217 | } |
|---|
| | 218 | opts_str += 'options[' + key + ']=' + value; |
|---|
| | 219 | i++; |
|---|
| | 220 | }); |
|---|
| | 221 | |
|---|
| | 222 | var get_params = { |
|---|
| | 223 | service: backend_service, |
|---|
| | 224 | }; |
|---|
| 44 | | jQuery('#' + input_data['latitude']['id']).attr('value',current_pos.lat); |
|---|
| 45 | | jQuery('#' + input_data['longitude']['id']).attr('value',current_pos.lon); |
|---|
| 46 | | |
|---|
| 47 | | set_marker('Current position','',widget_id); |
|---|
| 48 | | get_reversed_geodata(); |
|---|
| 49 | | } |
|---|
| 50 | | |
|---|
| | 267 | status_box.html(error_string); |
|---|
| | 268 | } |
|---|
| | 269 | |
|---|
| | 270 | function parse_response(data) |
|---|
| | 271 | { |
|---|
| | 272 | status_box.html(''); |
|---|
| | 273 | |
|---|
| | 274 | var results = []; |
|---|
| | 275 | jQuery('position',data).each(function(idx) { |
|---|
| | 276 | var rel_this = jQuery(this); |
|---|
| | 277 | |
|---|
| | 278 | results[idx] = { |
|---|
| | 279 | latitude: rel_this.find("latitude").text(), |
|---|
| | 280 | longitude: rel_this.find("longitude").text(), |
|---|
| | 281 | distance: { |
|---|
| | 282 | meters: rel_this.find("distance").find("meters").text(), |
|---|
| | 283 | bearing: rel_this.find("distance").find("bearing").text() |
|---|
| | 284 | }, |
|---|
| | 285 | accuracy: rel_this.find("accuracy").text(), |
|---|
| | 286 | city: rel_this.find("city").text(), |
|---|
| | 287 | region: rel_this.find("region").text(), |
|---|
| | 288 | country: rel_this.find("country").text(), |
|---|
| | 289 | alternate_names: rel_this.find("alternate_names").text(), |
|---|
| | 290 | postalcode: rel_this.find("postalcode").text() |
|---|
| | 291 | }; |
|---|
| | 292 | }); |
|---|
| | 293 | |
|---|
| | 294 | return results; |
|---|
| | 295 | } |
|---|
| | 296 | } |
|---|
| | 297 | |
|---|
| | 298 | function update_widget(location_data) |
|---|
| | 299 | { |
|---|
| | 300 | update_widget_inputs(location_data); |
|---|
| | 301 | |
|---|
| | 302 | current_pos = new LatLonPoint(location_data['latitude'],location_data['longitude']); |
|---|
| | 303 | |
|---|
| | 304 | var info = location_data['city'] + ", " + location_data['country'] + ", " + location_data['postalcode']; |
|---|
| | 305 | var label = 'Current position'; |
|---|
| | 306 | if (input_data['description']) |
|---|
| | 307 | { |
|---|
| | 308 | label = input_data['description']; |
|---|
| | 309 | } |
|---|
| | 310 | else if (location_data['description']) |
|---|
| | 311 | { |
|---|
| | 312 | label = location_data['description']; |
|---|
| | 313 | } |
|---|
| | 314 | |
|---|
| | 315 | set_marker(label, info); |
|---|
| | 316 | } |
|---|
| | 317 | |
|---|
| | 318 | function update_widget_inputs(location_data) |
|---|
| | 319 | { |
|---|
| | 320 | enable_tabs(); |
|---|
| 52 | | |
|---|
| 53 | | geodata_btn.bind('click', function(e){ |
|---|
| 54 | | refresh_geodata(); |
|---|
| 55 | | geodata_btn.hide(); |
|---|
| 56 | | indicator.show(); |
|---|
| 57 | | }); |
|---|
| 58 | | |
|---|
| 59 | | function get_reversed_geodata() |
|---|
| 60 | | { |
|---|
| 61 | | var get_params = { |
|---|
| 62 | | service: backend_service, |
|---|
| 63 | | dir: 'reverse', |
|---|
| 64 | | latitude: current_pos.lat, |
|---|
| 65 | | longitude: current_pos.lon |
|---|
| 66 | | }; |
|---|
| 67 | | |
|---|
| 68 | | jQuery.ajax({ |
|---|
| 69 | | type: "GET", |
|---|
| 70 | | url: backend_url, |
|---|
| 71 | | data: get_params, |
|---|
| 72 | | dataType: "xml", |
|---|
| 73 | | error: function(request, type, expobj){ |
|---|
| 74 | | parse_error(request.responseText); |
|---|
| 75 | | }, |
|---|
| 76 | | success: function(data){ |
|---|
| 77 | | var parsed = parse_response(data); |
|---|
| 78 | | update_widget_inputs(parsed); |
|---|
| | 322 | revindicator.hide(); |
|---|
| | 323 | geodata_btn.show(); |
|---|
| | 324 | revgeodata_btn.show(); |
|---|
| | 325 | |
|---|
| | 326 | jQuery.each(location_data, function(key,value){ |
|---|
| | 327 | if (input_data[key]) |
|---|
| | 328 | { |
|---|
| | 329 | jQuery('#' + input_data[key]['id']).attr('value',value); |
|---|
| | 330 | } |
|---|
| | 331 | }); |
|---|
| | 332 | } |
|---|
| | 333 | |
|---|
| | 334 | function get_key_name(key) |
|---|
| | 335 | { |
|---|
| | 336 | var re = /widget_([a-z]*)_([a-z]{5,11})_([a-z]*)/; |
|---|
| | 337 | var reg = re.exec(key); |
|---|
| | 338 | |
|---|
| | 339 | return reg[3]; |
|---|
| | 340 | } |
|---|
| | 341 | |
|---|
| | 342 | function handle_alternatives(items) |
|---|
| | 343 | { |
|---|
| | 344 | var total = items.length; |
|---|
| | 345 | for (var i=1; i<total; i++) |
|---|
| | 346 | { |
|---|
| | 347 | var point = new LatLonPoint(items[i]['latitude'],items[i]['longitude']); |
|---|
| | 348 | var info = items[i]['city'] + ", " + items[i]['country'] + ", " + items[i]['postalcode']; |
|---|
| | 349 | set_alternative_marker('Alternative position', info, point); |
|---|
| | 350 | } |
|---|
| | 351 | } |
|---|
| | 352 | |
|---|
| | 353 | function init_current_pos(lat, lon) |
|---|
| | 354 | { |
|---|
| | 355 | current_pos = new LatLonPoint(lat,lon); |
|---|
| | 356 | set_marker('Current position', ''); |
|---|
| | 357 | } |
|---|
| | 358 | |
|---|
| | 359 | function set_marker(label, info) |
|---|
| | 360 | { |
|---|
| | 361 | if (position_marker != null) |
|---|
| | 362 | { |
|---|
| | 363 | mapstraction.removeMarker(position_marker); |
|---|
| | 364 | } |
|---|
| | 365 | |
|---|
| | 366 | position_marker = new Marker(current_pos); |
|---|
| | 367 | position_marker.setIcon(current_pos_icon_url); |
|---|
| | 368 | |
|---|
| | 369 | //position_marker.draggable = true; |
|---|
| | 370 | //position_marker.draggable_end_event = function(marker){var p = marker.getPoint(); alert(p.lat);}; |
|---|
| | 371 | //position_marker.addEventListener('dragend', function(){alert('drop');}); |
|---|
| | 372 | // jQuery.extend(this, function(){ |
|---|
| | 373 | // var point = position_marker.getPoint(); |
|---|
| | 374 | // dm2_pw_new_position(point); |
|---|
| | 375 | // }); |
|---|
| | 376 | |
|---|
| | 377 | if (label != undefined) |
|---|
| | 378 | { |
|---|
| | 379 | position_marker.setLabel(label); |
|---|
| | 380 | } |
|---|
| | 381 | |
|---|
| | 382 | if ( info != undefined |
|---|
| | 383 | && info != '') |
|---|
| | 384 | { |
|---|
| | 385 | position_marker.setInfoBubble(info); |
|---|
| | 386 | } |
|---|
| | 387 | |
|---|
| | 388 | mapstraction.addMarker(position_marker); |
|---|
| | 389 | |
|---|
| | 390 | // if (info != undefined) |
|---|
| | 391 | // { |
|---|
| | 392 | // position_marker[widget_id].openBubble(); |
|---|
| | 393 | // } |
|---|
| | 394 | } |
|---|
| | 395 | |
|---|
| | 396 | function set_alternative_marker(label, info, pos) |
|---|
| | 397 | { |
|---|
| | 398 | if (! alternative_markers) |
|---|
| | 399 | { |
|---|
| | 400 | alternative_markers = []; |
|---|
| | 401 | } |
|---|
| | 402 | |
|---|
| | 403 | var last_key = alternative_markers.push( new Marker(pos) ); |
|---|
| | 404 | |
|---|
| | 405 | if (label != undefined) |
|---|
| | 406 | { |
|---|
| | 407 | alternative_markers[last_key-1].setLabel(label); |
|---|
| | 408 | } |
|---|
| | 409 | |
|---|
| | 410 | if ( info != undefined |
|---|
| | 411 | && info != '') |
|---|
| | 412 | { |
|---|
| | 413 | alternative_markers[last_key-1].setInfoBubble(info); |
|---|
| | 414 | } |
|---|
| | 415 | |
|---|
| | 416 | mapstraction.addMarker(alternative_markers[last_key-1]); |
|---|
| | 417 | |
|---|
| | 418 | // if (info != undefined) |
|---|
| | 419 | // { |
|---|
| | 420 | // alternative_markers[widget_id][last_key-1].openBubble(); |
|---|
| | 421 | // } |
|---|
| | 422 | } |
|---|
| | 423 | |
|---|
| | 424 | function clear_alternative_markers() |
|---|
| | 425 | { |
|---|
| | 426 | if ( alternative_markers |
|---|
| | 427 | && alternative_markers.length > 0) |
|---|
| | 428 | { |
|---|
| | 429 | var length = alternative_markers.length; |
|---|
| | 430 | for (var i=0; i<length; i++) |
|---|
| | 431 | { |
|---|
| | 432 | mapstraction.removeMarker(alternative_markers[i]); |
|---|
| | 433 | } |
|---|
| | 434 | } |
|---|
| | 435 | } |
|---|
| | 436 | |
|---|
| | 437 | function toggle_alternative_markers() |
|---|
| | 438 | { |
|---|
| | 439 | if (! alternative_markers_visible) |
|---|
| | 440 | { |
|---|
| | 441 | alternative_markers_visible = true; |
|---|
| | 442 | |
|---|
| | 443 | if ( alternative_markers |
|---|
| | 444 | && alternative_markers.length > 0) |
|---|
| | 445 | { |
|---|
| | 446 | var length = alternative_markers.length; |
|---|
| | 447 | for (var i=0; i<length; i++) |
|---|
| | 448 | { |
|---|
| | 449 | alternative_markers[i].show(); |
|---|
| 129 | | else |
|---|
| 130 | | { |
|---|
| 131 | | get_params[key] = input_data[key]['value']; |
|---|
| 132 | | } |
|---|
| 133 | | }); |
|---|
| 134 | | |
|---|
| 135 | | // jQuery.each(input_data, function(i,o){ |
|---|
| 136 | | // if (o['value'] != '') |
|---|
| 137 | | // { |
|---|
| 138 | | // get_params[i] = o['value']; |
|---|
| 139 | | // } |
|---|
| 140 | | // }); |
|---|
| 141 | | |
|---|
| 142 | | jQuery.ajax({ |
|---|
| 143 | | type: "GET", |
|---|
| 144 | | url: backend_url, |
|---|
| 145 | | data: get_params, |
|---|
| 146 | | dataType: "xml", |
|---|
| 147 | | error: function(request, type, expobj){ |
|---|
| 148 | | parse_error(request.responseText); |
|---|
| 149 | | }, |
|---|
| 150 | | success: function(data){ |
|---|
| 151 | | var parsed = parse_response(data); |
|---|
| 152 | | update_widget(parsed); |
|---|
| 153 | | } |
|---|
| 154 | | }); |
|---|
| 155 | | |
|---|
| 156 | | function parse_error(error_string) |
|---|
| 157 | | { |
|---|
| 158 | | indicator.hide(); |
|---|
| 159 | | geodata_btn.show(); |
|---|
| 160 | | |
|---|
| 161 | | status_box.html(error_string); |
|---|
| 162 | | } |
|---|
| 163 | | |
|---|
| 164 | | function parse_response(data) |
|---|
| 165 | | { |
|---|
| 166 | | status_box.html(''); |
|---|
| 167 | | |
|---|
| 168 | | var results = []; |
|---|
| 169 | | jQuery('position',data).each(function(idx) { |
|---|
| 170 | | var rel_this = jQuery(this); |
|---|
| 171 | | |
|---|
| 172 | | results[idx] = { |
|---|
| 173 | | latitude: rel_this.find("latitude").text(), |
|---|
| 174 | | longitude: rel_this.find("longitude").text(), |
|---|
| 175 | | accuracy: rel_this.find("accuracy").text(), |
|---|
| 176 | | city: rel_this.find("city").text(), |
|---|
| 177 | | region: rel_this.find("region").text(), |
|---|
| 178 | | country: rel_this.find("country").text(), |
|---|
| 179 | | postalcode: rel_this.find("postalcode").text() |
|---|
| 180 | | }; |
|---|
| 181 | | }); |
|---|
| 182 | | |
|---|
| 183 | | return results[0]; |
|---|
| 184 | | } |
|---|
| 185 | | } |
|---|
| 186 | | |
|---|
| 187 | | function update_widget(location_data) |
|---|
| 188 | | { |
|---|
| 189 | | indicator.hide(); |
|---|
| 190 | | geodata_btn.show(); |
|---|
| 191 | | |
|---|
| 192 | | update_widget_inputs(location_data); |
|---|
| 193 | | |
|---|
| 194 | | current_pos = new LatLonPoint(location_data['latitude'],location_data['longitude']); |
|---|
| 195 | | |
|---|
| 196 | | var info = location_data['city'] + ", " + location_data['country'] + ", " + location_data['postalcode']; |
|---|
| 197 | | var label = 'Current position'; |
|---|
| 198 | | if (input_data['description']) |
|---|
| 199 | | { |
|---|
| 200 | | label = input_data['description']; |
|---|
| 201 | | } |
|---|
| 202 | | else if (location_data['description']) |
|---|
| 203 | | { |
|---|
| 204 | | label = location_data['description']; |
|---|
| 205 | | } |
|---|
| 206 | | |
|---|
| 207 | | set_marker(label, info, widget_id); |
|---|
| 208 | | } |
|---|
| 209 | | |
|---|
| 210 | | function update_widget_inputs(location_data) |
|---|
| 211 | | { |
|---|
| 212 | | jQuery.each(location_data, function(key,value){ |
|---|
| 213 | | if (input_data[key]) |
|---|
| 214 | | { |
|---|
| 215 | | jQuery('#' + input_data[key]['id']).attr('value',value); |
|---|
| 216 | | } |
|---|
| 217 | | else |
|---|
| 218 | | { |
|---|
| 219 | | //console.log("Key: "+key+" not found in items."); |
|---|
| 220 | | } |
|---|
| 221 | | }); |
|---|
| 222 | | } |
|---|
| 223 | | |
|---|
| 224 | | function get_key_name(key) |
|---|
| 225 | | { |
|---|
| 226 | | //var re = /^\s*(\s*)_(\w{5,11})_(\s*)$/; |
|---|
| 227 | | var re = /widget_([a-z]*)_([a-z]{5,11})_([a-z]*)/; |
|---|
| 228 | | var reg = re.exec(key); |
|---|
| 229 | | // console.log("Reg: "+reg); |
|---|
| 230 | | // console.log("Reg.length: "+reg.length); |
|---|
| 231 | | // console.log("Reg[0]: "+reg[0]+" Reg[1]: "+reg[1]+" Reg[2]: "+reg[2]); |
|---|
| 232 | | |
|---|
| 233 | | return reg[3]; |
|---|
| 234 | | } |
|---|
| 235 | | |
|---|
| 236 | | }); |
|---|
| 237 | | } |
|---|
| 238 | | |
|---|
| 239 | | function init_current_pos(widget_id,lat,lon) |
|---|
| 240 | | { |
|---|
| 241 | | current_pos = new LatLonPoint(lat,lon); |
|---|
| 242 | | set_marker('Current position','',widget_id); |
|---|
| 243 | | } |
|---|
| 244 | | |
|---|
| 245 | | function set_marker(label, info, widget_id) |
|---|
| 246 | | { |
|---|
| 247 | | // console.log("set_marker label: "+label+" info: "+info+" widget_id: "+widget_id); |
|---|
| 248 | | |
|---|
| 249 | | if (position_marker != null) |
|---|
| 250 | | { |
|---|
| 251 | | widgets_mapstrations[widget_id].removeMarker(position_marker); |
|---|
| 252 | | } |
|---|
| 253 | | |
|---|
| 254 | | position_marker = new Marker(current_pos); |
|---|
| 255 | | |
|---|
| 256 | | if (label != undefined) |
|---|
| 257 | | { |
|---|
| 258 | | position_marker.setLabel(label); |
|---|
| 259 | | } |
|---|
| 260 | | |
|---|
| 261 | | if ( info != undefined |
|---|
| 262 | | && info != '') |
|---|
| 263 | | { |
|---|
| 264 | | position_marker.setInfoBubble(info); |
|---|
| 265 | | } |
|---|
| 266 | | |
|---|
| 267 | | widgets_mapstrations[widget_id].addMarker(position_marker); |
|---|
| 268 | | |
|---|
| 269 | | // if (info != undefined) |
|---|
| 270 | | // { |
|---|
| 271 | | // position_marker.openBubble(); |
|---|
| 272 | | // } |
|---|
| 273 | | } |
|---|
| 274 | | |
|---|
| 275 | | function position_map_to_current(widget_id) |
|---|
| 276 | | { |
|---|
| 277 | | //console.log("position_map_to_current widget_id: "+widget_id); |
|---|
| 278 | | //console.log("current_pos: "+current_pos); |
|---|
| 279 | | |
|---|
| 280 | | if (current_pos != null) |
|---|
| 281 | | { |
|---|
| 282 | | widgets_mapstrations[widget_id].resizeTo(400,280); |
|---|
| 283 | | widgets_mapstrations[widget_id].setCenterAndZoom(current_pos, 13); |
|---|
| 284 | | widgets_mapstrations[widget_id].resizeTo(420,300); |
|---|
| 285 | | } |
|---|
| 286 | | } |
|---|
| | 465 | } |
|---|
| | 466 | } |
|---|
| | 467 | } |
|---|
| | 468 | |
|---|
| | 469 | function position_map_to_current() |
|---|
| | 470 | { |
|---|
| | 471 | if (current_pos != null) |
|---|
| | 472 | { |
|---|
| | 473 | mapstraction.resizeTo(400,280); |
|---|
| | 474 | mapstraction.setCenterAndZoom(current_pos, 13); |
|---|
| | 475 | mapstraction.resizeTo(420,300); |
|---|
| | 476 | } |
|---|
| | 477 | } |
|---|
| | 478 | |
|---|
| | 479 | }; |
|---|
| | 480 | |
|---|
| | 481 | jQuery.midcom_helper_datamanager2_widget_position.defaults = { |
|---|
| | 482 | maxRows: 1, |
|---|
| | 483 | radius: 5 |
|---|
| | 484 | }; |
|---|