source: extensions/GMaps/templates/gmaps_dialog_area_choose.tpl @ 7177

Last change on this file since 7177 was 7177, checked in by grum, 14 years ago

Fix bugs on install process ; add street view control management

  • Property svn:executable set to *
File size: 11.9 KB
Line 
1{known_script id="jquery.ui" src=$ROOT_URL|@cat:"themes/default/js/ui/packed/ui.core.packed.js"}
2{known_script id="jquery.ui.dialog" src=$ROOT_URL|@cat:"themes/default/js/ui/packed/ui.dialog.packed.js"}
3{known_script id="maps.google.com/api" src="http://maps.google.com/maps/api/js?sensor=false"}
4
5
6{literal}
7<script type="text/javascript">
8
9/**
10 * include this template in a page and use dialogChooseGMapsBox.show({options}) to display
11 * the dialog box
12 *
13 */
14dialogChooseGMapsBox = function()
15{
16  var bounds = {
17    north:0,
18    south:0,
19    east:0,
20    west:0
21  }
22  var requestId = '';
23
24  var dialogGMapsOptions = {
25      id:'',
26      eventOk:null,
27      cBuilder:null,
28      gMap:null,
29    };
30
31
32  /**
33   * initialize the dialog box
34   */
35  var initDialogBox = function()
36  {
37    initializeMap();
38
39    $.ajax(
40      {
41        type: "POST",
42        url: "plugins/GMaps/gmaps_ajax.php",
43        async: true,
44        data: { ajaxfct:"public.maps.init", category:0 },
45        success: function (msg)
46          {
47            requestId=msg;
48          }
49      }
50    );
51
52    $("#iDialogGMapsChoose")
53    .dialog(
54      {
55        autoOpen: false,
56        resizable: false,
57        width:$(window).width()*0.8,
58        height:$(window).height()*0.8,
59        modal: true,
60        draggable:true,
61        dialogClass: 'gcBgTabSheet gcBorder',
62        title: '{/literal}{"gmaps_choose_geographic_area"|@translate}{literal}',
63        open: function(event, ui)
64        {
65          google.maps.event.trigger(dialogGMapsOptions.gMap, 'resize');
66
67          if(bounds.north!=0 && bounds.south!=0 && bounds.east!=0 && bounds.west!=0)
68          {
69            dialogGMapsOptions.gMap.fitBounds(
70              new google.maps.LatLngBounds(
71                new google.maps.LatLng(bounds.south, bounds.west),
72                new google.maps.LatLng(bounds.north, bounds.east)
73              )
74            );
75          }
76          else
77          {
78            dialogGMapsOptions.gMap.setZoom(2);
79            dialogGMapsOptions.gMap.setCenter(new google.maps.LatLng(0, 0));
80            updateBounds();
81          }
82
83          loadMarkers();
84        },
85        buttons:
86        {
87          '{/literal}{"gmaps_ok"|@translate}{literal}':
88            function()
89            {
90              updateBounds();
91
92              if(dialogGMapsOptions.cBuilder!=null)
93              {
94                setCBuilderItem(dialogGMapsOptions.id, bounds, requestId);
95              }
96
97              if(dialogGMapsOptions.eventOk!=null)
98              {
99                dialogGMapsOptions.eventOk(dialogGMapsOptions.id, bounds, requestId);
100              }
101
102
103              $(this).dialog('close');
104            },
105          '{/literal}{"gmaps_cancel"|@translate}{literal}':
106            function()
107            {
108              $(this).dialog('close');
109            }
110        }
111      }
112    );
113
114  }
115
116
117  /**
118   * initialize the map
119   */
120  var initializeMap = function ()
121  {
122    var latlng=new google.maps.LatLng(0, 0);
123
124    var map = new google.maps.Map($("#iDialogGMapsMap").get(0),
125      {
126        mapTypeId:google.maps.MapTypeId.HYBRID,
127        zoom:2,
128        center:latlng,
129        navigationControl: true,
130        navigationControlOptions:
131          {
132            style:google.maps.NavigationControlStyle.ZOOM_PAN,
133          },
134        scaleControl: true,
135        streetViewControl: false,
136        mapTypeControl:true,
137        mapTypeControlOptions:
138          {
139            style:google.maps.MapTypeControlStyle.DROPDOWN_MENU,
140          },
141        markerTitle:'',
142      }
143    );
144
145    map.markers=new Array();
146    dialogGMapsOptions.gMap=map;
147
148    google.maps.event.addListener(
149      dialogGMapsOptions.gMap,
150      'dragend',
151      function()
152      {
153        updateBounds();
154        loadMarkers();
155      }
156    );
157
158    google.maps.event.addListener(
159      dialogGMapsOptions.gMap,
160      'zoom_changed',
161      function()
162      {
163        updateBounds();
164        loadMarkers();
165      }
166    );
167  }
168
169
170  /**
171   * update the bounds properties from the map bounds
172   */
173  var updateBounds = function ()
174  {
175    var mapBounds=dialogGMapsOptions.gMap.getBounds();
176    bounds.north=mapBounds.getNorthEast().lat();
177    bounds.south=mapBounds.getSouthWest().lat();
178    bounds.east=mapBounds.getNorthEast().lng();
179    bounds.west=mapBounds.getSouthWest().lng();
180  }
181
182  /**
183   * set the request Id
184   */
185  var setRequestId = function(msg)
186  {
187    dialogGMapsOptions.requestId=msg;
188    requestId=msg;
189    dialogGMapsOptions.requestId=msg;
190  }
191
192
193  /**
194   * load markers from the server
195   */
196  function loadMarkers()
197  {
198    datas={
199      requestId:requestId,
200      bounds:{
201          north:bounds.north,
202          east:bounds.east,
203          south:bounds.south,
204          west:bounds.west
205        },
206      width:$(dialogGMapsOptions.gMap.getDiv()).width(),
207      height:$(dialogGMapsOptions.gMap.getDiv()).height(),
208      distanceTreshold:20,
209      levelInfo:0
210    }
211
212    $('#iDialogGMapNfo').html('<img src="./plugins/GrumPluginClasses/icons/processing.gif"><span>{/literal}{"gmaps_loading"|@translate}{literal}</span>');
213    $('#iDialogGMapWaiting').css(
214      {
215        width:$('#iDialogGMapsMap').width()+'px',
216        height:$('#iDialogGMapsMap').height()+'px',
217        top:(1+$('#iDialogGMapsMap').position().top)+'px',
218        left:(1+$('#iDialogGMapsMap').position().left)+'px',
219        display:'block'
220      }
221    );
222
223    $.ajax(
224      {
225        type: "POST",
226        url: "plugins/GMaps/gmaps_ajax.php",
227        async: true,
228        data: { ajaxfct:"public.maps.getMarkers", datas:datas },
229        success:
230          function(msg)
231          {
232            tmp=$.parseJSON(msg);
233            tmp.markers.sort(compareMarkers);
234            applyMarkers(tmp.markers);
235            $('#iDialogGMapNfo').html(tmp.datas.nbPhotos);
236            $('#iDialogGMapWaiting').css('display', 'none');
237          }
238      }
239    );
240  }
241
242  /**
243   * apply markers to map
244   *
245   * @param Array markers : array of markers properties
246   *                        each marker is an object with properties :
247   *                          latitude, longitude, nbImages
248   */
249  function applyMarkers(markers)
250  {
251    /*
252     * deleting markers from the map only if they are not in the new list
253     */
254    if(dialogGMapsOptions.gMap.markers.length>0)
255    {
256      var i=0;
257      while(i<dialogGMapsOptions.gMap.markers.length)
258      {
259        newListIndex=markerInList(dialogGMapsOptions.gMap.markers[i].uniqueId, markers);
260        if(newListIndex==-1)
261        {
262          dialogGMapsOptions.gMap.markers[i].marker.setMap(null);
263          dialogGMapsOptions.gMap.markers.splice(i, 1);
264        }
265        else
266        {
267          markers.splice(newListIndex,1);
268          i++;
269        }
270      }
271    }
272
273    /*
274     * add new markers on the map
275     */
276    for(var i=0;i<markers.length;i++)
277    {
278      var marker = new google.maps.Marker(
279        {
280          position:new google.maps.LatLng(markers[i].lat, markers[i].lng),
281          map: dialogGMapsOptions.gMap,
282          title:markers[i].nbImgTxt
283        }
284      );
285
286      marker.info=markers[i];
287      marker.info.displayed=0;
288
289      dialogGMapsOptions.gMap.markers.push(
290        {
291          marker:marker,
292          uniqueId:markers[i].uniqueId
293        }
294      );
295    }
296  }
297
298  function compareMarkers(m1,m2)
299  {
300    if(m1.uniqueId<m2.uniqueId)
301    {
302      return(-1);
303    }
304    else if(m1.uniqueId<m2.uniqueId)
305    {
306      return(1);
307    }
308    return(0);
309  }
310
311  function markerInList(uniqueId, markerList)
312  {
313    for(var i=0;i<markerList.length;i++)
314    {
315      if(markerList[i].uniqueId==uniqueId) return(i)
316    }
317    return(-1);
318  }
319
320
321  /**
322   * the show() function display and manage a dialog box to choose
323   * a geographic area
324   *
325   * @param options : properties to manage the dialog box
326   *                  - bounds : an object with properties 'north', 'south', 'east'
327   *                             and 'west'
328   *                  - id : a string to identify a DOM object ; this parameter
329   *                         is given to the callback when the OK button is pushed
330   *                  - eventOK : a callback function, with 2 parameters : id of
331   *                              the given DOM object and the bounds object
332   *                  - cBuilder : a criteriaBuilder object
333   *                               if set, the dialog box manage automaticaly
334   *                               the criteria builder interface
335   */
336  this.show = function (options)
337  {
338    showDialog(options);
339  }
340
341  /**
342   * private function used to show the dialog box
343   */
344  var showDialog = function(options)
345  {
346    if(options.bounds!=null)
347    {
348      bounds=options.bounds;
349    }
350    else
351    {
352      bounds={
353        north:0,
354        south:0,
355        east:0,
356        west:0
357      };
358    }
359
360    if(options.id!=null)
361    {
362      dialogGMapsOptions.id=options.id;
363    }
364    else
365    {
366      dialogGMapsOptions.id='';
367    }
368
369    if(options.eventOk!=null)
370    {
371      dialogGMapsOptions.eventOk=options.eventOk;
372    }
373
374    if(options.cBuilder!=null)
375    {
376      dialogGMapsOptions.cBuilder=options.cBuilder;
377      dialogGMapsOptions.cBuilder.doAction('setOptions',
378        {
379          onEdit:function (e) { editGA(e.data); },
380          onDelete:function (e) { deleteGA(e.data); },
381        }
382      );
383    }
384
385    $("#iDialogGMapsChoose").dialog('open');
386  }
387
388
389
390  /**
391   * manage the 'edit' button from criteria builder interface
392   * @param String itemId : the itemId
393   */
394  var editGA = function (itemId)
395  {
396    extraData=dialogGMapsOptions.cBuilder.doAction('getExtraData', itemId);
397    showDialog(
398      {
399        id:itemId,
400        bounds:extraData.param.bounds,
401        requestId:extraData.param.requestId
402      }
403    );
404  }
405
406  /**
407   * manage the 'delete' button from criteria builder interface
408   * @param String itemId : the itemId
409   */
410  var deleteGA = function (itemId)
411  {
412    dialogGMapsOptions.cBuilder.doAction('delete', itemId);
413  }
414
415  /**
416   * set the content for the cBuilder item
417   */
418  var setCBuilderItem = function(id, bounds)
419  {
420
421    var content="<div>{/literal}{'gmaps_geographic_coords_between'|@translate}{literal}<br>";
422    content+="<span style='margin-left:20px;'>&bull;&nbsp;"+DDtoDMS(bounds.south)+" {/literal}{'gmaps_south'|@translate} {'gmaps_and'|@translate} "+DDtoDMS(bounds.north)+" {'gmaps_north'|@translate}{literal}</span><br>";
423
424    if(bounds.east<bounds.west)
425    {
426      content+="<span style='margin-left:20px;'>&bull;&nbsp;"+DDtoDMS(bounds.east)+" {/literal}{'gmaps_east'|@translate} {'gmaps_and'|@translate} "+DDtoDMS(bounds.west)+" {'gmaps_west'|@translate}{literal}</span>";
427    }
428    else
429    {
430      content+="<span style='margin-left:20px;'>&bull;&nbsp;"+DDtoDMS(bounds.west)+" {/literal}{'gmaps_west'|@translate} {'gmaps_and'|@translate} "+DDtoDMS(bounds.east)+" {'gmaps_east'|@translate}{literal}</span>";
431    }
432    content+="</div>";
433
434
435    if(id=='')
436    {
437      //no id:add a new item in the list
438      dialogGMapsOptions.cBuilder.doAction('add', content, criteriaBuilder.makeExtendedData('GMaps', {bounds:$.extend({}, bounds), requestId:requestId } ) );
439    }
440    else
441    {
442      // update item
443      dialogGMapsOptions.cBuilder.doAction('edit', id,  content, criteriaBuilder.makeExtendedData('GMaps', {bounds:$.extend({}, bounds), requestId:requestId } ) );
444    }
445  }
446
447  /**
448   * convert a decimal degree to D°M'S'
449   * note : negative values are converted in positive values, assuming North/South
450   * and East/West are managed outside this function
451   *
452   * @param Float $value : the value to convert
453   * @return String : value converted in DMS
454   */
455  var DDtoDMS = function (value)
456  {
457    value=Math.abs(value);
458    degrees=Math.floor(value);
459    tmp=(value-degrees)*60;
460    minutes=Math.floor(tmp);
461    seconds=Math.round((tmp-minutes)*6000)/100;
462    return(degrees+"° "+minutes+"' "+seconds+'"');
463  }
464
465  initDialogBox();
466}
467
468
469</script>
470{/literal}
471
472<div id="iDialogGMapsChoose" style='display:none;'>
473  <div id='iDialogGMapNfo'>&nbsp;</div>
474  <div id='iDialogGMapsMap' style='width:95%; height:95%;margin-left:auto;margin-right:auto;border:1px solid;'></div>
475  <div id='iDialogGMapWaiting' style="display:none;"></div>
476</div>
477
478
Note: See TracBrowser for help on using the repository browser.